ocsp_cl.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  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 <time.h>
  11. #include "internal/cryptlib.h"
  12. #include <openssl/asn1.h>
  13. #include <openssl/objects.h>
  14. #include <openssl/x509.h>
  15. #include <openssl/pem.h>
  16. #include <openssl/x509v3.h>
  17. #include <openssl/ocsp.h>
  18. #include "ocsp_local.h"
  19. /*
  20. * Utility functions related to sending OCSP requests and extracting relevant
  21. * information from the response.
  22. */
  23. /*
  24. * Add an OCSP_CERTID to an OCSP request. Return new OCSP_ONEREQ pointer:
  25. * useful if we want to add extensions.
  26. */
  27. OCSP_ONEREQ *OCSP_request_add0_id(OCSP_REQUEST *req, OCSP_CERTID *cid)
  28. {
  29. OCSP_ONEREQ *one = NULL;
  30. if ((one = OCSP_ONEREQ_new()) == NULL)
  31. return NULL;
  32. OCSP_CERTID_free(one->reqCert);
  33. one->reqCert = cid;
  34. if (req && !sk_OCSP_ONEREQ_push(req->tbsRequest.requestList, one)) {
  35. one->reqCert = NULL; /* do not free on error */
  36. goto err;
  37. }
  38. return one;
  39. err:
  40. OCSP_ONEREQ_free(one);
  41. return NULL;
  42. }
  43. /* Set requestorName from an X509_NAME structure */
  44. int OCSP_request_set1_name(OCSP_REQUEST *req, const X509_NAME *nm)
  45. {
  46. GENERAL_NAME *gen;
  47. gen = GENERAL_NAME_new();
  48. if (gen == NULL)
  49. return 0;
  50. if (!X509_NAME_set(&gen->d.directoryName, nm)) {
  51. GENERAL_NAME_free(gen);
  52. return 0;
  53. }
  54. gen->type = GEN_DIRNAME;
  55. GENERAL_NAME_free(req->tbsRequest.requestorName);
  56. req->tbsRequest.requestorName = gen;
  57. return 1;
  58. }
  59. /* Add a certificate to an OCSP request */
  60. int OCSP_request_add1_cert(OCSP_REQUEST *req, X509 *cert)
  61. {
  62. OCSP_SIGNATURE *sig;
  63. if (req->optionalSignature == NULL)
  64. req->optionalSignature = OCSP_SIGNATURE_new();
  65. sig = req->optionalSignature;
  66. if (sig == NULL)
  67. return 0;
  68. if (cert == NULL)
  69. return 1;
  70. return X509_add_cert_new(&sig->certs, cert, X509_ADD_FLAG_UP_REF);
  71. }
  72. /*
  73. * Sign an OCSP request set the requestorName to the subject name of an
  74. * optional signers certificate and include one or more optional certificates
  75. * in the request. Behaves like PKCS7_sign().
  76. */
  77. int OCSP_request_sign(OCSP_REQUEST *req,
  78. X509 *signer,
  79. EVP_PKEY *key,
  80. const EVP_MD *dgst,
  81. STACK_OF(X509) *certs, unsigned long flags)
  82. {
  83. int i;
  84. X509 *x;
  85. if (!OCSP_request_set1_name(req, X509_get_subject_name(signer)))
  86. goto err;
  87. if ((req->optionalSignature = OCSP_SIGNATURE_new()) == NULL)
  88. goto err;
  89. if (key) {
  90. if (!X509_check_private_key(signer, key)) {
  91. OCSPerr(OCSP_F_OCSP_REQUEST_SIGN,
  92. OCSP_R_PRIVATE_KEY_DOES_NOT_MATCH_CERTIFICATE);
  93. goto err;
  94. }
  95. if (!OCSP_REQUEST_sign(req, key, dgst))
  96. goto err;
  97. }
  98. if (!(flags & OCSP_NOCERTS)) {
  99. if (!OCSP_request_add1_cert(req, signer))
  100. goto err;
  101. for (i = 0; i < sk_X509_num(certs); i++) {
  102. x = sk_X509_value(certs, i);
  103. if (!OCSP_request_add1_cert(req, x))
  104. goto err;
  105. }
  106. }
  107. return 1;
  108. err:
  109. OCSP_SIGNATURE_free(req->optionalSignature);
  110. req->optionalSignature = NULL;
  111. return 0;
  112. }
  113. /* Get response status */
  114. int OCSP_response_status(OCSP_RESPONSE *resp)
  115. {
  116. return ASN1_ENUMERATED_get(resp->responseStatus);
  117. }
  118. /*
  119. * Extract basic response from OCSP_RESPONSE or NULL if no basic response
  120. * present.
  121. */
  122. OCSP_BASICRESP *OCSP_response_get1_basic(OCSP_RESPONSE *resp)
  123. {
  124. OCSP_RESPBYTES *rb;
  125. rb = resp->responseBytes;
  126. if (!rb) {
  127. OCSPerr(OCSP_F_OCSP_RESPONSE_GET1_BASIC, OCSP_R_NO_RESPONSE_DATA);
  128. return NULL;
  129. }
  130. if (OBJ_obj2nid(rb->responseType) != NID_id_pkix_OCSP_basic) {
  131. OCSPerr(OCSP_F_OCSP_RESPONSE_GET1_BASIC, OCSP_R_NOT_BASIC_RESPONSE);
  132. return NULL;
  133. }
  134. return ASN1_item_unpack(rb->response, ASN1_ITEM_rptr(OCSP_BASICRESP));
  135. }
  136. const ASN1_OCTET_STRING *OCSP_resp_get0_signature(const OCSP_BASICRESP *bs)
  137. {
  138. return bs->signature;
  139. }
  140. const X509_ALGOR *OCSP_resp_get0_tbs_sigalg(const OCSP_BASICRESP *bs)
  141. {
  142. return &bs->signatureAlgorithm;
  143. }
  144. const OCSP_RESPDATA *OCSP_resp_get0_respdata(const OCSP_BASICRESP *bs)
  145. {
  146. return &bs->tbsResponseData;
  147. }
  148. /*
  149. * Return number of OCSP_SINGLERESP responses present in a basic response.
  150. */
  151. int OCSP_resp_count(OCSP_BASICRESP *bs)
  152. {
  153. if (!bs)
  154. return -1;
  155. return sk_OCSP_SINGLERESP_num(bs->tbsResponseData.responses);
  156. }
  157. /* Extract an OCSP_SINGLERESP response with a given index */
  158. OCSP_SINGLERESP *OCSP_resp_get0(OCSP_BASICRESP *bs, int idx)
  159. {
  160. if (!bs)
  161. return NULL;
  162. return sk_OCSP_SINGLERESP_value(bs->tbsResponseData.responses, idx);
  163. }
  164. const ASN1_GENERALIZEDTIME *OCSP_resp_get0_produced_at(const OCSP_BASICRESP* bs)
  165. {
  166. return bs->tbsResponseData.producedAt;
  167. }
  168. const STACK_OF(X509) *OCSP_resp_get0_certs(const OCSP_BASICRESP *bs)
  169. {
  170. return bs->certs;
  171. }
  172. int OCSP_resp_get0_id(const OCSP_BASICRESP *bs,
  173. const ASN1_OCTET_STRING **pid,
  174. const X509_NAME **pname)
  175. {
  176. const OCSP_RESPID *rid = &bs->tbsResponseData.responderId;
  177. if (rid->type == V_OCSP_RESPID_NAME) {
  178. *pname = rid->value.byName;
  179. *pid = NULL;
  180. } else if (rid->type == V_OCSP_RESPID_KEY) {
  181. *pid = rid->value.byKey;
  182. *pname = NULL;
  183. } else {
  184. return 0;
  185. }
  186. return 1;
  187. }
  188. int OCSP_resp_get1_id(const OCSP_BASICRESP *bs,
  189. ASN1_OCTET_STRING **pid,
  190. X509_NAME **pname)
  191. {
  192. const OCSP_RESPID *rid = &bs->tbsResponseData.responderId;
  193. if (rid->type == V_OCSP_RESPID_NAME) {
  194. *pname = X509_NAME_dup(rid->value.byName);
  195. *pid = NULL;
  196. } else if (rid->type == V_OCSP_RESPID_KEY) {
  197. *pid = ASN1_OCTET_STRING_dup(rid->value.byKey);
  198. *pname = NULL;
  199. } else {
  200. return 0;
  201. }
  202. if (*pname == NULL && *pid == NULL)
  203. return 0;
  204. return 1;
  205. }
  206. /* Look single response matching a given certificate ID */
  207. int OCSP_resp_find(OCSP_BASICRESP *bs, OCSP_CERTID *id, int last)
  208. {
  209. int i;
  210. STACK_OF(OCSP_SINGLERESP) *sresp;
  211. OCSP_SINGLERESP *single;
  212. if (!bs)
  213. return -1;
  214. if (last < 0)
  215. last = 0;
  216. else
  217. last++;
  218. sresp = bs->tbsResponseData.responses;
  219. for (i = last; i < sk_OCSP_SINGLERESP_num(sresp); i++) {
  220. single = sk_OCSP_SINGLERESP_value(sresp, i);
  221. if (!OCSP_id_cmp(id, single->certId))
  222. return i;
  223. }
  224. return -1;
  225. }
  226. /*
  227. * Extract status information from an OCSP_SINGLERESP structure. Note: the
  228. * revtime and reason values are only set if the certificate status is
  229. * revoked. Returns numerical value of status.
  230. */
  231. int OCSP_single_get0_status(OCSP_SINGLERESP *single, int *reason,
  232. ASN1_GENERALIZEDTIME **revtime,
  233. ASN1_GENERALIZEDTIME **thisupd,
  234. ASN1_GENERALIZEDTIME **nextupd)
  235. {
  236. int ret;
  237. OCSP_CERTSTATUS *cst;
  238. if (!single)
  239. return -1;
  240. cst = single->certStatus;
  241. ret = cst->type;
  242. if (ret == V_OCSP_CERTSTATUS_REVOKED) {
  243. OCSP_REVOKEDINFO *rev = cst->value.revoked;
  244. if (revtime)
  245. *revtime = rev->revocationTime;
  246. if (reason) {
  247. if (rev->revocationReason)
  248. *reason = ASN1_ENUMERATED_get(rev->revocationReason);
  249. else
  250. *reason = -1;
  251. }
  252. }
  253. if (thisupd)
  254. *thisupd = single->thisUpdate;
  255. if (nextupd)
  256. *nextupd = single->nextUpdate;
  257. return ret;
  258. }
  259. /*
  260. * This function combines the previous ones: look up a certificate ID and if
  261. * found extract status information. Return 0 is successful.
  262. */
  263. int OCSP_resp_find_status(OCSP_BASICRESP *bs, OCSP_CERTID *id, int *status,
  264. int *reason,
  265. ASN1_GENERALIZEDTIME **revtime,
  266. ASN1_GENERALIZEDTIME **thisupd,
  267. ASN1_GENERALIZEDTIME **nextupd)
  268. {
  269. int i;
  270. OCSP_SINGLERESP *single;
  271. i = OCSP_resp_find(bs, id, -1);
  272. /* Maybe check for multiple responses and give an error? */
  273. if (i < 0)
  274. return 0;
  275. single = OCSP_resp_get0(bs, i);
  276. i = OCSP_single_get0_status(single, reason, revtime, thisupd, nextupd);
  277. if (status)
  278. *status = i;
  279. return 1;
  280. }
  281. /*
  282. * Check validity of thisUpdate and nextUpdate fields. It is possible that
  283. * the request will take a few seconds to process and/or the time won't be
  284. * totally accurate. Therefore to avoid rejecting otherwise valid time we
  285. * allow the times to be within 'nsec' of the current time. Also to avoid
  286. * accepting very old responses without a nextUpdate field an optional maxage
  287. * parameter specifies the maximum age the thisUpdate field can be.
  288. */
  289. int OCSP_check_validity(ASN1_GENERALIZEDTIME *thisupd,
  290. ASN1_GENERALIZEDTIME *nextupd, long nsec, long maxsec)
  291. {
  292. int ret = 1;
  293. time_t t_now, t_tmp;
  294. time(&t_now);
  295. /* Check thisUpdate is valid and not more than nsec in the future */
  296. if (!ASN1_GENERALIZEDTIME_check(thisupd)) {
  297. OCSPerr(OCSP_F_OCSP_CHECK_VALIDITY, OCSP_R_ERROR_IN_THISUPDATE_FIELD);
  298. ret = 0;
  299. } else {
  300. t_tmp = t_now + nsec;
  301. if (X509_cmp_time(thisupd, &t_tmp) > 0) {
  302. OCSPerr(OCSP_F_OCSP_CHECK_VALIDITY, OCSP_R_STATUS_NOT_YET_VALID);
  303. ret = 0;
  304. }
  305. /*
  306. * If maxsec specified check thisUpdate is not more than maxsec in
  307. * the past
  308. */
  309. if (maxsec >= 0) {
  310. t_tmp = t_now - maxsec;
  311. if (X509_cmp_time(thisupd, &t_tmp) < 0) {
  312. OCSPerr(OCSP_F_OCSP_CHECK_VALIDITY, OCSP_R_STATUS_TOO_OLD);
  313. ret = 0;
  314. }
  315. }
  316. }
  317. if (!nextupd)
  318. return ret;
  319. /* Check nextUpdate is valid and not more than nsec in the past */
  320. if (!ASN1_GENERALIZEDTIME_check(nextupd)) {
  321. OCSPerr(OCSP_F_OCSP_CHECK_VALIDITY, OCSP_R_ERROR_IN_NEXTUPDATE_FIELD);
  322. ret = 0;
  323. } else {
  324. t_tmp = t_now - nsec;
  325. if (X509_cmp_time(nextupd, &t_tmp) < 0) {
  326. OCSPerr(OCSP_F_OCSP_CHECK_VALIDITY, OCSP_R_STATUS_EXPIRED);
  327. ret = 0;
  328. }
  329. }
  330. /* Also don't allow nextUpdate to precede thisUpdate */
  331. if (ASN1_STRING_cmp(nextupd, thisupd) < 0) {
  332. OCSPerr(OCSP_F_OCSP_CHECK_VALIDITY,
  333. OCSP_R_NEXTUPDATE_BEFORE_THISUPDATE);
  334. ret = 0;
  335. }
  336. return ret;
  337. }
  338. const OCSP_CERTID *OCSP_SINGLERESP_get0_id(const OCSP_SINGLERESP *single)
  339. {
  340. return single->certId;
  341. }