v3_alt.c 20 KB

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