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.
 
 
 
 
 
 

463 lines
14 KiB

  1. /* v3_conf.c */
  2. /*
  3. * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL project
  4. * 1999.
  5. */
  6. /* ====================================================================
  7. * Copyright (c) 1999-2002 The OpenSSL Project. All rights reserved.
  8. *
  9. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted provided that the following conditions
  11. * are met:
  12. *
  13. * 1. Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions and the following disclaimer.
  15. *
  16. * 2. Redistributions in binary form must reproduce the above copyright
  17. * notice, this list of conditions and the following disclaimer in
  18. * the documentation and/or other materials provided with the
  19. * distribution.
  20. *
  21. * 3. All advertising materials mentioning features or use of this
  22. * software must display the following acknowledgment:
  23. * "This product includes software developed by the OpenSSL Project
  24. * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
  25. *
  26. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  27. * endorse or promote products derived from this software without
  28. * prior written permission. For written permission, please contact
  29. * licensing@OpenSSL.org.
  30. *
  31. * 5. Products derived from this software may not be called "OpenSSL"
  32. * nor may "OpenSSL" appear in their names without prior written
  33. * permission of the OpenSSL Project.
  34. *
  35. * 6. Redistributions of any form whatsoever must retain the following
  36. * acknowledgment:
  37. * "This product includes software developed by the OpenSSL Project
  38. * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
  39. *
  40. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  41. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  42. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  43. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  44. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  45. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  46. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  47. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  48. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  49. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  50. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  51. * OF THE POSSIBILITY OF SUCH DAMAGE.
  52. * ====================================================================
  53. *
  54. * This product includes cryptographic software written by Eric Young
  55. * (eay@cryptsoft.com). This product includes software written by Tim
  56. * Hudson (tjh@cryptsoft.com). */
  57. /* extension creation utilities */
  58. #include <ctype.h>
  59. #include <stdio.h>
  60. #include <string.h>
  61. #include <openssl/conf.h>
  62. #include <openssl/err.h>
  63. #include <openssl/mem.h>
  64. #include <openssl/obj.h>
  65. #include <openssl/x509.h>
  66. #include <openssl/x509v3.h>
  67. #include "../internal.h"
  68. static int v3_check_critical(char **value);
  69. static int v3_check_generic(char **value);
  70. static X509_EXTENSION *do_ext_nconf(CONF *conf, X509V3_CTX *ctx, int ext_nid,
  71. int crit, char *value);
  72. static X509_EXTENSION *v3_generic_extension(const char *ext, char *value,
  73. int crit, int type,
  74. X509V3_CTX *ctx);
  75. static X509_EXTENSION *do_ext_i2d(const X509V3_EXT_METHOD *method,
  76. int ext_nid, int crit, void *ext_struc);
  77. static unsigned char *generic_asn1(char *value, X509V3_CTX *ctx,
  78. long *ext_len);
  79. /* CONF *conf: Config file */
  80. /* char *name: Name */
  81. /* char *value: Value */
  82. X509_EXTENSION *X509V3_EXT_nconf(CONF *conf, X509V3_CTX *ctx, char *name,
  83. char *value)
  84. {
  85. int crit;
  86. int ext_type;
  87. X509_EXTENSION *ret;
  88. crit = v3_check_critical(&value);
  89. if ((ext_type = v3_check_generic(&value)))
  90. return v3_generic_extension(name, value, crit, ext_type, ctx);
  91. ret = do_ext_nconf(conf, ctx, OBJ_sn2nid(name), crit, value);
  92. if (!ret) {
  93. OPENSSL_PUT_ERROR(X509V3, X509V3_R_ERROR_IN_EXTENSION);
  94. ERR_add_error_data(4, "name=", name, ", value=", value);
  95. }
  96. return ret;
  97. }
  98. /* CONF *conf: Config file */
  99. /* char *value: Value */
  100. X509_EXTENSION *X509V3_EXT_nconf_nid(CONF *conf, X509V3_CTX *ctx, int ext_nid,
  101. char *value)
  102. {
  103. int crit;
  104. int ext_type;
  105. crit = v3_check_critical(&value);
  106. if ((ext_type = v3_check_generic(&value)))
  107. return v3_generic_extension(OBJ_nid2sn(ext_nid),
  108. value, crit, ext_type, ctx);
  109. return do_ext_nconf(conf, ctx, ext_nid, crit, value);
  110. }
  111. /* CONF *conf: Config file */
  112. /* char *value: Value */
  113. static X509_EXTENSION *do_ext_nconf(CONF *conf, X509V3_CTX *ctx, int ext_nid,
  114. int crit, char *value)
  115. {
  116. const X509V3_EXT_METHOD *method;
  117. X509_EXTENSION *ext;
  118. STACK_OF(CONF_VALUE) *nval;
  119. void *ext_struc;
  120. if (ext_nid == NID_undef) {
  121. OPENSSL_PUT_ERROR(X509V3, X509V3_R_UNKNOWN_EXTENSION_NAME);
  122. return NULL;
  123. }
  124. if (!(method = X509V3_EXT_get_nid(ext_nid))) {
  125. OPENSSL_PUT_ERROR(X509V3, X509V3_R_UNKNOWN_EXTENSION);
  126. return NULL;
  127. }
  128. /* Now get internal extension representation based on type */
  129. if (method->v2i) {
  130. if (*value == '@')
  131. nval = NCONF_get_section(conf, value + 1);
  132. else
  133. nval = X509V3_parse_list(value);
  134. if (nval == NULL || sk_CONF_VALUE_num(nval) <= 0) {
  135. OPENSSL_PUT_ERROR(X509V3, X509V3_R_INVALID_EXTENSION_STRING);
  136. ERR_add_error_data(4, "name=", OBJ_nid2sn(ext_nid), ",section=",
  137. value);
  138. if (*value != '@')
  139. sk_CONF_VALUE_pop_free(nval, X509V3_conf_free);
  140. return NULL;
  141. }
  142. ext_struc = method->v2i(method, ctx, nval);
  143. if (*value != '@')
  144. sk_CONF_VALUE_pop_free(nval, X509V3_conf_free);
  145. if (!ext_struc)
  146. return NULL;
  147. } else if (method->s2i) {
  148. if (!(ext_struc = method->s2i(method, ctx, value)))
  149. return NULL;
  150. } else if (method->r2i) {
  151. if (!ctx->db || !ctx->db_meth) {
  152. OPENSSL_PUT_ERROR(X509V3, X509V3_R_NO_CONFIG_DATABASE);
  153. return NULL;
  154. }
  155. if (!(ext_struc = method->r2i(method, ctx, value)))
  156. return NULL;
  157. } else {
  158. OPENSSL_PUT_ERROR(X509V3, X509V3_R_EXTENSION_SETTING_NOT_SUPPORTED);
  159. ERR_add_error_data(2, "name=", OBJ_nid2sn(ext_nid));
  160. return NULL;
  161. }
  162. ext = do_ext_i2d(method, ext_nid, crit, ext_struc);
  163. if (method->it)
  164. ASN1_item_free(ext_struc, ASN1_ITEM_ptr(method->it));
  165. else
  166. method->ext_free(ext_struc);
  167. return ext;
  168. }
  169. static X509_EXTENSION *do_ext_i2d(const X509V3_EXT_METHOD *method,
  170. int ext_nid, int crit, void *ext_struc)
  171. {
  172. unsigned char *ext_der;
  173. int ext_len;
  174. ASN1_OCTET_STRING *ext_oct;
  175. X509_EXTENSION *ext;
  176. /* Convert internal representation to DER */
  177. if (method->it) {
  178. ext_der = NULL;
  179. ext_len =
  180. ASN1_item_i2d(ext_struc, &ext_der, ASN1_ITEM_ptr(method->it));
  181. if (ext_len < 0)
  182. goto merr;
  183. } else {
  184. unsigned char *p;
  185. ext_len = method->i2d(ext_struc, NULL);
  186. if (!(ext_der = OPENSSL_malloc(ext_len)))
  187. goto merr;
  188. p = ext_der;
  189. method->i2d(ext_struc, &p);
  190. }
  191. if (!(ext_oct = M_ASN1_OCTET_STRING_new()))
  192. goto merr;
  193. ext_oct->data = ext_der;
  194. ext_oct->length = ext_len;
  195. ext = X509_EXTENSION_create_by_NID(NULL, ext_nid, crit, ext_oct);
  196. if (!ext)
  197. goto merr;
  198. M_ASN1_OCTET_STRING_free(ext_oct);
  199. return ext;
  200. merr:
  201. OPENSSL_PUT_ERROR(X509V3, ERR_R_MALLOC_FAILURE);
  202. return NULL;
  203. }
  204. /* Given an internal structure, nid and critical flag create an extension */
  205. X509_EXTENSION *X509V3_EXT_i2d(int ext_nid, int crit, void *ext_struc)
  206. {
  207. const X509V3_EXT_METHOD *method;
  208. if (!(method = X509V3_EXT_get_nid(ext_nid))) {
  209. OPENSSL_PUT_ERROR(X509V3, X509V3_R_UNKNOWN_EXTENSION);
  210. return NULL;
  211. }
  212. return do_ext_i2d(method, ext_nid, crit, ext_struc);
  213. }
  214. /* Check the extension string for critical flag */
  215. static int v3_check_critical(char **value)
  216. {
  217. char *p = *value;
  218. if ((strlen(p) < 9) || strncmp(p, "critical,", 9))
  219. return 0;
  220. p += 9;
  221. while (isspace((unsigned char)*p))
  222. p++;
  223. *value = p;
  224. return 1;
  225. }
  226. /* Check extension string for generic extension and return the type */
  227. static int v3_check_generic(char **value)
  228. {
  229. int gen_type = 0;
  230. char *p = *value;
  231. if ((strlen(p) >= 4) && !strncmp(p, "DER:", 4)) {
  232. p += 4;
  233. gen_type = 1;
  234. } else if ((strlen(p) >= 5) && !strncmp(p, "ASN1:", 5)) {
  235. p += 5;
  236. gen_type = 2;
  237. } else
  238. return 0;
  239. while (isspace((unsigned char)*p))
  240. p++;
  241. *value = p;
  242. return gen_type;
  243. }
  244. /* Create a generic extension: for now just handle DER type */
  245. static X509_EXTENSION *v3_generic_extension(const char *ext, char *value,
  246. int crit, int gen_type,
  247. X509V3_CTX *ctx)
  248. {
  249. unsigned char *ext_der = NULL;
  250. long ext_len = 0;
  251. ASN1_OBJECT *obj = NULL;
  252. ASN1_OCTET_STRING *oct = NULL;
  253. X509_EXTENSION *extension = NULL;
  254. if (!(obj = OBJ_txt2obj(ext, 0))) {
  255. OPENSSL_PUT_ERROR(X509V3, X509V3_R_EXTENSION_NAME_ERROR);
  256. ERR_add_error_data(2, "name=", ext);
  257. goto err;
  258. }
  259. if (gen_type == 1)
  260. ext_der = string_to_hex(value, &ext_len);
  261. else if (gen_type == 2)
  262. ext_der = generic_asn1(value, ctx, &ext_len);
  263. if (ext_der == NULL) {
  264. OPENSSL_PUT_ERROR(X509V3, X509V3_R_EXTENSION_VALUE_ERROR);
  265. ERR_add_error_data(2, "value=", value);
  266. goto err;
  267. }
  268. if (!(oct = M_ASN1_OCTET_STRING_new())) {
  269. OPENSSL_PUT_ERROR(X509V3, ERR_R_MALLOC_FAILURE);
  270. goto err;
  271. }
  272. oct->data = ext_der;
  273. oct->length = ext_len;
  274. ext_der = NULL;
  275. extension = X509_EXTENSION_create_by_OBJ(NULL, obj, crit, oct);
  276. err:
  277. ASN1_OBJECT_free(obj);
  278. M_ASN1_OCTET_STRING_free(oct);
  279. if (ext_der)
  280. OPENSSL_free(ext_der);
  281. return extension;
  282. }
  283. static unsigned char *generic_asn1(char *value, X509V3_CTX *ctx,
  284. long *ext_len)
  285. {
  286. ASN1_TYPE *typ;
  287. unsigned char *ext_der = NULL;
  288. typ = ASN1_generate_v3(value, ctx);
  289. if (typ == NULL)
  290. return NULL;
  291. *ext_len = i2d_ASN1_TYPE(typ, &ext_der);
  292. ASN1_TYPE_free(typ);
  293. return ext_der;
  294. }
  295. /*
  296. * This is the main function: add a bunch of extensions based on a config
  297. * file section to an extension STACK.
  298. */
  299. int X509V3_EXT_add_nconf_sk(CONF *conf, X509V3_CTX *ctx, char *section,
  300. STACK_OF(X509_EXTENSION) **sk)
  301. {
  302. X509_EXTENSION *ext;
  303. STACK_OF(CONF_VALUE) *nval;
  304. CONF_VALUE *val;
  305. size_t i;
  306. if (!(nval = NCONF_get_section(conf, section)))
  307. return 0;
  308. for (i = 0; i < sk_CONF_VALUE_num(nval); i++) {
  309. val = sk_CONF_VALUE_value(nval, i);
  310. if (!(ext = X509V3_EXT_nconf(conf, ctx, val->name, val->value)))
  311. return 0;
  312. if (sk)
  313. X509v3_add_ext(sk, ext, -1);
  314. X509_EXTENSION_free(ext);
  315. }
  316. return 1;
  317. }
  318. /*
  319. * Convenience functions to add extensions to a certificate, CRL and request
  320. */
  321. int X509V3_EXT_add_nconf(CONF *conf, X509V3_CTX *ctx, char *section,
  322. X509 *cert)
  323. {
  324. STACK_OF(X509_EXTENSION) **sk = NULL;
  325. if (cert)
  326. sk = &cert->cert_info->extensions;
  327. return X509V3_EXT_add_nconf_sk(conf, ctx, section, sk);
  328. }
  329. /* Same as above but for a CRL */
  330. int X509V3_EXT_CRL_add_nconf(CONF *conf, X509V3_CTX *ctx, char *section,
  331. X509_CRL *crl)
  332. {
  333. STACK_OF(X509_EXTENSION) **sk = NULL;
  334. if (crl)
  335. sk = &crl->crl->extensions;
  336. return X509V3_EXT_add_nconf_sk(conf, ctx, section, sk);
  337. }
  338. /* Add extensions to certificate request */
  339. int X509V3_EXT_REQ_add_nconf(CONF *conf, X509V3_CTX *ctx, char *section,
  340. X509_REQ *req)
  341. {
  342. STACK_OF(X509_EXTENSION) *extlist = NULL, **sk = NULL;
  343. int i;
  344. if (req)
  345. sk = &extlist;
  346. i = X509V3_EXT_add_nconf_sk(conf, ctx, section, sk);
  347. if (!i || !sk)
  348. return i;
  349. i = X509_REQ_add_extensions(req, extlist);
  350. sk_X509_EXTENSION_pop_free(extlist, X509_EXTENSION_free);
  351. return i;
  352. }
  353. /* Config database functions */
  354. char *X509V3_get_string(X509V3_CTX *ctx, char *name, char *section)
  355. {
  356. if (!ctx->db || !ctx->db_meth || !ctx->db_meth->get_string) {
  357. OPENSSL_PUT_ERROR(X509V3, X509V3_R_OPERATION_NOT_DEFINED);
  358. return NULL;
  359. }
  360. if (ctx->db_meth->get_string)
  361. return ctx->db_meth->get_string(ctx->db, name, section);
  362. return NULL;
  363. }
  364. STACK_OF(CONF_VALUE) *X509V3_get_section(X509V3_CTX *ctx, char *section)
  365. {
  366. if (!ctx->db || !ctx->db_meth || !ctx->db_meth->get_section) {
  367. OPENSSL_PUT_ERROR(X509V3, X509V3_R_OPERATION_NOT_DEFINED);
  368. return NULL;
  369. }
  370. if (ctx->db_meth->get_section)
  371. return ctx->db_meth->get_section(ctx->db, section);
  372. return NULL;
  373. }
  374. void X509V3_string_free(X509V3_CTX *ctx, char *str)
  375. {
  376. if (!str)
  377. return;
  378. if (ctx->db_meth->free_string)
  379. ctx->db_meth->free_string(ctx->db, str);
  380. }
  381. void X509V3_section_free(X509V3_CTX *ctx, STACK_OF(CONF_VALUE) *section)
  382. {
  383. if (!section)
  384. return;
  385. if (ctx->db_meth->free_section)
  386. ctx->db_meth->free_section(ctx->db, section);
  387. }
  388. static char *nconf_get_string(void *db, char *section, char *value)
  389. {
  390. /* TODO(fork): this should return a const value. */
  391. return (char *)NCONF_get_string(db, section, value);
  392. }
  393. static STACK_OF(CONF_VALUE) *nconf_get_section(void *db, char *section)
  394. {
  395. return NCONF_get_section(db, section);
  396. }
  397. static const X509V3_CONF_METHOD nconf_method = {
  398. nconf_get_string,
  399. nconf_get_section,
  400. NULL,
  401. NULL
  402. };
  403. void X509V3_set_nconf(X509V3_CTX *ctx, CONF *conf)
  404. {
  405. ctx->db_meth = &nconf_method;
  406. ctx->db = conf;
  407. }
  408. void X509V3_set_ctx(X509V3_CTX *ctx, X509 *issuer, X509 *subj, X509_REQ *req,
  409. X509_CRL *crl, int flags)
  410. {
  411. ctx->issuer_cert = issuer;
  412. ctx->subject_cert = subj;
  413. ctx->crl = crl;
  414. ctx->subject_req = req;
  415. ctx->flags = flags;
  416. }