ocsp_vfy.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  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 <openssl/err.h>
  61. #include <string.h>
  62. static int ocsp_find_signer(X509 **psigner, OCSP_BASICRESP *bs,
  63. STACK_OF(X509) *certs, X509_STORE *st,
  64. unsigned long flags);
  65. static X509 *ocsp_find_signer_sk(STACK_OF(X509) *certs, OCSP_RESPID *id);
  66. static int ocsp_check_issuer(OCSP_BASICRESP *bs, STACK_OF(X509) *chain,
  67. unsigned long flags);
  68. static int ocsp_check_ids(STACK_OF(OCSP_SINGLERESP) *sresp,
  69. OCSP_CERTID **ret);
  70. static int ocsp_match_issuerid(X509 *cert, OCSP_CERTID *cid,
  71. STACK_OF(OCSP_SINGLERESP) *sresp);
  72. static int ocsp_check_delegated(X509 *x, int flags);
  73. static int ocsp_req_find_signer(X509 **psigner, OCSP_REQUEST *req,
  74. X509_NAME *nm, STACK_OF(X509) *certs,
  75. X509_STORE *st, unsigned long flags);
  76. /* Verify a basic response message */
  77. int OCSP_basic_verify(OCSP_BASICRESP *bs, STACK_OF(X509) *certs,
  78. X509_STORE *st, unsigned long flags)
  79. {
  80. X509 *signer, *x;
  81. STACK_OF(X509) *chain = NULL;
  82. STACK_OF(X509) *untrusted = NULL;
  83. X509_STORE_CTX ctx;
  84. int i, ret = 0;
  85. ret = ocsp_find_signer(&signer, bs, certs, st, flags);
  86. if (!ret) {
  87. OCSPerr(OCSP_F_OCSP_BASIC_VERIFY,
  88. OCSP_R_SIGNER_CERTIFICATE_NOT_FOUND);
  89. goto end;
  90. }
  91. if ((ret == 2) && (flags & OCSP_TRUSTOTHER))
  92. flags |= OCSP_NOVERIFY;
  93. if (!(flags & OCSP_NOSIGS)) {
  94. EVP_PKEY *skey;
  95. skey = X509_get_pubkey(signer);
  96. if (skey) {
  97. ret = OCSP_BASICRESP_verify(bs, skey, 0);
  98. EVP_PKEY_free(skey);
  99. }
  100. if (!skey || ret <= 0) {
  101. OCSPerr(OCSP_F_OCSP_BASIC_VERIFY, OCSP_R_SIGNATURE_FAILURE);
  102. goto end;
  103. }
  104. }
  105. if (!(flags & OCSP_NOVERIFY)) {
  106. int init_res;
  107. if (flags & OCSP_NOCHAIN) {
  108. untrusted = NULL;
  109. } else if (bs->certs && certs) {
  110. untrusted = sk_X509_dup(bs->certs);
  111. for (i = 0; i < sk_X509_num(certs); i++) {
  112. if (!sk_X509_push(untrusted, sk_X509_value(certs, i))) {
  113. OCSPerr(OCSP_F_OCSP_BASIC_VERIFY, ERR_R_MALLOC_FAILURE);
  114. goto end;
  115. }
  116. }
  117. } else {
  118. untrusted = bs->certs;
  119. }
  120. init_res = X509_STORE_CTX_init(&ctx, st, signer, untrusted);
  121. if (!init_res) {
  122. ret = -1;
  123. OCSPerr(OCSP_F_OCSP_BASIC_VERIFY, ERR_R_X509_LIB);
  124. goto end;
  125. }
  126. X509_STORE_CTX_set_purpose(&ctx, X509_PURPOSE_OCSP_HELPER);
  127. ret = X509_verify_cert(&ctx);
  128. chain = X509_STORE_CTX_get1_chain(&ctx);
  129. X509_STORE_CTX_cleanup(&ctx);
  130. if (ret <= 0) {
  131. i = X509_STORE_CTX_get_error(&ctx);
  132. OCSPerr(OCSP_F_OCSP_BASIC_VERIFY,
  133. OCSP_R_CERTIFICATE_VERIFY_ERROR);
  134. ERR_add_error_data(2, "Verify error:",
  135. X509_verify_cert_error_string(i));
  136. goto end;
  137. }
  138. if (flags & OCSP_NOCHECKS) {
  139. ret = 1;
  140. goto end;
  141. }
  142. /*
  143. * At this point we have a valid certificate chain need to verify it
  144. * against the OCSP issuer criteria.
  145. */
  146. ret = ocsp_check_issuer(bs, chain, flags);
  147. /* If fatal error or valid match then finish */
  148. if (ret != 0)
  149. goto end;
  150. /*
  151. * Easy case: explicitly trusted. Get root CA and check for explicit
  152. * trust
  153. */
  154. if (flags & OCSP_NOEXPLICIT)
  155. goto end;
  156. x = sk_X509_value(chain, sk_X509_num(chain) - 1);
  157. if (X509_check_trust(x, NID_OCSP_sign, 0) != X509_TRUST_TRUSTED) {
  158. OCSPerr(OCSP_F_OCSP_BASIC_VERIFY, OCSP_R_ROOT_CA_NOT_TRUSTED);
  159. goto end;
  160. }
  161. ret = 1;
  162. }
  163. end:
  164. if (chain)
  165. sk_X509_pop_free(chain, X509_free);
  166. if (bs->certs && certs)
  167. sk_X509_free(untrusted);
  168. return ret;
  169. }
  170. static int ocsp_find_signer(X509 **psigner, OCSP_BASICRESP *bs,
  171. STACK_OF(X509) *certs, X509_STORE *st,
  172. unsigned long flags)
  173. {
  174. X509 *signer;
  175. OCSP_RESPID *rid = bs->tbsResponseData->responderId;
  176. if ((signer = ocsp_find_signer_sk(certs, rid))) {
  177. *psigner = signer;
  178. return 2;
  179. }
  180. if (!(flags & OCSP_NOINTERN) &&
  181. (signer = ocsp_find_signer_sk(bs->certs, rid))) {
  182. *psigner = signer;
  183. return 1;
  184. }
  185. /* Maybe lookup from store if by subject name */
  186. *psigner = NULL;
  187. return 0;
  188. }
  189. static X509 *ocsp_find_signer_sk(STACK_OF(X509) *certs, OCSP_RESPID *id)
  190. {
  191. int i;
  192. unsigned char tmphash[SHA_DIGEST_LENGTH], *keyhash;
  193. X509 *x;
  194. /* Easy if lookup by name */
  195. if (id->type == V_OCSP_RESPID_NAME)
  196. return X509_find_by_subject(certs, id->value.byName);
  197. /* Lookup by key hash */
  198. /* If key hash isn't SHA1 length then forget it */
  199. if (id->value.byKey->length != SHA_DIGEST_LENGTH)
  200. return NULL;
  201. keyhash = id->value.byKey->data;
  202. /* Calculate hash of each key and compare */
  203. for (i = 0; i < sk_X509_num(certs); i++) {
  204. x = sk_X509_value(certs, i);
  205. X509_pubkey_digest(x, EVP_sha1(), tmphash, NULL);
  206. if (!memcmp(keyhash, tmphash, SHA_DIGEST_LENGTH))
  207. return x;
  208. }
  209. return NULL;
  210. }
  211. static int ocsp_check_issuer(OCSP_BASICRESP *bs, STACK_OF(X509) *chain,
  212. unsigned long flags)
  213. {
  214. STACK_OF(OCSP_SINGLERESP) *sresp;
  215. X509 *signer, *sca;
  216. OCSP_CERTID *caid = NULL;
  217. int i;
  218. sresp = bs->tbsResponseData->responses;
  219. if (sk_X509_num(chain) <= 0) {
  220. OCSPerr(OCSP_F_OCSP_CHECK_ISSUER, OCSP_R_NO_CERTIFICATES_IN_CHAIN);
  221. return -1;
  222. }
  223. /* See if the issuer IDs match. */
  224. i = ocsp_check_ids(sresp, &caid);
  225. /* If ID mismatch or other error then return */
  226. if (i <= 0)
  227. return i;
  228. signer = sk_X509_value(chain, 0);
  229. /* Check to see if OCSP responder CA matches request CA */
  230. if (sk_X509_num(chain) > 1) {
  231. sca = sk_X509_value(chain, 1);
  232. i = ocsp_match_issuerid(sca, caid, sresp);
  233. if (i < 0)
  234. return i;
  235. if (i) {
  236. /* We have a match, if extensions OK then success */
  237. if (ocsp_check_delegated(signer, flags))
  238. return 1;
  239. return 0;
  240. }
  241. }
  242. /* Otherwise check if OCSP request signed directly by request CA */
  243. return ocsp_match_issuerid(signer, caid, sresp);
  244. }
  245. /*
  246. * Check the issuer certificate IDs for equality. If there is a mismatch with
  247. * the same algorithm then there's no point trying to match any certificates
  248. * against the issuer. If the issuer IDs all match then we just need to check
  249. * equality against one of them.
  250. */
  251. static int ocsp_check_ids(STACK_OF(OCSP_SINGLERESP) *sresp, OCSP_CERTID **ret)
  252. {
  253. OCSP_CERTID *tmpid, *cid;
  254. int i, idcount;
  255. idcount = sk_OCSP_SINGLERESP_num(sresp);
  256. if (idcount <= 0) {
  257. OCSPerr(OCSP_F_OCSP_CHECK_IDS,
  258. OCSP_R_RESPONSE_CONTAINS_NO_REVOCATION_DATA);
  259. return -1;
  260. }
  261. cid = sk_OCSP_SINGLERESP_value(sresp, 0)->certId;
  262. *ret = NULL;
  263. for (i = 1; i < idcount; i++) {
  264. tmpid = sk_OCSP_SINGLERESP_value(sresp, i)->certId;
  265. /* Check to see if IDs match */
  266. if (OCSP_id_issuer_cmp(cid, tmpid)) {
  267. /* If algoritm mismatch let caller deal with it */
  268. if (OBJ_cmp(tmpid->hashAlgorithm->algorithm,
  269. cid->hashAlgorithm->algorithm))
  270. return 2;
  271. /* Else mismatch */
  272. return 0;
  273. }
  274. }
  275. /* All IDs match: only need to check one ID */
  276. *ret = cid;
  277. return 1;
  278. }
  279. static int ocsp_match_issuerid(X509 *cert, OCSP_CERTID *cid,
  280. STACK_OF(OCSP_SINGLERESP) *sresp)
  281. {
  282. /* If only one ID to match then do it */
  283. if (cid) {
  284. const EVP_MD *dgst;
  285. X509_NAME *iname;
  286. int mdlen;
  287. unsigned char md[EVP_MAX_MD_SIZE];
  288. if (!(dgst = EVP_get_digestbyobj(cid->hashAlgorithm->algorithm))) {
  289. OCSPerr(OCSP_F_OCSP_MATCH_ISSUERID,
  290. OCSP_R_UNKNOWN_MESSAGE_DIGEST);
  291. return -1;
  292. }
  293. mdlen = EVP_MD_size(dgst);
  294. if (mdlen < 0)
  295. return -1;
  296. if ((cid->issuerNameHash->length != mdlen) ||
  297. (cid->issuerKeyHash->length != mdlen))
  298. return 0;
  299. iname = X509_get_subject_name(cert);
  300. if (!X509_NAME_digest(iname, dgst, md, NULL))
  301. return -1;
  302. if (memcmp(md, cid->issuerNameHash->data, mdlen))
  303. return 0;
  304. X509_pubkey_digest(cert, dgst, md, NULL);
  305. if (memcmp(md, cid->issuerKeyHash->data, mdlen))
  306. return 0;
  307. return 1;
  308. } else {
  309. /* We have to match the whole lot */
  310. int i, ret;
  311. OCSP_CERTID *tmpid;
  312. for (i = 0; i < sk_OCSP_SINGLERESP_num(sresp); i++) {
  313. tmpid = sk_OCSP_SINGLERESP_value(sresp, i)->certId;
  314. ret = ocsp_match_issuerid(cert, tmpid, NULL);
  315. if (ret <= 0)
  316. return ret;
  317. }
  318. return 1;
  319. }
  320. }
  321. static int ocsp_check_delegated(X509 *x, int flags)
  322. {
  323. X509_check_purpose(x, -1, 0);
  324. if ((x->ex_flags & EXFLAG_XKUSAGE) && (x->ex_xkusage & XKU_OCSP_SIGN))
  325. return 1;
  326. OCSPerr(OCSP_F_OCSP_CHECK_DELEGATED, OCSP_R_MISSING_OCSPSIGNING_USAGE);
  327. return 0;
  328. }
  329. /*
  330. * Verify an OCSP request. This is fortunately much easier than OCSP response
  331. * verify. Just find the signers certificate and verify it against a given
  332. * trust value.
  333. */
  334. int OCSP_request_verify(OCSP_REQUEST *req, STACK_OF(X509) *certs,
  335. X509_STORE *store, unsigned long flags)
  336. {
  337. X509 *signer;
  338. X509_NAME *nm;
  339. GENERAL_NAME *gen;
  340. int ret;
  341. X509_STORE_CTX ctx;
  342. if (!req->optionalSignature) {
  343. OCSPerr(OCSP_F_OCSP_REQUEST_VERIFY, OCSP_R_REQUEST_NOT_SIGNED);
  344. return 0;
  345. }
  346. gen = req->tbsRequest->requestorName;
  347. if (!gen || gen->type != GEN_DIRNAME) {
  348. OCSPerr(OCSP_F_OCSP_REQUEST_VERIFY,
  349. OCSP_R_UNSUPPORTED_REQUESTORNAME_TYPE);
  350. return 0;
  351. }
  352. nm = gen->d.directoryName;
  353. ret = ocsp_req_find_signer(&signer, req, nm, certs, store, flags);
  354. if (ret <= 0) {
  355. OCSPerr(OCSP_F_OCSP_REQUEST_VERIFY,
  356. OCSP_R_SIGNER_CERTIFICATE_NOT_FOUND);
  357. return 0;
  358. }
  359. if ((ret == 2) && (flags & OCSP_TRUSTOTHER))
  360. flags |= OCSP_NOVERIFY;
  361. if (!(flags & OCSP_NOSIGS)) {
  362. EVP_PKEY *skey;
  363. skey = X509_get_pubkey(signer);
  364. ret = OCSP_REQUEST_verify(req, skey);
  365. EVP_PKEY_free(skey);
  366. if (ret <= 0) {
  367. OCSPerr(OCSP_F_OCSP_REQUEST_VERIFY, OCSP_R_SIGNATURE_FAILURE);
  368. return 0;
  369. }
  370. }
  371. if (!(flags & OCSP_NOVERIFY)) {
  372. int init_res;
  373. if (flags & OCSP_NOCHAIN)
  374. init_res = X509_STORE_CTX_init(&ctx, store, signer, NULL);
  375. else
  376. init_res = X509_STORE_CTX_init(&ctx, store, signer,
  377. req->optionalSignature->certs);
  378. if (!init_res) {
  379. OCSPerr(OCSP_F_OCSP_REQUEST_VERIFY, ERR_R_X509_LIB);
  380. return 0;
  381. }
  382. X509_STORE_CTX_set_purpose(&ctx, X509_PURPOSE_OCSP_HELPER);
  383. X509_STORE_CTX_set_trust(&ctx, X509_TRUST_OCSP_REQUEST);
  384. ret = X509_verify_cert(&ctx);
  385. X509_STORE_CTX_cleanup(&ctx);
  386. if (ret <= 0) {
  387. ret = X509_STORE_CTX_get_error(&ctx);
  388. OCSPerr(OCSP_F_OCSP_REQUEST_VERIFY,
  389. OCSP_R_CERTIFICATE_VERIFY_ERROR);
  390. ERR_add_error_data(2, "Verify error:",
  391. X509_verify_cert_error_string(ret));
  392. return 0;
  393. }
  394. }
  395. return 1;
  396. }
  397. static int ocsp_req_find_signer(X509 **psigner, OCSP_REQUEST *req,
  398. X509_NAME *nm, STACK_OF(X509) *certs,
  399. X509_STORE *st, unsigned long flags)
  400. {
  401. X509 *signer;
  402. if (!(flags & OCSP_NOINTERN)) {
  403. signer = X509_find_by_subject(req->optionalSignature->certs, nm);
  404. if (signer) {
  405. *psigner = signer;
  406. return 1;
  407. }
  408. }
  409. signer = X509_find_by_subject(certs, nm);
  410. if (signer) {
  411. *psigner = signer;
  412. return 2;
  413. }
  414. return 0;
  415. }