2
0

ocsp_srv.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. /*
  2. * Copyright 2001-2020 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. 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. return 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. int i;
  151. OCSP_RESPID *rid;
  152. EVP_PKEY *pkey;
  153. if (ctx == NULL || EVP_MD_CTX_pkey_ctx(ctx) == NULL) {
  154. OCSPerr(OCSP_F_OCSP_BASIC_SIGN_CTX, OCSP_R_NO_SIGNER_KEY);
  155. goto err;
  156. }
  157. pkey = EVP_PKEY_CTX_get0_pkey(EVP_MD_CTX_pkey_ctx(ctx));
  158. if (pkey == NULL || !X509_check_private_key(signer, pkey)) {
  159. OCSPerr(OCSP_F_OCSP_BASIC_SIGN_CTX,
  160. OCSP_R_PRIVATE_KEY_DOES_NOT_MATCH_CERTIFICATE);
  161. goto err;
  162. }
  163. if (!(flags & OCSP_NOCERTS)) {
  164. if (!OCSP_basic_add1_cert(brsp, signer))
  165. goto err;
  166. for (i = 0; i < sk_X509_num(certs); i++) {
  167. X509 *tmpcert = sk_X509_value(certs, i);
  168. if (!OCSP_basic_add1_cert(brsp, tmpcert))
  169. goto err;
  170. }
  171. }
  172. rid = &brsp->tbsResponseData.responderId;
  173. if (flags & OCSP_RESPID_KEY) {
  174. if (!OCSP_RESPID_set_by_key(rid, signer))
  175. goto err;
  176. } else if (!OCSP_RESPID_set_by_name(rid, signer)) {
  177. goto err;
  178. }
  179. if (!(flags & OCSP_NOTIME) &&
  180. !X509_gmtime_adj(brsp->tbsResponseData.producedAt, 0))
  181. goto err;
  182. /*
  183. * Right now, I think that not doing double hashing is the right thing.
  184. * -- Richard Levitte
  185. */
  186. if (!OCSP_BASICRESP_sign_ctx(brsp, ctx, 0))
  187. goto err;
  188. return 1;
  189. err:
  190. return 0;
  191. }
  192. int OCSP_basic_sign(OCSP_BASICRESP *brsp,
  193. X509 *signer, EVP_PKEY *key, const EVP_MD *dgst,
  194. STACK_OF(X509) *certs, unsigned long flags)
  195. {
  196. EVP_MD_CTX *ctx = EVP_MD_CTX_new();
  197. EVP_PKEY_CTX *pkctx = NULL;
  198. int i;
  199. if (ctx == NULL)
  200. return 0;
  201. if (!EVP_DigestSignInit(ctx, &pkctx, dgst, NULL, key)) {
  202. EVP_MD_CTX_free(ctx);
  203. return 0;
  204. }
  205. i = OCSP_basic_sign_ctx(brsp, signer, ctx, certs, flags);
  206. EVP_MD_CTX_free(ctx);
  207. return i;
  208. }
  209. int OCSP_RESPID_set_by_name(OCSP_RESPID *respid, X509 *cert)
  210. {
  211. if (!X509_NAME_set(&respid->value.byName, X509_get_subject_name(cert)))
  212. return 0;
  213. respid->type = V_OCSP_RESPID_NAME;
  214. return 1;
  215. }
  216. int OCSP_RESPID_set_by_key_ex(OCSP_RESPID *respid, X509 *cert,
  217. OSSL_LIB_CTX *libctx, const char *propq)
  218. {
  219. ASN1_OCTET_STRING *byKey = NULL;
  220. unsigned char md[SHA_DIGEST_LENGTH];
  221. EVP_MD *sha1 = EVP_MD_fetch(libctx, "SHA1", propq);
  222. int ret = 0;
  223. if (sha1 == NULL)
  224. return 0;
  225. /* RFC2560 requires SHA1 */
  226. if (!X509_pubkey_digest(cert, sha1, md, NULL))
  227. goto err;
  228. byKey = ASN1_OCTET_STRING_new();
  229. if (byKey == NULL)
  230. goto err;
  231. if (!(ASN1_OCTET_STRING_set(byKey, md, SHA_DIGEST_LENGTH))) {
  232. ASN1_OCTET_STRING_free(byKey);
  233. goto err;
  234. }
  235. respid->type = V_OCSP_RESPID_KEY;
  236. respid->value.byKey = byKey;
  237. ret = 1;
  238. err:
  239. EVP_MD_free(sha1);
  240. return ret;
  241. }
  242. int OCSP_RESPID_set_by_key(OCSP_RESPID *respid, X509 *cert)
  243. {
  244. return OCSP_RESPID_set_by_key_ex(respid, cert, NULL, NULL);
  245. }
  246. int OCSP_RESPID_match_ex(OCSP_RESPID *respid, X509 *cert, OSSL_LIB_CTX *libctx,
  247. const char *propq)
  248. {
  249. EVP_MD *sha1 = NULL;
  250. int ret = 0;
  251. if (respid->type == V_OCSP_RESPID_KEY) {
  252. unsigned char md[SHA_DIGEST_LENGTH];
  253. sha1 = EVP_MD_fetch(libctx, "SHA1", propq);
  254. if (sha1 == NULL)
  255. goto err;
  256. if (respid->value.byKey == NULL)
  257. goto err;
  258. /* RFC2560 requires SHA1 */
  259. if (!X509_pubkey_digest(cert, sha1, md, NULL))
  260. goto err;
  261. ret = (ASN1_STRING_length(respid->value.byKey) == SHA_DIGEST_LENGTH)
  262. && (memcmp(ASN1_STRING_get0_data(respid->value.byKey), md,
  263. SHA_DIGEST_LENGTH) == 0);
  264. } else if (respid->type == V_OCSP_RESPID_NAME) {
  265. if (respid->value.byName == NULL)
  266. return 0;
  267. return X509_NAME_cmp(respid->value.byName,
  268. X509_get_subject_name(cert)) == 0;
  269. }
  270. err:
  271. EVP_MD_free(sha1);
  272. return ret;
  273. }
  274. int OCSP_RESPID_match(OCSP_RESPID *respid, X509 *cert)
  275. {
  276. return OCSP_RESPID_match_ex(respid, cert, NULL, NULL);
  277. }