iPXE
ocsp.h File Reference

Online Certificate Status Protocol. More...

#include <stdarg.h>
#include <time.h>
#include <ipxe/asn1.h>
#include <ipxe/x509.h>
#include <ipxe/refcnt.h>
#include <config/crypto.h>

Go to the source code of this file.

Data Structures

struct  ocsp_request
 An OCSP request. More...
struct  ocsp_responder
 An OCSP responder. More...
struct  ocsp_response
 An OCSP response. More...
struct  ocsp_check
 An OCSP check. More...

Macros

#define OCSP_ENABLED   0
#define OCSP_ALGORITHM_IDENTIFIER(...)
 OCSP algorithm identifier.
#define OCSP_STATUS_SUCCESSFUL   0x00
#define OCSP_STATUS_MALFORMED_REQUEST   0x01
#define OCSP_STATUS_INTERNAL_ERROR   0x02
#define OCSP_STATUS_TRY_LATER   0x03
#define OCSP_STATUS_SIG_REQUIRED   0x05
#define OCSP_STATUS_UNAUTHORIZED   0x06

Functions

 FILE_LICENCE (GPL2_OR_LATER_OR_UBDL)
 FILE_SECBOOT (PERMITTED)
static struct ocsp_checkocsp_get (struct ocsp_check *ocsp)
 Get reference to OCSP check.
static void ocsp_put (struct ocsp_check *ocsp)
 Drop reference to OCSP check.
static int ocsp_required (struct x509_certificate *cert)
 Check if X.509 certificate requires an OCSP check.
int ocsp_check (struct x509_certificate *cert, struct x509_certificate *issuer, struct ocsp_check **ocsp)
 Create OCSP check.
int ocsp_response (struct ocsp_check *ocsp, const void *data, size_t len)
 Receive OCSP response.
int ocsp_validate (struct ocsp_check *check, time_t time)
 Validate OCSP response.

Detailed Description

Online Certificate Status Protocol.

Definition in file ocsp.h.

Macro Definition Documentation

◆ OCSP_ENABLED

#define OCSP_ENABLED   0

Definition at line 24 of file ocsp.h.

Referenced by ocsp_required().

◆ OCSP_ALGORITHM_IDENTIFIER

#define OCSP_ALGORITHM_IDENTIFIER ( ...)
Value:
ASN1_OID, VA_ARG_COUNT ( __VA_ARGS__ ), __VA_ARGS__, \
ASN1_NULL, 0x00
#define ASN1_NULL
ASN.1 null.
Definition asn1.h:72
#define ASN1_OID
ASN.1 object identifier.
Definition asn1.h:75
#define VA_ARG_COUNT(...)
Definition stdarg.h:28

OCSP algorithm identifier.

Definition at line 28 of file ocsp.h.

28#define OCSP_ALGORITHM_IDENTIFIER( ... ) \
29 ASN1_OID, VA_ARG_COUNT ( __VA_ARGS__ ), __VA_ARGS__, \
30 ASN1_NULL, 0x00

◆ OCSP_STATUS_SUCCESSFUL

#define OCSP_STATUS_SUCCESSFUL   0x00

Definition at line 33 of file ocsp.h.

Referenced by ocsp_parse_response_status().

◆ OCSP_STATUS_MALFORMED_REQUEST

#define OCSP_STATUS_MALFORMED_REQUEST   0x01

Definition at line 34 of file ocsp.h.

◆ OCSP_STATUS_INTERNAL_ERROR

#define OCSP_STATUS_INTERNAL_ERROR   0x02

Definition at line 35 of file ocsp.h.

◆ OCSP_STATUS_TRY_LATER

#define OCSP_STATUS_TRY_LATER   0x03

Definition at line 36 of file ocsp.h.

◆ OCSP_STATUS_SIG_REQUIRED

#define OCSP_STATUS_SIG_REQUIRED   0x05

Definition at line 37 of file ocsp.h.

◆ OCSP_STATUS_UNAUTHORIZED

#define OCSP_STATUS_UNAUTHORIZED   0x06

Definition at line 38 of file ocsp.h.

Function Documentation

◆ FILE_LICENCE()

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL )

◆ FILE_SECBOOT()

FILE_SECBOOT ( PERMITTED )

◆ ocsp_get()

struct ocsp_check * ocsp_get ( struct ocsp_check * ocsp)
inlinestatic

Get reference to OCSP check.

Parameters
ocspOCSP check
Return values
ocspOCSP check

Definition at line 108 of file ocsp.h.

108 {
109 ref_get ( &ocsp->refcnt );
110 return ocsp;
111}
#define ref_get(refcnt)
Get additional reference to object.
Definition refcnt.h:93
struct refcnt refcnt
Reference count.
Definition ocsp.h:88

References ref_get, and ocsp_check::refcnt.

◆ ocsp_put()

void ocsp_put ( struct ocsp_check * ocsp)
inlinestatic

Drop reference to OCSP check.

Parameters
ocspOCSP check

Definition at line 119 of file ocsp.h.

119 {
120 ref_put ( &ocsp->refcnt );
121}
#define ref_put(refcnt)
Drop reference to object.
Definition refcnt.h:107

References ref_put, and ocsp_check::refcnt.

Referenced by ocsp_check(), ocsp_test_exec(), validator_free(), validator_ocsp_validate(), and validator_start_ocsp().

◆ ocsp_required()

int ocsp_required ( struct x509_certificate * cert)
inlinestatic

Check if X.509 certificate requires an OCSP check.

Parameters
certX.509 certificate
Return values
ocsp_requiredAn OCSP check is required

Definition at line 129 of file ocsp.h.

129 {
130
131 /* An OCSP check is never required if OCSP checks are disabled */
132 if ( ! OCSP_ENABLED )
133 return 0;
134
135 /* An OCSP check is required if an OCSP URI exists but the
136 * OCSP status is not (yet) good.
137 */
138 return ( cert->extensions.auth_info.ocsp.uri.len &&
139 ( ! cert->extensions.auth_info.ocsp.good ) );
140}
#define OCSP_ENABLED
Definition ocsp.h:24
size_t len
Length of data.
Definition asn1.h:25
struct x509_ocsp_responder ocsp
OCSP responder.
Definition x509.h:140
struct x509_extensions extensions
Extensions.
Definition x509.h:249
struct x509_authority_info_access auth_info
Authority information access.
Definition x509.h:165
int good
OCSP status is good.
Definition x509.h:134
struct asn1_cursor uri
URI.
Definition x509.h:132

References x509_extensions::auth_info, ocsp_check::cert, x509_certificate::extensions, x509_ocsp_responder::good, asn1_cursor::len, x509_authority_info_access::ocsp, OCSP_ENABLED, and x509_ocsp_responder::uri.

Referenced by validator_step(), and x509_validate().

◆ ocsp_check()

int ocsp_check ( struct x509_certificate * cert,
struct x509_certificate * issuer,
struct ocsp_check ** ocsp )
extern

Create OCSP check.

Parameters
certCertificate to check
issuerIssuing certificate
Return values
ocspOCSP check
rcReturn status code

Definition at line 284 of file ocsp.c.

286 {
287 int rc;
288
289 /* Sanity checks */
290 assert ( cert != NULL );
291 assert ( issuer != NULL );
292 assert ( issuer->root != NULL );
293
294 /* Allocate and initialise check */
295 *ocsp = zalloc ( sizeof ( **ocsp ) );
296 if ( ! *ocsp ) {
297 rc = -ENOMEM;
298 goto err_alloc;
299 }
300 ref_init ( &(*ocsp)->refcnt, ocsp_free );
301 (*ocsp)->cert = x509_get ( cert );
302 (*ocsp)->issuer = x509_get ( issuer );
303
304 /* Build request */
305 if ( ( rc = ocsp_request ( *ocsp ) ) != 0 )
306 goto err_request;
307
308 /* Build URI string */
309 if ( ( rc = ocsp_uri_string ( *ocsp ) ) != 0 )
310 goto err_uri_string;
311
312 return 0;
313
314 err_uri_string:
315 err_request:
316 ocsp_put ( *ocsp );
317 err_alloc:
318 *ocsp = NULL;
319 return rc;
320}
#define NULL
NULL pointer (VOID *).
Definition Base.h:321
struct arbelprm_rc_send_wqe rc
Definition arbel.h:3
#define assert(condition)
Assert a condition at run-time.
Definition assert.h:61
#define ENOMEM
Not enough space.
Definition errno.h:578
void * zalloc(size_t size)
Allocate cleared memory.
Definition malloc.c:718
static void ocsp_free(struct refcnt *refcnt)
Free OCSP check.
Definition ocsp.c:127
static int ocsp_uri_string(struct ocsp_check *ocsp)
Build OCSP URI string.
Definition ocsp.c:212
static void ocsp_put(struct ocsp_check *ocsp)
Drop reference to OCSP check.
Definition ocsp.h:119
#define ref_init(refcnt, free)
Initialise a reference counter.
Definition refcnt.h:65
An OCSP request.
Definition ocsp.h:43
struct x509_root * root
Root against which certificate has been validated (if any).
Definition x509.h:226
static struct x509_certificate * x509_get(struct x509_certificate *cert)
Get reference to X.509 certificate.
Definition x509.h:267

References assert, ENOMEM, NULL, ocsp_free(), ocsp_put(), ocsp_uri_string(), rc, ref_init, x509_certificate::root, x509_get(), and zalloc().

◆ ocsp_response()

int ocsp_response ( struct ocsp_check * ocsp,
const void * data,
size_t len )
extern

Receive OCSP response.

Parameters
ocspOCSP check
dataResponse data
lenLength of response data
Return values
rcReturn status code

Definition at line 818 of file ocsp.c.

818 {
819 struct ocsp_response *response = &ocsp->response;
820 struct asn1_cursor cursor;
821 int rc;
822
823 /* Duplicate data */
824 x509_put ( response->signer );
825 response->signer = NULL;
826 free ( response->data );
827 response->data = malloc ( len );
828 if ( ! response->data )
829 return -ENOMEM;
830 memcpy ( response->data, data, len );
831 cursor.data = response->data;
832 cursor.len = len;
833
834 /* Parse response */
835 if ( ( rc = ocsp_parse_response ( ocsp, &cursor ) ) != 0 )
836 return rc;
837
838 return 0;
839}
ring len
Length.
Definition dwmac.h:226
uint8_t data[48]
Additional event data.
Definition ena.h:11
void * memcpy(void *dest, const void *src, size_t len) __nonnull
void * malloc(size_t size)
Allocate memory.
Definition malloc.c:677
static int ocsp_parse_response(struct ocsp_check *ocsp, const struct asn1_cursor *raw)
Parse OCSP response.
Definition ocsp.c:789
static void(* free)(struct refcnt *refcnt))
Definition refcnt.h:55
An ASN.1 object cursor.
Definition asn1.h:21
struct ocsp_response response
Response.
Definition ocsp.h:98
An OCSP response.
Definition ocsp.h:66
struct x509_certificate * signer
Signing certificate.
Definition ocsp.h:82
void * data
Raw response.
Definition ocsp.h:68
static void x509_put(struct x509_certificate *cert)
Drop reference to X.509 certificate.
Definition x509.h:278

References asn1_cursor::data, data, ocsp_response::data, ENOMEM, free, asn1_cursor::len, len, malloc(), memcpy(), NULL, ocsp_parse_response(), rc, ocsp_check::response, ocsp_response::signer, and x509_put().

◆ ocsp_validate()

int ocsp_validate ( struct ocsp_check * ocsp,
time_t time )
extern

Validate OCSP response.

Parameters
ocspOCSP check
timeTime at which to validate response
Return values
rcReturn status code

Definition at line 884 of file ocsp.c.

884 {
885 struct ocsp_response *response = &ocsp->response;
886 struct x509_certificate *signer;
887 int rc;
888
889 /* Sanity checks */
890 assert ( response->data != NULL );
891
892 /* The response may include a signer certificate; if this is
893 * not present then the response must have been signed
894 * directly by the issuer.
895 */
896 signer = ( response->signer ? response->signer : ocsp->issuer );
897
898 /* Validate signer, if applicable. If the signer is not the
899 * issuer, then it must be signed directly by the issuer.
900 */
901 if ( signer != ocsp->issuer ) {
902 /* Forcibly invalidate the signer, since we need to
903 * ensure that it was signed by our issuer (and not
904 * some other issuer). This prevents a sub-CA's OCSP
905 * certificate from fraudulently signing OCSP
906 * responses from the parent CA.
907 */
908 x509_invalidate ( signer );
909 if ( ( rc = x509_validate ( signer, ocsp->issuer, time,
910 ocsp->issuer->root ) ) != 0 ) {
911 DBGC ( ocsp, "OCSP %p \"%s\" could not validate ",
912 ocsp, x509_name ( ocsp->cert ) );
913 DBGC ( ocsp, "signer \"%s\": %s\n",
914 x509_name ( signer ), strerror ( rc ) );
915 return rc;
916 }
917
918 /* If signer is not the issuer, then it must have the
919 * extendedKeyUsage id-kp-OCSPSigning.
920 */
921 if ( ! ( signer->extensions.ext_usage.bits &
923 DBGC ( ocsp, "OCSP %p \"%s\" ",
924 ocsp, x509_name ( ocsp->cert ) );
925 DBGC ( ocsp, "signer \"%s\" is not an OCSP-signing "
926 "certificate\n", x509_name ( signer ) );
928 }
929 }
930
931 /* Check OCSP response signature */
932 if ( ( rc = ocsp_check_signature ( ocsp, signer ) ) != 0 )
933 return rc;
934
935 /* Check OCSP response is valid at the specified time
936 * (allowing for some margin of error).
937 */
938 if ( response->this_update > ( time + TIMESTAMP_ERROR_MARGIN ) ) {
939 DBGC ( ocsp, "OCSP %p \"%s\" response is not yet valid (at "
940 "time %lld)\n", ocsp, x509_name ( ocsp->cert ), time );
941 return -EACCES_STALE;
942 }
943 if ( response->next_update < ( time - TIMESTAMP_ERROR_MARGIN ) ) {
944 DBGC ( ocsp, "OCSP %p \"%s\" response is stale (at time "
945 "%lld)\n", ocsp, x509_name ( ocsp->cert ), time );
946 return -EACCES_STALE;
947 }
948 DBGC2 ( ocsp, "OCSP %p \"%s\" response is valid (at time %lld)\n",
949 ocsp, x509_name ( ocsp->cert ), time );
950
951 /* Mark certificate as passing OCSP verification */
952 ocsp->cert->extensions.auth_info.ocsp.good = 1;
953
954 /* Validate certificate against issuer */
955 if ( ( rc = x509_validate ( ocsp->cert, ocsp->issuer, time,
956 ocsp->issuer->root ) ) != 0 ) {
957 DBGC ( ocsp, "OCSP %p \"%s\" could not validate certificate: "
958 "%s\n", ocsp, x509_name ( ocsp->cert ), strerror ( rc ));
959 return rc;
960 }
961 DBGC ( ocsp, "OCSP %p \"%s\" successfully validated ",
962 ocsp, x509_name ( ocsp->cert ) );
963 DBGC ( ocsp, "using \"%s\"\n", x509_name ( signer ) );
964
965 return 0;
966}
#define TIMESTAMP_ERROR_MARGIN
Margin of error (in seconds) allowed in signed timestamps.
Definition crypto.h:100
#define DBGC2(...)
Definition compiler.h:547
#define DBGC(...)
Definition compiler.h:530
static int ocsp_check_signature(struct ocsp_check *ocsp, struct x509_certificate *signer)
Check OCSP response signature.
Definition ocsp.c:848
#define EACCES_NON_OCSP_SIGNING
Definition ocsp.c:53
#define EACCES_STALE
Definition ocsp.c:58
char * strerror(int errno)
Retrieve string representation of error number.
Definition strerror.c:79
struct x509_certificate * issuer
Issuing certificate.
Definition ocsp.h:92
struct x509_certificate * cert
Certificate being checked.
Definition ocsp.h:90
time_t next_update
Time at which newer status information will be available.
Definition ocsp.h:76
time_t this_update
Time at which status is known to be correct.
Definition ocsp.h:74
An X.509 certificate.
Definition x509.h:216
unsigned int bits
Usage bits.
Definition x509.h:116
struct x509_extended_key_usage ext_usage
Extended key usage.
Definition x509.h:163
int x509_validate(struct x509_certificate *cert, struct x509_certificate *issuer, time_t time, struct x509_root *root)
Validate X.509 certificate.
Definition x509.c:1366
const char * x509_name(struct x509_certificate *cert)
Get X.509 certificate display name.
Definition x509.c:147
@ X509_OCSP_SIGNING
Definition x509.h:126
static void x509_invalidate(struct x509_certificate *cert)
Invalidate X.509 certificate.
Definition x509.h:473

References assert, x509_extensions::auth_info, x509_extended_key_usage::bits, ocsp_check::cert, ocsp_response::data, DBGC, DBGC2, EACCES_NON_OCSP_SIGNING, EACCES_STALE, x509_extensions::ext_usage, x509_certificate::extensions, x509_ocsp_responder::good, ocsp_check::issuer, ocsp_response::next_update, NULL, x509_authority_info_access::ocsp, ocsp_check_signature(), rc, ocsp_check::response, x509_certificate::root, ocsp_response::signer, strerror(), ocsp_response::this_update, TIMESTAMP_ERROR_MARGIN, x509_invalidate(), x509_name(), X509_OCSP_SIGNING, and x509_validate().

Referenced by validator_ocsp_validate().