Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[crypto] Construct OCSP check URI
Signed-off-by: Michael Brown <mcb30@ipxe.org>
  • Loading branch information
mcb30 committed May 21, 2012
1 parent 57de8b6 commit 944e023
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 83 deletions.
74 changes: 74 additions & 0 deletions src/crypto/ocsp.c
Expand Up @@ -21,11 +21,14 @@ FILE_LICENCE ( GPL2_OR_LATER );

#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <ipxe/asn1.h>
#include <ipxe/x509.h>
#include <ipxe/sha1.h>
#include <ipxe/base64.h>
#include <ipxe/uri.h>
#include <ipxe/ocsp.h>

/** @file
Expand Down Expand Up @@ -110,6 +113,7 @@ static void ocsp_free ( struct refcnt *refcnt ) {

x509_put ( ocsp->cert );
x509_put ( ocsp->issuer );
free ( ocsp->uri_string );
free ( ocsp->request.builder.data );
free ( ocsp->response.data );
x509_put ( ocsp->response.signer );
Expand Down Expand Up @@ -180,6 +184,71 @@ static int ocsp_request ( struct ocsp_check *ocsp ) {
return 0;
}

/**
* Build OCSP URI string
*
* @v ocsp OCSP check
* @ret rc Return status code
*/
static int ocsp_uri_string ( struct ocsp_check *ocsp ) {
char *base_uri_string;
char *base64_request;
size_t base64_request_len;
size_t uri_string_len;
size_t prefix_len;
int rc;

/* Sanity check */
base_uri_string = ocsp->cert->extensions.auth_info.ocsp.uri;
if ( ! base_uri_string ) {
DBGC ( ocsp, "OCSP %p \"%s\" has no OCSP URI\n",
ocsp, ocsp->cert->subject.name );
rc = -ENOTTY;
goto err_no_uri;
}

/* Base64-encode the request */
base64_request_len = ( base64_encoded_len ( ocsp->request.builder.len )
+ 1 /* NUL */ );
base64_request = malloc ( base64_request_len );
if ( ! base64_request ) {
rc = -ENOMEM;
goto err_alloc_base64;
}
base64_encode ( ocsp->request.builder.data, ocsp->request.builder.len,
base64_request );

/* Allocate URI string */
uri_string_len = ( strlen ( base_uri_string ) + 1 /* "/" */ +
uri_encode ( base64_request, NULL, 0, URI_FRAGMENT )
+ 1 /* NUL */ );
ocsp->uri_string = malloc ( uri_string_len );
if ( ! ocsp->uri_string ) {
rc = -ENOMEM;
goto err_alloc_uri;
}

/* Construct URI string */
prefix_len = snprintf ( ocsp->uri_string, uri_string_len,
"%s/", base_uri_string );
uri_encode ( base64_request, ( ocsp->uri_string + prefix_len ),
( uri_string_len - prefix_len ), URI_FRAGMENT );
DBGC2 ( ocsp, "OCSP %p \"%s\" URI is %s\n",
ocsp, ocsp->cert->subject.name, ocsp->uri_string );

/* Free base64-encoded request */
free ( base64_request );
base64_request = NULL;

return 0;

err_alloc_uri:
free ( base64_request );
err_alloc_base64:
err_no_uri:
return rc;
}

/**
* Create OCSP check
*
Expand Down Expand Up @@ -212,8 +281,13 @@ int ocsp_check ( struct x509_certificate *cert,
if ( ( rc = ocsp_request ( *ocsp ) ) != 0 )
goto err_request;

/* Build URI string */
if ( ( rc = ocsp_uri_string ( *ocsp ) ) != 0 )
goto err_uri_string;

return 0;

err_uri_string:
err_request:
ocsp_put ( *ocsp );
err_alloc:
Expand Down
2 changes: 2 additions & 0 deletions src/include/ipxe/ocsp.h
Expand Up @@ -70,6 +70,8 @@ struct ocsp_check {
struct x509_certificate *cert;
/** Issuing certificate */
struct x509_certificate *issuer;
/** URI string */
char *uri_string;
/** Request */
struct ocsp_request request;
/** Response */
Expand Down
91 changes: 8 additions & 83 deletions src/tests/ocsp_test.c
Expand Up @@ -352,81 +352,6 @@ CERTIFICATE ( google_crt,
0x50, 0x48, 0xaf, 0x17, 0x94, 0x57, 0x48, 0x39,
0x6b, 0xd2, 0xec, 0xf1, 0x2b, 0x8d, 0xe2, 0x2c ) );

/*
* subject boot.test.ipxe.org
* issuer iPXE self-test leaf CA
*/
CERTIFICATE ( server_crt,
DATA ( 0x30, 0x82, 0x02, 0x7d, 0x30, 0x82, 0x01, 0xe6, 0x02, 0x01,
0x03, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7,
0x0d, 0x01, 0x01, 0x05, 0x05, 0x00, 0x30, 0x81, 0x88, 0x31,
0x0b, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02,
0x47, 0x42, 0x31, 0x17, 0x30, 0x15, 0x06, 0x03, 0x55, 0x04,
0x08, 0x0c, 0x0e, 0x43, 0x61, 0x6d, 0x62, 0x72, 0x69, 0x64,
0x67, 0x65, 0x73, 0x68, 0x69, 0x72, 0x65, 0x31, 0x12, 0x30,
0x10, 0x06, 0x03, 0x55, 0x04, 0x07, 0x0c, 0x09, 0x43, 0x61,
0x6d, 0x62, 0x72, 0x69, 0x64, 0x67, 0x65, 0x31, 0x18, 0x30,
0x16, 0x06, 0x03, 0x55, 0x04, 0x0a, 0x0c, 0x0f, 0x46, 0x65,
0x6e, 0x20, 0x53, 0x79, 0x73, 0x74, 0x65, 0x6d, 0x73, 0x20,
0x4c, 0x74, 0x64, 0x31, 0x11, 0x30, 0x0f, 0x06, 0x03, 0x55,
0x04, 0x0b, 0x0c, 0x08, 0x69, 0x70, 0x78, 0x65, 0x2e, 0x6f,
0x72, 0x67, 0x31, 0x1f, 0x30, 0x1d, 0x06, 0x03, 0x55, 0x04,
0x03, 0x0c, 0x16, 0x69, 0x50, 0x58, 0x45, 0x20, 0x73, 0x65,
0x6c, 0x66, 0x2d, 0x74, 0x65, 0x73, 0x74, 0x20, 0x6c, 0x65,
0x61, 0x66, 0x20, 0x43, 0x41, 0x30, 0x1e, 0x17, 0x0d, 0x31,
0x32, 0x30, 0x33, 0x32, 0x32, 0x30, 0x30, 0x30, 0x31, 0x33,
0x34, 0x5a, 0x17, 0x0d, 0x31, 0x33, 0x30, 0x33, 0x32, 0x32,
0x30, 0x30, 0x30, 0x31, 0x33, 0x34, 0x5a, 0x30, 0x81, 0x84,
0x31, 0x0b, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13,
0x02, 0x47, 0x42, 0x31, 0x17, 0x30, 0x15, 0x06, 0x03, 0x55,
0x04, 0x08, 0x0c, 0x0e, 0x43, 0x61, 0x6d, 0x62, 0x72, 0x69,
0x64, 0x67, 0x65, 0x73, 0x68, 0x69, 0x72, 0x65, 0x31, 0x12,
0x30, 0x10, 0x06, 0x03, 0x55, 0x04, 0x07, 0x0c, 0x09, 0x43,
0x61, 0x6d, 0x62, 0x72, 0x69, 0x64, 0x67, 0x65, 0x31, 0x18,
0x30, 0x16, 0x06, 0x03, 0x55, 0x04, 0x0a, 0x0c, 0x0f, 0x46,
0x65, 0x6e, 0x20, 0x53, 0x79, 0x73, 0x74, 0x65, 0x6d, 0x73,
0x20, 0x4c, 0x74, 0x64, 0x31, 0x11, 0x30, 0x0f, 0x06, 0x03,
0x55, 0x04, 0x0b, 0x0c, 0x08, 0x69, 0x70, 0x78, 0x65, 0x2e,
0x6f, 0x72, 0x67, 0x31, 0x1b, 0x30, 0x19, 0x06, 0x03, 0x55,
0x04, 0x03, 0x0c, 0x12, 0x62, 0x6f, 0x6f, 0x74, 0x2e, 0x74,
0x65, 0x73, 0x74, 0x2e, 0x69, 0x70, 0x78, 0x65, 0x2e, 0x6f,
0x72, 0x67, 0x30, 0x81, 0x9f, 0x30, 0x0d, 0x06, 0x09, 0x2a,
0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x01, 0x05, 0x00,
0x03, 0x81, 0x8d, 0x00, 0x30, 0x81, 0x89, 0x02, 0x81, 0x81,
0x00, 0xbd, 0x43, 0x97, 0x45, 0xa2, 0xe0, 0x1d, 0x38, 0x41,
0xb0, 0xd9, 0x91, 0xf9, 0x77, 0xa9, 0xcb, 0x9c, 0x9c, 0x93,
0xfe, 0x5a, 0xee, 0xbc, 0xd9, 0x0f, 0x39, 0xf6, 0x42, 0xe4,
0x55, 0x21, 0xbb, 0x11, 0xfd, 0xfd, 0xba, 0x25, 0x58, 0xc8,
0xc6, 0xa5, 0x3b, 0x6f, 0x80, 0xba, 0x5b, 0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x02, 0x47, 0x60, 0xd2, 0xe9, 0xed, 0xbf,
0x71, 0x5d, 0x07, 0x09, 0x01, 0x87, 0xeb, 0xf7, 0xa8, 0x26,
0x86, 0x24, 0x59, 0xf0, 0x31, 0x3b, 0x42, 0xd1, 0xf1, 0xfd,
0x7c, 0x49, 0x5f, 0x1a, 0xf0, 0x41, 0x67, 0xf0, 0x16, 0x3a,
0xfd, 0xb6, 0xb5, 0xf6, 0x2e, 0x0c, 0x18, 0x1f, 0x09, 0x8e,
0x4d ),
FINGERPRINT ( 0xe0, 0xdb, 0x60, 0x53, 0x7c, 0xf6, 0x25, 0x8f,
0xa7, 0xba, 0xdf, 0xe2, 0x1a, 0xfc, 0x27, 0x49,
0xf6, 0x83, 0x15, 0xbd, 0x1b, 0x4c, 0x3f, 0x36,
0x6f, 0x33, 0xf2, 0x47, 0x8e, 0x8b, 0x38, 0xa8 ) );

/*
* subject VeriSign Class 3 International Server CA - G3
* issuer VeriSign Class 3 Public Primary Certification Authority - G5
Expand Down Expand Up @@ -1193,14 +1118,16 @@ OCSP ( google_ocsp, &google_crt, &thawte_crt,
0x97, 0x85, 0xfb, 0x2a, 0xa3, 0x92, 0x65, 0x0b, 0x02, 0x58,
0x14, 0x89, 0x8f, 0x3b ) );

OCSP ( unauthorized_ocsp, &server_crt, &thawte_crt,
DATA ( 0x30, 0x42, 0x30, 0x40, 0x30, 0x3e, 0x30, 0x3c, 0x30, 0x3a,
OCSP ( unauthorized_ocsp, &barclays_crt, &thawte_crt,
DATA ( 0x30, 0x51, 0x30, 0x4f, 0x30, 0x4d, 0x30, 0x4b, 0x30, 0x49,
0x30, 0x09, 0x06, 0x05, 0x2b, 0x0e, 0x03, 0x02, 0x1a, 0x05,
0x00, 0x04, 0x14, 0x68, 0x7a, 0xcf, 0x39, 0x5d, 0xdf, 0x7c,
0x2c, 0x91, 0x6d, 0xdb, 0xcb, 0x9d, 0x0c, 0x27, 0x5b, 0x30,
0x64, 0x28, 0xf2, 0x04, 0x14, 0x3b, 0x34, 0x9a, 0x70, 0x91,
0x00, 0x04, 0x14, 0xd4, 0xb4, 0x3b, 0x8e, 0x3d, 0x02, 0x49,
0x1a, 0x65, 0x50, 0x6f, 0x96, 0x73, 0x14, 0xdd, 0xe8, 0x59,
0x44, 0x52, 0xe4, 0x04, 0x14, 0x3b, 0x34, 0x9a, 0x70, 0x91,
0x73, 0xb2, 0x8a, 0x1b, 0x0c, 0xf4, 0xe9, 0x37, 0xcd, 0xb3,
0x70, 0x32, 0x9e, 0x18, 0x54, 0x02, 0x01, 0x03 ),
0x70, 0x32, 0x9e, 0x18, 0x54, 0x02, 0x10, 0x49, 0x83, 0xfc,
0x05, 0x76, 0xdf, 0x36, 0x91, 0x7c, 0x64, 0x2a, 0x27, 0xc1,
0xf1, 0x48, 0xe3 ),
DATA ( 0x30, 0x03, 0x0a, 0x01, 0x06 ) );

OCSP ( unknown_ocsp, &thawte_crt, &startssl_crt,
Expand Down Expand Up @@ -1468,7 +1395,6 @@ static void ocsp_test_exec ( void ) {
/* Parse certificates */
ocsp_certificate_ok ( &barclays_crt );
ocsp_certificate_ok ( &google_crt );
ocsp_certificate_ok ( &server_crt );
ocsp_certificate_ok ( &verisign_crt );
ocsp_certificate_ok ( &thawte_crt );
ocsp_certificate_ok ( &startssl_crt );
Expand Down Expand Up @@ -1509,7 +1435,6 @@ static void ocsp_test_exec ( void ) {
x509_put ( startssl_crt.cert );
x509_put ( thawte_crt.cert );
x509_put ( verisign_crt.cert );
x509_put ( server_crt.cert );
x509_put ( google_crt.cert );
x509_put ( barclays_crt.cert );
}
Expand Down

0 comments on commit 944e023

Please sign in to comment.