ocsp_cl.c 9.9 KB

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