iPXE
Functions | Variables
efi_cacert.c File Reference

EFI CA certificates. More...

#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <errno.h>
#include <ipxe/init.h>
#include <ipxe/x509.h>
#include <ipxe/rootcert.h>
#include <ipxe/efi/efi.h>
#include <ipxe/efi/efi_siglist.h>
#include <ipxe/efi/Guid/TlsAuthentication.h>

Go to the source code of this file.

Functions

 FILE_LICENCE (GPL2_OR_LATER_OR_UBDL)
 
static int efi_cacert (const void *data, size_t len, size_t offset)
 Retrieve EFI CA certificate. More...
 
static int efi_cacert_all (void)
 Retrieve all EFI CA certificates. More...
 
static void efi_cacert_init (void)
 Initialise EFI CA certificates. More...
 
struct init_fn efi_cacert_init_fn __init_fn (INIT_LATE)
 EFI CA certificates initialisation function. More...
 
static void efi_cacert_shutdown (int booting __unused)
 Discard any EFI CA certificates. More...
 
struct startup_fn efi_cacert_shutdown_fn __startup_fn (STARTUP_NORMAL)
 EFI CA certificates shutdown function. More...
 

Variables

static struct x509_chain efi_cacerts
 List of EFI CA certificates. More...
 

Detailed Description

EFI CA certificates.

Definition in file efi_cacert.c.

Function Documentation

◆ FILE_LICENCE()

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL  )

◆ efi_cacert()

static int efi_cacert ( const void *  data,
size_t  len,
size_t  offset 
)
static

Retrieve EFI CA certificate.

Parameters
dataTlsCaCertificate variable data
lenLength of TlsCaCertificate
offsetOffset within data
nextNext offset, or negative error

Definition at line 57 of file efi_cacert.c.

57  {
58  struct asn1_cursor *cursor;
59  struct x509_certificate *cert;
60  int next;
61  int rc;
62 
63  /* Extract ASN.1 object */
64  next = efisig_asn1 ( data, len, offset, &cursor );
65  if ( next < 0 ) {
66  rc = next;
67  DBGC ( &efi_cacerts, "EFICA could not parse at +%#zx: %s\n",
68  offset, strerror ( rc ) );
69  goto err_asn1;
70  }
71 
72  /* Append to list of EFI CA certificates */
73  if ( ( rc = x509_append_raw ( &efi_cacerts, cursor->data,
74  cursor->len ) ) != 0 ) {
75  DBGC ( &efi_cacerts, "EFICA could not append at +%#zx: %s\n",
76  offset, strerror ( rc ) );
77  goto err_append;
78  }
79  cert = x509_last ( &efi_cacerts );
80  DBGC ( &efi_cacerts, "EFICA found certificate %s\n",
81  x509_name ( cert ) );
82 
83  /* Mark certificate as valid (i.e. trusted) if permitted */
84  if ( allow_trust_override ) {
85  DBGC ( &efi_cacerts, "EFICA trusting certificate %s\n",
86  x509_name ( cert ) );
88  }
89 
90  /* Free ASN.1 object */
91  free ( cursor );
92 
93  return next;
94 
95  err_append:
96  free ( cursor );
97  err_asn1:
98  return rc;
99 }
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
const int allow_trust_override
Flag indicating if root of trust may be overridden at runtime.
Definition: rootcert.c:64
int x509_append_raw(struct x509_chain *chain, const void *data, size_t len)
Append X.509 certificate to X.509 certificate chain.
Definition: x509.c:1673
struct x509_root root_certificates
Root certificates.
Definition: rootcert.c:78
const void * data
Start of data.
Definition: asn1.h:22
#define DBGC(...)
Definition: compiler.h:505
size_t len
Length of data.
Definition: asn1.h:24
static struct x509_chain efi_cacerts
List of EFI CA certificates.
Definition: efi_cacert.c:44
int efisig_asn1(const void *data, size_t len, size_t offset, struct asn1_cursor **cursor)
Extract ASN.1 object from EFI signature list.
Definition: efi_siglist.c:142
ring len
Length.
Definition: dwmac.h:231
static struct x509_certificate * x509_last(struct x509_chain *chain)
Get last certificate in X.509 certificate chain.
Definition: x509.h:324
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
uint32_t next
Next descriptor address.
Definition: dwmac.h:22
const char * x509_name(struct x509_certificate *cert)
Get X.509 certificate display name.
Definition: x509.c:146
uint8_t data[48]
Additional event data.
Definition: ena.h:22
uint16_t offset
Offset to command line.
Definition: bzimage.h:8
#define NULL
NULL pointer (VOID *)
Definition: Base.h:321
An ASN.1 object cursor.
Definition: asn1.h:20

References allow_trust_override, asn1_cursor::data, data, DBGC, efi_cacerts, efisig_asn1(), free, asn1_cursor::len, len, next, NULL, offset, rc, root_certificates, strerror(), x509_append_raw(), x509_last(), x509_name(), and x509_set_valid().

Referenced by efi_cacert_all(), efi_cacert_init(), and efi_cacert_shutdown().

◆ efi_cacert_all()

static int efi_cacert_all ( void  )
static

Retrieve all EFI CA certificates.

Return values
rcReturn status code

Definition at line 106 of file efi_cacert.c.

106  {
109  static CHAR16 *wname = EFI_TLS_CA_CERTIFICATE_VARIABLE;
110  int offset = 0;
111  UINT32 attrs;
112  UINTN size;
113  void *data;
114  EFI_STATUS efirc;
115  int rc;
116 
117  /* Get variable length */
118  size = 0;
119  if ( ( efirc = rs->GetVariable ( wname, guid, &attrs, &size,
120  NULL ) ) != EFI_BUFFER_TOO_SMALL ) {
121  rc = -EEFI ( efirc );
122  DBGC ( &efi_cacerts, "EFICA could not get %ls size: %s\n",
123  wname, strerror ( rc ) );
124  goto err_len;
125  }
126 
127  /* Allocate temporary buffer */
128  data = malloc ( size );
129  if ( ! data ) {
130  rc = -ENOMEM;
131  goto err_alloc;
132  }
133 
134  /* Read variable */
135  if ( ( efirc = rs->GetVariable ( wname, guid, &attrs, &size,
136  data ) ) != 0 ) {
137  rc = -EEFI ( efirc );
138  DBGC ( &efi_cacerts, "EFICA could not read %ls: %s\n",
139  wname, strerror ( rc ) );
140  goto err_get;
141  }
142 
143  /* Parse certificates */
144  while ( ( ( size_t ) offset ) < size ) {
145  offset = efi_cacert ( data, size, offset );
146  if ( offset < 0 ) {
147  rc = offset;
148  goto err_cacert;
149  }
150  }
151 
152  /* Success */
153  rc = 0;
154 
155  err_cacert:
156  err_get:
157  free ( data );
158  err_alloc:
159  err_len:
160  return rc;
161 }
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
#define EEFI(efirc)
Convert an EFI status code to an iPXE status code.
Definition: efi.h:174
128 bit buffer containing a unique identifier value.
Definition: Base.h:215
uint16_t size
Buffer size.
Definition: dwmac.h:14
#define DBGC(...)
Definition: compiler.h:505
unsigned int UINT32
Definition: ProcessorBind.h:98
unsigned short CHAR16
#define EFI_BUFFER_TOO_SMALL
Enumeration of EFI_STATUS.
Definition: UefiBaseType.h:119
#define ENOMEM
Not enough space.
Definition: errno.h:534
static struct x509_chain efi_cacerts
List of EFI CA certificates.
Definition: efi_cacert.c:44
#define EFI_TLS_CA_CERTIFICATE_VARIABLE
EFI_GET_VARIABLE GetVariable
Definition: UefiSpec.h:1902
EFI Runtime Services Table.
Definition: UefiSpec.h:1879
char * strerror(int errno)
Retrieve string representation of error number.
Definition: strerror.c:78
static void(* free)(struct refcnt *refcnt))
Definition: refcnt.h:54
UINT64 UINTN
Unsigned value of native width.
void * malloc(size_t size)
Allocate memory.
Definition: malloc.c:620
uint64_t guid
GUID.
Definition: edd.h:30
EFI_RUNTIME_SERVICES * RuntimeServices
A pointer to the EFI Runtime Services Table.
Definition: UefiSpec.h:2094
RETURN_STATUS EFI_STATUS
Function return status for EFI API.
Definition: UefiBaseType.h:31
EFI_GUID efi_tls_ca_certificate_guid
TLS CA certificate variable GUID.
Definition: efi_guid.c:477
uint8_t data[48]
Additional event data.
Definition: ena.h:22
EFI_SYSTEM_TABLE * efi_systab
uint16_t offset
Offset to command line.
Definition: bzimage.h:8
static int efi_cacert(const void *data, size_t len, size_t offset)
Retrieve EFI CA certificate.
Definition: efi_cacert.c:57
#define NULL
NULL pointer (VOID *)
Definition: Base.h:321

References data, DBGC, EEFI, EFI_BUFFER_TOO_SMALL, efi_cacert(), efi_cacerts, efi_systab, efi_tls_ca_certificate_guid, EFI_TLS_CA_CERTIFICATE_VARIABLE, ENOMEM, free, EFI_RUNTIME_SERVICES::GetVariable, guid, malloc(), NULL, offset, rc, EFI_SYSTEM_TABLE::RuntimeServices, size, and strerror().

Referenced by efi_cacert_init().

◆ efi_cacert_init()

static void efi_cacert_init ( void  )
static

Initialise EFI CA certificates.

Definition at line 167 of file efi_cacert.c.

167  {
168  int rc;
169 
170  /* Initialise all certificates */
171  if ( ( rc = efi_cacert_all() ) != 0 ) {
172  DBGC ( &efi_cacert, "EFICA could not initialise: %s\n",
173  strerror ( rc ) );
174  /* Nothing we can do at this point */
175  return;
176  }
177 }
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
#define DBGC(...)
Definition: compiler.h:505
static int efi_cacert_all(void)
Retrieve all EFI CA certificates.
Definition: efi_cacert.c:106
char * strerror(int errno)
Retrieve string representation of error number.
Definition: strerror.c:78
static int efi_cacert(const void *data, size_t len, size_t offset)
Retrieve EFI CA certificate.
Definition: efi_cacert.c:57

References DBGC, efi_cacert(), efi_cacert_all(), rc, and strerror().

◆ __init_fn()

struct init_fn efi_cacert_init_fn __init_fn ( INIT_LATE  )

EFI CA certificates initialisation function.

◆ efi_cacert_shutdown()

static void efi_cacert_shutdown ( int booting  __unused)
static

Discard any EFI CA certificates.

Definition at line 189 of file efi_cacert.c.

189  {
190 
191  /* Drop our references to the certificates */
192  DBGC ( &efi_cacert, "EFICA discarding certificates\n" );
195 }
struct list_head links
List of links.
Definition: x509.h:204
#define DBGC(...)
Definition: compiler.h:505
void x509_truncate(struct x509_chain *chain, struct x509_link *link)
Truncate X.509 certificate chain.
Definition: x509.c:1703
#define list_empty(list)
Test whether a list is empty.
Definition: list.h:136
static struct x509_chain efi_cacerts
List of EFI CA certificates.
Definition: efi_cacert.c:44
assert((readw(&hdr->flags) &(GTF_reading|GTF_writing))==0)
static int efi_cacert(const void *data, size_t len, size_t offset)
Retrieve EFI CA certificate.
Definition: efi_cacert.c:57
#define NULL
NULL pointer (VOID *)
Definition: Base.h:321

References assert(), DBGC, efi_cacert(), efi_cacerts, x509_chain::links, list_empty, NULL, and x509_truncate().

◆ __startup_fn()

struct startup_fn efi_cacert_shutdown_fn __startup_fn ( STARTUP_NORMAL  )

EFI CA certificates shutdown function.

Variable Documentation

◆ efi_cacerts

struct x509_chain efi_cacerts
static
Initial value:
= {
.refcnt = REF_INIT ( ref_no_free ),
}
struct list_head links
List of links.
Definition: x509.h:204
static struct x509_chain efi_cacerts
List of EFI CA certificates.
Definition: efi_cacert.c:44
#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

List of EFI CA certificates.

Definition at line 44 of file efi_cacert.c.

Referenced by efi_cacert(), efi_cacert_all(), and efi_cacert_shutdown().