ocsp_vfy.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  1. /*
  2. * Copyright 2001-2021 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #include <string.h>
  10. #include <openssl/ocsp.h>
  11. #include <openssl/err.h>
  12. #include "internal/sizes.h"
  13. #include "ocsp_local.h"
  14. static int ocsp_find_signer(X509 **psigner, OCSP_BASICRESP *bs,
  15. STACK_OF(X509) *certs, unsigned long flags);
  16. static X509 *ocsp_find_signer_sk(STACK_OF(X509) *certs, OCSP_RESPID *id);
  17. static int ocsp_check_issuer(OCSP_BASICRESP *bs, STACK_OF(X509) *chain);
  18. static int ocsp_check_ids(STACK_OF(OCSP_SINGLERESP) *sresp,
  19. OCSP_CERTID **ret);
  20. static int ocsp_match_issuerid(X509 *cert, OCSP_CERTID *cid,
  21. STACK_OF(OCSP_SINGLERESP) *sresp);
  22. static int ocsp_check_delegated(X509 *x);
  23. static int ocsp_req_find_signer(X509 **psigner, OCSP_REQUEST *req,
  24. const X509_NAME *nm, STACK_OF(X509) *certs,
  25. unsigned long flags);
  26. /* Returns 1 on success, 0 on failure, or -1 on fatal error */
  27. static int ocsp_verify_signer(X509 *signer, int response,
  28. X509_STORE *st, unsigned long flags,
  29. STACK_OF(X509) *untrusted, STACK_OF(X509) **chain)
  30. {
  31. X509_STORE_CTX *ctx = X509_STORE_CTX_new();
  32. X509_VERIFY_PARAM *vp;
  33. int ret = -1;
  34. if (ctx == NULL) {
  35. ERR_raise(ERR_LIB_OCSP, ERR_R_MALLOC_FAILURE);
  36. goto end;
  37. }
  38. if (!X509_STORE_CTX_init(ctx, st, signer, untrusted)) {
  39. ERR_raise(ERR_LIB_OCSP, ERR_R_X509_LIB);
  40. goto end;
  41. }
  42. if ((vp = X509_STORE_CTX_get0_param(ctx)) == NULL)
  43. goto end;
  44. if ((flags & OCSP_PARTIAL_CHAIN) != 0)
  45. X509_VERIFY_PARAM_set_flags(vp, X509_V_FLAG_PARTIAL_CHAIN);
  46. if (response
  47. && X509_get_ext_by_NID(signer, NID_id_pkix_OCSP_noCheck, -1) >= 0)
  48. /*
  49. * Locally disable revocation status checking for OCSP responder cert.
  50. * Done here for CRLs; should be done also for OCSP-based checks.
  51. */
  52. X509_VERIFY_PARAM_clear_flags(vp, X509_V_FLAG_CRL_CHECK);
  53. X509_STORE_CTX_set_purpose(ctx, X509_PURPOSE_OCSP_HELPER);
  54. X509_STORE_CTX_set_trust(ctx, X509_TRUST_OCSP_REQUEST);
  55. ret = X509_verify_cert(ctx);
  56. if (ret <= 0) {
  57. ret = X509_STORE_CTX_get_error(ctx);
  58. ERR_raise_data(ERR_LIB_OCSP, OCSP_R_CERTIFICATE_VERIFY_ERROR,
  59. "Verify error: %s", X509_verify_cert_error_string(ret));
  60. goto end;
  61. }
  62. if (chain != NULL)
  63. *chain = X509_STORE_CTX_get1_chain(ctx);
  64. end:
  65. X509_STORE_CTX_free(ctx);
  66. return ret;
  67. }
  68. static int ocsp_verify(OCSP_REQUEST *req, OCSP_BASICRESP *bs,
  69. X509 *signer, unsigned long flags)
  70. {
  71. EVP_PKEY *skey;
  72. int ret = 1;
  73. if ((flags & OCSP_NOSIGS) == 0) {
  74. if ((skey = X509_get0_pubkey(signer)) == NULL) {
  75. ERR_raise(ERR_LIB_OCSP, OCSP_R_NO_SIGNER_KEY);
  76. return -1;
  77. }
  78. if (req != NULL)
  79. ret = OCSP_REQUEST_verify(req, skey, signer->libctx, signer->propq);
  80. else
  81. ret = OCSP_BASICRESP_verify(bs, skey, signer->libctx, signer->propq);
  82. if (ret <= 0)
  83. ERR_raise(ERR_LIB_OCSP, OCSP_R_SIGNATURE_FAILURE);
  84. }
  85. return ret;
  86. }
  87. /* Verify a basic response message */
  88. int OCSP_basic_verify(OCSP_BASICRESP *bs, STACK_OF(X509) *certs,
  89. X509_STORE *st, unsigned long flags)
  90. {
  91. X509 *signer, *x;
  92. STACK_OF(X509) *chain = NULL;
  93. STACK_OF(X509) *untrusted = NULL;
  94. int ret = ocsp_find_signer(&signer, bs, certs, flags);
  95. if (ret == 0) {
  96. ERR_raise(ERR_LIB_OCSP, OCSP_R_SIGNER_CERTIFICATE_NOT_FOUND);
  97. goto end;
  98. }
  99. if ((ret == 2) && (flags & OCSP_TRUSTOTHER) != 0)
  100. flags |= OCSP_NOVERIFY;
  101. if ((ret = ocsp_verify(NULL, bs, signer, flags)) <= 0)
  102. goto end;
  103. if ((flags & OCSP_NOVERIFY) == 0) {
  104. ret = -1;
  105. if ((flags & OCSP_NOCHAIN) == 0) {
  106. if ((untrusted = sk_X509_dup(bs->certs)) == NULL)
  107. goto end;
  108. if (!X509_add_certs(untrusted, certs, X509_ADD_FLAG_DEFAULT))
  109. goto end;
  110. }
  111. ret = ocsp_verify_signer(signer, 1, st, flags, untrusted, &chain);
  112. if (ret <= 0)
  113. goto end;
  114. if ((flags & OCSP_NOCHECKS) != 0) {
  115. ret = 1;
  116. goto end;
  117. }
  118. /*
  119. * At this point we have a valid certificate chain need to verify it
  120. * against the OCSP issuer criteria.
  121. */
  122. ret = ocsp_check_issuer(bs, chain);
  123. /* If fatal error or valid match then finish */
  124. if (ret != 0)
  125. goto end;
  126. /*
  127. * Easy case: explicitly trusted. Get root CA and check for explicit
  128. * trust
  129. */
  130. if ((flags & OCSP_NOEXPLICIT) != 0)
  131. goto end;
  132. x = sk_X509_value(chain, sk_X509_num(chain) - 1);
  133. if (X509_check_trust(x, NID_OCSP_sign, 0) != X509_TRUST_TRUSTED) {
  134. ERR_raise(ERR_LIB_OCSP, OCSP_R_ROOT_CA_NOT_TRUSTED);
  135. ret = 0;
  136. goto end;
  137. }
  138. ret = 1;
  139. }
  140. end:
  141. sk_X509_pop_free(chain, X509_free);
  142. sk_X509_free(untrusted);
  143. return ret;
  144. }
  145. int OCSP_resp_get0_signer(OCSP_BASICRESP *bs, X509 **signer,
  146. STACK_OF(X509) *extra_certs)
  147. {
  148. return ocsp_find_signer(signer, bs, extra_certs, 0) > 0;
  149. }
  150. static int ocsp_find_signer(X509 **psigner, OCSP_BASICRESP *bs,
  151. STACK_OF(X509) *certs, unsigned long flags)
  152. {
  153. X509 *signer;
  154. OCSP_RESPID *rid = &bs->tbsResponseData.responderId;
  155. if ((signer = ocsp_find_signer_sk(certs, rid)) != NULL) {
  156. *psigner = signer;
  157. return 2;
  158. }
  159. if ((flags & OCSP_NOINTERN) == 0 &&
  160. (signer = ocsp_find_signer_sk(bs->certs, rid))) {
  161. *psigner = signer;
  162. return 1;
  163. }
  164. /* Maybe lookup from store if by subject name */
  165. *psigner = NULL;
  166. return 0;
  167. }
  168. static X509 *ocsp_find_signer_sk(STACK_OF(X509) *certs, OCSP_RESPID *id)
  169. {
  170. int i, r;
  171. unsigned char tmphash[SHA_DIGEST_LENGTH], *keyhash;
  172. EVP_MD *md;
  173. X509 *x;
  174. /* Easy if lookup by name */
  175. if (id->type == V_OCSP_RESPID_NAME)
  176. return X509_find_by_subject(certs, id->value.byName);
  177. /* Lookup by key hash */
  178. /* If key hash isn't SHA1 length then forget it */
  179. if (id->value.byKey->length != SHA_DIGEST_LENGTH)
  180. return NULL;
  181. keyhash = id->value.byKey->data;
  182. /* Calculate hash of each key and compare */
  183. for (i = 0; i < sk_X509_num(certs); i++) {
  184. if ((x = sk_X509_value(certs, i)) != NULL) {
  185. if ((md = EVP_MD_fetch(x->libctx, SN_sha1, x->propq)) == NULL)
  186. break;
  187. r = X509_pubkey_digest(x, md, tmphash, NULL);
  188. EVP_MD_free(md);
  189. if (!r)
  190. break;
  191. if (memcmp(keyhash, tmphash, SHA_DIGEST_LENGTH) == 0)
  192. return x;
  193. }
  194. }
  195. return NULL;
  196. }
  197. static int ocsp_check_issuer(OCSP_BASICRESP *bs, STACK_OF(X509) *chain)
  198. {
  199. STACK_OF(OCSP_SINGLERESP) *sresp = bs->tbsResponseData.responses;
  200. X509 *signer, *sca;
  201. OCSP_CERTID *caid = NULL;
  202. int ret;
  203. if (sk_X509_num(chain) <= 0) {
  204. ERR_raise(ERR_LIB_OCSP, OCSP_R_NO_CERTIFICATES_IN_CHAIN);
  205. return -1;
  206. }
  207. /* See if the issuer IDs match. */
  208. ret = ocsp_check_ids(sresp, &caid);
  209. /* If ID mismatch or other error then return */
  210. if (ret <= 0)
  211. return ret;
  212. signer = sk_X509_value(chain, 0);
  213. /* Check to see if OCSP responder CA matches request CA */
  214. if (sk_X509_num(chain) > 1) {
  215. sca = sk_X509_value(chain, 1);
  216. ret = ocsp_match_issuerid(sca, caid, sresp);
  217. if (ret < 0)
  218. return ret;
  219. if (ret != 0) {
  220. /* We have a match, if extensions OK then success */
  221. if (ocsp_check_delegated(signer))
  222. return 1;
  223. return 0;
  224. }
  225. }
  226. /* Otherwise check if OCSP request signed directly by request CA */
  227. return ocsp_match_issuerid(signer, caid, sresp);
  228. }
  229. /*
  230. * Check the issuer certificate IDs for equality. If there is a mismatch with
  231. * the same algorithm then there's no point trying to match any certificates
  232. * against the issuer. If the issuer IDs all match then we just need to check
  233. * equality against one of them.
  234. */
  235. static int ocsp_check_ids(STACK_OF(OCSP_SINGLERESP) *sresp, OCSP_CERTID **ret)
  236. {
  237. OCSP_CERTID *tmpid, *cid;
  238. int i, idcount;
  239. idcount = sk_OCSP_SINGLERESP_num(sresp);
  240. if (idcount <= 0) {
  241. ERR_raise(ERR_LIB_OCSP, OCSP_R_RESPONSE_CONTAINS_NO_REVOCATION_DATA);
  242. return -1;
  243. }
  244. cid = sk_OCSP_SINGLERESP_value(sresp, 0)->certId;
  245. *ret = NULL;
  246. for (i = 1; i < idcount; i++) {
  247. tmpid = sk_OCSP_SINGLERESP_value(sresp, i)->certId;
  248. /* Check to see if IDs match */
  249. if (OCSP_id_issuer_cmp(cid, tmpid)) {
  250. /* If algorithm mismatch let caller deal with it */
  251. if (OBJ_cmp(tmpid->hashAlgorithm.algorithm,
  252. cid->hashAlgorithm.algorithm))
  253. return 2;
  254. /* Else mismatch */
  255. return 0;
  256. }
  257. }
  258. /* All IDs match: only need to check one ID */
  259. *ret = cid;
  260. return 1;
  261. }
  262. /*
  263. * Match the certificate issuer ID.
  264. * Returns -1 on fatal error, 0 if there is no match and 1 if there is a match.
  265. */
  266. static int ocsp_match_issuerid(X509 *cert, OCSP_CERTID *cid,
  267. STACK_OF(OCSP_SINGLERESP) *sresp)
  268. {
  269. int ret = -1;
  270. EVP_MD *dgst = NULL;
  271. /* If only one ID to match then do it */
  272. if (cid != NULL) {
  273. char name[OSSL_MAX_NAME_SIZE];
  274. const X509_NAME *iname;
  275. int mdlen;
  276. unsigned char md[EVP_MAX_MD_SIZE];
  277. OBJ_obj2txt(name, sizeof(name), cid->hashAlgorithm.algorithm, 0);
  278. (void)ERR_set_mark();
  279. dgst = EVP_MD_fetch(NULL, name, NULL);
  280. if (dgst == NULL)
  281. dgst = (EVP_MD *)EVP_get_digestbyname(name);
  282. if (dgst == NULL) {
  283. (void)ERR_clear_last_mark();
  284. ERR_raise(ERR_LIB_OCSP, OCSP_R_UNKNOWN_MESSAGE_DIGEST);
  285. goto end;
  286. }
  287. (void)ERR_pop_to_mark();
  288. mdlen = EVP_MD_get_size(dgst);
  289. if (mdlen < 0) {
  290. ERR_raise(ERR_LIB_OCSP, OCSP_R_DIGEST_SIZE_ERR);
  291. goto end;
  292. }
  293. if (cid->issuerNameHash.length != mdlen ||
  294. cid->issuerKeyHash.length != mdlen) {
  295. ret = 0;
  296. goto end;
  297. }
  298. iname = X509_get_subject_name(cert);
  299. if (!X509_NAME_digest(iname, dgst, md, NULL))
  300. goto end;
  301. if (memcmp(md, cid->issuerNameHash.data, mdlen) != 0) {
  302. ret = 0;
  303. goto end;
  304. }
  305. if (!X509_pubkey_digest(cert, dgst, md, NULL)) {
  306. ERR_raise(ERR_LIB_OCSP, OCSP_R_DIGEST_ERR);
  307. goto end;
  308. }
  309. ret = memcmp(md, cid->issuerKeyHash.data, mdlen) == 0;
  310. goto end;
  311. } else {
  312. /* We have to match the whole lot */
  313. int i;
  314. OCSP_CERTID *tmpid;
  315. for (i = 0; i < sk_OCSP_SINGLERESP_num(sresp); i++) {
  316. tmpid = sk_OCSP_SINGLERESP_value(sresp, i)->certId;
  317. ret = ocsp_match_issuerid(cert, tmpid, NULL);
  318. if (ret <= 0)
  319. return ret;
  320. }
  321. }
  322. return 1;
  323. end:
  324. EVP_MD_free(dgst);
  325. return ret;
  326. }
  327. static int ocsp_check_delegated(X509 *x)
  328. {
  329. if ((X509_get_extension_flags(x) & EXFLAG_XKUSAGE)
  330. && (X509_get_extended_key_usage(x) & XKU_OCSP_SIGN))
  331. return 1;
  332. ERR_raise(ERR_LIB_OCSP, OCSP_R_MISSING_OCSPSIGNING_USAGE);
  333. return 0;
  334. }
  335. /*
  336. * Verify an OCSP request. This is much easier than OCSP response verify.
  337. * Just find the signer's certificate and verify it against a given trust value.
  338. * Returns 1 on success, 0 on failure and on fatal error.
  339. */
  340. int OCSP_request_verify(OCSP_REQUEST *req, STACK_OF(X509) *certs,
  341. X509_STORE *store, unsigned long flags)
  342. {
  343. X509 *signer;
  344. const X509_NAME *nm;
  345. GENERAL_NAME *gen;
  346. int ret;
  347. if (!req->optionalSignature) {
  348. ERR_raise(ERR_LIB_OCSP, OCSP_R_REQUEST_NOT_SIGNED);
  349. return 0;
  350. }
  351. gen = req->tbsRequest.requestorName;
  352. if (!gen || gen->type != GEN_DIRNAME) {
  353. ERR_raise(ERR_LIB_OCSP, OCSP_R_UNSUPPORTED_REQUESTORNAME_TYPE);
  354. return 0; /* not returning -1 here for backward compatibility*/
  355. }
  356. nm = gen->d.directoryName;
  357. ret = ocsp_req_find_signer(&signer, req, nm, certs, flags);
  358. if (ret <= 0) {
  359. ERR_raise(ERR_LIB_OCSP, OCSP_R_SIGNER_CERTIFICATE_NOT_FOUND);
  360. return 0; /* not returning -1 here for backward compatibility*/
  361. }
  362. if ((ret == 2) && (flags & OCSP_TRUSTOTHER) != 0)
  363. flags |= OCSP_NOVERIFY;
  364. if ((ret = ocsp_verify(req, NULL, signer, flags)) <= 0)
  365. return 0; /* not returning 'ret' here for backward compatibility*/
  366. if ((flags & OCSP_NOVERIFY) != 0)
  367. return 1;
  368. return ocsp_verify_signer(signer, 0, store, flags,
  369. (flags & OCSP_NOCHAIN) != 0 ?
  370. NULL : req->optionalSignature->certs, NULL) > 0;
  371. /* using '> 0' here to avoid breaking backward compatibility returning -1 */
  372. }
  373. static int ocsp_req_find_signer(X509 **psigner, OCSP_REQUEST *req,
  374. const X509_NAME *nm, STACK_OF(X509) *certs,
  375. unsigned long flags)
  376. {
  377. X509 *signer;
  378. if ((flags & OCSP_NOINTERN) == 0) {
  379. signer = X509_find_by_subject(req->optionalSignature->certs, nm);
  380. if (signer != NULL) {
  381. *psigner = signer;
  382. return 1;
  383. }
  384. }
  385. if ((signer = X509_find_by_subject(certs, nm)) != NULL) {
  386. *psigner = signer;
  387. return 2;
  388. }
  389. return 0;
  390. }