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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575
  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. /* Allocate URI string */
  212. uri_string_len = ( strlen ( crosscert ) + 22 /* "/%08x.der?subject=" */
  213. + base64_encoded_len ( issuer->len ) + 1 /* NUL */ );
  214. uri_string = zalloc ( uri_string_len );
  215. if ( ! uri_string ) {
  216. rc = -ENOMEM;
  217. goto err_alloc_uri_string;
  218. }
  219. /* Generate CRC32 */
  220. crc = crc32_le ( 0xffffffffUL, issuer->data, issuer->len );
  221. /* Generate URI string */
  222. len = snprintf ( uri_string, uri_string_len, "%s/%08x.der?subject=",
  223. crosscert, crc );
  224. base64_encode ( issuer->data, issuer->len, ( uri_string + len ),
  225. ( uri_string_len - len ) );
  226. DBGC ( validator, "VALIDATOR %p downloading cross-signed certificate "
  227. "from %s\n", validator, uri_string );
  228. /* Set completion handler */
  229. validator->done = validator_append;
  230. /* Open URI */
  231. if ( ( rc = xfer_open_uri_string ( &validator->xfer,
  232. uri_string ) ) != 0 ) {
  233. DBGC ( validator, "VALIDATOR %p could not open %s: %s\n",
  234. validator, uri_string, strerror ( rc ) );
  235. goto err_open_uri_string;
  236. }
  237. /* Success */
  238. rc = 0;
  239. err_open_uri_string:
  240. free ( uri_string );
  241. err_alloc_uri_string:
  242. free ( crosscert_copy );
  243. return rc;
  244. }
  245. /****************************************************************************
  246. *
  247. * OCSP checks
  248. *
  249. */
  250. /**
  251. * Validate OCSP response
  252. *
  253. * @v validator Certificate validator
  254. * @v data Raw OCSP response
  255. * @v len Length of raw data
  256. * @ret rc Return status code
  257. */
  258. static int validator_ocsp_validate ( struct validator *validator,
  259. const void *data, size_t len ) {
  260. time_t now;
  261. int rc;
  262. /* Record OCSP response */
  263. if ( ( rc = ocsp_response ( validator->ocsp, data, len ) ) != 0 ) {
  264. DBGC ( validator, "VALIDATOR %p could not record OCSP "
  265. "response: %s\n", validator, strerror ( rc ) );
  266. return rc;
  267. }
  268. /* Validate OCSP response */
  269. now = time ( NULL );
  270. if ( ( rc = ocsp_validate ( validator->ocsp, now ) ) != 0 ) {
  271. DBGC ( validator, "VALIDATOR %p could not validate OCSP "
  272. "response: %s\n", validator, strerror ( rc ) );
  273. return rc;
  274. }
  275. /* Drop reference to OCSP check */
  276. ocsp_put ( validator->ocsp );
  277. validator->ocsp = NULL;
  278. return 0;
  279. }
  280. /**
  281. * Start OCSP check
  282. *
  283. * @v validator Certificate validator
  284. * @v cert Certificate to check
  285. * @v issuer Issuing certificate
  286. * @ret rc Return status code
  287. */
  288. static int validator_start_ocsp ( struct validator *validator,
  289. struct x509_certificate *cert,
  290. struct x509_certificate *issuer ) {
  291. const char *uri_string;
  292. int rc;
  293. /* Create OCSP check */
  294. assert ( validator->ocsp == NULL );
  295. if ( ( rc = ocsp_check ( cert, issuer, &validator->ocsp ) ) != 0 ) {
  296. DBGC ( validator, "VALIDATOR %p could not create OCSP check: "
  297. "%s\n", validator, strerror ( rc ) );
  298. return rc;
  299. }
  300. /* Set completion handler */
  301. validator->done = validator_ocsp_validate;
  302. /* Open URI */
  303. uri_string = validator->ocsp->uri_string;
  304. DBGC ( validator, "VALIDATOR %p performing OCSP check at %s\n",
  305. validator, uri_string );
  306. if ( ( rc = xfer_open_uri_string ( &validator->xfer,
  307. uri_string ) ) != 0 ) {
  308. DBGC ( validator, "VALIDATOR %p could not open %s: %s\n",
  309. validator, uri_string, strerror ( rc ) );
  310. return rc;
  311. }
  312. return 0;
  313. }
  314. /****************************************************************************
  315. *
  316. * Data transfer interface
  317. *
  318. */
  319. /**
  320. * Close data transfer interface
  321. *
  322. * @v validator Certificate validator
  323. * @v rc Reason for close
  324. */
  325. static void validator_xfer_close ( struct validator *validator, int rc ) {
  326. /* Close data transfer interface */
  327. intf_restart ( &validator->xfer, rc );
  328. /* Check for errors */
  329. if ( rc != 0 ) {
  330. DBGC ( validator, "VALIDATOR %p transfer failed: %s\n",
  331. validator, strerror ( rc ) );
  332. goto err_transfer;
  333. }
  334. DBGC2 ( validator, "VALIDATOR %p transfer complete\n", validator );
  335. /* Process completed download */
  336. assert ( validator->done != NULL );
  337. if ( ( rc = validator->done ( validator, validator->buffer.data,
  338. validator->buffer.len ) ) != 0 )
  339. goto err_append;
  340. /* Free downloaded data */
  341. xferbuf_free ( &validator->buffer );
  342. /* Resume validation process */
  343. process_add ( &validator->process );
  344. return;
  345. err_append:
  346. err_transfer:
  347. validator_finished ( validator, rc );
  348. }
  349. /**
  350. * Receive data
  351. *
  352. * @v validator Certificate validator
  353. * @v iobuf I/O buffer
  354. * @v meta Data transfer metadata
  355. * @ret rc Return status code
  356. */
  357. static int validator_xfer_deliver ( struct validator *validator,
  358. struct io_buffer *iobuf,
  359. struct xfer_metadata *meta ) {
  360. int rc;
  361. /* Add data to buffer */
  362. if ( ( rc = xferbuf_deliver ( &validator->buffer, iob_disown ( iobuf ),
  363. meta ) ) != 0 ) {
  364. DBGC ( validator, "VALIDATOR %p could not receive data: %s\n",
  365. validator, strerror ( rc ) );
  366. validator_finished ( validator, rc );
  367. return rc;
  368. }
  369. return 0;
  370. }
  371. /** Certificate validator data transfer interface operations */
  372. static struct interface_operation validator_xfer_operations[] = {
  373. INTF_OP ( xfer_deliver, struct validator *, validator_xfer_deliver ),
  374. INTF_OP ( intf_close, struct validator *, validator_xfer_close ),
  375. };
  376. /** Certificate validator data transfer interface descriptor */
  377. static struct interface_descriptor validator_xfer_desc =
  378. INTF_DESC ( struct validator, xfer, validator_xfer_operations );
  379. /****************************************************************************
  380. *
  381. * Validation process
  382. *
  383. */
  384. /**
  385. * Certificate validation process
  386. *
  387. * @v validator Certificate validator
  388. */
  389. static void validator_step ( struct validator *validator ) {
  390. struct x509_link *link;
  391. struct x509_certificate *cert;
  392. struct x509_certificate *issuer = NULL;
  393. struct x509_certificate *last;
  394. time_t now;
  395. int rc;
  396. /* Try validating chain. Try even if the chain is incomplete,
  397. * since certificates may already have been validated
  398. * previously.
  399. */
  400. now = time ( NULL );
  401. if ( ( rc = x509_validate_chain ( validator->chain, now, NULL,
  402. NULL ) ) == 0 ) {
  403. validator_finished ( validator, 0 );
  404. return;
  405. }
  406. /* If there is a certificate that could be validated using
  407. * OCSP, try it.
  408. */
  409. list_for_each_entry ( link, &validator->chain->links, list ) {
  410. cert = issuer;
  411. issuer = link->cert;
  412. if ( ! cert )
  413. continue;
  414. if ( ! x509_is_valid ( issuer ) )
  415. continue;
  416. /* The issuer is valid, but this certificate is not
  417. * yet valid. If OCSP is applicable, start it.
  418. */
  419. if ( cert->extensions.auth_info.ocsp.uri.len &&
  420. ( ! cert->extensions.auth_info.ocsp.good ) ) {
  421. /* Start OCSP */
  422. if ( ( rc = validator_start_ocsp ( validator, cert,
  423. issuer ) ) != 0 ) {
  424. validator_finished ( validator, rc );
  425. return;
  426. }
  427. return;
  428. }
  429. /* Otherwise, this is a permanent failure */
  430. validator_finished ( validator, rc );
  431. return;
  432. }
  433. /* If chain ends with a self-issued certificate, then there is
  434. * nothing more to do.
  435. */
  436. last = x509_last ( validator->chain );
  437. if ( asn1_compare ( &last->issuer.raw, &last->subject.raw ) == 0 ) {
  438. validator_finished ( validator, rc );
  439. return;
  440. }
  441. /* Otherwise, try to download a suitable cross-signing
  442. * certificate.
  443. */
  444. if ( ( rc = validator_start_download ( validator,
  445. &last->issuer.raw ) ) != 0 ) {
  446. validator_finished ( validator, rc );
  447. return;
  448. }
  449. }
  450. /** Certificate validator process descriptor */
  451. static struct process_descriptor validator_process_desc =
  452. PROC_DESC_ONCE ( struct validator, process, validator_step );
  453. /****************************************************************************
  454. *
  455. * Instantiator
  456. *
  457. */
  458. /**
  459. * Instantiate a certificate validator
  460. *
  461. * @v job Job control interface
  462. * @v chain X.509 certificate chain
  463. * @ret rc Return status code
  464. */
  465. int create_validator ( struct interface *job, struct x509_chain *chain ) {
  466. struct validator *validator;
  467. int rc;
  468. /* Sanity check */
  469. if ( ! chain ) {
  470. rc = -EINVAL;
  471. goto err_sanity;
  472. }
  473. /* Allocate and initialise structure */
  474. validator = zalloc ( sizeof ( *validator ) );
  475. if ( ! validator ) {
  476. rc = -ENOMEM;
  477. goto err_alloc;
  478. }
  479. ref_init ( &validator->refcnt, validator_free );
  480. intf_init ( &validator->job, &validator_job_desc,
  481. &validator->refcnt );
  482. intf_init ( &validator->xfer, &validator_xfer_desc,
  483. &validator->refcnt );
  484. process_init ( &validator->process, &validator_process_desc,
  485. &validator->refcnt );
  486. validator->chain = x509_chain_get ( chain );
  487. xferbuf_malloc_init ( &validator->buffer );
  488. /* Attach parent interface, mortalise self, and return */
  489. intf_plug_plug ( &validator->job, job );
  490. ref_put ( &validator->refcnt );
  491. DBGC2 ( validator, "VALIDATOR %p validating X509 chain %p\n",
  492. validator, validator->chain );
  493. return 0;
  494. validator_finished ( validator, rc );
  495. ref_put ( &validator->refcnt );
  496. err_alloc:
  497. err_sanity:
  498. return rc;
  499. }