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 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579
  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/validator.h>
  42. #include <config/crypto.h>
  43. /** @file
  44. *
  45. * Certificate validator
  46. *
  47. */
  48. /** A certificate validator */
  49. struct validator {
  50. /** Reference count */
  51. struct refcnt refcnt;
  52. /** Job control interface */
  53. struct interface job;
  54. /** Data transfer interface */
  55. struct interface xfer;
  56. /** Process */
  57. struct process process;
  58. /** X.509 certificate chain */
  59. struct x509_chain *chain;
  60. /** OCSP check */
  61. struct ocsp_check *ocsp;
  62. /** Data buffer */
  63. struct xfer_buffer buffer;
  64. /** Action to take upon completed transfer */
  65. int ( * done ) ( struct validator *validator, const void *data,
  66. size_t len );
  67. };
  68. /**
  69. * Free certificate validator
  70. *
  71. * @v refcnt Reference count
  72. */
  73. static void validator_free ( struct refcnt *refcnt ) {
  74. struct validator *validator =
  75. container_of ( refcnt, struct validator, refcnt );
  76. DBGC2 ( validator, "VALIDATOR %p freed\n", validator );
  77. x509_chain_put ( validator->chain );
  78. ocsp_put ( validator->ocsp );
  79. xferbuf_free ( &validator->buffer );
  80. free ( validator );
  81. }
  82. /**
  83. * Mark certificate validation as finished
  84. *
  85. * @v validator Certificate validator
  86. * @v rc Reason for finishing
  87. */
  88. static void validator_finished ( struct validator *validator, int rc ) {
  89. /* Remove process */
  90. process_del ( &validator->process );
  91. /* Close all interfaces */
  92. intf_shutdown ( &validator->xfer, rc );
  93. intf_shutdown ( &validator->job, rc );
  94. }
  95. /****************************************************************************
  96. *
  97. * Job control interface
  98. *
  99. */
  100. /** Certificate validator job control interface operations */
  101. static struct interface_operation validator_job_operations[] = {
  102. INTF_OP ( intf_close, struct validator *, validator_finished ),
  103. };
  104. /** Certificate validator job control interface descriptor */
  105. static struct interface_descriptor validator_job_desc =
  106. INTF_DESC ( struct validator, job, validator_job_operations );
  107. /****************************************************************************
  108. *
  109. * Cross-signing certificates
  110. *
  111. */
  112. /** Cross-signed certificate source setting */
  113. const struct setting crosscert_setting __setting ( SETTING_CRYPTO, crosscert )={
  114. .name = "crosscert",
  115. .description = "Cross-signed certificate source",
  116. .tag = DHCP_EB_CROSS_CERT,
  117. .type = &setting_type_string,
  118. };
  119. /** Default cross-signed certificate source */
  120. static const char crosscert_default[] = CROSSCERT;
  121. /**
  122. * Append cross-signing certificates to certificate chain
  123. *
  124. * @v validator Certificate validator
  125. * @v data Raw cross-signing certificate data
  126. * @v len Length of raw data
  127. * @ret rc Return status code
  128. */
  129. static int validator_append ( struct validator *validator,
  130. const void *data, size_t len ) {
  131. struct asn1_cursor cursor;
  132. struct x509_chain *certs;
  133. struct x509_certificate *cert;
  134. struct x509_certificate *last;
  135. int rc;
  136. /* Allocate certificate list */
  137. certs = x509_alloc_chain();
  138. if ( ! certs ) {
  139. rc = -ENOMEM;
  140. goto err_alloc_certs;
  141. }
  142. /* Initialise cursor */
  143. cursor.data = data;
  144. cursor.len = len;
  145. /* Enter certificateSet */
  146. if ( ( rc = asn1_enter ( &cursor, ASN1_SET ) ) != 0 ) {
  147. DBGC ( validator, "VALIDATOR %p could not enter "
  148. "certificateSet: %s\n", validator, strerror ( rc ) );
  149. goto err_certificateset;
  150. }
  151. /* Add each certificate to list */
  152. while ( cursor.len ) {
  153. /* Add certificate to chain */
  154. if ( ( rc = x509_append_raw ( certs, cursor.data,
  155. cursor.len ) ) != 0 ) {
  156. DBGC ( validator, "VALIDATOR %p could not append "
  157. "certificate: %s\n",
  158. validator, strerror ( rc) );
  159. DBGC_HDA ( validator, 0, cursor.data, cursor.len );
  160. return rc;
  161. }
  162. cert = x509_last ( certs );
  163. DBGC ( validator, "VALIDATOR %p found certificate %s\n",
  164. validator, x509_name ( cert ) );
  165. /* Move to next certificate */
  166. asn1_skip_any ( &cursor );
  167. }
  168. /* Append certificates to chain */
  169. last = x509_last ( validator->chain );
  170. if ( ( rc = x509_auto_append ( validator->chain, certs ) ) != 0 ) {
  171. DBGC ( validator, "VALIDATOR %p could not append "
  172. "certificates: %s\n", validator, strerror ( rc ) );
  173. goto err_auto_append;
  174. }
  175. /* Check that at least one certificate has been added */
  176. if ( last == x509_last ( validator->chain ) ) {
  177. DBGC ( validator, "VALIDATOR %p failed to append any "
  178. "applicable certificates\n", validator );
  179. rc = -EACCES;
  180. goto err_no_progress;
  181. }
  182. /* Drop reference to certificate list */
  183. x509_chain_put ( certs );
  184. return 0;
  185. err_no_progress:
  186. err_auto_append:
  187. err_certificateset:
  188. x509_chain_put ( certs );
  189. err_alloc_certs:
  190. return rc;
  191. }
  192. /**
  193. * Start download of cross-signing certificate
  194. *
  195. * @v validator Certificate validator
  196. * @v issuer Required issuer
  197. * @ret rc Return status code
  198. */
  199. static int validator_start_download ( struct validator *validator,
  200. const struct asn1_cursor *issuer ) {
  201. const char *crosscert;
  202. char *crosscert_copy;
  203. char *uri_string;
  204. size_t uri_string_len;
  205. uint32_t crc;
  206. int len;
  207. int rc;
  208. /* Determine cross-signed certificate source */
  209. fetch_string_setting_copy ( NULL, &crosscert_setting, &crosscert_copy );
  210. crosscert = ( crosscert_copy ? crosscert_copy : crosscert_default );
  211. if ( ! crosscert[0] ) {
  212. rc = -EINVAL;
  213. goto err_check_uri_string;
  214. }
  215. /* Allocate URI string */
  216. uri_string_len = ( strlen ( crosscert ) + 22 /* "/%08x.der?subject=" */
  217. + base64_encoded_len ( issuer->len ) + 1 /* NUL */ );
  218. uri_string = zalloc ( uri_string_len );
  219. if ( ! uri_string ) {
  220. rc = -ENOMEM;
  221. goto err_alloc_uri_string;
  222. }
  223. /* Generate CRC32 */
  224. crc = crc32_le ( 0xffffffffUL, issuer->data, issuer->len );
  225. /* Generate URI string */
  226. len = snprintf ( uri_string, uri_string_len, "%s/%08x.der?subject=",
  227. crosscert, crc );
  228. base64_encode ( issuer->data, issuer->len, ( uri_string + len ),
  229. ( uri_string_len - len ) );
  230. DBGC ( validator, "VALIDATOR %p downloading cross-signed certificate "
  231. "from %s\n", validator, uri_string );
  232. /* Set completion handler */
  233. validator->done = validator_append;
  234. /* Open URI */
  235. if ( ( rc = xfer_open_uri_string ( &validator->xfer,
  236. uri_string ) ) != 0 ) {
  237. DBGC ( validator, "VALIDATOR %p could not open %s: %s\n",
  238. validator, uri_string, strerror ( rc ) );
  239. goto err_open_uri_string;
  240. }
  241. /* Success */
  242. rc = 0;
  243. err_open_uri_string:
  244. free ( uri_string );
  245. err_alloc_uri_string:
  246. err_check_uri_string:
  247. free ( crosscert_copy );
  248. return rc;
  249. }
  250. /****************************************************************************
  251. *
  252. * OCSP checks
  253. *
  254. */
  255. /**
  256. * Validate OCSP response
  257. *
  258. * @v validator Certificate validator
  259. * @v data Raw OCSP response
  260. * @v len Length of raw data
  261. * @ret rc Return status code
  262. */
  263. static int validator_ocsp_validate ( struct validator *validator,
  264. const void *data, size_t len ) {
  265. time_t now;
  266. int rc;
  267. /* Record OCSP response */
  268. if ( ( rc = ocsp_response ( validator->ocsp, data, len ) ) != 0 ) {
  269. DBGC ( validator, "VALIDATOR %p could not record OCSP "
  270. "response: %s\n", validator, strerror ( rc ) );
  271. return rc;
  272. }
  273. /* Validate OCSP response */
  274. now = time ( NULL );
  275. if ( ( rc = ocsp_validate ( validator->ocsp, now ) ) != 0 ) {
  276. DBGC ( validator, "VALIDATOR %p could not validate OCSP "
  277. "response: %s\n", validator, strerror ( rc ) );
  278. return rc;
  279. }
  280. /* Drop reference to OCSP check */
  281. ocsp_put ( validator->ocsp );
  282. validator->ocsp = NULL;
  283. return 0;
  284. }
  285. /**
  286. * Start OCSP check
  287. *
  288. * @v validator Certificate validator
  289. * @v cert Certificate to check
  290. * @v issuer Issuing certificate
  291. * @ret rc Return status code
  292. */
  293. static int validator_start_ocsp ( struct validator *validator,
  294. struct x509_certificate *cert,
  295. struct x509_certificate *issuer ) {
  296. const char *uri_string;
  297. int rc;
  298. /* Create OCSP check */
  299. assert ( validator->ocsp == NULL );
  300. if ( ( rc = ocsp_check ( cert, issuer, &validator->ocsp ) ) != 0 ) {
  301. DBGC ( validator, "VALIDATOR %p could not create OCSP check: "
  302. "%s\n", validator, strerror ( rc ) );
  303. return rc;
  304. }
  305. /* Set completion handler */
  306. validator->done = validator_ocsp_validate;
  307. /* Open URI */
  308. uri_string = validator->ocsp->uri_string;
  309. DBGC ( validator, "VALIDATOR %p performing OCSP check at %s\n",
  310. validator, uri_string );
  311. if ( ( rc = xfer_open_uri_string ( &validator->xfer,
  312. uri_string ) ) != 0 ) {
  313. DBGC ( validator, "VALIDATOR %p could not open %s: %s\n",
  314. validator, uri_string, strerror ( rc ) );
  315. return rc;
  316. }
  317. return 0;
  318. }
  319. /****************************************************************************
  320. *
  321. * Data transfer interface
  322. *
  323. */
  324. /**
  325. * Close data transfer interface
  326. *
  327. * @v validator Certificate validator
  328. * @v rc Reason for close
  329. */
  330. static void validator_xfer_close ( struct validator *validator, int rc ) {
  331. /* Close data transfer interface */
  332. intf_restart ( &validator->xfer, rc );
  333. /* Check for errors */
  334. if ( rc != 0 ) {
  335. DBGC ( validator, "VALIDATOR %p transfer failed: %s\n",
  336. validator, strerror ( rc ) );
  337. goto err_transfer;
  338. }
  339. DBGC2 ( validator, "VALIDATOR %p transfer complete\n", validator );
  340. /* Process completed download */
  341. assert ( validator->done != NULL );
  342. if ( ( rc = validator->done ( validator, validator->buffer.data,
  343. validator->buffer.len ) ) != 0 )
  344. goto err_append;
  345. /* Free downloaded data */
  346. xferbuf_free ( &validator->buffer );
  347. /* Resume validation process */
  348. process_add ( &validator->process );
  349. return;
  350. err_append:
  351. err_transfer:
  352. validator_finished ( validator, rc );
  353. }
  354. /**
  355. * Receive data
  356. *
  357. * @v validator Certificate validator
  358. * @v iobuf I/O buffer
  359. * @v meta Data transfer metadata
  360. * @ret rc Return status code
  361. */
  362. static int validator_xfer_deliver ( struct validator *validator,
  363. struct io_buffer *iobuf,
  364. struct xfer_metadata *meta ) {
  365. int rc;
  366. /* Add data to buffer */
  367. if ( ( rc = xferbuf_deliver ( &validator->buffer, iob_disown ( iobuf ),
  368. meta ) ) != 0 ) {
  369. DBGC ( validator, "VALIDATOR %p could not receive data: %s\n",
  370. validator, strerror ( rc ) );
  371. validator_finished ( validator, rc );
  372. return rc;
  373. }
  374. return 0;
  375. }
  376. /** Certificate validator data transfer interface operations */
  377. static struct interface_operation validator_xfer_operations[] = {
  378. INTF_OP ( xfer_deliver, struct validator *, validator_xfer_deliver ),
  379. INTF_OP ( intf_close, struct validator *, validator_xfer_close ),
  380. };
  381. /** Certificate validator data transfer interface descriptor */
  382. static struct interface_descriptor validator_xfer_desc =
  383. INTF_DESC ( struct validator, xfer, validator_xfer_operations );
  384. /****************************************************************************
  385. *
  386. * Validation process
  387. *
  388. */
  389. /**
  390. * Certificate validation process
  391. *
  392. * @v validator Certificate validator
  393. */
  394. static void validator_step ( struct validator *validator ) {
  395. struct x509_link *link;
  396. struct x509_certificate *cert;
  397. struct x509_certificate *issuer = NULL;
  398. struct x509_certificate *last;
  399. time_t now;
  400. int rc;
  401. /* Try validating chain. Try even if the chain is incomplete,
  402. * since certificates may already have been validated
  403. * previously.
  404. */
  405. now = time ( NULL );
  406. if ( ( rc = x509_validate_chain ( validator->chain, now, NULL,
  407. NULL ) ) == 0 ) {
  408. validator_finished ( validator, 0 );
  409. return;
  410. }
  411. /* If there is a certificate that could be validated using
  412. * OCSP, try it.
  413. */
  414. list_for_each_entry ( link, &validator->chain->links, list ) {
  415. cert = issuer;
  416. issuer = link->cert;
  417. if ( ! cert )
  418. continue;
  419. if ( ! x509_is_valid ( issuer ) )
  420. continue;
  421. /* The issuer is valid, but this certificate is not
  422. * yet valid. If OCSP is applicable, start it.
  423. */
  424. if ( ocsp_required ( cert ) ) {
  425. /* Start OCSP */
  426. if ( ( rc = validator_start_ocsp ( validator, cert,
  427. issuer ) ) != 0 ) {
  428. validator_finished ( validator, rc );
  429. return;
  430. }
  431. return;
  432. }
  433. /* Otherwise, this is a permanent failure */
  434. validator_finished ( validator, rc );
  435. return;
  436. }
  437. /* If chain ends with a self-issued certificate, then there is
  438. * nothing more to do.
  439. */
  440. last = x509_last ( validator->chain );
  441. if ( asn1_compare ( &last->issuer.raw, &last->subject.raw ) == 0 ) {
  442. validator_finished ( validator, rc );
  443. return;
  444. }
  445. /* Otherwise, try to download a suitable cross-signing
  446. * certificate.
  447. */
  448. if ( ( rc = validator_start_download ( validator,
  449. &last->issuer.raw ) ) != 0 ) {
  450. validator_finished ( validator, rc );
  451. return;
  452. }
  453. }
  454. /** Certificate validator process descriptor */
  455. static struct process_descriptor validator_process_desc =
  456. PROC_DESC_ONCE ( struct validator, process, validator_step );
  457. /****************************************************************************
  458. *
  459. * Instantiator
  460. *
  461. */
  462. /**
  463. * Instantiate a certificate validator
  464. *
  465. * @v job Job control interface
  466. * @v chain X.509 certificate chain
  467. * @ret rc Return status code
  468. */
  469. int create_validator ( struct interface *job, struct x509_chain *chain ) {
  470. struct validator *validator;
  471. int rc;
  472. /* Sanity check */
  473. if ( ! chain ) {
  474. rc = -EINVAL;
  475. goto err_sanity;
  476. }
  477. /* Allocate and initialise structure */
  478. validator = zalloc ( sizeof ( *validator ) );
  479. if ( ! validator ) {
  480. rc = -ENOMEM;
  481. goto err_alloc;
  482. }
  483. ref_init ( &validator->refcnt, validator_free );
  484. intf_init ( &validator->job, &validator_job_desc,
  485. &validator->refcnt );
  486. intf_init ( &validator->xfer, &validator_xfer_desc,
  487. &validator->refcnt );
  488. process_init ( &validator->process, &validator_process_desc,
  489. &validator->refcnt );
  490. validator->chain = x509_chain_get ( chain );
  491. xferbuf_malloc_init ( &validator->buffer );
  492. /* Attach parent interface, mortalise self, and return */
  493. intf_plug_plug ( &validator->job, job );
  494. ref_put ( &validator->refcnt );
  495. DBGC2 ( validator, "VALIDATOR %p validating X509 chain %p\n",
  496. validator, validator->chain );
  497. return 0;
  498. validator_finished ( validator, rc );
  499. ref_put ( &validator->refcnt );
  500. err_alloc:
  501. err_sanity:
  502. return rc;
  503. }