ocsp_vfy.c 15 KB

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