iPXE
Macros | Functions | Variables
certstore.c File Reference

Certificate store. More...

#include <string.h>
#include <stdlib.h>
#include <ipxe/init.h>
#include <ipxe/dhcp.h>
#include <ipxe/settings.h>
#include <ipxe/malloc.h>
#include <ipxe/crypto.h>
#include <ipxe/asn1.h>
#include <ipxe/x509.h>
#include <ipxe/certstore.h>

Go to the source code of this file.

Macros

#define CERT(_index, _path)
 Raw certificate data for all permanent stored certificates. More...
 
#define CERT(_index, _path)
 Raw certificate data for all permanent stored certificates. More...
 

Functions

 FILE_LICENCE (GPL2_OR_LATER_OR_UBDL)
 
 FILE_SECBOOT (PERMITTED)
 
static void certstore_found (struct x509_chain *store, struct x509_certificate *cert)
 Mark stored certificate as most recently used. More...
 
void certstore_add (struct x509_certificate *cert)
 Add certificate to store. More...
 
void certstore_del (struct x509_certificate *cert)
 Remove certificate from store. More...
 
static unsigned int certstore_discard (void)
 Discard a stored certificate. More...
 
struct cache_discarder certstore_discarder __cache_discarder (CACHE_NORMAL)
 Certificate store cache discarder. More...
 
static void certstore_init (void)
 Construct permanent certificate store. More...
 
struct init_fn certstore_init_fn __init_fn (INIT_LATE)
 Certificate store initialisation function. More...
 
static struct setting cert_setting __setting (SETTING_CRYPTO, cert)
 Additional certificate setting. More...
 
static int certstore_apply_settings (void)
 Apply certificate store configuration settings. More...
 
 REQUIRING_SYMBOL (certstore)
 
 REQUIRE_OBJECT (config_certs)
 

Variables

static struct asn1_cursor certstore_raw []
 
static struct x509_certificate certstore_certs [sizeof(certstore_raw)/sizeof(certstore_raw[0])]
 X.509 certificate structures for all permanent stored certificates. More...
 
struct x509_chain certstore
 Certificate store. More...
 
struct settings_applicator certstore_applicator __settings_applicator
 Certificate store settings applicator. More...
 

Detailed Description

Certificate store.

Definition in file certstore.c.

Macro Definition Documentation

◆ CERT [1/2]

#define CERT (   _index,
  _path 
)
Value:
extern char stored_cert_ ## _index ## _data[]; \
extern size_t ABS_SYMBOL ( stored_cert_ ## _index ## _len ); \
__asm__ ( ".section \".rodata\", \"a\", " PROGBITS "\n\t" \
"\nstored_cert_" #_index "_data:\n\t" \
".incbin \"" _path "\"\n\t" \
"\nstored_cert_" #_index "_end:\n\t" \
".equ stored_cert_" #_index "_len, " \
"( stored_cert_" #_index "_end - " \
" stored_cert_" #_index "_data )\n\t" \
".previous\n\t" );
#define PROGBITS
Definition: compiler.h:61
#define ABS_SYMBOL(name)
Declare an absolute symbol (e.g.
Definition: compiler.h:655

Raw certificate data for all permanent stored certificates.

Raw certificate cursors for all permanent stored certificates.

Definition at line 61 of file certstore.c.

◆ CERT [2/2]

#define CERT (   _index,
  _path 
)
Value:
{ \
.data = stored_cert_ ## _index ## _data, \
.len = ABS_VALUE_INIT ( stored_cert_ ## _index ## _len ), \
},
#define ABS_VALUE_INIT(name)
Get value of an absolute symbol for use in a static initializer.
Definition: compiler.h:668

Raw certificate data for all permanent stored certificates.

Raw certificate cursors for all permanent stored certificates.

Definition at line 61 of file certstore.c.

Function Documentation

◆ FILE_LICENCE()

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL  )

◆ FILE_SECBOOT()

FILE_SECBOOT ( PERMITTED  )

◆ certstore_found()

static void certstore_found ( struct x509_chain store,
struct x509_certificate cert 
)
static

Mark stored certificate as most recently used.

Parameters
storeCertificate store
certX.509 certificate

Definition at line 79 of file certstore.c.

80  {
81 
82  /* Mark as most recently used */
83  list_del ( &cert->store.list );
84  list_add ( &cert->store.list, &store->links );
85  DBGC2 ( store, "CERTSTORE found certificate %s\n",
86  x509_name ( cert ) );
87 }
#define list_add(new, head)
Add a new entry to the head of a list.
Definition: list.h:70
struct list_head links
List of links.
Definition: x509.h:205
#define list_del(list)
Delete an entry from a list.
Definition: list.h:120
const char * x509_name(struct x509_certificate *cert)
Get X.509 certificate display name.
Definition: x509.c:147
#define DBGC2(...)
Definition: compiler.h:522
struct x509_link store
Link in certificate store.
Definition: x509.h:221

References DBGC2, x509_link::list, list_add, list_del, x509_certificate::store, and x509_name().

◆ certstore_add()

void certstore_add ( struct x509_certificate cert)

Add certificate to store.

Parameters
certX.509 certificate

Definition at line 101 of file certstore.c.

101  {
102 
103  /* Add certificate to store */
104  cert->store.cert = cert;
105  x509_get ( cert );
106  list_add ( &cert->store.list, &certstore.links );
107  DBGC ( &certstore, "CERTSTORE added certificate %s\n",
108  x509_name ( cert ) );
109 }
struct x509_chain certstore
Certificate store.
Definition: certstore.c:90
static struct x509_certificate * x509_get(struct x509_certificate *cert)
Get reference to X.509 certificate.
Definition: x509.h:267
#define list_add(new, head)
Add a new entry to the head of a list.
Definition: list.h:70
struct list_head links
List of links.
Definition: x509.h:205
#define DBGC(...)
Definition: compiler.h:505
const char * x509_name(struct x509_certificate *cert)
Get X.509 certificate display name.
Definition: x509.c:147
struct x509_link store
Link in certificate store.
Definition: x509.h:221

References x509_link::cert, certstore, DBGC, x509_chain::links, x509_link::list, list_add, x509_certificate::store, x509_get(), and x509_name().

Referenced by certstore_init(), and x509_certificate().

◆ certstore_del()

void certstore_del ( struct x509_certificate cert)

Remove certificate from store.

Parameters
certX.509 certificate

Definition at line 116 of file certstore.c.

116  {
117 
118  /* Ignore attempts to remove permanent certificates */
119  if ( cert->flags & X509_FL_PERMANENT )
120  return;
121 
122  /* Remove certificate from store */
123  DBGC ( &certstore, "CERTSTORE removed certificate %s\n",
124  x509_name ( cert ) );
125  list_del ( &cert->store.list );
126  x509_put ( cert );
127 }
struct x509_chain certstore
Certificate store.
Definition: certstore.c:90
#define DBGC(...)
Definition: compiler.h:505
unsigned int flags
Flags.
Definition: x509.h:224
#define list_del(list)
Delete an entry from a list.
Definition: list.h:120
Certificate was added at build time.
Definition: x509.h:255
const char * x509_name(struct x509_certificate *cert)
Get X.509 certificate display name.
Definition: x509.c:147
static void x509_put(struct x509_certificate *cert)
Drop reference to X.509 certificate.
Definition: x509.h:278
struct x509_link store
Link in certificate store.
Definition: x509.h:221

References certstore, DBGC, x509_certificate::flags, x509_link::list, list_del, x509_certificate::store, X509_FL_PERMANENT, x509_name(), and x509_put().

Referenced by certfree_payload(), and certstore_discard().

◆ certstore_discard()

static unsigned int certstore_discard ( void  )
static

Discard a stored certificate.

Return values
discardedNumber of cached items discarded

Definition at line 134 of file certstore.c.

134  {
135  struct x509_certificate *cert;
136 
137  /* Discard the least recently used certificate for which the
138  * only reference is held by the store itself.
139  */
141 
142  /* Skip certificates for which another reference is held */
143  if ( cert->refcnt.count > 0 )
144  continue;
145 
146  /* Skip certificates that were added at build time or
147  * added explicitly at run time.
148  */
149  if ( cert->flags & ( X509_FL_PERMANENT | X509_FL_EXPLICIT ) )
150  continue;
151 
152  /* Discard certificate */
153  certstore_del ( cert );
154  return 1;
155  }
156 
157  return 0;
158 }
struct x509_chain certstore
Certificate store.
Definition: certstore.c:90
struct refcnt refcnt
Reference count.
Definition: x509.h:218
struct list_head links
List of links.
Definition: x509.h:205
void certstore_del(struct x509_certificate *cert)
Remove certificate from store.
Definition: certstore.c:116
unsigned int flags
Flags.
Definition: x509.h:224
int count
Current reference count.
Definition: refcnt.h:33
#define list_for_each_entry_reverse(pos, head, member)
Iterate over entries in a list in reverse order.
Definition: list.h:445
An X.509 certificate.
Definition: x509.h:216
Certificate was added at build time.
Definition: x509.h:255
struct x509_link store
Link in certificate store.
Definition: x509.h:221
Certificate was added explicitly at run time.
Definition: x509.h:257

References certstore, certstore_del(), refcnt::count, x509_certificate::flags, x509_chain::links, x509_link::list, list_for_each_entry_reverse, x509_certificate::refcnt, x509_certificate::store, X509_FL_EXPLICIT, and X509_FL_PERMANENT.

◆ __cache_discarder()

struct cache_discarder certstore_discarder __cache_discarder ( CACHE_NORMAL  )

Certificate store cache discarder.

◆ certstore_init()

static void certstore_init ( void  )
static

Construct permanent certificate store.

Definition at line 169 of file certstore.c.

169  {
170  struct asn1_cursor *raw;
171  struct x509_certificate *cert;
172  int i;
173  int rc;
174 
175  /* Skip if we have no permanent stored certificates */
176  if ( ! sizeof ( certstore_raw ) )
177  return;
178 
179  /* Add certificates */
180  for ( i = 0 ; i < ( int ) ( sizeof ( certstore_raw ) /
181  sizeof ( certstore_raw[0] ) ) ; i++ ) {
182 
183  /* Skip if certificate already present in store */
184  raw = &certstore_raw[i];
185  if ( ( cert = x509_find ( &certstore, raw ) ) != NULL ) {
186  DBGC ( &certstore, "CERTSTORE permanent certificate %d "
187  "is a duplicate of %s\n", i, x509_name ( cert ));
188  continue;
189  }
190 
191  /* Parse certificate */
192  cert = &certstore_certs[i];
193  ref_init ( &cert->refcnt, ref_no_free );
194  if ( ( rc = x509_parse ( cert, raw ) ) != 0 ) {
195  DBGC ( &certstore, "CERTSTORE could not parse "
196  "permanent certificate %d: %s\n",
197  i, strerror ( rc ) );
198  continue;
199  }
200 
201  /* Add certificate to store. Certificate will never
202  * be discarded from the store, since we retain a
203  * permanent reference to it.
204  */
205  certstore_add ( cert );
206  cert->flags |= X509_FL_PERMANENT;
207  DBGC ( &certstore, "CERTSTORE permanent certificate %d is %s\n",
208  i, x509_name ( cert ) );
209  }
210 }
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
struct x509_chain certstore
Certificate store.
Definition: certstore.c:90
struct refcnt refcnt
Reference count.
Definition: x509.h:218
#define ref_init(refcnt, free)
Initialise a reference counter.
Definition: refcnt.h:65
static struct asn1_cursor certstore_raw[]
Definition: certstore.c:65
#define DBGC(...)
Definition: compiler.h:505
void certstore_add(struct x509_certificate *cert)
Add certificate to store.
Definition: certstore.c:101
unsigned int flags
Flags.
Definition: x509.h:224
char * strerror(int errno)
Retrieve string representation of error number.
Definition: strerror.c:79
An X.509 certificate.
Definition: x509.h:216
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:1746
Certificate was added at build time.
Definition: x509.h:255
const char * x509_name(struct x509_certificate *cert)
Get X.509 certificate display name.
Definition: x509.c:147
static struct x509_certificate certstore_certs[sizeof(certstore_raw)/sizeof(certstore_raw[0])]
X.509 certificate structures for all permanent stored certificates.
Definition: certstore.c:70
__be32 raw[7]
Definition: CIB_PRM.h:28
void ref_no_free(struct refcnt *refcnt __unused)
Do not free reference-counted object.
Definition: refcnt.c:102
#define NULL
NULL pointer (VOID *)
Definition: Base.h:322
An ASN.1 object cursor.
Definition: asn1.h:21
int x509_parse(struct x509_certificate *cert, const struct asn1_cursor *raw)
Parse X.509 certificate from ASN.1 data.
Definition: x509.c:1008

References certstore, certstore_add(), certstore_certs, certstore_raw, DBGC, x509_certificate::flags, NULL, raw, rc, ref_init, ref_no_free(), x509_certificate::refcnt, strerror(), x509_find(), X509_FL_PERMANENT, x509_name(), and x509_parse().

◆ __init_fn()

struct init_fn certstore_init_fn __init_fn ( INIT_LATE  )

Certificate store initialisation function.

◆ __setting()

static struct setting cert_setting __setting ( SETTING_CRYPTO  ,
cert   
)
static

Additional certificate setting.

◆ certstore_apply_settings()

static int certstore_apply_settings ( void  )
static

Apply certificate store configuration settings.

Return values
rcReturn status code

Definition at line 231 of file certstore.c.

231  {
232  static struct x509_certificate *cert = NULL;
233  struct x509_certificate *old_cert;
234  void *cert_data;
235  int len;
236  int rc;
237 
238  /* Record any existing additional certificate */
239  old_cert = cert;
240  cert = NULL;
241 
242  /* Add additional certificate, if any */
243  if ( ( len = fetch_raw_setting_copy ( NULL, &cert_setting,
244  &cert_data ) ) >= 0 ) {
245  if ( ( rc = x509_certificate ( cert_data, len, &cert ) ) == 0 ){
246  DBGC ( &certstore, "CERTSTORE added additional "
247  "certificate %s\n", x509_name ( cert ) );
248  } else {
249  DBGC ( &certstore, "CERTSTORE could not parse "
250  "additional certificate: %s\n",
251  strerror ( rc ) );
252  /* Do not fail; leave as an unusable certificate */
253  }
254  free ( cert_data );
255  }
256 
257  /* Free old additional certificiate. Do this after reparsing
258  * the additional certificate; in the common case that the
259  * certificate has not changed, this will allow the stored
260  * certificate to be reused.
261  */
262  x509_put ( old_cert );
263 
264  return 0;
265 }
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
struct x509_chain certstore
Certificate store.
Definition: certstore.c:90
int fetch_raw_setting_copy(struct settings *settings, const struct setting *setting, void **data)
Fetch value of setting.
Definition: settings.c:822
#define DBGC(...)
Definition: compiler.h:505
ring len
Length.
Definition: dwmac.h:231
char * strerror(int errno)
Retrieve string representation of error number.
Definition: strerror.c:79
An X.509 certificate.
Definition: x509.h:216
static void(* free)(struct refcnt *refcnt))
Definition: refcnt.h:55
const char * x509_name(struct x509_certificate *cert)
Get X.509 certificate display name.
Definition: x509.c:147
static void x509_put(struct x509_certificate *cert)
Drop reference to X.509 certificate.
Definition: x509.h:278
#define NULL
NULL pointer (VOID *)
Definition: Base.h:322

References certstore, DBGC, fetch_raw_setting_copy(), free, len, NULL, rc, strerror(), x509_name(), and x509_put().

◆ REQUIRING_SYMBOL()

REQUIRING_SYMBOL ( certstore  )

◆ REQUIRE_OBJECT()

REQUIRE_OBJECT ( config_certs  )

Variable Documentation

◆ certstore_raw

struct asn1_cursor certstore_raw[]
static
Initial value:
= {
CERT_ALL
}

Definition at line 65 of file certstore.c.

Referenced by certstore_init().

◆ certstore_certs

struct x509_certificate certstore_certs[sizeof(certstore_raw)/ sizeof(certstore_raw[0])]
static

X.509 certificate structures for all permanent stored certificates.

Definition at line 70 of file certstore.c.

Referenced by certstore_init().

◆ certstore

struct x509_chain certstore
Initial value:
= {
.refcnt = REF_INIT ( ref_no_free ),
.found = certstore_found,
}
struct x509_chain certstore
Certificate store.
Definition: certstore.c:90
struct list_head links
List of links.
Definition: x509.h:205
static void certstore_found(struct x509_chain *store, struct x509_certificate *cert)
Mark stored certificate as most recently used.
Definition: certstore.c:79
#define REF_INIT(free_fn)
Initialise a static reference counter.
Definition: refcnt.h:78
#define LIST_HEAD_INIT(list)
Initialise a static list head.
Definition: list.h:31
void ref_no_free(struct refcnt *refcnt __unused)
Do not free reference-counted object.
Definition: refcnt.c:102

Certificate store.

Definition at line 90 of file certstore.c.

Referenced by certstore_add(), certstore_apply_settings(), certstore_del(), certstore_discard(), certstore_init(), tls_new_certificate_request(), x509_find(), x509_find_issuer_serial(), x509_find_key(), and x509_find_subject().

◆ __settings_applicator

struct settings_applicator certstore_applicator __settings_applicator
Initial value:
= {
}
static int certstore_apply_settings(void)
Apply certificate store configuration settings.
Definition: certstore.c:231

Certificate store settings applicator.

Definition at line 268 of file certstore.c.