v3_conf.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532
  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. *
  58. */
  59. /* extension creation utilities */
  60. #include <stdio.h>
  61. #include <ctype.h>
  62. #include "cryptlib.h"
  63. #include <openssl/conf.h>
  64. #include <openssl/x509.h>
  65. #include <openssl/x509v3.h>
  66. static int v3_check_critical(char **value);
  67. static int v3_check_generic(char **value);
  68. static X509_EXTENSION *do_ext_nconf(CONF *conf, X509V3_CTX *ctx, int ext_nid,
  69. int crit, char *value);
  70. static X509_EXTENSION *v3_generic_extension(const char *ext, char *value,
  71. int crit, int type,
  72. X509V3_CTX *ctx);
  73. static char *conf_lhash_get_string(void *db, char *section, char *value);
  74. static STACK_OF(CONF_VALUE) *conf_lhash_get_section(void *db, char *section);
  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. X509V3err(X509V3_F_X509V3_EXT_NCONF, 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. X509V3err(X509V3_F_DO_EXT_NCONF, X509V3_R_UNKNOWN_EXTENSION_NAME);
  122. return NULL;
  123. }
  124. if (!(method = X509V3_EXT_get_nid(ext_nid))) {
  125. X509V3err(X509V3_F_DO_EXT_NCONF, 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 (sk_CONF_VALUE_num(nval) <= 0) {
  135. X509V3err(X509V3_F_DO_EXT_NCONF,
  136. X509V3_R_INVALID_EXTENSION_STRING);
  137. ERR_add_error_data(4, "name=", OBJ_nid2sn(ext_nid), ",section=",
  138. value);
  139. return NULL;
  140. }
  141. ext_struc = method->v2i(method, ctx, nval);
  142. if (*value != '@')
  143. sk_CONF_VALUE_pop_free(nval, X509V3_conf_free);
  144. if (!ext_struc)
  145. return NULL;
  146. } else if (method->s2i) {
  147. if (!(ext_struc = method->s2i(method, ctx, value)))
  148. return NULL;
  149. } else if (method->r2i) {
  150. if (!ctx->db || !ctx->db_meth) {
  151. X509V3err(X509V3_F_DO_EXT_NCONF, X509V3_R_NO_CONFIG_DATABASE);
  152. return NULL;
  153. }
  154. if (!(ext_struc = method->r2i(method, ctx, value)))
  155. return NULL;
  156. } else {
  157. X509V3err(X509V3_F_DO_EXT_NCONF,
  158. 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. X509V3err(X509V3_F_DO_EXT_I2D, 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. X509V3err(X509V3_F_X509V3_EXT_I2D, 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;
  251. ASN1_OBJECT *obj = NULL;
  252. ASN1_OCTET_STRING *oct = NULL;
  253. X509_EXTENSION *extension = NULL;
  254. if (!(obj = OBJ_txt2obj(ext, 0))) {
  255. X509V3err(X509V3_F_V3_GENERIC_EXTENSION,
  256. X509V3_R_EXTENSION_NAME_ERROR);
  257. ERR_add_error_data(2, "name=", ext);
  258. goto err;
  259. }
  260. if (gen_type == 1)
  261. ext_der = string_to_hex(value, &ext_len);
  262. else if (gen_type == 2)
  263. ext_der = generic_asn1(value, ctx, &ext_len);
  264. if (ext_der == NULL) {
  265. X509V3err(X509V3_F_V3_GENERIC_EXTENSION,
  266. X509V3_R_EXTENSION_VALUE_ERROR);
  267. ERR_add_error_data(2, "value=", value);
  268. goto err;
  269. }
  270. if (!(oct = M_ASN1_OCTET_STRING_new())) {
  271. X509V3err(X509V3_F_V3_GENERIC_EXTENSION, ERR_R_MALLOC_FAILURE);
  272. goto err;
  273. }
  274. oct->data = ext_der;
  275. oct->length = ext_len;
  276. ext_der = NULL;
  277. extension = X509_EXTENSION_create_by_OBJ(NULL, obj, crit, oct);
  278. err:
  279. ASN1_OBJECT_free(obj);
  280. M_ASN1_OCTET_STRING_free(oct);
  281. if (ext_der)
  282. OPENSSL_free(ext_der);
  283. return extension;
  284. }
  285. static unsigned char *generic_asn1(char *value, X509V3_CTX *ctx,
  286. long *ext_len)
  287. {
  288. ASN1_TYPE *typ;
  289. unsigned char *ext_der = NULL;
  290. typ = ASN1_generate_v3(value, ctx);
  291. if (typ == NULL)
  292. return NULL;
  293. *ext_len = i2d_ASN1_TYPE(typ, &ext_der);
  294. ASN1_TYPE_free(typ);
  295. return ext_der;
  296. }
  297. /*
  298. * This is the main function: add a bunch of extensions based on a config
  299. * file section to an extension STACK.
  300. */
  301. int X509V3_EXT_add_nconf_sk(CONF *conf, X509V3_CTX *ctx, char *section,
  302. STACK_OF(X509_EXTENSION) **sk)
  303. {
  304. X509_EXTENSION *ext;
  305. STACK_OF(CONF_VALUE) *nval;
  306. CONF_VALUE *val;
  307. int i;
  308. if (!(nval = NCONF_get_section(conf, section)))
  309. return 0;
  310. for (i = 0; i < sk_CONF_VALUE_num(nval); i++) {
  311. val = sk_CONF_VALUE_value(nval, i);
  312. if (!(ext = X509V3_EXT_nconf(conf, ctx, val->name, val->value)))
  313. return 0;
  314. if (sk)
  315. X509v3_add_ext(sk, ext, -1);
  316. X509_EXTENSION_free(ext);
  317. }
  318. return 1;
  319. }
  320. /*
  321. * Convenience functions to add extensions to a certificate, CRL and request
  322. */
  323. int X509V3_EXT_add_nconf(CONF *conf, X509V3_CTX *ctx, char *section,
  324. X509 *cert)
  325. {
  326. STACK_OF(X509_EXTENSION) **sk = NULL;
  327. if (cert)
  328. sk = &cert->cert_info->extensions;
  329. return X509V3_EXT_add_nconf_sk(conf, ctx, section, sk);
  330. }
  331. /* Same as above but for a CRL */
  332. int X509V3_EXT_CRL_add_nconf(CONF *conf, X509V3_CTX *ctx, char *section,
  333. X509_CRL *crl)
  334. {
  335. STACK_OF(X509_EXTENSION) **sk = NULL;
  336. if (crl)
  337. sk = &crl->crl->extensions;
  338. return X509V3_EXT_add_nconf_sk(conf, ctx, section, sk);
  339. }
  340. /* Add extensions to certificate request */
  341. int X509V3_EXT_REQ_add_nconf(CONF *conf, X509V3_CTX *ctx, char *section,
  342. X509_REQ *req)
  343. {
  344. STACK_OF(X509_EXTENSION) *extlist = NULL, **sk = NULL;
  345. int i;
  346. if (req)
  347. sk = &extlist;
  348. i = X509V3_EXT_add_nconf_sk(conf, ctx, section, sk);
  349. if (!i || !sk)
  350. return i;
  351. i = X509_REQ_add_extensions(req, extlist);
  352. sk_X509_EXTENSION_pop_free(extlist, X509_EXTENSION_free);
  353. return i;
  354. }
  355. /* Config database functions */
  356. char *X509V3_get_string(X509V3_CTX *ctx, char *name, char *section)
  357. {
  358. if (!ctx->db || !ctx->db_meth || !ctx->db_meth->get_string) {
  359. X509V3err(X509V3_F_X509V3_GET_STRING, X509V3_R_OPERATION_NOT_DEFINED);
  360. return NULL;
  361. }
  362. if (ctx->db_meth->get_string)
  363. return ctx->db_meth->get_string(ctx->db, name, section);
  364. return NULL;
  365. }
  366. STACK_OF(CONF_VALUE) *X509V3_get_section(X509V3_CTX *ctx, char *section)
  367. {
  368. if (!ctx->db || !ctx->db_meth || !ctx->db_meth->get_section) {
  369. X509V3err(X509V3_F_X509V3_GET_SECTION,
  370. X509V3_R_OPERATION_NOT_DEFINED);
  371. return NULL;
  372. }
  373. if (ctx->db_meth->get_section)
  374. return ctx->db_meth->get_section(ctx->db, section);
  375. return NULL;
  376. }
  377. void X509V3_string_free(X509V3_CTX *ctx, char *str)
  378. {
  379. if (!str)
  380. return;
  381. if (ctx->db_meth->free_string)
  382. ctx->db_meth->free_string(ctx->db, str);
  383. }
  384. void X509V3_section_free(X509V3_CTX *ctx, STACK_OF(CONF_VALUE) *section)
  385. {
  386. if (!section)
  387. return;
  388. if (ctx->db_meth->free_section)
  389. ctx->db_meth->free_section(ctx->db, section);
  390. }
  391. static char *nconf_get_string(void *db, char *section, char *value)
  392. {
  393. return NCONF_get_string(db, section, value);
  394. }
  395. static STACK_OF(CONF_VALUE) *nconf_get_section(void *db, char *section)
  396. {
  397. return NCONF_get_section(db, section);
  398. }
  399. static X509V3_CONF_METHOD nconf_method = {
  400. nconf_get_string,
  401. nconf_get_section,
  402. NULL,
  403. NULL
  404. };
  405. void X509V3_set_nconf(X509V3_CTX *ctx, CONF *conf)
  406. {
  407. ctx->db_meth = &nconf_method;
  408. ctx->db = conf;
  409. }
  410. void X509V3_set_ctx(X509V3_CTX *ctx, X509 *issuer, X509 *subj, X509_REQ *req,
  411. X509_CRL *crl, int flags)
  412. {
  413. ctx->issuer_cert = issuer;
  414. ctx->subject_cert = subj;
  415. ctx->crl = crl;
  416. ctx->subject_req = req;
  417. ctx->flags = flags;
  418. }
  419. /* Old conf compatibility functions */
  420. X509_EXTENSION *X509V3_EXT_conf(LHASH_OF(CONF_VALUE) *conf, X509V3_CTX *ctx,
  421. char *name, char *value)
  422. {
  423. CONF ctmp;
  424. CONF_set_nconf(&ctmp, conf);
  425. return X509V3_EXT_nconf(&ctmp, ctx, name, value);
  426. }
  427. /* LHASH *conf: Config file */
  428. /* char *value: Value */
  429. X509_EXTENSION *X509V3_EXT_conf_nid(LHASH_OF(CONF_VALUE) *conf,
  430. X509V3_CTX *ctx, int ext_nid, char *value)
  431. {
  432. CONF ctmp;
  433. CONF_set_nconf(&ctmp, conf);
  434. return X509V3_EXT_nconf_nid(&ctmp, ctx, ext_nid, value);
  435. }
  436. static char *conf_lhash_get_string(void *db, char *section, char *value)
  437. {
  438. return CONF_get_string(db, section, value);
  439. }
  440. static STACK_OF(CONF_VALUE) *conf_lhash_get_section(void *db, char *section)
  441. {
  442. return CONF_get_section(db, section);
  443. }
  444. static X509V3_CONF_METHOD conf_lhash_method = {
  445. conf_lhash_get_string,
  446. conf_lhash_get_section,
  447. NULL,
  448. NULL
  449. };
  450. void X509V3_set_conf_lhash(X509V3_CTX *ctx, LHASH_OF(CONF_VALUE) *lhash)
  451. {
  452. ctx->db_meth = &conf_lhash_method;
  453. ctx->db = lhash;
  454. }
  455. int X509V3_EXT_add_conf(LHASH_OF(CONF_VALUE) *conf, X509V3_CTX *ctx,
  456. char *section, X509 *cert)
  457. {
  458. CONF ctmp;
  459. CONF_set_nconf(&ctmp, conf);
  460. return X509V3_EXT_add_nconf(&ctmp, ctx, section, cert);
  461. }
  462. /* Same as above but for a CRL */
  463. int X509V3_EXT_CRL_add_conf(LHASH_OF(CONF_VALUE) *conf, X509V3_CTX *ctx,
  464. char *section, X509_CRL *crl)
  465. {
  466. CONF ctmp;
  467. CONF_set_nconf(&ctmp, conf);
  468. return X509V3_EXT_CRL_add_nconf(&ctmp, ctx, section, crl);
  469. }
  470. /* Add extensions to certificate request */
  471. int X509V3_EXT_REQ_add_conf(LHASH_OF(CONF_VALUE) *conf, X509V3_CTX *ctx,
  472. char *section, X509_REQ *req)
  473. {
  474. CONF ctmp;
  475. CONF_set_nconf(&ctmp, conf);
  476. return X509V3_EXT_REQ_add_nconf(&ctmp, ctx, section, req);
  477. }