v3_san.c 20 KB

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