ocsp_vfy.c 14 KB

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