ocsp_srv.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. /*
  2. * Copyright 2001-2021 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (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_local.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. ERR_raise(ERR_LIB_OCSP, 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. return ossl_x509_add_cert_new(&resp->certs, cert, X509_ADD_FLAG_UP_REF);
  140. }
  141. /*
  142. * Sign an OCSP response using the parameters contained in the digest context,
  143. * set the responderID to the subject name in the signer's certificate, and
  144. * include one or more optional certificates in the response.
  145. */
  146. int OCSP_basic_sign_ctx(OCSP_BASICRESP *brsp,
  147. X509 *signer, EVP_MD_CTX *ctx,
  148. STACK_OF(X509) *certs, unsigned long flags)
  149. {
  150. OCSP_RESPID *rid;
  151. EVP_PKEY *pkey;
  152. if (ctx == NULL || EVP_MD_CTX_get_pkey_ctx(ctx) == NULL) {
  153. ERR_raise(ERR_LIB_OCSP, OCSP_R_NO_SIGNER_KEY);
  154. goto err;
  155. }
  156. pkey = EVP_PKEY_CTX_get0_pkey(EVP_MD_CTX_get_pkey_ctx(ctx));
  157. if (pkey == NULL || !X509_check_private_key(signer, pkey)) {
  158. ERR_raise(ERR_LIB_OCSP, OCSP_R_PRIVATE_KEY_DOES_NOT_MATCH_CERTIFICATE);
  159. goto err;
  160. }
  161. if (!(flags & OCSP_NOCERTS)) {
  162. if (!OCSP_basic_add1_cert(brsp, signer)
  163. || !X509_add_certs(brsp->certs, certs, X509_ADD_FLAG_UP_REF))
  164. goto err;
  165. }
  166. rid = &brsp->tbsResponseData.responderId;
  167. if (flags & OCSP_RESPID_KEY) {
  168. if (!OCSP_RESPID_set_by_key(rid, signer))
  169. goto err;
  170. } else if (!OCSP_RESPID_set_by_name(rid, signer)) {
  171. goto err;
  172. }
  173. if (!(flags & OCSP_NOTIME) &&
  174. !X509_gmtime_adj(brsp->tbsResponseData.producedAt, 0))
  175. goto err;
  176. /*
  177. * Right now, I think that not doing double hashing is the right thing.
  178. * -- Richard Levitte
  179. */
  180. if (!OCSP_BASICRESP_sign_ctx(brsp, ctx, 0))
  181. goto err;
  182. return 1;
  183. err:
  184. return 0;
  185. }
  186. int OCSP_basic_sign(OCSP_BASICRESP *brsp,
  187. X509 *signer, EVP_PKEY *key, const EVP_MD *dgst,
  188. STACK_OF(X509) *certs, unsigned long flags)
  189. {
  190. EVP_MD_CTX *ctx = EVP_MD_CTX_new();
  191. EVP_PKEY_CTX *pkctx = NULL;
  192. int i;
  193. if (ctx == NULL)
  194. return 0;
  195. if (!EVP_DigestSignInit_ex(ctx, &pkctx, EVP_MD_get0_name(dgst),
  196. signer->libctx, signer->propq, key, NULL)) {
  197. EVP_MD_CTX_free(ctx);
  198. return 0;
  199. }
  200. i = OCSP_basic_sign_ctx(brsp, signer, ctx, certs, flags);
  201. EVP_MD_CTX_free(ctx);
  202. return i;
  203. }
  204. int OCSP_RESPID_set_by_name(OCSP_RESPID *respid, X509 *cert)
  205. {
  206. if (!X509_NAME_set(&respid->value.byName, X509_get_subject_name(cert)))
  207. return 0;
  208. respid->type = V_OCSP_RESPID_NAME;
  209. return 1;
  210. }
  211. int OCSP_RESPID_set_by_key_ex(OCSP_RESPID *respid, X509 *cert,
  212. OSSL_LIB_CTX *libctx, const char *propq)
  213. {
  214. ASN1_OCTET_STRING *byKey = NULL;
  215. unsigned char md[SHA_DIGEST_LENGTH];
  216. EVP_MD *sha1 = EVP_MD_fetch(libctx, "SHA1", propq);
  217. int ret = 0;
  218. if (sha1 == NULL)
  219. return 0;
  220. /* RFC2560 requires SHA1 */
  221. if (!X509_pubkey_digest(cert, sha1, md, NULL))
  222. goto err;
  223. byKey = ASN1_OCTET_STRING_new();
  224. if (byKey == NULL)
  225. goto err;
  226. if (!(ASN1_OCTET_STRING_set(byKey, md, SHA_DIGEST_LENGTH))) {
  227. ASN1_OCTET_STRING_free(byKey);
  228. goto err;
  229. }
  230. respid->type = V_OCSP_RESPID_KEY;
  231. respid->value.byKey = byKey;
  232. ret = 1;
  233. err:
  234. EVP_MD_free(sha1);
  235. return ret;
  236. }
  237. int OCSP_RESPID_set_by_key(OCSP_RESPID *respid, X509 *cert)
  238. {
  239. if (cert == NULL)
  240. return 0;
  241. return OCSP_RESPID_set_by_key_ex(respid, cert, cert->libctx, cert->propq);
  242. }
  243. int OCSP_RESPID_match_ex(OCSP_RESPID *respid, X509 *cert, OSSL_LIB_CTX *libctx,
  244. const char *propq)
  245. {
  246. EVP_MD *sha1 = NULL;
  247. int ret = 0;
  248. if (respid->type == V_OCSP_RESPID_KEY) {
  249. unsigned char md[SHA_DIGEST_LENGTH];
  250. sha1 = EVP_MD_fetch(libctx, "SHA1", propq);
  251. if (sha1 == NULL)
  252. goto err;
  253. if (respid->value.byKey == NULL)
  254. goto err;
  255. /* RFC2560 requires SHA1 */
  256. if (!X509_pubkey_digest(cert, sha1, md, NULL))
  257. goto err;
  258. ret = (ASN1_STRING_length(respid->value.byKey) == SHA_DIGEST_LENGTH)
  259. && (memcmp(ASN1_STRING_get0_data(respid->value.byKey), md,
  260. SHA_DIGEST_LENGTH) == 0);
  261. } else if (respid->type == V_OCSP_RESPID_NAME) {
  262. if (respid->value.byName == NULL)
  263. return 0;
  264. return X509_NAME_cmp(respid->value.byName,
  265. X509_get_subject_name(cert)) == 0;
  266. }
  267. err:
  268. EVP_MD_free(sha1);
  269. return ret;
  270. }
  271. int OCSP_RESPID_match(OCSP_RESPID *respid, X509 *cert)
  272. {
  273. if (cert == NULL)
  274. return 0;
  275. return OCSP_RESPID_match_ex(respid, cert, cert->libctx, cert->propq);
  276. }