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.

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