v3_ocsp.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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/conf.h>
  12. # include <openssl/asn1.h>
  13. # include <openssl/ocsp.h>
  14. # include "ocsp_local.h"
  15. # include <openssl/x509v3.h>
  16. # include "../x509/ext_dat.h"
  17. DEFINE_STACK_OF(ACCESS_DESCRIPTION)
  18. /*
  19. * OCSP extensions and a couple of CRL entry extensions
  20. */
  21. static int i2r_ocsp_crlid(const X509V3_EXT_METHOD *method, void *nonce,
  22. BIO *out, int indent);
  23. static int i2r_ocsp_acutoff(const X509V3_EXT_METHOD *method, void *nonce,
  24. BIO *out, int indent);
  25. static int i2r_object(const X509V3_EXT_METHOD *method, void *obj, BIO *out,
  26. int indent);
  27. static void *ocsp_nonce_new(void);
  28. static int i2d_ocsp_nonce(const void *a, unsigned char **pp);
  29. static void *d2i_ocsp_nonce(void *a, const unsigned char **pp, long length);
  30. static void ocsp_nonce_free(void *a);
  31. static int i2r_ocsp_nonce(const X509V3_EXT_METHOD *method, void *nonce,
  32. BIO *out, int indent);
  33. static int i2r_ocsp_nocheck(const X509V3_EXT_METHOD *method,
  34. void *nocheck, BIO *out, int indent);
  35. static void *s2i_ocsp_nocheck(const X509V3_EXT_METHOD *method,
  36. X509V3_CTX *ctx, const char *str);
  37. static int i2r_ocsp_serviceloc(const X509V3_EXT_METHOD *method, void *in,
  38. BIO *bp, int ind);
  39. const X509V3_EXT_METHOD v3_ocsp_crlid = {
  40. NID_id_pkix_OCSP_CrlID, 0, ASN1_ITEM_ref(OCSP_CRLID),
  41. 0, 0, 0, 0,
  42. 0, 0,
  43. 0, 0,
  44. i2r_ocsp_crlid, 0,
  45. NULL
  46. };
  47. const X509V3_EXT_METHOD v3_ocsp_acutoff = {
  48. NID_id_pkix_OCSP_archiveCutoff, 0, ASN1_ITEM_ref(ASN1_GENERALIZEDTIME),
  49. 0, 0, 0, 0,
  50. 0, 0,
  51. 0, 0,
  52. i2r_ocsp_acutoff, 0,
  53. NULL
  54. };
  55. const X509V3_EXT_METHOD v3_crl_invdate = {
  56. NID_invalidity_date, 0, ASN1_ITEM_ref(ASN1_GENERALIZEDTIME),
  57. 0, 0, 0, 0,
  58. 0, 0,
  59. 0, 0,
  60. i2r_ocsp_acutoff, 0,
  61. NULL
  62. };
  63. const X509V3_EXT_METHOD v3_crl_hold = {
  64. NID_hold_instruction_code, 0, ASN1_ITEM_ref(ASN1_OBJECT),
  65. 0, 0, 0, 0,
  66. 0, 0,
  67. 0, 0,
  68. i2r_object, 0,
  69. NULL
  70. };
  71. const X509V3_EXT_METHOD v3_ocsp_nonce = {
  72. NID_id_pkix_OCSP_Nonce, 0, NULL,
  73. ocsp_nonce_new,
  74. ocsp_nonce_free,
  75. d2i_ocsp_nonce,
  76. i2d_ocsp_nonce,
  77. 0, 0,
  78. 0, 0,
  79. i2r_ocsp_nonce, 0,
  80. NULL
  81. };
  82. const X509V3_EXT_METHOD v3_ocsp_nocheck = {
  83. NID_id_pkix_OCSP_noCheck, 0, ASN1_ITEM_ref(ASN1_NULL),
  84. 0, 0, 0, 0,
  85. 0, s2i_ocsp_nocheck,
  86. 0, 0,
  87. i2r_ocsp_nocheck, 0,
  88. NULL
  89. };
  90. const X509V3_EXT_METHOD v3_ocsp_serviceloc = {
  91. NID_id_pkix_OCSP_serviceLocator, 0, ASN1_ITEM_ref(OCSP_SERVICELOC),
  92. 0, 0, 0, 0,
  93. 0, 0,
  94. 0, 0,
  95. i2r_ocsp_serviceloc, 0,
  96. NULL
  97. };
  98. static int i2r_ocsp_crlid(const X509V3_EXT_METHOD *method, void *in, BIO *bp,
  99. int ind)
  100. {
  101. OCSP_CRLID *a = in;
  102. if (a->crlUrl) {
  103. if (BIO_printf(bp, "%*scrlUrl: ", ind, "") <= 0)
  104. goto err;
  105. if (!ASN1_STRING_print(bp, (ASN1_STRING *)a->crlUrl))
  106. goto err;
  107. if (BIO_write(bp, "\n", 1) <= 0)
  108. goto err;
  109. }
  110. if (a->crlNum) {
  111. if (BIO_printf(bp, "%*scrlNum: ", ind, "") <= 0)
  112. goto err;
  113. if (i2a_ASN1_INTEGER(bp, a->crlNum) <= 0)
  114. goto err;
  115. if (BIO_write(bp, "\n", 1) <= 0)
  116. goto err;
  117. }
  118. if (a->crlTime) {
  119. if (BIO_printf(bp, "%*scrlTime: ", ind, "") <= 0)
  120. goto err;
  121. if (!ASN1_GENERALIZEDTIME_print(bp, a->crlTime))
  122. goto err;
  123. if (BIO_write(bp, "\n", 1) <= 0)
  124. goto err;
  125. }
  126. return 1;
  127. err:
  128. return 0;
  129. }
  130. static int i2r_ocsp_acutoff(const X509V3_EXT_METHOD *method, void *cutoff,
  131. BIO *bp, int ind)
  132. {
  133. if (BIO_printf(bp, "%*s", ind, "") <= 0)
  134. return 0;
  135. if (!ASN1_GENERALIZEDTIME_print(bp, cutoff))
  136. return 0;
  137. return 1;
  138. }
  139. static int i2r_object(const X509V3_EXT_METHOD *method, void *oid, BIO *bp,
  140. int ind)
  141. {
  142. if (BIO_printf(bp, "%*s", ind, "") <= 0)
  143. return 0;
  144. if (i2a_ASN1_OBJECT(bp, oid) <= 0)
  145. return 0;
  146. return 1;
  147. }
  148. /*
  149. * OCSP nonce. This is needs special treatment because it doesn't have an
  150. * ASN1 encoding at all: it just contains arbitrary data.
  151. */
  152. static void *ocsp_nonce_new(void)
  153. {
  154. return ASN1_OCTET_STRING_new();
  155. }
  156. static int i2d_ocsp_nonce(const void *a, unsigned char **pp)
  157. {
  158. const ASN1_OCTET_STRING *os = a;
  159. if (pp) {
  160. memcpy(*pp, os->data, os->length);
  161. *pp += os->length;
  162. }
  163. return os->length;
  164. }
  165. static void *d2i_ocsp_nonce(void *a, const unsigned char **pp, long length)
  166. {
  167. ASN1_OCTET_STRING *os, **pos;
  168. pos = a;
  169. if (pos == NULL || *pos == NULL) {
  170. os = ASN1_OCTET_STRING_new();
  171. if (os == NULL)
  172. goto err;
  173. } else {
  174. os = *pos;
  175. }
  176. if (!ASN1_OCTET_STRING_set(os, *pp, length))
  177. goto err;
  178. *pp += length;
  179. if (pos)
  180. *pos = os;
  181. return os;
  182. err:
  183. if ((pos == NULL) || (*pos != os))
  184. ASN1_OCTET_STRING_free(os);
  185. OCSPerr(OCSP_F_D2I_OCSP_NONCE, ERR_R_MALLOC_FAILURE);
  186. return NULL;
  187. }
  188. static void ocsp_nonce_free(void *a)
  189. {
  190. ASN1_OCTET_STRING_free(a);
  191. }
  192. static int i2r_ocsp_nonce(const X509V3_EXT_METHOD *method, void *nonce,
  193. BIO *out, int indent)
  194. {
  195. if (BIO_printf(out, "%*s", indent, "") <= 0)
  196. return 0;
  197. if (i2a_ASN1_STRING(out, nonce, V_ASN1_OCTET_STRING) <= 0)
  198. return 0;
  199. return 1;
  200. }
  201. /* Nocheck is just a single NULL. Don't print anything and always set it */
  202. static int i2r_ocsp_nocheck(const X509V3_EXT_METHOD *method, void *nocheck,
  203. BIO *out, int indent)
  204. {
  205. return 1;
  206. }
  207. static void *s2i_ocsp_nocheck(const X509V3_EXT_METHOD *method,
  208. X509V3_CTX *ctx, const char *str)
  209. {
  210. return ASN1_NULL_new();
  211. }
  212. static int i2r_ocsp_serviceloc(const X509V3_EXT_METHOD *method, void *in,
  213. BIO *bp, int ind)
  214. {
  215. int i;
  216. OCSP_SERVICELOC *a = in;
  217. ACCESS_DESCRIPTION *ad;
  218. if (BIO_printf(bp, "%*sIssuer: ", ind, "") <= 0)
  219. goto err;
  220. if (X509_NAME_print_ex(bp, a->issuer, 0, XN_FLAG_ONELINE) <= 0)
  221. goto err;
  222. for (i = 0; i < sk_ACCESS_DESCRIPTION_num(a->locator); i++) {
  223. ad = sk_ACCESS_DESCRIPTION_value(a->locator, i);
  224. if (BIO_printf(bp, "\n%*s", (2 * ind), "") <= 0)
  225. goto err;
  226. if (i2a_ASN1_OBJECT(bp, ad->method) <= 0)
  227. goto err;
  228. if (BIO_puts(bp, " - ") <= 0)
  229. goto err;
  230. if (GENERAL_NAME_print(bp, ad->location) <= 0)
  231. goto err;
  232. }
  233. return 1;
  234. err:
  235. return 0;
  236. }