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.

ocsp.c 29KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976
  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 <stdint.h>
  21. #include <stdlib.h>
  22. #include <stdio.h>
  23. #include <string.h>
  24. #include <errno.h>
  25. #include <ipxe/asn1.h>
  26. #include <ipxe/x509.h>
  27. #include <ipxe/sha1.h>
  28. #include <ipxe/base64.h>
  29. #include <ipxe/uri.h>
  30. #include <ipxe/ocsp.h>
  31. #include <config/crypto.h>
  32. /** @file
  33. *
  34. * Online Certificate Status Protocol
  35. *
  36. */
  37. /* Disambiguate the various error causes */
  38. #define EACCES_CERT_STATUS \
  39. __einfo_error ( EINFO_EACCES_CERT_STATUS )
  40. #define EINFO_EACCES_CERT_STATUS \
  41. __einfo_uniqify ( EINFO_EACCES, 0x01, \
  42. "Certificate status not good" )
  43. #define EACCES_CERT_MISMATCH \
  44. __einfo_error ( EINFO_EACCES_CERT_MISMATCH )
  45. #define EINFO_EACCES_CERT_MISMATCH \
  46. __einfo_uniqify ( EINFO_EACCES, 0x02, \
  47. "Certificate ID mismatch" )
  48. #define EACCES_NON_OCSP_SIGNING \
  49. __einfo_error ( EINFO_EACCES_NON_OCSP_SIGNING )
  50. #define EINFO_EACCES_NON_OCSP_SIGNING \
  51. __einfo_uniqify ( EINFO_EACCES, 0x03, \
  52. "Not an OCSP signing certificate" )
  53. #define EACCES_STALE \
  54. __einfo_error ( EINFO_EACCES_STALE )
  55. #define EINFO_EACCES_STALE \
  56. __einfo_uniqify ( EINFO_EACCES, 0x04, \
  57. "Stale (or premature) OCSP repsonse" )
  58. #define EACCES_NO_RESPONDER \
  59. __einfo_error ( EINFO_EACCES_NO_RESPONDER )
  60. #define EINFO_EACCES_NO_RESPONDER \
  61. __einfo_uniqify ( EINFO_EACCES, 0x05, \
  62. "Missing OCSP responder certificate" )
  63. #define ENOTSUP_RESPONSE_TYPE \
  64. __einfo_error ( EINFO_ENOTSUP_RESPONSE_TYPE )
  65. #define EINFO_ENOTSUP_RESPONSE_TYPE \
  66. __einfo_uniqify ( EINFO_ENOTSUP, 0x01, \
  67. "Unsupported OCSP response type" )
  68. #define ENOTSUP_RESPONDER_ID \
  69. __einfo_error ( EINFO_ENOTSUP_RESPONDER_ID )
  70. #define EINFO_ENOTSUP_RESPONDER_ID \
  71. __einfo_uniqify ( EINFO_ENOTSUP, 0x02, \
  72. "Unsupported OCSP responder ID" )
  73. #define EPROTO_MALFORMED_REQUEST \
  74. __einfo_error ( EINFO_EPROTO_MALFORMED_REQUEST )
  75. #define EINFO_EPROTO_MALFORMED_REQUEST \
  76. __einfo_uniqify ( EINFO_EPROTO, OCSP_STATUS_MALFORMED_REQUEST, \
  77. "Illegal confirmation request" )
  78. #define EPROTO_INTERNAL_ERROR \
  79. __einfo_error ( EINFO_EPROTO_INTERNAL_ERROR )
  80. #define EINFO_EPROTO_INTERNAL_ERROR \
  81. __einfo_uniqify ( EINFO_EPROTO, OCSP_STATUS_INTERNAL_ERROR, \
  82. "Internal error in issuer" )
  83. #define EPROTO_TRY_LATER \
  84. __einfo_error ( EINFO_EPROTO_TRY_LATER )
  85. #define EINFO_EPROTO_TRY_LATER \
  86. __einfo_uniqify ( EINFO_EPROTO, OCSP_STATUS_TRY_LATER, \
  87. "Try again later" )
  88. #define EPROTO_SIG_REQUIRED \
  89. __einfo_error ( EINFO_EPROTO_SIG_REQUIRED )
  90. #define EINFO_EPROTO_SIG_REQUIRED \
  91. __einfo_uniqify ( EINFO_EPROTO, OCSP_STATUS_SIG_REQUIRED, \
  92. "Must sign the request" )
  93. #define EPROTO_UNAUTHORIZED \
  94. __einfo_error ( EINFO_EPROTO_UNAUTHORIZED )
  95. #define EINFO_EPROTO_UNAUTHORIZED \
  96. __einfo_uniqify ( EINFO_EPROTO, OCSP_STATUS_UNAUTHORIZED, \
  97. "Request unauthorized" )
  98. #define EPROTO_STATUS( status ) \
  99. EUNIQ ( EINFO_EPROTO, (status), EPROTO_MALFORMED_REQUEST, \
  100. EPROTO_INTERNAL_ERROR, EPROTO_TRY_LATER, \
  101. EPROTO_SIG_REQUIRED, EPROTO_UNAUTHORIZED )
  102. /** OCSP digest algorithm */
  103. #define ocsp_digest_algorithm sha1_algorithm
  104. /** OCSP digest algorithm identifier */
  105. static const uint8_t ocsp_algorithm_id[] =
  106. { OCSP_ALGORITHM_IDENTIFIER ( ASN1_OID_SHA1 ) };
  107. /** OCSP basic response type */
  108. static const uint8_t oid_basic_response_type[] = { ASN1_OID_OCSP_BASIC };
  109. /** OCSP basic response type cursor */
  110. static struct asn1_cursor oid_basic_response_type_cursor =
  111. ASN1_OID_CURSOR ( oid_basic_response_type );
  112. /**
  113. * Free OCSP check
  114. *
  115. * @v refcnt Reference count
  116. */
  117. static void ocsp_free ( struct refcnt *refcnt ) {
  118. struct ocsp_check *ocsp =
  119. container_of ( refcnt, struct ocsp_check, refcnt );
  120. x509_put ( ocsp->cert );
  121. x509_put ( ocsp->issuer );
  122. free ( ocsp->uri_string );
  123. free ( ocsp->request.builder.data );
  124. free ( ocsp->response.data );
  125. x509_put ( ocsp->response.signer );
  126. free ( ocsp );
  127. }
  128. /**
  129. * Build OCSP request
  130. *
  131. * @v ocsp OCSP check
  132. * @ret rc Return status code
  133. */
  134. static int ocsp_request ( struct ocsp_check *ocsp ) {
  135. struct digest_algorithm *digest = &ocsp_digest_algorithm;
  136. struct asn1_builder *builder = &ocsp->request.builder;
  137. struct asn1_cursor *cert_id_tail = &ocsp->request.cert_id_tail;
  138. uint8_t digest_ctx[digest->ctxsize];
  139. uint8_t name_digest[digest->digestsize];
  140. uint8_t pubkey_digest[digest->digestsize];
  141. int rc;
  142. /* Generate digests */
  143. digest_init ( digest, digest_ctx );
  144. digest_update ( digest, digest_ctx, ocsp->cert->issuer.raw.data,
  145. ocsp->cert->issuer.raw.len );
  146. digest_final ( digest, digest_ctx, name_digest );
  147. digest_init ( digest, digest_ctx );
  148. digest_update ( digest, digest_ctx,
  149. ocsp->issuer->subject.public_key.raw_bits.data,
  150. ocsp->issuer->subject.public_key.raw_bits.len );
  151. digest_final ( digest, digest_ctx, pubkey_digest );
  152. /* Construct request */
  153. if ( ( rc = ( asn1_prepend_raw ( builder, ocsp->cert->serial.raw.data,
  154. ocsp->cert->serial.raw.len ),
  155. asn1_prepend ( builder, ASN1_OCTET_STRING,
  156. pubkey_digest, sizeof ( pubkey_digest ) ),
  157. asn1_prepend ( builder, ASN1_OCTET_STRING,
  158. name_digest, sizeof ( name_digest ) ),
  159. asn1_prepend ( builder, ASN1_SEQUENCE,
  160. ocsp_algorithm_id,
  161. sizeof ( ocsp_algorithm_id ) ),
  162. asn1_wrap ( builder, ASN1_SEQUENCE ),
  163. asn1_wrap ( builder, ASN1_SEQUENCE ),
  164. asn1_wrap ( builder, ASN1_SEQUENCE ),
  165. asn1_wrap ( builder, ASN1_SEQUENCE ),
  166. asn1_wrap ( builder, ASN1_SEQUENCE ) ) ) != 0 ) {
  167. DBGC ( ocsp, "OCSP %p \"%s\" could not build request: %s\n",
  168. ocsp, x509_name ( ocsp->cert ), strerror ( rc ) );
  169. return rc;
  170. }
  171. DBGC2 ( ocsp, "OCSP %p \"%s\" request is:\n",
  172. ocsp, x509_name ( ocsp->cert ) );
  173. DBGC2_HDA ( ocsp, 0, builder->data, builder->len );
  174. /* Parse certificate ID for comparison with response */
  175. cert_id_tail->data = builder->data;
  176. cert_id_tail->len = builder->len;
  177. if ( ( rc = ( asn1_enter ( cert_id_tail, ASN1_SEQUENCE ),
  178. asn1_enter ( cert_id_tail, ASN1_SEQUENCE ),
  179. asn1_enter ( cert_id_tail, ASN1_SEQUENCE ),
  180. asn1_enter ( cert_id_tail, ASN1_SEQUENCE ),
  181. asn1_enter ( cert_id_tail, ASN1_SEQUENCE ),
  182. asn1_skip ( cert_id_tail, ASN1_SEQUENCE ) ) ) != 0 ) {
  183. DBGC ( ocsp, "OCSP %p \"%s\" could not locate certID: %s\n",
  184. ocsp, x509_name ( ocsp->cert ), strerror ( rc ) );
  185. return rc;
  186. }
  187. return 0;
  188. }
  189. /**
  190. * Build OCSP URI string
  191. *
  192. * @v ocsp OCSP check
  193. * @ret rc Return status code
  194. */
  195. static int ocsp_uri_string ( struct ocsp_check *ocsp ) {
  196. struct x509_ocsp_responder *responder =
  197. &ocsp->cert->extensions.auth_info.ocsp;
  198. char *base64;
  199. char *sep;
  200. size_t base64_len;
  201. size_t uri_len;
  202. size_t len;
  203. int rc;
  204. /* Sanity check */
  205. if ( ! responder->uri.len ) {
  206. DBGC ( ocsp, "OCSP %p \"%s\" has no OCSP URI\n",
  207. ocsp, x509_name ( ocsp->cert ) );
  208. rc = -ENOTTY;
  209. goto err_no_uri;
  210. }
  211. /* Calculate base64-encoded request length */
  212. base64_len = ( base64_encoded_len ( ocsp->request.builder.len )
  213. + 1 /* NUL */ );
  214. /* Allocate and construct the base64-encoded request */
  215. base64 = malloc ( base64_len );
  216. if ( ! base64 ) {
  217. rc = -ENOMEM;
  218. goto err_alloc_base64;
  219. }
  220. base64_encode ( ocsp->request.builder.data, ocsp->request.builder.len,
  221. base64, base64_len );
  222. /* Calculate URI-encoded base64-encoded request length */
  223. uri_len = ( uri_encode ( URI_PATH, base64, ( base64_len - 1 /* NUL */ ),
  224. NULL, 0 ) + 1 /* NUL */ );
  225. /* Allocate and construct the URI string */
  226. len = ( responder->uri.len + 1 /* possible "/" */ + uri_len );
  227. ocsp->uri_string = zalloc ( len );
  228. if ( ! ocsp->uri_string ) {
  229. rc = -ENOMEM;
  230. goto err_alloc_uri;
  231. }
  232. memcpy ( ocsp->uri_string, responder->uri.data, responder->uri.len );
  233. sep = &ocsp->uri_string[ responder->uri.len - 1 ];
  234. if ( *sep != '/' )
  235. *(++sep) = '/';
  236. uri_encode ( URI_PATH, base64, base64_len, ( sep + 1 ), uri_len );
  237. DBGC2 ( ocsp, "OCSP %p \"%s\" URI is %s\n",
  238. ocsp, x509_name ( ocsp->cert ), ocsp->uri_string );
  239. /* Success */
  240. rc = 0;
  241. err_alloc_uri:
  242. free ( base64 );
  243. err_alloc_base64:
  244. err_no_uri:
  245. return rc;
  246. }
  247. /**
  248. * Create OCSP check
  249. *
  250. * @v cert Certificate to check
  251. * @v issuer Issuing certificate
  252. * @ret ocsp OCSP check
  253. * @ret rc Return status code
  254. */
  255. int ocsp_check ( struct x509_certificate *cert,
  256. struct x509_certificate *issuer,
  257. struct ocsp_check **ocsp ) {
  258. int rc;
  259. /* Sanity checks */
  260. assert ( cert != NULL );
  261. assert ( issuer != NULL );
  262. assert ( x509_is_valid ( issuer ) );
  263. /* Allocate and initialise check */
  264. *ocsp = zalloc ( sizeof ( **ocsp ) );
  265. if ( ! *ocsp ) {
  266. rc = -ENOMEM;
  267. goto err_alloc;
  268. }
  269. ref_init ( &(*ocsp)->refcnt, ocsp_free );
  270. (*ocsp)->cert = x509_get ( cert );
  271. (*ocsp)->issuer = x509_get ( issuer );
  272. /* Build request */
  273. if ( ( rc = ocsp_request ( *ocsp ) ) != 0 )
  274. goto err_request;
  275. /* Build URI string */
  276. if ( ( rc = ocsp_uri_string ( *ocsp ) ) != 0 )
  277. goto err_uri_string;
  278. return 0;
  279. err_uri_string:
  280. err_request:
  281. ocsp_put ( *ocsp );
  282. err_alloc:
  283. *ocsp = NULL;
  284. return rc;
  285. }
  286. /**
  287. * Parse OCSP response status
  288. *
  289. * @v ocsp OCSP check
  290. * @v raw ASN.1 cursor
  291. * @ret rc Return status code
  292. */
  293. static int ocsp_parse_response_status ( struct ocsp_check *ocsp,
  294. const struct asn1_cursor *raw ) {
  295. struct asn1_cursor cursor;
  296. uint8_t status;
  297. int rc;
  298. /* Enter responseStatus */
  299. memcpy ( &cursor, raw, sizeof ( cursor ) );
  300. if ( ( rc = asn1_enter ( &cursor, ASN1_ENUMERATED ) ) != 0 ) {
  301. DBGC ( ocsp, "OCSP %p \"%s\" could not locate responseStatus: "
  302. "%s\n", ocsp, x509_name ( ocsp->cert ), strerror ( rc ));
  303. return rc;
  304. }
  305. /* Extract response status */
  306. if ( cursor.len != sizeof ( status ) ) {
  307. DBGC ( ocsp, "OCSP %p \"%s\" invalid status:\n",
  308. ocsp, x509_name ( ocsp->cert ) );
  309. DBGC_HDA ( ocsp, 0, cursor.data, cursor.len );
  310. return -EINVAL;
  311. }
  312. memcpy ( &status, cursor.data, sizeof ( status ) );
  313. /* Check response status */
  314. if ( status != OCSP_STATUS_SUCCESSFUL ) {
  315. DBGC ( ocsp, "OCSP %p \"%s\" response status %d\n",
  316. ocsp, x509_name ( ocsp->cert ), status );
  317. return EPROTO_STATUS ( status );
  318. }
  319. return 0;
  320. }
  321. /**
  322. * Parse OCSP response type
  323. *
  324. * @v ocsp OCSP check
  325. * @v raw ASN.1 cursor
  326. * @ret rc Return status code
  327. */
  328. static int ocsp_parse_response_type ( struct ocsp_check *ocsp,
  329. const struct asn1_cursor *raw ) {
  330. struct asn1_cursor cursor;
  331. /* Enter responseType */
  332. memcpy ( &cursor, raw, sizeof ( cursor ) );
  333. asn1_enter ( &cursor, ASN1_OID );
  334. /* Check responseType is "basic" */
  335. if ( asn1_compare ( &oid_basic_response_type_cursor, &cursor ) != 0 ) {
  336. DBGC ( ocsp, "OCSP %p \"%s\" response type not supported:\n",
  337. ocsp, x509_name ( ocsp->cert ) );
  338. DBGC_HDA ( ocsp, 0, cursor.data, cursor.len );
  339. return -ENOTSUP_RESPONSE_TYPE;
  340. }
  341. return 0;
  342. }
  343. /**
  344. * Compare responder's certificate name
  345. *
  346. * @v ocsp OCSP check
  347. * @v cert Certificate
  348. * @ret difference Difference as returned by memcmp()
  349. */
  350. static int ocsp_compare_responder_name ( struct ocsp_check *ocsp,
  351. struct x509_certificate *cert ) {
  352. struct ocsp_responder *responder = &ocsp->response.responder;
  353. /* Compare responder ID with certificate's subject */
  354. return asn1_compare ( &responder->id, &cert->subject.raw );
  355. }
  356. /**
  357. * Compare responder's certificate public key hash
  358. *
  359. * @v ocsp OCSP check
  360. * @v cert Certificate
  361. * @ret difference Difference as returned by memcmp()
  362. */
  363. static int ocsp_compare_responder_key_hash ( struct ocsp_check *ocsp,
  364. struct x509_certificate *cert ) {
  365. struct ocsp_responder *responder = &ocsp->response.responder;
  366. struct asn1_cursor key_hash;
  367. uint8_t ctx[SHA1_CTX_SIZE];
  368. uint8_t digest[SHA1_DIGEST_SIZE];
  369. int difference;
  370. /* Enter responder key hash */
  371. memcpy ( &key_hash, &responder->id, sizeof ( key_hash ) );
  372. asn1_enter ( &key_hash, ASN1_OCTET_STRING );
  373. /* Sanity check */
  374. difference = ( sizeof ( digest ) - key_hash.len );
  375. if ( difference )
  376. return difference;
  377. /* Generate SHA1 hash of certificate's public key */
  378. digest_init ( &sha1_algorithm, ctx );
  379. digest_update ( &sha1_algorithm, ctx,
  380. cert->subject.public_key.raw_bits.data,
  381. cert->subject.public_key.raw_bits.len );
  382. digest_final ( &sha1_algorithm, ctx, digest );
  383. /* Compare responder key hash with hash of certificate's public key */
  384. return memcmp ( digest, key_hash.data, sizeof ( digest ) );
  385. }
  386. /**
  387. * Parse OCSP responder ID
  388. *
  389. * @v ocsp OCSP check
  390. * @v raw ASN.1 cursor
  391. * @ret rc Return status code
  392. */
  393. static int ocsp_parse_responder_id ( struct ocsp_check *ocsp,
  394. const struct asn1_cursor *raw ) {
  395. struct ocsp_responder *responder = &ocsp->response.responder;
  396. struct asn1_cursor *responder_id = &responder->id;
  397. unsigned int type;
  398. /* Enter responder ID */
  399. memcpy ( responder_id, raw, sizeof ( *responder_id ) );
  400. type = asn1_type ( responder_id );
  401. asn1_enter_any ( responder_id );
  402. /* Identify responder ID type */
  403. switch ( type ) {
  404. case ASN1_EXPLICIT_TAG ( 1 ) :
  405. DBGC2 ( ocsp, "OCSP %p \"%s\" responder identified by name\n",
  406. ocsp, x509_name ( ocsp->cert ) );
  407. responder->compare = ocsp_compare_responder_name;
  408. return 0;
  409. case ASN1_EXPLICIT_TAG ( 2 ) :
  410. DBGC2 ( ocsp, "OCSP %p \"%s\" responder identified by key "
  411. "hash\n", ocsp, x509_name ( ocsp->cert ) );
  412. responder->compare = ocsp_compare_responder_key_hash;
  413. return 0;
  414. default:
  415. DBGC ( ocsp, "OCSP %p \"%s\" unsupported responder ID type "
  416. "%d\n", ocsp, x509_name ( ocsp->cert ), type );
  417. return -ENOTSUP_RESPONDER_ID;
  418. }
  419. }
  420. /**
  421. * Parse OCSP certificate ID
  422. *
  423. * @v ocsp OCSP check
  424. * @v raw ASN.1 cursor
  425. * @ret rc Return status code
  426. */
  427. static int ocsp_parse_cert_id ( struct ocsp_check *ocsp,
  428. const struct asn1_cursor *raw ) {
  429. struct asn1_cursor cursor;
  430. struct asn1_algorithm *algorithm;
  431. int rc;
  432. /* Check certID algorithm */
  433. memcpy ( &cursor, raw, sizeof ( cursor ) );
  434. asn1_enter ( &cursor, ASN1_SEQUENCE );
  435. if ( ( rc = asn1_digest_algorithm ( &cursor, &algorithm ) ) != 0 ) {
  436. DBGC ( ocsp, "OCSP %p \"%s\" certID unknown algorithm: %s\n",
  437. ocsp, x509_name ( ocsp->cert ), strerror ( rc ) );
  438. return rc;
  439. }
  440. if ( algorithm->digest != &ocsp_digest_algorithm ) {
  441. DBGC ( ocsp, "OCSP %p \"%s\" certID wrong algorithm %s\n",
  442. ocsp, x509_name ( ocsp->cert ),
  443. algorithm->digest->name );
  444. return -EACCES_CERT_MISMATCH;
  445. }
  446. /* Check remaining certID fields */
  447. asn1_skip ( &cursor, ASN1_SEQUENCE );
  448. if ( asn1_compare ( &cursor, &ocsp->request.cert_id_tail ) != 0 ) {
  449. DBGC ( ocsp, "OCSP %p \"%s\" certID mismatch:\n",
  450. ocsp, x509_name ( ocsp->cert ) );
  451. DBGC_HDA ( ocsp, 0, ocsp->request.cert_id_tail.data,
  452. ocsp->request.cert_id_tail.len );
  453. DBGC_HDA ( ocsp, 0, cursor.data, cursor.len );
  454. return -EACCES_CERT_MISMATCH;
  455. }
  456. return 0;
  457. }
  458. /**
  459. * Parse OCSP responses
  460. *
  461. * @v ocsp OCSP check
  462. * @v raw ASN.1 cursor
  463. * @ret rc Return status code
  464. */
  465. static int ocsp_parse_responses ( struct ocsp_check *ocsp,
  466. const struct asn1_cursor *raw ) {
  467. struct ocsp_response *response = &ocsp->response;
  468. struct asn1_cursor cursor;
  469. int rc;
  470. /* Enter responses */
  471. memcpy ( &cursor, raw, sizeof ( cursor ) );
  472. asn1_enter ( &cursor, ASN1_SEQUENCE );
  473. /* Enter first singleResponse */
  474. asn1_enter ( &cursor, ASN1_SEQUENCE );
  475. /* Parse certID */
  476. if ( ( rc = ocsp_parse_cert_id ( ocsp, &cursor ) ) != 0 )
  477. return rc;
  478. asn1_skip_any ( &cursor );
  479. /* Check certStatus */
  480. if ( asn1_type ( &cursor ) != ASN1_IMPLICIT_TAG ( 0 ) ) {
  481. DBGC ( ocsp, "OCSP %p \"%s\" non-good certStatus:\n",
  482. ocsp, x509_name ( ocsp->cert ) );
  483. DBGC_HDA ( ocsp, 0, cursor.data, cursor.len );
  484. return -EACCES_CERT_STATUS;
  485. }
  486. asn1_skip_any ( &cursor );
  487. /* Parse thisUpdate */
  488. if ( ( rc = asn1_generalized_time ( &cursor,
  489. &response->this_update ) ) != 0 ) {
  490. DBGC ( ocsp, "OCSP %p \"%s\" could not parse thisUpdate: %s\n",
  491. ocsp, x509_name ( ocsp->cert ), strerror ( rc ) );
  492. return rc;
  493. }
  494. DBGC2 ( ocsp, "OCSP %p \"%s\" this update was at time %lld\n",
  495. ocsp, x509_name ( ocsp->cert ), response->this_update );
  496. asn1_skip_any ( &cursor );
  497. /* Parse nextUpdate, if present */
  498. if ( asn1_type ( &cursor ) == ASN1_EXPLICIT_TAG ( 0 ) ) {
  499. asn1_enter ( &cursor, ASN1_EXPLICIT_TAG ( 0 ) );
  500. if ( ( rc = asn1_generalized_time ( &cursor,
  501. &response->next_update ) ) != 0 ) {
  502. DBGC ( ocsp, "OCSP %p \"%s\" could not parse "
  503. "nextUpdate: %s\n", ocsp,
  504. x509_name ( ocsp->cert ), strerror ( rc ) );
  505. return rc;
  506. }
  507. DBGC2 ( ocsp, "OCSP %p \"%s\" next update is at time %lld\n",
  508. ocsp, x509_name ( ocsp->cert ), response->next_update );
  509. } else {
  510. /* If no nextUpdate is present, this indicates that
  511. * "newer revocation information is available all the
  512. * time". Actually, this indicates that there is no
  513. * point to performing the OCSP check, since an
  514. * attacker could replay the response at any future
  515. * time and it would still be valid.
  516. */
  517. DBGC ( ocsp, "OCSP %p \"%s\" responder is a moron\n",
  518. ocsp, x509_name ( ocsp->cert ) );
  519. response->next_update = time ( NULL );
  520. }
  521. return 0;
  522. }
  523. /**
  524. * Parse OCSP response data
  525. *
  526. * @v ocsp OCSP check
  527. * @v raw ASN.1 cursor
  528. * @ret rc Return status code
  529. */
  530. static int ocsp_parse_tbs_response_data ( struct ocsp_check *ocsp,
  531. const struct asn1_cursor *raw ) {
  532. struct ocsp_response *response = &ocsp->response;
  533. struct asn1_cursor cursor;
  534. int rc;
  535. /* Record raw tbsResponseData */
  536. memcpy ( &cursor, raw, sizeof ( cursor ) );
  537. asn1_shrink_any ( &cursor );
  538. memcpy ( &response->tbs, &cursor, sizeof ( response->tbs ) );
  539. /* Enter tbsResponseData */
  540. asn1_enter ( &cursor, ASN1_SEQUENCE );
  541. /* Skip version, if present */
  542. asn1_skip_if_exists ( &cursor, ASN1_EXPLICIT_TAG ( 0 ) );
  543. /* Parse responderID */
  544. if ( ( rc = ocsp_parse_responder_id ( ocsp, &cursor ) ) != 0 )
  545. return rc;
  546. asn1_skip_any ( &cursor );
  547. /* Skip producedAt */
  548. asn1_skip_any ( &cursor );
  549. /* Parse responses */
  550. if ( ( rc = ocsp_parse_responses ( ocsp, &cursor ) ) != 0 )
  551. return rc;
  552. return 0;
  553. }
  554. /**
  555. * Parse OCSP certificates
  556. *
  557. * @v ocsp OCSP check
  558. * @v raw ASN.1 cursor
  559. * @ret rc Return status code
  560. */
  561. static int ocsp_parse_certs ( struct ocsp_check *ocsp,
  562. const struct asn1_cursor *raw ) {
  563. struct ocsp_response *response = &ocsp->response;
  564. struct asn1_cursor cursor;
  565. struct x509_certificate *cert;
  566. int rc;
  567. /* Enter certs */
  568. memcpy ( &cursor, raw, sizeof ( cursor ) );
  569. asn1_enter ( &cursor, ASN1_EXPLICIT_TAG ( 0 ) );
  570. asn1_enter ( &cursor, ASN1_SEQUENCE );
  571. /* Parse certificate, if present. The data structure permits
  572. * multiple certificates, but the protocol requires that the
  573. * OCSP signing certificate must either be the issuer itself,
  574. * or must be directly issued by the issuer (see RFC2560
  575. * section 4.2.2.2 "Authorized Responders"). We therefore
  576. * need to identify only the single certificate matching the
  577. * Responder ID.
  578. */
  579. while ( cursor.len ) {
  580. /* Parse certificate */
  581. if ( ( rc = x509_certificate ( cursor.data, cursor.len,
  582. &cert ) ) != 0 ) {
  583. DBGC ( ocsp, "OCSP %p \"%s\" could not parse "
  584. "certificate: %s\n", ocsp,
  585. x509_name ( ocsp->cert ), strerror ( rc ) );
  586. DBGC_HDA ( ocsp, 0, cursor.data, cursor.len );
  587. return rc;
  588. }
  589. /* Use if this certificate matches the responder ID */
  590. if ( response->responder.compare ( ocsp, cert ) == 0 ) {
  591. response->signer = cert;
  592. DBGC2 ( ocsp, "OCSP %p \"%s\" response is signed by ",
  593. ocsp, x509_name ( ocsp->cert ) );
  594. DBGC2 ( ocsp, "\"%s\"\n",
  595. x509_name ( response->signer ) );
  596. return 0;
  597. }
  598. /* Otherwise, discard this certificate */
  599. x509_put ( cert );
  600. asn1_skip_any ( &cursor );
  601. }
  602. DBGC ( ocsp, "OCSP %p \"%s\" missing responder certificate\n",
  603. ocsp, x509_name ( ocsp->cert ) );
  604. return -EACCES_NO_RESPONDER;
  605. }
  606. /**
  607. * Parse OCSP basic response
  608. *
  609. * @v ocsp OCSP check
  610. * @v raw ASN.1 cursor
  611. * @ret rc Return status code
  612. */
  613. static int ocsp_parse_basic_response ( struct ocsp_check *ocsp,
  614. const struct asn1_cursor *raw ) {
  615. struct ocsp_response *response = &ocsp->response;
  616. struct asn1_algorithm **algorithm = &response->algorithm;
  617. struct asn1_bit_string *signature = &response->signature;
  618. struct asn1_cursor cursor;
  619. int rc;
  620. /* Enter BasicOCSPResponse */
  621. memcpy ( &cursor, raw, sizeof ( cursor ) );
  622. asn1_enter ( &cursor, ASN1_SEQUENCE );
  623. /* Parse tbsResponseData */
  624. if ( ( rc = ocsp_parse_tbs_response_data ( ocsp, &cursor ) ) != 0 )
  625. return rc;
  626. asn1_skip_any ( &cursor );
  627. /* Parse signatureAlgorithm */
  628. if ( ( rc = asn1_signature_algorithm ( &cursor, algorithm ) ) != 0 ) {
  629. DBGC ( ocsp, "OCSP %p \"%s\" cannot parse signature "
  630. "algorithm: %s\n",
  631. ocsp, x509_name ( ocsp->cert ), strerror ( rc ) );
  632. return rc;
  633. }
  634. DBGC2 ( ocsp, "OCSP %p \"%s\" signature algorithm is %s\n",
  635. ocsp, x509_name ( ocsp->cert ), (*algorithm)->name );
  636. asn1_skip_any ( &cursor );
  637. /* Parse signature */
  638. if ( ( rc = asn1_integral_bit_string ( &cursor, signature ) ) != 0 ) {
  639. DBGC ( ocsp, "OCSP %p \"%s\" cannot parse signature: %s\n",
  640. ocsp, x509_name ( ocsp->cert ), strerror ( rc ) );
  641. return rc;
  642. }
  643. asn1_skip_any ( &cursor );
  644. /* Parse certs, if present */
  645. if ( ( asn1_type ( &cursor ) == ASN1_EXPLICIT_TAG ( 0 ) ) &&
  646. ( ( rc = ocsp_parse_certs ( ocsp, &cursor ) ) != 0 ) )
  647. return rc;
  648. return 0;
  649. }
  650. /**
  651. * Parse OCSP response bytes
  652. *
  653. * @v ocsp OCSP check
  654. * @v raw ASN.1 cursor
  655. * @ret rc Return status code
  656. */
  657. static int ocsp_parse_response_bytes ( struct ocsp_check *ocsp,
  658. const struct asn1_cursor *raw ) {
  659. struct asn1_cursor cursor;
  660. int rc;
  661. /* Enter responseBytes */
  662. memcpy ( &cursor, raw, sizeof ( cursor ) );
  663. asn1_enter ( &cursor, ASN1_EXPLICIT_TAG ( 0 ) );
  664. asn1_enter ( &cursor, ASN1_SEQUENCE );
  665. /* Parse responseType */
  666. if ( ( rc = ocsp_parse_response_type ( ocsp, &cursor ) ) != 0 )
  667. return rc;
  668. asn1_skip_any ( &cursor );
  669. /* Enter response */
  670. asn1_enter ( &cursor, ASN1_OCTET_STRING );
  671. /* Parse response */
  672. if ( ( rc = ocsp_parse_basic_response ( ocsp, &cursor ) ) != 0 )
  673. return rc;
  674. return 0;
  675. }
  676. /**
  677. * Parse OCSP response
  678. *
  679. * @v ocsp OCSP check
  680. * @v raw ASN.1 cursor
  681. * @ret rc Return status code
  682. */
  683. static int ocsp_parse_response ( struct ocsp_check *ocsp,
  684. const struct asn1_cursor *raw ) {
  685. struct asn1_cursor cursor;
  686. int rc;
  687. /* Enter OCSPResponse */
  688. memcpy ( &cursor, raw, sizeof ( cursor ) );
  689. asn1_enter ( &cursor, ASN1_SEQUENCE );
  690. /* Parse responseStatus */
  691. if ( ( rc = ocsp_parse_response_status ( ocsp, &cursor ) ) != 0 )
  692. return rc;
  693. asn1_skip_any ( &cursor );
  694. /* Parse responseBytes */
  695. if ( ( rc = ocsp_parse_response_bytes ( ocsp, &cursor ) ) != 0 )
  696. return rc;
  697. return 0;
  698. }
  699. /**
  700. * Receive OCSP response
  701. *
  702. * @v ocsp OCSP check
  703. * @v data Response data
  704. * @v len Length of response data
  705. * @ret rc Return status code
  706. */
  707. int ocsp_response ( struct ocsp_check *ocsp, const void *data, size_t len ) {
  708. struct ocsp_response *response = &ocsp->response;
  709. struct asn1_cursor cursor;
  710. int rc;
  711. /* Duplicate data */
  712. x509_put ( response->signer );
  713. response->signer = NULL;
  714. free ( response->data );
  715. response->data = malloc ( len );
  716. if ( ! response->data )
  717. return -ENOMEM;
  718. memcpy ( response->data, data, len );
  719. cursor.data = response->data;
  720. cursor.len = len;
  721. /* Parse response */
  722. if ( ( rc = ocsp_parse_response ( ocsp, &cursor ) ) != 0 )
  723. return rc;
  724. return 0;
  725. }
  726. /**
  727. * OCSP dummy root certificate store
  728. *
  729. * OCSP validation uses no root certificates, since it takes place
  730. * only when there already exists a validated issuer certificate.
  731. */
  732. static struct x509_root ocsp_root = {
  733. .digest = &ocsp_digest_algorithm,
  734. .count = 0,
  735. .fingerprints = NULL,
  736. };
  737. /**
  738. * Check OCSP response signature
  739. *
  740. * @v ocsp OCSP check
  741. * @v signer Signing certificate
  742. * @ret rc Return status code
  743. */
  744. static int ocsp_check_signature ( struct ocsp_check *ocsp,
  745. struct x509_certificate *signer ) {
  746. struct ocsp_response *response = &ocsp->response;
  747. struct digest_algorithm *digest = response->algorithm->digest;
  748. struct pubkey_algorithm *pubkey = response->algorithm->pubkey;
  749. struct x509_public_key *public_key = &signer->subject.public_key;
  750. uint8_t digest_ctx[ digest->ctxsize ];
  751. uint8_t digest_out[ digest->digestsize ];
  752. uint8_t pubkey_ctx[ pubkey->ctxsize ];
  753. int rc;
  754. /* Generate digest */
  755. digest_init ( digest, digest_ctx );
  756. digest_update ( digest, digest_ctx, response->tbs.data,
  757. response->tbs.len );
  758. digest_final ( digest, digest_ctx, digest_out );
  759. /* Initialise public-key algorithm */
  760. if ( ( rc = pubkey_init ( pubkey, pubkey_ctx, public_key->raw.data,
  761. public_key->raw.len ) ) != 0 ) {
  762. DBGC ( ocsp, "OCSP %p \"%s\" could not initialise public key: "
  763. "%s\n", ocsp, x509_name ( ocsp->cert ), strerror ( rc ));
  764. goto err_init;
  765. }
  766. /* Verify digest */
  767. if ( ( rc = pubkey_verify ( pubkey, pubkey_ctx, digest, digest_out,
  768. response->signature.data,
  769. response->signature.len ) ) != 0 ) {
  770. DBGC ( ocsp, "OCSP %p \"%s\" signature verification failed: "
  771. "%s\n", ocsp, x509_name ( ocsp->cert ), strerror ( rc ));
  772. goto err_verify;
  773. }
  774. DBGC2 ( ocsp, "OCSP %p \"%s\" signature is correct\n",
  775. ocsp, x509_name ( ocsp->cert ) );
  776. err_verify:
  777. pubkey_final ( pubkey, pubkey_ctx );
  778. err_init:
  779. return rc;
  780. }
  781. /**
  782. * Validate OCSP response
  783. *
  784. * @v ocsp OCSP check
  785. * @v time Time at which to validate response
  786. * @ret rc Return status code
  787. */
  788. int ocsp_validate ( struct ocsp_check *ocsp, time_t time ) {
  789. struct ocsp_response *response = &ocsp->response;
  790. struct x509_certificate *signer;
  791. int rc;
  792. /* Sanity checks */
  793. assert ( response->data != NULL );
  794. /* The response may include a signer certificate; if this is
  795. * not present then the response must have been signed
  796. * directly by the issuer.
  797. */
  798. signer = ( response->signer ? response->signer : ocsp->issuer );
  799. /* Validate signer, if applicable. If the signer is not the
  800. * issuer, then it must be signed directly by the issuer.
  801. */
  802. if ( signer != ocsp->issuer ) {
  803. /* Forcibly invalidate the signer, since we need to
  804. * ensure that it was signed by our issuer (and not
  805. * some other issuer). This prevents a sub-CA's OCSP
  806. * certificate from fraudulently signing OCSP
  807. * responses from the parent CA.
  808. */
  809. x509_invalidate ( signer );
  810. if ( ( rc = x509_validate ( signer, ocsp->issuer, time,
  811. &ocsp_root ) ) != 0 ) {
  812. DBGC ( ocsp, "OCSP %p \"%s\" could not validate ",
  813. ocsp, x509_name ( ocsp->cert ) );
  814. DBGC ( ocsp, "signer \"%s\": %s\n",
  815. x509_name ( signer ), strerror ( rc ) );
  816. return rc;
  817. }
  818. /* If signer is not the issuer, then it must have the
  819. * extendedKeyUsage id-kp-OCSPSigning.
  820. */
  821. if ( ! ( signer->extensions.ext_usage.bits &
  822. X509_OCSP_SIGNING ) ) {
  823. DBGC ( ocsp, "OCSP %p \"%s\" ",
  824. ocsp, x509_name ( ocsp->cert ) );
  825. DBGC ( ocsp, "signer \"%s\" is not an OCSP-signing "
  826. "certificate\n", x509_name ( signer ) );
  827. return -EACCES_NON_OCSP_SIGNING;
  828. }
  829. }
  830. /* Check OCSP response signature */
  831. if ( ( rc = ocsp_check_signature ( ocsp, signer ) ) != 0 )
  832. return rc;
  833. /* Check OCSP response is valid at the specified time
  834. * (allowing for some margin of error).
  835. */
  836. if ( response->this_update > ( time + TIMESTAMP_ERROR_MARGIN ) ) {
  837. DBGC ( ocsp, "OCSP %p \"%s\" response is not yet valid (at "
  838. "time %lld)\n", ocsp, x509_name ( ocsp->cert ), time );
  839. return -EACCES_STALE;
  840. }
  841. if ( response->next_update < ( time - TIMESTAMP_ERROR_MARGIN ) ) {
  842. DBGC ( ocsp, "OCSP %p \"%s\" response is stale (at time "
  843. "%lld)\n", ocsp, x509_name ( ocsp->cert ), time );
  844. return -EACCES_STALE;
  845. }
  846. DBGC2 ( ocsp, "OCSP %p \"%s\" response is valid (at time %lld)\n",
  847. ocsp, x509_name ( ocsp->cert ), time );
  848. /* Mark certificate as passing OCSP verification */
  849. ocsp->cert->extensions.auth_info.ocsp.good = 1;
  850. /* Validate certificate against issuer */
  851. if ( ( rc = x509_validate ( ocsp->cert, ocsp->issuer, time,
  852. &ocsp_root ) ) != 0 ) {
  853. DBGC ( ocsp, "OCSP %p \"%s\" could not validate certificate: "
  854. "%s\n", ocsp, x509_name ( ocsp->cert ), strerror ( rc ));
  855. return rc;
  856. }
  857. DBGC ( ocsp, "OCSP %p \"%s\" successfully validated ",
  858. ocsp, x509_name ( ocsp->cert ) );
  859. DBGC ( ocsp, "using \"%s\"\n", x509_name ( signer ) );
  860. return 0;
  861. }