v3_utl.c 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313
  1. /* v3_utl.c */
  2. /*
  3. * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
  4. * project.
  5. */
  6. /* ====================================================================
  7. * Copyright (c) 1999-2003 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. * licensing@OpenSSL.org.
  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. * (eay@cryptsoft.com). This product includes software written by Tim
  56. * Hudson (tjh@cryptsoft.com).
  57. *
  58. */
  59. /* X509 v3 extension utilities */
  60. #include <ctype.h>
  61. #include <stdio.h>
  62. #include <string.h>
  63. #include <openssl/bn.h>
  64. #include <openssl/buf.h>
  65. #include <openssl/conf.h>
  66. #include <openssl/err.h>
  67. #include <openssl/mem.h>
  68. #include <openssl/obj.h>
  69. #include <openssl/x509v3.h>
  70. #include "../conf/internal.h"
  71. #include "../internal.h"
  72. static char *strip_spaces(char *name);
  73. static int sk_strcmp(const OPENSSL_STRING *a, const OPENSSL_STRING *b);
  74. static STACK_OF(OPENSSL_STRING) *get_email(X509_NAME *name,
  75. GENERAL_NAMES *gens);
  76. static void str_free(OPENSSL_STRING str);
  77. static int append_ia5(STACK_OF(OPENSSL_STRING) **sk, ASN1_IA5STRING *email);
  78. static int ipv4_from_asc(unsigned char *v4, const char *in);
  79. static int ipv6_from_asc(unsigned char *v6, const char *in);
  80. static int ipv6_cb(const char *elem, int len, void *usr);
  81. static int ipv6_hex(unsigned char *out, const char *in, int inlen);
  82. /* Add a CONF_VALUE name value pair to stack */
  83. int X509V3_add_value(const char *name, const char *value,
  84. STACK_OF(CONF_VALUE) **extlist)
  85. {
  86. CONF_VALUE *vtmp = NULL;
  87. char *tname = NULL, *tvalue = NULL;
  88. if (name && !(tname = BUF_strdup(name)))
  89. goto err;
  90. if (value && !(tvalue = BUF_strdup(value)))
  91. goto err;
  92. if (!(vtmp = CONF_VALUE_new()))
  93. goto err;
  94. if (!*extlist && !(*extlist = sk_CONF_VALUE_new_null()))
  95. goto err;
  96. vtmp->section = NULL;
  97. vtmp->name = tname;
  98. vtmp->value = tvalue;
  99. if (!sk_CONF_VALUE_push(*extlist, vtmp))
  100. goto err;
  101. return 1;
  102. err:
  103. OPENSSL_PUT_ERROR(X509V3, ERR_R_MALLOC_FAILURE);
  104. if (vtmp)
  105. OPENSSL_free(vtmp);
  106. if (tname)
  107. OPENSSL_free(tname);
  108. if (tvalue)
  109. OPENSSL_free(tvalue);
  110. return 0;
  111. }
  112. int X509V3_add_value_uchar(const char *name, const unsigned char *value,
  113. STACK_OF(CONF_VALUE) **extlist)
  114. {
  115. return X509V3_add_value(name, (const char *)value, extlist);
  116. }
  117. /* Free function for STACK_OF(CONF_VALUE) */
  118. void X509V3_conf_free(CONF_VALUE *conf)
  119. {
  120. if (!conf)
  121. return;
  122. if (conf->name)
  123. OPENSSL_free(conf->name);
  124. if (conf->value)
  125. OPENSSL_free(conf->value);
  126. if (conf->section)
  127. OPENSSL_free(conf->section);
  128. OPENSSL_free(conf);
  129. }
  130. int X509V3_add_value_bool(const char *name, int asn1_bool,
  131. STACK_OF(CONF_VALUE) **extlist)
  132. {
  133. if (asn1_bool)
  134. return X509V3_add_value(name, "TRUE", extlist);
  135. return X509V3_add_value(name, "FALSE", extlist);
  136. }
  137. int X509V3_add_value_bool_nf(char *name, int asn1_bool,
  138. STACK_OF(CONF_VALUE) **extlist)
  139. {
  140. if (asn1_bool)
  141. return X509V3_add_value(name, "TRUE", extlist);
  142. return 1;
  143. }
  144. char *i2s_ASN1_ENUMERATED(X509V3_EXT_METHOD *method, ASN1_ENUMERATED *a)
  145. {
  146. BIGNUM *bntmp = NULL;
  147. char *strtmp = NULL;
  148. if (!a)
  149. return NULL;
  150. if (!(bntmp = ASN1_ENUMERATED_to_BN(a, NULL)) ||
  151. !(strtmp = BN_bn2dec(bntmp)))
  152. OPENSSL_PUT_ERROR(X509V3, ERR_R_MALLOC_FAILURE);
  153. BN_free(bntmp);
  154. return strtmp;
  155. }
  156. char *i2s_ASN1_INTEGER(X509V3_EXT_METHOD *method, ASN1_INTEGER *a)
  157. {
  158. BIGNUM *bntmp = NULL;
  159. char *strtmp = NULL;
  160. if (!a)
  161. return NULL;
  162. if (!(bntmp = ASN1_INTEGER_to_BN(a, NULL)) ||
  163. !(strtmp = BN_bn2dec(bntmp)))
  164. OPENSSL_PUT_ERROR(X509V3, ERR_R_MALLOC_FAILURE);
  165. BN_free(bntmp);
  166. return strtmp;
  167. }
  168. ASN1_INTEGER *s2i_ASN1_INTEGER(X509V3_EXT_METHOD *method, char *value)
  169. {
  170. BIGNUM *bn = NULL;
  171. ASN1_INTEGER *aint;
  172. int isneg, ishex;
  173. int ret;
  174. if (!value) {
  175. OPENSSL_PUT_ERROR(X509V3, X509V3_R_INVALID_NULL_VALUE);
  176. return 0;
  177. }
  178. bn = BN_new();
  179. if (value[0] == '-') {
  180. value++;
  181. isneg = 1;
  182. } else
  183. isneg = 0;
  184. if (value[0] == '0' && ((value[1] == 'x') || (value[1] == 'X'))) {
  185. value += 2;
  186. ishex = 1;
  187. } else
  188. ishex = 0;
  189. if (ishex)
  190. ret = BN_hex2bn(&bn, value);
  191. else
  192. ret = BN_dec2bn(&bn, value);
  193. if (!ret || value[ret]) {
  194. BN_free(bn);
  195. OPENSSL_PUT_ERROR(X509V3, X509V3_R_BN_DEC2BN_ERROR);
  196. return 0;
  197. }
  198. if (isneg && BN_is_zero(bn))
  199. isneg = 0;
  200. aint = BN_to_ASN1_INTEGER(bn, NULL);
  201. BN_free(bn);
  202. if (!aint) {
  203. OPENSSL_PUT_ERROR(X509V3, X509V3_R_BN_TO_ASN1_INTEGER_ERROR);
  204. return 0;
  205. }
  206. if (isneg)
  207. aint->type |= V_ASN1_NEG;
  208. return aint;
  209. }
  210. int X509V3_add_value_int(const char *name, ASN1_INTEGER *aint,
  211. STACK_OF(CONF_VALUE) **extlist)
  212. {
  213. char *strtmp;
  214. int ret;
  215. if (!aint)
  216. return 1;
  217. if (!(strtmp = i2s_ASN1_INTEGER(NULL, aint)))
  218. return 0;
  219. ret = X509V3_add_value(name, strtmp, extlist);
  220. OPENSSL_free(strtmp);
  221. return ret;
  222. }
  223. int X509V3_get_value_bool(CONF_VALUE *value, int *asn1_bool)
  224. {
  225. char *btmp;
  226. if (!(btmp = value->value))
  227. goto err;
  228. if (!strcmp(btmp, "TRUE") || !strcmp(btmp, "true")
  229. || !strcmp(btmp, "Y") || !strcmp(btmp, "y")
  230. || !strcmp(btmp, "YES") || !strcmp(btmp, "yes")) {
  231. *asn1_bool = 0xff;
  232. return 1;
  233. } else if (!strcmp(btmp, "FALSE") || !strcmp(btmp, "false")
  234. || !strcmp(btmp, "N") || !strcmp(btmp, "n")
  235. || !strcmp(btmp, "NO") || !strcmp(btmp, "no")) {
  236. *asn1_bool = 0;
  237. return 1;
  238. }
  239. err:
  240. OPENSSL_PUT_ERROR(X509V3, X509V3_R_INVALID_BOOLEAN_STRING);
  241. X509V3_conf_err(value);
  242. return 0;
  243. }
  244. int X509V3_get_value_int(CONF_VALUE *value, ASN1_INTEGER **aint)
  245. {
  246. ASN1_INTEGER *itmp;
  247. if (!(itmp = s2i_ASN1_INTEGER(NULL, value->value))) {
  248. X509V3_conf_err(value);
  249. return 0;
  250. }
  251. *aint = itmp;
  252. return 1;
  253. }
  254. #define HDR_NAME 1
  255. #define HDR_VALUE 2
  256. /*
  257. * #define DEBUG
  258. */
  259. STACK_OF(CONF_VALUE) *X509V3_parse_list(const char *line)
  260. {
  261. char *p, *q, c;
  262. char *ntmp, *vtmp;
  263. STACK_OF(CONF_VALUE) *values = NULL;
  264. char *linebuf;
  265. int state;
  266. /* We are going to modify the line so copy it first */
  267. linebuf = BUF_strdup(line);
  268. if (linebuf == NULL) {
  269. OPENSSL_PUT_ERROR(X509V3, ERR_R_MALLOC_FAILURE);
  270. goto err;
  271. }
  272. state = HDR_NAME;
  273. ntmp = NULL;
  274. /* Go through all characters */
  275. for (p = linebuf, q = linebuf; (c = *p) && (c != '\r') && (c != '\n');
  276. p++) {
  277. switch (state) {
  278. case HDR_NAME:
  279. if (c == ':') {
  280. state = HDR_VALUE;
  281. *p = 0;
  282. ntmp = strip_spaces(q);
  283. if (!ntmp) {
  284. OPENSSL_PUT_ERROR(X509V3, X509V3_R_INVALID_NULL_NAME);
  285. goto err;
  286. }
  287. q = p + 1;
  288. } else if (c == ',') {
  289. *p = 0;
  290. ntmp = strip_spaces(q);
  291. q = p + 1;
  292. #if 0
  293. printf("%s\n", ntmp);
  294. #endif
  295. if (!ntmp) {
  296. OPENSSL_PUT_ERROR(X509V3, X509V3_R_INVALID_NULL_NAME);
  297. goto err;
  298. }
  299. X509V3_add_value(ntmp, NULL, &values);
  300. }
  301. break;
  302. case HDR_VALUE:
  303. if (c == ',') {
  304. state = HDR_NAME;
  305. *p = 0;
  306. vtmp = strip_spaces(q);
  307. #if 0
  308. printf("%s\n", ntmp);
  309. #endif
  310. if (!vtmp) {
  311. OPENSSL_PUT_ERROR(X509V3, X509V3_R_INVALID_NULL_VALUE);
  312. goto err;
  313. }
  314. X509V3_add_value(ntmp, vtmp, &values);
  315. ntmp = NULL;
  316. q = p + 1;
  317. }
  318. }
  319. }
  320. if (state == HDR_VALUE) {
  321. vtmp = strip_spaces(q);
  322. #if 0
  323. printf("%s=%s\n", ntmp, vtmp);
  324. #endif
  325. if (!vtmp) {
  326. OPENSSL_PUT_ERROR(X509V3, X509V3_R_INVALID_NULL_VALUE);
  327. goto err;
  328. }
  329. X509V3_add_value(ntmp, vtmp, &values);
  330. } else {
  331. ntmp = strip_spaces(q);
  332. #if 0
  333. printf("%s\n", ntmp);
  334. #endif
  335. if (!ntmp) {
  336. OPENSSL_PUT_ERROR(X509V3, X509V3_R_INVALID_NULL_NAME);
  337. goto err;
  338. }
  339. X509V3_add_value(ntmp, NULL, &values);
  340. }
  341. OPENSSL_free(linebuf);
  342. return values;
  343. err:
  344. OPENSSL_free(linebuf);
  345. sk_CONF_VALUE_pop_free(values, X509V3_conf_free);
  346. return NULL;
  347. }
  348. /* Delete leading and trailing spaces from a string */
  349. static char *strip_spaces(char *name)
  350. {
  351. char *p, *q;
  352. /* Skip over leading spaces */
  353. p = name;
  354. while (*p && isspace((unsigned char)*p))
  355. p++;
  356. if (!*p)
  357. return NULL;
  358. q = p + strlen(p) - 1;
  359. while ((q != p) && isspace((unsigned char)*q))
  360. q--;
  361. if (p != q)
  362. q[1] = 0;
  363. if (!*p)
  364. return NULL;
  365. return p;
  366. }
  367. /* hex string utilities */
  368. /*
  369. * Given a buffer of length 'len' return a OPENSSL_malloc'ed string with its
  370. * hex representation @@@ (Contents of buffer are always kept in ASCII, also
  371. * on EBCDIC machines)
  372. */
  373. char *hex_to_string(const unsigned char *buffer, long len)
  374. {
  375. char *tmp, *q;
  376. const unsigned char *p;
  377. int i;
  378. static const char hexdig[] = "0123456789ABCDEF";
  379. if (!buffer || !len)
  380. return NULL;
  381. if (!(tmp = OPENSSL_malloc(len * 3 + 1))) {
  382. OPENSSL_PUT_ERROR(X509V3, ERR_R_MALLOC_FAILURE);
  383. return NULL;
  384. }
  385. q = tmp;
  386. for (i = 0, p = buffer; i < len; i++, p++) {
  387. *q++ = hexdig[(*p >> 4) & 0xf];
  388. *q++ = hexdig[*p & 0xf];
  389. *q++ = ':';
  390. }
  391. q[-1] = 0;
  392. return tmp;
  393. }
  394. /*
  395. * Give a string of hex digits convert to a buffer
  396. */
  397. unsigned char *string_to_hex(const char *str, long *len)
  398. {
  399. unsigned char *hexbuf, *q;
  400. unsigned char ch, cl, *p;
  401. if (!str) {
  402. OPENSSL_PUT_ERROR(X509V3, X509V3_R_INVALID_NULL_ARGUMENT);
  403. return NULL;
  404. }
  405. if (!(hexbuf = OPENSSL_malloc(strlen(str) >> 1)))
  406. goto err;
  407. for (p = (unsigned char *)str, q = hexbuf; *p;) {
  408. ch = *p++;
  409. if (ch == ':')
  410. continue;
  411. cl = *p++;
  412. if (!cl) {
  413. OPENSSL_PUT_ERROR(X509V3, X509V3_R_ODD_NUMBER_OF_DIGITS);
  414. OPENSSL_free(hexbuf);
  415. return NULL;
  416. }
  417. if (isupper(ch))
  418. ch = tolower(ch);
  419. if (isupper(cl))
  420. cl = tolower(cl);
  421. if ((ch >= '0') && (ch <= '9'))
  422. ch -= '0';
  423. else if ((ch >= 'a') && (ch <= 'f'))
  424. ch -= 'a' - 10;
  425. else
  426. goto badhex;
  427. if ((cl >= '0') && (cl <= '9'))
  428. cl -= '0';
  429. else if ((cl >= 'a') && (cl <= 'f'))
  430. cl -= 'a' - 10;
  431. else
  432. goto badhex;
  433. *q++ = (ch << 4) | cl;
  434. }
  435. if (len)
  436. *len = q - hexbuf;
  437. return hexbuf;
  438. err:
  439. if (hexbuf)
  440. OPENSSL_free(hexbuf);
  441. OPENSSL_PUT_ERROR(X509V3, ERR_R_MALLOC_FAILURE);
  442. return NULL;
  443. badhex:
  444. OPENSSL_free(hexbuf);
  445. OPENSSL_PUT_ERROR(X509V3, X509V3_R_ILLEGAL_HEX_DIGIT);
  446. return NULL;
  447. }
  448. /*
  449. * V2I name comparison function: returns zero if 'name' matches cmp or cmp.*
  450. */
  451. int name_cmp(const char *name, const char *cmp)
  452. {
  453. int len, ret;
  454. char c;
  455. len = strlen(cmp);
  456. if ((ret = strncmp(name, cmp, len)))
  457. return ret;
  458. c = name[len];
  459. if (!c || (c == '.'))
  460. return 0;
  461. return 1;
  462. }
  463. static int sk_strcmp(const OPENSSL_STRING *a, const OPENSSL_STRING *b)
  464. {
  465. return strcmp(*a, *b);
  466. }
  467. STACK_OF(OPENSSL_STRING) *X509_get1_email(X509 *x)
  468. {
  469. GENERAL_NAMES *gens;
  470. STACK_OF(OPENSSL_STRING) *ret;
  471. gens = X509_get_ext_d2i(x, NID_subject_alt_name, NULL, NULL);
  472. ret = get_email(X509_get_subject_name(x), gens);
  473. sk_GENERAL_NAME_pop_free(gens, GENERAL_NAME_free);
  474. return ret;
  475. }
  476. STACK_OF(OPENSSL_STRING) *X509_get1_ocsp(X509 *x)
  477. {
  478. AUTHORITY_INFO_ACCESS *info;
  479. STACK_OF(OPENSSL_STRING) *ret = NULL;
  480. size_t i;
  481. info = X509_get_ext_d2i(x, NID_info_access, NULL, NULL);
  482. if (!info)
  483. return NULL;
  484. for (i = 0; i < sk_ACCESS_DESCRIPTION_num(info); i++) {
  485. ACCESS_DESCRIPTION *ad = sk_ACCESS_DESCRIPTION_value(info, i);
  486. if (OBJ_obj2nid(ad->method) == NID_ad_OCSP) {
  487. if (ad->location->type == GEN_URI) {
  488. if (!append_ia5
  489. (&ret, ad->location->d.uniformResourceIdentifier))
  490. break;
  491. }
  492. }
  493. }
  494. AUTHORITY_INFO_ACCESS_free(info);
  495. return ret;
  496. }
  497. STACK_OF(OPENSSL_STRING) *X509_REQ_get1_email(X509_REQ *x)
  498. {
  499. GENERAL_NAMES *gens;
  500. STACK_OF(X509_EXTENSION) *exts;
  501. STACK_OF(OPENSSL_STRING) *ret;
  502. exts = X509_REQ_get_extensions(x);
  503. gens = X509V3_get_d2i(exts, NID_subject_alt_name, NULL, NULL);
  504. ret = get_email(X509_REQ_get_subject_name(x), gens);
  505. sk_GENERAL_NAME_pop_free(gens, GENERAL_NAME_free);
  506. sk_X509_EXTENSION_pop_free(exts, X509_EXTENSION_free);
  507. return ret;
  508. }
  509. static STACK_OF(OPENSSL_STRING) *get_email(X509_NAME *name,
  510. GENERAL_NAMES *gens)
  511. {
  512. STACK_OF(OPENSSL_STRING) *ret = NULL;
  513. X509_NAME_ENTRY *ne;
  514. ASN1_IA5STRING *email;
  515. GENERAL_NAME *gen;
  516. int i;
  517. size_t j;
  518. /* Now add any email address(es) to STACK */
  519. i = -1;
  520. /* First supplied X509_NAME */
  521. while ((i = X509_NAME_get_index_by_NID(name,
  522. NID_pkcs9_emailAddress, i)) >= 0) {
  523. ne = X509_NAME_get_entry(name, i);
  524. email = X509_NAME_ENTRY_get_data(ne);
  525. if (!append_ia5(&ret, email))
  526. return NULL;
  527. }
  528. for (j = 0; j < sk_GENERAL_NAME_num(gens); j++) {
  529. gen = sk_GENERAL_NAME_value(gens, j);
  530. if (gen->type != GEN_EMAIL)
  531. continue;
  532. if (!append_ia5(&ret, gen->d.ia5))
  533. return NULL;
  534. }
  535. return ret;
  536. }
  537. static void str_free(OPENSSL_STRING str)
  538. {
  539. OPENSSL_free(str);
  540. }
  541. static int append_ia5(STACK_OF(OPENSSL_STRING) **sk, ASN1_IA5STRING *email)
  542. {
  543. char *emtmp;
  544. /* First some sanity checks */
  545. if (email->type != V_ASN1_IA5STRING)
  546. return 1;
  547. if (!email->data || !email->length)
  548. return 1;
  549. if (!*sk)
  550. *sk = sk_OPENSSL_STRING_new(sk_strcmp);
  551. if (!*sk)
  552. return 0;
  553. /* Don't add duplicates */
  554. if (sk_OPENSSL_STRING_find(*sk, NULL, (char *)email->data))
  555. return 1;
  556. emtmp = BUF_strdup((char *)email->data);
  557. if (!emtmp || !sk_OPENSSL_STRING_push(*sk, emtmp)) {
  558. X509_email_free(*sk);
  559. *sk = NULL;
  560. return 0;
  561. }
  562. return 1;
  563. }
  564. void X509_email_free(STACK_OF(OPENSSL_STRING) *sk)
  565. {
  566. sk_OPENSSL_STRING_pop_free(sk, str_free);
  567. }
  568. typedef int (*equal_fn) (const unsigned char *pattern, size_t pattern_len,
  569. const unsigned char *subject, size_t subject_len,
  570. unsigned int flags);
  571. /* Skip pattern prefix to match "wildcard" subject */
  572. static void skip_prefix(const unsigned char **p, size_t *plen,
  573. const unsigned char *subject, size_t subject_len,
  574. unsigned int flags)
  575. {
  576. const unsigned char *pattern = *p;
  577. size_t pattern_len = *plen;
  578. /*
  579. * If subject starts with a leading '.' followed by more octets, and
  580. * pattern is longer, compare just an equal-length suffix with the
  581. * full subject (starting at the '.'), provided the prefix contains
  582. * no NULs.
  583. */
  584. if ((flags & _X509_CHECK_FLAG_DOT_SUBDOMAINS) == 0)
  585. return;
  586. while (pattern_len > subject_len && *pattern) {
  587. if ((flags & X509_CHECK_FLAG_SINGLE_LABEL_SUBDOMAINS) &&
  588. *pattern == '.')
  589. break;
  590. ++pattern;
  591. --pattern_len;
  592. }
  593. /* Skip if entire prefix acceptable */
  594. if (pattern_len == subject_len) {
  595. *p = pattern;
  596. *plen = pattern_len;
  597. }
  598. }
  599. /* Compare while ASCII ignoring case. */
  600. static int equal_nocase(const unsigned char *pattern, size_t pattern_len,
  601. const unsigned char *subject, size_t subject_len,
  602. unsigned int flags)
  603. {
  604. skip_prefix(&pattern, &pattern_len, subject, subject_len, flags);
  605. if (pattern_len != subject_len)
  606. return 0;
  607. while (pattern_len) {
  608. unsigned char l = *pattern;
  609. unsigned char r = *subject;
  610. /* The pattern must not contain NUL characters. */
  611. if (l == 0)
  612. return 0;
  613. if (l != r) {
  614. if ('A' <= l && l <= 'Z')
  615. l = (l - 'A') + 'a';
  616. if ('A' <= r && r <= 'Z')
  617. r = (r - 'A') + 'a';
  618. if (l != r)
  619. return 0;
  620. }
  621. ++pattern;
  622. ++subject;
  623. --pattern_len;
  624. }
  625. return 1;
  626. }
  627. /* Compare using OPENSSL_memcmp. */
  628. static int equal_case(const unsigned char *pattern, size_t pattern_len,
  629. const unsigned char *subject, size_t subject_len,
  630. unsigned int flags)
  631. {
  632. skip_prefix(&pattern, &pattern_len, subject, subject_len, flags);
  633. if (pattern_len != subject_len)
  634. return 0;
  635. return !OPENSSL_memcmp(pattern, subject, pattern_len);
  636. }
  637. /*
  638. * RFC 5280, section 7.5, requires that only the domain is compared in a
  639. * case-insensitive manner.
  640. */
  641. static int equal_email(const unsigned char *a, size_t a_len,
  642. const unsigned char *b, size_t b_len,
  643. unsigned int unused_flags)
  644. {
  645. size_t i = a_len;
  646. if (a_len != b_len)
  647. return 0;
  648. /*
  649. * We search backwards for the '@' character, so that we do not have to
  650. * deal with quoted local-parts. The domain part is compared in a
  651. * case-insensitive manner.
  652. */
  653. while (i > 0) {
  654. --i;
  655. if (a[i] == '@' || b[i] == '@') {
  656. if (!equal_nocase(a + i, a_len - i, b + i, a_len - i, 0))
  657. return 0;
  658. break;
  659. }
  660. }
  661. if (i == 0)
  662. i = a_len;
  663. return equal_case(a, i, b, i, 0);
  664. }
  665. /*
  666. * Compare the prefix and suffix with the subject, and check that the
  667. * characters in-between are valid.
  668. */
  669. static int wildcard_match(const unsigned char *prefix, size_t prefix_len,
  670. const unsigned char *suffix, size_t suffix_len,
  671. const unsigned char *subject, size_t subject_len,
  672. unsigned int flags)
  673. {
  674. const unsigned char *wildcard_start;
  675. const unsigned char *wildcard_end;
  676. const unsigned char *p;
  677. int allow_multi = 0;
  678. int allow_idna = 0;
  679. if (subject_len < prefix_len + suffix_len)
  680. return 0;
  681. if (!equal_nocase(prefix, prefix_len, subject, prefix_len, flags))
  682. return 0;
  683. wildcard_start = subject + prefix_len;
  684. wildcard_end = subject + (subject_len - suffix_len);
  685. if (!equal_nocase(wildcard_end, suffix_len, suffix, suffix_len, flags))
  686. return 0;
  687. /*
  688. * If the wildcard makes up the entire first label, it must match at
  689. * least one character.
  690. */
  691. if (prefix_len == 0 && *suffix == '.') {
  692. if (wildcard_start == wildcard_end)
  693. return 0;
  694. allow_idna = 1;
  695. if (flags & X509_CHECK_FLAG_MULTI_LABEL_WILDCARDS)
  696. allow_multi = 1;
  697. }
  698. /* IDNA labels cannot match partial wildcards */
  699. if (!allow_idna &&
  700. subject_len >= 4
  701. && OPENSSL_strncasecmp((char *)subject, "xn--", 4) == 0)
  702. return 0;
  703. /* The wildcard may match a literal '*' */
  704. if (wildcard_end == wildcard_start + 1 && *wildcard_start == '*')
  705. return 1;
  706. /*
  707. * Check that the part matched by the wildcard contains only
  708. * permitted characters and only matches a single label unless
  709. * allow_multi is set.
  710. */
  711. for (p = wildcard_start; p != wildcard_end; ++p)
  712. if (!(('0' <= *p && *p <= '9') ||
  713. ('A' <= *p && *p <= 'Z') ||
  714. ('a' <= *p && *p <= 'z') ||
  715. *p == '-' || (allow_multi && *p == '.')))
  716. return 0;
  717. return 1;
  718. }
  719. #define LABEL_START (1 << 0)
  720. #define LABEL_END (1 << 1)
  721. #define LABEL_HYPHEN (1 << 2)
  722. #define LABEL_IDNA (1 << 3)
  723. static const unsigned char *valid_star(const unsigned char *p, size_t len,
  724. unsigned int flags)
  725. {
  726. const unsigned char *star = 0;
  727. size_t i;
  728. int state = LABEL_START;
  729. int dots = 0;
  730. for (i = 0; i < len; ++i) {
  731. /*
  732. * Locate first and only legal wildcard, either at the start
  733. * or end of a non-IDNA first and not final label.
  734. */
  735. if (p[i] == '*') {
  736. int atstart = (state & LABEL_START);
  737. int atend = (i == len - 1 || p[i + 1] == '.');
  738. /*
  739. * At most one wildcard per pattern.
  740. * No wildcards in IDNA labels.
  741. * No wildcards after the first label.
  742. */
  743. if (star != NULL || (state & LABEL_IDNA) != 0 || dots)
  744. return NULL;
  745. /* Only full-label '*.example.com' wildcards? */
  746. if ((flags & X509_CHECK_FLAG_NO_PARTIAL_WILDCARDS)
  747. && (!atstart || !atend))
  748. return NULL;
  749. /* No 'foo*bar' wildcards */
  750. if (!atstart && !atend)
  751. return NULL;
  752. star = &p[i];
  753. state &= ~LABEL_START;
  754. } else if (('a' <= p[i] && p[i] <= 'z')
  755. || ('A' <= p[i] && p[i] <= 'Z')
  756. || ('0' <= p[i] && p[i] <= '9')) {
  757. if ((state & LABEL_START) != 0
  758. && len - i >= 4
  759. && OPENSSL_strncasecmp((char *)&p[i], "xn--", 4) == 0)
  760. state |= LABEL_IDNA;
  761. state &= ~(LABEL_HYPHEN | LABEL_START);
  762. } else if (p[i] == '.') {
  763. if ((state & (LABEL_HYPHEN | LABEL_START)) != 0)
  764. return NULL;
  765. state = LABEL_START;
  766. ++dots;
  767. } else if (p[i] == '-') {
  768. /* no domain/subdomain starts with '-' */
  769. if ((state & LABEL_START) != 0)
  770. return NULL;
  771. state |= LABEL_HYPHEN;
  772. } else
  773. return NULL;
  774. }
  775. /*
  776. * The final label must not end in a hyphen or ".", and
  777. * there must be at least two dots after the star.
  778. */
  779. if ((state & (LABEL_START | LABEL_HYPHEN)) != 0 || dots < 2)
  780. return NULL;
  781. return star;
  782. }
  783. /* Compare using wildcards. */
  784. static int equal_wildcard(const unsigned char *pattern, size_t pattern_len,
  785. const unsigned char *subject, size_t subject_len,
  786. unsigned int flags)
  787. {
  788. const unsigned char *star = NULL;
  789. /*
  790. * Subject names starting with '.' can only match a wildcard pattern
  791. * via a subject sub-domain pattern suffix match.
  792. */
  793. if (!(subject_len > 1 && subject[0] == '.'))
  794. star = valid_star(pattern, pattern_len, flags);
  795. if (star == NULL)
  796. return equal_nocase(pattern, pattern_len,
  797. subject, subject_len, flags);
  798. return wildcard_match(pattern, star - pattern,
  799. star + 1, (pattern + pattern_len) - star - 1,
  800. subject, subject_len, flags);
  801. }
  802. /*
  803. * Compare an ASN1_STRING to a supplied string. If they match return 1. If
  804. * cmp_type > 0 only compare if string matches the type, otherwise convert it
  805. * to UTF8.
  806. */
  807. static int do_check_string(ASN1_STRING *a, int cmp_type, equal_fn equal,
  808. unsigned int flags, const char *b, size_t blen,
  809. char **peername)
  810. {
  811. int rv = 0;
  812. if (!a->data || !a->length)
  813. return 0;
  814. if (cmp_type > 0) {
  815. if (cmp_type != a->type)
  816. return 0;
  817. if (cmp_type == V_ASN1_IA5STRING)
  818. rv = equal(a->data, a->length, (unsigned char *)b, blen, flags);
  819. else if (a->length == (int)blen && !OPENSSL_memcmp(a->data, b, blen))
  820. rv = 1;
  821. if (rv > 0 && peername)
  822. *peername = BUF_strndup((char *)a->data, a->length);
  823. } else {
  824. int astrlen;
  825. unsigned char *astr;
  826. astrlen = ASN1_STRING_to_UTF8(&astr, a);
  827. if (astrlen < 0)
  828. return -1;
  829. rv = equal(astr, astrlen, (unsigned char *)b, blen, flags);
  830. if (rv > 0 && peername)
  831. *peername = BUF_strndup((char *)astr, astrlen);
  832. OPENSSL_free(astr);
  833. }
  834. return rv;
  835. }
  836. static int do_x509_check(X509 *x, const char *chk, size_t chklen,
  837. unsigned int flags, int check_type, char **peername)
  838. {
  839. GENERAL_NAMES *gens = NULL;
  840. X509_NAME *name = NULL;
  841. size_t i;
  842. int j;
  843. int cnid = NID_undef;
  844. int alt_type;
  845. int san_present = 0;
  846. int rv = 0;
  847. equal_fn equal;
  848. /* See below, this flag is internal-only */
  849. flags &= ~_X509_CHECK_FLAG_DOT_SUBDOMAINS;
  850. if (check_type == GEN_EMAIL) {
  851. cnid = NID_pkcs9_emailAddress;
  852. alt_type = V_ASN1_IA5STRING;
  853. equal = equal_email;
  854. } else if (check_type == GEN_DNS) {
  855. cnid = NID_commonName;
  856. /* Implicit client-side DNS sub-domain pattern */
  857. if (chklen > 1 && chk[0] == '.')
  858. flags |= _X509_CHECK_FLAG_DOT_SUBDOMAINS;
  859. alt_type = V_ASN1_IA5STRING;
  860. if (flags & X509_CHECK_FLAG_NO_WILDCARDS)
  861. equal = equal_nocase;
  862. else
  863. equal = equal_wildcard;
  864. } else {
  865. alt_type = V_ASN1_OCTET_STRING;
  866. equal = equal_case;
  867. }
  868. gens = X509_get_ext_d2i(x, NID_subject_alt_name, NULL, NULL);
  869. if (gens) {
  870. for (i = 0; i < sk_GENERAL_NAME_num(gens); i++) {
  871. GENERAL_NAME *gen;
  872. ASN1_STRING *cstr;
  873. gen = sk_GENERAL_NAME_value(gens, i);
  874. if (gen->type != check_type)
  875. continue;
  876. san_present = 1;
  877. if (check_type == GEN_EMAIL)
  878. cstr = gen->d.rfc822Name;
  879. else if (check_type == GEN_DNS)
  880. cstr = gen->d.dNSName;
  881. else
  882. cstr = gen->d.iPAddress;
  883. /* Positive on success, negative on error! */
  884. if ((rv = do_check_string(cstr, alt_type, equal, flags,
  885. chk, chklen, peername)) != 0)
  886. break;
  887. }
  888. GENERAL_NAMES_free(gens);
  889. if (rv != 0)
  890. return rv;
  891. if (cnid == NID_undef
  892. || (san_present
  893. && !(flags & X509_CHECK_FLAG_ALWAYS_CHECK_SUBJECT)))
  894. return 0;
  895. }
  896. /* We're done if CN-ID is not pertinent */
  897. if (cnid == NID_undef)
  898. return 0;
  899. j = -1;
  900. name = X509_get_subject_name(x);
  901. while ((j = X509_NAME_get_index_by_NID(name, cnid, j)) >= 0) {
  902. X509_NAME_ENTRY *ne;
  903. ASN1_STRING *str;
  904. ne = X509_NAME_get_entry(name, j);
  905. str = X509_NAME_ENTRY_get_data(ne);
  906. /* Positive on success, negative on error! */
  907. if ((rv = do_check_string(str, -1, equal, flags,
  908. chk, chklen, peername)) != 0)
  909. return rv;
  910. }
  911. return 0;
  912. }
  913. int X509_check_host(X509 *x, const char *chk, size_t chklen,
  914. unsigned int flags, char **peername)
  915. {
  916. if (chk == NULL)
  917. return -2;
  918. if (OPENSSL_memchr(chk, '\0', chklen))
  919. return -2;
  920. return do_x509_check(x, chk, chklen, flags, GEN_DNS, peername);
  921. }
  922. int X509_check_email(X509 *x, const char *chk, size_t chklen,
  923. unsigned int flags)
  924. {
  925. if (chk == NULL)
  926. return -2;
  927. if (OPENSSL_memchr(chk, '\0', chklen))
  928. return -2;
  929. return do_x509_check(x, chk, chklen, flags, GEN_EMAIL, NULL);
  930. }
  931. int X509_check_ip(X509 *x, const unsigned char *chk, size_t chklen,
  932. unsigned int flags)
  933. {
  934. if (chk == NULL)
  935. return -2;
  936. return do_x509_check(x, (char *)chk, chklen, flags, GEN_IPADD, NULL);
  937. }
  938. int X509_check_ip_asc(X509 *x, const char *ipasc, unsigned int flags)
  939. {
  940. unsigned char ipout[16];
  941. size_t iplen;
  942. if (ipasc == NULL)
  943. return -2;
  944. iplen = (size_t)a2i_ipadd(ipout, ipasc);
  945. if (iplen == 0)
  946. return -2;
  947. return do_x509_check(x, (char *)ipout, iplen, flags, GEN_IPADD, NULL);
  948. }
  949. /*
  950. * Convert IP addresses both IPv4 and IPv6 into an OCTET STRING compatible
  951. * with RFC3280.
  952. */
  953. ASN1_OCTET_STRING *a2i_IPADDRESS(const char *ipasc)
  954. {
  955. unsigned char ipout[16];
  956. ASN1_OCTET_STRING *ret;
  957. int iplen;
  958. /* If string contains a ':' assume IPv6 */
  959. iplen = a2i_ipadd(ipout, ipasc);
  960. if (!iplen)
  961. return NULL;
  962. ret = ASN1_OCTET_STRING_new();
  963. if (!ret)
  964. return NULL;
  965. if (!ASN1_OCTET_STRING_set(ret, ipout, iplen)) {
  966. ASN1_OCTET_STRING_free(ret);
  967. return NULL;
  968. }
  969. return ret;
  970. }
  971. ASN1_OCTET_STRING *a2i_IPADDRESS_NC(const char *ipasc)
  972. {
  973. ASN1_OCTET_STRING *ret = NULL;
  974. unsigned char ipout[32];
  975. char *iptmp = NULL, *p;
  976. int iplen1, iplen2;
  977. p = strchr(ipasc, '/');
  978. if (!p)
  979. return NULL;
  980. iptmp = BUF_strdup(ipasc);
  981. if (!iptmp)
  982. return NULL;
  983. p = iptmp + (p - ipasc);
  984. *p++ = 0;
  985. iplen1 = a2i_ipadd(ipout, iptmp);
  986. if (!iplen1)
  987. goto err;
  988. iplen2 = a2i_ipadd(ipout + iplen1, p);
  989. OPENSSL_free(iptmp);
  990. iptmp = NULL;
  991. if (!iplen2 || (iplen1 != iplen2))
  992. goto err;
  993. ret = ASN1_OCTET_STRING_new();
  994. if (!ret)
  995. goto err;
  996. if (!ASN1_OCTET_STRING_set(ret, ipout, iplen1 + iplen2))
  997. goto err;
  998. return ret;
  999. err:
  1000. if (iptmp)
  1001. OPENSSL_free(iptmp);
  1002. if (ret)
  1003. ASN1_OCTET_STRING_free(ret);
  1004. return NULL;
  1005. }
  1006. int a2i_ipadd(unsigned char *ipout, const char *ipasc)
  1007. {
  1008. /* If string contains a ':' assume IPv6 */
  1009. if (strchr(ipasc, ':')) {
  1010. if (!ipv6_from_asc(ipout, ipasc))
  1011. return 0;
  1012. return 16;
  1013. } else {
  1014. if (!ipv4_from_asc(ipout, ipasc))
  1015. return 0;
  1016. return 4;
  1017. }
  1018. }
  1019. static int ipv4_from_asc(unsigned char *v4, const char *in)
  1020. {
  1021. int a0, a1, a2, a3;
  1022. if (sscanf(in, "%d.%d.%d.%d", &a0, &a1, &a2, &a3) != 4)
  1023. return 0;
  1024. if ((a0 < 0) || (a0 > 255) || (a1 < 0) || (a1 > 255)
  1025. || (a2 < 0) || (a2 > 255) || (a3 < 0) || (a3 > 255))
  1026. return 0;
  1027. v4[0] = a0;
  1028. v4[1] = a1;
  1029. v4[2] = a2;
  1030. v4[3] = a3;
  1031. return 1;
  1032. }
  1033. typedef struct {
  1034. /* Temporary store for IPV6 output */
  1035. unsigned char tmp[16];
  1036. /* Total number of bytes in tmp */
  1037. int total;
  1038. /* The position of a zero (corresponding to '::') */
  1039. int zero_pos;
  1040. /* Number of zeroes */
  1041. int zero_cnt;
  1042. } IPV6_STAT;
  1043. static int ipv6_from_asc(unsigned char *v6, const char *in)
  1044. {
  1045. IPV6_STAT v6stat;
  1046. v6stat.total = 0;
  1047. v6stat.zero_pos = -1;
  1048. v6stat.zero_cnt = 0;
  1049. /*
  1050. * Treat the IPv6 representation as a list of values separated by ':'.
  1051. * The presence of a '::' will parse as one, two or three zero length
  1052. * elements.
  1053. */
  1054. if (!CONF_parse_list(in, ':', 0, ipv6_cb, &v6stat))
  1055. return 0;
  1056. /* Now for some sanity checks */
  1057. if (v6stat.zero_pos == -1) {
  1058. /* If no '::' must have exactly 16 bytes */
  1059. if (v6stat.total != 16)
  1060. return 0;
  1061. } else {
  1062. /* If '::' must have less than 16 bytes */
  1063. if (v6stat.total == 16)
  1064. return 0;
  1065. /* More than three zeroes is an error */
  1066. if (v6stat.zero_cnt > 3)
  1067. return 0;
  1068. /* Can only have three zeroes if nothing else present */
  1069. else if (v6stat.zero_cnt == 3) {
  1070. if (v6stat.total > 0)
  1071. return 0;
  1072. }
  1073. /* Can only have two zeroes if at start or end */
  1074. else if (v6stat.zero_cnt == 2) {
  1075. if ((v6stat.zero_pos != 0)
  1076. && (v6stat.zero_pos != v6stat.total))
  1077. return 0;
  1078. } else
  1079. /* Can only have one zero if *not* start or end */
  1080. {
  1081. if ((v6stat.zero_pos == 0)
  1082. || (v6stat.zero_pos == v6stat.total))
  1083. return 0;
  1084. }
  1085. }
  1086. /* Format result */
  1087. if (v6stat.zero_pos >= 0) {
  1088. /* Copy initial part */
  1089. OPENSSL_memcpy(v6, v6stat.tmp, v6stat.zero_pos);
  1090. /* Zero middle */
  1091. OPENSSL_memset(v6 + v6stat.zero_pos, 0, 16 - v6stat.total);
  1092. /* Copy final part */
  1093. if (v6stat.total != v6stat.zero_pos)
  1094. OPENSSL_memcpy(v6 + v6stat.zero_pos + 16 - v6stat.total,
  1095. v6stat.tmp + v6stat.zero_pos,
  1096. v6stat.total - v6stat.zero_pos);
  1097. } else
  1098. OPENSSL_memcpy(v6, v6stat.tmp, 16);
  1099. return 1;
  1100. }
  1101. static int ipv6_cb(const char *elem, int len, void *usr)
  1102. {
  1103. IPV6_STAT *s = usr;
  1104. /* Error if 16 bytes written */
  1105. if (s->total == 16)
  1106. return 0;
  1107. if (len == 0) {
  1108. /* Zero length element, corresponds to '::' */
  1109. if (s->zero_pos == -1)
  1110. s->zero_pos = s->total;
  1111. /* If we've already got a :: its an error */
  1112. else if (s->zero_pos != s->total)
  1113. return 0;
  1114. s->zero_cnt++;
  1115. } else {
  1116. /* If more than 4 characters could be final a.b.c.d form */
  1117. if (len > 4) {
  1118. /* Need at least 4 bytes left */
  1119. if (s->total > 12)
  1120. return 0;
  1121. /* Must be end of string */
  1122. if (elem[len])
  1123. return 0;
  1124. if (!ipv4_from_asc(s->tmp + s->total, elem))
  1125. return 0;
  1126. s->total += 4;
  1127. } else {
  1128. if (!ipv6_hex(s->tmp + s->total, elem, len))
  1129. return 0;
  1130. s->total += 2;
  1131. }
  1132. }
  1133. return 1;
  1134. }
  1135. /*
  1136. * Convert a string of up to 4 hex digits into the corresponding IPv6 form.
  1137. */
  1138. static int ipv6_hex(unsigned char *out, const char *in, int inlen)
  1139. {
  1140. unsigned char c;
  1141. unsigned int num = 0;
  1142. if (inlen > 4)
  1143. return 0;
  1144. while (inlen--) {
  1145. c = *in++;
  1146. num <<= 4;
  1147. if ((c >= '0') && (c <= '9'))
  1148. num |= c - '0';
  1149. else if ((c >= 'A') && (c <= 'F'))
  1150. num |= c - 'A' + 10;
  1151. else if ((c >= 'a') && (c <= 'f'))
  1152. num |= c - 'a' + 10;
  1153. else
  1154. return 0;
  1155. }
  1156. out[0] = num >> 8;
  1157. out[1] = num & 0xff;
  1158. return 1;
  1159. }
  1160. int X509V3_NAME_from_section(X509_NAME *nm, STACK_OF (CONF_VALUE) * dn_sk,
  1161. unsigned long chtype)
  1162. {
  1163. CONF_VALUE *v;
  1164. int mval;
  1165. size_t i;
  1166. char *p, *type;
  1167. if (!nm)
  1168. return 0;
  1169. for (i = 0; i < sk_CONF_VALUE_num(dn_sk); i++) {
  1170. v = sk_CONF_VALUE_value(dn_sk, i);
  1171. type = v->name;
  1172. /*
  1173. * Skip past any leading X. X: X, etc to allow for multiple instances
  1174. */
  1175. for (p = type; *p; p++)
  1176. if ((*p == ':') || (*p == ',') || (*p == '.')) {
  1177. p++;
  1178. if (*p)
  1179. type = p;
  1180. break;
  1181. }
  1182. if (*type == '+') {
  1183. mval = -1;
  1184. type++;
  1185. } else
  1186. mval = 0;
  1187. if (!X509_NAME_add_entry_by_txt(nm, type, chtype,
  1188. (unsigned char *)v->value, -1, -1,
  1189. mval))
  1190. return 0;
  1191. }
  1192. return 1;
  1193. }