v3_san.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698
  1. /*
  2. * Copyright 1999-2021 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 "crypto/x509.h"
  12. #include <openssl/conf.h>
  13. #include <openssl/x509v3.h>
  14. #include <openssl/bio.h>
  15. #include "ext_dat.h"
  16. static GENERAL_NAMES *v2i_subject_alt(X509V3_EXT_METHOD *method,
  17. X509V3_CTX *ctx,
  18. STACK_OF(CONF_VALUE) *nval);
  19. static GENERAL_NAMES *v2i_issuer_alt(X509V3_EXT_METHOD *method,
  20. X509V3_CTX *ctx,
  21. STACK_OF(CONF_VALUE) *nval);
  22. static int copy_email(X509V3_CTX *ctx, GENERAL_NAMES *gens, int move_p);
  23. static int copy_issuer(X509V3_CTX *ctx, GENERAL_NAMES *gens);
  24. static int do_othername(GENERAL_NAME *gen, const char *value, X509V3_CTX *ctx);
  25. static int do_dirname(GENERAL_NAME *gen, const char *value, X509V3_CTX *ctx);
  26. const X509V3_EXT_METHOD ossl_v3_alt[3] = {
  27. {NID_subject_alt_name, 0, ASN1_ITEM_ref(GENERAL_NAMES),
  28. 0, 0, 0, 0,
  29. 0, 0,
  30. (X509V3_EXT_I2V) i2v_GENERAL_NAMES,
  31. (X509V3_EXT_V2I)v2i_subject_alt,
  32. NULL, NULL, NULL},
  33. {NID_issuer_alt_name, 0, ASN1_ITEM_ref(GENERAL_NAMES),
  34. 0, 0, 0, 0,
  35. 0, 0,
  36. (X509V3_EXT_I2V) i2v_GENERAL_NAMES,
  37. (X509V3_EXT_V2I)v2i_issuer_alt,
  38. NULL, NULL, NULL},
  39. {NID_certificate_issuer, 0, ASN1_ITEM_ref(GENERAL_NAMES),
  40. 0, 0, 0, 0,
  41. 0, 0,
  42. (X509V3_EXT_I2V) i2v_GENERAL_NAMES,
  43. NULL, NULL, NULL, NULL},
  44. };
  45. STACK_OF(CONF_VALUE) *i2v_GENERAL_NAMES(X509V3_EXT_METHOD *method,
  46. GENERAL_NAMES *gens,
  47. STACK_OF(CONF_VALUE) *ret)
  48. {
  49. int i;
  50. GENERAL_NAME *gen;
  51. STACK_OF(CONF_VALUE) *tmpret = NULL, *origret = ret;
  52. for (i = 0; i < sk_GENERAL_NAME_num(gens); i++) {
  53. gen = sk_GENERAL_NAME_value(gens, i);
  54. /*
  55. * i2v_GENERAL_NAME allocates ret if it is NULL. If something goes
  56. * wrong we need to free the stack - but only if it was empty when we
  57. * originally entered this function.
  58. */
  59. tmpret = i2v_GENERAL_NAME(method, gen, ret);
  60. if (tmpret == NULL) {
  61. if (origret == NULL)
  62. sk_CONF_VALUE_pop_free(ret, X509V3_conf_free);
  63. return NULL;
  64. }
  65. ret = tmpret;
  66. }
  67. if (ret == NULL)
  68. return sk_CONF_VALUE_new_null();
  69. return ret;
  70. }
  71. STACK_OF(CONF_VALUE) *i2v_GENERAL_NAME(X509V3_EXT_METHOD *method,
  72. GENERAL_NAME *gen,
  73. STACK_OF(CONF_VALUE) *ret)
  74. {
  75. char othername[300];
  76. char oline[256], *tmp;
  77. switch (gen->type) {
  78. case GEN_OTHERNAME:
  79. switch (OBJ_obj2nid(gen->d.otherName->type_id)) {
  80. case NID_id_on_SmtpUTF8Mailbox:
  81. if (gen->d.otherName->value->type != V_ASN1_UTF8STRING
  82. || !x509v3_add_len_value_uchar("othername: SmtpUTF8Mailbox:",
  83. gen->d.otherName->value->value.utf8string->data,
  84. gen->d.otherName->value->value.utf8string->length,
  85. &ret))
  86. return NULL;
  87. break;
  88. case NID_XmppAddr:
  89. if (gen->d.otherName->value->type != V_ASN1_UTF8STRING
  90. || !x509v3_add_len_value_uchar("othername: XmppAddr:",
  91. gen->d.otherName->value->value.utf8string->data,
  92. gen->d.otherName->value->value.utf8string->length,
  93. &ret))
  94. return NULL;
  95. break;
  96. case NID_SRVName:
  97. if (gen->d.otherName->value->type != V_ASN1_IA5STRING
  98. || !x509v3_add_len_value_uchar("othername: SRVName:",
  99. gen->d.otherName->value->value.ia5string->data,
  100. gen->d.otherName->value->value.ia5string->length,
  101. &ret))
  102. return NULL;
  103. break;
  104. case NID_ms_upn:
  105. if (gen->d.otherName->value->type != V_ASN1_UTF8STRING
  106. || !x509v3_add_len_value_uchar("othername: UPN:",
  107. gen->d.otherName->value->value.utf8string->data,
  108. gen->d.otherName->value->value.utf8string->length,
  109. &ret))
  110. return NULL;
  111. break;
  112. case NID_NAIRealm:
  113. if (gen->d.otherName->value->type != V_ASN1_UTF8STRING
  114. || !x509v3_add_len_value_uchar("othername: NAIRealm:",
  115. gen->d.otherName->value->value.utf8string->data,
  116. gen->d.otherName->value->value.utf8string->length,
  117. &ret))
  118. return NULL;
  119. break;
  120. default:
  121. if (OBJ_obj2txt(oline, sizeof(oline), gen->d.otherName->type_id, 0) > 0)
  122. BIO_snprintf(othername, sizeof(othername), "othername: %s:",
  123. oline);
  124. else
  125. OPENSSL_strlcpy(othername, "othername:", sizeof(othername));
  126. /* check if the value is something printable */
  127. if (gen->d.otherName->value->type == V_ASN1_IA5STRING) {
  128. if (x509v3_add_len_value_uchar(othername,
  129. gen->d.otherName->value->value.ia5string->data,
  130. gen->d.otherName->value->value.ia5string->length,
  131. &ret))
  132. return ret;
  133. }
  134. if (gen->d.otherName->value->type == V_ASN1_UTF8STRING) {
  135. if (x509v3_add_len_value_uchar(othername,
  136. gen->d.otherName->value->value.utf8string->data,
  137. gen->d.otherName->value->value.utf8string->length,
  138. &ret))
  139. return ret;
  140. }
  141. if (!X509V3_add_value(othername, "<unsupported>", &ret))
  142. return NULL;
  143. break;
  144. }
  145. break;
  146. case GEN_X400:
  147. if (!X509V3_add_value("X400Name", "<unsupported>", &ret))
  148. return NULL;
  149. break;
  150. case GEN_EDIPARTY:
  151. if (!X509V3_add_value("EdiPartyName", "<unsupported>", &ret))
  152. return NULL;
  153. break;
  154. case GEN_EMAIL:
  155. if (!x509v3_add_len_value_uchar("email", gen->d.ia5->data,
  156. gen->d.ia5->length, &ret))
  157. return NULL;
  158. break;
  159. case GEN_DNS:
  160. if (!x509v3_add_len_value_uchar("DNS", gen->d.ia5->data,
  161. gen->d.ia5->length, &ret))
  162. return NULL;
  163. break;
  164. case GEN_URI:
  165. if (!x509v3_add_len_value_uchar("URI", gen->d.ia5->data,
  166. gen->d.ia5->length, &ret))
  167. return NULL;
  168. break;
  169. case GEN_DIRNAME:
  170. if (X509_NAME_oneline(gen->d.dirn, oline, sizeof(oline)) == NULL
  171. || !X509V3_add_value("DirName", oline, &ret))
  172. return NULL;
  173. break;
  174. case GEN_IPADD:
  175. tmp = ossl_ipaddr_to_asc(gen->d.ip->data, gen->d.ip->length);
  176. if (tmp == NULL || !X509V3_add_value("IP Address", tmp, &ret))
  177. ret = NULL;
  178. OPENSSL_free(tmp);
  179. break;
  180. case GEN_RID:
  181. i2t_ASN1_OBJECT(oline, 256, gen->d.rid);
  182. if (!X509V3_add_value("Registered ID", oline, &ret))
  183. return NULL;
  184. break;
  185. }
  186. return ret;
  187. }
  188. int GENERAL_NAME_print(BIO *out, GENERAL_NAME *gen)
  189. {
  190. char *tmp;
  191. int nid;
  192. switch (gen->type) {
  193. case GEN_OTHERNAME:
  194. nid = OBJ_obj2nid(gen->d.otherName->type_id);
  195. /* Validate the types are as we expect before we use them */
  196. if ((nid == NID_SRVName
  197. && gen->d.otherName->value->type != V_ASN1_IA5STRING)
  198. || (nid != NID_SRVName
  199. && gen->d.otherName->value->type != V_ASN1_UTF8STRING)) {
  200. BIO_printf(out, "othername:<unsupported>");
  201. break;
  202. }
  203. switch (nid) {
  204. case NID_id_on_SmtpUTF8Mailbox:
  205. BIO_printf(out, "othername:SmtpUTF8Mailbox:%.*s",
  206. gen->d.otherName->value->value.utf8string->length,
  207. gen->d.otherName->value->value.utf8string->data);
  208. break;
  209. case NID_XmppAddr:
  210. BIO_printf(out, "othername:XmppAddr:%.*s",
  211. gen->d.otherName->value->value.utf8string->length,
  212. gen->d.otherName->value->value.utf8string->data);
  213. break;
  214. case NID_SRVName:
  215. BIO_printf(out, "othername:SRVName:%.*s",
  216. gen->d.otherName->value->value.ia5string->length,
  217. gen->d.otherName->value->value.ia5string->data);
  218. break;
  219. case NID_ms_upn:
  220. BIO_printf(out, "othername:UPN:%.*s",
  221. gen->d.otherName->value->value.utf8string->length,
  222. gen->d.otherName->value->value.utf8string->data);
  223. break;
  224. case NID_NAIRealm:
  225. BIO_printf(out, "othername:NAIRealm:%.*s",
  226. gen->d.otherName->value->value.utf8string->length,
  227. gen->d.otherName->value->value.utf8string->data);
  228. break;
  229. default:
  230. BIO_printf(out, "othername:<unsupported>");
  231. break;
  232. }
  233. break;
  234. case GEN_X400:
  235. BIO_printf(out, "X400Name:<unsupported>");
  236. break;
  237. case GEN_EDIPARTY:
  238. /* Maybe fix this: it is supported now */
  239. BIO_printf(out, "EdiPartyName:<unsupported>");
  240. break;
  241. case GEN_EMAIL:
  242. BIO_printf(out, "email:");
  243. ASN1_STRING_print(out, gen->d.ia5);
  244. break;
  245. case GEN_DNS:
  246. BIO_printf(out, "DNS:");
  247. ASN1_STRING_print(out, gen->d.ia5);
  248. break;
  249. case GEN_URI:
  250. BIO_printf(out, "URI:");
  251. ASN1_STRING_print(out, gen->d.ia5);
  252. break;
  253. case GEN_DIRNAME:
  254. BIO_printf(out, "DirName:");
  255. X509_NAME_print_ex(out, gen->d.dirn, 0, XN_FLAG_ONELINE);
  256. break;
  257. case GEN_IPADD:
  258. tmp = ossl_ipaddr_to_asc(gen->d.ip->data, gen->d.ip->length);
  259. if (tmp == NULL)
  260. return 0;
  261. BIO_printf(out, "IP Address:%s", tmp);
  262. OPENSSL_free(tmp);
  263. break;
  264. case GEN_RID:
  265. BIO_printf(out, "Registered ID:");
  266. i2a_ASN1_OBJECT(out, gen->d.rid);
  267. break;
  268. }
  269. return 1;
  270. }
  271. static GENERAL_NAMES *v2i_issuer_alt(X509V3_EXT_METHOD *method,
  272. X509V3_CTX *ctx,
  273. STACK_OF(CONF_VALUE) *nval)
  274. {
  275. const int num = sk_CONF_VALUE_num(nval);
  276. GENERAL_NAMES *gens = sk_GENERAL_NAME_new_reserve(NULL, num);
  277. int i;
  278. if (gens == NULL) {
  279. ERR_raise(ERR_LIB_X509V3, ERR_R_CRYPTO_LIB);
  280. sk_GENERAL_NAME_free(gens);
  281. return NULL;
  282. }
  283. for (i = 0; i < num; i++) {
  284. CONF_VALUE *cnf = sk_CONF_VALUE_value(nval, i);
  285. if (!ossl_v3_name_cmp(cnf->name, "issuer")
  286. && cnf->value && strcmp(cnf->value, "copy") == 0) {
  287. if (!copy_issuer(ctx, gens))
  288. goto err;
  289. } else {
  290. GENERAL_NAME *gen = v2i_GENERAL_NAME(method, ctx, cnf);
  291. if (gen == NULL)
  292. goto err;
  293. sk_GENERAL_NAME_push(gens, gen); /* no failure as it was reserved */
  294. }
  295. }
  296. return gens;
  297. err:
  298. sk_GENERAL_NAME_pop_free(gens, GENERAL_NAME_free);
  299. return NULL;
  300. }
  301. /* Append subject altname of issuer to issuer alt name of subject */
  302. static int copy_issuer(X509V3_CTX *ctx, GENERAL_NAMES *gens)
  303. {
  304. GENERAL_NAMES *ialt;
  305. GENERAL_NAME *gen;
  306. X509_EXTENSION *ext;
  307. int i, num;
  308. if (ctx != NULL && (ctx->flags & X509V3_CTX_TEST) != 0)
  309. return 1;
  310. if (!ctx || !ctx->issuer_cert) {
  311. ERR_raise(ERR_LIB_X509V3, X509V3_R_NO_ISSUER_DETAILS);
  312. goto err;
  313. }
  314. i = X509_get_ext_by_NID(ctx->issuer_cert, NID_subject_alt_name, -1);
  315. if (i < 0)
  316. return 1;
  317. if ((ext = X509_get_ext(ctx->issuer_cert, i)) == NULL
  318. || (ialt = X509V3_EXT_d2i(ext)) == NULL) {
  319. ERR_raise(ERR_LIB_X509V3, X509V3_R_ISSUER_DECODE_ERROR);
  320. goto err;
  321. }
  322. num = sk_GENERAL_NAME_num(ialt);
  323. if (!sk_GENERAL_NAME_reserve(gens, num)) {
  324. ERR_raise(ERR_LIB_X509V3, ERR_R_CRYPTO_LIB);
  325. goto err;
  326. }
  327. for (i = 0; i < num; i++) {
  328. gen = sk_GENERAL_NAME_value(ialt, i);
  329. sk_GENERAL_NAME_push(gens, gen); /* no failure as it was reserved */
  330. }
  331. sk_GENERAL_NAME_free(ialt);
  332. return 1;
  333. err:
  334. return 0;
  335. }
  336. static GENERAL_NAMES *v2i_subject_alt(X509V3_EXT_METHOD *method,
  337. X509V3_CTX *ctx,
  338. STACK_OF(CONF_VALUE) *nval)
  339. {
  340. GENERAL_NAMES *gens;
  341. CONF_VALUE *cnf;
  342. const int num = sk_CONF_VALUE_num(nval);
  343. int i;
  344. gens = sk_GENERAL_NAME_new_reserve(NULL, num);
  345. if (gens == NULL) {
  346. ERR_raise(ERR_LIB_X509V3, ERR_R_CRYPTO_LIB);
  347. sk_GENERAL_NAME_free(gens);
  348. return NULL;
  349. }
  350. for (i = 0; i < num; i++) {
  351. cnf = sk_CONF_VALUE_value(nval, i);
  352. if (ossl_v3_name_cmp(cnf->name, "email") == 0
  353. && cnf->value && strcmp(cnf->value, "copy") == 0) {
  354. if (!copy_email(ctx, gens, 0))
  355. goto err;
  356. } else if (ossl_v3_name_cmp(cnf->name, "email") == 0
  357. && cnf->value && strcmp(cnf->value, "move") == 0) {
  358. if (!copy_email(ctx, gens, 1))
  359. goto err;
  360. } else {
  361. GENERAL_NAME *gen;
  362. if ((gen = v2i_GENERAL_NAME(method, ctx, cnf)) == NULL)
  363. goto err;
  364. sk_GENERAL_NAME_push(gens, gen); /* no failure as it was reserved */
  365. }
  366. }
  367. return gens;
  368. err:
  369. sk_GENERAL_NAME_pop_free(gens, GENERAL_NAME_free);
  370. return NULL;
  371. }
  372. /*
  373. * Copy any email addresses in a certificate or request to GENERAL_NAMES
  374. */
  375. static int copy_email(X509V3_CTX *ctx, GENERAL_NAMES *gens, int move_p)
  376. {
  377. X509_NAME *nm;
  378. ASN1_IA5STRING *email = NULL;
  379. X509_NAME_ENTRY *ne;
  380. GENERAL_NAME *gen = NULL;
  381. int i = -1;
  382. if (ctx != NULL && (ctx->flags & X509V3_CTX_TEST) != 0)
  383. return 1;
  384. if (ctx == NULL
  385. || (ctx->subject_cert == NULL && ctx->subject_req == NULL)) {
  386. ERR_raise(ERR_LIB_X509V3, X509V3_R_NO_SUBJECT_DETAILS);
  387. return 0;
  388. }
  389. /* Find the subject name */
  390. nm = ctx->subject_cert != NULL ?
  391. X509_get_subject_name(ctx->subject_cert) :
  392. X509_REQ_get_subject_name(ctx->subject_req);
  393. /* Now add any email address(es) to STACK */
  394. while ((i = X509_NAME_get_index_by_NID(nm,
  395. NID_pkcs9_emailAddress, i)) >= 0) {
  396. ne = X509_NAME_get_entry(nm, i);
  397. email = ASN1_STRING_dup(X509_NAME_ENTRY_get_data(ne));
  398. if (move_p) {
  399. X509_NAME_delete_entry(nm, i);
  400. X509_NAME_ENTRY_free(ne);
  401. i--;
  402. }
  403. if (email == NULL || (gen = GENERAL_NAME_new()) == NULL) {
  404. ERR_raise(ERR_LIB_X509V3, ERR_R_ASN1_LIB);
  405. goto err;
  406. }
  407. gen->d.ia5 = email;
  408. email = NULL;
  409. gen->type = GEN_EMAIL;
  410. if (!sk_GENERAL_NAME_push(gens, gen)) {
  411. ERR_raise(ERR_LIB_X509V3, ERR_R_CRYPTO_LIB);
  412. goto err;
  413. }
  414. gen = NULL;
  415. }
  416. return 1;
  417. err:
  418. GENERAL_NAME_free(gen);
  419. ASN1_IA5STRING_free(email);
  420. return 0;
  421. }
  422. GENERAL_NAMES *v2i_GENERAL_NAMES(const X509V3_EXT_METHOD *method,
  423. X509V3_CTX *ctx, STACK_OF(CONF_VALUE) *nval)
  424. {
  425. GENERAL_NAME *gen;
  426. GENERAL_NAMES *gens;
  427. CONF_VALUE *cnf;
  428. const int num = sk_CONF_VALUE_num(nval);
  429. int i;
  430. gens = sk_GENERAL_NAME_new_reserve(NULL, num);
  431. if (gens == NULL) {
  432. ERR_raise(ERR_LIB_X509V3, ERR_R_CRYPTO_LIB);
  433. sk_GENERAL_NAME_free(gens);
  434. return NULL;
  435. }
  436. for (i = 0; i < num; i++) {
  437. cnf = sk_CONF_VALUE_value(nval, i);
  438. if ((gen = v2i_GENERAL_NAME(method, ctx, cnf)) == NULL)
  439. goto err;
  440. sk_GENERAL_NAME_push(gens, gen); /* no failure as it was reserved */
  441. }
  442. return gens;
  443. err:
  444. sk_GENERAL_NAME_pop_free(gens, GENERAL_NAME_free);
  445. return NULL;
  446. }
  447. GENERAL_NAME *v2i_GENERAL_NAME(const X509V3_EXT_METHOD *method,
  448. X509V3_CTX *ctx, CONF_VALUE *cnf)
  449. {
  450. return v2i_GENERAL_NAME_ex(NULL, method, ctx, cnf, 0);
  451. }
  452. GENERAL_NAME *a2i_GENERAL_NAME(GENERAL_NAME *out,
  453. const X509V3_EXT_METHOD *method,
  454. X509V3_CTX *ctx, int gen_type, const char *value,
  455. int is_nc)
  456. {
  457. char is_string = 0;
  458. GENERAL_NAME *gen = NULL;
  459. if (!value) {
  460. ERR_raise(ERR_LIB_X509V3, X509V3_R_MISSING_VALUE);
  461. return NULL;
  462. }
  463. if (out)
  464. gen = out;
  465. else {
  466. gen = GENERAL_NAME_new();
  467. if (gen == NULL) {
  468. ERR_raise(ERR_LIB_X509V3, ERR_R_ASN1_LIB);
  469. return NULL;
  470. }
  471. }
  472. switch (gen_type) {
  473. case GEN_URI:
  474. case GEN_EMAIL:
  475. case GEN_DNS:
  476. is_string = 1;
  477. break;
  478. case GEN_RID:
  479. {
  480. ASN1_OBJECT *obj;
  481. if ((obj = OBJ_txt2obj(value, 0)) == NULL) {
  482. ERR_raise_data(ERR_LIB_X509V3, X509V3_R_BAD_OBJECT,
  483. "value=%s", value);
  484. goto err;
  485. }
  486. gen->d.rid = obj;
  487. }
  488. break;
  489. case GEN_IPADD:
  490. if (is_nc)
  491. gen->d.ip = a2i_IPADDRESS_NC(value);
  492. else
  493. gen->d.ip = a2i_IPADDRESS(value);
  494. if (gen->d.ip == NULL) {
  495. ERR_raise_data(ERR_LIB_X509V3, X509V3_R_BAD_IP_ADDRESS,
  496. "value=%s", value);
  497. goto err;
  498. }
  499. break;
  500. case GEN_DIRNAME:
  501. if (!do_dirname(gen, value, ctx)) {
  502. ERR_raise(ERR_LIB_X509V3, X509V3_R_DIRNAME_ERROR);
  503. goto err;
  504. }
  505. break;
  506. case GEN_OTHERNAME:
  507. if (!do_othername(gen, value, ctx)) {
  508. ERR_raise(ERR_LIB_X509V3, X509V3_R_OTHERNAME_ERROR);
  509. goto err;
  510. }
  511. break;
  512. default:
  513. ERR_raise(ERR_LIB_X509V3, X509V3_R_UNSUPPORTED_TYPE);
  514. goto err;
  515. }
  516. if (is_string) {
  517. if ((gen->d.ia5 = ASN1_IA5STRING_new()) == NULL ||
  518. !ASN1_STRING_set(gen->d.ia5, (unsigned char *)value,
  519. strlen(value))) {
  520. ASN1_IA5STRING_free(gen->d.ia5);
  521. gen->d.ia5 = NULL;
  522. ERR_raise(ERR_LIB_X509V3, ERR_R_ASN1_LIB);
  523. goto err;
  524. }
  525. }
  526. gen->type = gen_type;
  527. return gen;
  528. err:
  529. if (!out)
  530. GENERAL_NAME_free(gen);
  531. return NULL;
  532. }
  533. GENERAL_NAME *v2i_GENERAL_NAME_ex(GENERAL_NAME *out,
  534. const X509V3_EXT_METHOD *method,
  535. X509V3_CTX *ctx, CONF_VALUE *cnf, int is_nc)
  536. {
  537. int type;
  538. char *name, *value;
  539. name = cnf->name;
  540. value = cnf->value;
  541. if (!value) {
  542. ERR_raise(ERR_LIB_X509V3, X509V3_R_MISSING_VALUE);
  543. return NULL;
  544. }
  545. if (!ossl_v3_name_cmp(name, "email"))
  546. type = GEN_EMAIL;
  547. else if (!ossl_v3_name_cmp(name, "URI"))
  548. type = GEN_URI;
  549. else if (!ossl_v3_name_cmp(name, "DNS"))
  550. type = GEN_DNS;
  551. else if (!ossl_v3_name_cmp(name, "RID"))
  552. type = GEN_RID;
  553. else if (!ossl_v3_name_cmp(name, "IP"))
  554. type = GEN_IPADD;
  555. else if (!ossl_v3_name_cmp(name, "dirName"))
  556. type = GEN_DIRNAME;
  557. else if (!ossl_v3_name_cmp(name, "otherName"))
  558. type = GEN_OTHERNAME;
  559. else {
  560. ERR_raise_data(ERR_LIB_X509V3, X509V3_R_UNSUPPORTED_OPTION,
  561. "name=%s", name);
  562. return NULL;
  563. }
  564. return a2i_GENERAL_NAME(out, method, ctx, type, value, is_nc);
  565. }
  566. static int do_othername(GENERAL_NAME *gen, const char *value, X509V3_CTX *ctx)
  567. {
  568. char *objtmp = NULL, *p;
  569. int objlen;
  570. if ((p = strchr(value, ';')) == NULL)
  571. return 0;
  572. if ((gen->d.otherName = OTHERNAME_new()) == NULL)
  573. return 0;
  574. /*
  575. * Free this up because we will overwrite it. no need to free type_id
  576. * because it is static
  577. */
  578. ASN1_TYPE_free(gen->d.otherName->value);
  579. if ((gen->d.otherName->value = ASN1_generate_v3(p + 1, ctx)) == NULL)
  580. goto err;
  581. objlen = p - value;
  582. objtmp = OPENSSL_strndup(value, objlen);
  583. if (objtmp == NULL)
  584. goto err;
  585. gen->d.otherName->type_id = OBJ_txt2obj(objtmp, 0);
  586. OPENSSL_free(objtmp);
  587. if (!gen->d.otherName->type_id)
  588. goto err;
  589. return 1;
  590. err:
  591. OTHERNAME_free(gen->d.otherName);
  592. gen->d.otherName = NULL;
  593. return 0;
  594. }
  595. static int do_dirname(GENERAL_NAME *gen, const char *value, X509V3_CTX *ctx)
  596. {
  597. int ret = 0;
  598. STACK_OF(CONF_VALUE) *sk = NULL;
  599. X509_NAME *nm;
  600. if ((nm = X509_NAME_new()) == NULL)
  601. goto err;
  602. sk = X509V3_get_section(ctx, value);
  603. if (!sk) {
  604. ERR_raise_data(ERR_LIB_X509V3, X509V3_R_SECTION_NOT_FOUND,
  605. "section=%s", value);
  606. goto err;
  607. }
  608. /* FIXME: should allow other character types... */
  609. ret = X509V3_NAME_from_section(nm, sk, MBSTRING_ASC);
  610. if (!ret)
  611. goto err;
  612. gen->d.dirn = nm;
  613. err:
  614. if (ret == 0)
  615. X509_NAME_free(nm);
  616. X509V3_section_free(ctx, sk);
  617. return ret;
  618. }