2
0

ocsp_srv.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. /* ocsp_srv.c */
  2. /*
  3. * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL project
  4. * 2001.
  5. */
  6. /* ====================================================================
  7. * Copyright (c) 1998-2001 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. * openssl-core@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 <stdio.h>
  60. #include "internal/cryptlib.h"
  61. #include <openssl/objects.h>
  62. #include <openssl/rand.h>
  63. #include <openssl/x509.h>
  64. #include <openssl/pem.h>
  65. #include <openssl/x509v3.h>
  66. #include <openssl/ocsp.h>
  67. #include "ocsp_lcl.h"
  68. /*
  69. * Utility functions related to sending OCSP responses and extracting
  70. * relevant information from the request.
  71. */
  72. int OCSP_request_onereq_count(OCSP_REQUEST *req)
  73. {
  74. return sk_OCSP_ONEREQ_num(req->tbsRequest.requestList);
  75. }
  76. OCSP_ONEREQ *OCSP_request_onereq_get0(OCSP_REQUEST *req, int i)
  77. {
  78. return sk_OCSP_ONEREQ_value(req->tbsRequest.requestList, i);
  79. }
  80. OCSP_CERTID *OCSP_onereq_get0_id(OCSP_ONEREQ *one)
  81. {
  82. return one->reqCert;
  83. }
  84. int OCSP_id_get0_info(ASN1_OCTET_STRING **piNameHash, ASN1_OBJECT **pmd,
  85. ASN1_OCTET_STRING **pikeyHash,
  86. ASN1_INTEGER **pserial, OCSP_CERTID *cid)
  87. {
  88. if (!cid)
  89. return 0;
  90. if (pmd)
  91. *pmd = cid->hashAlgorithm.algorithm;
  92. if (piNameHash)
  93. *piNameHash = &cid->issuerNameHash;
  94. if (pikeyHash)
  95. *pikeyHash = &cid->issuerKeyHash;
  96. if (pserial)
  97. *pserial = &cid->serialNumber;
  98. return 1;
  99. }
  100. int OCSP_request_is_signed(OCSP_REQUEST *req)
  101. {
  102. if (req->optionalSignature)
  103. return 1;
  104. return 0;
  105. }
  106. /* Create an OCSP response and encode an optional basic response */
  107. OCSP_RESPONSE *OCSP_response_create(int status, OCSP_BASICRESP *bs)
  108. {
  109. OCSP_RESPONSE *rsp = NULL;
  110. if ((rsp = OCSP_RESPONSE_new()) == NULL)
  111. goto err;
  112. if (!(ASN1_ENUMERATED_set(rsp->responseStatus, status)))
  113. goto err;
  114. if (!bs)
  115. return rsp;
  116. if ((rsp->responseBytes = OCSP_RESPBYTES_new()) == NULL)
  117. goto err;
  118. rsp->responseBytes->responseType = OBJ_nid2obj(NID_id_pkix_OCSP_basic);
  119. if (!ASN1_item_pack
  120. (bs, ASN1_ITEM_rptr(OCSP_BASICRESP), &rsp->responseBytes->response))
  121. goto err;
  122. return rsp;
  123. err:
  124. OCSP_RESPONSE_free(rsp);
  125. return NULL;
  126. }
  127. OCSP_SINGLERESP *OCSP_basic_add1_status(OCSP_BASICRESP *rsp,
  128. OCSP_CERTID *cid,
  129. int status, int reason,
  130. ASN1_TIME *revtime,
  131. ASN1_TIME *thisupd,
  132. ASN1_TIME *nextupd)
  133. {
  134. OCSP_SINGLERESP *single = NULL;
  135. OCSP_CERTSTATUS *cs;
  136. OCSP_REVOKEDINFO *ri;
  137. if (rsp->tbsResponseData.responses == NULL
  138. && (rsp->tbsResponseData.responses
  139. = sk_OCSP_SINGLERESP_new_null()) == NULL)
  140. goto err;
  141. if ((single = OCSP_SINGLERESP_new()) == NULL)
  142. goto err;
  143. if (!ASN1_TIME_to_generalizedtime(thisupd, &single->thisUpdate))
  144. goto err;
  145. if (nextupd &&
  146. !ASN1_TIME_to_generalizedtime(nextupd, &single->nextUpdate))
  147. goto err;
  148. OCSP_CERTID_free(single->certId);
  149. if ((single->certId = OCSP_CERTID_dup(cid)) == NULL)
  150. goto err;
  151. cs = single->certStatus;
  152. switch (cs->type = status) {
  153. case V_OCSP_CERTSTATUS_REVOKED:
  154. if (!revtime) {
  155. OCSPerr(OCSP_F_OCSP_BASIC_ADD1_STATUS, OCSP_R_NO_REVOKED_TIME);
  156. goto err;
  157. }
  158. if ((cs->value.revoked = ri = OCSP_REVOKEDINFO_new()) == NULL)
  159. goto err;
  160. if (!ASN1_TIME_to_generalizedtime(revtime, &ri->revocationTime))
  161. goto err;
  162. if (reason != OCSP_REVOKED_STATUS_NOSTATUS) {
  163. if ((ri->revocationReason = ASN1_ENUMERATED_new()) == NULL)
  164. goto err;
  165. if (!(ASN1_ENUMERATED_set(ri->revocationReason, reason)))
  166. goto err;
  167. }
  168. break;
  169. case V_OCSP_CERTSTATUS_GOOD:
  170. if ((cs->value.good = ASN1_NULL_new()) == NULL)
  171. goto err;
  172. break;
  173. case V_OCSP_CERTSTATUS_UNKNOWN:
  174. if ((cs->value.unknown = ASN1_NULL_new()) == NULL)
  175. goto err;
  176. break;
  177. default:
  178. goto err;
  179. }
  180. if (!(sk_OCSP_SINGLERESP_push(rsp->tbsResponseData.responses, single)))
  181. goto err;
  182. return single;
  183. err:
  184. OCSP_SINGLERESP_free(single);
  185. return NULL;
  186. }
  187. /* Add a certificate to an OCSP request */
  188. int OCSP_basic_add1_cert(OCSP_BASICRESP *resp, X509 *cert)
  189. {
  190. if (resp->certs == NULL
  191. && (resp->certs = sk_X509_new_null()) == NULL)
  192. return 0;
  193. if (!sk_X509_push(resp->certs, cert))
  194. return 0;
  195. X509_up_ref(cert);
  196. return 1;
  197. }
  198. int OCSP_basic_sign(OCSP_BASICRESP *brsp,
  199. X509 *signer, EVP_PKEY *key, const EVP_MD *dgst,
  200. STACK_OF(X509) *certs, unsigned long flags)
  201. {
  202. int i;
  203. OCSP_RESPID *rid;
  204. if (!X509_check_private_key(signer, key)) {
  205. OCSPerr(OCSP_F_OCSP_BASIC_SIGN,
  206. OCSP_R_PRIVATE_KEY_DOES_NOT_MATCH_CERTIFICATE);
  207. goto err;
  208. }
  209. if (!(flags & OCSP_NOCERTS)) {
  210. if (!OCSP_basic_add1_cert(brsp, signer))
  211. goto err;
  212. for (i = 0; i < sk_X509_num(certs); i++) {
  213. X509 *tmpcert = sk_X509_value(certs, i);
  214. if (!OCSP_basic_add1_cert(brsp, tmpcert))
  215. goto err;
  216. }
  217. }
  218. rid = &brsp->tbsResponseData.responderId;
  219. if (flags & OCSP_RESPID_KEY) {
  220. unsigned char md[SHA_DIGEST_LENGTH];
  221. X509_pubkey_digest(signer, EVP_sha1(), md, NULL);
  222. if ((rid->value.byKey = ASN1_OCTET_STRING_new()) == NULL)
  223. goto err;
  224. if (!(ASN1_OCTET_STRING_set(rid->value.byKey, md, SHA_DIGEST_LENGTH)))
  225. goto err;
  226. rid->type = V_OCSP_RESPID_KEY;
  227. } else {
  228. if (!X509_NAME_set(&rid->value.byName, X509_get_subject_name(signer)))
  229. goto err;
  230. rid->type = V_OCSP_RESPID_NAME;
  231. }
  232. if (!(flags & OCSP_NOTIME) &&
  233. !X509_gmtime_adj(brsp->tbsResponseData.producedAt, 0))
  234. goto err;
  235. /*
  236. * Right now, I think that not doing double hashing is the right thing.
  237. * -- Richard Levitte
  238. */
  239. if (!OCSP_BASICRESP_sign(brsp, key, dgst, 0))
  240. goto err;
  241. return 1;
  242. err:
  243. return 0;
  244. }