A local copy of OpenSSL from GitHub
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

343 lines
9.3 KiB

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