選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

ocsp.c 27KB

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