ocsp_vfy.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  1. /* ocsp_vfy.c */
  2. /*
  3. * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL project
  4. * 2000.
  5. */
  6. /* ====================================================================
  7. * Copyright (c) 2000-2004 The OpenSSL Project. All rights reserved.
  8. *
  9. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted provided that the following conditions
  11. * are met:
  12. *
  13. * 1. Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions and the following disclaimer.
  15. *
  16. * 2. Redistributions in binary form must reproduce the above copyright
  17. * notice, this list of conditions and the following disclaimer in
  18. * the documentation and/or other materials provided with the
  19. * distribution.
  20. *
  21. * 3. All advertising materials mentioning features or use of this
  22. * software must display the following acknowledgment:
  23. * "This product includes software developed by the OpenSSL Project
  24. * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
  25. *
  26. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  27. * endorse or promote products derived from this software without
  28. * prior written permission. For written permission, please contact
  29. * licensing@OpenSSL.org.
  30. *
  31. * 5. Products derived from this software may not be called "OpenSSL"
  32. * nor may "OpenSSL" appear in their names without prior written
  33. * permission of the OpenSSL Project.
  34. *
  35. * 6. Redistributions of any form whatsoever must retain the following
  36. * acknowledgment:
  37. * "This product includes software developed by the OpenSSL Project
  38. * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
  39. *
  40. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  41. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  42. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  43. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  44. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  45. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  46. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  47. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  48. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  49. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  50. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  51. * OF THE POSSIBILITY OF SUCH DAMAGE.
  52. * ====================================================================
  53. *
  54. * This product includes cryptographic software written by Eric Young
  55. * (eay@cryptsoft.com). This product includes software written by Tim
  56. * Hudson (tjh@cryptsoft.com).
  57. *
  58. */
  59. #include <openssl/ocsp.h>
  60. #include "ocsp_lcl.h"
  61. #include <openssl/err.h>
  62. #include <string.h>
  63. static int ocsp_find_signer(X509 **psigner, OCSP_BASICRESP *bs,
  64. STACK_OF(X509) *certs, X509_STORE *st,
  65. unsigned long flags);
  66. static X509 *ocsp_find_signer_sk(STACK_OF(X509) *certs, OCSP_RESPID *id);
  67. static int ocsp_check_issuer(OCSP_BASICRESP *bs, STACK_OF(X509) *chain,
  68. unsigned long flags);
  69. static int ocsp_check_ids(STACK_OF(OCSP_SINGLERESP) *sresp,
  70. OCSP_CERTID **ret);
  71. static int ocsp_match_issuerid(X509 *cert, OCSP_CERTID *cid,
  72. STACK_OF(OCSP_SINGLERESP) *sresp);
  73. static int ocsp_check_delegated(X509 *x, int flags);
  74. static int ocsp_req_find_signer(X509 **psigner, OCSP_REQUEST *req,
  75. X509_NAME *nm, STACK_OF(X509) *certs,
  76. X509_STORE *st, unsigned long flags);
  77. /* Verify a basic response message */
  78. int OCSP_basic_verify(OCSP_BASICRESP *bs, STACK_OF(X509) *certs,
  79. X509_STORE *st, unsigned long flags)
  80. {
  81. X509 *signer, *x;
  82. STACK_OF(X509) *chain = NULL;
  83. STACK_OF(X509) *untrusted = NULL;
  84. X509_STORE_CTX ctx;
  85. int i, ret = 0;
  86. ret = ocsp_find_signer(&signer, bs, certs, st, flags);
  87. if (!ret) {
  88. OCSPerr(OCSP_F_OCSP_BASIC_VERIFY,
  89. OCSP_R_SIGNER_CERTIFICATE_NOT_FOUND);
  90. goto end;
  91. }
  92. if ((ret == 2) && (flags & OCSP_TRUSTOTHER))
  93. flags |= OCSP_NOVERIFY;
  94. if (!(flags & OCSP_NOSIGS)) {
  95. EVP_PKEY *skey;
  96. skey = X509_get0_pubkey(signer);
  97. if (skey)
  98. ret = OCSP_BASICRESP_verify(bs, skey, 0);
  99. if (!skey || ret <= 0) {
  100. OCSPerr(OCSP_F_OCSP_BASIC_VERIFY, OCSP_R_SIGNATURE_FAILURE);
  101. goto end;
  102. }
  103. }
  104. if (!(flags & OCSP_NOVERIFY)) {
  105. int init_res;
  106. if (flags & OCSP_NOCHAIN) {
  107. untrusted = NULL;
  108. } else if (bs->certs && certs) {
  109. untrusted = sk_X509_dup(bs->certs);
  110. for (i = 0; i < sk_X509_num(certs); i++) {
  111. if (!sk_X509_push(untrusted, sk_X509_value(certs, i))) {
  112. OCSPerr(OCSP_F_OCSP_BASIC_VERIFY, ERR_R_MALLOC_FAILURE);
  113. goto end;
  114. }
  115. }
  116. } else {
  117. untrusted = bs->certs;
  118. }
  119. init_res = X509_STORE_CTX_init(&ctx, st, signer, untrusted);
  120. if (!init_res) {
  121. ret = -1;
  122. OCSPerr(OCSP_F_OCSP_BASIC_VERIFY, ERR_R_X509_LIB);
  123. goto end;
  124. }
  125. X509_STORE_CTX_set_purpose(&ctx, X509_PURPOSE_OCSP_HELPER);
  126. ret = X509_verify_cert(&ctx);
  127. chain = X509_STORE_CTX_get1_chain(&ctx);
  128. X509_STORE_CTX_cleanup(&ctx);
  129. if (ret <= 0) {
  130. i = X509_STORE_CTX_get_error(&ctx);
  131. OCSPerr(OCSP_F_OCSP_BASIC_VERIFY,
  132. OCSP_R_CERTIFICATE_VERIFY_ERROR);
  133. ERR_add_error_data(2, "Verify error:",
  134. X509_verify_cert_error_string(i));
  135. goto end;
  136. }
  137. if (flags & OCSP_NOCHECKS) {
  138. ret = 1;
  139. goto end;
  140. }
  141. /*
  142. * At this point we have a valid certificate chain need to verify it
  143. * against the OCSP issuer criteria.
  144. */
  145. ret = ocsp_check_issuer(bs, chain, flags);
  146. /* If fatal error or valid match then finish */
  147. if (ret != 0)
  148. goto end;
  149. /*
  150. * Easy case: explicitly trusted. Get root CA and check for explicit
  151. * trust
  152. */
  153. if (flags & OCSP_NOEXPLICIT)
  154. goto end;
  155. x = sk_X509_value(chain, sk_X509_num(chain) - 1);
  156. if (X509_check_trust(x, NID_OCSP_sign, 0) != X509_TRUST_TRUSTED) {
  157. OCSPerr(OCSP_F_OCSP_BASIC_VERIFY, OCSP_R_ROOT_CA_NOT_TRUSTED);
  158. goto end;
  159. }
  160. ret = 1;
  161. }
  162. end:
  163. sk_X509_pop_free(chain, X509_free);
  164. if (bs->certs && certs)
  165. sk_X509_free(untrusted);
  166. return ret;
  167. }
  168. static int ocsp_find_signer(X509 **psigner, OCSP_BASICRESP *bs,
  169. STACK_OF(X509) *certs, X509_STORE *st,
  170. unsigned long flags)
  171. {
  172. X509 *signer;
  173. OCSP_RESPID *rid = &bs->tbsResponseData.responderId;
  174. if ((signer = ocsp_find_signer_sk(certs, rid))) {
  175. *psigner = signer;
  176. return 2;
  177. }
  178. if (!(flags & OCSP_NOINTERN) &&
  179. (signer = ocsp_find_signer_sk(bs->certs, rid))) {
  180. *psigner = signer;
  181. return 1;
  182. }
  183. /* Maybe lookup from store if by subject name */
  184. *psigner = NULL;
  185. return 0;
  186. }
  187. static X509 *ocsp_find_signer_sk(STACK_OF(X509) *certs, OCSP_RESPID *id)
  188. {
  189. int i;
  190. unsigned char tmphash[SHA_DIGEST_LENGTH], *keyhash;
  191. X509 *x;
  192. /* Easy if lookup by name */
  193. if (id->type == V_OCSP_RESPID_NAME)
  194. return X509_find_by_subject(certs, id->value.byName);
  195. /* Lookup by key hash */
  196. /* If key hash isn't SHA1 length then forget it */
  197. if (id->value.byKey->length != SHA_DIGEST_LENGTH)
  198. return NULL;
  199. keyhash = id->value.byKey->data;
  200. /* Calculate hash of each key and compare */
  201. for (i = 0; i < sk_X509_num(certs); i++) {
  202. x = sk_X509_value(certs, i);
  203. X509_pubkey_digest(x, EVP_sha1(), tmphash, NULL);
  204. if (!memcmp(keyhash, tmphash, SHA_DIGEST_LENGTH))
  205. return x;
  206. }
  207. return NULL;
  208. }
  209. static int ocsp_check_issuer(OCSP_BASICRESP *bs, STACK_OF(X509) *chain,
  210. unsigned long flags)
  211. {
  212. STACK_OF(OCSP_SINGLERESP) *sresp;
  213. X509 *signer, *sca;
  214. OCSP_CERTID *caid = NULL;
  215. int i;
  216. sresp = bs->tbsResponseData.responses;
  217. if (sk_X509_num(chain) <= 0) {
  218. OCSPerr(OCSP_F_OCSP_CHECK_ISSUER, OCSP_R_NO_CERTIFICATES_IN_CHAIN);
  219. return -1;
  220. }
  221. /* See if the issuer IDs match. */
  222. i = ocsp_check_ids(sresp, &caid);
  223. /* If ID mismatch or other error then return */
  224. if (i <= 0)
  225. return i;
  226. signer = sk_X509_value(chain, 0);
  227. /* Check to see if OCSP responder CA matches request CA */
  228. if (sk_X509_num(chain) > 1) {
  229. sca = sk_X509_value(chain, 1);
  230. i = ocsp_match_issuerid(sca, caid, sresp);
  231. if (i < 0)
  232. return i;
  233. if (i) {
  234. /* We have a match, if extensions OK then success */
  235. if (ocsp_check_delegated(signer, flags))
  236. return 1;
  237. return 0;
  238. }
  239. }
  240. /* Otherwise check if OCSP request signed directly by request CA */
  241. return ocsp_match_issuerid(signer, caid, sresp);
  242. }
  243. /*
  244. * Check the issuer certificate IDs for equality. If there is a mismatch with
  245. * the same algorithm then there's no point trying to match any certificates
  246. * against the issuer. If the issuer IDs all match then we just need to check
  247. * equality against one of them.
  248. */
  249. static int ocsp_check_ids(STACK_OF(OCSP_SINGLERESP) *sresp, OCSP_CERTID **ret)
  250. {
  251. OCSP_CERTID *tmpid, *cid;
  252. int i, idcount;
  253. idcount = sk_OCSP_SINGLERESP_num(sresp);
  254. if (idcount <= 0) {
  255. OCSPerr(OCSP_F_OCSP_CHECK_IDS,
  256. OCSP_R_RESPONSE_CONTAINS_NO_REVOCATION_DATA);
  257. return -1;
  258. }
  259. cid = sk_OCSP_SINGLERESP_value(sresp, 0)->certId;
  260. *ret = NULL;
  261. for (i = 1; i < idcount; i++) {
  262. tmpid = sk_OCSP_SINGLERESP_value(sresp, i)->certId;
  263. /* Check to see if IDs match */
  264. if (OCSP_id_issuer_cmp(cid, tmpid)) {
  265. /* If algoritm mismatch let caller deal with it */
  266. if (OBJ_cmp(tmpid->hashAlgorithm.algorithm,
  267. cid->hashAlgorithm.algorithm))
  268. return 2;
  269. /* Else mismatch */
  270. return 0;
  271. }
  272. }
  273. /* All IDs match: only need to check one ID */
  274. *ret = cid;
  275. return 1;
  276. }
  277. static int ocsp_match_issuerid(X509 *cert, OCSP_CERTID *cid,
  278. STACK_OF(OCSP_SINGLERESP) *sresp)
  279. {
  280. /* If only one ID to match then do it */
  281. if (cid) {
  282. const EVP_MD *dgst;
  283. X509_NAME *iname;
  284. int mdlen;
  285. unsigned char md[EVP_MAX_MD_SIZE];
  286. if ((dgst = EVP_get_digestbyobj(cid->hashAlgorithm.algorithm))
  287. == NULL) {
  288. OCSPerr(OCSP_F_OCSP_MATCH_ISSUERID,
  289. OCSP_R_UNKNOWN_MESSAGE_DIGEST);
  290. return -1;
  291. }
  292. mdlen = EVP_MD_size(dgst);
  293. if (mdlen < 0)
  294. return -1;
  295. if ((cid->issuerNameHash.length != mdlen) ||
  296. (cid->issuerKeyHash.length != mdlen))
  297. return 0;
  298. iname = X509_get_subject_name(cert);
  299. if (!X509_NAME_digest(iname, dgst, md, NULL))
  300. return -1;
  301. if (memcmp(md, cid->issuerNameHash.data, mdlen))
  302. return 0;
  303. X509_pubkey_digest(cert, dgst, md, NULL);
  304. if (memcmp(md, cid->issuerKeyHash.data, mdlen))
  305. return 0;
  306. return 1;
  307. } else {
  308. /* We have to match the whole lot */
  309. int i, ret;
  310. OCSP_CERTID *tmpid;
  311. for (i = 0; i < sk_OCSP_SINGLERESP_num(sresp); i++) {
  312. tmpid = sk_OCSP_SINGLERESP_value(sresp, i)->certId;
  313. ret = ocsp_match_issuerid(cert, tmpid, NULL);
  314. if (ret <= 0)
  315. return ret;
  316. }
  317. return 1;
  318. }
  319. }
  320. static int ocsp_check_delegated(X509 *x, int flags)
  321. {
  322. if ((X509_get_extension_flags(x) & EXFLAG_XKUSAGE)
  323. && (X509_get_extended_key_usage(x) & XKU_OCSP_SIGN))
  324. return 1;
  325. OCSPerr(OCSP_F_OCSP_CHECK_DELEGATED, OCSP_R_MISSING_OCSPSIGNING_USAGE);
  326. return 0;
  327. }
  328. /*
  329. * Verify an OCSP request. This is fortunately much easier than OCSP response
  330. * verify. Just find the signers certificate and verify it against a given
  331. * trust value.
  332. */
  333. int OCSP_request_verify(OCSP_REQUEST *req, STACK_OF(X509) *certs,
  334. X509_STORE *store, unsigned long flags)
  335. {
  336. X509 *signer;
  337. X509_NAME *nm;
  338. GENERAL_NAME *gen;
  339. int ret;
  340. X509_STORE_CTX ctx;
  341. if (!req->optionalSignature) {
  342. OCSPerr(OCSP_F_OCSP_REQUEST_VERIFY, OCSP_R_REQUEST_NOT_SIGNED);
  343. return 0;
  344. }
  345. gen = req->tbsRequest.requestorName;
  346. if (!gen || gen->type != GEN_DIRNAME) {
  347. OCSPerr(OCSP_F_OCSP_REQUEST_VERIFY,
  348. OCSP_R_UNSUPPORTED_REQUESTORNAME_TYPE);
  349. return 0;
  350. }
  351. nm = gen->d.directoryName;
  352. ret = ocsp_req_find_signer(&signer, req, nm, certs, store, flags);
  353. if (ret <= 0) {
  354. OCSPerr(OCSP_F_OCSP_REQUEST_VERIFY,
  355. OCSP_R_SIGNER_CERTIFICATE_NOT_FOUND);
  356. return 0;
  357. }
  358. if ((ret == 2) && (flags & OCSP_TRUSTOTHER))
  359. flags |= OCSP_NOVERIFY;
  360. if (!(flags & OCSP_NOSIGS)) {
  361. EVP_PKEY *skey;
  362. skey = X509_get0_pubkey(signer);
  363. ret = OCSP_REQUEST_verify(req, skey);
  364. if (ret <= 0) {
  365. OCSPerr(OCSP_F_OCSP_REQUEST_VERIFY, OCSP_R_SIGNATURE_FAILURE);
  366. return 0;
  367. }
  368. }
  369. if (!(flags & OCSP_NOVERIFY)) {
  370. int init_res;
  371. if (flags & OCSP_NOCHAIN)
  372. init_res = X509_STORE_CTX_init(&ctx, store, signer, NULL);
  373. else
  374. init_res = X509_STORE_CTX_init(&ctx, store, signer,
  375. req->optionalSignature->certs);
  376. if (!init_res) {
  377. OCSPerr(OCSP_F_OCSP_REQUEST_VERIFY, ERR_R_X509_LIB);
  378. return 0;
  379. }
  380. X509_STORE_CTX_set_purpose(&ctx, X509_PURPOSE_OCSP_HELPER);
  381. X509_STORE_CTX_set_trust(&ctx, X509_TRUST_OCSP_REQUEST);
  382. ret = X509_verify_cert(&ctx);
  383. X509_STORE_CTX_cleanup(&ctx);
  384. if (ret <= 0) {
  385. ret = X509_STORE_CTX_get_error(&ctx);
  386. OCSPerr(OCSP_F_OCSP_REQUEST_VERIFY,
  387. OCSP_R_CERTIFICATE_VERIFY_ERROR);
  388. ERR_add_error_data(2, "Verify error:",
  389. X509_verify_cert_error_string(ret));
  390. return 0;
  391. }
  392. }
  393. return 1;
  394. }
  395. static int ocsp_req_find_signer(X509 **psigner, OCSP_REQUEST *req,
  396. X509_NAME *nm, STACK_OF(X509) *certs,
  397. X509_STORE *st, unsigned long flags)
  398. {
  399. X509 *signer;
  400. if (!(flags & OCSP_NOINTERN)) {
  401. signer = X509_find_by_subject(req->optionalSignature->certs, nm);
  402. if (signer) {
  403. *psigner = signer;
  404. return 1;
  405. }
  406. }
  407. signer = X509_find_by_subject(certs, nm);
  408. if (signer) {
  409. *psigner = signer;
  410. return 2;
  411. }
  412. return 0;
  413. }