ocsp_cl.c 12 KB

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