v3_conf.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  1. /*
  2. * Copyright 1999-2018 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. /* extension creation utilities */
  10. #include <stdio.h>
  11. #include "internal/ctype.h"
  12. #include "internal/cryptlib.h"
  13. #include <openssl/conf.h>
  14. #include <openssl/x509.h>
  15. #include "internal/x509_int.h"
  16. #include <openssl/x509v3.h>
  17. static int v3_check_critical(const char **value);
  18. static int v3_check_generic(const char **value);
  19. static X509_EXTENSION *do_ext_nconf(CONF *conf, X509V3_CTX *ctx, int ext_nid,
  20. int crit, const char *value);
  21. static X509_EXTENSION *v3_generic_extension(const char *ext, const char *value,
  22. int crit, int type,
  23. X509V3_CTX *ctx);
  24. static char *conf_lhash_get_string(void *db, const char *section, const char *value);
  25. static STACK_OF(CONF_VALUE) *conf_lhash_get_section(void *db, const char *section);
  26. static X509_EXTENSION *do_ext_i2d(const X509V3_EXT_METHOD *method,
  27. int ext_nid, int crit, void *ext_struc);
  28. static unsigned char *generic_asn1(const char *value, X509V3_CTX *ctx,
  29. long *ext_len);
  30. /* CONF *conf: Config file */
  31. /* char *name: Name */
  32. /* char *value: Value */
  33. X509_EXTENSION *X509V3_EXT_nconf(CONF *conf, X509V3_CTX *ctx, const char *name,
  34. const char *value)
  35. {
  36. int crit;
  37. int ext_type;
  38. X509_EXTENSION *ret;
  39. crit = v3_check_critical(&value);
  40. if ((ext_type = v3_check_generic(&value)))
  41. return v3_generic_extension(name, value, crit, ext_type, ctx);
  42. ret = do_ext_nconf(conf, ctx, OBJ_sn2nid(name), crit, value);
  43. if (!ret) {
  44. X509V3err(X509V3_F_X509V3_EXT_NCONF, X509V3_R_ERROR_IN_EXTENSION);
  45. ERR_add_error_data(4, "name=", name, ", value=", value);
  46. }
  47. return ret;
  48. }
  49. /* CONF *conf: Config file */
  50. /* char *value: Value */
  51. X509_EXTENSION *X509V3_EXT_nconf_nid(CONF *conf, X509V3_CTX *ctx, int ext_nid,
  52. const char *value)
  53. {
  54. int crit;
  55. int ext_type;
  56. crit = v3_check_critical(&value);
  57. if ((ext_type = v3_check_generic(&value)))
  58. return v3_generic_extension(OBJ_nid2sn(ext_nid),
  59. value, crit, ext_type, ctx);
  60. return do_ext_nconf(conf, ctx, ext_nid, crit, value);
  61. }
  62. /* CONF *conf: Config file */
  63. /* char *value: Value */
  64. static X509_EXTENSION *do_ext_nconf(CONF *conf, X509V3_CTX *ctx, int ext_nid,
  65. int crit, const char *value)
  66. {
  67. const X509V3_EXT_METHOD *method;
  68. X509_EXTENSION *ext;
  69. STACK_OF(CONF_VALUE) *nval;
  70. void *ext_struc;
  71. if (ext_nid == NID_undef) {
  72. X509V3err(X509V3_F_DO_EXT_NCONF, X509V3_R_UNKNOWN_EXTENSION_NAME);
  73. return NULL;
  74. }
  75. if ((method = X509V3_EXT_get_nid(ext_nid)) == NULL) {
  76. X509V3err(X509V3_F_DO_EXT_NCONF, X509V3_R_UNKNOWN_EXTENSION);
  77. return NULL;
  78. }
  79. /* Now get internal extension representation based on type */
  80. if (method->v2i) {
  81. if (*value == '@')
  82. nval = NCONF_get_section(conf, value + 1);
  83. else
  84. nval = X509V3_parse_list(value);
  85. if (nval == NULL || sk_CONF_VALUE_num(nval) <= 0) {
  86. X509V3err(X509V3_F_DO_EXT_NCONF,
  87. X509V3_R_INVALID_EXTENSION_STRING);
  88. ERR_add_error_data(4, "name=", OBJ_nid2sn(ext_nid), ",section=",
  89. value);
  90. if (*value != '@')
  91. sk_CONF_VALUE_pop_free(nval, X509V3_conf_free);
  92. return NULL;
  93. }
  94. ext_struc = method->v2i(method, ctx, nval);
  95. if (*value != '@')
  96. sk_CONF_VALUE_pop_free(nval, X509V3_conf_free);
  97. if (!ext_struc)
  98. return NULL;
  99. } else if (method->s2i) {
  100. if ((ext_struc = method->s2i(method, ctx, value)) == NULL)
  101. return NULL;
  102. } else if (method->r2i) {
  103. if (!ctx->db || !ctx->db_meth) {
  104. X509V3err(X509V3_F_DO_EXT_NCONF, X509V3_R_NO_CONFIG_DATABASE);
  105. return NULL;
  106. }
  107. if ((ext_struc = method->r2i(method, ctx, value)) == NULL)
  108. return NULL;
  109. } else {
  110. X509V3err(X509V3_F_DO_EXT_NCONF,
  111. X509V3_R_EXTENSION_SETTING_NOT_SUPPORTED);
  112. ERR_add_error_data(2, "name=", OBJ_nid2sn(ext_nid));
  113. return NULL;
  114. }
  115. ext = do_ext_i2d(method, ext_nid, crit, ext_struc);
  116. if (method->it)
  117. ASN1_item_free(ext_struc, ASN1_ITEM_ptr(method->it));
  118. else
  119. method->ext_free(ext_struc);
  120. return ext;
  121. }
  122. static X509_EXTENSION *do_ext_i2d(const X509V3_EXT_METHOD *method,
  123. int ext_nid, int crit, void *ext_struc)
  124. {
  125. unsigned char *ext_der = NULL;
  126. int ext_len;
  127. ASN1_OCTET_STRING *ext_oct = NULL;
  128. X509_EXTENSION *ext;
  129. /* Convert internal representation to DER */
  130. if (method->it) {
  131. ext_der = NULL;
  132. ext_len =
  133. ASN1_item_i2d(ext_struc, &ext_der, ASN1_ITEM_ptr(method->it));
  134. if (ext_len < 0)
  135. goto merr;
  136. } else {
  137. unsigned char *p;
  138. ext_len = method->i2d(ext_struc, NULL);
  139. if ((ext_der = OPENSSL_malloc(ext_len)) == NULL)
  140. goto merr;
  141. p = ext_der;
  142. method->i2d(ext_struc, &p);
  143. }
  144. if ((ext_oct = ASN1_OCTET_STRING_new()) == NULL)
  145. goto merr;
  146. ext_oct->data = ext_der;
  147. ext_der = NULL;
  148. ext_oct->length = ext_len;
  149. ext = X509_EXTENSION_create_by_NID(NULL, ext_nid, crit, ext_oct);
  150. if (!ext)
  151. goto merr;
  152. ASN1_OCTET_STRING_free(ext_oct);
  153. return ext;
  154. merr:
  155. X509V3err(X509V3_F_DO_EXT_I2D, ERR_R_MALLOC_FAILURE);
  156. OPENSSL_free(ext_der);
  157. ASN1_OCTET_STRING_free(ext_oct);
  158. return NULL;
  159. }
  160. /* Given an internal structure, nid and critical flag create an extension */
  161. X509_EXTENSION *X509V3_EXT_i2d(int ext_nid, int crit, void *ext_struc)
  162. {
  163. const X509V3_EXT_METHOD *method;
  164. if ((method = X509V3_EXT_get_nid(ext_nid)) == NULL) {
  165. X509V3err(X509V3_F_X509V3_EXT_I2D, X509V3_R_UNKNOWN_EXTENSION);
  166. return NULL;
  167. }
  168. return do_ext_i2d(method, ext_nid, crit, ext_struc);
  169. }
  170. /* Check the extension string for critical flag */
  171. static int v3_check_critical(const char **value)
  172. {
  173. const char *p = *value;
  174. if ((strlen(p) < 9) || strncmp(p, "critical,", 9))
  175. return 0;
  176. p += 9;
  177. while (ossl_isspace(*p))
  178. p++;
  179. *value = p;
  180. return 1;
  181. }
  182. /* Check extension string for generic extension and return the type */
  183. static int v3_check_generic(const char **value)
  184. {
  185. int gen_type = 0;
  186. const char *p = *value;
  187. if ((strlen(p) >= 4) && strncmp(p, "DER:", 4) == 0) {
  188. p += 4;
  189. gen_type = 1;
  190. } else if ((strlen(p) >= 5) && strncmp(p, "ASN1:", 5) == 0) {
  191. p += 5;
  192. gen_type = 2;
  193. } else
  194. return 0;
  195. while (ossl_isspace(*p))
  196. p++;
  197. *value = p;
  198. return gen_type;
  199. }
  200. /* Create a generic extension: for now just handle DER type */
  201. static X509_EXTENSION *v3_generic_extension(const char *ext, const char *value,
  202. int crit, int gen_type,
  203. X509V3_CTX *ctx)
  204. {
  205. unsigned char *ext_der = NULL;
  206. long ext_len = 0;
  207. ASN1_OBJECT *obj = NULL;
  208. ASN1_OCTET_STRING *oct = NULL;
  209. X509_EXTENSION *extension = NULL;
  210. if ((obj = OBJ_txt2obj(ext, 0)) == NULL) {
  211. X509V3err(X509V3_F_V3_GENERIC_EXTENSION,
  212. X509V3_R_EXTENSION_NAME_ERROR);
  213. ERR_add_error_data(2, "name=", ext);
  214. goto err;
  215. }
  216. if (gen_type == 1)
  217. ext_der = OPENSSL_hexstr2buf(value, &ext_len);
  218. else if (gen_type == 2)
  219. ext_der = generic_asn1(value, ctx, &ext_len);
  220. if (ext_der == NULL) {
  221. X509V3err(X509V3_F_V3_GENERIC_EXTENSION,
  222. X509V3_R_EXTENSION_VALUE_ERROR);
  223. ERR_add_error_data(2, "value=", value);
  224. goto err;
  225. }
  226. if ((oct = ASN1_OCTET_STRING_new()) == NULL) {
  227. X509V3err(X509V3_F_V3_GENERIC_EXTENSION, ERR_R_MALLOC_FAILURE);
  228. goto err;
  229. }
  230. oct->data = ext_der;
  231. oct->length = ext_len;
  232. ext_der = NULL;
  233. extension = X509_EXTENSION_create_by_OBJ(NULL, obj, crit, oct);
  234. err:
  235. ASN1_OBJECT_free(obj);
  236. ASN1_OCTET_STRING_free(oct);
  237. OPENSSL_free(ext_der);
  238. return extension;
  239. }
  240. static unsigned char *generic_asn1(const char *value, X509V3_CTX *ctx,
  241. long *ext_len)
  242. {
  243. ASN1_TYPE *typ;
  244. unsigned char *ext_der = NULL;
  245. typ = ASN1_generate_v3(value, ctx);
  246. if (typ == NULL)
  247. return NULL;
  248. *ext_len = i2d_ASN1_TYPE(typ, &ext_der);
  249. ASN1_TYPE_free(typ);
  250. return ext_der;
  251. }
  252. static void delete_ext(STACK_OF(X509_EXTENSION) *sk, X509_EXTENSION *dext)
  253. {
  254. int idx;
  255. ASN1_OBJECT *obj;
  256. obj = X509_EXTENSION_get_object(dext);
  257. while ((idx = X509v3_get_ext_by_OBJ(sk, obj, -1)) >= 0) {
  258. X509_EXTENSION *tmpext = X509v3_get_ext(sk, idx);
  259. X509v3_delete_ext(sk, idx);
  260. X509_EXTENSION_free(tmpext);
  261. }
  262. }
  263. /*
  264. * This is the main function: add a bunch of extensions based on a config
  265. * file section to an extension STACK.
  266. */
  267. int X509V3_EXT_add_nconf_sk(CONF *conf, X509V3_CTX *ctx, const char *section,
  268. STACK_OF(X509_EXTENSION) **sk)
  269. {
  270. X509_EXTENSION *ext;
  271. STACK_OF(CONF_VALUE) *nval;
  272. CONF_VALUE *val;
  273. int i;
  274. if ((nval = NCONF_get_section(conf, section)) == NULL)
  275. return 0;
  276. for (i = 0; i < sk_CONF_VALUE_num(nval); i++) {
  277. val = sk_CONF_VALUE_value(nval, i);
  278. if ((ext = X509V3_EXT_nconf(conf, ctx, val->name, val->value)) == NULL)
  279. return 0;
  280. if (ctx->flags == X509V3_CTX_REPLACE)
  281. delete_ext(*sk, ext);
  282. if (sk != NULL) {
  283. if (X509v3_add_ext(sk, ext, -1) == NULL) {
  284. X509_EXTENSION_free(ext);
  285. return 0;
  286. }
  287. }
  288. X509_EXTENSION_free(ext);
  289. }
  290. return 1;
  291. }
  292. /*
  293. * Convenience functions to add extensions to a certificate, CRL and request
  294. */
  295. int X509V3_EXT_add_nconf(CONF *conf, X509V3_CTX *ctx, const char *section,
  296. X509 *cert)
  297. {
  298. STACK_OF(X509_EXTENSION) **sk = NULL;
  299. if (cert)
  300. sk = &cert->cert_info.extensions;
  301. return X509V3_EXT_add_nconf_sk(conf, ctx, section, sk);
  302. }
  303. /* Same as above but for a CRL */
  304. int X509V3_EXT_CRL_add_nconf(CONF *conf, X509V3_CTX *ctx, const char *section,
  305. X509_CRL *crl)
  306. {
  307. STACK_OF(X509_EXTENSION) **sk = NULL;
  308. if (crl)
  309. sk = &crl->crl.extensions;
  310. return X509V3_EXT_add_nconf_sk(conf, ctx, section, sk);
  311. }
  312. /* Add extensions to certificate request */
  313. int X509V3_EXT_REQ_add_nconf(CONF *conf, X509V3_CTX *ctx, const char *section,
  314. X509_REQ *req)
  315. {
  316. STACK_OF(X509_EXTENSION) *extlist = NULL, **sk = NULL;
  317. int i;
  318. if (req)
  319. sk = &extlist;
  320. i = X509V3_EXT_add_nconf_sk(conf, ctx, section, sk);
  321. if (!i || !sk)
  322. return i;
  323. i = X509_REQ_add_extensions(req, extlist);
  324. sk_X509_EXTENSION_pop_free(extlist, X509_EXTENSION_free);
  325. return i;
  326. }
  327. /* Config database functions */
  328. char *X509V3_get_string(X509V3_CTX *ctx, const char *name, const char *section)
  329. {
  330. if (!ctx->db || !ctx->db_meth || !ctx->db_meth->get_string) {
  331. X509V3err(X509V3_F_X509V3_GET_STRING, X509V3_R_OPERATION_NOT_DEFINED);
  332. return NULL;
  333. }
  334. if (ctx->db_meth->get_string)
  335. return ctx->db_meth->get_string(ctx->db, name, section);
  336. return NULL;
  337. }
  338. STACK_OF(CONF_VALUE) *X509V3_get_section(X509V3_CTX *ctx, const char *section)
  339. {
  340. if (!ctx->db || !ctx->db_meth || !ctx->db_meth->get_section) {
  341. X509V3err(X509V3_F_X509V3_GET_SECTION,
  342. X509V3_R_OPERATION_NOT_DEFINED);
  343. return NULL;
  344. }
  345. if (ctx->db_meth->get_section)
  346. return ctx->db_meth->get_section(ctx->db, section);
  347. return NULL;
  348. }
  349. void X509V3_string_free(X509V3_CTX *ctx, char *str)
  350. {
  351. if (!str)
  352. return;
  353. if (ctx->db_meth->free_string)
  354. ctx->db_meth->free_string(ctx->db, str);
  355. }
  356. void X509V3_section_free(X509V3_CTX *ctx, STACK_OF(CONF_VALUE) *section)
  357. {
  358. if (!section)
  359. return;
  360. if (ctx->db_meth->free_section)
  361. ctx->db_meth->free_section(ctx->db, section);
  362. }
  363. static char *nconf_get_string(void *db, const char *section, const char *value)
  364. {
  365. return NCONF_get_string(db, section, value);
  366. }
  367. static STACK_OF(CONF_VALUE) *nconf_get_section(void *db, const char *section)
  368. {
  369. return NCONF_get_section(db, section);
  370. }
  371. static X509V3_CONF_METHOD nconf_method = {
  372. nconf_get_string,
  373. nconf_get_section,
  374. NULL,
  375. NULL
  376. };
  377. void X509V3_set_nconf(X509V3_CTX *ctx, CONF *conf)
  378. {
  379. ctx->db_meth = &nconf_method;
  380. ctx->db = conf;
  381. }
  382. void X509V3_set_ctx(X509V3_CTX *ctx, X509 *issuer, X509 *subj, X509_REQ *req,
  383. X509_CRL *crl, int flags)
  384. {
  385. ctx->issuer_cert = issuer;
  386. ctx->subject_cert = subj;
  387. ctx->crl = crl;
  388. ctx->subject_req = req;
  389. ctx->flags = flags;
  390. }
  391. /* Old conf compatibility functions */
  392. X509_EXTENSION *X509V3_EXT_conf(LHASH_OF(CONF_VALUE) *conf, X509V3_CTX *ctx,
  393. const char *name, const char *value)
  394. {
  395. CONF ctmp;
  396. CONF_set_nconf(&ctmp, conf);
  397. return X509V3_EXT_nconf(&ctmp, ctx, name, value);
  398. }
  399. /* LHASH *conf: Config file */
  400. /* char *value: Value */
  401. X509_EXTENSION *X509V3_EXT_conf_nid(LHASH_OF(CONF_VALUE) *conf,
  402. X509V3_CTX *ctx, int ext_nid, const char *value)
  403. {
  404. CONF ctmp;
  405. CONF_set_nconf(&ctmp, conf);
  406. return X509V3_EXT_nconf_nid(&ctmp, ctx, ext_nid, value);
  407. }
  408. static char *conf_lhash_get_string(void *db, const char *section, const char *value)
  409. {
  410. return CONF_get_string(db, section, value);
  411. }
  412. static STACK_OF(CONF_VALUE) *conf_lhash_get_section(void *db, const char *section)
  413. {
  414. return CONF_get_section(db, section);
  415. }
  416. static X509V3_CONF_METHOD conf_lhash_method = {
  417. conf_lhash_get_string,
  418. conf_lhash_get_section,
  419. NULL,
  420. NULL
  421. };
  422. void X509V3_set_conf_lhash(X509V3_CTX *ctx, LHASH_OF(CONF_VALUE) *lhash)
  423. {
  424. ctx->db_meth = &conf_lhash_method;
  425. ctx->db = lhash;
  426. }
  427. int X509V3_EXT_add_conf(LHASH_OF(CONF_VALUE) *conf, X509V3_CTX *ctx,
  428. const char *section, X509 *cert)
  429. {
  430. CONF ctmp;
  431. CONF_set_nconf(&ctmp, conf);
  432. return X509V3_EXT_add_nconf(&ctmp, ctx, section, cert);
  433. }
  434. /* Same as above but for a CRL */
  435. int X509V3_EXT_CRL_add_conf(LHASH_OF(CONF_VALUE) *conf, X509V3_CTX *ctx,
  436. const char *section, X509_CRL *crl)
  437. {
  438. CONF ctmp;
  439. CONF_set_nconf(&ctmp, conf);
  440. return X509V3_EXT_CRL_add_nconf(&ctmp, ctx, section, crl);
  441. }
  442. /* Add extensions to certificate request */
  443. int X509V3_EXT_REQ_add_conf(LHASH_OF(CONF_VALUE) *conf, X509V3_CTX *ctx,
  444. const char *section, X509_REQ *req)
  445. {
  446. CONF ctmp;
  447. CONF_set_nconf(&ctmp, conf);
  448. return X509V3_EXT_REQ_add_nconf(&ctmp, ctx, section, req);
  449. }