ocsp_cl.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  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 (key) {
  150. if (!X509_check_private_key(signer, key)) {
  151. OCSPerr(OCSP_F_OCSP_REQUEST_SIGN,
  152. OCSP_R_PRIVATE_KEY_DOES_NOT_MATCH_CERTIFICATE);
  153. goto err;
  154. }
  155. if (!OCSP_REQUEST_sign(req, key, dgst))
  156. goto err;
  157. }
  158. if (!(flags & OCSP_NOCERTS)) {
  159. if (!OCSP_request_add1_cert(req, signer))
  160. goto err;
  161. for (i = 0; i < sk_X509_num(certs); i++) {
  162. x = sk_X509_value(certs, i);
  163. if (!OCSP_request_add1_cert(req, x))
  164. goto err;
  165. }
  166. }
  167. return 1;
  168. err:
  169. OCSP_SIGNATURE_free(req->optionalSignature);
  170. req->optionalSignature = NULL;
  171. return 0;
  172. }
  173. /* Get response status */
  174. int OCSP_response_status(OCSP_RESPONSE *resp)
  175. {
  176. return ASN1_ENUMERATED_get(resp->responseStatus);
  177. }
  178. /*
  179. * Extract basic response from OCSP_RESPONSE or NULL if no basic response
  180. * present.
  181. */
  182. OCSP_BASICRESP *OCSP_response_get1_basic(OCSP_RESPONSE *resp)
  183. {
  184. OCSP_RESPBYTES *rb;
  185. rb = resp->responseBytes;
  186. if (!rb) {
  187. OCSPerr(OCSP_F_OCSP_RESPONSE_GET1_BASIC, OCSP_R_NO_RESPONSE_DATA);
  188. return NULL;
  189. }
  190. if (OBJ_obj2nid(rb->responseType) != NID_id_pkix_OCSP_basic) {
  191. OCSPerr(OCSP_F_OCSP_RESPONSE_GET1_BASIC, OCSP_R_NOT_BASIC_RESPONSE);
  192. return NULL;
  193. }
  194. return ASN1_item_unpack(rb->response, ASN1_ITEM_rptr(OCSP_BASICRESP));
  195. }
  196. /*
  197. * Return number of OCSP_SINGLERESP reponses present in a basic response.
  198. */
  199. int OCSP_resp_count(OCSP_BASICRESP *bs)
  200. {
  201. if (!bs)
  202. return -1;
  203. return sk_OCSP_SINGLERESP_num(bs->tbsResponseData->responses);
  204. }
  205. /* Extract an OCSP_SINGLERESP response with a given index */
  206. OCSP_SINGLERESP *OCSP_resp_get0(OCSP_BASICRESP *bs, int idx)
  207. {
  208. if (!bs)
  209. return NULL;
  210. return sk_OCSP_SINGLERESP_value(bs->tbsResponseData->responses, idx);
  211. }
  212. /* Look single response matching a given certificate ID */
  213. int OCSP_resp_find(OCSP_BASICRESP *bs, OCSP_CERTID *id, int last)
  214. {
  215. int i;
  216. STACK_OF(OCSP_SINGLERESP) *sresp;
  217. OCSP_SINGLERESP *single;
  218. if (!bs)
  219. return -1;
  220. if (last < 0)
  221. last = 0;
  222. else
  223. last++;
  224. sresp = bs->tbsResponseData->responses;
  225. for (i = last; i < sk_OCSP_SINGLERESP_num(sresp); i++) {
  226. single = sk_OCSP_SINGLERESP_value(sresp, i);
  227. if (!OCSP_id_cmp(id, single->certId))
  228. return i;
  229. }
  230. return -1;
  231. }
  232. /*
  233. * Extract status information from an OCSP_SINGLERESP structure. Note: the
  234. * revtime and reason values are only set if the certificate status is
  235. * revoked. Returns numerical value of status.
  236. */
  237. int OCSP_single_get0_status(OCSP_SINGLERESP *single, int *reason,
  238. ASN1_GENERALIZEDTIME **revtime,
  239. ASN1_GENERALIZEDTIME **thisupd,
  240. ASN1_GENERALIZEDTIME **nextupd)
  241. {
  242. int ret;
  243. OCSP_CERTSTATUS *cst;
  244. if (!single)
  245. return -1;
  246. cst = single->certStatus;
  247. ret = cst->type;
  248. if (ret == V_OCSP_CERTSTATUS_REVOKED) {
  249. OCSP_REVOKEDINFO *rev = cst->value.revoked;
  250. if (revtime)
  251. *revtime = rev->revocationTime;
  252. if (reason) {
  253. if (rev->revocationReason)
  254. *reason = ASN1_ENUMERATED_get(rev->revocationReason);
  255. else
  256. *reason = -1;
  257. }
  258. }
  259. if (thisupd)
  260. *thisupd = single->thisUpdate;
  261. if (nextupd)
  262. *nextupd = single->nextUpdate;
  263. return ret;
  264. }
  265. /*
  266. * This function combines the previous ones: look up a certificate ID and if
  267. * found extract status information. Return 0 is successful.
  268. */
  269. int OCSP_resp_find_status(OCSP_BASICRESP *bs, OCSP_CERTID *id, int *status,
  270. int *reason,
  271. ASN1_GENERALIZEDTIME **revtime,
  272. ASN1_GENERALIZEDTIME **thisupd,
  273. ASN1_GENERALIZEDTIME **nextupd)
  274. {
  275. int i;
  276. OCSP_SINGLERESP *single;
  277. i = OCSP_resp_find(bs, id, -1);
  278. /* Maybe check for multiple responses and give an error? */
  279. if (i < 0)
  280. return 0;
  281. single = OCSP_resp_get0(bs, i);
  282. i = OCSP_single_get0_status(single, reason, revtime, thisupd, nextupd);
  283. if (status)
  284. *status = i;
  285. return 1;
  286. }
  287. /*
  288. * Check validity of thisUpdate and nextUpdate fields. It is possible that
  289. * the request will take a few seconds to process and/or the time wont be
  290. * totally accurate. Therefore to avoid rejecting otherwise valid time we
  291. * allow the times to be within 'nsec' of the current time. Also to avoid
  292. * accepting very old responses without a nextUpdate field an optional maxage
  293. * parameter specifies the maximum age the thisUpdate field can be.
  294. */
  295. int OCSP_check_validity(ASN1_GENERALIZEDTIME *thisupd,
  296. ASN1_GENERALIZEDTIME *nextupd, long nsec, long maxsec)
  297. {
  298. int ret = 1;
  299. time_t t_now, t_tmp;
  300. time(&t_now);
  301. /* Check thisUpdate is valid and not more than nsec in the future */
  302. if (!ASN1_GENERALIZEDTIME_check(thisupd)) {
  303. OCSPerr(OCSP_F_OCSP_CHECK_VALIDITY, OCSP_R_ERROR_IN_THISUPDATE_FIELD);
  304. ret = 0;
  305. } else {
  306. t_tmp = t_now + nsec;
  307. if (X509_cmp_time(thisupd, &t_tmp) > 0) {
  308. OCSPerr(OCSP_F_OCSP_CHECK_VALIDITY, OCSP_R_STATUS_NOT_YET_VALID);
  309. ret = 0;
  310. }
  311. /*
  312. * If maxsec specified check thisUpdate is not more than maxsec in
  313. * the past
  314. */
  315. if (maxsec >= 0) {
  316. t_tmp = t_now - maxsec;
  317. if (X509_cmp_time(thisupd, &t_tmp) < 0) {
  318. OCSPerr(OCSP_F_OCSP_CHECK_VALIDITY, OCSP_R_STATUS_TOO_OLD);
  319. ret = 0;
  320. }
  321. }
  322. }
  323. if (!nextupd)
  324. return ret;
  325. /* Check nextUpdate is valid and not more than nsec in the past */
  326. if (!ASN1_GENERALIZEDTIME_check(nextupd)) {
  327. OCSPerr(OCSP_F_OCSP_CHECK_VALIDITY, OCSP_R_ERROR_IN_NEXTUPDATE_FIELD);
  328. ret = 0;
  329. } else {
  330. t_tmp = t_now - nsec;
  331. if (X509_cmp_time(nextupd, &t_tmp) < 0) {
  332. OCSPerr(OCSP_F_OCSP_CHECK_VALIDITY, OCSP_R_STATUS_EXPIRED);
  333. ret = 0;
  334. }
  335. }
  336. /* Also don't allow nextUpdate to precede thisUpdate */
  337. if (ASN1_STRING_cmp(nextupd, thisupd) < 0) {
  338. OCSPerr(OCSP_F_OCSP_CHECK_VALIDITY,
  339. OCSP_R_NEXTUPDATE_BEFORE_THISUPDATE);
  340. ret = 0;
  341. }
  342. return ret;
  343. }