v3_conf.c 15 KB

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