ocsp_ext.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  1. /*
  2. * Copyright 2000-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/ocsp.h>
  14. #include "ocsp_local.h"
  15. #include <openssl/rand.h>
  16. #include <openssl/x509v3.h>
  17. DEFINE_STACK_OF(ASN1_OBJECT)
  18. DEFINE_STACK_OF(ACCESS_DESCRIPTION)
  19. /* Standard wrapper functions for extensions */
  20. /* OCSP request extensions */
  21. int OCSP_REQUEST_get_ext_count(OCSP_REQUEST *x)
  22. {
  23. return X509v3_get_ext_count(x->tbsRequest.requestExtensions);
  24. }
  25. int OCSP_REQUEST_get_ext_by_NID(OCSP_REQUEST *x, int nid, int lastpos)
  26. {
  27. return (X509v3_get_ext_by_NID
  28. (x->tbsRequest.requestExtensions, nid, lastpos));
  29. }
  30. int OCSP_REQUEST_get_ext_by_OBJ(OCSP_REQUEST *x, const ASN1_OBJECT *obj,
  31. int lastpos)
  32. {
  33. return (X509v3_get_ext_by_OBJ
  34. (x->tbsRequest.requestExtensions, obj, lastpos));
  35. }
  36. int OCSP_REQUEST_get_ext_by_critical(OCSP_REQUEST *x, int crit, int lastpos)
  37. {
  38. return (X509v3_get_ext_by_critical
  39. (x->tbsRequest.requestExtensions, crit, lastpos));
  40. }
  41. X509_EXTENSION *OCSP_REQUEST_get_ext(OCSP_REQUEST *x, int loc)
  42. {
  43. return X509v3_get_ext(x->tbsRequest.requestExtensions, loc);
  44. }
  45. X509_EXTENSION *OCSP_REQUEST_delete_ext(OCSP_REQUEST *x, int loc)
  46. {
  47. return X509v3_delete_ext(x->tbsRequest.requestExtensions, loc);
  48. }
  49. void *OCSP_REQUEST_get1_ext_d2i(OCSP_REQUEST *x, int nid, int *crit, int *idx)
  50. {
  51. return X509V3_get_d2i(x->tbsRequest.requestExtensions, nid, crit, idx);
  52. }
  53. int OCSP_REQUEST_add1_ext_i2d(OCSP_REQUEST *x, int nid, void *value, int crit,
  54. unsigned long flags)
  55. {
  56. return X509V3_add1_i2d(&x->tbsRequest.requestExtensions, nid, value,
  57. crit, flags);
  58. }
  59. int OCSP_REQUEST_add_ext(OCSP_REQUEST *x, X509_EXTENSION *ex, int loc)
  60. {
  61. return (X509v3_add_ext(&(x->tbsRequest.requestExtensions), ex, loc) !=
  62. NULL);
  63. }
  64. /* Single extensions */
  65. int OCSP_ONEREQ_get_ext_count(OCSP_ONEREQ *x)
  66. {
  67. return X509v3_get_ext_count(x->singleRequestExtensions);
  68. }
  69. int OCSP_ONEREQ_get_ext_by_NID(OCSP_ONEREQ *x, int nid, int lastpos)
  70. {
  71. return X509v3_get_ext_by_NID(x->singleRequestExtensions, nid, lastpos);
  72. }
  73. int OCSP_ONEREQ_get_ext_by_OBJ(OCSP_ONEREQ *x, const ASN1_OBJECT *obj,
  74. int lastpos)
  75. {
  76. return X509v3_get_ext_by_OBJ(x->singleRequestExtensions, obj, lastpos);
  77. }
  78. int OCSP_ONEREQ_get_ext_by_critical(OCSP_ONEREQ *x, int crit, int lastpos)
  79. {
  80. return (X509v3_get_ext_by_critical
  81. (x->singleRequestExtensions, crit, lastpos));
  82. }
  83. X509_EXTENSION *OCSP_ONEREQ_get_ext(OCSP_ONEREQ *x, int loc)
  84. {
  85. return X509v3_get_ext(x->singleRequestExtensions, loc);
  86. }
  87. X509_EXTENSION *OCSP_ONEREQ_delete_ext(OCSP_ONEREQ *x, int loc)
  88. {
  89. return X509v3_delete_ext(x->singleRequestExtensions, loc);
  90. }
  91. void *OCSP_ONEREQ_get1_ext_d2i(OCSP_ONEREQ *x, int nid, int *crit, int *idx)
  92. {
  93. return X509V3_get_d2i(x->singleRequestExtensions, nid, crit, idx);
  94. }
  95. int OCSP_ONEREQ_add1_ext_i2d(OCSP_ONEREQ *x, int nid, void *value, int crit,
  96. unsigned long flags)
  97. {
  98. return X509V3_add1_i2d(&x->singleRequestExtensions, nid, value, crit,
  99. flags);
  100. }
  101. int OCSP_ONEREQ_add_ext(OCSP_ONEREQ *x, X509_EXTENSION *ex, int loc)
  102. {
  103. return (X509v3_add_ext(&(x->singleRequestExtensions), ex, loc) != NULL);
  104. }
  105. /* OCSP Basic response */
  106. int OCSP_BASICRESP_get_ext_count(OCSP_BASICRESP *x)
  107. {
  108. return X509v3_get_ext_count(x->tbsResponseData.responseExtensions);
  109. }
  110. int OCSP_BASICRESP_get_ext_by_NID(OCSP_BASICRESP *x, int nid, int lastpos)
  111. {
  112. return (X509v3_get_ext_by_NID
  113. (x->tbsResponseData.responseExtensions, nid, lastpos));
  114. }
  115. int OCSP_BASICRESP_get_ext_by_OBJ(OCSP_BASICRESP *x, const ASN1_OBJECT *obj,
  116. int lastpos)
  117. {
  118. return (X509v3_get_ext_by_OBJ
  119. (x->tbsResponseData.responseExtensions, obj, lastpos));
  120. }
  121. int OCSP_BASICRESP_get_ext_by_critical(OCSP_BASICRESP *x, int crit,
  122. int lastpos)
  123. {
  124. return (X509v3_get_ext_by_critical
  125. (x->tbsResponseData.responseExtensions, crit, lastpos));
  126. }
  127. X509_EXTENSION *OCSP_BASICRESP_get_ext(OCSP_BASICRESP *x, int loc)
  128. {
  129. return X509v3_get_ext(x->tbsResponseData.responseExtensions, loc);
  130. }
  131. X509_EXTENSION *OCSP_BASICRESP_delete_ext(OCSP_BASICRESP *x, int loc)
  132. {
  133. return X509v3_delete_ext(x->tbsResponseData.responseExtensions, loc);
  134. }
  135. void *OCSP_BASICRESP_get1_ext_d2i(OCSP_BASICRESP *x, int nid, int *crit,
  136. int *idx)
  137. {
  138. return X509V3_get_d2i(x->tbsResponseData.responseExtensions, nid, crit,
  139. idx);
  140. }
  141. int OCSP_BASICRESP_add1_ext_i2d(OCSP_BASICRESP *x, int nid, void *value,
  142. int crit, unsigned long flags)
  143. {
  144. return X509V3_add1_i2d(&x->tbsResponseData.responseExtensions, nid,
  145. value, crit, flags);
  146. }
  147. int OCSP_BASICRESP_add_ext(OCSP_BASICRESP *x, X509_EXTENSION *ex, int loc)
  148. {
  149. return (X509v3_add_ext(&(x->tbsResponseData.responseExtensions), ex, loc)
  150. != NULL);
  151. }
  152. /* OCSP single response extensions */
  153. int OCSP_SINGLERESP_get_ext_count(OCSP_SINGLERESP *x)
  154. {
  155. return X509v3_get_ext_count(x->singleExtensions);
  156. }
  157. int OCSP_SINGLERESP_get_ext_by_NID(OCSP_SINGLERESP *x, int nid, int lastpos)
  158. {
  159. return X509v3_get_ext_by_NID(x->singleExtensions, nid, lastpos);
  160. }
  161. int OCSP_SINGLERESP_get_ext_by_OBJ(OCSP_SINGLERESP *x, const ASN1_OBJECT *obj,
  162. int lastpos)
  163. {
  164. return X509v3_get_ext_by_OBJ(x->singleExtensions, obj, lastpos);
  165. }
  166. int OCSP_SINGLERESP_get_ext_by_critical(OCSP_SINGLERESP *x, int crit,
  167. int lastpos)
  168. {
  169. return X509v3_get_ext_by_critical(x->singleExtensions, crit, lastpos);
  170. }
  171. X509_EXTENSION *OCSP_SINGLERESP_get_ext(OCSP_SINGLERESP *x, int loc)
  172. {
  173. return X509v3_get_ext(x->singleExtensions, loc);
  174. }
  175. X509_EXTENSION *OCSP_SINGLERESP_delete_ext(OCSP_SINGLERESP *x, int loc)
  176. {
  177. return X509v3_delete_ext(x->singleExtensions, loc);
  178. }
  179. void *OCSP_SINGLERESP_get1_ext_d2i(OCSP_SINGLERESP *x, int nid, int *crit,
  180. int *idx)
  181. {
  182. return X509V3_get_d2i(x->singleExtensions, nid, crit, idx);
  183. }
  184. int OCSP_SINGLERESP_add1_ext_i2d(OCSP_SINGLERESP *x, int nid, void *value,
  185. int crit, unsigned long flags)
  186. {
  187. return X509V3_add1_i2d(&x->singleExtensions, nid, value, crit, flags);
  188. }
  189. int OCSP_SINGLERESP_add_ext(OCSP_SINGLERESP *x, X509_EXTENSION *ex, int loc)
  190. {
  191. return (X509v3_add_ext(&(x->singleExtensions), ex, loc) != NULL);
  192. }
  193. /* also CRL Entry Extensions */
  194. /* Nonce handling functions */
  195. /*
  196. * Add a nonce to an extension stack. A nonce can be specified or if NULL a
  197. * random nonce will be generated. Note: OpenSSL 0.9.7d and later create an
  198. * OCTET STRING containing the nonce, previous versions used the raw nonce.
  199. */
  200. static int ocsp_add1_nonce(STACK_OF(X509_EXTENSION) **exts,
  201. unsigned char *val, int len)
  202. {
  203. unsigned char *tmpval;
  204. ASN1_OCTET_STRING os;
  205. int ret = 0;
  206. if (len <= 0)
  207. len = OCSP_DEFAULT_NONCE_LENGTH;
  208. /*
  209. * Create the OCTET STRING manually by writing out the header and
  210. * appending the content octets. This avoids an extra memory allocation
  211. * operation in some cases. Applications should *NOT* do this because it
  212. * relies on library internals.
  213. */
  214. os.length = ASN1_object_size(0, len, V_ASN1_OCTET_STRING);
  215. if (os.length < 0)
  216. return 0;
  217. os.data = OPENSSL_malloc(os.length);
  218. if (os.data == NULL)
  219. goto err;
  220. tmpval = os.data;
  221. ASN1_put_object(&tmpval, 0, len, V_ASN1_OCTET_STRING, V_ASN1_UNIVERSAL);
  222. if (val)
  223. memcpy(tmpval, val, len);
  224. else if (RAND_bytes(tmpval, len) <= 0)
  225. goto err;
  226. if (!X509V3_add1_i2d(exts, NID_id_pkix_OCSP_Nonce,
  227. &os, 0, X509V3_ADD_REPLACE))
  228. goto err;
  229. ret = 1;
  230. err:
  231. OPENSSL_free(os.data);
  232. return ret;
  233. }
  234. /* Add nonce to an OCSP request */
  235. int OCSP_request_add1_nonce(OCSP_REQUEST *req, unsigned char *val, int len)
  236. {
  237. return ocsp_add1_nonce(&req->tbsRequest.requestExtensions, val, len);
  238. }
  239. /* Same as above but for a response */
  240. int OCSP_basic_add1_nonce(OCSP_BASICRESP *resp, unsigned char *val, int len)
  241. {
  242. return ocsp_add1_nonce(&resp->tbsResponseData.responseExtensions, val,
  243. len);
  244. }
  245. /*-
  246. * Check nonce validity in a request and response.
  247. * Return value reflects result:
  248. * 1: nonces present and equal.
  249. * 2: nonces both absent.
  250. * 3: nonce present in response only.
  251. * 0: nonces both present and not equal.
  252. * -1: nonce in request only.
  253. *
  254. * For most responders clients can check return > 0.
  255. * If responder doesn't handle nonces return != 0 may be
  256. * necessary. return == 0 is always an error.
  257. */
  258. int OCSP_check_nonce(OCSP_REQUEST *req, OCSP_BASICRESP *bs)
  259. {
  260. /*
  261. * Since we are only interested in the presence or absence of
  262. * the nonce and comparing its value there is no need to use
  263. * the X509V3 routines: this way we can avoid them allocating an
  264. * ASN1_OCTET_STRING structure for the value which would be
  265. * freed immediately anyway.
  266. */
  267. int req_idx, resp_idx;
  268. X509_EXTENSION *req_ext, *resp_ext;
  269. req_idx = OCSP_REQUEST_get_ext_by_NID(req, NID_id_pkix_OCSP_Nonce, -1);
  270. resp_idx = OCSP_BASICRESP_get_ext_by_NID(bs, NID_id_pkix_OCSP_Nonce, -1);
  271. /* Check both absent */
  272. if ((req_idx < 0) && (resp_idx < 0))
  273. return 2;
  274. /* Check in request only */
  275. if ((req_idx >= 0) && (resp_idx < 0))
  276. return -1;
  277. /* Check in response but not request */
  278. if ((req_idx < 0) && (resp_idx >= 0))
  279. return 3;
  280. /*
  281. * Otherwise nonce in request and response so retrieve the extensions
  282. */
  283. req_ext = OCSP_REQUEST_get_ext(req, req_idx);
  284. resp_ext = OCSP_BASICRESP_get_ext(bs, resp_idx);
  285. if (ASN1_OCTET_STRING_cmp(X509_EXTENSION_get_data(req_ext),
  286. X509_EXTENSION_get_data(resp_ext)))
  287. return 0;
  288. return 1;
  289. }
  290. /*
  291. * Copy the nonce value (if any) from an OCSP request to a response.
  292. */
  293. int OCSP_copy_nonce(OCSP_BASICRESP *resp, OCSP_REQUEST *req)
  294. {
  295. X509_EXTENSION *req_ext;
  296. int req_idx;
  297. /* Check for nonce in request */
  298. req_idx = OCSP_REQUEST_get_ext_by_NID(req, NID_id_pkix_OCSP_Nonce, -1);
  299. /* If no nonce that's OK */
  300. if (req_idx < 0)
  301. return 2;
  302. req_ext = OCSP_REQUEST_get_ext(req, req_idx);
  303. return OCSP_BASICRESP_add_ext(resp, req_ext, -1);
  304. }
  305. X509_EXTENSION *OCSP_crlID_new(const char *url, long *n, char *tim)
  306. {
  307. X509_EXTENSION *x = NULL;
  308. OCSP_CRLID *cid = NULL;
  309. if ((cid = OCSP_CRLID_new()) == NULL)
  310. goto err;
  311. if (url) {
  312. if ((cid->crlUrl = ASN1_IA5STRING_new()) == NULL)
  313. goto err;
  314. if (!(ASN1_STRING_set(cid->crlUrl, url, -1)))
  315. goto err;
  316. }
  317. if (n) {
  318. if ((cid->crlNum = ASN1_INTEGER_new()) == NULL)
  319. goto err;
  320. if (!(ASN1_INTEGER_set(cid->crlNum, *n)))
  321. goto err;
  322. }
  323. if (tim) {
  324. if ((cid->crlTime = ASN1_GENERALIZEDTIME_new()) == NULL)
  325. goto err;
  326. if (!(ASN1_GENERALIZEDTIME_set_string(cid->crlTime, tim)))
  327. goto err;
  328. }
  329. x = X509V3_EXT_i2d(NID_id_pkix_OCSP_CrlID, 0, cid);
  330. err:
  331. OCSP_CRLID_free(cid);
  332. return x;
  333. }
  334. /* AcceptableResponses ::= SEQUENCE OF OBJECT IDENTIFIER */
  335. X509_EXTENSION *OCSP_accept_responses_new(char **oids)
  336. {
  337. int nid;
  338. STACK_OF(ASN1_OBJECT) *sk = NULL;
  339. ASN1_OBJECT *o = NULL;
  340. X509_EXTENSION *x = NULL;
  341. if ((sk = sk_ASN1_OBJECT_new_null()) == NULL)
  342. goto err;
  343. while (oids && *oids) {
  344. if ((nid = OBJ_txt2nid(*oids)) != NID_undef && (o = OBJ_nid2obj(nid)))
  345. sk_ASN1_OBJECT_push(sk, o);
  346. oids++;
  347. }
  348. x = X509V3_EXT_i2d(NID_id_pkix_OCSP_acceptableResponses, 0, sk);
  349. err:
  350. sk_ASN1_OBJECT_pop_free(sk, ASN1_OBJECT_free);
  351. return x;
  352. }
  353. /* ArchiveCutoff ::= GeneralizedTime */
  354. X509_EXTENSION *OCSP_archive_cutoff_new(char *tim)
  355. {
  356. X509_EXTENSION *x = NULL;
  357. ASN1_GENERALIZEDTIME *gt = NULL;
  358. if ((gt = ASN1_GENERALIZEDTIME_new()) == NULL)
  359. goto err;
  360. if (!(ASN1_GENERALIZEDTIME_set_string(gt, tim)))
  361. goto err;
  362. x = X509V3_EXT_i2d(NID_id_pkix_OCSP_archiveCutoff, 0, gt);
  363. err:
  364. ASN1_GENERALIZEDTIME_free(gt);
  365. return x;
  366. }
  367. /*
  368. * per ACCESS_DESCRIPTION parameter are oids, of which there are currently
  369. * two--NID_ad_ocsp, NID_id_ad_caIssuers--and GeneralName value. This method
  370. * forces NID_ad_ocsp and uniformResourceLocator [6] IA5String.
  371. */
  372. X509_EXTENSION *OCSP_url_svcloc_new(const X509_NAME *issuer, const char **urls)
  373. {
  374. X509_EXTENSION *x = NULL;
  375. ASN1_IA5STRING *ia5 = NULL;
  376. OCSP_SERVICELOC *sloc = NULL;
  377. ACCESS_DESCRIPTION *ad = NULL;
  378. if ((sloc = OCSP_SERVICELOC_new()) == NULL)
  379. goto err;
  380. X509_NAME_free(sloc->issuer);
  381. if ((sloc->issuer = X509_NAME_dup(issuer)) == NULL)
  382. goto err;
  383. if (urls && *urls
  384. && (sloc->locator = sk_ACCESS_DESCRIPTION_new_null()) == NULL)
  385. goto err;
  386. while (urls && *urls) {
  387. if ((ad = ACCESS_DESCRIPTION_new()) == NULL)
  388. goto err;
  389. if ((ad->method = OBJ_nid2obj(NID_ad_OCSP)) == NULL)
  390. goto err;
  391. if ((ia5 = ASN1_IA5STRING_new()) == NULL)
  392. goto err;
  393. if (!ASN1_STRING_set((ASN1_STRING *)ia5, *urls, -1))
  394. goto err;
  395. /* ad->location is allocated inside ACCESS_DESCRIPTION_new */
  396. ad->location->type = GEN_URI;
  397. ad->location->d.ia5 = ia5;
  398. ia5 = NULL;
  399. if (!sk_ACCESS_DESCRIPTION_push(sloc->locator, ad))
  400. goto err;
  401. ad = NULL;
  402. urls++;
  403. }
  404. x = X509V3_EXT_i2d(NID_id_pkix_OCSP_serviceLocator, 0, sloc);
  405. err:
  406. ASN1_IA5STRING_free(ia5);
  407. ACCESS_DESCRIPTION_free(ad);
  408. OCSP_SERVICELOC_free(sloc);
  409. return x;
  410. }