iPXE
Data Structures | Macros | Enumerations | Functions
x509.h File Reference

X.509 certificates. More...

#include <stdint.h>
#include <stddef.h>
#include <time.h>
#include <ipxe/asn1.h>
#include <ipxe/refcnt.h>
#include <ipxe/list.h>

Go to the source code of this file.

Data Structures

struct  x509_serial
 An X.509 serial number. More...
 
struct  x509_issuer
 An X.509 issuer. More...
 
struct  x509_time
 An X.509 time. More...
 
struct  x509_validity
 An X.509 certificate validity period. More...
 
struct  x509_public_key
 An X.509 certificate public key. More...
 
struct  x509_subject
 An X.509 certificate subject. More...
 
struct  x509_signature
 An X.509 certificate signature. More...
 
struct  x509_basic_constraints
 An X.509 certificate basic constraints set. More...
 
struct  x509_key_usage
 An X.509 certificate key usage. More...
 
struct  x509_extended_key_usage
 An X.509 certificate extended key usage. More...
 
struct  x509_ocsp_responder
 X.509 certificate OCSP responder. More...
 
struct  x509_authority_info_access
 X.509 certificate authority information access. More...
 
struct  x509_subject_alt_name
 X.509 certificate subject alternative name. More...
 
struct  x509_extensions
 An X.509 certificate extensions set. More...
 
struct  x509_link
 A link in an X.509 certificate chain. More...
 
struct  x509_chain
 An X.509 certificate chain. More...
 
struct  x509_certificate
 An X.509 certificate. More...
 
struct  x509_extension
 An X.509 extension. More...
 
struct  x509_key_purpose
 An X.509 key purpose. More...
 
struct  x509_access_method
 An X.509 access method. More...
 
struct  x509_root
 An X.509 root certificate list. More...
 

Macros

#define X509_PATH_LEN_UNLIMITED   -2U
 Unlimited path length. More...
 

Enumerations

enum  x509_key_usage_bits {
  X509_DIGITAL_SIGNATURE = 0x0080, X509_NON_REPUDIATION = 0x0040, X509_KEY_ENCIPHERMENT = 0x0020, X509_DATA_ENCIPHERMENT = 0x0010,
  X509_KEY_AGREEMENT = 0x0008, X509_KEY_CERT_SIGN = 0x0004, X509_CRL_SIGN = 0x0002, X509_ENCIPHER_ONLY = 0x0001,
  X509_DECIPHER_ONLY = 0x8000
}
 X.509 certificate key usage bits. More...
 
enum  x509_extended_key_usage_bits { X509_CODE_SIGNING = 0x0001, X509_OCSP_SIGNING = 0x0002 }
 X.509 certificate extended key usage bits. More...
 
enum  x509_general_name_types { X509_GENERAL_NAME_DNS = ASN1_IMPLICIT_TAG ( 2 ), X509_GENERAL_NAME_URI = ASN1_IMPLICIT_TAG ( 6 ), X509_GENERAL_NAME_IP = ASN1_IMPLICIT_TAG ( 7 ) }
 X.509 certificate general name types. More...
 
enum  x509_link_flags { X509_LINK_FL_CROSSED = 0x0001, X509_LINK_FL_OCSPED = 0x0002 }
 X.509 certficate chain link flags. More...
 
enum  x509_flags { X509_FL_PERMANENT = 0x0001, X509_FL_EXPLICIT = 0x0002 }
 X.509 certificate flags. More...
 

Functions

 FILE_LICENCE (GPL2_OR_LATER_OR_UBDL)
 
static struct x509_certificatex509_get (struct x509_certificate *cert)
 Get reference to X.509 certificate. More...
 
static void x509_put (struct x509_certificate *cert)
 Drop reference to X.509 certificate. More...
 
static struct x509_chainx509_chain_get (struct x509_chain *chain)
 Get reference to X.509 certificate chain. More...
 
static void x509_chain_put (struct x509_chain *chain)
 Drop reference to X.509 certificate chain. More...
 
static struct x509_certificatex509_first (struct x509_chain *chain)
 Get first certificate in X.509 certificate chain. More...
 
static struct x509_certificatex509_last (struct x509_chain *chain)
 Get last certificate in X.509 certificate chain. More...
 
static struct x509_rootx509_root_get (struct x509_root *root)
 Get reference to X.509 root certificate list. More...
 
static void x509_root_put (struct x509_root *root)
 Drop reference to X.509 root certificate list. More...
 
static int x509_is_self_signed (struct x509_certificate *cert)
 Check if X.509 certificate is self-signed. More...
 
const char * x509_name (struct x509_certificate *cert)
 Get X.509 certificate display name. More...
 
int x509_parse (struct x509_certificate *cert, const struct asn1_cursor *raw)
 Parse X.509 certificate from ASN.1 data. More...
 
int x509_certificate (const void *data, size_t len, struct x509_certificate **cert)
 Create X.509 certificate. More...
 
int x509_is_valid (struct x509_certificate *cert, struct x509_root *root)
 Check if X.509 certificate is valid. More...
 
void x509_set_valid (struct x509_certificate *cert, struct x509_certificate *issuer, struct x509_root *root)
 Set X.509 certificate as validated. More...
 
int x509_validate (struct x509_certificate *cert, struct x509_certificate *issuer, time_t time, struct x509_root *root)
 Validate X.509 certificate. More...
 
int x509_check_name (struct x509_certificate *cert, const char *name)
 Check X.509 certificate name. More...
 
struct x509_chainx509_alloc_chain (void)
 Allocate X.509 certificate chain. More...
 
int x509_append (struct x509_chain *chain, struct x509_certificate *cert)
 Append X.509 certificate to X.509 certificate chain. More...
 
int x509_append_raw (struct x509_chain *chain, const void *data, size_t len)
 Append X.509 certificate to X.509 certificate chain. More...
 
void x509_truncate (struct x509_chain *chain, struct x509_link *link)
 Truncate X.509 certificate chain. More...
 
struct x509_certificatex509_find (struct x509_chain *store, const struct asn1_cursor *raw)
 Identify X.509 certificate by raw certificate data. More...
 
struct x509_certificatex509_find_subject (struct x509_chain *store, const struct asn1_cursor *subject)
 Identify X.509 certificate by subject. More...
 
struct x509_certificatex509_find_issuer_serial (struct x509_chain *store, const struct asn1_cursor *issuer, const struct asn1_cursor *serial)
 Identify X.509 certificate by issuer and serial number. More...
 
struct x509_certificatex509_find_key (struct x509_chain *store, struct private_key *key)
 Identify X.509 certificate by corresponding public key. More...
 
int x509_auto_append (struct x509_chain *chain, struct x509_chain *store)
 Append X.509 certificates to X.509 certificate chain. More...
 
int x509_validate_chain (struct x509_chain *chain, time_t time, struct x509_chain *store, struct x509_root *root)
 Validate X.509 certificate chain. More...
 
int image_x509 (struct image *image, size_t offset, struct x509_certificate **cert)
 Extract X.509 certificate object from image. More...
 
int x509_check_issuer (struct x509_certificate *cert, struct x509_certificate *issuer)
 Check X.509 certificate against issuer certificate. More...
 
void x509_fingerprint (struct x509_certificate *cert, struct digest_algorithm *digest, void *fingerprint)
 Calculate X.509 certificate fingerprint. More...
 
int x509_check_root (struct x509_certificate *cert, struct x509_root *root)
 Check X.509 root certificate. More...
 
int x509_check_time (struct x509_certificate *cert, time_t time)
 Check X.509 certificate validity period. More...
 
static void x509_invalidate (struct x509_certificate *cert)
 Invalidate X.509 certificate. More...
 
static void x509_invalidate_chain (struct x509_chain *chain)
 Invalidate X.509 certificate chain. More...
 

Detailed Description

X.509 certificates.

Definition in file x509.h.

Macro Definition Documentation

◆ X509_PATH_LEN_UNLIMITED

#define X509_PATH_LEN_UNLIMITED   -2U

Unlimited path length.

We use -2U, since this quantity represents one fewer than the maximum number of remaining certificates in a chain.

Definition at line 89 of file x509.h.

Enumeration Type Documentation

◆ x509_key_usage_bits

X.509 certificate key usage bits.

Enumerator
X509_DIGITAL_SIGNATURE 
X509_NON_REPUDIATION 
X509_KEY_ENCIPHERMENT 
X509_DATA_ENCIPHERMENT 
X509_KEY_AGREEMENT 
X509_KEY_CERT_SIGN 
X509_CRL_SIGN 
X509_ENCIPHER_ONLY 
X509_DECIPHER_ONLY 

Definition at line 100 of file x509.h.

◆ x509_extended_key_usage_bits

X.509 certificate extended key usage bits.

Extended key usages are identified by OID; these bits are purely an internal definition.

Enumerator
X509_CODE_SIGNING 
X509_OCSP_SIGNING 

Definition at line 123 of file x509.h.

123  {
124  X509_CODE_SIGNING = 0x0001,
125  X509_OCSP_SIGNING = 0x0002,
126 };

◆ x509_general_name_types

X.509 certificate general name types.

Enumerator
X509_GENERAL_NAME_DNS 
X509_GENERAL_NAME_URI 
X509_GENERAL_NAME_IP 

Definition at line 149 of file x509.h.

149  {
153 };
#define ASN1_IMPLICIT_TAG(number)
ASN.1 implicit tag.
Definition: asn1.h:95

◆ x509_link_flags

X.509 certficate chain link flags.

Enumerator
X509_LINK_FL_CROSSED 

Cross-signed certificate download has been attempted.

   This indicates that a cross-signature download attempt has
   been made to find a cross-signed issuer for this link's
   certificate.
X509_LINK_FL_OCSPED 

OCSP has been attempted.

   This indicates that an OCSP attempt has been made using
   this link's certificate as an issuer.  (We record the flag
   on the issuer rather than on the issued certificate, since
   we want to retry OCSP if an issuer is replaced with a
   downloaded cross-signed certificate.)

Definition at line 180 of file x509.h.

180  {
181  /** Cross-signed certificate download has been attempted
182  *
183  * This indicates that a cross-signature download attempt has
184  * been made to find a cross-signed issuer for this link's
185  * certificate.
186  */
187  X509_LINK_FL_CROSSED = 0x0001,
188  /** OCSP has been attempted
189  *
190  * This indicates that an OCSP attempt has been made using
191  * this link's certificate as an issuer. (We record the flag
192  * on the issuer rather than on the issued certificate, since
193  * we want to retry OCSP if an issuer is replaced with a
194  * downloaded cross-signed certificate.)
195  */
196  X509_LINK_FL_OCSPED = 0x0002,
197 };
OCSP has been attempted.
Definition: x509.h:196
Cross-signed certificate download has been attempted.
Definition: x509.h:187

◆ x509_flags

enum x509_flags

X.509 certificate flags.

Enumerator
X509_FL_PERMANENT 

Certificate was added at build time.

X509_FL_EXPLICIT 

Certificate was added explicitly at run time.

Definition at line 252 of file x509.h.

252  {
253  /** Certificate was added at build time */
254  X509_FL_PERMANENT = 0x0001,
255  /** Certificate was added explicitly at run time */
256  X509_FL_EXPLICIT = 0x0002,
257 };
Certificate was added at build time.
Definition: x509.h:254
Certificate was added explicitly at run time.
Definition: x509.h:256

Function Documentation

◆ FILE_LICENCE()

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL  )

◆ x509_get()

static struct x509_certificate* x509_get ( struct x509_certificate cert)
inlinestatic

Get reference to X.509 certificate.

Parameters
certX.509 certificate
Return values
certX.509 certificate

Definition at line 266 of file x509.h.

266  {
267  ref_get ( &cert->refcnt );
268  return cert;
269 }
struct refcnt refcnt
Reference count.
Definition: x509.h:217
#define ref_get(refcnt)
Get additional reference to object.
Definition: refcnt.h:92

References ref_get, and x509_certificate::refcnt.

Referenced by cert_exec(), certstore_add(), ocsp_check(), x509_append(), and x509_certificate().

◆ x509_put()

static void x509_put ( struct x509_certificate cert)
inlinestatic

Drop reference to X.509 certificate.

Parameters
certX.509 certificate

Definition at line 277 of file x509.h.

277  {
278  ref_put ( &cert->refcnt );
279 }
struct refcnt refcnt
Reference count.
Definition: x509.h:217
#define ref_put(refcnt)
Drop reference to object.
Definition: refcnt.h:106

References ref_put, and x509_certificate::refcnt.

Referenced by cert_exec(), certstore_apply_settings(), certstore_del(), cms_test_exec(), icert_free(), image_x509(), ocsp_free(), ocsp_parse_certs(), ocsp_response(), ocsp_test_exec(), x509_append(), x509_append_raw(), x509_cached_okx(), x509_certificate(), x509_test_exec(), and x509_truncate().

◆ x509_chain_get()

static struct x509_chain* x509_chain_get ( struct x509_chain chain)
inlinestatic

Get reference to X.509 certificate chain.

Parameters
chainX.509 certificate chain
Return values
chainX.509 certificate chain

Definition at line 288 of file x509.h.

288  {
289  ref_get ( &chain->refcnt );
290  return chain;
291 }
#define ref_get(refcnt)
Get additional reference to object.
Definition: refcnt.h:92
struct refcnt refcnt
Reference count.
Definition: x509.h:202

References ref_get, and x509_chain::refcnt.

Referenced by create_validator().

◆ x509_chain_put()

static void x509_chain_put ( struct x509_chain chain)
inlinestatic

Drop reference to X.509 certificate chain.

Parameters
chainX.509 certificate chain

Definition at line 299 of file x509.h.

299  {
300  ref_put ( &chain->refcnt );
301 }
struct refcnt refcnt
Reference count.
Definition: x509.h:202
#define ref_put(refcnt)
Drop reference to object.
Definition: refcnt.h:106

References ref_put, and x509_chain::refcnt.

Referenced by cms_free(), free_tls(), tls_new_certificate_request(), tls_parse_chain(), validator_append(), validator_free(), and x509_test_exec().

◆ x509_first()

static struct x509_certificate* x509_first ( struct x509_chain chain)
inlinestatic

Get first certificate in X.509 certificate chain.

Parameters
chainX.509 certificate chain
Return values
certX.509 certificate, or NULL

Definition at line 310 of file x509.h.

310  {
311  struct x509_link *link;
312 
313  link = list_first_entry ( &chain->links, struct x509_link, list );
314  return ( link ? link->cert : NULL );
315 }
struct list_head links
List of links.
Definition: x509.h:204
#define list_first_entry(list, type, member)
Get the container of the first entry in a list.
Definition: list.h:333
u32 link
Link to next descriptor.
Definition: ar9003_mac.h:68
#define NULL
NULL pointer (VOID *)
Definition: Base.h:321

References link, x509_chain::links, x509_link::list, list_first_entry, and NULL.

Referenced by cms_recipient(), cms_verify(), cms_verify_signer(), tls_send_certificate_verify(), tls_validator_done(), validator_name(), and x509_chain_okx().

◆ x509_last()

static struct x509_certificate* x509_last ( struct x509_chain chain)
inlinestatic

Get last certificate in X.509 certificate chain.

Parameters
chainX.509 certificate chain
Return values
certX.509 certificate, or NULL

Definition at line 324 of file x509.h.

324  {
325  struct x509_link *link;
326 
327  link = list_last_entry ( &chain->links, struct x509_link, list );
328  return ( link ? link->cert : NULL );
329 }
struct list_head links
List of links.
Definition: x509.h:204
#define list_last_entry(list, type, member)
Get the container of the last entry in a list.
Definition: list.h:346
u32 link
Link to next descriptor.
Definition: ar9003_mac.h:68
#define NULL
NULL pointer (VOID *)
Definition: Base.h:321

References link, x509_chain::links, x509_link::list, list_last_entry, and NULL.

Referenced by cms_parse_certificates(), efi_cacert(), tls_parse_chain(), validator_append(), and x509_auto_append().

◆ x509_root_get()

static struct x509_root* x509_root_get ( struct x509_root root)
inlinestatic

Get reference to X.509 root certificate list.

Parameters
rootX.509 root certificate list
Return values
rootX.509 root certificate list

Definition at line 392 of file x509.h.

392  {
393  ref_get ( &root->refcnt );
394  return root;
395 }
struct stp_switch root
Root switch.
Definition: stp.h:26
#define ref_get(refcnt)
Get additional reference to object.
Definition: refcnt.h:92

References ref_get, and root.

Referenced by add_tls(), create_validator(), tls_session(), and x509_set_valid().

◆ x509_root_put()

static void x509_root_put ( struct x509_root root)
inlinestatic

Drop reference to X.509 root certificate list.

Parameters
rootX.509 root certificate list

Definition at line 403 of file x509.h.

403  {
404  ref_put ( &root->refcnt );
405 }
struct stp_switch root
Root switch.
Definition: stp.h:26
#define ref_put(refcnt)
Drop reference to object.
Definition: refcnt.h:106

References ref_put, and root.

Referenced by free_tls(), free_tls_session(), validator_free(), x509_free(), x509_invalidate(), and x509_set_valid().

◆ x509_is_self_signed()

static int x509_is_self_signed ( struct x509_certificate cert)
inlinestatic

Check if X.509 certificate is self-signed.

Parameters
certX.509 certificate
Return values
is_self_signedX.509 certificate is self-signed

Definition at line 413 of file x509.h.

413  {
414  return ( asn1_compare ( &cert->issuer.raw, &cert->subject.raw ) == 0 );
415 }
struct asn1_cursor raw
Raw issuer.
Definition: x509.h:31
int asn1_compare(const struct asn1_cursor *cursor1, const struct asn1_cursor *cursor2)
Compare two ASN.1 objects.
Definition: asn1.c:447
struct x509_issuer issuer
Issuer.
Definition: x509.h:240
struct x509_subject subject
Subject.
Definition: x509.h:244
struct asn1_cursor raw
Raw subject.
Definition: x509.h:61

References asn1_compare(), x509_certificate::issuer, x509_subject::raw, x509_issuer::raw, and x509_certificate::subject.

Referenced by validator_step(), and x509_test_exec().

◆ x509_name()

const char* x509_name ( struct x509_certificate cert)

Get X.509 certificate display name.

Parameters
certX.509 certificate
Return values
nameDisplay name

Definition at line 146 of file x509.c.

146  {
147  struct asn1_cursor *common_name = &cert->subject.common_name;
148  struct digest_algorithm *digest = &sha1_algorithm;
149  static char buf[64];
150  uint8_t fingerprint[ digest->digestsize ];
151  size_t len;
152 
153  len = common_name->len;
154  if ( len ) {
155  /* Certificate has a commonName: use that */
156  if ( len > ( sizeof ( buf ) - 1 /* NUL */ ) )
157  len = ( sizeof ( buf ) - 1 /* NUL */ );
158  memcpy ( buf, common_name->data, len );
159  buf[len] = '\0';
160  } else {
161  /* Certificate has no commonName: use SHA-1 fingerprint */
162  x509_fingerprint ( cert, digest, fingerprint );
163  base16_encode ( fingerprint, sizeof ( fingerprint ),
164  buf, sizeof ( buf ) );
165  }
166  return buf;
167 }
const void * data
Start of data.
Definition: asn1.h:22
size_t len
Length of data.
Definition: asn1.h:24
void * memcpy(void *dest, const void *src, size_t len) __nonnull
ring len
Length.
Definition: dwmac.h:231
struct x509_subject subject
Subject.
Definition: x509.h:244
unsigned char uint8_t
Definition: stdint.h:10
void x509_fingerprint(struct x509_certificate *cert, struct digest_algorithm *digest, void *fingerprint)
Calculate X.509 certificate fingerprint.
Definition: x509.c:1236
struct asn1_cursor common_name
Common name.
Definition: x509.h:63
size_t digestsize
Digest size.
Definition: crypto.h:26
A message digest algorithm.
Definition: crypto.h:18
An ASN.1 object cursor.
Definition: asn1.h:20
struct digest_algorithm sha1_algorithm
SHA-1 algorithm.
Definition: sha1.c:257

References x509_subject::common_name, asn1_cursor::data, digest_algorithm::digestsize, asn1_cursor::len, len, memcpy(), sha1_algorithm, x509_certificate::subject, and x509_fingerprint().

Referenced by certstat(), certstore_add(), certstore_apply_settings(), certstore_del(), certstore_found(), certstore_init(), cms_parse_certificates(), efi_cacert(), icert_encode(), ocsp_check_signature(), ocsp_parse_basic_response(), ocsp_parse_cert_id(), ocsp_parse_certs(), ocsp_parse_responder_id(), ocsp_parse_response_status(), ocsp_parse_response_type(), ocsp_parse_responses(), ocsp_request(), ocsp_uri_string(), ocsp_validate(), tls_new_certificate_request(), tls_parse_chain(), tls_send_certificate(), validator_append(), validator_name(), validator_ocsp_validate(), validator_progress(), validator_start_download(), validator_start_ocsp(), validator_step(), x509_append(), x509_check_alt_name(), x509_check_dnsname(), x509_check_ipaddress(), x509_check_issuer(), x509_check_name(), x509_check_root(), x509_check_signature(), x509_check_time(), x509_parse_subject(), and x509_validate().

◆ x509_parse()

int x509_parse ( struct x509_certificate cert,
const struct asn1_cursor raw 
)

Parse X.509 certificate from ASN.1 data.

Parameters
certX.509 certificate
rawASN.1 cursor
Return values
rcReturn status code

Definition at line 1007 of file x509.c.

1008  {
1009  struct x509_signature *signature = &cert->signature;
1010  struct asn1_algorithm **signature_algorithm = &signature->algorithm;
1011  struct asn1_cursor *signature_value = &signature->value;
1012  struct asn1_cursor cursor;
1013  int rc;
1014 
1015  /* Record raw certificate */
1016  memcpy ( &cursor, raw, sizeof ( cursor ) );
1017  memcpy ( &cert->raw, &cursor, sizeof ( cert->raw ) );
1018 
1019  /* Enter certificate */
1020  asn1_enter ( &cursor, ASN1_SEQUENCE );
1021 
1022  /* Parse tbsCertificate */
1023  if ( ( rc = x509_parse_tbscertificate ( cert, &cursor ) ) != 0 )
1024  return rc;
1025  asn1_skip_any ( &cursor );
1026 
1027  /* Parse signatureAlgorithm */
1028  if ( ( rc = asn1_signature_algorithm ( &cursor,
1029  signature_algorithm ) ) != 0 ) {
1030  DBGC ( cert, "X509 %p could not parse signature algorithm: "
1031  "%s\n", cert, strerror ( rc ) );
1032  return rc;
1033  }
1034  DBGC2 ( cert, "X509 %p signatureAlgorithm is %s\n",
1035  cert, (*signature_algorithm)->name );
1036  asn1_skip_any ( &cursor );
1037 
1038  /* Parse signatureValue */
1039  memcpy ( signature_value, &cursor, sizeof ( *signature_value ) );
1040  if ( ( rc = asn1_enter_bits ( signature_value, NULL ) ) != 0 ) {
1041  DBGC ( cert, "X509 %p could not parse signature value: %s\n",
1042  cert, strerror ( rc ) );
1043  return rc;
1044  }
1045  DBGC2 ( cert, "X509 %p signatureValue is:\n", cert );
1046  DBGC2_HDA ( cert, 0, signature_value->data, signature_value->len );
1047 
1048  /* Check that algorithm in tbsCertificate matches algorithm in
1049  * signature
1050  */
1051  if ( signature->algorithm != (*signature_algorithm) ) {
1052  DBGC ( cert, "X509 %p signature algorithm %s does not match "
1053  "signatureAlgorithm %s\n",
1054  cert, signature->algorithm->name,
1055  (*signature_algorithm)->name );
1056  return -EINVAL_ALGORITHM_MISMATCH;
1057  }
1058 
1059  return 0;
1060 }
An ASN.1 OID-identified algorithm.
Definition: asn1.h:383
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
int asn1_enter(struct asn1_cursor *cursor, unsigned int type)
Enter ASN.1 object.
Definition: asn1.c:181
int asn1_enter_bits(struct asn1_cursor *cursor, unsigned int *unused)
Enter ASN.1 bit string.
Definition: asn1.c:323
const void * data
Start of data.
Definition: asn1.h:22
#define DBGC(...)
Definition: compiler.h:505
int asn1_skip_any(struct asn1_cursor *cursor)
Skip ASN.1 object of any type.
Definition: asn1.c:302
int asn1_signature_algorithm(const struct asn1_cursor *cursor, struct asn1_algorithm **algorithm)
Parse ASN.1 OID-identified signature algorithm.
Definition: asn1.c:613
size_t len
Length of data.
Definition: asn1.h:24
void * memcpy(void *dest, const void *src, size_t len) __nonnull
struct x509_signature signature
Signature.
Definition: x509.h:246
#define DBGC2_HDA(...)
Definition: compiler.h:523
static int x509_parse_tbscertificate(struct x509_certificate *cert, const struct asn1_cursor *raw)
Parse X.509 certificate tbsCertificate.
Definition: x509.c:937
char * strerror(int errno)
Retrieve string representation of error number.
Definition: strerror.c:78
#define ASN1_SEQUENCE
ASN.1 sequence.
Definition: asn1.h:89
#define DBGC2(...)
Definition: compiler.h:522
#define EINVAL_ALGORITHM_MISMATCH
Definition: x509.c:70
__be32 raw[7]
Definition: CIB_PRM.h:28
struct asn1_cursor raw
Raw certificate.
Definition: x509.h:230
u8 signature
CPU signature.
Definition: CIB_PRM.h:35
#define NULL
NULL pointer (VOID *)
Definition: Base.h:321
An ASN.1 object cursor.
Definition: asn1.h:20
An X.509 certificate signature.
Definition: x509.h:69

References asn1_enter(), asn1_enter_bits(), ASN1_SEQUENCE, asn1_signature_algorithm(), asn1_skip_any(), asn1_cursor::data, DBGC, DBGC2, DBGC2_HDA, EINVAL_ALGORITHM_MISMATCH, asn1_cursor::len, memcpy(), NULL, raw, x509_certificate::raw, rc, signature, x509_certificate::signature, strerror(), and x509_parse_tbscertificate().

Referenced by certstore_init(), and x509_certificate().

◆ x509_certificate()

int x509_certificate ( const void *  data,
size_t  len,
struct x509_certificate **  cert 
)

Create X.509 certificate.

Parameters
dataRaw certificate data
lenLength of raw data
Return values
certX.509 certificate
rcReturn status code

On success, the caller holds a reference to the X.509 certificate, and is responsible for ultimately calling x509_put().

Definition at line 1073 of file x509.c.

1074  {
1075  struct asn1_cursor cursor;
1076  void *raw;
1077  int rc;
1078 
1079  /* Initialise cursor */
1080  cursor.data = data;
1081  cursor.len = len;
1082  asn1_shrink_any ( &cursor );
1083 
1084  /* Return stored certificate, if present */
1085  if ( ( *cert = x509_find ( NULL, &cursor ) ) != NULL ) {
1086 
1087  /* Add caller's reference */
1088  x509_get ( *cert );
1089  return 0;
1090  }
1091 
1092  /* Allocate and initialise certificate */
1093  *cert = zalloc ( sizeof ( **cert ) + cursor.len );
1094  if ( ! *cert )
1095  return -ENOMEM;
1096  ref_init ( &(*cert)->refcnt, x509_free );
1097  raw = ( *cert + 1 );
1098 
1099  /* Copy raw data */
1100  memcpy ( raw, cursor.data, cursor.len );
1101  cursor.data = raw;
1102 
1103  /* Parse certificate */
1104  if ( ( rc = x509_parse ( *cert, &cursor ) ) != 0 ) {
1105  x509_put ( *cert );
1106  *cert = NULL;
1107  return rc;
1108  }
1109 
1110  /* Add certificate to store */
1111  certstore_add ( *cert );
1112 
1113  return 0;
1114 }
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
static struct x509_certificate * x509_get(struct x509_certificate *cert)
Get reference to X.509 certificate.
Definition: x509.h:266
static void x509_free(struct refcnt *refcnt)
Free X.509 certificate.
Definition: x509.c:132
#define ref_init(refcnt, free)
Initialise a reference counter.
Definition: refcnt.h:64
void certstore_add(struct x509_certificate *cert)
Add certificate to store.
Definition: certstore.c:100
#define ENOMEM
Not enough space.
Definition: errno.h:534
void * memcpy(void *dest, const void *src, size_t len) __nonnull
ring len
Length.
Definition: dwmac.h:231
void * zalloc(size_t size)
Allocate cleared memory.
Definition: malloc.c:661
struct x509_certificate * x509_find(struct x509_chain *store, const struct asn1_cursor *raw)
Identify X.509 certificate by raw certificate data.
Definition: x509.c:1745
int asn1_shrink_any(struct asn1_cursor *cursor)
Shrink ASN.1 object of any type.
Definition: asn1.c:312
static void x509_put(struct x509_certificate *cert)
Drop reference to X.509 certificate.
Definition: x509.h:277
uint8_t data[48]
Additional event data.
Definition: ena.h:22
__be32 raw[7]
Definition: CIB_PRM.h:28
struct arbelprm_wqe_segment_data_ptr data[ARBEL_MAX_GATHER]
Definition: arbel.h:236
#define NULL
NULL pointer (VOID *)
Definition: Base.h:321
An ASN.1 object cursor.
Definition: asn1.h:20
int x509_parse(struct x509_certificate *cert, const struct asn1_cursor *raw)
Parse X.509 certificate from ASN.1 data.
Definition: x509.c:1007

References asn1_shrink_any(), certstore_add(), data, asn1_cursor::data, ENOMEM, asn1_cursor::len, len, memcpy(), NULL, raw, rc, ref_init, x509_find(), x509_free(), x509_get(), x509_parse(), x509_put(), and zalloc().

◆ x509_is_valid()

int x509_is_valid ( struct x509_certificate cert,
struct x509_root root 
)

Check if X.509 certificate is valid.

Parameters
certX.509 certificate
rootRoot certificate list, or NULL to use default

Definition at line 1312 of file x509.c.

1312  {
1313 
1314  /* Use default root certificate store if none specified */
1315  if ( ! root )
1317 
1318  return ( cert->root == root );
1319 }
struct stp_switch root
Root switch.
Definition: stp.h:26
struct x509_root root_certificates
Root certificates.
Definition: rootcert.c:78
struct x509_root * root
Root against which certificate has been validated (if any)
Definition: x509.h:225

References root, x509_certificate::root, and root_certificates.

Referenced by certstat(), ipair_window_changed(), validator_step(), x509_validate(), and x509_validate_chain_okx().

◆ x509_set_valid()

void x509_set_valid ( struct x509_certificate cert,
struct x509_certificate issuer,
struct x509_root root 
)

Set X.509 certificate as validated.

Parameters
certX.509 certificate
issuerIssuing X.509 certificate (or NULL)
rootRoot certificate list

Definition at line 1328 of file x509.c.

1330  {
1331  unsigned int max_path_remaining;
1332 
1333  /* Sanity checks */
1334  assert ( root != NULL );
1335  assert ( ( issuer == NULL ) || ( issuer->path_remaining >= 1 ) );
1336 
1337  /* Record validation root */
1338  x509_root_put ( cert->root );
1339  cert->root = x509_root_get ( root );
1340 
1341  /* Calculate effective path length */
1342  cert->path_remaining = ( cert->extensions.basic.path_len + 1 );
1343  if ( issuer ) {
1344  max_path_remaining = ( issuer->path_remaining - 1 );
1345  if ( cert->path_remaining > max_path_remaining )
1346  cert->path_remaining = max_path_remaining;
1347  }
1348 }
unsigned int path_remaining
Maximum number of subsequent certificates in chain.
Definition: x509.h:227
unsigned int path_len
Path length.
Definition: x509.h:81
struct stp_switch root
Root switch.
Definition: stp.h:26
static void x509_root_put(struct x509_root *root)
Drop reference to X.509 root certificate list.
Definition: x509.h:403
assert((readw(&hdr->flags) &(GTF_reading|GTF_writing))==0)
struct x509_root * root
Root against which certificate has been validated (if any)
Definition: x509.h:225
static struct x509_root * x509_root_get(struct x509_root *root)
Get reference to X.509 root certificate list.
Definition: x509.h:392
#define NULL
NULL pointer (VOID *)
Definition: Base.h:321
struct x509_basic_constraints basic
Basic constraints.
Definition: x509.h:158
struct x509_extensions extensions
Extensions.
Definition: x509.h:248

References assert(), x509_extensions::basic, x509_certificate::extensions, NULL, x509_basic_constraints::path_len, x509_certificate::path_remaining, root, x509_certificate::root, x509_root_get(), and x509_root_put().

Referenced by efi_cacert(), and x509_validate().

◆ x509_validate()

int x509_validate ( struct x509_certificate cert,
struct x509_certificate issuer,
time_t  time,
struct x509_root root 
)

Validate X.509 certificate.

Parameters
certX.509 certificate
issuerIssuing X.509 certificate (or NULL)
timeTime at which to validate certificate
rootRoot certificate list, or NULL to use default
Return values
rcReturn status code

The issuing certificate must have already been validated.

Validation results are cached: if a certificate has already been successfully validated then issuer, time, and root will be ignored.

Definition at line 1365 of file x509.c.

1367  {
1368  int rc;
1369 
1370  /* Use default root certificate store if none specified */
1371  if ( ! root )
1373 
1374  /* Return success if certificate has already been validated */
1375  if ( x509_is_valid ( cert, root ) )
1376  return 0;
1377 
1378  /* Fail if certificate is invalid at specified time */
1379  if ( ( rc = x509_check_time ( cert, time ) ) != 0 )
1380  return rc;
1381 
1382  /* Succeed if certificate is a trusted root certificate */
1383  if ( x509_check_root ( cert, root ) == 0 ) {
1384  x509_set_valid ( cert, NULL, root );
1385  return 0;
1386  }
1387 
1388  /* Fail unless we have an issuer */
1389  if ( ! issuer ) {
1390  DBGC2 ( cert, "X509 %p \"%s\" has no trusted issuer\n",
1391  cert, x509_name ( cert ) );
1392  return -EACCES_UNTRUSTED;
1393  }
1394 
1395  /* Fail unless issuer has already been validated */
1396  if ( ! x509_is_valid ( issuer, root ) ) {
1397  DBGC ( cert, "X509 %p \"%s\" ", cert, x509_name ( cert ) );
1398  DBGC ( cert, "issuer %p \"%s\" has not yet been validated\n",
1399  issuer, x509_name ( issuer ) );
1400  return -EACCES_OUT_OF_ORDER;
1401  }
1402 
1403  /* Fail if issuing certificate cannot validate this certificate */
1404  if ( ( rc = x509_check_issuer ( cert, issuer ) ) != 0 )
1405  return rc;
1406 
1407  /* Fail if path length constraint is violated */
1408  if ( issuer->path_remaining == 0 ) {
1409  DBGC ( cert, "X509 %p \"%s\" ", cert, x509_name ( cert ) );
1410  DBGC ( cert, "issuer %p \"%s\" path length exceeded\n",
1411  issuer, x509_name ( issuer ) );
1412  return -EACCES_PATH_LEN;
1413  }
1414 
1415  /* Fail if OCSP is required */
1416  if ( ocsp_required ( cert ) ) {
1417  DBGC ( cert, "X509 %p \"%s\" requires an OCSP check\n",
1418  cert, x509_name ( cert ) );
1419  return -EACCES_OCSP_REQUIRED;
1420  }
1421 
1422  /* Mark certificate as valid */
1423  x509_set_valid ( cert, issuer, root );
1424 
1425  DBGC ( cert, "X509 %p \"%s\" successfully validated using ",
1426  cert, x509_name ( cert ) );
1427  DBGC ( cert, "issuer %p \"%s\"\n", issuer, x509_name ( issuer ) );
1428  return 0;
1429 }
void x509_set_valid(struct x509_certificate *cert, struct x509_certificate *issuer, struct x509_root *root)
Set X.509 certificate as validated.
Definition: x509.c:1328
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
unsigned int path_remaining
Maximum number of subsequent certificates in chain.
Definition: x509.h:227
struct stp_switch root
Root switch.
Definition: stp.h:26
struct x509_root root_certificates
Root certificates.
Definition: rootcert.c:78
#define DBGC(...)
Definition: compiler.h:505
int x509_is_valid(struct x509_certificate *cert, struct x509_root *root)
Check if X.509 certificate is valid.
Definition: x509.c:1312
static int ocsp_required(struct x509_certificate *cert)
Check if X.509 certificate requires an OCSP check.
Definition: ocsp.h:128
#define EACCES_OCSP_REQUIRED
Definition: x509.c:114
#define EACCES_OUT_OF_ORDER
Definition: x509.c:106
#define EACCES_PATH_LEN
Definition: x509.c:98
int x509_check_root(struct x509_certificate *cert, struct x509_root *root)
Check X.509 root certificate.
Definition: x509.c:1254
const char * x509_name(struct x509_certificate *cert)
Get X.509 certificate display name.
Definition: x509.c:146
#define DBGC2(...)
Definition: compiler.h:522
int x509_check_issuer(struct x509_certificate *cert, struct x509_certificate *issuer)
Check X.509 certificate against issuer certificate.
Definition: x509.c:1176
#define EACCES_UNTRUSTED
Definition: x509.c:102
#define NULL
NULL pointer (VOID *)
Definition: Base.h:321
int x509_check_time(struct x509_certificate *cert, time_t time)
Check X.509 certificate validity period.
Definition: x509.c:1286

References DBGC, DBGC2, EACCES_OCSP_REQUIRED, EACCES_OUT_OF_ORDER, EACCES_PATH_LEN, EACCES_UNTRUSTED, NULL, ocsp_required(), x509_certificate::path_remaining, rc, root, root_certificates, x509_check_issuer(), x509_check_root(), x509_check_time(), x509_is_valid(), x509_name(), and x509_set_valid().

Referenced by ocsp_validate(), and x509_validate_chain().

◆ x509_check_name()

int x509_check_name ( struct x509_certificate cert,
const char *  name 
)

Check X.509 certificate name.

Parameters
certX.509 certificate
nameName
Return values
rcReturn status code

Definition at line 1563 of file x509.c.

1563  {
1564  struct asn1_cursor *common_name = &cert->subject.common_name;
1565  struct asn1_cursor alt_name;
1566  int rc;
1567 
1568  /* Check commonName */
1569  if ( x509_check_dnsname ( cert, common_name, name ) == 0 ) {
1570  DBGC2 ( cert, "X509 %p \"%s\" commonName matches \"%s\"\n",
1571  cert, x509_name ( cert ), name );
1572  return 0;
1573  }
1574 
1575  /* Check any subjectAlternativeNames */
1576  memcpy ( &alt_name, &cert->extensions.alt_name.names,
1577  sizeof ( alt_name ) );
1578  for ( ; alt_name.len ; asn1_skip_any ( &alt_name ) ) {
1579  if ( ( rc = x509_check_alt_name ( cert, &alt_name,
1580  name ) ) == 0 ) {
1581  DBGC2 ( cert, "X509 %p \"%s\" subjectAltName matches "
1582  "\"%s\"\n", cert, x509_name ( cert ), name );
1583  return 0;
1584  }
1585  }
1586 
1587  DBGC ( cert, "X509 %p \"%s\" does not match name \"%s\"\n",
1588  cert, x509_name ( cert ), name );
1589  return -EACCES_WRONG_NAME;
1590 }
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
const char * name
Definition: ath9k_hw.c:1984
struct asn1_cursor names
Names.
Definition: x509.h:145
static int x509_check_alt_name(struct x509_certificate *cert, const struct asn1_cursor *raw, const char *name)
Check X.509 certificate alternative name.
Definition: x509.c:1531
#define DBGC(...)
Definition: compiler.h:505
int asn1_skip_any(struct asn1_cursor *cursor)
Skip ASN.1 object of any type.
Definition: asn1.c:302
void * memcpy(void *dest, const void *src, size_t len) __nonnull
#define EACCES_WRONG_NAME
Definition: x509.c:118
static int x509_check_dnsname(struct x509_certificate *cert, const struct asn1_cursor *raw, const char *name)
Check X.509 certificate alternative dNSName.
Definition: x509.c:1439
struct x509_subject subject
Subject.
Definition: x509.h:244
struct asn1_cursor common_name
Common name.
Definition: x509.h:63
const char * x509_name(struct x509_certificate *cert)
Get X.509 certificate display name.
Definition: x509.c:146
struct x509_subject_alt_name alt_name
Subject alternative name.
Definition: x509.h:166
#define DBGC2(...)
Definition: compiler.h:522
An ASN.1 object cursor.
Definition: asn1.h:20
struct x509_extensions extensions
Extensions.
Definition: x509.h:248

References x509_extensions::alt_name, asn1_skip_any(), x509_subject::common_name, DBGC, DBGC2, EACCES_WRONG_NAME, x509_certificate::extensions, asn1_cursor::len, memcpy(), name, x509_subject_alt_name::names, rc, x509_certificate::subject, x509_check_alt_name(), x509_check_dnsname(), and x509_name().

Referenced by cert_exec(), cms_verify(), tls_validator_done(), x509_check_name_fail_okx(), and x509_check_name_okx().

◆ x509_alloc_chain()

struct x509_chain* x509_alloc_chain ( void  )

Allocate X.509 certificate chain.

Return values
chainX.509 certificate chain, or NULL

Definition at line 1614 of file x509.c.

1614  {
1615  struct x509_chain *chain;
1616 
1617  /* Allocate chain */
1618  chain = zalloc ( sizeof ( *chain ) );
1619  if ( ! chain )
1620  return NULL;
1621 
1622  /* Initialise chain */
1623  ref_init ( &chain->refcnt, x509_free_chain );
1624  INIT_LIST_HEAD ( &chain->links );
1625 
1626  DBGC2 ( chain, "X509 chain %p allocated\n", chain );
1627  return chain;
1628 }
struct list_head links
List of links.
Definition: x509.h:204
#define ref_init(refcnt, free)
Initialise a reference counter.
Definition: refcnt.h:64
static void x509_free_chain(struct refcnt *refcnt)
Free X.509 certificate chain.
Definition: x509.c:1597
An X.509 certificate chain.
Definition: x509.h:200
void * zalloc(size_t size)
Allocate cleared memory.
Definition: malloc.c:661
#define INIT_LIST_HEAD(list)
Initialise a list head.
Definition: list.h:45
#define DBGC2(...)
Definition: compiler.h:522
#define NULL
NULL pointer (VOID *)
Definition: Base.h:321
struct refcnt refcnt
Reference count.
Definition: x509.h:202

References DBGC2, INIT_LIST_HEAD, x509_chain::links, NULL, ref_init, x509_chain::refcnt, x509_free_chain(), and zalloc().

Referenced by cms_parse_participants(), cms_parse_signed(), tls_new_certificate_request(), tls_parse_chain(), validator_append(), and x509_chain_okx().

◆ x509_append()

int x509_append ( struct x509_chain chain,
struct x509_certificate cert 
)

Append X.509 certificate to X.509 certificate chain.

Parameters
chainX.509 certificate chain
certX.509 certificate
Return values
rcReturn status code

Definition at line 1637 of file x509.c.

1637  {
1638  struct x509_link *link;
1639  int rc;
1640 
1641  /* Ensure allocation of link cannot invalidate certificate */
1642  x509_get ( cert );
1643 
1644  /* Allocate link */
1645  link = zalloc ( sizeof ( *link ) );
1646  if ( ! link ) {
1647  rc = -ENOMEM;
1648  goto err_alloc;
1649  }
1650 
1651  /* Add link to chain */
1652  link->cert = x509_get ( cert );
1653  list_add_tail ( &link->list, &chain->links );
1654  DBGC ( chain, "X509 chain %p added X509 %p \"%s\"\n",
1655  chain, cert, x509_name ( cert ) );
1656 
1657  /* Success */
1658  rc = 0;
1659 
1660  x509_put ( cert );
1661  err_alloc:
1662  return rc;
1663 }
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
static struct x509_certificate * x509_get(struct x509_certificate *cert)
Get reference to X.509 certificate.
Definition: x509.h:266
struct list_head links
List of links.
Definition: x509.h:204
#define DBGC(...)
Definition: compiler.h:505
#define ENOMEM
Not enough space.
Definition: errno.h:534
#define list_add_tail(new, head)
Add a new entry to the tail of a list.
Definition: list.h:93
u32 link
Link to next descriptor.
Definition: ar9003_mac.h:68
void * zalloc(size_t size)
Allocate cleared memory.
Definition: malloc.c:661
const char * x509_name(struct x509_certificate *cert)
Get X.509 certificate display name.
Definition: x509.c:146
static void x509_put(struct x509_certificate *cert)
Drop reference to X.509 certificate.
Definition: x509.h:277

References x509_link::cert, DBGC, ENOMEM, link, x509_chain::links, list_add_tail, rc, x509_get(), x509_name(), x509_put(), and zalloc().

Referenced by cms_parse_identifier(), tls_new_certificate_request(), x509_append_raw(), x509_auto_append(), and x509_chain_okx().

◆ x509_append_raw()

int x509_append_raw ( struct x509_chain chain,
const void *  data,
size_t  len 
)

Append X.509 certificate to X.509 certificate chain.

Parameters
chainX.509 certificate chain
dataRaw certificate data
lenLength of raw data
Return values
rcReturn status code

Definition at line 1673 of file x509.c.

1674  {
1675  struct x509_certificate *cert;
1676  int rc;
1677 
1678  /* Parse certificate */
1679  if ( ( rc = x509_certificate ( data, len, &cert ) ) != 0 )
1680  goto err_parse;
1681 
1682  /* Append certificate to chain */
1683  if ( ( rc = x509_append ( chain, cert ) ) != 0 )
1684  goto err_append;
1685 
1686  /* Drop reference to certificate */
1687  x509_put ( cert );
1688 
1689  return 0;
1690 
1691  err_append:
1692  x509_put ( cert );
1693  err_parse:
1694  return rc;
1695 }
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
int x509_append(struct x509_chain *chain, struct x509_certificate *cert)
Append X.509 certificate to X.509 certificate chain.
Definition: x509.c:1637
ring len
Length.
Definition: dwmac.h:231
An X.509 certificate.
Definition: x509.h:215
static void x509_put(struct x509_certificate *cert)
Drop reference to X.509 certificate.
Definition: x509.h:277
uint8_t data[48]
Additional event data.
Definition: ena.h:22

References data, len, rc, x509_append(), and x509_put().

Referenced by cms_parse_certificates(), efi_cacert(), tls_parse_chain(), and validator_append().

◆ x509_truncate()

void x509_truncate ( struct x509_chain chain,
struct x509_link link 
)

Truncate X.509 certificate chain.

Parameters
chainX.509 certificate chain
linkLink after which to truncate chain, or NULL

Definition at line 1703 of file x509.c.

1703  {
1704  struct x509_link *tmp;
1705 
1706  /* Truncate entire chain if no link is specified */
1707  if ( ! link )
1708  link = list_entry ( &chain->links, struct x509_link, list );
1709 
1710  /* Free each link in the chain */
1712  x509_put ( link->cert );
1713  list_del ( &link->list );
1714  free ( link );
1715  }
1716 }
struct list_head links
List of links.
Definition: x509.h:204
unsigned long tmp
Definition: linux_pci.h:64
#define list_del(list)
Delete an entry from a list.
Definition: list.h:119
#define list_for_each_entry_safe_continue(pos, tmp, head, member)
Iterate over subsequent entries in a list, safe against deletion.
Definition: list.h:500
u32 link
Link to next descriptor.
Definition: ar9003_mac.h:68
static void(* free)(struct refcnt *refcnt))
Definition: refcnt.h:54
static void x509_put(struct x509_certificate *cert)
Drop reference to X.509 certificate.
Definition: x509.h:277
#define list_entry(list, type, member)
Get the container of a list entry.
Definition: list.h:321

References free, link, x509_chain::links, x509_link::list, list_del, list_entry, list_for_each_entry_safe_continue, tmp, and x509_put().

Referenced by efi_cacert_shutdown(), validator_append(), x509_free_chain(), and x509_test_exec().

◆ x509_find()

struct x509_certificate* x509_find ( struct x509_chain store,
const struct asn1_cursor raw 
)

Identify X.509 certificate by raw certificate data.

Parameters
storeCertificate store, or NULL to use default
rawRaw certificate data
Return values
certX.509 certificate, or NULL if not found

Definition at line 1745 of file x509.c.

1746  {
1747  struct x509_link *link;
1748  struct x509_certificate *cert;
1749 
1750  /* Use default certificate store if none specified */
1751  if ( ! store )
1752  store = &certstore;
1753 
1754  /* Search for certificate within store */
1755  list_for_each_entry ( link, &store->links, list ) {
1756 
1757  /* Check raw certificate data */
1758  cert = link->cert;
1759  if ( asn1_compare ( raw, &cert->raw ) == 0 )
1760  return x509_found ( store, cert );
1761  }
1762 
1763  return NULL;
1764 }
int asn1_compare(const struct asn1_cursor *cursor1, const struct asn1_cursor *cursor2)
Compare two ASN.1 objects.
Definition: asn1.c:447
struct x509_chain certstore
Certificate store.
Definition: certstore.c:89
#define list_for_each_entry(pos, head, member)
Iterate over entries in a list.
Definition: list.h:431
u32 link
Link to next descriptor.
Definition: ar9003_mac.h:68
An X.509 certificate.
Definition: x509.h:215
struct x509_link store
Link in certificate store.
Definition: x509.h:220
__be32 raw[7]
Definition: CIB_PRM.h:28
static struct x509_certificate * x509_found(struct x509_chain *store, struct x509_certificate *cert)
Mark X.509 certificate as found.
Definition: x509.c:1725
struct asn1_cursor raw
Raw certificate.
Definition: x509.h:230
#define NULL
NULL pointer (VOID *)
Definition: Base.h:321

References asn1_compare(), certstore, link, list_for_each_entry, NULL, raw, x509_certificate::raw, x509_certificate::store, and x509_found().

Referenced by certstore_init(), and x509_certificate().

◆ x509_find_subject()

struct x509_certificate* x509_find_subject ( struct x509_chain store,
const struct asn1_cursor subject 
)

Identify X.509 certificate by subject.

Parameters
storeCertificate store, or NULL to use default
subjectSubject
Return values
certX.509 certificate, or NULL if not found

Definition at line 1774 of file x509.c.

1775  {
1776  struct x509_link *link;
1777  struct x509_certificate *cert;
1778 
1779  /* Use default certificate store if none specified */
1780  if ( ! store )
1781  store = &certstore;
1782 
1783  /* Scan through certificate list */
1784  list_for_each_entry ( link, &store->links, list ) {
1785 
1786  /* Check subject */
1787  cert = link->cert;
1788  if ( asn1_compare ( subject, &cert->subject.raw ) == 0 )
1789  return x509_found ( store, cert );
1790  }
1791 
1792  return NULL;
1793 }
int asn1_compare(const struct asn1_cursor *cursor1, const struct asn1_cursor *cursor2)
Compare two ASN.1 objects.
Definition: asn1.c:447
struct x509_chain certstore
Certificate store.
Definition: certstore.c:89
#define list_for_each_entry(pos, head, member)
Iterate over entries in a list.
Definition: list.h:431
u32 link
Link to next descriptor.
Definition: ar9003_mac.h:68
An X.509 certificate.
Definition: x509.h:215
struct x509_subject subject
Subject.
Definition: x509.h:244
struct asn1_cursor raw
Raw subject.
Definition: x509.h:61
struct x509_link store
Link in certificate store.
Definition: x509.h:220
static struct x509_certificate * x509_found(struct x509_chain *store, struct x509_certificate *cert)
Mark X.509 certificate as found.
Definition: x509.c:1725
#define NULL
NULL pointer (VOID *)
Definition: Base.h:321

References asn1_compare(), certstore, link, list_for_each_entry, NULL, x509_subject::raw, x509_certificate::store, x509_certificate::subject, and x509_found().

Referenced by x509_auto_append().

◆ x509_find_issuer_serial()

struct x509_certificate* x509_find_issuer_serial ( struct x509_chain store,
const struct asn1_cursor issuer,
const struct asn1_cursor serial 
)

Identify X.509 certificate by issuer and serial number.

Parameters
storeCertificate store, or NULL to use default
issuerIssuer
serialSerial number
Return values
certX.509 certificate, or NULL if not found

Definition at line 1804 of file x509.c.

1806  {
1807  struct x509_link *link;
1808  struct x509_certificate *cert;
1809 
1810  /* Use default certificate store if none specified */
1811  if ( ! store )
1812  store = &certstore;
1813 
1814  /* Scan through certificate list */
1815  list_for_each_entry ( link, &store->links, list ) {
1816 
1817  /* Check issuer and serial number */
1818  cert = link->cert;
1819  if ( ( asn1_compare ( issuer, &cert->issuer.raw ) == 0 ) &&
1820  ( asn1_compare ( serial, &cert->serial.raw ) == 0 ) )
1821  return x509_found ( store, cert );
1822  }
1823 
1824  return NULL;
1825 }
struct asn1_cursor raw
Raw issuer.
Definition: x509.h:31
int asn1_compare(const struct asn1_cursor *cursor1, const struct asn1_cursor *cursor2)
Compare two ASN.1 objects.
Definition: asn1.c:447
struct x509_chain certstore
Certificate store.
Definition: certstore.c:89
struct x509_issuer issuer
Issuer.
Definition: x509.h:240
struct asn1_cursor raw
Raw serial number.
Definition: x509.h:25
#define list_for_each_entry(pos, head, member)
Iterate over entries in a list.
Definition: list.h:431
u32 link
Link to next descriptor.
Definition: ar9003_mac.h:68
An X.509 certificate.
Definition: x509.h:215
struct x509_serial serial
Serial number.
Definition: x509.h:234
uint64_t serial
Serial number.
Definition: edd.h:30
struct x509_link store
Link in certificate store.
Definition: x509.h:220
static struct x509_certificate * x509_found(struct x509_chain *store, struct x509_certificate *cert)
Mark X.509 certificate as found.
Definition: x509.c:1725
#define NULL
NULL pointer (VOID *)
Definition: Base.h:321

References asn1_compare(), certstore, x509_certificate::issuer, link, list_for_each_entry, NULL, x509_issuer::raw, x509_serial::raw, serial, x509_certificate::serial, x509_certificate::store, and x509_found().

Referenced by cms_parse_identifier().

◆ x509_find_key()

struct x509_certificate* x509_find_key ( struct x509_chain store,
struct private_key key 
)

Identify X.509 certificate by corresponding public key.

Parameters
storeCertificate store, or NULL to use default
keyPrivate key
Return values
certX.509 certificate, or NULL if not found

Definition at line 1834 of file x509.c.

1835  {
1836  struct x509_link *link;
1837  struct x509_certificate *cert;
1838 
1839  /* Use default certificate store if none specified */
1840  if ( ! store )
1841  store = &certstore;
1842 
1843  /* Scan through certificate list */
1844  list_for_each_entry ( link, &store->links, list ) {
1845 
1846  /* Check public key */
1847  cert = link->cert;
1848  if ( pubkey_match ( cert->signature_algorithm->pubkey,
1849  privkey_cursor ( key ),
1850  &cert->subject.public_key.raw ) == 0 )
1851  return x509_found ( store, cert );
1852  }
1853 
1854  return NULL;
1855 }
struct asn1_cursor raw
Raw public key information.
Definition: x509.h:51
struct x509_chain certstore
Certificate store.
Definition: certstore.c:89
struct asn1_algorithm * signature_algorithm
Signature algorithm.
Definition: x509.h:238
static int pubkey_match(struct pubkey_algorithm *pubkey, const struct asn1_cursor *private_key, const struct asn1_cursor *public_key)
Definition: crypto.h:292
static struct asn1_cursor * privkey_cursor(struct private_key *key)
Get private key ASN.1 cursor.
Definition: privkey.h:52
struct pubkey_algorithm * pubkey
Public-key algorithm (if applicable)
Definition: asn1.h:389
#define list_for_each_entry(pos, head, member)
Iterate over entries in a list.
Definition: list.h:431
u32 link
Link to next descriptor.
Definition: ar9003_mac.h:68
struct x509_public_key public_key
Public key information.
Definition: x509.h:65
An X.509 certificate.
Definition: x509.h:215
struct x509_subject subject
Subject.
Definition: x509.h:244
struct x509_link store
Link in certificate store.
Definition: x509.h:220
static struct x509_certificate * x509_found(struct x509_chain *store, struct x509_certificate *cert)
Mark X.509 certificate as found.
Definition: x509.c:1725
#define NULL
NULL pointer (VOID *)
Definition: Base.h:321
union @391 key
Sense key.
Definition: scsi.h:17

References certstore, key, link, list_for_each_entry, NULL, privkey_cursor(), asn1_algorithm::pubkey, pubkey_match(), x509_subject::public_key, x509_public_key::raw, x509_certificate::signature_algorithm, x509_certificate::store, x509_certificate::subject, and x509_found().

Referenced by cms_keypair_okx(), cms_recipient(), and tls_new_certificate_request().

◆ x509_auto_append()

int x509_auto_append ( struct x509_chain chain,
struct x509_chain store 
)

Append X.509 certificates to X.509 certificate chain.

Parameters
chainX.509 certificate chain
storeCertificate store, or NULL to use default
Return values
rcReturn status code

Certificates will be automatically appended to the chain based upon the subject and issuer names.

Definition at line 1867 of file x509.c.

1867  {
1868  struct x509_certificate *cert;
1869  struct x509_certificate *previous;
1870  int rc;
1871 
1872  /* Get current certificate */
1873  cert = x509_last ( chain );
1874  if ( ! cert ) {
1875  DBGC ( chain, "X509 chain %p has no certificates\n", chain );
1876  return -EACCES_EMPTY;
1877  }
1878 
1879  /* Append certificates, in order */
1880  while ( 1 ) {
1881 
1882  /* Find issuing certificate */
1883  previous = cert;
1884  cert = x509_find_subject ( store, &cert->issuer.raw );
1885  if ( ! cert )
1886  break;
1887  if ( cert == previous )
1888  break;
1889 
1890  /* Append certificate to chain */
1891  if ( ( rc = x509_append ( chain, cert ) ) != 0 )
1892  return rc;
1893  }
1894 
1895  return 0;
1896 }
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
struct asn1_cursor raw
Raw issuer.
Definition: x509.h:31
#define EACCES_EMPTY
Definition: x509.c:110
struct x509_issuer issuer
Issuer.
Definition: x509.h:240
#define DBGC(...)
Definition: compiler.h:505
int x509_append(struct x509_chain *chain, struct x509_certificate *cert)
Append X.509 certificate to X.509 certificate chain.
Definition: x509.c:1637
static struct x509_certificate * x509_last(struct x509_chain *chain)
Get last certificate in X.509 certificate chain.
Definition: x509.h:324
An X.509 certificate.
Definition: x509.h:215
struct x509_certificate * x509_find_subject(struct x509_chain *store, const struct asn1_cursor *subject)
Identify X.509 certificate by subject.
Definition: x509.c:1774
struct x509_link store
Link in certificate store.
Definition: x509.h:220

References DBGC, EACCES_EMPTY, x509_certificate::issuer, x509_issuer::raw, rc, x509_certificate::store, x509_append(), x509_find_subject(), and x509_last().

Referenced by cms_parse_identifier(), tls_new_certificate_request(), validator_append(), and x509_validate_chain().

◆ x509_validate_chain()

int x509_validate_chain ( struct x509_chain chain,
time_t  time,
struct x509_chain store,
struct x509_root root 
)

Validate X.509 certificate chain.

Parameters
chainX.509 certificate chain
timeTime at which to validate certificates
storeCertificate store, or NULL to use default
rootRoot certificate list, or NULL to use default
Return values
rcReturn status code

Definition at line 1907 of file x509.c.

1908  {
1909  struct x509_certificate *issuer = NULL;
1910  struct x509_link *link;
1911  int rc;
1912 
1913  /* Append any applicable certificates from the certificate store */
1914  if ( ( rc = x509_auto_append ( chain, store ) ) != 0 )
1915  return rc;
1916 
1917  /* Find first certificate that can be validated as a
1918  * standalone (i.e. is already valid, or can be validated as
1919  * a trusted root certificate).
1920  */
1921  list_for_each_entry ( link, &chain->links, list ) {
1922 
1923  /* Try validating this certificate as a standalone */
1924  if ( ( rc = x509_validate ( link->cert, NULL, time,
1925  root ) ) != 0 )
1926  continue;
1927 
1928  /* Work back up to start of chain, performing pairwise
1929  * validation.
1930  */
1931  issuer = link->cert;
1933  list ) {
1934 
1935  /* Validate this certificate against its issuer */
1936  if ( ( rc = x509_validate ( link->cert, issuer, time,
1937  root ) ) != 0 )
1938  return rc;
1939  issuer = link->cert;
1940  }
1941 
1942  return 0;
1943  }
1944 
1945  DBGC ( chain, "X509 chain %p found no usable certificates\n", chain );
1946  return -EACCES_USELESS;
1947 }
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
struct stp_switch root
Root switch.
Definition: stp.h:26
struct list_head links
List of links.
Definition: x509.h:204
struct x509_issuer issuer
Issuer.
Definition: x509.h:240
#define DBGC(...)
Definition: compiler.h:505
#define list_for_each_entry(pos, head, member)
Iterate over entries in a list.
Definition: list.h:431
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:1365
u32 link
Link to next descriptor.
Definition: ar9003_mac.h:68
int x509_auto_append(struct x509_chain *chain, struct x509_chain *store)
Append X.509 certificates to X.509 certificate chain.
Definition: x509.c:1867
An X.509 certificate.
Definition: x509.h:215
#define list_for_each_entry_continue_reverse(pos, head, member)
Iterate over entries in a list in reverse, starting after current position.
Definition: list.h:486
#define EACCES_USELESS
Definition: x509.c:122
#define NULL
NULL pointer (VOID *)
Definition: Base.h:321

References DBGC, EACCES_USELESS, x509_certificate::issuer, link, x509_chain::links, x509_link::list, list_for_each_entry, list_for_each_entry_continue_reverse, NULL, rc, root, x509_auto_append(), and x509_validate().

Referenced by cms_verify_signer(), validator_step(), x509_validate_chain_fail_okx(), and x509_validate_chain_okx().

◆ image_x509()

int image_x509 ( struct image image,
size_t  offset,
struct x509_certificate **  cert 
)

Extract X.509 certificate object from image.

Parameters
imageImage
offsetOffset within image
Return values
certX.509 certificate
nextOffset to next image, or negative error

On success, the caller holds a reference to the X.509 certificate, and is responsible for ultimately calling x509_put().

Definition at line 1960 of file x509.c.

1961  {
1962  struct asn1_cursor *cursor;
1963  int next;
1964  int rc;
1965 
1966  /* Get ASN.1 object */
1967  next = image_asn1 ( image, offset, &cursor );
1968  if ( next < 0 ) {
1969  rc = next;
1970  goto err_asn1;
1971  }
1972 
1973  /* Parse certificate */
1974  if ( ( rc = x509_certificate ( cursor->data, cursor->len,
1975  cert ) ) != 0 )
1976  goto err_certificate;
1977 
1978  /* Free ASN.1 object */
1979  free ( cursor );
1980 
1981  return next;
1982 
1983  x509_put ( *cert );
1984  err_certificate:
1985  free ( cursor );
1986  err_asn1:
1987  return rc;
1988 }
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
const void * data
Start of data.
Definition: asn1.h:22
int image_asn1(struct image *image, size_t offset, struct asn1_cursor **cursor)
Extract ASN.1 object from image.
Definition: asn1.c:1015
An executable image.
Definition: image.h:23
size_t len
Length of data.
Definition: asn1.h:24
An X.509 certificate.
Definition: x509.h:215
static void(* free)(struct refcnt *refcnt))
Definition: refcnt.h:54
uint32_t next
Next descriptor address.
Definition: dwmac.h:22
static void x509_put(struct x509_certificate *cert)
Drop reference to X.509 certificate.
Definition: x509.h:277
uint16_t offset
Offset to command line.
Definition: bzimage.h:8
An ASN.1 object cursor.
Definition: asn1.h:20

References asn1_cursor::data, free, image_asn1(), asn1_cursor::len, next, offset, rc, and x509_put().

Referenced by cert_exec().

◆ x509_check_issuer()

int x509_check_issuer ( struct x509_certificate cert,
struct x509_certificate issuer 
)

Check X.509 certificate against issuer certificate.

Parameters
certX.509 certificate
issuerX.509 issuer certificate
Return values
rcReturn status code

Definition at line 1176 of file x509.c.

1177  {
1178  struct x509_public_key *public_key = &issuer->subject.public_key;
1179  int rc;
1180 
1181  /* Check issuer. In theory, this should be a full X.500 DN
1182  * comparison, which would require support for a plethora of
1183  * abominations such as TeletexString (which allows the
1184  * character set to be changed mid-string using escape codes).
1185  * In practice, we assume that anyone who deliberately changes
1186  * the encoding of the issuer DN is probably a masochist who
1187  * will rather enjoy the process of figuring out exactly why
1188  * their certificate doesn't work.
1189  *
1190  * See http://www.cs.auckland.ac.nz/~pgut001/pubs/x509guide.txt
1191  * for some enjoyable ranting on this subject.
1192  */
1193  if ( asn1_compare ( &cert->issuer.raw, &issuer->subject.raw ) != 0 ) {
1194  DBGC ( cert, "X509 %p \"%s\" issuer does not match ",
1195  cert, x509_name ( cert ) );
1196  DBGC ( cert, "X509 %p \"%s\" subject\n",
1197  issuer, x509_name ( issuer ) );
1198  DBGC_HDA ( cert, 0, cert->issuer.raw.data,
1199  cert->issuer.raw.len );
1200  DBGC_HDA ( issuer, 0, issuer->subject.raw.data,
1201  issuer->subject.raw.len );
1202  return -EACCES_WRONG_ISSUER;
1203  }
1204 
1205  /* Check that issuer is allowed to sign certificates */
1206  if ( ! issuer->extensions.basic.ca ) {
1207  DBGC ( issuer, "X509 %p \"%s\" cannot sign ",
1208  issuer, x509_name ( issuer ) );
1209  DBGC ( issuer, "X509 %p \"%s\": not a CA certificate\n",
1210  cert, x509_name ( cert ) );
1211  return -EACCES_NOT_CA;
1212  }
1213  if ( issuer->extensions.usage.present &&
1214  ( ! ( issuer->extensions.usage.bits & X509_KEY_CERT_SIGN ) ) ) {
1215  DBGC ( issuer, "X509 %p \"%s\" cannot sign ",
1216  issuer, x509_name ( issuer ) );
1217  DBGC ( issuer, "X509 %p \"%s\": no keyCertSign usage\n",
1218  cert, x509_name ( cert ) );
1219  return -EACCES_KEY_USAGE;
1220  }
1221 
1222  /* Check signature */
1223  if ( ( rc = x509_check_signature ( cert, public_key ) ) != 0 )
1224  return rc;
1225 
1226  return 0;
1227 }
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
struct asn1_cursor raw
Raw issuer.
Definition: x509.h:31
int asn1_compare(const struct asn1_cursor *cursor1, const struct asn1_cursor *cursor2)
Compare two ASN.1 objects.
Definition: asn1.c:447
struct x509_issuer issuer
Issuer.
Definition: x509.h:240
const void * data
Start of data.
Definition: asn1.h:22
#define DBGC(...)
Definition: compiler.h:505
int present
Key usage extension is present.
Definition: x509.h:94
size_t len
Length of data.
Definition: asn1.h:24
#define EACCES_KEY_USAGE
Definition: x509.c:90
#define DBGC_HDA(...)
Definition: compiler.h:506
An X.509 certificate public key.
Definition: x509.h:49
struct x509_public_key public_key
Public key information.
Definition: x509.h:65
struct x509_subject subject
Subject.
Definition: x509.h:244
int ca
Subject is a CA.
Definition: x509.h:79
#define EACCES_NOT_CA
Definition: x509.c:86
struct asn1_cursor raw
Raw subject.
Definition: x509.h:61
const char * x509_name(struct x509_certificate *cert)
Get X.509 certificate display name.
Definition: x509.c:146
static int x509_check_signature(struct x509_certificate *cert, struct x509_public_key *public_key)
Check X.509 certificate signature.
Definition: x509.c:1123
unsigned int bits
Usage bits.
Definition: x509.h:96
struct x509_key_usage usage
Key usage.
Definition: x509.h:160
#define EACCES_WRONG_ISSUER
Definition: x509.c:82
struct x509_basic_constraints basic
Basic constraints.
Definition: x509.h:158
struct x509_extensions extensions
Extensions.
Definition: x509.h:248

References asn1_compare(), x509_extensions::basic, x509_key_usage::bits, x509_basic_constraints::ca, asn1_cursor::data, DBGC, DBGC_HDA, EACCES_KEY_USAGE, EACCES_NOT_CA, EACCES_WRONG_ISSUER, x509_certificate::extensions, x509_certificate::issuer, asn1_cursor::len, x509_key_usage::present, x509_subject::public_key, x509_subject::raw, x509_issuer::raw, rc, x509_certificate::subject, x509_extensions::usage, x509_check_signature(), X509_KEY_CERT_SIGN, and x509_name().

Referenced by x509_check_issuer_fail_okx(), x509_check_issuer_okx(), and x509_validate().

◆ x509_fingerprint()

void x509_fingerprint ( struct x509_certificate cert,
struct digest_algorithm digest,
void *  fingerprint 
)

Calculate X.509 certificate fingerprint.

Parameters
certX.509 certificate
digestDigest algorithm
fingerprintFingerprint buffer

Definition at line 1236 of file x509.c.

1238  {
1239  uint8_t ctx[ digest->ctxsize ];
1240 
1241  /* Calculate fingerprint */
1242  digest_init ( digest, ctx );
1243  digest_update ( digest, ctx, cert->raw.data, cert->raw.len );
1244  digest_final ( digest, ctx, fingerprint );
1245 }
static void digest_update(struct digest_algorithm *digest, void *ctx, const void *data, size_t len)
Definition: crypto.h:201
static void digest_final(struct digest_algorithm *digest, void *ctx, void *out)
Definition: crypto.h:207
const void * data
Start of data.
Definition: asn1.h:22
struct golan_eq_context ctx
Definition: CIB_PRM.h:28
size_t len
Length of data.
Definition: asn1.h:24
static void digest_init(struct digest_algorithm *digest, void *ctx)
Definition: crypto.h:196
unsigned char uint8_t
Definition: stdint.h:10
size_t ctxsize
Context size.
Definition: crypto.h:22
struct asn1_cursor raw
Raw certificate.
Definition: x509.h:230

References ctx, digest_algorithm::ctxsize, asn1_cursor::data, digest_final(), digest_init(), digest_update(), asn1_cursor::len, and x509_certificate::raw.

Referenced by certstat(), icert_certs(), x509_check_root(), x509_fingerprint_okx(), and x509_name().

◆ x509_check_root()

int x509_check_root ( struct x509_certificate cert,
struct x509_root root 
)

Check X.509 root certificate.

Parameters
certX.509 certificate
rootX.509 root certificate list
Return values
rcReturn status code

Definition at line 1254 of file x509.c.

1254  {
1255  struct digest_algorithm *digest = root->digest;
1256  uint8_t fingerprint[ digest->digestsize ];
1257  const uint8_t *root_fingerprint = root->fingerprints;
1258  unsigned int i;
1259 
1260  /* Calculate certificate fingerprint */
1261  x509_fingerprint ( cert, digest, fingerprint );
1262 
1263  /* Check fingerprint against all root certificates */
1264  for ( i = 0 ; i < root->count ; i++ ) {
1265  if ( memcmp ( fingerprint, root_fingerprint,
1266  sizeof ( fingerprint ) ) == 0 ) {
1267  DBGC ( cert, "X509 %p \"%s\" is a root certificate\n",
1268  cert, x509_name ( cert ) );
1269  return 0;
1270  }
1271  root_fingerprint += sizeof ( fingerprint );
1272  }
1273 
1274  DBGC2 ( cert, "X509 %p \"%s\" is not a root certificate\n",
1275  cert, x509_name ( cert ) );
1276  return -ENOENT;
1277 }
struct stp_switch root
Root switch.
Definition: stp.h:26
#define DBGC(...)
Definition: compiler.h:505
#define ENOENT
No such file or directory.
Definition: errno.h:514
unsigned char uint8_t
Definition: stdint.h:10
void x509_fingerprint(struct x509_certificate *cert, struct digest_algorithm *digest, void *fingerprint)
Calculate X.509 certificate fingerprint.
Definition: x509.c:1236
const char * x509_name(struct x509_certificate *cert)
Get X.509 certificate display name.
Definition: x509.c:146
#define DBGC2(...)
Definition: compiler.h:522
size_t digestsize
Digest size.
Definition: crypto.h:26
A message digest algorithm.
Definition: crypto.h:18
int memcmp(const void *first, const void *second, size_t len)
Compare memory regions.
Definition: string.c:114

References DBGC, DBGC2, digest_algorithm::digestsize, ENOENT, memcmp(), root, x509_fingerprint(), and x509_name().

Referenced by x509_check_root_fail_okx(), x509_check_root_okx(), and x509_validate().

◆ x509_check_time()

int x509_check_time ( struct x509_certificate cert,
time_t  time 
)

Check X.509 certificate validity period.

Parameters
certX.509 certificate
timeTime at which to check certificate
Return values
rcReturn status code

Definition at line 1286 of file x509.c.

1286  {
1287  struct x509_validity *validity = &cert->validity;
1288 
1289  /* Check validity period */
1290  if ( validity->not_before.time > ( time + TIMESTAMP_ERROR_MARGIN ) ) {
1291  DBGC ( cert, "X509 %p \"%s\" is not yet valid (at time %lld)\n",
1292  cert, x509_name ( cert ), time );
1293  return -EACCES_EXPIRED;
1294  }
1295  if ( validity->not_after.time < ( time - TIMESTAMP_ERROR_MARGIN ) ) {
1296  DBGC ( cert, "X509 %p \"%s\" has expired (at time %lld)\n",
1297  cert, x509_name ( cert ), time );
1298  return -EACCES_EXPIRED;
1299  }
1300 
1301  DBGC2 ( cert, "X509 %p \"%s\" is valid (at time %lld)\n",
1302  cert, x509_name ( cert ), time );
1303  return 0;
1304 }
#define DBGC(...)
Definition: compiler.h:505
time_t time
Seconds since the Epoch.
Definition: x509.h:37
struct x509_time not_before
Not valid before.
Definition: x509.h:43
#define EACCES_EXPIRED
Definition: x509.c:94
An X.509 certificate validity period.
Definition: x509.h:41
struct x509_validity validity
Validity.
Definition: x509.h:242
const char * x509_name(struct x509_certificate *cert)
Get X.509 certificate display name.
Definition: x509.c:146
#define TIMESTAMP_ERROR_MARGIN
Margin of error (in seconds) allowed in signed timestamps.
Definition: crypto.h:75
#define DBGC2(...)
Definition: compiler.h:522
struct x509_time not_after
Not valid after.
Definition: x509.h:45

References DBGC, DBGC2, EACCES_EXPIRED, x509_validity::not_after, x509_validity::not_before, x509_time::time, TIMESTAMP_ERROR_MARGIN, x509_certificate::validity, and x509_name().

Referenced by x509_check_time_fail_okx(), x509_check_time_okx(), and x509_validate().

◆ x509_invalidate()

static void x509_invalidate ( struct x509_certificate cert)
inlinestatic

Invalidate X.509 certificate.

Parameters
certX.509 certificate

Definition at line 472 of file x509.h.

472  {
473  x509_root_put ( cert->root );
474  cert->root = NULL;
475  cert->path_remaining = 0;
476 }
unsigned int path_remaining
Maximum number of subsequent certificates in chain.
Definition: x509.h:227
static void x509_root_put(struct x509_root *root)
Drop reference to X.509 root certificate list.
Definition: x509.h:403
struct x509_root * root
Root against which certificate has been validated (if any)
Definition: x509.h:225
#define NULL
NULL pointer (VOID *)
Definition: Base.h:321

References NULL, x509_certificate::path_remaining, x509_certificate::root, and x509_root_put().

Referenced by ocsp_prepare_test(), ocsp_validate(), and x509_invalidate_chain().

◆ x509_invalidate_chain()

static void x509_invalidate_chain ( struct x509_chain chain)
inlinestatic

Invalidate X.509 certificate chain.

Parameters
chainX.509 certificate chain

Definition at line 483 of file x509.h.

483  {
484  struct x509_link *link;
485 
486  list_for_each_entry ( link, &chain->links, list )
487  x509_invalidate ( link->cert );
488 }
struct list_head links
List of links.
Definition: x509.h:204
#define list_for_each_entry(pos, head, member)
Iterate over entries in a list.
Definition: list.h:431
u32 link
Link to next descriptor.
Definition: ar9003_mac.h:68
static void x509_invalidate(struct x509_certificate *cert)
Invalidate X.509 certificate.
Definition: x509.h:472

References link, x509_chain::links, x509_link::list, list_for_each_entry, and x509_invalidate().

Referenced by cms_verify_fail_okx(), cms_verify_okx(), x509_validate_chain_fail_okx(), and x509_validate_chain_okx().