ocsp_srv.c 8.9 KB

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