iPXE
Data Structures | Functions | Variables
validator.c File Reference

Certificate validator. More...

#include <string.h>
#include <stdio.h>
#include <errno.h>
#include <ipxe/refcnt.h>
#include <ipxe/malloc.h>
#include <ipxe/interface.h>
#include <ipxe/xfer.h>
#include <ipxe/open.h>
#include <ipxe/iobuf.h>
#include <ipxe/xferbuf.h>
#include <ipxe/process.h>
#include <ipxe/x509.h>
#include <ipxe/settings.h>
#include <ipxe/dhcp.h>
#include <ipxe/base64.h>
#include <ipxe/crc32.h>
#include <ipxe/ocsp.h>
#include <ipxe/job.h>
#include <ipxe/validator.h>
#include <config/crypto.h>

Go to the source code of this file.

Data Structures

struct  validator_action
 A certificate validator action. More...
 
struct  validator
 A certificate validator. More...
 

Functions

 FILE_LICENCE (GPL2_OR_LATER_OR_UBDL)
 
 FILE_SECBOOT (PERMITTED)
 
static const char * validator_name (struct validator *validator)
 Get validator name (for debug messages) More...
 
static void validator_free (struct refcnt *refcnt)
 Free certificate validator. More...
 
static void validator_finished (struct validator *validator, int rc)
 Mark certificate validation as finished. More...
 
static int validator_progress (struct validator *validator, struct job_progress *progress)
 Report job progress. More...
 
const struct setting crosscert_setting __setting (SETTING_CRYPTO, crosscert)
 Cross-signed certificate source setting. More...
 
static void validator_append (struct validator *validator, int rc)
 Append cross-signing certificates to certificate chain. More...
 
static int validator_start_download (struct validator *validator, struct x509_link *link)
 Start download of cross-signing certificate. More...
 
static void validator_ocsp_validate (struct validator *validator, int rc)
 Validate OCSP response. More...
 
static int validator_start_ocsp (struct validator *validator, struct x509_certificate *cert, struct x509_certificate *issuer)
 Start OCSP check. More...
 
static void validator_xfer_close (struct validator *validator, int rc)
 Close data transfer interface. More...
 
static int validator_xfer_deliver (struct validator *validator, struct io_buffer *iobuf, struct xfer_metadata *meta)
 Receive data. More...
 
static void validator_step (struct validator *validator)
 Certificate validation process. More...
 
int create_validator (struct interface *job, struct x509_chain *chain, struct x509_root *root)
 Instantiate a certificate validator. More...
 

Variables

static struct interface_operation validator_job_operations []
 Certificate validator job control interface operations. More...
 
static struct interface_descriptor validator_job_desc
 Certificate validator job control interface descriptor. More...
 
static const char crosscert_default [] = CROSSCERT
 Default cross-signed certificate source. More...
 
static const struct validator_action validator_crosscert
 Cross-signing certificate download validator action. More...
 
static const struct validator_action validator_ocsp
 OCSP validator action. More...
 
static struct interface_operation validator_xfer_operations []
 Certificate validator data transfer interface operations. More...
 
static struct interface_descriptor validator_xfer_desc
 Certificate validator data transfer interface descriptor. More...
 
static struct process_descriptor validator_process_desc
 Certificate validator process descriptor. More...
 

Detailed Description

Certificate validator.

Definition in file validator.c.

Function Documentation

◆ FILE_LICENCE()

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL  )

◆ FILE_SECBOOT()

FILE_SECBOOT ( PERMITTED  )

◆ validator_name()

static const char* validator_name ( struct validator validator)
static

Get validator name (for debug messages)

Parameters
validatorCertificate validator
Return values
nameValidator name

Definition at line 138 of file validator.c.

138  {
139  struct x509_certificate *cert;
140 
141  /* Use name of first certificate in chain, if present */
142  cert = x509_first ( validator->chain );
143  return ( cert ? x509_name ( cert ) : "<empty>" );
144 }
struct x509_chain * chain
X.509 certificate chain.
Definition: validator.c:113
A certificate validator.
Definition: validator.c:65
An X.509 certificate.
Definition: x509.h:216
const char * x509_name(struct x509_certificate *cert)
Get X.509 certificate display name.
Definition: x509.c:147
static struct x509_certificate * x509_first(struct x509_chain *chain)
Get first certificate in X.509 certificate chain.
Definition: x509.h:311

References validator::chain, x509_first(), and x509_name().

Referenced by create_validator(), validator_append(), validator_free(), validator_ocsp_validate(), validator_start_download(), validator_start_ocsp(), validator_step(), validator_xfer_close(), and validator_xfer_deliver().

◆ validator_free()

static void validator_free ( struct refcnt refcnt)
static

Free certificate validator.

Parameters
refcntReference count

Definition at line 151 of file validator.c.

151  {
152  struct validator *validator =
153  container_of ( refcnt, struct validator, refcnt );
154 
155  DBGC2 ( validator, "VALIDATOR %p \"%s\" freed\n",
159  ocsp_put ( validator->ocsp );
161  free ( validator );
162 }
static void x509_chain_put(struct x509_chain *chain)
Drop reference to X.509 certificate chain.
Definition: x509.h:300
static void x509_root_put(struct x509_root *root)
Drop reference to X.509 root certificate list.
Definition: x509.h:404
struct x509_chain * chain
X.509 certificate chain.
Definition: validator.c:113
void xferbuf_free(struct xfer_buffer *xferbuf)
Free data transfer buffer.
Definition: xferbuf.c:74
A reference counter.
Definition: refcnt.h:27
A certificate validator.
Definition: validator.c:65
#define container_of(ptr, type, field)
Get containing structure.
Definition: stddef.h:36
struct ocsp_check * ocsp
OCSP check.
Definition: validator.c:115
struct xfer_buffer buffer
Data buffer.
Definition: validator.c:117
static void(* free)(struct refcnt *refcnt))
Definition: refcnt.h:55
static void ocsp_put(struct ocsp_check *ocsp)
Drop reference to OCSP check.
Definition: ocsp.h:119
#define DBGC2(...)
Definition: compiler.h:522
struct x509_root * root
Root of trust (or NULL to use default)
Definition: validator.c:111
static const char * validator_name(struct validator *validator)
Get validator name (for debug messages)
Definition: validator.c:138

References validator::buffer, validator::chain, container_of, DBGC2, free, validator::ocsp, ocsp_put(), validator::root, validator_name(), x509_chain_put(), x509_root_put(), and xferbuf_free().

Referenced by create_validator().

◆ validator_finished()

static void validator_finished ( struct validator validator,
int  rc 
)
static

Mark certificate validation as finished.

Parameters
validatorCertificate validator
rcReason for finishing

Definition at line 170 of file validator.c.

170  {
171 
172  /* Remove process */
174 
175  /* Close all interfaces */
177  intf_shutdown ( &validator->job, rc );
178 }
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
void intf_shutdown(struct interface *intf, int rc)
Shut down an object interface.
Definition: interface.c:279
struct process process
Process.
Definition: validator.c:74
void process_del(struct process *process)
Remove process from process list.
Definition: process.c:80
A certificate validator.
Definition: validator.c:65
struct interface xfer
Data transfer interface.
Definition: validator.c:71
struct interface job
Job control interface.
Definition: validator.c:69

References intf_shutdown(), validator::job, validator::process, process_del(), rc, and validator::xfer.

Referenced by create_validator(), and validator_step().

◆ validator_progress()

static int validator_progress ( struct validator validator,
struct job_progress progress 
)
static

Report job progress.

Parameters
validatorCertificate validator
progressProgress report to fill in
Return values
ongoing_rcOngoing job status code (if known)

Definition at line 193 of file validator.c.

194  {
195 
196  /* Report current action, if applicable */
197  if ( validator->action ) {
198  snprintf ( progress->message, sizeof ( progress->message ),
199  "%s %s", validator->action->name,
200  x509_name ( validator->cert ) );
201  }
202 
203  return 0;
204 }
A certificate validator.
Definition: validator.c:65
const struct validator_action * action
Current action.
Definition: validator.c:120
char message[32]
Message (optional)
Definition: job.h:33
const char * name
Name.
Definition: validator.c:59
struct x509_certificate * cert
Current certificate (for progress reporting)
Definition: validator.c:127
const char * x509_name(struct x509_certificate *cert)
Get X.509 certificate display name.
Definition: x509.c:147
int snprintf(char *buf, size_t size, const char *fmt,...)
Write a formatted string to a buffer.
Definition: vsprintf.c:383

References validator::action, validator::cert, job_progress::message, validator_action::name, snprintf(), and x509_name().

◆ __setting()

const struct setting crosscert_setting __setting ( SETTING_CRYPTO  ,
crosscert   
)

Cross-signed certificate source setting.

◆ validator_append()

static void validator_append ( struct validator validator,
int  rc 
)
static

Append cross-signing certificates to certificate chain.

Parameters
validatorCertificate validator
rcCompletion status code
Return values
rcReturn status code

Definition at line 240 of file validator.c.

240  {
241  struct asn1_cursor cursor;
242  struct x509_chain *certs;
243  struct x509_certificate *cert;
244  struct x509_link *link;
245  struct x509_link *prev;
246 
247  /* Check for errors */
248  if ( rc != 0 ) {
249  DBGC ( validator, "VALIDATOR %p \"%s\" could not download ",
251  DBGC ( validator, "\"%s\" cross-signature: %s\n",
252  x509_name ( validator->cert ), strerror ( rc ) );
253  /* If the overall validation is going to fail, then we
254  * will end up attempting multiple downloads for
255  * non-existent cross-signed certificates as we work
256  * our way up the certificate chain. Do not record
257  * these as relevant errors, since we want to
258  * eventually report whichever much more relevant
259  * error occurred previously.
260  */
261  goto err_irrelevant;
262  }
263  DBGC ( validator, "VALIDATOR %p \"%s\" downloaded ",
265  DBGC ( validator, "\"%s\" cross-signature\n",
266  x509_name ( validator->cert ) );
267 
268  /* Allocate certificate list */
269  certs = x509_alloc_chain();
270  if ( ! certs ) {
271  rc = -ENOMEM;
272  goto err_alloc_certs;
273  }
274 
275  /* Initialise cursor */
276  cursor.data = validator->buffer.data;
277  cursor.len = validator->buffer.len;
278 
279  /* Enter certificateSet */
280  if ( ( rc = asn1_enter ( &cursor, ASN1_SET ) ) != 0 ) {
281  DBGC ( validator, "VALIDATOR %p \"%s\" could not enter "
282  "certificateSet: %s\n", validator,
283  validator_name ( validator ), strerror ( rc ) );
284  goto err_certificateset;
285  }
286 
287  /* Add each certificate to list */
288  while ( cursor.len ) {
289 
290  /* Add certificate to list */
291  if ( ( rc = x509_append_raw ( certs, cursor.data,
292  cursor.len ) ) != 0 ) {
293  DBGC ( validator, "VALIDATOR %p \"%s\" could not "
294  "append certificate: %s\n", validator,
296  DBGC_HDA ( validator, 0, cursor.data, cursor.len );
297  goto err_append_raw;
298  }
299  cert = x509_last ( certs );
300  DBGC ( validator, "VALIDATOR %p \"%s\" found certificate ",
302  DBGC ( validator, "%s\n", x509_name ( cert ) );
303 
304  /* Move to next certificate */
305  asn1_skip_any ( &cursor );
306  }
307 
308  /* Truncate existing certificate chain at current link */
309  link = validator->link;
310  assert ( link->flags & X509_LINK_FL_CROSSED );
312 
313  /* Append certificates to chain */
314  if ( ( rc = x509_auto_append ( validator->chain, certs ) ) != 0 ) {
315  DBGC ( validator, "VALIDATOR %p \"%s\" could not append "
316  "certificates: %s\n", validator,
317  validator_name ( validator ), strerror ( rc ) );
318  goto err_auto_append;
319  }
320 
321  /* Record that a cross-signed certificate download has already
322  * been performed for all but the last of the appended
323  * certificates. (It may be necessary to perform a further
324  * download to complete the chain, if this download did not
325  * extend all the way to a root of trust.)
326  */
327  prev = NULL;
329  if ( prev )
330  prev->flags |= X509_LINK_FL_CROSSED;
331  prev = link;
332  }
333 
334  /* Success */
335  rc = 0;
336 
337  err_auto_append:
338  err_append_raw:
339  err_certificateset:
340  x509_chain_put ( certs );
341  err_alloc_certs:
342  validator->rc = rc;
343  err_irrelevant:
344  /* Do not record irrelevant errors */
345  return;
346 }
void * data
Data.
Definition: xferbuf.h:21
static void x509_chain_put(struct x509_chain *chain)
Drop reference to X.509 certificate chain.
Definition: x509.h:300
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:169
struct list_head links
List of links.
Definition: x509.h:205
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:1674
#define list_for_each_entry_continue(pos, head, member)
Iterate over entries in a list, starting after current position.
Definition: list.h:474
#define DBGC(...)
Definition: compiler.h:505
struct x509_chain * x509_alloc_chain(void)
Allocate X.509 certificate chain.
Definition: x509.c:1615
struct x509_chain * chain
X.509 certificate chain.
Definition: validator.c:113
void x509_truncate(struct x509_chain *chain, struct x509_link *link)
Truncate X.509 certificate chain.
Definition: x509.c:1704
int asn1_skip_any(struct asn1_cursor *cursor)
Skip ASN.1 object of any type.
Definition: asn1.c:290
#define ASN1_SET
ASN.1 set.
Definition: asn1.h:93
A certificate validator.
Definition: validator.c:65
size_t len
Size of data.
Definition: xferbuf.h:23
An X.509 certificate chain.
Definition: x509.h:201
#define ENOMEM
Not enough space.
Definition: errno.h:535
assert((readw(&hdr->flags) &(GTF_reading|GTF_writing))==0)
#define DBGC_HDA(...)
Definition: compiler.h:506
u32 link
Link to next descriptor.
Definition: ar9003_mac.h:25
struct xfer_buffer buffer
Data buffer.
Definition: validator.c:117
int x509_auto_append(struct x509_chain *chain, struct x509_chain *store)
Append X.509 certificates to X.509 certificate chain.
Definition: x509.c:1868
static struct x509_certificate * x509_last(struct x509_chain *chain)
Get last certificate in X.509 certificate chain.
Definition: x509.h:325
char * strerror(int errno)
Retrieve string representation of error number.
Definition: strerror.c:79
An X.509 certificate.
Definition: x509.h:216
int rc
Most relevant status code.
Definition: validator.c:108
struct x509_certificate * cert
Current certificate (for progress reporting)
Definition: validator.c:127
const char * x509_name(struct x509_certificate *cert)
Get X.509 certificate display name.
Definition: x509.c:147
struct x509_link * link
Current link within certificate chain.
Definition: validator.c:129
#define NULL
NULL pointer (VOID *)
Definition: Base.h:322
An ASN.1 object cursor.
Definition: asn1.h:21
Cross-signed certificate download has been attempted.
Definition: x509.h:188
static const char * validator_name(struct validator *validator)
Get validator name (for debug messages)
Definition: validator.c:138

References asn1_enter(), ASN1_SET, asn1_skip_any(), assert(), validator::buffer, validator::cert, x509_link::cert, validator::chain, xfer_buffer::data, asn1_cursor::data, DBGC, DBGC_HDA, ENOMEM, x509_link::flags, xfer_buffer::len, asn1_cursor::len, link, validator::link, x509_chain::links, x509_link::list, list_for_each_entry_continue, NULL, validator::rc, rc, strerror(), validator_name(), x509_alloc_chain(), x509_append_raw(), x509_auto_append(), x509_chain_put(), x509_last(), X509_LINK_FL_CROSSED, x509_name(), and x509_truncate().

◆ validator_start_download()

static int validator_start_download ( struct validator validator,
struct x509_link link 
)
static

Start download of cross-signing certificate.

Parameters
validatorCertificate validator
linkLink in certificate chain
Return values
rcReturn status code

Definition at line 361 of file validator.c.

362  {
363  struct x509_certificate *cert = link->cert;
364  const struct asn1_cursor *issuer = &cert->issuer.raw;
365  const char *crosscert;
366  char *crosscert_copy;
367  char *uri_string;
368  size_t uri_string_len;
369  uint32_t crc;
370  int len;
371  int rc;
372 
373  /* Determine cross-signed certificate source */
374  fetch_string_setting_copy ( NULL, &crosscert_setting, &crosscert_copy );
375  crosscert = ( crosscert_copy ? crosscert_copy : crosscert_default );
376  if ( ! crosscert[0] ) {
377  rc = -EINVAL;
378  goto err_check_uri_string;
379  }
380 
381  /* Allocate URI string */
382  uri_string_len = ( strlen ( crosscert ) + 22 /* "/%08x.der?subject=" */
383  + base64_encoded_len ( issuer->len ) + 1 /* NUL */ );
384  uri_string = zalloc ( uri_string_len );
385  if ( ! uri_string ) {
386  rc = -ENOMEM;
387  goto err_alloc_uri_string;
388  }
389 
390  /* Generate CRC32 */
391  crc = crc32_le ( 0xffffffffUL, issuer->data, issuer->len );
392 
393  /* Generate URI string */
394  len = snprintf ( uri_string, uri_string_len, "%s/%08x.der?subject=",
395  crosscert, crc );
396  base64_encode ( issuer->data, issuer->len, ( uri_string + len ),
397  ( uri_string_len - len ) );
398  DBGC ( validator, "VALIDATOR %p \"%s\" downloading ",
400  DBGC ( validator, "\"%s\" cross-signature from %s\n",
401  x509_name ( cert ), uri_string );
402 
403  /* Set completion handler */
405  validator->cert = cert;
406  validator->link = link;
407 
408  /* Open URI */
409  if ( ( rc = xfer_open_uri_string ( &validator->xfer,
410  uri_string ) ) != 0 ) {
411  DBGC ( validator, "VALIDATOR %p \"%s\" could not open %s: "
412  "%s\n", validator, validator_name ( validator ),
413  uri_string, strerror ( rc ) );
414  goto err_open_uri_string;
415  }
416 
417  /* Free temporary allocations */
418  free ( uri_string );
419  free ( crosscert_copy );
420 
421  /* Success */
422  return 0;
423 
424  intf_restart ( &validator->xfer, rc );
425  err_open_uri_string:
426  free ( uri_string );
427  err_alloc_uri_string:
428  err_check_uri_string:
429  free ( crosscert_copy );
430  validator->rc = rc;
431  return rc;
432 }
#define EINVAL
Invalid argument.
Definition: errno.h:429
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
struct asn1_cursor raw
Raw issuer.
Definition: x509.h:32
void intf_restart(struct interface *intf, int rc)
Shut down and restart an object interface.
Definition: interface.c:344
struct x509_issuer issuer
Issuer.
Definition: x509.h:241
const void * data
Start of data.
Definition: asn1.h:23
#define DBGC(...)
Definition: compiler.h:505
static const char crosscert_default[]
Default cross-signed certificate source.
Definition: validator.c:231
size_t len
Length of data.
Definition: asn1.h:25
A certificate validator.
Definition: validator.c:65
u32 crc32_le(u32 seed, const void *data, size_t len)
Calculate 32-bit little-endian CRC checksum.
Definition: crc32.c:40
struct interface xfer
Data transfer interface.
Definition: validator.c:71
static size_t base64_encoded_len(size_t raw_len)
Calculate length of base64-encoded data.
Definition: base64.h:22
#define ENOMEM
Not enough space.
Definition: errno.h:535
const struct validator_action * action
Current action.
Definition: validator.c:120
ring len
Length.
Definition: dwmac.h:231
u32 link
Link to next descriptor.
Definition: ar9003_mac.h:25
int fetch_string_setting_copy(struct settings *settings, const struct setting *setting, char **data)
Fetch value of string setting.
Definition: settings.c:874
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
void * zalloc(size_t size)
Allocate cleared memory.
Definition: malloc.c:662
int rc
Most relevant status code.
Definition: validator.c:108
size_t strlen(const char *src)
Get length of string.
Definition: string.c:244
unsigned int uint32_t
Definition: stdint.h:12
struct x509_certificate * cert
Current certificate (for progress reporting)
Definition: validator.c:127
const char * x509_name(struct x509_certificate *cert)
Get X.509 certificate display name.
Definition: x509.c:147
int snprintf(char *buf, size_t size, const char *fmt,...)
Write a formatted string to a buffer.
Definition: vsprintf.c:383
struct x509_link * link
Current link within certificate chain.
Definition: validator.c:129
int xfer_open_uri_string(struct interface *intf, const char *uri_string)
Open URI string.
Definition: open.c:116
#define NULL
NULL pointer (VOID *)
Definition: Base.h:322
An ASN.1 object cursor.
Definition: asn1.h:21
size_t base64_encode(const void *raw, size_t raw_len, char *data, size_t len)
Base64-encode data.
Definition: base64.c:52
static const struct validator_action validator_crosscert
Cross-signing certificate download validator action.
Definition: validator.c:349
static const char * validator_name(struct validator *validator)
Get validator name (for debug messages)
Definition: validator.c:138

References validator::action, base64_encode(), base64_encoded_len(), validator::cert, crc32_le(), crosscert_default, asn1_cursor::data, DBGC, EINVAL, ENOMEM, fetch_string_setting_copy(), free, intf_restart(), x509_certificate::issuer, asn1_cursor::len, len, link, validator::link, NULL, x509_issuer::raw, validator::rc, rc, snprintf(), strerror(), strlen(), validator_crosscert, validator_name(), x509_name(), validator::xfer, xfer_open_uri_string(), and zalloc().

Referenced by validator_step().

◆ validator_ocsp_validate()

static void validator_ocsp_validate ( struct validator validator,
int  rc 
)
static

Validate OCSP response.

Parameters
validatorCertificate validator
rcCompletion status code

Definition at line 446 of file validator.c.

446  {
447  const void *data = validator->buffer.data;
448  size_t len = validator->buffer.len;
449  time_t now;
450 
451  /* Check for errors */
452  if ( rc != 0 ) {
453  DBGC ( validator, "VALIDATOR %p \"%s\" could not fetch OCSP "
454  "response: %s\n", validator,
455  validator_name ( validator ), strerror ( rc ) );
456  goto err_status;
457  }
458 
459  /* Record OCSP response */
460  if ( ( rc = ocsp_response ( validator->ocsp, data, len ) ) != 0 ) {
461  DBGC ( validator, "VALIDATOR %p \"%s\" could not record OCSP "
462  "response: %s\n", validator,
463  validator_name ( validator ), strerror ( rc ) );
464  goto err_response;
465  }
466 
467  /* Validate OCSP response */
468  now = time ( NULL );
469  if ( ( rc = ocsp_validate ( validator->ocsp, now ) ) != 0 ) {
470  DBGC ( validator, "VALIDATOR %p \"%s\" could not validate "
471  "OCSP response: %s\n", validator,
472  validator_name ( validator ), strerror ( rc ) );
473  goto err_validate;
474  }
475 
476  /* Success */
477  DBGC ( validator, "VALIDATOR %p \"%s\" checked ",
479  DBGC ( validator, "\"%s\" via OCSP\n", x509_name ( validator->cert ) );
480 
481  err_validate:
482  err_response:
483  err_status:
484  ocsp_put ( validator->ocsp );
485  validator->ocsp = NULL;
486  validator->rc = rc;
487 }
void * data
Data.
Definition: xferbuf.h:21
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
#define DBGC(...)
Definition: compiler.h:505
int ocsp_validate(struct ocsp_check *ocsp, time_t time)
Validate OCSP response.
Definition: ocsp.c:880
A certificate validator.
Definition: validator.c:65
size_t len
Size of data.
Definition: xferbuf.h:23
struct ocsp_check * ocsp
OCSP check.
Definition: validator.c:115
ring len
Length.
Definition: dwmac.h:231
struct xfer_buffer buffer
Data buffer.
Definition: validator.c:117
char * strerror(int errno)
Retrieve string representation of error number.
Definition: strerror.c:79
static void ocsp_put(struct ocsp_check *ocsp)
Drop reference to OCSP check.
Definition: ocsp.h:119
int rc
Most relevant status code.
Definition: validator.c:108
An OCSP response.
Definition: ocsp.h:66
struct x509_certificate * cert
Current certificate (for progress reporting)
Definition: validator.c:127
const char * x509_name(struct x509_certificate *cert)
Get X.509 certificate display name.
Definition: x509.c:147
uint8_t data[48]
Additional event data.
Definition: ena.h:22
int64_t time_t
Seconds since the Epoch.
Definition: time.h:19
#define NULL
NULL pointer (VOID *)
Definition: Base.h:322
static const char * validator_name(struct validator *validator)
Get validator name (for debug messages)
Definition: validator.c:138

References validator::buffer, validator::cert, xfer_buffer::data, data, DBGC, xfer_buffer::len, len, NULL, validator::ocsp, ocsp_put(), ocsp_validate(), validator::rc, rc, strerror(), validator_name(), and x509_name().

◆ validator_start_ocsp()

static int validator_start_ocsp ( struct validator validator,
struct x509_certificate cert,
struct x509_certificate issuer 
)
static

Start OCSP check.

Parameters
validatorCertificate validator
certCertificate to check
issuerIssuing certificate
Return values
rcReturn status code

Definition at line 503 of file validator.c.

505  {
506  const char *uri_string;
507  int rc;
508 
509  /* Create OCSP check */
510  assert ( validator->ocsp == NULL );
511  if ( ( rc = ocsp_check ( cert, issuer, &validator->ocsp ) ) != 0 ) {
512  DBGC ( validator, "VALIDATOR %p \"%s\" could not create OCSP "
513  "check: %s\n", validator, validator_name ( validator ),
514  strerror ( rc ) );
515  goto err_check;
516  }
517 
518  /* Set completion handler */
520  validator->cert = cert;
521 
522  /* Open URI */
523  uri_string = validator->ocsp->uri_string;
524  DBGC ( validator, "VALIDATOR %p \"%s\" checking ",
526  DBGC ( validator, "\"%s\" via %s\n",
527  x509_name ( cert ), uri_string );
528  if ( ( rc = xfer_open_uri_string ( &validator->xfer,
529  uri_string ) ) != 0 ) {
530  DBGC ( validator, "VALIDATOR %p \"%s\" could not open %s: "
531  "%s\n", validator, validator_name ( validator ),
532  uri_string, strerror ( rc ) );
533  goto err_open;
534  }
535 
536  return 0;
537 
538  intf_restart ( &validator->xfer, rc );
539  err_open:
540  ocsp_put ( validator->ocsp );
541  validator->ocsp = NULL;
542  err_check:
543  validator->rc = rc;
544  return rc;
545 }
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
void intf_restart(struct interface *intf, int rc)
Shut down and restart an object interface.
Definition: interface.c:344
#define DBGC(...)
Definition: compiler.h:505
A certificate validator.
Definition: validator.c:65
struct interface xfer
Data transfer interface.
Definition: validator.c:71
assert((readw(&hdr->flags) &(GTF_reading|GTF_writing))==0)
struct ocsp_check * ocsp
OCSP check.
Definition: validator.c:115
const struct validator_action * action
Current action.
Definition: validator.c:120
char * strerror(int errno)
Retrieve string representation of error number.
Definition: strerror.c:79
static void ocsp_put(struct ocsp_check *ocsp)
Drop reference to OCSP check.
Definition: ocsp.h:119
int rc
Most relevant status code.
Definition: validator.c:108
struct x509_certificate * cert
Current certificate (for progress reporting)
Definition: validator.c:127
static const struct validator_action validator_ocsp
OCSP validator action.
Definition: validator.c:490
const char * x509_name(struct x509_certificate *cert)
Get X.509 certificate display name.
Definition: x509.c:147
int xfer_open_uri_string(struct interface *intf, const char *uri_string)
Open URI string.
Definition: open.c:116
An OCSP check.
Definition: ocsp.h:86
#define NULL
NULL pointer (VOID *)
Definition: Base.h:322
char * uri_string
URI string.
Definition: ocsp.h:94
static const char * validator_name(struct validator *validator)
Get validator name (for debug messages)
Definition: validator.c:138

References validator::action, assert(), validator::cert, DBGC, intf_restart(), NULL, validator::ocsp, ocsp_put(), validator::rc, rc, strerror(), ocsp_check::uri_string, validator_name(), validator_ocsp, x509_name(), validator::xfer, and xfer_open_uri_string().

Referenced by validator_step().

◆ validator_xfer_close()

static void validator_xfer_close ( struct validator validator,
int  rc 
)
static

Close data transfer interface.

Parameters
validatorCertificate validator
rcReason for close

Definition at line 559 of file validator.c.

559  {
560 
561  /* Close data transfer interface */
562  intf_restart ( &validator->xfer, rc );
563  DBGC2 ( validator, "VALIDATOR %p \"%s\" transfer complete\n",
565 
566  /* Process completed download */
567  assert ( validator->action != NULL );
569 
570  /* Free downloaded data */
572 
573  /* Resume validation process */
575 }
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
void intf_restart(struct interface *intf, int rc)
Shut down and restart an object interface.
Definition: interface.c:344
struct process process
Process.
Definition: validator.c:74
void(* done)(struct validator *validator, int rc)
Action to take upon completed transfer.
Definition: validator.c:61
void xferbuf_free(struct xfer_buffer *xferbuf)
Free data transfer buffer.
Definition: xferbuf.c:74
A certificate validator.
Definition: validator.c:65
struct interface xfer
Data transfer interface.
Definition: validator.c:71
assert((readw(&hdr->flags) &(GTF_reading|GTF_writing))==0)
const struct validator_action * action
Current action.
Definition: validator.c:120
void process_add(struct process *process)
Add process to process list.
Definition: process.c:60
struct xfer_buffer buffer
Data buffer.
Definition: validator.c:117
#define DBGC2(...)
Definition: compiler.h:522
#define NULL
NULL pointer (VOID *)
Definition: Base.h:322
static const char * validator_name(struct validator *validator)
Get validator name (for debug messages)
Definition: validator.c:138

References validator::action, assert(), validator::buffer, DBGC2, validator_action::done, intf_restart(), NULL, validator::process, process_add(), rc, validator_name(), validator::xfer, and xferbuf_free().

Referenced by validator_xfer_deliver().

◆ validator_xfer_deliver()

static int validator_xfer_deliver ( struct validator validator,
struct io_buffer iobuf,
struct xfer_metadata meta 
)
static

Receive data.

Parameters
validatorCertificate validator
iobufI/O buffer
metaData transfer metadata
Return values
rcReturn status code

Definition at line 585 of file validator.c.

587  {
588  int rc;
589 
590  /* Add data to buffer */
591  if ( ( rc = xferbuf_deliver ( &validator->buffer, iob_disown ( iobuf ),
592  meta ) ) != 0 ) {
593  DBGC ( validator, "VALIDATOR %p \"%s\" could not receive "
594  "data: %s\n", validator, validator_name ( validator ),
595  strerror ( rc ) );
597  return rc;
598  }
599 
600  return 0;
601 }
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
int xferbuf_deliver(struct xfer_buffer *xferbuf, struct io_buffer *iobuf, struct xfer_metadata *meta)
Add received data to data transfer buffer.
Definition: xferbuf.c:175
#define DBGC(...)
Definition: compiler.h:505
A certificate validator.
Definition: validator.c:65
#define iob_disown(iobuf)
Disown an I/O buffer.
Definition: iobuf.h:217
struct xfer_buffer buffer
Data buffer.
Definition: validator.c:117
char * strerror(int errno)
Retrieve string representation of error number.
Definition: strerror.c:79
static void validator_xfer_close(struct validator *validator, int rc)
Close data transfer interface.
Definition: validator.c:559
uint8_t meta
Metadata flags.
Definition: ena.h:14
static const char * validator_name(struct validator *validator)
Get validator name (for debug messages)
Definition: validator.c:138

References validator::buffer, DBGC, iob_disown, meta, rc, strerror(), validator_name(), validator_xfer_close(), and xferbuf_deliver().

◆ validator_step()

static void validator_step ( struct validator validator)
static

Certificate validation process.

Parameters
validatorCertificate validator

Definition at line 624 of file validator.c.

624  {
625  struct x509_chain *chain = validator->chain;
626  struct x509_link *link;
627  struct x509_link *prev;
628  struct x509_certificate *cert;
629  time_t now;
630  int rc;
631 
632  /* Try validating chain. Try even if the chain is incomplete,
633  * since certificates may already have been validated
634  * previously.
635  */
636  now = time ( NULL );
637  if ( ( rc = x509_validate_chain ( chain, now, NULL,
638  validator->root ) ) == 0 ) {
639  DBGC ( validator, "VALIDATOR %p \"%s\" validated\n",
642  return;
643  }
644  DBGC ( validator, "VALIDATOR %p \"%s\" not yet valid: %s\n",
646 
647  /* Record as the most relevant error, if no more relevant
648  * error has already been recorded.
649  */
650  if ( validator->rc == 0 )
651  validator->rc = rc;
652 
653  /* Find the first valid link in the chain, if any
654  *
655  * There is no point in attempting OCSP or cross-signed
656  * certificate downloads for certificates after the first
657  * valid link in the chain, since they cannot make a
658  * difference to the overall validation of the chain.
659  */
660  prev = NULL;
661  list_for_each_entry ( link, &chain->links, list ) {
662 
663  /* Dump link information (for debugging) */
664  DBGC ( validator, "VALIDATOR %p \"%s\" has link ",
666  DBGC ( validator, "\"%s\"%s%s%s%s%s\n",
667  x509_name ( link->cert ),
668  ( ocsp_required ( link->cert ) ? " [NEEDOCSP]" : "" ),
669  ( ( link->flags & X509_LINK_FL_OCSPED ) ?
670  " [OCSPED]" : "" ),
671  ( ( link->flags & X509_LINK_FL_CROSSED ) ?
672  " [CROSSED]" : "" ),
673  ( x509_is_self_signed ( link->cert ) ? " [SELF]" : "" ),
674  ( x509_is_valid ( link->cert, validator->root ) ?
675  " [VALID]" : "" ) );
676 
677  /* Stop at first valid link */
678  if ( x509_is_valid ( link->cert, validator->root ) )
679  break;
680  prev = link;
681  }
682 
683  /* If this link is the issuer for a certificate that is
684  * pending an OCSP check attempt, then start OCSP to validate
685  * that certificate.
686  *
687  * If OCSP is not required for the issued certificate, or has
688  * already been attempted, or if we were unable to start OCSP
689  * for any reason, then proceed to attempting a cross-signed
690  * certificate download (which may end up replacing this
691  * issuer anyway).
692  */
693  if ( ( ! list_is_head_entry ( link, &chain->links, list ) ) &&
694  ( ! ( link->flags & X509_LINK_FL_OCSPED ) ) &&
695  ( prev != NULL ) && ocsp_required ( prev->cert ) ) {
696 
697  /* Mark OCSP as attempted with this issuer */
698  link->flags |= X509_LINK_FL_OCSPED;
699 
700  /* Start OCSP */
701  if ( ( rc = validator_start_ocsp ( validator, prev->cert,
702  link->cert ) ) == 0 ) {
703  /* Sleep until OCSP is complete */
704  return;
705  }
706  }
707 
708  /* Work back up the chain (starting from the already
709  * identified first valid link, if any) to find a not-yet
710  * valid certificate for which we could attempt to download a
711  * cross-signed certificate chain.
712  */
713  list_for_each_entry_continue_reverse ( link, &chain->links, list ) {
714  cert = link->cert;
715 
716  /* Sanity check */
717  assert ( ! x509_is_valid ( cert, validator->root ) );
718 
719  /* Skip self-signed certificates (cannot be cross-signed) */
720  if ( x509_is_self_signed ( cert ) )
721  continue;
722 
723  /* Skip previously attempted cross-signed downloads */
724  if ( link->flags & X509_LINK_FL_CROSSED )
725  continue;
726 
727  /* Mark cross-signed certificate download as attempted */
728  link->flags |= X509_LINK_FL_CROSSED;
729 
730  /* Start cross-signed certificate download */
732  link ) ) == 0 ) {
733  /* Sleep until download is complete */
734  return;
735  }
736  }
737 
738  /* Nothing more to try: fail the validation */
740 }
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
struct list_head links
List of links.
Definition: x509.h:205
#define DBGC(...)
Definition: compiler.h:505
struct x509_chain * chain
X.509 certificate chain.
Definition: validator.c:113
static int validator_start_download(struct validator *validator, struct x509_link *link)
Start download of cross-signing certificate.
Definition: validator.c:361
int x509_is_valid(struct x509_certificate *cert, struct x509_root *root)
Check if X.509 certificate is valid.
Definition: x509.c:1313
static int ocsp_required(struct x509_certificate *cert)
Check if X.509 certificate requires an OCSP check.
Definition: ocsp.h:129
A certificate validator.
Definition: validator.c:65
static int x509_is_self_signed(struct x509_certificate *cert)
Check if X.509 certificate is self-signed.
Definition: x509.h:414
An X.509 certificate chain.
Definition: x509.h:201
static int validator_start_ocsp(struct validator *validator, struct x509_certificate *cert, struct x509_certificate *issuer)
Start OCSP check.
Definition: validator.c:503
assert((readw(&hdr->flags) &(GTF_reading|GTF_writing))==0)
#define list_for_each_entry(pos, head, member)
Iterate over entries in a list.
Definition: list.h:432
u32 link
Link to next descriptor.
Definition: ar9003_mac.h:25
int x509_validate_chain(struct x509_chain *chain, time_t time, struct x509_chain *store, struct x509_root *root)
Validate X.509 certificate chain.
Definition: x509.c:1908
char * strerror(int errno)
Retrieve string representation of error number.
Definition: strerror.c:79
An X.509 certificate.
Definition: x509.h:216
OCSP has been attempted.
Definition: x509.h:197
int rc
Most relevant status code.
Definition: validator.c:108
#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:487
const char * x509_name(struct x509_certificate *cert)
Get X.509 certificate display name.
Definition: x509.c:147
struct x509_root * root
Root of trust (or NULL to use default)
Definition: validator.c:111
static void validator_finished(struct validator *validator, int rc)
Mark certificate validation as finished.
Definition: validator.c:170
int64_t time_t
Seconds since the Epoch.
Definition: time.h:19
#define NULL
NULL pointer (VOID *)
Definition: Base.h:322
#define list_is_head_entry(entry, head, member)
Test if entry is the list head.
Definition: list.h:410
Cross-signed certificate download has been attempted.
Definition: x509.h:188
static const char * validator_name(struct validator *validator)
Get validator name (for debug messages)
Definition: validator.c:138

References assert(), x509_link::cert, validator::chain, DBGC, link, x509_chain::links, list_for_each_entry, list_for_each_entry_continue_reverse, list_is_head_entry, NULL, ocsp_required(), validator::rc, rc, validator::root, strerror(), validator_finished(), validator_name(), validator_start_download(), validator_start_ocsp(), x509_is_self_signed(), x509_is_valid(), X509_LINK_FL_CROSSED, X509_LINK_FL_OCSPED, x509_name(), and x509_validate_chain().

◆ create_validator()

int create_validator ( struct interface job,
struct x509_chain chain,
struct x509_root root 
)

Instantiate a certificate validator.

Parameters
jobJob control interface
chainX.509 certificate chain
rootRoot of trust, or NULL to use default
Return values
rcReturn status code

Definition at line 760 of file validator.c.

761  {
762  struct validator *validator;
763  int rc;
764 
765  /* Sanity check */
766  if ( ! chain ) {
767  rc = -EINVAL;
768  goto err_sanity;
769  }
770 
771  /* Allocate and initialise structure */
772  validator = zalloc ( sizeof ( *validator ) );
773  if ( ! validator ) {
774  rc = -ENOMEM;
775  goto err_alloc;
776  }
779  &validator->refcnt );
781  &validator->refcnt );
783  &validator->refcnt );
787 
788  /* Attach parent interface, mortalise self, and return */
790  ref_put ( &validator->refcnt );
791  DBGC2 ( validator, "VALIDATOR %p \"%s\" validating X509 chain %p\n",
793  return 0;
794 
796  ref_put ( &validator->refcnt );
797  err_alloc:
798  err_sanity:
799  return rc;
800 }
#define EINVAL
Invalid argument.
Definition: errno.h:429
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
static struct x509_chain * x509_chain_get(struct x509_chain *chain)
Get reference to X.509 certificate chain.
Definition: x509.h:289
struct process process
Process.
Definition: validator.c:74
struct stp_switch root
Root switch.
Definition: stp.h:27
struct refcnt refcnt
Reference count.
Definition: validator.c:67
#define ref_init(refcnt, free)
Initialise a reference counter.
Definition: refcnt.h:65
static void process_init(struct process *process, struct process_descriptor *desc, struct refcnt *refcnt)
Initialise process and add to process list.
Definition: process.h:162
void intf_plug_plug(struct interface *a, struct interface *b)
Plug two object interfaces together.
Definition: interface.c:108
struct x509_chain * chain
X.509 certificate chain.
Definition: validator.c:113
A certificate validator.
Definition: validator.c:65
struct interface xfer
Data transfer interface.
Definition: validator.c:71
#define ENOMEM
Not enough space.
Definition: errno.h:535
static struct interface_descriptor validator_xfer_desc
Certificate validator data transfer interface descriptor.
Definition: validator.c:610
static struct process_descriptor validator_process_desc
Certificate validator process descriptor.
Definition: validator.c:743
static void xferbuf_malloc_init(struct xfer_buffer *xferbuf)
Initialise malloc()-based data transfer buffer.
Definition: xferbuf.h:54
static struct x509_root * x509_root_get(struct x509_root *root)
Get reference to X.509 root certificate list.
Definition: x509.h:393
static void validator_free(struct refcnt *refcnt)
Free certificate validator.
Definition: validator.c:151
struct xfer_buffer buffer
Data buffer.
Definition: validator.c:117
void * zalloc(size_t size)
Allocate cleared memory.
Definition: malloc.c:662
static struct interface_descriptor validator_job_desc
Certificate validator job control interface descriptor.
Definition: validator.c:213
#define DBGC2(...)
Definition: compiler.h:522
struct x509_root * root
Root of trust (or NULL to use default)
Definition: validator.c:111
struct interface job
Job control interface.
Definition: validator.c:69
static void validator_finished(struct validator *validator, int rc)
Mark certificate validation as finished.
Definition: validator.c:170
static void intf_init(struct interface *intf, struct interface_descriptor *desc, struct refcnt *refcnt)
Initialise an object interface.
Definition: interface.h:204
#define ref_put(refcnt)
Drop reference to object.
Definition: refcnt.h:107
static const char * validator_name(struct validator *validator)
Get validator name (for debug messages)
Definition: validator.c:138

References validator::buffer, validator::chain, DBGC2, EINVAL, ENOMEM, intf_init(), intf_plug_plug(), validator::job, validator::process, process_init(), rc, ref_init, ref_put, validator::refcnt, root, validator::root, validator_finished(), validator_free(), validator_job_desc, validator_name(), validator_process_desc, validator_xfer_desc, x509_chain_get(), x509_root_get(), validator::xfer, xferbuf_malloc_init(), and zalloc().

Referenced by imgverify(), and tls_new_server_hello_done().

Variable Documentation

◆ validator_job_operations

struct interface_operation validator_job_operations[]
static
Initial value:
= {
}
void intf_close(struct interface *intf, int rc)
Close an object interface.
Definition: interface.c:250
A certificate validator.
Definition: validator.c:65
static int validator_progress(struct validator *validator, struct job_progress *progress)
Report job progress.
Definition: validator.c:193
Job progress.
Definition: job.h:16
#define INTF_OP(op_type, object_type, op_func)
Define an object interface operation.
Definition: interface.h:33
static void validator_finished(struct validator *validator, int rc)
Mark certificate validation as finished.
Definition: validator.c:170

Certificate validator job control interface operations.

Definition at line 207 of file validator.c.

◆ validator_job_desc

struct interface_descriptor validator_job_desc
static
Initial value:
=
static struct interface_operation validator_job_operations[]
Certificate validator job control interface operations.
Definition: validator.c:207
A certificate validator.
Definition: validator.c:65
#define INTF_DESC(object_type, intf, operations)
Define an object interface descriptor.
Definition: interface.h:81
struct interface job
Job control interface.
Definition: validator.c:69

Certificate validator job control interface descriptor.

Definition at line 213 of file validator.c.

Referenced by create_validator().

◆ crosscert_default

const char crosscert_default[] = CROSSCERT
static

Default cross-signed certificate source.

Definition at line 231 of file validator.c.

Referenced by validator_start_download().

◆ validator_crosscert

const struct validator_action validator_crosscert
static
Initial value:
= {
.name = "XCRT",
}
static void validator_append(struct validator *validator, int rc)
Append cross-signing certificates to certificate chain.
Definition: validator.c:240

Cross-signing certificate download validator action.

Definition at line 349 of file validator.c.

Referenced by validator_start_download().

◆ validator_ocsp

const struct validator_action validator_ocsp
static
Initial value:
= {
.name = "OCSP",
}
static void validator_ocsp_validate(struct validator *validator, int rc)
Validate OCSP response.
Definition: validator.c:446

OCSP validator action.

Definition at line 490 of file validator.c.

Referenced by validator_start_ocsp().

◆ validator_xfer_operations

struct interface_operation validator_xfer_operations[]
static
Initial value:
= {
}
void intf_close(struct interface *intf, int rc)
Close an object interface.
Definition: interface.c:250
A certificate validator.
Definition: validator.c:65
static int validator_xfer_deliver(struct validator *validator, struct io_buffer *iobuf, struct xfer_metadata *meta)
Receive data.
Definition: validator.c:585
#define INTF_OP(op_type, object_type, op_func)
Define an object interface operation.
Definition: interface.h:33
int xfer_deliver(struct interface *intf, struct io_buffer *iobuf, struct xfer_metadata *meta)
Deliver datagram.
Definition: xfer.c:195
static void validator_xfer_close(struct validator *validator, int rc)
Close data transfer interface.
Definition: validator.c:559

Certificate validator data transfer interface operations.

Definition at line 604 of file validator.c.

◆ validator_xfer_desc

struct interface_descriptor validator_xfer_desc
static
Initial value:
=
static struct interface_operation validator_xfer_operations[]
Certificate validator data transfer interface operations.
Definition: validator.c:604
A certificate validator.
Definition: validator.c:65
struct interface xfer
Data transfer interface.
Definition: validator.c:71
#define INTF_DESC(object_type, intf, operations)
Define an object interface descriptor.
Definition: interface.h:81

Certificate validator data transfer interface descriptor.

Definition at line 610 of file validator.c.

Referenced by create_validator().

◆ validator_process_desc

struct process_descriptor validator_process_desc
static
Initial value:
=
A process.
Definition: process.h:18
static void validator_step(struct validator *validator)
Certificate validation process.
Definition: validator.c:624
#define PROC_DESC_ONCE(object_type, process, _step)
Define a process descriptor for a process that runs only once.
Definition: process.h:98
A certificate validator.
Definition: validator.c:65

Certificate validator process descriptor.

Definition at line 743 of file validator.c.

Referenced by create_validator().