ocsp_srv.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. /*
  2. * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL project
  3. * 2001.
  4. */
  5. /* ====================================================================
  6. * Copyright (c) 1998-2001 The OpenSSL Project. All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. *
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. *
  15. * 2. Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in
  17. * the documentation and/or other materials provided with the
  18. * distribution.
  19. *
  20. * 3. All advertising materials mentioning features or use of this
  21. * software must display the following acknowledgment:
  22. * "This product includes software developed by the OpenSSL Project
  23. * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
  24. *
  25. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  26. * endorse or promote products derived from this software without
  27. * prior written permission. For written permission, please contact
  28. * openssl-core@openssl.org.
  29. *
  30. * 5. Products derived from this software may not be called "OpenSSL"
  31. * nor may "OpenSSL" appear in their names without prior written
  32. * permission of the OpenSSL Project.
  33. *
  34. * 6. Redistributions of any form whatsoever must retain the following
  35. * acknowledgment:
  36. * "This product includes software developed by the OpenSSL Project
  37. * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
  38. *
  39. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  40. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  41. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  42. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  43. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  44. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  45. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  46. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  47. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  48. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  49. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  50. * OF THE POSSIBILITY OF SUCH DAMAGE.
  51. * ====================================================================
  52. *
  53. * This product includes cryptographic software written by Eric Young
  54. * (eay@cryptsoft.com). This product includes software written by Tim
  55. * Hudson (tjh@cryptsoft.com).
  56. *
  57. */
  58. #include <stdio.h>
  59. #include "internal/cryptlib.h"
  60. #include <openssl/objects.h>
  61. #include <openssl/rand.h>
  62. #include <openssl/x509.h>
  63. #include <openssl/pem.h>
  64. #include <openssl/x509v3.h>
  65. #include <openssl/ocsp.h>
  66. #include "ocsp_lcl.h"
  67. /*
  68. * Utility functions related to sending OCSP responses and extracting
  69. * relevant information from the request.
  70. */
  71. int OCSP_request_onereq_count(OCSP_REQUEST *req)
  72. {
  73. return sk_OCSP_ONEREQ_num(req->tbsRequest.requestList);
  74. }
  75. OCSP_ONEREQ *OCSP_request_onereq_get0(OCSP_REQUEST *req, int i)
  76. {
  77. return sk_OCSP_ONEREQ_value(req->tbsRequest.requestList, i);
  78. }
  79. OCSP_CERTID *OCSP_onereq_get0_id(OCSP_ONEREQ *one)
  80. {
  81. return one->reqCert;
  82. }
  83. int OCSP_id_get0_info(ASN1_OCTET_STRING **piNameHash, ASN1_OBJECT **pmd,
  84. ASN1_OCTET_STRING **pikeyHash,
  85. ASN1_INTEGER **pserial, OCSP_CERTID *cid)
  86. {
  87. if (!cid)
  88. return 0;
  89. if (pmd)
  90. *pmd = cid->hashAlgorithm.algorithm;
  91. if (piNameHash)
  92. *piNameHash = &cid->issuerNameHash;
  93. if (pikeyHash)
  94. *pikeyHash = &cid->issuerKeyHash;
  95. if (pserial)
  96. *pserial = &cid->serialNumber;
  97. return 1;
  98. }
  99. int OCSP_request_is_signed(OCSP_REQUEST *req)
  100. {
  101. if (req->optionalSignature)
  102. return 1;
  103. return 0;
  104. }
  105. /* Create an OCSP response and encode an optional basic response */
  106. OCSP_RESPONSE *OCSP_response_create(int status, OCSP_BASICRESP *bs)
  107. {
  108. OCSP_RESPONSE *rsp = NULL;
  109. if ((rsp = OCSP_RESPONSE_new()) == NULL)
  110. goto err;
  111. if (!(ASN1_ENUMERATED_set(rsp->responseStatus, status)))
  112. goto err;
  113. if (!bs)
  114. return rsp;
  115. if ((rsp->responseBytes = OCSP_RESPBYTES_new()) == NULL)
  116. goto err;
  117. rsp->responseBytes->responseType = OBJ_nid2obj(NID_id_pkix_OCSP_basic);
  118. if (!ASN1_item_pack
  119. (bs, ASN1_ITEM_rptr(OCSP_BASICRESP), &rsp->responseBytes->response))
  120. goto err;
  121. return rsp;
  122. err:
  123. OCSP_RESPONSE_free(rsp);
  124. return NULL;
  125. }
  126. OCSP_SINGLERESP *OCSP_basic_add1_status(OCSP_BASICRESP *rsp,
  127. OCSP_CERTID *cid,
  128. int status, int reason,
  129. ASN1_TIME *revtime,
  130. ASN1_TIME *thisupd,
  131. ASN1_TIME *nextupd)
  132. {
  133. OCSP_SINGLERESP *single = NULL;
  134. OCSP_CERTSTATUS *cs;
  135. OCSP_REVOKEDINFO *ri;
  136. if (rsp->tbsResponseData.responses == NULL
  137. && (rsp->tbsResponseData.responses
  138. = sk_OCSP_SINGLERESP_new_null()) == NULL)
  139. goto err;
  140. if ((single = OCSP_SINGLERESP_new()) == NULL)
  141. goto err;
  142. if (!ASN1_TIME_to_generalizedtime(thisupd, &single->thisUpdate))
  143. goto err;
  144. if (nextupd &&
  145. !ASN1_TIME_to_generalizedtime(nextupd, &single->nextUpdate))
  146. goto err;
  147. OCSP_CERTID_free(single->certId);
  148. if ((single->certId = OCSP_CERTID_dup(cid)) == NULL)
  149. goto err;
  150. cs = single->certStatus;
  151. switch (cs->type = status) {
  152. case V_OCSP_CERTSTATUS_REVOKED:
  153. if (!revtime) {
  154. OCSPerr(OCSP_F_OCSP_BASIC_ADD1_STATUS, OCSP_R_NO_REVOKED_TIME);
  155. goto err;
  156. }
  157. if ((cs->value.revoked = ri = OCSP_REVOKEDINFO_new()) == NULL)
  158. goto err;
  159. if (!ASN1_TIME_to_generalizedtime(revtime, &ri->revocationTime))
  160. goto err;
  161. if (reason != OCSP_REVOKED_STATUS_NOSTATUS) {
  162. if ((ri->revocationReason = ASN1_ENUMERATED_new()) == NULL)
  163. goto err;
  164. if (!(ASN1_ENUMERATED_set(ri->revocationReason, reason)))
  165. goto err;
  166. }
  167. break;
  168. case V_OCSP_CERTSTATUS_GOOD:
  169. if ((cs->value.good = ASN1_NULL_new()) == NULL)
  170. goto err;
  171. break;
  172. case V_OCSP_CERTSTATUS_UNKNOWN:
  173. if ((cs->value.unknown = ASN1_NULL_new()) == NULL)
  174. goto err;
  175. break;
  176. default:
  177. goto err;
  178. }
  179. if (!(sk_OCSP_SINGLERESP_push(rsp->tbsResponseData.responses, single)))
  180. goto err;
  181. return single;
  182. err:
  183. OCSP_SINGLERESP_free(single);
  184. return NULL;
  185. }
  186. /* Add a certificate to an OCSP request */
  187. int OCSP_basic_add1_cert(OCSP_BASICRESP *resp, X509 *cert)
  188. {
  189. if (resp->certs == NULL
  190. && (resp->certs = sk_X509_new_null()) == NULL)
  191. return 0;
  192. if (!sk_X509_push(resp->certs, cert))
  193. return 0;
  194. X509_up_ref(cert);
  195. return 1;
  196. }
  197. int OCSP_basic_sign(OCSP_BASICRESP *brsp,
  198. X509 *signer, EVP_PKEY *key, const EVP_MD *dgst,
  199. STACK_OF(X509) *certs, unsigned long flags)
  200. {
  201. int i;
  202. OCSP_RESPID *rid;
  203. if (!X509_check_private_key(signer, key)) {
  204. OCSPerr(OCSP_F_OCSP_BASIC_SIGN,
  205. OCSP_R_PRIVATE_KEY_DOES_NOT_MATCH_CERTIFICATE);
  206. goto err;
  207. }
  208. if (!(flags & OCSP_NOCERTS)) {
  209. if (!OCSP_basic_add1_cert(brsp, signer))
  210. goto err;
  211. for (i = 0; i < sk_X509_num(certs); i++) {
  212. X509 *tmpcert = sk_X509_value(certs, i);
  213. if (!OCSP_basic_add1_cert(brsp, tmpcert))
  214. goto err;
  215. }
  216. }
  217. rid = &brsp->tbsResponseData.responderId;
  218. if (flags & OCSP_RESPID_KEY) {
  219. unsigned char md[SHA_DIGEST_LENGTH];
  220. X509_pubkey_digest(signer, EVP_sha1(), md, NULL);
  221. if ((rid->value.byKey = ASN1_OCTET_STRING_new()) == NULL)
  222. goto err;
  223. if (!(ASN1_OCTET_STRING_set(rid->value.byKey, md, SHA_DIGEST_LENGTH)))
  224. goto err;
  225. rid->type = V_OCSP_RESPID_KEY;
  226. } else {
  227. if (!X509_NAME_set(&rid->value.byName, X509_get_subject_name(signer)))
  228. goto err;
  229. rid->type = V_OCSP_RESPID_NAME;
  230. }
  231. if (!(flags & OCSP_NOTIME) &&
  232. !X509_gmtime_adj(brsp->tbsResponseData.producedAt, 0))
  233. goto err;
  234. /*
  235. * Right now, I think that not doing double hashing is the right thing.
  236. * -- Richard Levitte
  237. */
  238. if (!OCSP_BASICRESP_sign(brsp, key, dgst, 0))
  239. goto err;
  240. return 1;
  241. err:
  242. return 0;
  243. }