ocsp_vfy.c 13 KB

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