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 280 of file ocsp.c.

282 {
283 int rc;
284
285 /* Sanity checks */
286 assert ( cert != NULL );
287 assert ( issuer != NULL );
288 assert ( issuer->root != NULL );
289
290 /* Allocate and initialise check */
291 *ocsp = zalloc ( sizeof ( **ocsp ) );
292 if ( ! *ocsp ) {
293 rc = -ENOMEM;
294 goto err_alloc;
295 }
296 ref_init ( &(*ocsp)->refcnt, ocsp_free );
297 (*ocsp)->cert = x509_get ( cert );
298 (*ocsp)->issuer = x509_get ( issuer );
299
300 /* Build request */
301 if ( ( rc = ocsp_request ( *ocsp ) ) != 0 )
302 goto err_request;
303
304 /* Build URI string */
305 if ( ( rc = ocsp_uri_string ( *ocsp ) ) != 0 )
306 goto err_uri_string;
307
308 return 0;
309
310 err_uri_string:
311 err_request:
312 ocsp_put ( *ocsp );
313 err_alloc:
314 *ocsp = NULL;
315 return rc;
316}
#define NULL
NULL pointer (VOID *)
Definition Base.h:322
struct arbelprm_rc_send_wqe rc
Definition arbel.h:3
#define assert(condition)
Assert a condition at run-time.
Definition assert.h:50
#define ENOMEM
Not enough space.
Definition errno.h:535
void * zalloc(size_t size)
Allocate cleared memory.
Definition malloc.c:662
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 814 of file ocsp.c.

814 {
815 struct ocsp_response *response = &ocsp->response;
816 struct asn1_cursor cursor;
817 int rc;
818
819 /* Duplicate data */
820 x509_put ( response->signer );
821 response->signer = NULL;
822 free ( response->data );
823 response->data = malloc ( len );
824 if ( ! response->data )
825 return -ENOMEM;
826 memcpy ( response->data, data, len );
827 cursor.data = response->data;
828 cursor.len = len;
829
830 /* Parse response */
831 if ( ( rc = ocsp_parse_response ( ocsp, &cursor ) ) != 0 )
832 return rc;
833
834 return 0;
835}
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:621
static int ocsp_parse_response(struct ocsp_check *ocsp, const struct asn1_cursor *raw)
Parse OCSP response.
Definition ocsp.c:785
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 880 of file ocsp.c.

880 {
881 struct ocsp_response *response = &ocsp->response;
882 struct x509_certificate *signer;
883 int rc;
884
885 /* Sanity checks */
886 assert ( response->data != NULL );
887
888 /* The response may include a signer certificate; if this is
889 * not present then the response must have been signed
890 * directly by the issuer.
891 */
892 signer = ( response->signer ? response->signer : ocsp->issuer );
893
894 /* Validate signer, if applicable. If the signer is not the
895 * issuer, then it must be signed directly by the issuer.
896 */
897 if ( signer != ocsp->issuer ) {
898 /* Forcibly invalidate the signer, since we need to
899 * ensure that it was signed by our issuer (and not
900 * some other issuer). This prevents a sub-CA's OCSP
901 * certificate from fraudulently signing OCSP
902 * responses from the parent CA.
903 */
904 x509_invalidate ( signer );
905 if ( ( rc = x509_validate ( signer, ocsp->issuer, time,
906 ocsp->issuer->root ) ) != 0 ) {
907 DBGC ( ocsp, "OCSP %p \"%s\" could not validate ",
908 ocsp, x509_name ( ocsp->cert ) );
909 DBGC ( ocsp, "signer \"%s\": %s\n",
910 x509_name ( signer ), strerror ( rc ) );
911 return rc;
912 }
913
914 /* If signer is not the issuer, then it must have the
915 * extendedKeyUsage id-kp-OCSPSigning.
916 */
917 if ( ! ( signer->extensions.ext_usage.bits &
919 DBGC ( ocsp, "OCSP %p \"%s\" ",
920 ocsp, x509_name ( ocsp->cert ) );
921 DBGC ( ocsp, "signer \"%s\" is not an OCSP-signing "
922 "certificate\n", x509_name ( signer ) );
924 }
925 }
926
927 /* Check OCSP response signature */
928 if ( ( rc = ocsp_check_signature ( ocsp, signer ) ) != 0 )
929 return rc;
930
931 /* Check OCSP response is valid at the specified time
932 * (allowing for some margin of error).
933 */
934 if ( response->this_update > ( time + TIMESTAMP_ERROR_MARGIN ) ) {
935 DBGC ( ocsp, "OCSP %p \"%s\" response is not yet valid (at "
936 "time %lld)\n", ocsp, x509_name ( ocsp->cert ), time );
937 return -EACCES_STALE;
938 }
939 if ( response->next_update < ( time - TIMESTAMP_ERROR_MARGIN ) ) {
940 DBGC ( ocsp, "OCSP %p \"%s\" response is stale (at time "
941 "%lld)\n", ocsp, x509_name ( ocsp->cert ), time );
942 return -EACCES_STALE;
943 }
944 DBGC2 ( ocsp, "OCSP %p \"%s\" response is valid (at time %lld)\n",
945 ocsp, x509_name ( ocsp->cert ), time );
946
947 /* Mark certificate as passing OCSP verification */
948 ocsp->cert->extensions.auth_info.ocsp.good = 1;
949
950 /* Validate certificate against issuer */
951 if ( ( rc = x509_validate ( ocsp->cert, ocsp->issuer, time,
952 ocsp->issuer->root ) ) != 0 ) {
953 DBGC ( ocsp, "OCSP %p \"%s\" could not validate certificate: "
954 "%s\n", ocsp, x509_name ( ocsp->cert ), strerror ( rc ));
955 return rc;
956 }
957 DBGC ( ocsp, "OCSP %p \"%s\" successfully validated ",
958 ocsp, x509_name ( ocsp->cert ) );
959 DBGC ( ocsp, "using \"%s\"\n", x509_name ( signer ) );
960
961 return 0;
962}
#define TIMESTAMP_ERROR_MARGIN
Margin of error (in seconds) allowed in signed timestamps.
Definition crypto.h:79
#define DBGC2(...)
Definition compiler.h:522
#define DBGC(...)
Definition compiler.h:505
static int ocsp_check_signature(struct ocsp_check *ocsp, struct x509_certificate *signer)
Check OCSP response signature.
Definition ocsp.c:844
#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().