ocsp_srv.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. /*
  2. * Copyright 2001-2018 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the OpenSSL license (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #include <stdio.h>
  10. #include "internal/cryptlib.h"
  11. #include <openssl/objects.h>
  12. #include <openssl/x509.h>
  13. #include <openssl/pem.h>
  14. #include <openssl/x509v3.h>
  15. #include <openssl/ocsp.h>
  16. #include "ocsp_lcl.h"
  17. /*
  18. * Utility functions related to sending OCSP responses and extracting
  19. * relevant information from the request.
  20. */
  21. int OCSP_request_onereq_count(OCSP_REQUEST *req)
  22. {
  23. return sk_OCSP_ONEREQ_num(req->tbsRequest.requestList);
  24. }
  25. OCSP_ONEREQ *OCSP_request_onereq_get0(OCSP_REQUEST *req, int i)
  26. {
  27. return sk_OCSP_ONEREQ_value(req->tbsRequest.requestList, i);
  28. }
  29. OCSP_CERTID *OCSP_onereq_get0_id(OCSP_ONEREQ *one)
  30. {
  31. return one->reqCert;
  32. }
  33. int OCSP_id_get0_info(ASN1_OCTET_STRING **piNameHash, ASN1_OBJECT **pmd,
  34. ASN1_OCTET_STRING **pikeyHash,
  35. ASN1_INTEGER **pserial, OCSP_CERTID *cid)
  36. {
  37. if (!cid)
  38. return 0;
  39. if (pmd)
  40. *pmd = cid->hashAlgorithm.algorithm;
  41. if (piNameHash)
  42. *piNameHash = &cid->issuerNameHash;
  43. if (pikeyHash)
  44. *pikeyHash = &cid->issuerKeyHash;
  45. if (pserial)
  46. *pserial = &cid->serialNumber;
  47. return 1;
  48. }
  49. int OCSP_request_is_signed(OCSP_REQUEST *req)
  50. {
  51. if (req->optionalSignature)
  52. return 1;
  53. return 0;
  54. }
  55. /* Create an OCSP response and encode an optional basic response */
  56. OCSP_RESPONSE *OCSP_response_create(int status, OCSP_BASICRESP *bs)
  57. {
  58. OCSP_RESPONSE *rsp = NULL;
  59. if ((rsp = OCSP_RESPONSE_new()) == NULL)
  60. goto err;
  61. if (!(ASN1_ENUMERATED_set(rsp->responseStatus, status)))
  62. goto err;
  63. if (!bs)
  64. return rsp;
  65. if ((rsp->responseBytes = OCSP_RESPBYTES_new()) == NULL)
  66. goto err;
  67. rsp->responseBytes->responseType = OBJ_nid2obj(NID_id_pkix_OCSP_basic);
  68. if (!ASN1_item_pack
  69. (bs, ASN1_ITEM_rptr(OCSP_BASICRESP), &rsp->responseBytes->response))
  70. goto err;
  71. return rsp;
  72. err:
  73. OCSP_RESPONSE_free(rsp);
  74. return NULL;
  75. }
  76. OCSP_SINGLERESP *OCSP_basic_add1_status(OCSP_BASICRESP *rsp,
  77. OCSP_CERTID *cid,
  78. int status, int reason,
  79. ASN1_TIME *revtime,
  80. ASN1_TIME *thisupd,
  81. ASN1_TIME *nextupd)
  82. {
  83. OCSP_SINGLERESP *single = NULL;
  84. OCSP_CERTSTATUS *cs;
  85. OCSP_REVOKEDINFO *ri;
  86. if (rsp->tbsResponseData.responses == NULL
  87. && (rsp->tbsResponseData.responses
  88. = sk_OCSP_SINGLERESP_new_null()) == NULL)
  89. goto err;
  90. if ((single = OCSP_SINGLERESP_new()) == NULL)
  91. goto err;
  92. if (!ASN1_TIME_to_generalizedtime(thisupd, &single->thisUpdate))
  93. goto err;
  94. if (nextupd &&
  95. !ASN1_TIME_to_generalizedtime(nextupd, &single->nextUpdate))
  96. goto err;
  97. OCSP_CERTID_free(single->certId);
  98. if ((single->certId = OCSP_CERTID_dup(cid)) == NULL)
  99. goto err;
  100. cs = single->certStatus;
  101. switch (cs->type = status) {
  102. case V_OCSP_CERTSTATUS_REVOKED:
  103. if (!revtime) {
  104. OCSPerr(OCSP_F_OCSP_BASIC_ADD1_STATUS, OCSP_R_NO_REVOKED_TIME);
  105. goto err;
  106. }
  107. if ((cs->value.revoked = ri = OCSP_REVOKEDINFO_new()) == NULL)
  108. goto err;
  109. if (!ASN1_TIME_to_generalizedtime(revtime, &ri->revocationTime))
  110. goto err;
  111. if (reason != OCSP_REVOKED_STATUS_NOSTATUS) {
  112. if ((ri->revocationReason = ASN1_ENUMERATED_new()) == NULL)
  113. goto err;
  114. if (!(ASN1_ENUMERATED_set(ri->revocationReason, reason)))
  115. goto err;
  116. }
  117. break;
  118. case V_OCSP_CERTSTATUS_GOOD:
  119. if ((cs->value.good = ASN1_NULL_new()) == NULL)
  120. goto err;
  121. break;
  122. case V_OCSP_CERTSTATUS_UNKNOWN:
  123. if ((cs->value.unknown = ASN1_NULL_new()) == NULL)
  124. goto err;
  125. break;
  126. default:
  127. goto err;
  128. }
  129. if (!(sk_OCSP_SINGLERESP_push(rsp->tbsResponseData.responses, single)))
  130. goto err;
  131. return single;
  132. err:
  133. OCSP_SINGLERESP_free(single);
  134. return NULL;
  135. }
  136. /* Add a certificate to an OCSP request */
  137. int OCSP_basic_add1_cert(OCSP_BASICRESP *resp, X509 *cert)
  138. {
  139. if (resp->certs == NULL
  140. && (resp->certs = sk_X509_new_null()) == NULL)
  141. return 0;
  142. if (!sk_X509_push(resp->certs, cert))
  143. return 0;
  144. X509_up_ref(cert);
  145. return 1;
  146. }
  147. /*
  148. * Sign an OCSP response using the parameters contained in the digest context,
  149. * set the responderID to the subject name in the signer's certificate, and
  150. * include one or more optional certificates in the response.
  151. */
  152. int OCSP_basic_sign_ctx(OCSP_BASICRESP *brsp,
  153. X509 *signer, EVP_MD_CTX *ctx,
  154. STACK_OF(X509) *certs, unsigned long flags)
  155. {
  156. int i;
  157. OCSP_RESPID *rid;
  158. EVP_PKEY *pkey;
  159. if (ctx == NULL || EVP_MD_CTX_pkey_ctx(ctx) == NULL) {
  160. OCSPerr(OCSP_F_OCSP_BASIC_SIGN_CTX, OCSP_R_NO_SIGNER_KEY);
  161. goto err;
  162. }
  163. pkey = EVP_PKEY_CTX_get0_pkey(EVP_MD_CTX_pkey_ctx(ctx));
  164. if (pkey == NULL || !X509_check_private_key(signer, pkey)) {
  165. OCSPerr(OCSP_F_OCSP_BASIC_SIGN_CTX,
  166. OCSP_R_PRIVATE_KEY_DOES_NOT_MATCH_CERTIFICATE);
  167. goto err;
  168. }
  169. if (!(flags & OCSP_NOCERTS)) {
  170. if (!OCSP_basic_add1_cert(brsp, signer))
  171. goto err;
  172. for (i = 0; i < sk_X509_num(certs); i++) {
  173. X509 *tmpcert = sk_X509_value(certs, i);
  174. if (!OCSP_basic_add1_cert(brsp, tmpcert))
  175. goto err;
  176. }
  177. }
  178. rid = &brsp->tbsResponseData.responderId;
  179. if (flags & OCSP_RESPID_KEY) {
  180. if (!OCSP_RESPID_set_by_key(rid, signer))
  181. goto err;
  182. } else if (!OCSP_RESPID_set_by_name(rid, signer)) {
  183. goto err;
  184. }
  185. if (!(flags & OCSP_NOTIME) &&
  186. !X509_gmtime_adj(brsp->tbsResponseData.producedAt, 0))
  187. goto err;
  188. /*
  189. * Right now, I think that not doing double hashing is the right thing.
  190. * -- Richard Levitte
  191. */
  192. if (!OCSP_BASICRESP_sign_ctx(brsp, ctx, 0))
  193. goto err;
  194. return 1;
  195. err:
  196. return 0;
  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. EVP_MD_CTX *ctx = EVP_MD_CTX_new();
  203. EVP_PKEY_CTX *pkctx = NULL;
  204. int i;
  205. if (!EVP_DigestSignInit(ctx, &pkctx, dgst, NULL, key)) {
  206. EVP_MD_CTX_free(ctx);
  207. return 0;
  208. }
  209. i = OCSP_basic_sign_ctx(brsp, signer, ctx, certs, flags);
  210. EVP_MD_CTX_free(ctx);
  211. return i;
  212. }
  213. int OCSP_RESPID_set_by_name(OCSP_RESPID *respid, X509 *cert)
  214. {
  215. if (!X509_NAME_set(&respid->value.byName, X509_get_subject_name(cert)))
  216. return 0;
  217. respid->type = V_OCSP_RESPID_NAME;
  218. return 1;
  219. }
  220. int OCSP_RESPID_set_by_key(OCSP_RESPID *respid, X509 *cert)
  221. {
  222. ASN1_OCTET_STRING *byKey = NULL;
  223. unsigned char md[SHA_DIGEST_LENGTH];
  224. /* RFC2560 requires SHA1 */
  225. if (!X509_pubkey_digest(cert, EVP_sha1(), md, NULL))
  226. return 0;
  227. byKey = ASN1_OCTET_STRING_new();
  228. if (byKey == NULL)
  229. return 0;
  230. if (!(ASN1_OCTET_STRING_set(byKey, md, SHA_DIGEST_LENGTH))) {
  231. ASN1_OCTET_STRING_free(byKey);
  232. return 0;
  233. }
  234. respid->type = V_OCSP_RESPID_KEY;
  235. respid->value.byKey = byKey;
  236. return 1;
  237. }
  238. int OCSP_RESPID_match(OCSP_RESPID *respid, X509 *cert)
  239. {
  240. if (respid->type == V_OCSP_RESPID_KEY) {
  241. unsigned char md[SHA_DIGEST_LENGTH];
  242. if (respid->value.byKey == NULL)
  243. return 0;
  244. /* RFC2560 requires SHA1 */
  245. if (!X509_pubkey_digest(cert, EVP_sha1(), md, NULL))
  246. return 0;
  247. return (ASN1_STRING_length(respid->value.byKey) == SHA_DIGEST_LENGTH)
  248. && (memcmp(ASN1_STRING_get0_data(respid->value.byKey), md,
  249. SHA_DIGEST_LENGTH) == 0);
  250. } else if (respid->type == V_OCSP_RESPID_NAME) {
  251. if (respid->value.byName == NULL)
  252. return 0;
  253. return X509_NAME_cmp(respid->value.byName,
  254. X509_get_subject_name(cert)) == 0;
  255. }
  256. return 0;
  257. }