ocsp.c 27KB

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