v3_alt.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624
  1. /* v3_alt.c */
  2. /*
  3. * Written by Dr Stephen N Henson ([email protected]) for the OpenSSL
  4. * project.
  5. */
  6. /* ====================================================================
  7. * Copyright (c) 1999-2019 The OpenSSL Project. All rights reserved.
  8. *
  9. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted provided that the following conditions
  11. * are met:
  12. *
  13. * 1. Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions and the following disclaimer.
  15. *
  16. * 2. Redistributions in binary form must reproduce the above copyright
  17. * notice, this list of conditions and the following disclaimer in
  18. * the documentation and/or other materials provided with the
  19. * distribution.
  20. *
  21. * 3. All advertising materials mentioning features or use of this
  22. * software must display the following acknowledgment:
  23. * "This product includes software developed by the OpenSSL Project
  24. * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
  25. *
  26. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  27. * endorse or promote products derived from this software without
  28. * prior written permission. For written permission, please contact
  29. * [email protected].
  30. *
  31. * 5. Products derived from this software may not be called "OpenSSL"
  32. * nor may "OpenSSL" appear in their names without prior written
  33. * permission of the OpenSSL Project.
  34. *
  35. * 6. Redistributions of any form whatsoever must retain the following
  36. * acknowledgment:
  37. * "This product includes software developed by the OpenSSL Project
  38. * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
  39. *
  40. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  41. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  42. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  43. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  44. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  45. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  46. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  47. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  48. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  49. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  50. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  51. * OF THE POSSIBILITY OF SUCH DAMAGE.
  52. * ====================================================================
  53. *
  54. * This product includes cryptographic software written by Eric Young
  55. * ([email protected]). This product includes software written by Tim
  56. * Hudson ([email protected]).
  57. *
  58. */
  59. #include <stdio.h>
  60. #include "cryptlib.h"
  61. #include <openssl/conf.h>
  62. #include <openssl/x509v3.h>
  63. static GENERAL_NAMES *v2i_subject_alt(X509V3_EXT_METHOD *method,
  64. X509V3_CTX *ctx,
  65. STACK_OF(CONF_VALUE) *nval);
  66. static GENERAL_NAMES *v2i_issuer_alt(X509V3_EXT_METHOD *method,
  67. X509V3_CTX *ctx,
  68. STACK_OF(CONF_VALUE) *nval);
  69. static int copy_email(X509V3_CTX *ctx, GENERAL_NAMES *gens, int move_p);
  70. static int copy_issuer(X509V3_CTX *ctx, GENERAL_NAMES *gens);
  71. static int do_othername(GENERAL_NAME *gen, char *value, X509V3_CTX *ctx);
  72. static int do_dirname(GENERAL_NAME *gen, char *value, X509V3_CTX *ctx);
  73. const X509V3_EXT_METHOD v3_alt[] = {
  74. {NID_subject_alt_name, 0, ASN1_ITEM_ref(GENERAL_NAMES),
  75. 0, 0, 0, 0,
  76. 0, 0,
  77. (X509V3_EXT_I2V) i2v_GENERAL_NAMES,
  78. (X509V3_EXT_V2I)v2i_subject_alt,
  79. NULL, NULL, NULL},
  80. {NID_issuer_alt_name, 0, ASN1_ITEM_ref(GENERAL_NAMES),
  81. 0, 0, 0, 0,
  82. 0, 0,
  83. (X509V3_EXT_I2V) i2v_GENERAL_NAMES,
  84. (X509V3_EXT_V2I)v2i_issuer_alt,
  85. NULL, NULL, NULL},
  86. {NID_certificate_issuer, 0, ASN1_ITEM_ref(GENERAL_NAMES),
  87. 0, 0, 0, 0,
  88. 0, 0,
  89. (X509V3_EXT_I2V) i2v_GENERAL_NAMES,
  90. NULL, NULL, NULL, NULL},
  91. };
  92. STACK_OF(CONF_VALUE) *i2v_GENERAL_NAMES(X509V3_EXT_METHOD *method,
  93. GENERAL_NAMES *gens,
  94. STACK_OF(CONF_VALUE) *ret)
  95. {
  96. int i;
  97. GENERAL_NAME *gen;
  98. for (i = 0; i < sk_GENERAL_NAME_num(gens); i++) {
  99. gen = sk_GENERAL_NAME_value(gens, i);
  100. ret = i2v_GENERAL_NAME(method, gen, ret);
  101. }
  102. if (!ret)
  103. return sk_CONF_VALUE_new_null();
  104. return ret;
  105. }
  106. STACK_OF(CONF_VALUE) *i2v_GENERAL_NAME(X509V3_EXT_METHOD *method,
  107. GENERAL_NAME *gen,
  108. STACK_OF(CONF_VALUE) *ret)
  109. {
  110. unsigned char *p;
  111. char oline[256], htmp[5];
  112. int i;
  113. switch (gen->type) {
  114. case GEN_OTHERNAME:
  115. if (!X509V3_add_value("othername", "<unsupported>", &ret))
  116. return NULL;
  117. break;
  118. case GEN_X400:
  119. if (!X509V3_add_value("X400Name", "<unsupported>", &ret))
  120. return NULL;
  121. break;
  122. case GEN_EDIPARTY:
  123. if (!X509V3_add_value("EdiPartyName", "<unsupported>", &ret))
  124. return NULL;
  125. break;
  126. case GEN_EMAIL:
  127. if (!X509V3_add_value_uchar("email", gen->d.ia5->data, &ret))
  128. return NULL;
  129. break;
  130. case GEN_DNS:
  131. if (!X509V3_add_value_uchar("DNS", gen->d.ia5->data, &ret))
  132. return NULL;
  133. break;
  134. case GEN_URI:
  135. if (!X509V3_add_value_uchar("URI", gen->d.ia5->data, &ret))
  136. return NULL;
  137. break;
  138. case GEN_DIRNAME:
  139. if (X509_NAME_oneline(gen->d.dirn, oline, 256) == NULL
  140. || !X509V3_add_value("DirName", oline, &ret))
  141. return NULL;
  142. break;
  143. case GEN_IPADD:
  144. p = gen->d.ip->data;
  145. if (gen->d.ip->length == 4)
  146. BIO_snprintf(oline, sizeof(oline),
  147. "%d.%d.%d.%d", p[0], p[1], p[2], p[3]);
  148. else if (gen->d.ip->length == 16) {
  149. oline[0] = 0;
  150. for (i = 0; i < 8; i++) {
  151. BIO_snprintf(htmp, sizeof(htmp), "%X", p[0] << 8 | p[1]);
  152. p += 2;
  153. strcat(oline, htmp);
  154. if (i != 7)
  155. strcat(oline, ":");
  156. }
  157. } else {
  158. if (!X509V3_add_value("IP Address", "<invalid>", &ret))
  159. return NULL;
  160. break;
  161. }
  162. if (!X509V3_add_value("IP Address", oline, &ret))
  163. return NULL;
  164. break;
  165. case GEN_RID:
  166. i2t_ASN1_OBJECT(oline, 256, gen->d.rid);
  167. if (!X509V3_add_value("Registered ID", oline, &ret))
  168. return NULL;
  169. break;
  170. }
  171. return ret;
  172. }
  173. int GENERAL_NAME_print(BIO *out, GENERAL_NAME *gen)
  174. {
  175. unsigned char *p;
  176. int i;
  177. switch (gen->type) {
  178. case GEN_OTHERNAME:
  179. BIO_printf(out, "othername:<unsupported>");
  180. break;
  181. case GEN_X400:
  182. BIO_printf(out, "X400Name:<unsupported>");
  183. break;
  184. case GEN_EDIPARTY:
  185. /* Maybe fix this: it is supported now */
  186. BIO_printf(out, "EdiPartyName:<unsupported>");
  187. break;
  188. case GEN_EMAIL:
  189. BIO_printf(out, "email:");
  190. ASN1_STRING_print(out, gen->d.ia5);
  191. break;
  192. case GEN_DNS:
  193. BIO_printf(out, "DNS:");
  194. ASN1_STRING_print(out, gen->d.ia5);
  195. break;
  196. case GEN_URI:
  197. BIO_printf(out, "URI:");
  198. ASN1_STRING_print(out, gen->d.ia5);
  199. break;
  200. case GEN_DIRNAME:
  201. BIO_printf(out, "DirName: ");
  202. X509_NAME_print_ex(out, gen->d.dirn, 0, XN_FLAG_ONELINE);
  203. break;
  204. case GEN_IPADD:
  205. p = gen->d.ip->data;
  206. if (gen->d.ip->length == 4)
  207. BIO_printf(out, "IP Address:%d.%d.%d.%d", p[0], p[1], p[2], p[3]);
  208. else if (gen->d.ip->length == 16) {
  209. BIO_printf(out, "IP Address");
  210. for (i = 0; i < 8; i++) {
  211. BIO_printf(out, ":%X", p[0] << 8 | p[1]);
  212. p += 2;
  213. }
  214. BIO_puts(out, "\n");
  215. } else {
  216. BIO_printf(out, "IP Address:<invalid>");
  217. break;
  218. }
  219. break;
  220. case GEN_RID:
  221. BIO_printf(out, "Registered ID");
  222. i2a_ASN1_OBJECT(out, gen->d.rid);
  223. break;
  224. }
  225. return 1;
  226. }
  227. static GENERAL_NAMES *v2i_issuer_alt(X509V3_EXT_METHOD *method,
  228. X509V3_CTX *ctx,
  229. STACK_OF(CONF_VALUE) *nval)
  230. {
  231. GENERAL_NAMES *gens = NULL;
  232. CONF_VALUE *cnf;
  233. int i;
  234. if (!(gens = sk_GENERAL_NAME_new_null())) {
  235. X509V3err(X509V3_F_V2I_ISSUER_ALT, ERR_R_MALLOC_FAILURE);
  236. return NULL;
  237. }
  238. for (i = 0; i < sk_CONF_VALUE_num(nval); i++) {
  239. cnf = sk_CONF_VALUE_value(nval, i);
  240. if (!name_cmp(cnf->name, "issuer") && cnf->value &&
  241. !strcmp(cnf->value, "copy")) {
  242. if (!copy_issuer(ctx, gens))
  243. goto err;
  244. } else {
  245. GENERAL_NAME *gen;
  246. if (!(gen = v2i_GENERAL_NAME(method, ctx, cnf)))
  247. goto err;
  248. sk_GENERAL_NAME_push(gens, gen);
  249. }
  250. }
  251. return gens;
  252. err:
  253. sk_GENERAL_NAME_pop_free(gens, GENERAL_NAME_free);
  254. return NULL;
  255. }
  256. /* Append subject altname of issuer to issuer alt name of subject */
  257. static int copy_issuer(X509V3_CTX *ctx, GENERAL_NAMES *gens)
  258. {
  259. GENERAL_NAMES *ialt;
  260. GENERAL_NAME *gen;
  261. X509_EXTENSION *ext;
  262. int i;
  263. if (ctx && (ctx->flags == CTX_TEST))
  264. return 1;
  265. if (!ctx || !ctx->issuer_cert) {
  266. X509V3err(X509V3_F_COPY_ISSUER, X509V3_R_NO_ISSUER_DETAILS);
  267. goto err;
  268. }
  269. i = X509_get_ext_by_NID(ctx->issuer_cert, NID_subject_alt_name, -1);
  270. if (i < 0)
  271. return 1;
  272. if (!(ext = X509_get_ext(ctx->issuer_cert, i)) ||
  273. !(ialt = X509V3_EXT_d2i(ext))) {
  274. X509V3err(X509V3_F_COPY_ISSUER, X509V3_R_ISSUER_DECODE_ERROR);
  275. goto err;
  276. }
  277. for (i = 0; i < sk_GENERAL_NAME_num(ialt); i++) {
  278. gen = sk_GENERAL_NAME_value(ialt, i);
  279. if (!sk_GENERAL_NAME_push(gens, gen)) {
  280. X509V3err(X509V3_F_COPY_ISSUER, ERR_R_MALLOC_FAILURE);
  281. goto err;
  282. }
  283. }
  284. sk_GENERAL_NAME_free(ialt);
  285. return 1;
  286. err:
  287. return 0;
  288. }
  289. static GENERAL_NAMES *v2i_subject_alt(X509V3_EXT_METHOD *method,
  290. X509V3_CTX *ctx,
  291. STACK_OF(CONF_VALUE) *nval)
  292. {
  293. GENERAL_NAMES *gens = NULL;
  294. CONF_VALUE *cnf;
  295. int i;
  296. if (!(gens = sk_GENERAL_NAME_new_null())) {
  297. X509V3err(X509V3_F_V2I_SUBJECT_ALT, ERR_R_MALLOC_FAILURE);
  298. return NULL;
  299. }
  300. for (i = 0; i < sk_CONF_VALUE_num(nval); i++) {
  301. cnf = sk_CONF_VALUE_value(nval, i);
  302. if (!name_cmp(cnf->name, "email") && cnf->value &&
  303. !strcmp(cnf->value, "copy")) {
  304. if (!copy_email(ctx, gens, 0))
  305. goto err;
  306. } else if (!name_cmp(cnf->name, "email") && cnf->value &&
  307. !strcmp(cnf->value, "move")) {
  308. if (!copy_email(ctx, gens, 1))
  309. goto err;
  310. } else {
  311. GENERAL_NAME *gen;
  312. if (!(gen = v2i_GENERAL_NAME(method, ctx, cnf)))
  313. goto err;
  314. sk_GENERAL_NAME_push(gens, gen);
  315. }
  316. }
  317. return gens;
  318. err:
  319. sk_GENERAL_NAME_pop_free(gens, GENERAL_NAME_free);
  320. return NULL;
  321. }
  322. /*
  323. * Copy any email addresses in a certificate or request to GENERAL_NAMES
  324. */
  325. static int copy_email(X509V3_CTX *ctx, GENERAL_NAMES *gens, int move_p)
  326. {
  327. X509_NAME *nm;
  328. ASN1_IA5STRING *email = NULL;
  329. X509_NAME_ENTRY *ne;
  330. GENERAL_NAME *gen = NULL;
  331. int i;
  332. if (ctx != NULL && ctx->flags == CTX_TEST)
  333. return 1;
  334. if (!ctx || (!ctx->subject_cert && !ctx->subject_req)) {
  335. X509V3err(X509V3_F_COPY_EMAIL, X509V3_R_NO_SUBJECT_DETAILS);
  336. goto err;
  337. }
  338. /* Find the subject name */
  339. if (ctx->subject_cert)
  340. nm = X509_get_subject_name(ctx->subject_cert);
  341. else
  342. nm = X509_REQ_get_subject_name(ctx->subject_req);
  343. /* Now add any email address(es) to STACK */
  344. i = -1;
  345. while ((i = X509_NAME_get_index_by_NID(nm,
  346. NID_pkcs9_emailAddress, i)) >= 0) {
  347. ne = X509_NAME_get_entry(nm, i);
  348. email = M_ASN1_IA5STRING_dup(X509_NAME_ENTRY_get_data(ne));
  349. if (move_p) {
  350. X509_NAME_delete_entry(nm, i);
  351. X509_NAME_ENTRY_free(ne);
  352. i--;
  353. }
  354. if (!email || !(gen = GENERAL_NAME_new())) {
  355. X509V3err(X509V3_F_COPY_EMAIL, ERR_R_MALLOC_FAILURE);
  356. goto err;
  357. }
  358. gen->d.ia5 = email;
  359. email = NULL;
  360. gen->type = GEN_EMAIL;
  361. if (!sk_GENERAL_NAME_push(gens, gen)) {
  362. X509V3err(X509V3_F_COPY_EMAIL, ERR_R_MALLOC_FAILURE);
  363. goto err;
  364. }
  365. gen = NULL;
  366. }
  367. return 1;
  368. err:
  369. GENERAL_NAME_free(gen);
  370. M_ASN1_IA5STRING_free(email);
  371. return 0;
  372. }
  373. GENERAL_NAMES *v2i_GENERAL_NAMES(const X509V3_EXT_METHOD *method,
  374. X509V3_CTX *ctx, STACK_OF(CONF_VALUE) *nval)
  375. {
  376. GENERAL_NAME *gen;
  377. GENERAL_NAMES *gens = NULL;
  378. CONF_VALUE *cnf;
  379. int i;
  380. if (!(gens = sk_GENERAL_NAME_new_null())) {
  381. X509V3err(X509V3_F_V2I_GENERAL_NAMES, ERR_R_MALLOC_FAILURE);
  382. return NULL;
  383. }
  384. for (i = 0; i < sk_CONF_VALUE_num(nval); i++) {
  385. cnf = sk_CONF_VALUE_value(nval, i);
  386. if (!(gen = v2i_GENERAL_NAME(method, ctx, cnf)))
  387. goto err;
  388. sk_GENERAL_NAME_push(gens, gen);
  389. }
  390. return gens;
  391. err:
  392. sk_GENERAL_NAME_pop_free(gens, GENERAL_NAME_free);
  393. return NULL;
  394. }
  395. GENERAL_NAME *v2i_GENERAL_NAME(const X509V3_EXT_METHOD *method,
  396. X509V3_CTX *ctx, CONF_VALUE *cnf)
  397. {
  398. return v2i_GENERAL_NAME_ex(NULL, method, ctx, cnf, 0);
  399. }
  400. GENERAL_NAME *a2i_GENERAL_NAME(GENERAL_NAME *out,
  401. const X509V3_EXT_METHOD *method,
  402. X509V3_CTX *ctx, int gen_type, char *value,
  403. int is_nc)
  404. {
  405. char is_string = 0;
  406. GENERAL_NAME *gen = NULL;
  407. if (!value) {
  408. X509V3err(X509V3_F_A2I_GENERAL_NAME, X509V3_R_MISSING_VALUE);
  409. return NULL;
  410. }
  411. if (out)
  412. gen = out;
  413. else {
  414. gen = GENERAL_NAME_new();
  415. if (gen == NULL) {
  416. X509V3err(X509V3_F_A2I_GENERAL_NAME, ERR_R_MALLOC_FAILURE);
  417. return NULL;
  418. }
  419. }
  420. switch (gen_type) {
  421. case GEN_URI:
  422. case GEN_EMAIL:
  423. case GEN_DNS:
  424. is_string = 1;
  425. break;
  426. case GEN_RID:
  427. {
  428. ASN1_OBJECT *obj;
  429. if (!(obj = OBJ_txt2obj(value, 0))) {
  430. X509V3err(X509V3_F_A2I_GENERAL_NAME, X509V3_R_BAD_OBJECT);
  431. ERR_add_error_data(2, "value=", value);
  432. goto err;
  433. }
  434. gen->d.rid = obj;
  435. }
  436. break;
  437. case GEN_IPADD:
  438. if (is_nc)
  439. gen->d.ip = a2i_IPADDRESS_NC(value);
  440. else
  441. gen->d.ip = a2i_IPADDRESS(value);
  442. if (gen->d.ip == NULL) {
  443. X509V3err(X509V3_F_A2I_GENERAL_NAME, X509V3_R_BAD_IP_ADDRESS);
  444. ERR_add_error_data(2, "value=", value);
  445. goto err;
  446. }
  447. break;
  448. case GEN_DIRNAME:
  449. if (!do_dirname(gen, value, ctx)) {
  450. X509V3err(X509V3_F_A2I_GENERAL_NAME, X509V3_R_DIRNAME_ERROR);
  451. goto err;
  452. }
  453. break;
  454. case GEN_OTHERNAME:
  455. if (!do_othername(gen, value, ctx)) {
  456. X509V3err(X509V3_F_A2I_GENERAL_NAME, X509V3_R_OTHERNAME_ERROR);
  457. goto err;
  458. }
  459. break;
  460. default:
  461. X509V3err(X509V3_F_A2I_GENERAL_NAME, X509V3_R_UNSUPPORTED_TYPE);
  462. goto err;
  463. }
  464. if (is_string) {
  465. if (!(gen->d.ia5 = M_ASN1_IA5STRING_new()) ||
  466. !ASN1_STRING_set(gen->d.ia5, (unsigned char *)value,
  467. strlen(value))) {
  468. X509V3err(X509V3_F_A2I_GENERAL_NAME, ERR_R_MALLOC_FAILURE);
  469. goto err;
  470. }
  471. }
  472. gen->type = gen_type;
  473. return gen;
  474. err:
  475. if (!out)
  476. GENERAL_NAME_free(gen);
  477. return NULL;
  478. }
  479. GENERAL_NAME *v2i_GENERAL_NAME_ex(GENERAL_NAME *out,
  480. const X509V3_EXT_METHOD *method,
  481. X509V3_CTX *ctx, CONF_VALUE *cnf, int is_nc)
  482. {
  483. int type;
  484. char *name, *value;
  485. name = cnf->name;
  486. value = cnf->value;
  487. if (!value) {
  488. X509V3err(X509V3_F_V2I_GENERAL_NAME_EX, X509V3_R_MISSING_VALUE);
  489. return NULL;
  490. }
  491. if (!name_cmp(name, "email"))
  492. type = GEN_EMAIL;
  493. else if (!name_cmp(name, "URI"))
  494. type = GEN_URI;
  495. else if (!name_cmp(name, "DNS"))
  496. type = GEN_DNS;
  497. else if (!name_cmp(name, "RID"))
  498. type = GEN_RID;
  499. else if (!name_cmp(name, "IP"))
  500. type = GEN_IPADD;
  501. else if (!name_cmp(name, "dirName"))
  502. type = GEN_DIRNAME;
  503. else if (!name_cmp(name, "otherName"))
  504. type = GEN_OTHERNAME;
  505. else {
  506. X509V3err(X509V3_F_V2I_GENERAL_NAME_EX, X509V3_R_UNSUPPORTED_OPTION);
  507. ERR_add_error_data(2, "name=", name);
  508. return NULL;
  509. }
  510. return a2i_GENERAL_NAME(out, method, ctx, type, value, is_nc);
  511. }
  512. static int do_othername(GENERAL_NAME *gen, char *value, X509V3_CTX *ctx)
  513. {
  514. char *objtmp = NULL, *p;
  515. int objlen;
  516. if (!(p = strchr(value, ';')))
  517. return 0;
  518. if (!(gen->d.otherName = OTHERNAME_new()))
  519. return 0;
  520. /*
  521. * Free this up because we will overwrite it. no need to free type_id
  522. * because it is static
  523. */
  524. ASN1_TYPE_free(gen->d.otherName->value);
  525. if (!(gen->d.otherName->value = ASN1_generate_v3(p + 1, ctx)))
  526. return 0;
  527. objlen = p - value;
  528. objtmp = OPENSSL_malloc(objlen + 1);
  529. if (objtmp == NULL)
  530. return 0;
  531. strncpy(objtmp, value, objlen);
  532. objtmp[objlen] = 0;
  533. gen->d.otherName->type_id = OBJ_txt2obj(objtmp, 0);
  534. OPENSSL_free(objtmp);
  535. if (!gen->d.otherName->type_id)
  536. return 0;
  537. return 1;
  538. }
  539. static int do_dirname(GENERAL_NAME *gen, char *value, X509V3_CTX *ctx)
  540. {
  541. int ret = 0;
  542. STACK_OF(CONF_VALUE) *sk = NULL;
  543. X509_NAME *nm = NULL;
  544. if (!(nm = X509_NAME_new()))
  545. goto err;
  546. sk = X509V3_get_section(ctx, value);
  547. if (!sk) {
  548. X509V3err(X509V3_F_DO_DIRNAME, X509V3_R_SECTION_NOT_FOUND);
  549. ERR_add_error_data(2, "section=", value);
  550. goto err;
  551. }
  552. /* FIXME: should allow other character types... */
  553. ret = X509V3_NAME_from_section(nm, sk, MBSTRING_ASC);
  554. if (!ret)
  555. goto err;
  556. gen->d.dirn = nm;
  557. err:
  558. if (ret == 0)
  559. X509_NAME_free(nm);
  560. X509V3_section_free(ctx, sk);
  561. return ret;
  562. }