v3_utl.c 38 KB

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