ocsp_cl.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  1. /* ocsp_cl.c */
  2. /*
  3. * Written by Tom Titchener <Tom_Titchener@groove.net> for the OpenSSL
  4. * project.
  5. */
  6. /*
  7. * History: This file was transfered to Richard Levitte from CertCo by Kathy
  8. * Weinhold in mid-spring 2000 to be included in OpenSSL or released as a
  9. * patch kit.
  10. */
  11. /* ====================================================================
  12. * Copyright (c) 1998-2000 The OpenSSL Project. All rights reserved.
  13. *
  14. * Redistribution and use in source and binary forms, with or without
  15. * modification, are permitted provided that the following conditions
  16. * are met:
  17. *
  18. * 1. Redistributions of source code must retain the above copyright
  19. * notice, this list of conditions and the following disclaimer.
  20. *
  21. * 2. Redistributions in binary form must reproduce the above copyright
  22. * notice, this list of conditions and the following disclaimer in
  23. * the documentation and/or other materials provided with the
  24. * distribution.
  25. *
  26. * 3. All advertising materials mentioning features or use of this
  27. * software must display the following acknowledgment:
  28. * "This product includes software developed by the OpenSSL Project
  29. * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
  30. *
  31. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  32. * endorse or promote products derived from this software without
  33. * prior written permission. For written permission, please contact
  34. * openssl-core@openssl.org.
  35. *
  36. * 5. Products derived from this software may not be called "OpenSSL"
  37. * nor may "OpenSSL" appear in their names without prior written
  38. * permission of the OpenSSL Project.
  39. *
  40. * 6. Redistributions of any form whatsoever must retain the following
  41. * acknowledgment:
  42. * "This product includes software developed by the OpenSSL Project
  43. * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
  44. *
  45. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  46. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  47. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  48. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  49. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  50. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  51. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  52. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  53. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  54. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  55. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  56. * OF THE POSSIBILITY OF SUCH DAMAGE.
  57. * ====================================================================
  58. *
  59. * This product includes cryptographic software written by Eric Young
  60. * (eay@cryptsoft.com). This product includes software written by Tim
  61. * Hudson (tjh@cryptsoft.com).
  62. *
  63. */
  64. #include <stdio.h>
  65. #include <time.h>
  66. #include <cryptlib.h>
  67. #include <openssl/objects.h>
  68. #include <openssl/rand.h>
  69. #include <openssl/x509.h>
  70. #include <openssl/pem.h>
  71. #include <openssl/x509v3.h>
  72. #include <openssl/ocsp.h>
  73. /*
  74. * Utility functions related to sending OCSP requests and extracting relevant
  75. * information from the response.
  76. */
  77. /*
  78. * Add an OCSP_CERTID to an OCSP request. Return new OCSP_ONEREQ pointer:
  79. * useful if we want to add extensions.
  80. */
  81. OCSP_ONEREQ *OCSP_request_add0_id(OCSP_REQUEST *req, OCSP_CERTID *cid)
  82. {
  83. OCSP_ONEREQ *one = NULL;
  84. if (!(one = OCSP_ONEREQ_new()))
  85. goto err;
  86. if (one->reqCert)
  87. OCSP_CERTID_free(one->reqCert);
  88. one->reqCert = cid;
  89. if (req && !sk_OCSP_ONEREQ_push(req->tbsRequest->requestList, one))
  90. goto err;
  91. return one;
  92. err:
  93. OCSP_ONEREQ_free(one);
  94. return NULL;
  95. }
  96. /* Set requestorName from an X509_NAME structure */
  97. int OCSP_request_set1_name(OCSP_REQUEST *req, X509_NAME *nm)
  98. {
  99. GENERAL_NAME *gen;
  100. gen = GENERAL_NAME_new();
  101. if (gen == NULL)
  102. return 0;
  103. if (!X509_NAME_set(&gen->d.directoryName, nm)) {
  104. GENERAL_NAME_free(gen);
  105. return 0;
  106. }
  107. gen->type = GEN_DIRNAME;
  108. if (req->tbsRequest->requestorName)
  109. GENERAL_NAME_free(req->tbsRequest->requestorName);
  110. req->tbsRequest->requestorName = gen;
  111. return 1;
  112. }
  113. /* Add a certificate to an OCSP request */
  114. int OCSP_request_add1_cert(OCSP_REQUEST *req, X509 *cert)
  115. {
  116. OCSP_SIGNATURE *sig;
  117. if (!req->optionalSignature)
  118. req->optionalSignature = OCSP_SIGNATURE_new();
  119. sig = req->optionalSignature;
  120. if (!sig)
  121. return 0;
  122. if (!cert)
  123. return 1;
  124. if (!sig->certs && !(sig->certs = sk_X509_new_null()))
  125. return 0;
  126. if (!sk_X509_push(sig->certs, cert))
  127. return 0;
  128. CRYPTO_add(&cert->references, 1, CRYPTO_LOCK_X509);
  129. return 1;
  130. }
  131. /*
  132. * Sign an OCSP request set the requestorName to the subjec name of an
  133. * optional signers certificate and include one or more optional certificates
  134. * in the request. Behaves like PKCS7_sign().
  135. */
  136. int OCSP_request_sign(OCSP_REQUEST *req,
  137. X509 *signer,
  138. EVP_PKEY *key,
  139. const EVP_MD *dgst,
  140. STACK_OF(X509) *certs, unsigned long flags)
  141. {
  142. int i;
  143. OCSP_SIGNATURE *sig;
  144. X509 *x;
  145. if (!OCSP_request_set1_name(req, X509_get_subject_name(signer)))
  146. goto err;
  147. if (!(req->optionalSignature = sig = OCSP_SIGNATURE_new()))
  148. goto err;
  149. if (!dgst)
  150. dgst = EVP_sha1();
  151. if (key) {
  152. if (!X509_check_private_key(signer, key)) {
  153. OCSPerr(OCSP_F_OCSP_REQUEST_SIGN,
  154. OCSP_R_PRIVATE_KEY_DOES_NOT_MATCH_CERTIFICATE);
  155. goto err;
  156. }
  157. if (!OCSP_REQUEST_sign(req, key, dgst))
  158. goto err;
  159. }
  160. if (!(flags & OCSP_NOCERTS)) {
  161. if (!OCSP_request_add1_cert(req, signer))
  162. goto err;
  163. for (i = 0; i < sk_X509_num(certs); i++) {
  164. x = sk_X509_value(certs, i);
  165. if (!OCSP_request_add1_cert(req, x))
  166. goto err;
  167. }
  168. }
  169. return 1;
  170. err:
  171. OCSP_SIGNATURE_free(req->optionalSignature);
  172. req->optionalSignature = NULL;
  173. return 0;
  174. }
  175. /* Get response status */
  176. int OCSP_response_status(OCSP_RESPONSE *resp)
  177. {
  178. return ASN1_ENUMERATED_get(resp->responseStatus);
  179. }
  180. /*
  181. * Extract basic response from OCSP_RESPONSE or NULL if no basic response
  182. * present.
  183. */
  184. OCSP_BASICRESP *OCSP_response_get1_basic(OCSP_RESPONSE *resp)
  185. {
  186. OCSP_RESPBYTES *rb;
  187. rb = resp->responseBytes;
  188. if (!rb) {
  189. OCSPerr(OCSP_F_OCSP_RESPONSE_GET1_BASIC, OCSP_R_NO_RESPONSE_DATA);
  190. return NULL;
  191. }
  192. if (OBJ_obj2nid(rb->responseType) != NID_id_pkix_OCSP_basic) {
  193. OCSPerr(OCSP_F_OCSP_RESPONSE_GET1_BASIC, OCSP_R_NOT_BASIC_RESPONSE);
  194. return NULL;
  195. }
  196. return ASN1_item_unpack(rb->response, ASN1_ITEM_rptr(OCSP_BASICRESP));
  197. }
  198. /*
  199. * Return number of OCSP_SINGLERESP reponses present in a basic response.
  200. */
  201. int OCSP_resp_count(OCSP_BASICRESP *bs)
  202. {
  203. if (!bs)
  204. return -1;
  205. return sk_OCSP_SINGLERESP_num(bs->tbsResponseData->responses);
  206. }
  207. /* Extract an OCSP_SINGLERESP response with a given index */
  208. OCSP_SINGLERESP *OCSP_resp_get0(OCSP_BASICRESP *bs, int idx)
  209. {
  210. if (!bs)
  211. return NULL;
  212. return sk_OCSP_SINGLERESP_value(bs->tbsResponseData->responses, idx);
  213. }
  214. /* Look single response matching a given certificate ID */
  215. int OCSP_resp_find(OCSP_BASICRESP *bs, OCSP_CERTID *id, int last)
  216. {
  217. int i;
  218. STACK_OF(OCSP_SINGLERESP) *sresp;
  219. OCSP_SINGLERESP *single;
  220. if (!bs)
  221. return -1;
  222. if (last < 0)
  223. last = 0;
  224. else
  225. last++;
  226. sresp = bs->tbsResponseData->responses;
  227. for (i = last; i < sk_OCSP_SINGLERESP_num(sresp); i++) {
  228. single = sk_OCSP_SINGLERESP_value(sresp, i);
  229. if (!OCSP_id_cmp(id, single->certId))
  230. return i;
  231. }
  232. return -1;
  233. }
  234. /*
  235. * Extract status information from an OCSP_SINGLERESP structure. Note: the
  236. * revtime and reason values are only set if the certificate status is
  237. * revoked. Returns numerical value of status.
  238. */
  239. int OCSP_single_get0_status(OCSP_SINGLERESP *single, int *reason,
  240. ASN1_GENERALIZEDTIME **revtime,
  241. ASN1_GENERALIZEDTIME **thisupd,
  242. ASN1_GENERALIZEDTIME **nextupd)
  243. {
  244. int ret;
  245. OCSP_CERTSTATUS *cst;
  246. if (!single)
  247. return -1;
  248. cst = single->certStatus;
  249. ret = cst->type;
  250. if (ret == V_OCSP_CERTSTATUS_REVOKED) {
  251. OCSP_REVOKEDINFO *rev = cst->value.revoked;
  252. if (revtime)
  253. *revtime = rev->revocationTime;
  254. if (reason) {
  255. if (rev->revocationReason)
  256. *reason = ASN1_ENUMERATED_get(rev->revocationReason);
  257. else
  258. *reason = -1;
  259. }
  260. }
  261. if (thisupd)
  262. *thisupd = single->thisUpdate;
  263. if (nextupd)
  264. *nextupd = single->nextUpdate;
  265. return ret;
  266. }
  267. /*
  268. * This function combines the previous ones: look up a certificate ID and if
  269. * found extract status information. Return 0 is successful.
  270. */
  271. int OCSP_resp_find_status(OCSP_BASICRESP *bs, OCSP_CERTID *id, int *status,
  272. int *reason,
  273. ASN1_GENERALIZEDTIME **revtime,
  274. ASN1_GENERALIZEDTIME **thisupd,
  275. ASN1_GENERALIZEDTIME **nextupd)
  276. {
  277. int i;
  278. OCSP_SINGLERESP *single;
  279. i = OCSP_resp_find(bs, id, -1);
  280. /* Maybe check for multiple responses and give an error? */
  281. if (i < 0)
  282. return 0;
  283. single = OCSP_resp_get0(bs, i);
  284. i = OCSP_single_get0_status(single, reason, revtime, thisupd, nextupd);
  285. if (status)
  286. *status = i;
  287. return 1;
  288. }
  289. /*
  290. * Check validity of thisUpdate and nextUpdate fields. It is possible that
  291. * the request will take a few seconds to process and/or the time wont be
  292. * totally accurate. Therefore to avoid rejecting otherwise valid time we
  293. * allow the times to be within 'nsec' of the current time. Also to avoid
  294. * accepting very old responses without a nextUpdate field an optional maxage
  295. * parameter specifies the maximum age the thisUpdate field can be.
  296. */
  297. int OCSP_check_validity(ASN1_GENERALIZEDTIME *thisupd,
  298. ASN1_GENERALIZEDTIME *nextupd, long nsec, long maxsec)
  299. {
  300. int ret = 1;
  301. time_t t_now, t_tmp;
  302. time(&t_now);
  303. /* Check thisUpdate is valid and not more than nsec in the future */
  304. if (!ASN1_GENERALIZEDTIME_check(thisupd)) {
  305. OCSPerr(OCSP_F_OCSP_CHECK_VALIDITY, OCSP_R_ERROR_IN_THISUPDATE_FIELD);
  306. ret = 0;
  307. } else {
  308. t_tmp = t_now + nsec;
  309. if (X509_cmp_time(thisupd, &t_tmp) > 0) {
  310. OCSPerr(OCSP_F_OCSP_CHECK_VALIDITY, OCSP_R_STATUS_NOT_YET_VALID);
  311. ret = 0;
  312. }
  313. /*
  314. * If maxsec specified check thisUpdate is not more than maxsec in
  315. * the past
  316. */
  317. if (maxsec >= 0) {
  318. t_tmp = t_now - maxsec;
  319. if (X509_cmp_time(thisupd, &t_tmp) < 0) {
  320. OCSPerr(OCSP_F_OCSP_CHECK_VALIDITY, OCSP_R_STATUS_TOO_OLD);
  321. ret = 0;
  322. }
  323. }
  324. }
  325. if (!nextupd)
  326. return ret;
  327. /* Check nextUpdate is valid and not more than nsec in the past */
  328. if (!ASN1_GENERALIZEDTIME_check(nextupd)) {
  329. OCSPerr(OCSP_F_OCSP_CHECK_VALIDITY, OCSP_R_ERROR_IN_NEXTUPDATE_FIELD);
  330. ret = 0;
  331. } else {
  332. t_tmp = t_now - nsec;
  333. if (X509_cmp_time(nextupd, &t_tmp) < 0) {
  334. OCSPerr(OCSP_F_OCSP_CHECK_VALIDITY, OCSP_R_STATUS_EXPIRED);
  335. ret = 0;
  336. }
  337. }
  338. /* Also don't allow nextUpdate to precede thisUpdate */
  339. if (ASN1_STRING_cmp(nextupd, thisupd) < 0) {
  340. OCSPerr(OCSP_F_OCSP_CHECK_VALIDITY,
  341. OCSP_R_NEXTUPDATE_BEFORE_THISUPDATE);
  342. ret = 0;
  343. }
  344. return ret;
  345. }