v3_san.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699
  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 = NULL;
  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. sk_GENERAL_NAME_free(ialt);
  335. return 0;
  336. }
  337. static GENERAL_NAMES *v2i_subject_alt(X509V3_EXT_METHOD *method,
  338. X509V3_CTX *ctx,
  339. STACK_OF(CONF_VALUE) *nval)
  340. {
  341. GENERAL_NAMES *gens;
  342. CONF_VALUE *cnf;
  343. const int num = sk_CONF_VALUE_num(nval);
  344. int i;
  345. gens = sk_GENERAL_NAME_new_reserve(NULL, num);
  346. if (gens == NULL) {
  347. ERR_raise(ERR_LIB_X509V3, ERR_R_CRYPTO_LIB);
  348. sk_GENERAL_NAME_free(gens);
  349. return NULL;
  350. }
  351. for (i = 0; i < num; i++) {
  352. cnf = sk_CONF_VALUE_value(nval, i);
  353. if (ossl_v3_name_cmp(cnf->name, "email") == 0
  354. && cnf->value && strcmp(cnf->value, "copy") == 0) {
  355. if (!copy_email(ctx, gens, 0))
  356. goto err;
  357. } else if (ossl_v3_name_cmp(cnf->name, "email") == 0
  358. && cnf->value && strcmp(cnf->value, "move") == 0) {
  359. if (!copy_email(ctx, gens, 1))
  360. goto err;
  361. } else {
  362. GENERAL_NAME *gen;
  363. if ((gen = v2i_GENERAL_NAME(method, ctx, cnf)) == NULL)
  364. goto err;
  365. sk_GENERAL_NAME_push(gens, gen); /* no failure as it was reserved */
  366. }
  367. }
  368. return gens;
  369. err:
  370. sk_GENERAL_NAME_pop_free(gens, GENERAL_NAME_free);
  371. return NULL;
  372. }
  373. /*
  374. * Copy any email addresses in a certificate or request to GENERAL_NAMES
  375. */
  376. static int copy_email(X509V3_CTX *ctx, GENERAL_NAMES *gens, int move_p)
  377. {
  378. X509_NAME *nm;
  379. ASN1_IA5STRING *email = NULL;
  380. X509_NAME_ENTRY *ne;
  381. GENERAL_NAME *gen = NULL;
  382. int i = -1;
  383. if (ctx != NULL && (ctx->flags & X509V3_CTX_TEST) != 0)
  384. return 1;
  385. if (ctx == NULL
  386. || (ctx->subject_cert == NULL && ctx->subject_req == NULL)) {
  387. ERR_raise(ERR_LIB_X509V3, X509V3_R_NO_SUBJECT_DETAILS);
  388. return 0;
  389. }
  390. /* Find the subject name */
  391. nm = ctx->subject_cert != NULL ?
  392. X509_get_subject_name(ctx->subject_cert) :
  393. X509_REQ_get_subject_name(ctx->subject_req);
  394. /* Now add any email address(es) to STACK */
  395. while ((i = X509_NAME_get_index_by_NID(nm,
  396. NID_pkcs9_emailAddress, i)) >= 0) {
  397. ne = X509_NAME_get_entry(nm, i);
  398. email = ASN1_STRING_dup(X509_NAME_ENTRY_get_data(ne));
  399. if (move_p) {
  400. X509_NAME_delete_entry(nm, i);
  401. X509_NAME_ENTRY_free(ne);
  402. i--;
  403. }
  404. if (email == NULL || (gen = GENERAL_NAME_new()) == NULL) {
  405. ERR_raise(ERR_LIB_X509V3, ERR_R_ASN1_LIB);
  406. goto err;
  407. }
  408. gen->d.ia5 = email;
  409. email = NULL;
  410. gen->type = GEN_EMAIL;
  411. if (!sk_GENERAL_NAME_push(gens, gen)) {
  412. ERR_raise(ERR_LIB_X509V3, ERR_R_CRYPTO_LIB);
  413. goto err;
  414. }
  415. gen = NULL;
  416. }
  417. return 1;
  418. err:
  419. GENERAL_NAME_free(gen);
  420. ASN1_IA5STRING_free(email);
  421. return 0;
  422. }
  423. GENERAL_NAMES *v2i_GENERAL_NAMES(const X509V3_EXT_METHOD *method,
  424. X509V3_CTX *ctx, STACK_OF(CONF_VALUE) *nval)
  425. {
  426. GENERAL_NAME *gen;
  427. GENERAL_NAMES *gens;
  428. CONF_VALUE *cnf;
  429. const int num = sk_CONF_VALUE_num(nval);
  430. int i;
  431. gens = sk_GENERAL_NAME_new_reserve(NULL, num);
  432. if (gens == NULL) {
  433. ERR_raise(ERR_LIB_X509V3, ERR_R_CRYPTO_LIB);
  434. sk_GENERAL_NAME_free(gens);
  435. return NULL;
  436. }
  437. for (i = 0; i < num; i++) {
  438. cnf = sk_CONF_VALUE_value(nval, i);
  439. if ((gen = v2i_GENERAL_NAME(method, ctx, cnf)) == NULL)
  440. goto err;
  441. sk_GENERAL_NAME_push(gens, gen); /* no failure as it was reserved */
  442. }
  443. return gens;
  444. err:
  445. sk_GENERAL_NAME_pop_free(gens, GENERAL_NAME_free);
  446. return NULL;
  447. }
  448. GENERAL_NAME *v2i_GENERAL_NAME(const X509V3_EXT_METHOD *method,
  449. X509V3_CTX *ctx, CONF_VALUE *cnf)
  450. {
  451. return v2i_GENERAL_NAME_ex(NULL, method, ctx, cnf, 0);
  452. }
  453. GENERAL_NAME *a2i_GENERAL_NAME(GENERAL_NAME *out,
  454. const X509V3_EXT_METHOD *method,
  455. X509V3_CTX *ctx, int gen_type, const char *value,
  456. int is_nc)
  457. {
  458. char is_string = 0;
  459. GENERAL_NAME *gen = NULL;
  460. if (!value) {
  461. ERR_raise(ERR_LIB_X509V3, X509V3_R_MISSING_VALUE);
  462. return NULL;
  463. }
  464. if (out)
  465. gen = out;
  466. else {
  467. gen = GENERAL_NAME_new();
  468. if (gen == NULL) {
  469. ERR_raise(ERR_LIB_X509V3, ERR_R_ASN1_LIB);
  470. return NULL;
  471. }
  472. }
  473. switch (gen_type) {
  474. case GEN_URI:
  475. case GEN_EMAIL:
  476. case GEN_DNS:
  477. is_string = 1;
  478. break;
  479. case GEN_RID:
  480. {
  481. ASN1_OBJECT *obj;
  482. if ((obj = OBJ_txt2obj(value, 0)) == NULL) {
  483. ERR_raise_data(ERR_LIB_X509V3, X509V3_R_BAD_OBJECT,
  484. "value=%s", value);
  485. goto err;
  486. }
  487. gen->d.rid = obj;
  488. }
  489. break;
  490. case GEN_IPADD:
  491. if (is_nc)
  492. gen->d.ip = a2i_IPADDRESS_NC(value);
  493. else
  494. gen->d.ip = a2i_IPADDRESS(value);
  495. if (gen->d.ip == NULL) {
  496. ERR_raise_data(ERR_LIB_X509V3, X509V3_R_BAD_IP_ADDRESS,
  497. "value=%s", value);
  498. goto err;
  499. }
  500. break;
  501. case GEN_DIRNAME:
  502. if (!do_dirname(gen, value, ctx)) {
  503. ERR_raise(ERR_LIB_X509V3, X509V3_R_DIRNAME_ERROR);
  504. goto err;
  505. }
  506. break;
  507. case GEN_OTHERNAME:
  508. if (!do_othername(gen, value, ctx)) {
  509. ERR_raise(ERR_LIB_X509V3, X509V3_R_OTHERNAME_ERROR);
  510. goto err;
  511. }
  512. break;
  513. default:
  514. ERR_raise(ERR_LIB_X509V3, X509V3_R_UNSUPPORTED_TYPE);
  515. goto err;
  516. }
  517. if (is_string) {
  518. if ((gen->d.ia5 = ASN1_IA5STRING_new()) == NULL ||
  519. !ASN1_STRING_set(gen->d.ia5, (unsigned char *)value,
  520. strlen(value))) {
  521. ASN1_IA5STRING_free(gen->d.ia5);
  522. gen->d.ia5 = NULL;
  523. ERR_raise(ERR_LIB_X509V3, ERR_R_ASN1_LIB);
  524. goto err;
  525. }
  526. }
  527. gen->type = gen_type;
  528. return gen;
  529. err:
  530. if (!out)
  531. GENERAL_NAME_free(gen);
  532. return NULL;
  533. }
  534. GENERAL_NAME *v2i_GENERAL_NAME_ex(GENERAL_NAME *out,
  535. const X509V3_EXT_METHOD *method,
  536. X509V3_CTX *ctx, CONF_VALUE *cnf, int is_nc)
  537. {
  538. int type;
  539. char *name, *value;
  540. name = cnf->name;
  541. value = cnf->value;
  542. if (!value) {
  543. ERR_raise(ERR_LIB_X509V3, X509V3_R_MISSING_VALUE);
  544. return NULL;
  545. }
  546. if (!ossl_v3_name_cmp(name, "email"))
  547. type = GEN_EMAIL;
  548. else if (!ossl_v3_name_cmp(name, "URI"))
  549. type = GEN_URI;
  550. else if (!ossl_v3_name_cmp(name, "DNS"))
  551. type = GEN_DNS;
  552. else if (!ossl_v3_name_cmp(name, "RID"))
  553. type = GEN_RID;
  554. else if (!ossl_v3_name_cmp(name, "IP"))
  555. type = GEN_IPADD;
  556. else if (!ossl_v3_name_cmp(name, "dirName"))
  557. type = GEN_DIRNAME;
  558. else if (!ossl_v3_name_cmp(name, "otherName"))
  559. type = GEN_OTHERNAME;
  560. else {
  561. ERR_raise_data(ERR_LIB_X509V3, X509V3_R_UNSUPPORTED_OPTION,
  562. "name=%s", name);
  563. return NULL;
  564. }
  565. return a2i_GENERAL_NAME(out, method, ctx, type, value, is_nc);
  566. }
  567. static int do_othername(GENERAL_NAME *gen, const char *value, X509V3_CTX *ctx)
  568. {
  569. char *objtmp = NULL, *p;
  570. int objlen;
  571. if ((p = strchr(value, ';')) == NULL)
  572. return 0;
  573. if ((gen->d.otherName = OTHERNAME_new()) == NULL)
  574. return 0;
  575. /*
  576. * Free this up because we will overwrite it. no need to free type_id
  577. * because it is static
  578. */
  579. ASN1_TYPE_free(gen->d.otherName->value);
  580. if ((gen->d.otherName->value = ASN1_generate_v3(p + 1, ctx)) == NULL)
  581. goto err;
  582. objlen = p - value;
  583. objtmp = OPENSSL_strndup(value, objlen);
  584. if (objtmp == NULL)
  585. goto err;
  586. gen->d.otherName->type_id = OBJ_txt2obj(objtmp, 0);
  587. OPENSSL_free(objtmp);
  588. if (!gen->d.otherName->type_id)
  589. goto err;
  590. return 1;
  591. err:
  592. OTHERNAME_free(gen->d.otherName);
  593. gen->d.otherName = NULL;
  594. return 0;
  595. }
  596. static int do_dirname(GENERAL_NAME *gen, const char *value, X509V3_CTX *ctx)
  597. {
  598. int ret = 0;
  599. STACK_OF(CONF_VALUE) *sk = NULL;
  600. X509_NAME *nm;
  601. if ((nm = X509_NAME_new()) == NULL)
  602. goto err;
  603. sk = X509V3_get_section(ctx, value);
  604. if (!sk) {
  605. ERR_raise_data(ERR_LIB_X509V3, X509V3_R_SECTION_NOT_FOUND,
  606. "section=%s", value);
  607. goto err;
  608. }
  609. /* FIXME: should allow other character types... */
  610. ret = X509V3_NAME_from_section(nm, sk, MBSTRING_ASC);
  611. if (!ret)
  612. goto err;
  613. gen->d.dirn = nm;
  614. err:
  615. if (ret == 0)
  616. X509_NAME_free(nm);
  617. X509V3_section_free(ctx, sk);
  618. return ret;
  619. }