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 store. 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_flags { X509_FL_VALIDATED = 0x0001, X509_FL_PERMANENT = 0x0002, X509_FL_EXPLICIT = 0x0004 }
 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...
 
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_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...
 
int x509_auto_append (struct x509_chain *chain, struct x509_chain *certs)
 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 int x509_is_valid (struct x509_certificate *cert)
 Check if X.509 certificate is valid. 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 88 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 99 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 122 of file x509.h.

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

◆ 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 148 of file x509.h.

148  {
152 };
#define ASN1_IMPLICIT_TAG(number)
ASN.1 implicit tag.
Definition: asn1.h:91

◆ x509_flags

enum x509_flags

X.509 certificate flags.

Enumerator
X509_FL_VALIDATED 

Certificate has been validated.

X509_FL_PERMANENT 

Certificate was added at build time.

X509_FL_EXPLICIT 

Certificate was added explicitly at run time.

Definition at line 220 of file x509.h.

220  {
221  /** Certificate has been validated */
222  X509_FL_VALIDATED = 0x0001,
223  /** Certificate was added at build time */
224  X509_FL_PERMANENT = 0x0002,
225  /** Certificate was added explicitly at run time */
226  X509_FL_EXPLICIT = 0x0004,
227 };
Certificate has been validated.
Definition: x509.h:222
Certificate was added at build time.
Definition: x509.h:224
Certificate was added explicitly at run time.
Definition: x509.h:226

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 236 of file x509.h.

236  {
237  ref_get ( &cert->refcnt );
238  return cert;
239 }
struct refcnt refcnt
Reference count.
Definition: x509.h:187
#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(), tls_new_certificate_request(), 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 247 of file x509.h.

247  {
248  ref_put ( &cert->refcnt );
249 }
struct refcnt refcnt
Reference count.
Definition: x509.h:187
#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(), free_tls(), image_x509(), ocsp_free(), ocsp_parse_certs(), ocsp_response(), ocsp_test_exec(), tls_new_certificate_request(), x509_append_raw(), x509_cached_okx(), x509_certificate(), x509_free_chain(), and x509_test_exec().

◆ 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 258 of file x509.h.

258  {
259  ref_get ( &chain->refcnt );
260  return chain;
261 }
#define ref_get(refcnt)
Get additional reference to object.
Definition: refcnt.h:92
struct refcnt refcnt
Reference count.
Definition: x509.h:179

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 269 of file x509.h.

269  {
270  ref_put ( &chain->refcnt );
271 }
struct refcnt refcnt
Reference count.
Definition: x509.h:179
#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_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 280 of file x509.h.

280  {
281  struct x509_link *link;
282 
283  link = list_first_entry ( &chain->links, struct x509_link, list );
284  return ( link ? link->cert : NULL );
285 }
struct list_head links
List of links.
Definition: x509.h:181
#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:362

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

Referenced by cms_verify(), cms_verify_signer_info(), 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 294 of file x509.h.

294  {
295  struct x509_link *link;
296 
297  link = list_last_entry ( &chain->links, struct x509_link, list );
298  return ( link ? link->cert : NULL );
299 }
struct list_head links
List of links.
Definition: x509.h:181
#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:362

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

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

◆ 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 131 of file x509.c.

131  {
132  struct asn1_cursor *common_name = &cert->subject.common_name;
134  static char buf[64];
135  uint8_t fingerprint[ digest->digestsize ];
136  size_t len;
137 
138  len = common_name->len;
139  if ( len ) {
140  /* Certificate has a commonName: use that */
141  if ( len > ( sizeof ( buf ) - 1 /* NUL */ ) )
142  len = ( sizeof ( buf ) - 1 /* NUL */ );
143  memcpy ( buf, common_name->data, len );
144  buf[len] = '\0';
145  } else {
146  /* Certificate has no commonName: use SHA-1 fingerprint */
147  x509_fingerprint ( cert, digest, fingerprint );
148  base16_encode ( fingerprint, sizeof ( fingerprint ),
149  buf, sizeof ( buf ) );
150  }
151  return buf;
152 }
const void * data
Start of data.
Definition: asn1.h:21
struct md4_digest digest
Digest of data already processed.
Definition: md4.h:12
size_t len
Length of data.
Definition: asn1.h:23
void * memcpy(void *dest, const void *src, size_t len) __nonnull
struct x509_subject subject
Subject.
Definition: x509.h:212
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:1228
struct asn1_cursor common_name
Common name.
Definition: x509.h:62
uint32_t len
Length.
Definition: ena.h:14
A message digest algorithm.
Definition: crypto.h:16
An ASN.1 object cursor.
Definition: asn1.h:19
struct digest_algorithm sha1_algorithm
SHA-1 algorithm.
Definition: sha1.c:257

References x509_subject::common_name, asn1_cursor::data, digest, len, asn1_cursor::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(), 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(), validator_append(), validator_name(), validator_progress(), validator_start_download(), validator_start_ocsp(), 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 989 of file x509.c.

990  {
991  struct x509_signature *signature = &cert->signature;
992  struct asn1_algorithm **signature_algorithm = &signature->algorithm;
993  struct asn1_bit_string *signature_value = &signature->value;
994  struct asn1_cursor cursor;
995  int rc;
996 
997  /* Record raw certificate */
998  memcpy ( &cursor, raw, sizeof ( cursor ) );
999  memcpy ( &cert->raw, &cursor, sizeof ( cert->raw ) );
1000 
1001  /* Enter certificate */
1002  asn1_enter ( &cursor, ASN1_SEQUENCE );
1003 
1004  /* Parse tbsCertificate */
1005  if ( ( rc = x509_parse_tbscertificate ( cert, &cursor ) ) != 0 )
1006  return rc;
1007  asn1_skip_any ( &cursor );
1008 
1009  /* Parse signatureAlgorithm */
1010  if ( ( rc = asn1_signature_algorithm ( &cursor,
1011  signature_algorithm ) ) != 0 ) {
1012  DBGC ( cert, "X509 %p could not parse signature algorithm: "
1013  "%s\n", cert, strerror ( rc ) );
1014  return rc;
1015  }
1016  DBGC2 ( cert, "X509 %p signatureAlgorithm is %s\n",
1017  cert, (*signature_algorithm)->name );
1018  asn1_skip_any ( &cursor );
1019 
1020  /* Parse signatureValue */
1021  if ( ( rc = asn1_integral_bit_string ( &cursor,
1022  signature_value ) ) != 0 ) {
1023  DBGC ( cert, "X509 %p could not parse signature value: %s\n",
1024  cert, strerror ( rc ) );
1025  return rc;
1026  }
1027  DBGC2 ( cert, "X509 %p signatureValue is:\n", cert );
1028  DBGC2_HDA ( cert, 0, signature_value->data, signature_value->len );
1029 
1030  /* Check that algorithm in tbsCertificate matches algorithm in
1031  * signature
1032  */
1033  if ( signature->algorithm != (*signature_algorithm) ) {
1034  DBGC ( cert, "X509 %p signature algorithm %s does not match "
1035  "signatureAlgorithm %s\n",
1036  cert, signature->algorithm->name,
1037  (*signature_algorithm)->name );
1038  return -EINVAL_ALGORITHM_MISMATCH;
1039  }
1040 
1041  return 0;
1042 }
const void * data
Data.
Definition: asn1.h:341
An ASN.1 OID-identified algorithm.
Definition: asn1.h:298
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:160
#define DBGC(...)
Definition: compiler.h:505
int asn1_skip_any(struct asn1_cursor *cursor)
Skip ASN.1 object of any type.
Definition: asn1.c:276
int asn1_signature_algorithm(const struct asn1_cursor *cursor, struct asn1_algorithm **algorithm)
Parse ASN.1 OID-identified signature algorithm.
Definition: asn1.c:565
u8 signature
Definition: CIB_PRM.h:35
void * memcpy(void *dest, const void *src, size_t len) __nonnull
struct x509_signature signature
Signature.
Definition: x509.h:214
#define DBGC2_HDA(...)
Definition: compiler.h:523
int asn1_integral_bit_string(const struct asn1_cursor *cursor, struct asn1_bit_string *bits)
Parse ASN.1 bit string that must be an integral number of bytes.
Definition: asn1.c:414
static int x509_parse_tbscertificate(struct x509_certificate *cert, const struct asn1_cursor *raw)
Parse X.509 certificate tbsCertificate.
Definition: x509.c:919
char * strerror(int errno)
Retrieve string representation of error number.
Definition: strerror.c:78
size_t len
Length.
Definition: asn1.h:343
#define ASN1_SEQUENCE
ASN.1 sequence.
Definition: asn1.h:85
#define DBGC2(...)
Definition: compiler.h:522
#define EINVAL_ALGORITHM_MISMATCH
Definition: x509.c:68
__be32 raw[7]
Definition: CIB_PRM.h:28
struct asn1_cursor raw
Raw certificate.
Definition: x509.h:198
An ASN.1 object cursor.
Definition: asn1.h:19
An ASN.1 bit string.
Definition: asn1.h:339
An X.509 certificate signature.
Definition: x509.h:68

References asn1_enter(), asn1_integral_bit_string(), ASN1_SEQUENCE, asn1_signature_algorithm(), asn1_skip_any(), asn1_bit_string::data, DBGC, DBGC2, DBGC2_HDA, EINVAL_ALGORITHM_MISMATCH, asn1_bit_string::len, memcpy(), 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 1055 of file x509.c.

1056  {
1057  struct asn1_cursor cursor;
1058  void *raw;
1059  int rc;
1060 
1061  /* Initialise cursor */
1062  cursor.data = data;
1063  cursor.len = len;
1064  asn1_shrink_any ( &cursor );
1065 
1066  /* Return stored certificate, if present */
1067  if ( ( *cert = certstore_find ( &cursor ) ) != NULL ) {
1068 
1069  /* Add caller's reference */
1070  x509_get ( *cert );
1071  return 0;
1072  }
1073 
1074  /* Allocate and initialise certificate */
1075  *cert = zalloc ( sizeof ( **cert ) + cursor.len );
1076  if ( ! *cert )
1077  return -ENOMEM;
1078  ref_init ( &(*cert)->refcnt, NULL );
1079  raw = ( *cert + 1 );
1080 
1081  /* Copy raw data */
1082  memcpy ( raw, cursor.data, cursor.len );
1083  cursor.data = raw;
1084 
1085  /* Parse certificate */
1086  if ( ( rc = x509_parse ( *cert, &cursor ) ) != 0 ) {
1087  x509_put ( *cert );
1088  *cert = NULL;
1089  return rc;
1090  }
1091 
1092  /* Add certificate to store */
1093  certstore_add ( *cert );
1094 
1095  return 0;
1096 }
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:236
#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:138
#define ENOMEM
Not enough space.
Definition: errno.h:534
void * memcpy(void *dest, const void *src, size_t len) __nonnull
void * zalloc(size_t size)
Allocate cleared memory.
Definition: malloc.c:624
int asn1_shrink_any(struct asn1_cursor *cursor)
Shrink ASN.1 object of any type.
Definition: asn1.c:286
struct x509_certificate * certstore_find(struct asn1_cursor *raw)
Find certificate in store.
Definition: certstore.c:102
uint32_t len
Length.
Definition: ena.h:14
static void x509_put(struct x509_certificate *cert)
Drop reference to X.509 certificate.
Definition: x509.h:247
__be32 raw[7]
Definition: CIB_PRM.h:28
struct arbelprm_port_state_change_st data
Message.
Definition: arbel.h:12
struct arbelprm_wqe_segment_data_ptr data[ARBEL_MAX_GATHER]
Definition: arbel.h:237
#define NULL
NULL pointer (VOID *)
Definition: Base.h:362
An ASN.1 object cursor.
Definition: asn1.h:19
int x509_parse(struct x509_certificate *cert, const struct asn1_cursor *raw)
Parse X.509 certificate from ASN.1 data.
Definition: x509.c:989

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

◆ 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 1313 of file x509.c.

1315  {
1316  unsigned int max_path_remaining;
1317  int rc;
1318 
1319  /* Use default root certificate store if none specified */
1320  if ( ! root )
1322 
1323  /* Return success if certificate has already been validated */
1324  if ( x509_is_valid ( cert ) )
1325  return 0;
1326 
1327  /* Fail if certificate is invalid at specified time */
1328  if ( ( rc = x509_check_time ( cert, time ) ) != 0 )
1329  return rc;
1330 
1331  /* Succeed if certificate is a trusted root certificate */
1332  if ( x509_check_root ( cert, root ) == 0 ) {
1333  cert->flags |= X509_FL_VALIDATED;
1334  cert->path_remaining = ( cert->extensions.basic.path_len + 1 );
1335  return 0;
1336  }
1337 
1338  /* Fail unless we have an issuer */
1339  if ( ! issuer ) {
1340  DBGC2 ( cert, "X509 %p \"%s\" has no issuer\n",
1341  cert, x509_name ( cert ) );
1342  return -EACCES_UNTRUSTED;
1343  }
1344 
1345  /* Fail unless issuer has already been validated */
1346  if ( ! x509_is_valid ( issuer ) ) {
1347  DBGC ( cert, "X509 %p \"%s\" ", cert, x509_name ( cert ) );
1348  DBGC ( cert, "issuer %p \"%s\" has not yet been validated\n",
1349  issuer, x509_name ( issuer ) );
1350  return -EACCES_OUT_OF_ORDER;
1351  }
1352 
1353  /* Fail if issuing certificate cannot validate this certificate */
1354  if ( ( rc = x509_check_issuer ( cert, issuer ) ) != 0 )
1355  return rc;
1356 
1357  /* Fail if path length constraint is violated */
1358  if ( issuer->path_remaining == 0 ) {
1359  DBGC ( cert, "X509 %p \"%s\" ", cert, x509_name ( cert ) );
1360  DBGC ( cert, "issuer %p \"%s\" path length exceeded\n",
1361  issuer, x509_name ( issuer ) );
1362  return -EACCES_PATH_LEN;
1363  }
1364 
1365  /* Fail if OCSP is required */
1366  if ( ocsp_required ( cert ) ) {
1367  DBGC ( cert, "X509 %p \"%s\" requires an OCSP check\n",
1368  cert, x509_name ( cert ) );
1369  return -EACCES_OCSP_REQUIRED;
1370  }
1371 
1372  /* Calculate effective path length */
1373  cert->path_remaining = ( issuer->path_remaining - 1 );
1374  max_path_remaining = ( cert->extensions.basic.path_len + 1 );
1375  if ( cert->path_remaining > max_path_remaining )
1376  cert->path_remaining = max_path_remaining;
1377 
1378  /* Mark certificate as valid */
1379  cert->flags |= X509_FL_VALIDATED;
1380 
1381  DBGC ( cert, "X509 %p \"%s\" successfully validated using ",
1382  cert, x509_name ( cert ) );
1383  DBGC ( cert, "issuer %p \"%s\"\n", issuer, x509_name ( issuer ) );
1384  return 0;
1385 }
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
unsigned int path_remaining
Maximum number of subsequent certificates in chain.
Definition: x509.h:195
unsigned int path_len
Path length.
Definition: x509.h:80
struct stp_switch root
Root switch.
Definition: stp.h:26
struct x509_root root_certificates
Root certificates.
Definition: rootcert.c:73
#define DBGC(...)
Definition: compiler.h:505
static int ocsp_required(struct x509_certificate *cert)
Check if X.509 certificate requires an OCSP check.
Definition: ocsp.h:128
unsigned int flags
Flags.
Definition: x509.h:193
#define EACCES_OCSP_REQUIRED
Definition: x509.c:112
#define EACCES_OUT_OF_ORDER
Definition: x509.c:104
#define EACCES_PATH_LEN
Definition: x509.c:96
int x509_check_root(struct x509_certificate *cert, struct x509_root *root)
Check X.509 root certificate.
Definition: x509.c:1246
static int x509_is_valid(struct x509_certificate *cert)
Check if X.509 certificate is valid.
Definition: x509.h:391
Certificate has been validated.
Definition: x509.h:222
const char * x509_name(struct x509_certificate *cert)
Get X.509 certificate display name.
Definition: x509.c:131
#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:1168
#define EACCES_UNTRUSTED
Definition: x509.c:100
uint64_t time
Current time.
Definition: ntlm.h:20
int x509_check_time(struct x509_certificate *cert, time_t time)
Check X.509 certificate validity period.
Definition: x509.c:1278
struct x509_basic_constraints basic
Basic constraints.
Definition: x509.h:157
struct x509_extensions extensions
Extensions.
Definition: x509.h:216

References x509_extensions::basic, DBGC, DBGC2, EACCES_OCSP_REQUIRED, EACCES_OUT_OF_ORDER, EACCES_PATH_LEN, EACCES_UNTRUSTED, x509_certificate::extensions, x509_certificate::flags, ocsp_required(), x509_basic_constraints::path_len, x509_certificate::path_remaining, rc, root, root_certificates, time, x509_check_issuer(), x509_check_root(), x509_check_time(), X509_FL_VALIDATED, x509_is_valid(), and x509_name().

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 1519 of file x509.c.

1519  {
1520  struct asn1_cursor *common_name = &cert->subject.common_name;
1521  struct asn1_cursor alt_name;
1522  int rc;
1523 
1524  /* Check commonName */
1525  if ( x509_check_dnsname ( cert, common_name, name ) == 0 ) {
1526  DBGC2 ( cert, "X509 %p \"%s\" commonName matches \"%s\"\n",
1527  cert, x509_name ( cert ), name );
1528  return 0;
1529  }
1530 
1531  /* Check any subjectAlternativeNames */
1532  memcpy ( &alt_name, &cert->extensions.alt_name.names,
1533  sizeof ( alt_name ) );
1534  for ( ; alt_name.len ; asn1_skip_any ( &alt_name ) ) {
1535  if ( ( rc = x509_check_alt_name ( cert, &alt_name,
1536  name ) ) == 0 ) {
1537  DBGC2 ( cert, "X509 %p \"%s\" subjectAltName matches "
1538  "\"%s\"\n", cert, x509_name ( cert ), name );
1539  return 0;
1540  }
1541  }
1542 
1543  DBGC ( cert, "X509 %p \"%s\" does not match name \"%s\"\n",
1544  cert, x509_name ( cert ), name );
1545  return -EACCES_WRONG_NAME;
1546 }
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:144
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:1487
#define DBGC(...)
Definition: compiler.h:505
int asn1_skip_any(struct asn1_cursor *cursor)
Skip ASN.1 object of any type.
Definition: asn1.c:276
void * memcpy(void *dest, const void *src, size_t len) __nonnull
#define EACCES_WRONG_NAME
Definition: x509.c:116
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:1395
struct x509_subject subject
Subject.
Definition: x509.h:212
struct asn1_cursor common_name
Common name.
Definition: x509.h:62
const char * x509_name(struct x509_certificate *cert)
Get X.509 certificate display name.
Definition: x509.c:131
struct x509_subject_alt_name alt_name
Subject alternative name.
Definition: x509.h:165
#define DBGC2(...)
Definition: compiler.h:522
An ASN.1 object cursor.
Definition: asn1.h:19
struct x509_extensions extensions
Extensions.
Definition: x509.h:216

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 1577 of file x509.c.

1577  {
1578  struct x509_chain *chain;
1579 
1580  /* Allocate chain */
1581  chain = zalloc ( sizeof ( *chain ) );
1582  if ( ! chain )
1583  return NULL;
1584 
1585  /* Initialise chain */
1586  ref_init ( &chain->refcnt, x509_free_chain );
1587  INIT_LIST_HEAD ( &chain->links );
1588 
1589  DBGC2 ( chain, "X509 chain %p allocated\n", chain );
1590  return chain;
1591 }
struct list_head links
List of links.
Definition: x509.h:181
#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:1553
An X.509 certificate chain.
Definition: x509.h:177
void * zalloc(size_t size)
Allocate cleared memory.
Definition: malloc.c:624
#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:362
struct refcnt refcnt
Reference count.
Definition: x509.h:179

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

Referenced by cms_parse(), cms_signature(), 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 1600 of file x509.c.

1600  {
1601  struct x509_link *link;
1602 
1603  /* Allocate link */
1604  link = zalloc ( sizeof ( *link ) );
1605  if ( ! link )
1606  return -ENOMEM;
1607 
1608  /* Add link to chain */
1609  link->cert = x509_get ( cert );
1610  list_add_tail ( &link->list, &chain->links );
1611  DBGC ( chain, "X509 chain %p added X509 %p \"%s\"\n",
1612  chain, cert, x509_name ( cert ) );
1613 
1614  return 0;
1615 }
static struct x509_certificate * x509_get(struct x509_certificate *cert)
Get reference to X.509 certificate.
Definition: x509.h:236
struct list_head links
List of links.
Definition: x509.h:181
#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:624
const char * x509_name(struct x509_certificate *cert)
Get X.509 certificate display name.
Definition: x509.c:131

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

Referenced by cms_parse_signer_identifier(), 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 1625 of file x509.c.

1626  {
1627  struct x509_certificate *cert;
1628  int rc;
1629 
1630  /* Parse certificate */
1631  if ( ( rc = x509_certificate ( data, len, &cert ) ) != 0 )
1632  goto err_parse;
1633 
1634  /* Append certificate to chain */
1635  if ( ( rc = x509_append ( chain, cert ) ) != 0 )
1636  goto err_append;
1637 
1638  /* Drop reference to certificate */
1639  x509_put ( cert );
1640 
1641  return 0;
1642 
1643  err_append:
1644  x509_put ( cert );
1645  err_parse:
1646  return rc;
1647 }
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:1600
An X.509 certificate.
Definition: x509.h:185
uint32_t len
Length.
Definition: ena.h:14
static void x509_put(struct x509_certificate *cert)
Drop reference to X.509 certificate.
Definition: x509.h:247
struct arbelprm_port_state_change_st data
Message.
Definition: arbel.h:12

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

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

◆ x509_auto_append()

int x509_auto_append ( struct x509_chain chain,
struct x509_chain certs 
)

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

Parameters
chainX.509 certificate chain
certsX.509 certificate list
Return values
rcReturn status code

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

Definition at line 1684 of file x509.c.

1684  {
1685  struct x509_certificate *cert;
1686  struct x509_certificate *previous;
1687  int rc;
1688 
1689  /* Get current certificate */
1690  cert = x509_last ( chain );
1691  if ( ! cert ) {
1692  DBGC ( chain, "X509 chain %p has no certificates\n", chain );
1693  return -EACCES_EMPTY;
1694  }
1695 
1696  /* Append certificates, in order */
1697  while ( 1 ) {
1698 
1699  /* Find issuing certificate */
1700  previous = cert;
1701  cert = x509_find_subject ( certs, &cert->issuer.raw );
1702  if ( ! cert )
1703  break;
1704  if ( cert == previous )
1705  break;
1706 
1707  /* Append certificate to chain */
1708  if ( ( rc = x509_append ( chain, cert ) ) != 0 )
1709  return rc;
1710  }
1711 
1712  return 0;
1713 }
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
struct asn1_cursor raw
Raw issuer.
Definition: x509.h:30
#define EACCES_EMPTY
Definition: x509.c:108
struct x509_issuer issuer
Issuer.
Definition: x509.h:208
#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:1600
static struct x509_certificate * x509_last(struct x509_chain *chain)
Get last certificate in X.509 certificate chain.
Definition: x509.h:294
An X.509 certificate.
Definition: x509.h:185
static struct x509_certificate * x509_find_subject(struct x509_chain *certs, const struct asn1_cursor *subject)
Identify X.509 certificate by subject.
Definition: x509.c:1657

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

Referenced by cms_parse_signer_identifier(), 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 1724 of file x509.c.

1725  {
1726  struct x509_certificate *issuer = NULL;
1727  struct x509_link *link;
1728  int rc;
1729 
1730  /* Use default certificate store if none specified */
1731  if ( ! store )
1732  store = &certstore;
1733 
1734  /* Append any applicable certificates from the certificate store */
1735  if ( ( rc = x509_auto_append ( chain, store ) ) != 0 )
1736  return rc;
1737 
1738  /* Find first certificate that can be validated as a
1739  * standalone (i.e. is already valid, or can be validated as
1740  * a trusted root certificate).
1741  */
1742  list_for_each_entry ( link, &chain->links, list ) {
1743 
1744  /* Try validating this certificate as a standalone */
1745  if ( ( rc = x509_validate ( link->cert, NULL, time,
1746  root ) ) != 0 )
1747  continue;
1748 
1749  /* Work back up to start of chain, performing pairwise
1750  * validation.
1751  */
1752  issuer = link->cert;
1754  list ) {
1755 
1756  /* Validate this certificate against its issuer */
1757  if ( ( rc = x509_validate ( link->cert, issuer, time,
1758  root ) ) != 0 )
1759  return rc;
1760  issuer = link->cert;
1761  }
1762 
1763  return 0;
1764  }
1765 
1766  DBGC ( chain, "X509 chain %p found no usable certificates\n", chain );
1767  return -EACCES_USELESS;
1768 }
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
struct x509_chain certstore
Certificate store.
Definition: certstore.c:73
struct stp_switch root
Root switch.
Definition: stp.h:26
struct list_head links
List of links.
Definition: x509.h:181
struct x509_issuer issuer
Issuer.
Definition: x509.h:208
#define DBGC(...)
Definition: compiler.h:505
#define list_for_each_entry(pos, head, member)
Iterate over entries in a list.
Definition: list.h:420
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:1313
u32 link
Link to next descriptor.
Definition: ar9003_mac.h:68
An X.509 certificate.
Definition: x509.h:185
#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:475
int x509_auto_append(struct x509_chain *chain, struct x509_chain *certs)
Append X.509 certificates to X.509 certificate chain.
Definition: x509.c:1684
#define EACCES_USELESS
Definition: x509.c:120
uint64_t time
Current time.
Definition: ntlm.h:20
#define NULL
NULL pointer (VOID *)
Definition: Base.h:362

References certstore, 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, time, x509_auto_append(), and x509_validate().

Referenced by cms_verify_signer_info(), 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 1781 of file x509.c.

1782  {
1783  struct asn1_cursor *cursor;
1784  int next;
1785  int rc;
1786 
1787  /* Get ASN.1 object */
1788  next = image_asn1 ( image, offset, &cursor );
1789  if ( next < 0 ) {
1790  rc = next;
1791  goto err_asn1;
1792  }
1793 
1794  /* Parse certificate */
1795  if ( ( rc = x509_certificate ( cursor->data, cursor->len,
1796  cert ) ) != 0 )
1797  goto err_certificate;
1798 
1799  /* Free ASN.1 object */
1800  free ( cursor );
1801 
1802  return next;
1803 
1804  x509_put ( *cert );
1805  err_certificate:
1806  free ( cursor );
1807  err_asn1:
1808  return rc;
1809 }
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
uint32_t next
Next descriptor address.
Definition: myson.h:18
const void * data
Start of data.
Definition: asn1.h:21
int image_asn1(struct image *image, size_t offset, struct asn1_cursor **cursor)
Extract ASN.1 object from image.
Definition: asn1.c:854
An executable image.
Definition: image.h:24
size_t len
Length of data.
Definition: asn1.h:23
static userptr_t size_t offset
Offset of the first segment within the content.
Definition: deflate.h:259
An X.509 certificate.
Definition: x509.h:185
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:247
An ASN.1 object cursor.
Definition: asn1.h:19

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 1168 of file x509.c.

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

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 1228 of file x509.c.

1230  {
1231  uint8_t ctx[ digest->ctxsize ];
1232 
1233  /* Calculate fingerprint */
1234  digest_init ( digest, ctx );
1235  digest_update ( digest, ctx, cert->raw.data, cert->raw.len );
1236  digest_final ( digest, ctx, fingerprint );
1237 }
static void digest_update(struct digest_algorithm *digest, void *ctx, const void *data, size_t len)
Definition: crypto.h:177
static void digest_final(struct digest_algorithm *digest, void *ctx, void *out)
Definition: crypto.h:182
const void * data
Start of data.
Definition: asn1.h:21
struct golan_eq_context ctx
Definition: CIB_PRM.h:28
struct md4_digest digest
Digest of data already processed.
Definition: md4.h:12
size_t len
Length of data.
Definition: asn1.h:23
static void digest_init(struct digest_algorithm *digest, void *ctx)
Definition: crypto.h:172
unsigned char uint8_t
Definition: stdint.h:10
struct asn1_cursor raw
Raw certificate.
Definition: x509.h:198

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

Referenced by certstat(), 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 1246 of file x509.c.

1246  {
1247  struct digest_algorithm *digest = root->digest;
1248  uint8_t fingerprint[ digest->digestsize ];
1249  const uint8_t *root_fingerprint = root->fingerprints;
1250  unsigned int i;
1251 
1252  /* Calculate certificate fingerprint */
1253  x509_fingerprint ( cert, digest, fingerprint );
1254 
1255  /* Check fingerprint against all root certificates */
1256  for ( i = 0 ; i < root->count ; i++ ) {
1257  if ( memcmp ( fingerprint, root_fingerprint,
1258  sizeof ( fingerprint ) ) == 0 ) {
1259  DBGC ( cert, "X509 %p \"%s\" is a root certificate\n",
1260  cert, x509_name ( cert ) );
1261  return 0;
1262  }
1263  root_fingerprint += sizeof ( fingerprint );
1264  }
1265 
1266  DBGC2 ( cert, "X509 %p \"%s\" is not a root certificate\n",
1267  cert, x509_name ( cert ) );
1268  return -ENOENT;
1269 }
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
struct md4_digest digest
Digest of data already processed.
Definition: md4.h:12
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:1228
const char * x509_name(struct x509_certificate *cert)
Get X.509 certificate display name.
Definition: x509.c:131
#define DBGC2(...)
Definition: compiler.h:522
A message digest algorithm.
Definition: crypto.h:16
int memcmp(const void *first, const void *second, size_t len)
Compare memory regions.
Definition: string.c:113

References DBGC, DBGC2, digest, 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 1278 of file x509.c.

1278  {
1279  struct x509_validity *validity = &cert->validity;
1280 
1281  /* Check validity period */
1282  if ( validity->not_before.time > ( time + TIMESTAMP_ERROR_MARGIN ) ) {
1283  DBGC ( cert, "X509 %p \"%s\" is not yet valid (at time %lld)\n",
1284  cert, x509_name ( cert ), time );
1285  return -EACCES_EXPIRED;
1286  }
1287  if ( validity->not_after.time < ( time - TIMESTAMP_ERROR_MARGIN ) ) {
1288  DBGC ( cert, "X509 %p \"%s\" has expired (at time %lld)\n",
1289  cert, x509_name ( cert ), time );
1290  return -EACCES_EXPIRED;
1291  }
1292 
1293  DBGC2 ( cert, "X509 %p \"%s\" is valid (at time %lld)\n",
1294  cert, x509_name ( cert ), time );
1295  return 0;
1296 }
#define DBGC(...)
Definition: compiler.h:505
time_t time
Seconds since the Epoch.
Definition: x509.h:36
struct x509_time not_before
Not valid before.
Definition: x509.h:42
#define EACCES_EXPIRED
Definition: x509.c:92
An X.509 certificate validity period.
Definition: x509.h:40
struct x509_validity validity
Validity.
Definition: x509.h:210
const char * x509_name(struct x509_certificate *cert)
Get X.509 certificate display name.
Definition: x509.c:131
#define TIMESTAMP_ERROR_MARGIN
SHA-512/224 digest algorithm.
Definition: crypto.h:54
#define DBGC2(...)
Definition: compiler.h:522
struct x509_time not_after
Not valid after.
Definition: x509.h:44
uint64_t time
Current time.
Definition: ntlm.h:20

References DBGC, DBGC2, EACCES_EXPIRED, x509_validity::not_after, x509_validity::not_before, time, 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_is_valid()

static int x509_is_valid ( struct x509_certificate cert)
inlinestatic

Check if X.509 certificate is valid.

Parameters
certX.509 certificate

Definition at line 391 of file x509.h.

391  {
392  return ( cert->flags & X509_FL_VALIDATED );
393 }
unsigned int flags
Flags.
Definition: x509.h:193
Certificate has been validated.
Definition: x509.h:222

References x509_certificate::flags, and X509_FL_VALIDATED.

Referenced by certstat(), ocsp_check(), validator_step(), 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 400 of file x509.h.

400  {
401  cert->flags &= ~X509_FL_VALIDATED;
402  cert->path_remaining = 0;
403 }
unsigned int path_remaining
Maximum number of subsequent certificates in chain.
Definition: x509.h:195
unsigned int flags
Flags.
Definition: x509.h:193
Certificate has been validated.
Definition: x509.h:222

References x509_certificate::flags, x509_certificate::path_remaining, and X509_FL_VALIDATED.

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 410 of file x509.h.

410  {
411  struct x509_link *link;
412 
413  list_for_each_entry ( link, &chain->links, list )
414  x509_invalidate ( link->cert );
415 }
struct list_head links
List of links.
Definition: x509.h:181
#define list_for_each_entry(pos, head, member)
Iterate over entries in a list.
Definition: list.h:420
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:400

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().