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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960
  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 = &ocsp->request.cert_id;
  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->data = builder->data;
  176. cert_id->len = builder->len;
  177. if ( ( rc = ( asn1_enter ( cert_id, ASN1_SEQUENCE ),
  178. asn1_enter ( cert_id, ASN1_SEQUENCE ),
  179. asn1_enter ( cert_id, ASN1_SEQUENCE ),
  180. asn1_enter ( cert_id, ASN1_SEQUENCE ) ) ) != 0 ) {
  181. DBGC ( ocsp, "OCSP %p \"%s\" could not locate certID: %s\n",
  182. ocsp, x509_name ( ocsp->cert ), strerror ( rc ) );
  183. return rc;
  184. }
  185. return 0;
  186. }
  187. /**
  188. * Build OCSP URI string
  189. *
  190. * @v ocsp OCSP check
  191. * @ret rc Return status code
  192. */
  193. static int ocsp_uri_string ( struct ocsp_check *ocsp ) {
  194. struct x509_ocsp_responder *responder =
  195. &ocsp->cert->extensions.auth_info.ocsp;
  196. struct uri path_uri;
  197. char *path_base64_string;
  198. char *path_uri_string;
  199. size_t path_len;
  200. size_t len;
  201. int rc;
  202. /* Sanity check */
  203. if ( ! responder->uri.len ) {
  204. DBGC ( ocsp, "OCSP %p \"%s\" has no OCSP URI\n",
  205. ocsp, x509_name ( ocsp->cert ) );
  206. rc = -ENOTTY;
  207. goto err_no_uri;
  208. }
  209. /* Base64-encode the request as the URI path */
  210. path_len = ( base64_encoded_len ( ocsp->request.builder.len )
  211. + 1 /* NUL */ );
  212. path_base64_string = malloc ( path_len );
  213. if ( ! path_base64_string ) {
  214. rc = -ENOMEM;
  215. goto err_path_base64;
  216. }
  217. base64_encode ( ocsp->request.builder.data, ocsp->request.builder.len,
  218. path_base64_string, path_len );
  219. /* URI-encode the Base64-encoded request */
  220. memset ( &path_uri, 0, sizeof ( path_uri ) );
  221. path_uri.path = path_base64_string;
  222. path_uri_string = format_uri_alloc ( &path_uri );
  223. if ( ! path_uri_string ) {
  224. rc = -ENOMEM;
  225. goto err_path_uri;
  226. }
  227. /* Construct URI string */
  228. len = ( responder->uri.len + strlen ( path_uri_string ) + 1 /* NUL */ );
  229. ocsp->uri_string = zalloc ( len );
  230. if ( ! ocsp->uri_string ) {
  231. rc = -ENOMEM;
  232. goto err_ocsp_uri;
  233. }
  234. memcpy ( ocsp->uri_string, responder->uri.data, responder->uri.len );
  235. strcpy ( &ocsp->uri_string[responder->uri.len], path_uri_string );
  236. DBGC2 ( ocsp, "OCSP %p \"%s\" URI is %s\n",
  237. ocsp, x509_name ( ocsp->cert ), ocsp->uri_string );
  238. /* Success */
  239. rc = 0;
  240. err_ocsp_uri:
  241. free ( path_uri_string );
  242. err_path_uri:
  243. free ( path_base64_string );
  244. err_path_base64:
  245. err_no_uri:
  246. return rc;
  247. }
  248. /**
  249. * Create OCSP check
  250. *
  251. * @v cert Certificate to check
  252. * @v issuer Issuing certificate
  253. * @ret ocsp OCSP check
  254. * @ret rc Return status code
  255. */
  256. int ocsp_check ( struct x509_certificate *cert,
  257. struct x509_certificate *issuer,
  258. struct ocsp_check **ocsp ) {
  259. int rc;
  260. /* Sanity checks */
  261. assert ( cert != NULL );
  262. assert ( issuer != NULL );
  263. assert ( issuer->valid );
  264. /* Allocate and initialise check */
  265. *ocsp = zalloc ( sizeof ( **ocsp ) );
  266. if ( ! *ocsp ) {
  267. rc = -ENOMEM;
  268. goto err_alloc;
  269. }
  270. ref_init ( &(*ocsp)->refcnt, ocsp_free );
  271. (*ocsp)->cert = x509_get ( cert );
  272. (*ocsp)->issuer = x509_get ( issuer );
  273. /* Build request */
  274. if ( ( rc = ocsp_request ( *ocsp ) ) != 0 )
  275. goto err_request;
  276. /* Build URI string */
  277. if ( ( rc = ocsp_uri_string ( *ocsp ) ) != 0 )
  278. goto err_uri_string;
  279. return 0;
  280. err_uri_string:
  281. err_request:
  282. ocsp_put ( *ocsp );
  283. err_alloc:
  284. *ocsp = NULL;
  285. return rc;
  286. }
  287. /**
  288. * Parse OCSP response status
  289. *
  290. * @v ocsp OCSP check
  291. * @v raw ASN.1 cursor
  292. * @ret rc Return status code
  293. */
  294. static int ocsp_parse_response_status ( struct ocsp_check *ocsp,
  295. const struct asn1_cursor *raw ) {
  296. struct asn1_cursor cursor;
  297. uint8_t status;
  298. int rc;
  299. /* Enter responseStatus */
  300. memcpy ( &cursor, raw, sizeof ( cursor ) );
  301. if ( ( rc = asn1_enter ( &cursor, ASN1_ENUMERATED ) ) != 0 ) {
  302. DBGC ( ocsp, "OCSP %p \"%s\" could not locate responseStatus: "
  303. "%s\n", ocsp, x509_name ( ocsp->cert ), strerror ( rc ));
  304. return rc;
  305. }
  306. /* Extract response status */
  307. if ( cursor.len != sizeof ( status ) ) {
  308. DBGC ( ocsp, "OCSP %p \"%s\" invalid status:\n",
  309. ocsp, x509_name ( ocsp->cert ) );
  310. DBGC_HDA ( ocsp, 0, cursor.data, cursor.len );
  311. return -EINVAL;
  312. }
  313. memcpy ( &status, cursor.data, sizeof ( status ) );
  314. /* Check response status */
  315. if ( status != OCSP_STATUS_SUCCESSFUL ) {
  316. DBGC ( ocsp, "OCSP %p \"%s\" response status %d\n",
  317. ocsp, x509_name ( ocsp->cert ), status );
  318. return EPROTO_STATUS ( status );
  319. }
  320. return 0;
  321. }
  322. /**
  323. * Parse OCSP response type
  324. *
  325. * @v ocsp OCSP check
  326. * @v raw ASN.1 cursor
  327. * @ret rc Return status code
  328. */
  329. static int ocsp_parse_response_type ( struct ocsp_check *ocsp,
  330. const struct asn1_cursor *raw ) {
  331. struct asn1_cursor cursor;
  332. /* Enter responseType */
  333. memcpy ( &cursor, raw, sizeof ( cursor ) );
  334. asn1_enter ( &cursor, ASN1_OID );
  335. /* Check responseType is "basic" */
  336. if ( asn1_compare ( &oid_basic_response_type_cursor, &cursor ) != 0 ) {
  337. DBGC ( ocsp, "OCSP %p \"%s\" response type not supported:\n",
  338. ocsp, x509_name ( ocsp->cert ) );
  339. DBGC_HDA ( ocsp, 0, cursor.data, cursor.len );
  340. return -ENOTSUP_RESPONSE_TYPE;
  341. }
  342. return 0;
  343. }
  344. /**
  345. * Compare responder's certificate name
  346. *
  347. * @v ocsp OCSP check
  348. * @v cert Certificate
  349. * @ret difference Difference as returned by memcmp()
  350. */
  351. static int ocsp_compare_responder_name ( struct ocsp_check *ocsp,
  352. struct x509_certificate *cert ) {
  353. struct ocsp_responder *responder = &ocsp->response.responder;
  354. /* Compare responder ID with certificate's subject */
  355. return asn1_compare ( &responder->id, &cert->subject.raw );
  356. }
  357. /**
  358. * Compare responder's certificate public key hash
  359. *
  360. * @v ocsp OCSP check
  361. * @v cert Certificate
  362. * @ret difference Difference as returned by memcmp()
  363. */
  364. static int ocsp_compare_responder_key_hash ( struct ocsp_check *ocsp,
  365. struct x509_certificate *cert ) {
  366. struct ocsp_responder *responder = &ocsp->response.responder;
  367. struct asn1_cursor key_hash;
  368. uint8_t ctx[SHA1_CTX_SIZE];
  369. uint8_t digest[SHA1_DIGEST_SIZE];
  370. int difference;
  371. /* Enter responder key hash */
  372. memcpy ( &key_hash, &responder->id, sizeof ( key_hash ) );
  373. asn1_enter ( &key_hash, ASN1_OCTET_STRING );
  374. /* Sanity check */
  375. difference = ( sizeof ( digest ) - key_hash.len );
  376. if ( difference )
  377. return difference;
  378. /* Generate SHA1 hash of certificate's public key */
  379. digest_init ( &sha1_algorithm, ctx );
  380. digest_update ( &sha1_algorithm, ctx,
  381. cert->subject.public_key.raw_bits.data,
  382. cert->subject.public_key.raw_bits.len );
  383. digest_final ( &sha1_algorithm, ctx, digest );
  384. /* Compare responder key hash with hash of certificate's public key */
  385. return memcmp ( digest, key_hash.data, sizeof ( digest ) );
  386. }
  387. /**
  388. * Parse OCSP responder ID
  389. *
  390. * @v ocsp OCSP check
  391. * @v raw ASN.1 cursor
  392. * @ret rc Return status code
  393. */
  394. static int ocsp_parse_responder_id ( struct ocsp_check *ocsp,
  395. const struct asn1_cursor *raw ) {
  396. struct ocsp_responder *responder = &ocsp->response.responder;
  397. struct asn1_cursor *responder_id = &responder->id;
  398. unsigned int type;
  399. /* Enter responder ID */
  400. memcpy ( responder_id, raw, sizeof ( *responder_id ) );
  401. type = asn1_type ( responder_id );
  402. asn1_enter_any ( responder_id );
  403. /* Identify responder ID type */
  404. switch ( type ) {
  405. case ASN1_EXPLICIT_TAG ( 1 ) :
  406. DBGC2 ( ocsp, "OCSP %p \"%s\" responder identified by name\n",
  407. ocsp, x509_name ( ocsp->cert ) );
  408. responder->compare = ocsp_compare_responder_name;
  409. return 0;
  410. case ASN1_EXPLICIT_TAG ( 2 ) :
  411. DBGC2 ( ocsp, "OCSP %p \"%s\" responder identified by key "
  412. "hash\n", ocsp, x509_name ( ocsp->cert ) );
  413. responder->compare = ocsp_compare_responder_key_hash;
  414. return 0;
  415. default:
  416. DBGC ( ocsp, "OCSP %p \"%s\" unsupported responder ID type "
  417. "%d\n", ocsp, x509_name ( ocsp->cert ), type );
  418. return -ENOTSUP_RESPONDER_ID;
  419. }
  420. }
  421. /**
  422. * Parse OCSP certificate ID
  423. *
  424. * @v ocsp OCSP check
  425. * @v raw ASN.1 cursor
  426. * @ret rc Return status code
  427. */
  428. static int ocsp_parse_cert_id ( struct ocsp_check *ocsp,
  429. const struct asn1_cursor *raw ) {
  430. struct asn1_cursor cursor;
  431. /* Check certID matches request */
  432. memcpy ( &cursor, raw, sizeof ( cursor ) );
  433. asn1_shrink_any ( &cursor );
  434. if ( asn1_compare ( &cursor, &ocsp->request.cert_id ) != 0 ) {
  435. DBGC ( ocsp, "OCSP %p \"%s\" certID mismatch:\n",
  436. ocsp, x509_name ( ocsp->cert ) );
  437. DBGC_HDA ( ocsp, 0, ocsp->request.cert_id.data,
  438. ocsp->request.cert_id.len );
  439. DBGC_HDA ( ocsp, 0, cursor.data, cursor.len );
  440. return -EACCES_CERT_MISMATCH;
  441. }
  442. return 0;
  443. }
  444. /**
  445. * Parse OCSP responses
  446. *
  447. * @v ocsp OCSP check
  448. * @v raw ASN.1 cursor
  449. * @ret rc Return status code
  450. */
  451. static int ocsp_parse_responses ( struct ocsp_check *ocsp,
  452. const struct asn1_cursor *raw ) {
  453. struct ocsp_response *response = &ocsp->response;
  454. struct asn1_cursor cursor;
  455. int rc;
  456. /* Enter responses */
  457. memcpy ( &cursor, raw, sizeof ( cursor ) );
  458. asn1_enter ( &cursor, ASN1_SEQUENCE );
  459. /* Enter first singleResponse */
  460. asn1_enter ( &cursor, ASN1_SEQUENCE );
  461. /* Parse certID */
  462. if ( ( rc = ocsp_parse_cert_id ( ocsp, &cursor ) ) != 0 )
  463. return rc;
  464. asn1_skip_any ( &cursor );
  465. /* Check certStatus */
  466. if ( asn1_type ( &cursor ) != ASN1_IMPLICIT_TAG ( 0 ) ) {
  467. DBGC ( ocsp, "OCSP %p \"%s\" non-good certStatus:\n",
  468. ocsp, x509_name ( ocsp->cert ) );
  469. DBGC_HDA ( ocsp, 0, cursor.data, cursor.len );
  470. return -EACCES_CERT_STATUS;
  471. }
  472. asn1_skip_any ( &cursor );
  473. /* Parse thisUpdate */
  474. if ( ( rc = asn1_generalized_time ( &cursor,
  475. &response->this_update ) ) != 0 ) {
  476. DBGC ( ocsp, "OCSP %p \"%s\" could not parse thisUpdate: %s\n",
  477. ocsp, x509_name ( ocsp->cert ), strerror ( rc ) );
  478. return rc;
  479. }
  480. DBGC2 ( ocsp, "OCSP %p \"%s\" this update was at time %lld\n",
  481. ocsp, x509_name ( ocsp->cert ), response->this_update );
  482. asn1_skip_any ( &cursor );
  483. /* Parse nextUpdate, if present */
  484. if ( asn1_type ( &cursor ) == ASN1_EXPLICIT_TAG ( 0 ) ) {
  485. asn1_enter ( &cursor, ASN1_EXPLICIT_TAG ( 0 ) );
  486. if ( ( rc = asn1_generalized_time ( &cursor,
  487. &response->next_update ) ) != 0 ) {
  488. DBGC ( ocsp, "OCSP %p \"%s\" could not parse "
  489. "nextUpdate: %s\n", ocsp,
  490. x509_name ( ocsp->cert ), strerror ( rc ) );
  491. return rc;
  492. }
  493. DBGC2 ( ocsp, "OCSP %p \"%s\" next update is at time %lld\n",
  494. ocsp, x509_name ( ocsp->cert ), response->next_update );
  495. } else {
  496. /* If no nextUpdate is present, this indicates that
  497. * "newer revocation information is available all the
  498. * time". Actually, this indicates that there is no
  499. * point to performing the OCSP check, since an
  500. * attacker could replay the response at any future
  501. * time and it would still be valid.
  502. */
  503. DBGC ( ocsp, "OCSP %p \"%s\" responder is a moron\n",
  504. ocsp, x509_name ( ocsp->cert ) );
  505. response->next_update = time ( NULL );
  506. }
  507. return 0;
  508. }
  509. /**
  510. * Parse OCSP response data
  511. *
  512. * @v ocsp OCSP check
  513. * @v raw ASN.1 cursor
  514. * @ret rc Return status code
  515. */
  516. static int ocsp_parse_tbs_response_data ( struct ocsp_check *ocsp,
  517. const struct asn1_cursor *raw ) {
  518. struct ocsp_response *response = &ocsp->response;
  519. struct asn1_cursor cursor;
  520. int rc;
  521. /* Record raw tbsResponseData */
  522. memcpy ( &cursor, raw, sizeof ( cursor ) );
  523. asn1_shrink_any ( &cursor );
  524. memcpy ( &response->tbs, &cursor, sizeof ( response->tbs ) );
  525. /* Enter tbsResponseData */
  526. asn1_enter ( &cursor, ASN1_SEQUENCE );
  527. /* Skip version, if present */
  528. asn1_skip_if_exists ( &cursor, ASN1_EXPLICIT_TAG ( 0 ) );
  529. /* Parse responderID */
  530. if ( ( rc = ocsp_parse_responder_id ( ocsp, &cursor ) ) != 0 )
  531. return rc;
  532. asn1_skip_any ( &cursor );
  533. /* Skip producedAt */
  534. asn1_skip_any ( &cursor );
  535. /* Parse responses */
  536. if ( ( rc = ocsp_parse_responses ( ocsp, &cursor ) ) != 0 )
  537. return rc;
  538. return 0;
  539. }
  540. /**
  541. * Parse OCSP certificates
  542. *
  543. * @v ocsp OCSP check
  544. * @v raw ASN.1 cursor
  545. * @ret rc Return status code
  546. */
  547. static int ocsp_parse_certs ( struct ocsp_check *ocsp,
  548. const struct asn1_cursor *raw ) {
  549. struct ocsp_response *response = &ocsp->response;
  550. struct asn1_cursor cursor;
  551. struct x509_certificate *cert;
  552. int rc;
  553. /* Enter certs */
  554. memcpy ( &cursor, raw, sizeof ( cursor ) );
  555. asn1_enter ( &cursor, ASN1_EXPLICIT_TAG ( 0 ) );
  556. asn1_enter ( &cursor, ASN1_SEQUENCE );
  557. /* Parse certificate, if present. The data structure permits
  558. * multiple certificates, but the protocol requires that the
  559. * OCSP signing certificate must either be the issuer itself,
  560. * or must be directly issued by the issuer (see RFC2560
  561. * section 4.2.2.2 "Authorized Responders"). We therefore
  562. * need to identify only the single certificate matching the
  563. * Responder ID.
  564. */
  565. while ( cursor.len ) {
  566. /* Parse certificate */
  567. if ( ( rc = x509_certificate ( cursor.data, cursor.len,
  568. &cert ) ) != 0 ) {
  569. DBGC ( ocsp, "OCSP %p \"%s\" could not parse "
  570. "certificate: %s\n", ocsp,
  571. x509_name ( ocsp->cert ), strerror ( rc ) );
  572. DBGC_HDA ( ocsp, 0, cursor.data, cursor.len );
  573. return rc;
  574. }
  575. /* Use if this certificate matches the responder ID */
  576. if ( response->responder.compare ( ocsp, cert ) == 0 ) {
  577. response->signer = cert;
  578. DBGC2 ( ocsp, "OCSP %p \"%s\" response is signed by ",
  579. ocsp, x509_name ( ocsp->cert ) );
  580. DBGC2 ( ocsp, "\"%s\"\n",
  581. x509_name ( response->signer ) );
  582. return 0;
  583. }
  584. /* Otherwise, discard this certificate */
  585. x509_put ( cert );
  586. asn1_skip_any ( &cursor );
  587. }
  588. DBGC ( ocsp, "OCSP %p \"%s\" missing responder certificate\n",
  589. ocsp, x509_name ( ocsp->cert ) );
  590. return -EACCES_NO_RESPONDER;
  591. }
  592. /**
  593. * Parse OCSP basic response
  594. *
  595. * @v ocsp OCSP check
  596. * @v raw ASN.1 cursor
  597. * @ret rc Return status code
  598. */
  599. static int ocsp_parse_basic_response ( struct ocsp_check *ocsp,
  600. const struct asn1_cursor *raw ) {
  601. struct ocsp_response *response = &ocsp->response;
  602. struct asn1_algorithm **algorithm = &response->algorithm;
  603. struct asn1_bit_string *signature = &response->signature;
  604. struct asn1_cursor cursor;
  605. int rc;
  606. /* Enter BasicOCSPResponse */
  607. memcpy ( &cursor, raw, sizeof ( cursor ) );
  608. asn1_enter ( &cursor, ASN1_SEQUENCE );
  609. /* Parse tbsResponseData */
  610. if ( ( rc = ocsp_parse_tbs_response_data ( ocsp, &cursor ) ) != 0 )
  611. return rc;
  612. asn1_skip_any ( &cursor );
  613. /* Parse signatureAlgorithm */
  614. if ( ( rc = asn1_signature_algorithm ( &cursor, algorithm ) ) != 0 ) {
  615. DBGC ( ocsp, "OCSP %p \"%s\" cannot parse signature "
  616. "algorithm: %s\n",
  617. ocsp, x509_name ( ocsp->cert ), strerror ( rc ) );
  618. return rc;
  619. }
  620. DBGC2 ( ocsp, "OCSP %p \"%s\" signature algorithm is %s\n",
  621. ocsp, x509_name ( ocsp->cert ), (*algorithm)->name );
  622. asn1_skip_any ( &cursor );
  623. /* Parse signature */
  624. if ( ( rc = asn1_integral_bit_string ( &cursor, signature ) ) != 0 ) {
  625. DBGC ( ocsp, "OCSP %p \"%s\" cannot parse signature: %s\n",
  626. ocsp, x509_name ( ocsp->cert ), strerror ( rc ) );
  627. return rc;
  628. }
  629. asn1_skip_any ( &cursor );
  630. /* Parse certs, if present */
  631. if ( ( asn1_type ( &cursor ) == ASN1_EXPLICIT_TAG ( 0 ) ) &&
  632. ( ( rc = ocsp_parse_certs ( ocsp, &cursor ) ) != 0 ) )
  633. return rc;
  634. return 0;
  635. }
  636. /**
  637. * Parse OCSP response bytes
  638. *
  639. * @v ocsp OCSP check
  640. * @v raw ASN.1 cursor
  641. * @ret rc Return status code
  642. */
  643. static int ocsp_parse_response_bytes ( struct ocsp_check *ocsp,
  644. const struct asn1_cursor *raw ) {
  645. struct asn1_cursor cursor;
  646. int rc;
  647. /* Enter responseBytes */
  648. memcpy ( &cursor, raw, sizeof ( cursor ) );
  649. asn1_enter ( &cursor, ASN1_EXPLICIT_TAG ( 0 ) );
  650. asn1_enter ( &cursor, ASN1_SEQUENCE );
  651. /* Parse responseType */
  652. if ( ( rc = ocsp_parse_response_type ( ocsp, &cursor ) ) != 0 )
  653. return rc;
  654. asn1_skip_any ( &cursor );
  655. /* Enter response */
  656. asn1_enter ( &cursor, ASN1_OCTET_STRING );
  657. /* Parse response */
  658. if ( ( rc = ocsp_parse_basic_response ( ocsp, &cursor ) ) != 0 )
  659. return rc;
  660. return 0;
  661. }
  662. /**
  663. * Parse OCSP response
  664. *
  665. * @v ocsp OCSP check
  666. * @v raw ASN.1 cursor
  667. * @ret rc Return status code
  668. */
  669. static int ocsp_parse_response ( struct ocsp_check *ocsp,
  670. const struct asn1_cursor *raw ) {
  671. struct asn1_cursor cursor;
  672. int rc;
  673. /* Enter OCSPResponse */
  674. memcpy ( &cursor, raw, sizeof ( cursor ) );
  675. asn1_enter ( &cursor, ASN1_SEQUENCE );
  676. /* Parse responseStatus */
  677. if ( ( rc = ocsp_parse_response_status ( ocsp, &cursor ) ) != 0 )
  678. return rc;
  679. asn1_skip_any ( &cursor );
  680. /* Parse responseBytes */
  681. if ( ( rc = ocsp_parse_response_bytes ( ocsp, &cursor ) ) != 0 )
  682. return rc;
  683. return 0;
  684. }
  685. /**
  686. * Receive OCSP response
  687. *
  688. * @v ocsp OCSP check
  689. * @v data Response data
  690. * @v len Length of response data
  691. * @ret rc Return status code
  692. */
  693. int ocsp_response ( struct ocsp_check *ocsp, const void *data, size_t len ) {
  694. struct ocsp_response *response = &ocsp->response;
  695. struct asn1_cursor cursor;
  696. int rc;
  697. /* Duplicate data */
  698. x509_put ( response->signer );
  699. response->signer = NULL;
  700. free ( response->data );
  701. response->data = malloc ( len );
  702. if ( ! response->data )
  703. return -ENOMEM;
  704. memcpy ( response->data, data, len );
  705. cursor.data = response->data;
  706. cursor.len = len;
  707. /* Parse response */
  708. if ( ( rc = ocsp_parse_response ( ocsp, &cursor ) ) != 0 )
  709. return rc;
  710. return 0;
  711. }
  712. /**
  713. * OCSP dummy root certificate store
  714. *
  715. * OCSP validation uses no root certificates, since it takes place
  716. * only when there already exists a validated issuer certificate.
  717. */
  718. static struct x509_root ocsp_root = {
  719. .digest = &ocsp_digest_algorithm,
  720. .count = 0,
  721. .fingerprints = NULL,
  722. };
  723. /**
  724. * Check OCSP response signature
  725. *
  726. * @v ocsp OCSP check
  727. * @v signer Signing certificate
  728. * @ret rc Return status code
  729. */
  730. static int ocsp_check_signature ( struct ocsp_check *ocsp,
  731. struct x509_certificate *signer ) {
  732. struct ocsp_response *response = &ocsp->response;
  733. struct digest_algorithm *digest = response->algorithm->digest;
  734. struct pubkey_algorithm *pubkey = response->algorithm->pubkey;
  735. struct x509_public_key *public_key = &signer->subject.public_key;
  736. uint8_t digest_ctx[ digest->ctxsize ];
  737. uint8_t digest_out[ digest->digestsize ];
  738. uint8_t pubkey_ctx[ pubkey->ctxsize ];
  739. int rc;
  740. /* Generate digest */
  741. digest_init ( digest, digest_ctx );
  742. digest_update ( digest, digest_ctx, response->tbs.data,
  743. response->tbs.len );
  744. digest_final ( digest, digest_ctx, digest_out );
  745. /* Initialise public-key algorithm */
  746. if ( ( rc = pubkey_init ( pubkey, pubkey_ctx, public_key->raw.data,
  747. public_key->raw.len ) ) != 0 ) {
  748. DBGC ( ocsp, "OCSP %p \"%s\" could not initialise public key: "
  749. "%s\n", ocsp, x509_name ( ocsp->cert ), strerror ( rc ));
  750. goto err_init;
  751. }
  752. /* Verify digest */
  753. if ( ( rc = pubkey_verify ( pubkey, pubkey_ctx, digest, digest_out,
  754. response->signature.data,
  755. response->signature.len ) ) != 0 ) {
  756. DBGC ( ocsp, "OCSP %p \"%s\" signature verification failed: "
  757. "%s\n", ocsp, x509_name ( ocsp->cert ), strerror ( rc ));
  758. goto err_verify;
  759. }
  760. DBGC2 ( ocsp, "OCSP %p \"%s\" signature is correct\n",
  761. ocsp, x509_name ( ocsp->cert ) );
  762. err_verify:
  763. pubkey_final ( pubkey, pubkey_ctx );
  764. err_init:
  765. return rc;
  766. }
  767. /**
  768. * Validate OCSP response
  769. *
  770. * @v ocsp OCSP check
  771. * @v time Time at which to validate response
  772. * @ret rc Return status code
  773. */
  774. int ocsp_validate ( struct ocsp_check *ocsp, time_t time ) {
  775. struct ocsp_response *response = &ocsp->response;
  776. struct x509_certificate *signer;
  777. int rc;
  778. /* Sanity checks */
  779. assert ( response->data != NULL );
  780. /* The response may include a signer certificate; if this is
  781. * not present then the response must have been signed
  782. * directly by the issuer.
  783. */
  784. signer = ( response->signer ? response->signer : ocsp->issuer );
  785. /* Validate signer, if applicable. If the signer is not the
  786. * issuer, then it must be signed directly by the issuer.
  787. */
  788. if ( signer != ocsp->issuer ) {
  789. /* Forcibly invalidate the signer, since we need to
  790. * ensure that it was signed by our issuer (and not
  791. * some other issuer). This prevents a sub-CA's OCSP
  792. * certificate from fraudulently signing OCSP
  793. * responses from the parent CA.
  794. */
  795. x509_invalidate ( signer );
  796. if ( ( rc = x509_validate ( signer, ocsp->issuer, time,
  797. &ocsp_root ) ) != 0 ) {
  798. DBGC ( ocsp, "OCSP %p \"%s\" could not validate ",
  799. ocsp, x509_name ( ocsp->cert ) );
  800. DBGC ( ocsp, "signer \"%s\": %s\n",
  801. x509_name ( signer ), strerror ( rc ) );
  802. return rc;
  803. }
  804. /* If signer is not the issuer, then it must have the
  805. * extendedKeyUsage id-kp-OCSPSigning.
  806. */
  807. if ( ! ( signer->extensions.ext_usage.bits &
  808. X509_OCSP_SIGNING ) ) {
  809. DBGC ( ocsp, "OCSP %p \"%s\" ",
  810. ocsp, x509_name ( ocsp->cert ) );
  811. DBGC ( ocsp, "signer \"%s\" is not an OCSP-signing "
  812. "certificate\n", x509_name ( signer ) );
  813. return -EACCES_NON_OCSP_SIGNING;
  814. }
  815. }
  816. /* Check OCSP response signature */
  817. if ( ( rc = ocsp_check_signature ( ocsp, signer ) ) != 0 )
  818. return rc;
  819. /* Check OCSP response is valid at the specified time
  820. * (allowing for some margin of error).
  821. */
  822. if ( response->this_update > ( time + TIMESTAMP_ERROR_MARGIN ) ) {
  823. DBGC ( ocsp, "OCSP %p \"%s\" response is not yet valid (at "
  824. "time %lld)\n", ocsp, x509_name ( ocsp->cert ), time );
  825. return -EACCES_STALE;
  826. }
  827. if ( response->next_update < ( time - TIMESTAMP_ERROR_MARGIN ) ) {
  828. DBGC ( ocsp, "OCSP %p \"%s\" response is stale (at time "
  829. "%lld)\n", ocsp, x509_name ( ocsp->cert ), time );
  830. return -EACCES_STALE;
  831. }
  832. DBGC2 ( ocsp, "OCSP %p \"%s\" response is valid (at time %lld)\n",
  833. ocsp, x509_name ( ocsp->cert ), time );
  834. /* Mark certificate as passing OCSP verification */
  835. ocsp->cert->extensions.auth_info.ocsp.good = 1;
  836. /* Validate certificate against issuer */
  837. if ( ( rc = x509_validate ( ocsp->cert, ocsp->issuer, time,
  838. &ocsp_root ) ) != 0 ) {
  839. DBGC ( ocsp, "OCSP %p \"%s\" could not validate certificate: "
  840. "%s\n", ocsp, x509_name ( ocsp->cert ), strerror ( rc ));
  841. return rc;
  842. }
  843. DBGC ( ocsp, "OCSP %p \"%s\" successfully validated ",
  844. ocsp, x509_name ( ocsp->cert ) );
  845. DBGC ( ocsp, "using \"%s\"\n", x509_name ( signer ) );
  846. return 0;
  847. }