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

◆ 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 137 of file validator.c.

137  {
138 
139  /* Use name of first certificate in chain */
140  return x509_name ( x509_first ( validator->chain ) );
141 }
struct x509_chain * chain
X.509 certificate chain.
Definition: validator.c:112
A certificate validator.
Definition: validator.c:64
const char * x509_name(struct x509_certificate *cert)
Get X.509 certificate display name.
Definition: x509.c:145
static struct x509_certificate * x509_first(struct x509_chain *chain)
Get first certificate in X.509 certificate chain.
Definition: x509.h:302

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 148 of file validator.c.

148  {
149  struct validator *validator =
150  container_of ( refcnt, struct validator, refcnt );
151 
152  DBGC2 ( validator, "VALIDATOR %p \"%s\" freed\n",
156  ocsp_put ( validator->ocsp );
158  free ( validator );
159 }
static void x509_chain_put(struct x509_chain *chain)
Drop reference to X.509 certificate chain.
Definition: x509.h:291
static void x509_root_put(struct x509_root *root)
Drop reference to X.509 root certificate list.
Definition: x509.h:395
struct x509_chain * chain
X.509 certificate chain.
Definition: validator.c:112
void xferbuf_free(struct xfer_buffer *xferbuf)
Free data transfer buffer.
Definition: xferbuf.c:58
A reference counter.
Definition: refcnt.h:26
A certificate validator.
Definition: validator.c:64
#define container_of(ptr, type, field)
Get containing structure.
Definition: stddef.h:35
struct ocsp_check * ocsp
OCSP check.
Definition: validator.c:114
struct xfer_buffer buffer
Data buffer.
Definition: validator.c:116
static void(* free)(struct refcnt *refcnt))
Definition: refcnt.h:54
static void ocsp_put(struct ocsp_check *ocsp)
Drop reference to OCSP check.
Definition: ocsp.h:118
#define DBGC2(...)
Definition: compiler.h:522
struct x509_root * root
Root of trust (or NULL to use default)
Definition: validator.c:110
static const char * validator_name(struct validator *validator)
Get validator name (for debug messages)
Definition: validator.c:137

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 167 of file validator.c.

167  {
168 
169  /* Remove process */
171 
172  /* Close all interfaces */
174  intf_shutdown ( &validator->job, rc );
175 }
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:278
struct process process
Process.
Definition: validator.c:73
void process_del(struct process *process)
Remove process from process list.
Definition: process.c:79
A certificate validator.
Definition: validator.c:64
struct interface xfer
Data transfer interface.
Definition: validator.c:70
struct interface job
Job control interface.
Definition: validator.c:68

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 190 of file validator.c.

191  {
192 
193  /* Report current action, if applicable */
194  if ( validator->action ) {
195  snprintf ( progress->message, sizeof ( progress->message ),
196  "%s %s", validator->action->name,
197  x509_name ( validator->cert ) );
198  }
199 
200  return 0;
201 }
A certificate validator.
Definition: validator.c:64
const struct validator_action * action
Current action.
Definition: validator.c:119
char message[32]
Message (optional)
Definition: job.h:32
const char * name
Name.
Definition: validator.c:58
struct x509_certificate * cert
Current certificate (for progress reporting)
Definition: validator.c:126
const char * x509_name(struct x509_certificate *cert)
Get X.509 certificate display name.
Definition: x509.c:145
int snprintf(char *buf, size_t size, const char *fmt,...)
Write a formatted string to a buffer.
Definition: vsprintf.c:382

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 237 of file validator.c.

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

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 358 of file validator.c.

359  {
360  struct x509_certificate *cert = link->cert;
361  const struct asn1_cursor *issuer = &cert->issuer.raw;
362  const char *crosscert;
363  char *crosscert_copy;
364  char *uri_string;
365  size_t uri_string_len;
366  uint32_t crc;
367  int len;
368  int rc;
369 
370  /* Determine cross-signed certificate source */
371  fetch_string_setting_copy ( NULL, &crosscert_setting, &crosscert_copy );
372  crosscert = ( crosscert_copy ? crosscert_copy : crosscert_default );
373  if ( ! crosscert[0] ) {
374  rc = -EINVAL;
375  goto err_check_uri_string;
376  }
377 
378  /* Allocate URI string */
379  uri_string_len = ( strlen ( crosscert ) + 22 /* "/%08x.der?subject=" */
380  + base64_encoded_len ( issuer->len ) + 1 /* NUL */ );
381  uri_string = zalloc ( uri_string_len );
382  if ( ! uri_string ) {
383  rc = -ENOMEM;
384  goto err_alloc_uri_string;
385  }
386 
387  /* Generate CRC32 */
388  crc = crc32_le ( 0xffffffffUL, issuer->data, issuer->len );
389 
390  /* Generate URI string */
391  len = snprintf ( uri_string, uri_string_len, "%s/%08x.der?subject=",
392  crosscert, crc );
393  base64_encode ( issuer->data, issuer->len, ( uri_string + len ),
394  ( uri_string_len - len ) );
395  DBGC ( validator, "VALIDATOR %p \"%s\" downloading ",
397  DBGC ( validator, "\"%s\" cross-signature from %s\n",
398  x509_name ( cert ), uri_string );
399 
400  /* Set completion handler */
402  validator->cert = cert;
403  validator->link = link;
404 
405  /* Open URI */
406  if ( ( rc = xfer_open_uri_string ( &validator->xfer,
407  uri_string ) ) != 0 ) {
408  DBGC ( validator, "VALIDATOR %p \"%s\" could not open %s: "
409  "%s\n", validator, validator_name ( validator ),
410  uri_string, strerror ( rc ) );
411  goto err_open_uri_string;
412  }
413 
414  /* Free temporary allocations */
415  free ( uri_string );
416  free ( crosscert_copy );
417 
418  /* Success */
419  return 0;
420 
421  intf_restart ( &validator->xfer, rc );
422  err_open_uri_string:
423  free ( uri_string );
424  err_alloc_uri_string:
425  err_check_uri_string:
426  free ( crosscert_copy );
427  validator->rc = rc;
428  return rc;
429 }
#define EINVAL
Invalid argument.
Definition: errno.h:428
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
struct asn1_cursor raw
Raw issuer.
Definition: x509.h:30
void intf_restart(struct interface *intf, int rc)
Shut down and restart an object interface.
Definition: interface.c:343
struct x509_issuer issuer
Issuer.
Definition: x509.h:232
const void * data
Start of data.
Definition: asn1.h:22
#define DBGC(...)
Definition: compiler.h:505
static const char crosscert_default[]
Default cross-signed certificate source.
Definition: validator.c:228
size_t len
Length of data.
Definition: asn1.h:24
A certificate validator.
Definition: validator.c:64
u32 crc32_le(u32 seed, const void *data, size_t len)
Calculate 32-bit little-endian CRC checksum.
Definition: crc32.c:39
struct interface xfer
Data transfer interface.
Definition: validator.c:70
static size_t base64_encoded_len(size_t raw_len)
Calculate length of base64-encoded data.
Definition: base64.h:21
#define ENOMEM
Not enough space.
Definition: errno.h:534
const struct validator_action * action
Current action.
Definition: validator.c:119
u32 link
Link to next descriptor.
Definition: ar9003_mac.h:68
int fetch_string_setting_copy(struct settings *settings, const struct setting *setting, char **data)
Fetch value of string setting.
Definition: settings.c:873
char * strerror(int errno)
Retrieve string representation of error number.
Definition: strerror.c:78
An X.509 certificate.
Definition: x509.h:207
static void(* free)(struct refcnt *refcnt))
Definition: refcnt.h:54
void * zalloc(size_t size)
Allocate cleared memory.
Definition: malloc.c:624
int rc
Most relevant status code.
Definition: validator.c:107
size_t strlen(const char *src)
Get length of string.
Definition: string.c:243
unsigned int uint32_t
Definition: stdint.h:12
struct x509_certificate * cert
Current certificate (for progress reporting)
Definition: validator.c:126
const char * x509_name(struct x509_certificate *cert)
Get X.509 certificate display name.
Definition: x509.c:145
uint32_t len
Length.
Definition: ena.h:14
int snprintf(char *buf, size_t size, const char *fmt,...)
Write a formatted string to a buffer.
Definition: vsprintf.c:382
struct x509_link * link
Current link within certificate chain.
Definition: validator.c:128
int xfer_open_uri_string(struct interface *intf, const char *uri_string)
Open URI string.
Definition: open.c:115
#define NULL
NULL pointer (VOID *)
Definition: Base.h:321
An ASN.1 object cursor.
Definition: asn1.h:20
size_t base64_encode(const void *raw, size_t raw_len, char *data, size_t len)
Base64-encode data.
Definition: base64.c:51
static const struct validator_action validator_crosscert
Cross-signing certificate download validator action.
Definition: validator.c:346
static const char * validator_name(struct validator *validator)
Get validator name (for debug messages)
Definition: validator.c:137

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, len, asn1_cursor::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 443 of file validator.c.

443  {
444  const void *data = validator->buffer.data;
445  size_t len = validator->buffer.len;
446  time_t now;
447 
448  /* Check for errors */
449  if ( rc != 0 ) {
450  DBGC ( validator, "VALIDATOR %p \"%s\" could not fetch OCSP "
451  "response: %s\n", validator,
452  validator_name ( validator ), strerror ( rc ) );
453  goto err_status;
454  }
455 
456  /* Record OCSP response */
457  if ( ( rc = ocsp_response ( validator->ocsp, data, len ) ) != 0 ) {
458  DBGC ( validator, "VALIDATOR %p \"%s\" could not record OCSP "
459  "response: %s\n", validator,
460  validator_name ( validator ), strerror ( rc ) );
461  goto err_response;
462  }
463 
464  /* Validate OCSP response */
465  now = time ( NULL );
466  if ( ( rc = ocsp_validate ( validator->ocsp, now ) ) != 0 ) {
467  DBGC ( validator, "VALIDATOR %p \"%s\" could not validate "
468  "OCSP response: %s\n", validator,
469  validator_name ( validator ), strerror ( rc ) );
470  goto err_validate;
471  }
472 
473  /* Success */
474  DBGC ( validator, "VALIDATOR %p \"%s\" checked ",
476  DBGC ( validator, "\"%s\" via OCSP\n", x509_name ( validator->cert ) );
477 
478  err_validate:
479  err_response:
480  err_status:
481  ocsp_put ( validator->ocsp );
482  validator->ocsp = NULL;
483  validator->rc = rc;
484 }
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:892
A certificate validator.
Definition: validator.c:64
size_t len
Size of data.
Definition: xferbuf.h:23
struct ocsp_check * ocsp
OCSP check.
Definition: validator.c:114
struct xfer_buffer buffer
Data buffer.
Definition: validator.c:116
char * strerror(int errno)
Retrieve string representation of error number.
Definition: strerror.c:78
static void ocsp_put(struct ocsp_check *ocsp)
Drop reference to OCSP check.
Definition: ocsp.h:118
int rc
Most relevant status code.
Definition: validator.c:107
An OCSP response.
Definition: ocsp.h:65
struct x509_certificate * cert
Current certificate (for progress reporting)
Definition: validator.c:126
const char * x509_name(struct x509_certificate *cert)
Get X.509 certificate display name.
Definition: x509.c:145
uint32_t len
Length.
Definition: ena.h:14
uint8_t data[48]
Additional event data.
Definition: ena.h:22
int64_t time_t
Seconds since the Epoch.
Definition: time.h:18
uint64_t time
Current time.
Definition: ntlm.h:20
#define NULL
NULL pointer (VOID *)
Definition: Base.h:321
static const char * validator_name(struct validator *validator)
Get validator name (for debug messages)
Definition: validator.c:137

References validator::buffer, validator::cert, xfer_buffer::data, data, DBGC, len, xfer_buffer::len, NULL, validator::ocsp, ocsp_put(), ocsp_validate(), validator::rc, rc, strerror(), time, 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 500 of file validator.c.

502  {
503  const char *uri_string;
504  int rc;
505 
506  /* Create OCSP check */
507  assert ( validator->ocsp == NULL );
508  if ( ( rc = ocsp_check ( cert, issuer, &validator->ocsp ) ) != 0 ) {
509  DBGC ( validator, "VALIDATOR %p \"%s\" could not create OCSP "
510  "check: %s\n", validator, validator_name ( validator ),
511  strerror ( rc ) );
512  goto err_check;
513  }
514 
515  /* Set completion handler */
517  validator->cert = cert;
518 
519  /* Open URI */
520  uri_string = validator->ocsp->uri_string;
521  DBGC ( validator, "VALIDATOR %p \"%s\" checking ",
523  DBGC ( validator, "\"%s\" via %s\n",
524  x509_name ( cert ), uri_string );
525  if ( ( rc = xfer_open_uri_string ( &validator->xfer,
526  uri_string ) ) != 0 ) {
527  DBGC ( validator, "VALIDATOR %p \"%s\" could not open %s: "
528  "%s\n", validator, validator_name ( validator ),
529  uri_string, strerror ( rc ) );
530  goto err_open;
531  }
532 
533  return 0;
534 
535  intf_restart ( &validator->xfer, rc );
536  err_open:
537  ocsp_put ( validator->ocsp );
538  validator->ocsp = NULL;
539  err_check:
540  validator->rc = rc;
541  return rc;
542 }
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:343
#define DBGC(...)
Definition: compiler.h:505
A certificate validator.
Definition: validator.c:64
struct interface xfer
Data transfer interface.
Definition: validator.c:70
assert((readw(&hdr->flags) &(GTF_reading|GTF_writing))==0)
struct ocsp_check * ocsp
OCSP check.
Definition: validator.c:114
const struct validator_action * action
Current action.
Definition: validator.c:119
char * strerror(int errno)
Retrieve string representation of error number.
Definition: strerror.c:78
static void ocsp_put(struct ocsp_check *ocsp)
Drop reference to OCSP check.
Definition: ocsp.h:118
int rc
Most relevant status code.
Definition: validator.c:107
struct x509_certificate * cert
Current certificate (for progress reporting)
Definition: validator.c:126
static const struct validator_action validator_ocsp
OCSP validator action.
Definition: validator.c:487
const char * x509_name(struct x509_certificate *cert)
Get X.509 certificate display name.
Definition: x509.c:145
int xfer_open_uri_string(struct interface *intf, const char *uri_string)
Open URI string.
Definition: open.c:115
An OCSP check.
Definition: ocsp.h:85
#define NULL
NULL pointer (VOID *)
Definition: Base.h:321
char * uri_string
URI string.
Definition: ocsp.h:93
static const char * validator_name(struct validator *validator)
Get validator name (for debug messages)
Definition: validator.c:137

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 556 of file validator.c.

556  {
557 
558  /* Close data transfer interface */
559  intf_restart ( &validator->xfer, rc );
560  DBGC2 ( validator, "VALIDATOR %p \"%s\" transfer complete\n",
562 
563  /* Process completed download */
564  assert ( validator->action != NULL );
566 
567  /* Free downloaded data */
569 
570  /* Resume validation process */
572 }
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:343
struct process process
Process.
Definition: validator.c:73
void(* done)(struct validator *validator, int rc)
Action to take upon completed transfer.
Definition: validator.c:60
void xferbuf_free(struct xfer_buffer *xferbuf)
Free data transfer buffer.
Definition: xferbuf.c:58
A certificate validator.
Definition: validator.c:64
struct interface xfer
Data transfer interface.
Definition: validator.c:70
assert((readw(&hdr->flags) &(GTF_reading|GTF_writing))==0)
const struct validator_action * action
Current action.
Definition: validator.c:119
void process_add(struct process *process)
Add process to process list.
Definition: process.c:59
struct xfer_buffer buffer
Data buffer.
Definition: validator.c:116
#define DBGC2(...)
Definition: compiler.h:522
#define NULL
NULL pointer (VOID *)
Definition: Base.h:321
static const char * validator_name(struct validator *validator)
Get validator name (for debug messages)
Definition: validator.c:137

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 582 of file validator.c.

584  {
585  int rc;
586 
587  /* Add data to buffer */
588  if ( ( rc = xferbuf_deliver ( &validator->buffer, iob_disown ( iobuf ),
589  meta ) ) != 0 ) {
590  DBGC ( validator, "VALIDATOR %p \"%s\" could not receive "
591  "data: %s\n", validator, validator_name ( validator ),
592  strerror ( rc ) );
594  return rc;
595  }
596 
597  return 0;
598 }
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:152
#define DBGC(...)
Definition: compiler.h:505
A certificate validator.
Definition: validator.c:64
#define iob_disown(iobuf)
Disown an I/O buffer.
Definition: iobuf.h:212
int meta(WINDOW *, bool)
struct xfer_buffer buffer
Data buffer.
Definition: validator.c:116
char * strerror(int errno)
Retrieve string representation of error number.
Definition: strerror.c:78
static void validator_xfer_close(struct validator *validator, int rc)
Close data transfer interface.
Definition: validator.c:556
static const char * validator_name(struct validator *validator)
Get validator name (for debug messages)
Definition: validator.c:137

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 621 of file validator.c.

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

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(), time, 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 757 of file validator.c.

758  {
759  struct validator *validator;
760  int rc;
761 
762  /* Sanity check */
763  if ( ! chain ) {
764  rc = -EINVAL;
765  goto err_sanity;
766  }
767 
768  /* Allocate and initialise structure */
769  validator = zalloc ( sizeof ( *validator ) );
770  if ( ! validator ) {
771  rc = -ENOMEM;
772  goto err_alloc;
773  }
776  &validator->refcnt );
778  &validator->refcnt );
780  &validator->refcnt );
784 
785  /* Attach parent interface, mortalise self, and return */
787  ref_put ( &validator->refcnt );
788  DBGC2 ( validator, "VALIDATOR %p \"%s\" validating X509 chain %p\n",
790  return 0;
791 
793  ref_put ( &validator->refcnt );
794  err_alloc:
795  err_sanity:
796  return rc;
797 }
#define EINVAL
Invalid argument.
Definition: errno.h:428
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:280
struct process process
Process.
Definition: validator.c:73
struct stp_switch root
Root switch.
Definition: stp.h:26
struct refcnt refcnt
Reference count.
Definition: validator.c:66
#define ref_init(refcnt, free)
Initialise a reference counter.
Definition: refcnt.h:64
static void process_init(struct process *process, struct process_descriptor *desc, struct refcnt *refcnt)
Initialise process and add to process list.
Definition: process.h:161
void intf_plug_plug(struct interface *a, struct interface *b)
Plug two object interfaces together.
Definition: interface.c:107
struct x509_chain * chain
X.509 certificate chain.
Definition: validator.c:112
A certificate validator.
Definition: validator.c:64
struct interface xfer
Data transfer interface.
Definition: validator.c:70
#define ENOMEM
Not enough space.
Definition: errno.h:534
static struct interface_descriptor validator_xfer_desc
Certificate validator data transfer interface descriptor.
Definition: validator.c:607
static struct process_descriptor validator_process_desc
Certificate validator process descriptor.
Definition: validator.c:740
static void xferbuf_malloc_init(struct xfer_buffer *xferbuf)
Initialise malloc()-based data transfer buffer.
Definition: xferbuf.h:76
static struct x509_root * x509_root_get(struct x509_root *root)
Get reference to X.509 root certificate list.
Definition: x509.h:384
static void validator_free(struct refcnt *refcnt)
Free certificate validator.
Definition: validator.c:148
struct xfer_buffer buffer
Data buffer.
Definition: validator.c:116
void * zalloc(size_t size)
Allocate cleared memory.
Definition: malloc.c:624
static struct interface_descriptor validator_job_desc
Certificate validator job control interface descriptor.
Definition: validator.c:210
#define DBGC2(...)
Definition: compiler.h:522
struct x509_root * root
Root of trust (or NULL to use default)
Definition: validator.c:110
struct interface job
Job control interface.
Definition: validator.c:68
static void validator_finished(struct validator *validator, int rc)
Mark certificate validation as finished.
Definition: validator.c:167
static void intf_init(struct interface *intf, struct interface_descriptor *desc, struct refcnt *refcnt)
Initialise an object interface.
Definition: interface.h:203
#define ref_put(refcnt)
Drop reference to object.
Definition: refcnt.h:106
static const char * validator_name(struct validator *validator)
Get validator name (for debug messages)
Definition: validator.c:137

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:249
A certificate validator.
Definition: validator.c:64
static int validator_progress(struct validator *validator, struct job_progress *progress)
Report job progress.
Definition: validator.c:190
Job progress.
Definition: job.h:15
#define INTF_OP(op_type, object_type, op_func)
Define an object interface operation.
Definition: interface.h:32
static void validator_finished(struct validator *validator, int rc)
Mark certificate validation as finished.
Definition: validator.c:167

Certificate validator job control interface operations.

Definition at line 204 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:204
A certificate validator.
Definition: validator.c:64
#define INTF_DESC(object_type, intf, operations)
Define an object interface descriptor.
Definition: interface.h:80
struct interface job
Job control interface.
Definition: validator.c:68

Certificate validator job control interface descriptor.

Definition at line 210 of file validator.c.

Referenced by create_validator().

◆ crosscert_default

const char crosscert_default[] = CROSSCERT
static

Default cross-signed certificate source.

Definition at line 228 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:237

Cross-signing certificate download validator action.

Definition at line 346 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:443

OCSP validator action.

Definition at line 487 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:249
A certificate validator.
Definition: validator.c:64
static int validator_xfer_deliver(struct validator *validator, struct io_buffer *iobuf, struct xfer_metadata *meta)
Receive data.
Definition: validator.c:582
#define INTF_OP(op_type, object_type, op_func)
Define an object interface operation.
Definition: interface.h:32
int xfer_deliver(struct interface *intf, struct io_buffer *iobuf, struct xfer_metadata *meta)
Deliver datagram.
Definition: xfer.c:194
static void validator_xfer_close(struct validator *validator, int rc)
Close data transfer interface.
Definition: validator.c:556

Certificate validator data transfer interface operations.

Definition at line 601 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:601
A certificate validator.
Definition: validator.c:64
struct interface xfer
Data transfer interface.
Definition: validator.c:70
#define INTF_DESC(object_type, intf, operations)
Define an object interface descriptor.
Definition: interface.h:80

Certificate validator data transfer interface descriptor.

Definition at line 607 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:17
static void validator_step(struct validator *validator)
Certificate validation process.
Definition: validator.c:621
#define PROC_DESC_ONCE(object_type, process, _step)
Define a process descriptor for a process that runs only once.
Definition: process.h:97
A certificate validator.
Definition: validator.c:64

Certificate validator process descriptor.

Definition at line 740 of file validator.c.

Referenced by create_validator().