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

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 char 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

Raw certificate data for all permanent stored certificates.

Raw certificate cursors for all permanent stored certificates.

Definition at line 60 of file certstore.c.

◆ CERT [2/2]

#define CERT (   _index,
  _path 
)
Value:
{ \
.data = stored_cert_ ## _index ## _data, \
.len = ( size_t ) stored_cert_ ## _index ## _len, \
},
__SIZE_TYPE__ size_t
Definition: stdint.h:6

Raw certificate data for all permanent stored certificates.

Raw certificate cursors for all permanent stored certificates.

Definition at line 60 of file certstore.c.

Function Documentation

◆ FILE_LICENCE()

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL  )

◆ 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 78 of file certstore.c.

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

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 100 of file certstore.c.

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

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 115 of file certstore.c.

115  {
116 
117  /* Ignore attempts to remove permanent certificates */
118  if ( cert->flags & X509_FL_PERMANENT )
119  return;
120 
121  /* Remove certificate from store */
122  DBGC ( &certstore, "CERTSTORE removed certificate %s\n",
123  x509_name ( cert ) );
124  list_del ( &cert->store.list );
125  x509_put ( cert );
126 }
struct x509_chain certstore
Certificate store.
Definition: certstore.c:89
#define DBGC(...)
Definition: compiler.h:505
unsigned int flags
Flags.
Definition: x509.h:223
#define list_del(list)
Delete an entry from a list.
Definition: list.h:119
Certificate was added at build time.
Definition: x509.h:254
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
struct x509_link store
Link in certificate store.
Definition: x509.h:220

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 133 of file certstore.c.

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

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 168 of file certstore.c.

168  {
169  struct asn1_cursor *raw;
170  struct x509_certificate *cert;
171  int i;
172  int rc;
173 
174  /* Skip if we have no permanent stored certificates */
175  if ( ! sizeof ( certstore_raw ) )
176  return;
177 
178  /* Add certificates */
179  for ( i = 0 ; i < ( int ) ( sizeof ( certstore_raw ) /
180  sizeof ( certstore_raw[0] ) ) ; i++ ) {
181 
182  /* Skip if certificate already present in store */
183  raw = &certstore_raw[i];
184  if ( ( cert = x509_find ( &certstore, raw ) ) != NULL ) {
185  DBGC ( &certstore, "CERTSTORE permanent certificate %d "
186  "is a duplicate of %s\n", i, x509_name ( cert ));
187  continue;
188  }
189 
190  /* Parse certificate */
191  cert = &certstore_certs[i];
192  ref_init ( &cert->refcnt, ref_no_free );
193  if ( ( rc = x509_parse ( cert, raw ) ) != 0 ) {
194  DBGC ( &certstore, "CERTSTORE could not parse "
195  "permanent certificate %d: %s\n",
196  i, strerror ( rc ) );
197  continue;
198  }
199 
200  /* Add certificate to store. Certificate will never
201  * be discarded from the store, since we retain a
202  * permanent reference to it.
203  */
204  certstore_add ( cert );
205  cert->flags |= X509_FL_PERMANENT;
206  DBGC ( &certstore, "CERTSTORE permanent certificate %d is %s\n",
207  i, x509_name ( cert ) );
208  }
209 }
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
struct x509_chain certstore
Certificate store.
Definition: certstore.c:89
struct refcnt refcnt
Reference count.
Definition: x509.h:217
#define ref_init(refcnt, free)
Initialise a reference counter.
Definition: refcnt.h:64
static struct asn1_cursor certstore_raw[]
Definition: certstore.c:64
#define DBGC(...)
Definition: compiler.h:505
void certstore_add(struct x509_certificate *cert)
Add certificate to store.
Definition: certstore.c:100
unsigned int flags
Flags.
Definition: x509.h:223
char * strerror(int errno)
Retrieve string representation of error number.
Definition: strerror.c:78
An X.509 certificate.
Definition: x509.h:215
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:1732
Certificate was added at build time.
Definition: x509.h:254
const char * x509_name(struct x509_certificate *cert)
Get X.509 certificate display name.
Definition: x509.c:146
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:69
__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:101
#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:1004

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 229 of file certstore.c.

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

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

Variable Documentation

◆ certstore_raw

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

Definition at line 64 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 69 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:89
struct list_head links
List of links.
Definition: x509.h:204
static void certstore_found(struct x509_chain *store, struct x509_certificate *cert)
Mark stored certificate as most recently used.
Definition: certstore.c:78
#define REF_INIT(free_fn)
Initialise a static reference counter.
Definition: refcnt.h:77
#define LIST_HEAD_INIT(list)
Initialise a static list head.
Definition: list.h:30
void ref_no_free(struct refcnt *refcnt __unused)
Do not free reference-counted object.
Definition: refcnt.c:101

Certificate store.

Definition at line 89 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:229

Certificate store settings applicator.

Definition at line 266 of file certstore.c.