You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

validator.c 18KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664
  1. /*
  2. * Copyright (C) 2012 Michael Brown <mbrown@fensystems.co.uk>.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License as
  6. * published by the Free Software Foundation; either version 2 of the
  7. * License, or (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  17. * 02110-1301, USA.
  18. *
  19. * You can also choose to distribute this program under the terms of
  20. * the Unmodified Binary Distribution Licence (as given in the file
  21. * COPYING.UBDL), provided that you have satisfied its requirements.
  22. */
  23. FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
  24. #include <string.h>
  25. #include <stdio.h>
  26. #include <errno.h>
  27. #include <ipxe/refcnt.h>
  28. #include <ipxe/malloc.h>
  29. #include <ipxe/interface.h>
  30. #include <ipxe/xfer.h>
  31. #include <ipxe/open.h>
  32. #include <ipxe/iobuf.h>
  33. #include <ipxe/xferbuf.h>
  34. #include <ipxe/process.h>
  35. #include <ipxe/x509.h>
  36. #include <ipxe/settings.h>
  37. #include <ipxe/dhcp.h>
  38. #include <ipxe/base64.h>
  39. #include <ipxe/crc32.h>
  40. #include <ipxe/ocsp.h>
  41. #include <ipxe/job.h>
  42. #include <ipxe/validator.h>
  43. #include <config/crypto.h>
  44. /** @file
  45. *
  46. * Certificate validator
  47. *
  48. */
  49. struct validator;
  50. /** A certificate validator action */
  51. struct validator_action {
  52. /** Name */
  53. const char *name;
  54. /** Action to take upon completed transfer */
  55. int ( * done ) ( struct validator *validator, const void *data,
  56. size_t len );
  57. };
  58. /** A certificate validator */
  59. struct validator {
  60. /** Reference count */
  61. struct refcnt refcnt;
  62. /** Job control interface */
  63. struct interface job;
  64. /** Data transfer interface */
  65. struct interface xfer;
  66. /** Process */
  67. struct process process;
  68. /** X.509 certificate chain */
  69. struct x509_chain *chain;
  70. /** OCSP check */
  71. struct ocsp_check *ocsp;
  72. /** Data buffer */
  73. struct xfer_buffer buffer;
  74. /** Current action */
  75. const struct validator_action *action;
  76. /** Current certificate
  77. *
  78. * This will always be present within the certificate chain
  79. * and so this pointer does not hold a reference to the
  80. * certificate.
  81. */
  82. struct x509_certificate *cert;
  83. };
  84. /**
  85. * Get validator name (for debug messages)
  86. *
  87. * @v validator Certificate validator
  88. * @ret name Validator name
  89. */
  90. static const char * validator_name ( struct validator *validator ) {
  91. /* Use name of first certificate in chain */
  92. return x509_name ( x509_first ( validator->chain ) );
  93. }
  94. /**
  95. * Free certificate validator
  96. *
  97. * @v refcnt Reference count
  98. */
  99. static void validator_free ( struct refcnt *refcnt ) {
  100. struct validator *validator =
  101. container_of ( refcnt, struct validator, refcnt );
  102. DBGC2 ( validator, "VALIDATOR %p \"%s\" freed\n",
  103. validator, validator_name ( validator ) );
  104. x509_chain_put ( validator->chain );
  105. ocsp_put ( validator->ocsp );
  106. xferbuf_free ( &validator->buffer );
  107. free ( validator );
  108. }
  109. /**
  110. * Mark certificate validation as finished
  111. *
  112. * @v validator Certificate validator
  113. * @v rc Reason for finishing
  114. */
  115. static void validator_finished ( struct validator *validator, int rc ) {
  116. /* Remove process */
  117. process_del ( &validator->process );
  118. /* Close all interfaces */
  119. intf_shutdown ( &validator->xfer, rc );
  120. intf_shutdown ( &validator->job, rc );
  121. }
  122. /****************************************************************************
  123. *
  124. * Job control interface
  125. *
  126. */
  127. /**
  128. * Report job progress
  129. *
  130. * @v validator Certificate validator
  131. * @v progress Progress report to fill in
  132. * @ret ongoing_rc Ongoing job status code (if known)
  133. */
  134. static int validator_progress ( struct validator *validator,
  135. struct job_progress *progress ) {
  136. /* Report current action, if applicable */
  137. if ( validator->action ) {
  138. snprintf ( progress->message, sizeof ( progress->message ),
  139. "%s %s", validator->action->name,
  140. x509_name ( validator->cert ) );
  141. }
  142. return 0;
  143. }
  144. /** Certificate validator job control interface operations */
  145. static struct interface_operation validator_job_operations[] = {
  146. INTF_OP ( job_progress, struct validator *, validator_progress ),
  147. INTF_OP ( intf_close, struct validator *, validator_finished ),
  148. };
  149. /** Certificate validator job control interface descriptor */
  150. static struct interface_descriptor validator_job_desc =
  151. INTF_DESC ( struct validator, job, validator_job_operations );
  152. /****************************************************************************
  153. *
  154. * Cross-signing certificates
  155. *
  156. */
  157. /** Cross-signed certificate source setting */
  158. const struct setting crosscert_setting __setting ( SETTING_CRYPTO, crosscert )={
  159. .name = "crosscert",
  160. .description = "Cross-signed certificate source",
  161. .tag = DHCP_EB_CROSS_CERT,
  162. .type = &setting_type_string,
  163. };
  164. /** Default cross-signed certificate source */
  165. static const char crosscert_default[] = CROSSCERT;
  166. /**
  167. * Append cross-signing certificates to certificate chain
  168. *
  169. * @v validator Certificate validator
  170. * @v data Raw cross-signing certificate data
  171. * @v len Length of raw data
  172. * @ret rc Return status code
  173. */
  174. static int validator_append ( struct validator *validator,
  175. const void *data, size_t len ) {
  176. struct asn1_cursor cursor;
  177. struct x509_chain *certs;
  178. struct x509_certificate *cert;
  179. struct x509_certificate *last;
  180. int rc;
  181. /* Allocate certificate list */
  182. certs = x509_alloc_chain();
  183. if ( ! certs ) {
  184. rc = -ENOMEM;
  185. goto err_alloc_certs;
  186. }
  187. /* Initialise cursor */
  188. cursor.data = data;
  189. cursor.len = len;
  190. /* Enter certificateSet */
  191. if ( ( rc = asn1_enter ( &cursor, ASN1_SET ) ) != 0 ) {
  192. DBGC ( validator, "VALIDATOR %p \"%s\" could not enter "
  193. "certificateSet: %s\n", validator,
  194. validator_name ( validator ), strerror ( rc ) );
  195. goto err_certificateset;
  196. }
  197. /* Add each certificate to list */
  198. while ( cursor.len ) {
  199. /* Add certificate to chain */
  200. if ( ( rc = x509_append_raw ( certs, cursor.data,
  201. cursor.len ) ) != 0 ) {
  202. DBGC ( validator, "VALIDATOR %p \"%s\" could not "
  203. "append certificate: %s\n", validator,
  204. validator_name ( validator ), strerror ( rc) );
  205. DBGC_HDA ( validator, 0, cursor.data, cursor.len );
  206. return rc;
  207. }
  208. cert = x509_last ( certs );
  209. DBGC ( validator, "VALIDATOR %p \"%s\" found certificate ",
  210. validator, validator_name ( validator ) );
  211. DBGC ( validator, "%s\n", x509_name ( cert ) );
  212. /* Move to next certificate */
  213. asn1_skip_any ( &cursor );
  214. }
  215. /* Append certificates to chain */
  216. last = x509_last ( validator->chain );
  217. if ( ( rc = x509_auto_append ( validator->chain, certs ) ) != 0 ) {
  218. DBGC ( validator, "VALIDATOR %p \"%s\" could not append "
  219. "certificates: %s\n", validator,
  220. validator_name ( validator ), strerror ( rc ) );
  221. goto err_auto_append;
  222. }
  223. /* Check that at least one certificate has been added */
  224. if ( last == x509_last ( validator->chain ) ) {
  225. DBGC ( validator, "VALIDATOR %p \"%s\" failed to append any "
  226. "applicable certificates\n", validator,
  227. validator_name ( validator ) );
  228. rc = -EACCES;
  229. goto err_no_progress;
  230. }
  231. /* Drop reference to certificate list */
  232. x509_chain_put ( certs );
  233. return 0;
  234. err_no_progress:
  235. err_auto_append:
  236. err_certificateset:
  237. x509_chain_put ( certs );
  238. err_alloc_certs:
  239. return rc;
  240. }
  241. /** Cross-signing certificate download validator action */
  242. static const struct validator_action validator_crosscert = {
  243. .name = "XCRT",
  244. .done = validator_append,
  245. };
  246. /**
  247. * Start download of cross-signing certificate
  248. *
  249. * @v validator Certificate validator
  250. * @v cert X.509 certificate
  251. * @ret rc Return status code
  252. */
  253. static int validator_start_download ( struct validator *validator,
  254. struct x509_certificate *cert ) {
  255. const struct asn1_cursor *issuer = &cert->issuer.raw;
  256. const char *crosscert;
  257. char *crosscert_copy;
  258. char *uri_string;
  259. size_t uri_string_len;
  260. uint32_t crc;
  261. int len;
  262. int rc;
  263. /* Determine cross-signed certificate source */
  264. fetch_string_setting_copy ( NULL, &crosscert_setting, &crosscert_copy );
  265. crosscert = ( crosscert_copy ? crosscert_copy : crosscert_default );
  266. if ( ! crosscert[0] ) {
  267. rc = -EINVAL;
  268. goto err_check_uri_string;
  269. }
  270. /* Allocate URI string */
  271. uri_string_len = ( strlen ( crosscert ) + 22 /* "/%08x.der?subject=" */
  272. + base64_encoded_len ( issuer->len ) + 1 /* NUL */ );
  273. uri_string = zalloc ( uri_string_len );
  274. if ( ! uri_string ) {
  275. rc = -ENOMEM;
  276. goto err_alloc_uri_string;
  277. }
  278. /* Generate CRC32 */
  279. crc = crc32_le ( 0xffffffffUL, issuer->data, issuer->len );
  280. /* Generate URI string */
  281. len = snprintf ( uri_string, uri_string_len, "%s/%08x.der?subject=",
  282. crosscert, crc );
  283. base64_encode ( issuer->data, issuer->len, ( uri_string + len ),
  284. ( uri_string_len - len ) );
  285. DBGC ( validator, "VALIDATOR %p \"%s\" downloading ",
  286. validator, validator_name ( validator ) );
  287. DBGC ( validator, "\"%s\" cross-signature from %s\n",
  288. x509_name ( cert ), uri_string );
  289. /* Set completion handler */
  290. validator->action = &validator_crosscert;
  291. validator->cert = cert;
  292. /* Open URI */
  293. if ( ( rc = xfer_open_uri_string ( &validator->xfer,
  294. uri_string ) ) != 0 ) {
  295. DBGC ( validator, "VALIDATOR %p \"%s\" could not open %s: "
  296. "%s\n", validator, validator_name ( validator ),
  297. uri_string, strerror ( rc ) );
  298. goto err_open_uri_string;
  299. }
  300. /* Success */
  301. rc = 0;
  302. err_open_uri_string:
  303. free ( uri_string );
  304. err_alloc_uri_string:
  305. err_check_uri_string:
  306. free ( crosscert_copy );
  307. return rc;
  308. }
  309. /****************************************************************************
  310. *
  311. * OCSP checks
  312. *
  313. */
  314. /**
  315. * Validate OCSP response
  316. *
  317. * @v validator Certificate validator
  318. * @v data Raw OCSP response
  319. * @v len Length of raw data
  320. * @ret rc Return status code
  321. */
  322. static int validator_ocsp_validate ( struct validator *validator,
  323. const void *data, size_t len ) {
  324. time_t now;
  325. int rc;
  326. /* Record OCSP response */
  327. if ( ( rc = ocsp_response ( validator->ocsp, data, len ) ) != 0 ) {
  328. DBGC ( validator, "VALIDATOR %p \"%s\" could not record OCSP "
  329. "response: %s\n", validator,
  330. validator_name ( validator ),strerror ( rc ) );
  331. return rc;
  332. }
  333. /* Validate OCSP response */
  334. now = time ( NULL );
  335. if ( ( rc = ocsp_validate ( validator->ocsp, now ) ) != 0 ) {
  336. DBGC ( validator, "VALIDATOR %p \"%s\" could not validate "
  337. "OCSP response: %s\n", validator,
  338. validator_name ( validator ), strerror ( rc ) );
  339. return rc;
  340. }
  341. /* Drop reference to OCSP check */
  342. ocsp_put ( validator->ocsp );
  343. validator->ocsp = NULL;
  344. return 0;
  345. }
  346. /** OCSP validator action */
  347. static const struct validator_action validator_ocsp = {
  348. .name = "OCSP",
  349. .done = validator_ocsp_validate,
  350. };
  351. /**
  352. * Start OCSP check
  353. *
  354. * @v validator Certificate validator
  355. * @v cert Certificate to check
  356. * @v issuer Issuing certificate
  357. * @ret rc Return status code
  358. */
  359. static int validator_start_ocsp ( struct validator *validator,
  360. struct x509_certificate *cert,
  361. struct x509_certificate *issuer ) {
  362. const char *uri_string;
  363. int rc;
  364. /* Create OCSP check */
  365. assert ( validator->ocsp == NULL );
  366. if ( ( rc = ocsp_check ( cert, issuer, &validator->ocsp ) ) != 0 ) {
  367. DBGC ( validator, "VALIDATOR %p \"%s\" could not create OCSP "
  368. "check: %s\n", validator, validator_name ( validator ),
  369. strerror ( rc ) );
  370. return rc;
  371. }
  372. /* Set completion handler */
  373. validator->action = &validator_ocsp;
  374. validator->cert = cert;
  375. /* Open URI */
  376. uri_string = validator->ocsp->uri_string;
  377. DBGC ( validator, "VALIDATOR %p \"%s\" checking ",
  378. validator, validator_name ( validator ) );
  379. DBGC ( validator, "\"%s\" via %s\n",
  380. x509_name ( cert ), uri_string );
  381. if ( ( rc = xfer_open_uri_string ( &validator->xfer,
  382. uri_string ) ) != 0 ) {
  383. DBGC ( validator, "VALIDATOR %p \"%s\" could not open %s: "
  384. "%s\n", validator, validator_name ( validator ),
  385. uri_string, strerror ( rc ) );
  386. return rc;
  387. }
  388. return 0;
  389. }
  390. /****************************************************************************
  391. *
  392. * Data transfer interface
  393. *
  394. */
  395. /**
  396. * Close data transfer interface
  397. *
  398. * @v validator Certificate validator
  399. * @v rc Reason for close
  400. */
  401. static void validator_xfer_close ( struct validator *validator, int rc ) {
  402. /* Close data transfer interface */
  403. intf_restart ( &validator->xfer, rc );
  404. /* Check for errors */
  405. if ( rc != 0 ) {
  406. DBGC ( validator, "VALIDATOR %p \"%s\" transfer failed: %s\n",
  407. validator, validator_name ( validator ),
  408. strerror ( rc ) );
  409. goto err_transfer;
  410. }
  411. DBGC2 ( validator, "VALIDATOR %p \"%s\" transfer complete\n",
  412. validator, validator_name ( validator ) );
  413. /* Process completed download */
  414. assert ( validator->action != NULL );
  415. if ( ( rc = validator->action->done ( validator, validator->buffer.data,
  416. validator->buffer.len ) ) != 0 )
  417. goto err_append;
  418. /* Free downloaded data */
  419. xferbuf_free ( &validator->buffer );
  420. /* Resume validation process */
  421. process_add ( &validator->process );
  422. return;
  423. err_append:
  424. err_transfer:
  425. validator_finished ( validator, rc );
  426. }
  427. /**
  428. * Receive data
  429. *
  430. * @v validator Certificate validator
  431. * @v iobuf I/O buffer
  432. * @v meta Data transfer metadata
  433. * @ret rc Return status code
  434. */
  435. static int validator_xfer_deliver ( struct validator *validator,
  436. struct io_buffer *iobuf,
  437. struct xfer_metadata *meta ) {
  438. int rc;
  439. /* Add data to buffer */
  440. if ( ( rc = xferbuf_deliver ( &validator->buffer, iob_disown ( iobuf ),
  441. meta ) ) != 0 ) {
  442. DBGC ( validator, "VALIDATOR %p \"%s\" could not receive "
  443. "data: %s\n", validator, validator_name ( validator ),
  444. strerror ( rc ) );
  445. validator_finished ( validator, rc );
  446. return rc;
  447. }
  448. return 0;
  449. }
  450. /** Certificate validator data transfer interface operations */
  451. static struct interface_operation validator_xfer_operations[] = {
  452. INTF_OP ( xfer_deliver, struct validator *, validator_xfer_deliver ),
  453. INTF_OP ( intf_close, struct validator *, validator_xfer_close ),
  454. };
  455. /** Certificate validator data transfer interface descriptor */
  456. static struct interface_descriptor validator_xfer_desc =
  457. INTF_DESC ( struct validator, xfer, validator_xfer_operations );
  458. /****************************************************************************
  459. *
  460. * Validation process
  461. *
  462. */
  463. /**
  464. * Certificate validation process
  465. *
  466. * @v validator Certificate validator
  467. */
  468. static void validator_step ( struct validator *validator ) {
  469. struct x509_link *link;
  470. struct x509_certificate *cert;
  471. struct x509_certificate *issuer = NULL;
  472. struct x509_certificate *last;
  473. time_t now;
  474. int rc;
  475. /* Try validating chain. Try even if the chain is incomplete,
  476. * since certificates may already have been validated
  477. * previously.
  478. */
  479. now = time ( NULL );
  480. if ( ( rc = x509_validate_chain ( validator->chain, now, NULL,
  481. NULL ) ) == 0 ) {
  482. DBGC ( validator, "VALIDATOR %p \"%s\" validated\n",
  483. validator, validator_name ( validator ) );
  484. validator_finished ( validator, 0 );
  485. return;
  486. }
  487. /* If there is a certificate that could be validated using
  488. * OCSP, try it.
  489. */
  490. list_for_each_entry ( link, &validator->chain->links, list ) {
  491. cert = issuer;
  492. issuer = link->cert;
  493. if ( ! cert )
  494. continue;
  495. if ( ! x509_is_valid ( issuer ) )
  496. continue;
  497. /* The issuer is valid, but this certificate is not
  498. * yet valid. If OCSP is applicable, start it.
  499. */
  500. if ( ocsp_required ( cert ) ) {
  501. /* Start OCSP */
  502. if ( ( rc = validator_start_ocsp ( validator, cert,
  503. issuer ) ) != 0 ) {
  504. validator_finished ( validator, rc );
  505. return;
  506. }
  507. return;
  508. }
  509. /* Otherwise, this is a permanent failure */
  510. validator_finished ( validator, rc );
  511. return;
  512. }
  513. /* If chain ends with a self-issued certificate, then there is
  514. * nothing more to do.
  515. */
  516. last = x509_last ( validator->chain );
  517. if ( asn1_compare ( &last->issuer.raw, &last->subject.raw ) == 0 ) {
  518. validator_finished ( validator, rc );
  519. return;
  520. }
  521. /* Otherwise, try to download a suitable cross-signing
  522. * certificate.
  523. */
  524. if ( ( rc = validator_start_download ( validator, last ) ) != 0 ) {
  525. validator_finished ( validator, rc );
  526. return;
  527. }
  528. }
  529. /** Certificate validator process descriptor */
  530. static struct process_descriptor validator_process_desc =
  531. PROC_DESC_ONCE ( struct validator, process, validator_step );
  532. /****************************************************************************
  533. *
  534. * Instantiator
  535. *
  536. */
  537. /**
  538. * Instantiate a certificate validator
  539. *
  540. * @v job Job control interface
  541. * @v chain X.509 certificate chain
  542. * @ret rc Return status code
  543. */
  544. int create_validator ( struct interface *job, struct x509_chain *chain ) {
  545. struct validator *validator;
  546. int rc;
  547. /* Sanity check */
  548. if ( ! chain ) {
  549. rc = -EINVAL;
  550. goto err_sanity;
  551. }
  552. /* Allocate and initialise structure */
  553. validator = zalloc ( sizeof ( *validator ) );
  554. if ( ! validator ) {
  555. rc = -ENOMEM;
  556. goto err_alloc;
  557. }
  558. ref_init ( &validator->refcnt, validator_free );
  559. intf_init ( &validator->job, &validator_job_desc,
  560. &validator->refcnt );
  561. intf_init ( &validator->xfer, &validator_xfer_desc,
  562. &validator->refcnt );
  563. process_init ( &validator->process, &validator_process_desc,
  564. &validator->refcnt );
  565. validator->chain = x509_chain_get ( chain );
  566. xferbuf_malloc_init ( &validator->buffer );
  567. /* Attach parent interface, mortalise self, and return */
  568. intf_plug_plug ( &validator->job, job );
  569. ref_put ( &validator->refcnt );
  570. DBGC2 ( validator, "VALIDATOR %p \"%s\" validating X509 chain %p\n",
  571. validator, validator_name ( validator ), validator->chain );
  572. return 0;
  573. validator_finished ( validator, rc );
  574. ref_put ( &validator->refcnt );
  575. err_alloc:
  576. err_sanity:
  577. return rc;
  578. }