You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

1312 lines
32 KiB

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