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.
 
 
 
 
 
 

535 lines
16 KiB

  1. /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
  2. * All rights reserved.
  3. *
  4. * This package is an SSL implementation written
  5. * by Eric Young (eay@cryptsoft.com).
  6. * The implementation was written so as to conform with Netscapes SSL.
  7. *
  8. * This library is free for commercial and non-commercial use as long as
  9. * the following conditions are aheared to. The following conditions
  10. * apply to all code found in this distribution, be it the RC4, RSA,
  11. * lhash, DES, etc., code; not just the SSL code. The SSL documentation
  12. * included with this distribution is covered by the same copyright terms
  13. * except that the holder is Tim Hudson (tjh@cryptsoft.com).
  14. *
  15. * Copyright remains Eric Young's, and as such any Copyright notices in
  16. * the code are not to be removed.
  17. * If this package is used in a product, Eric Young should be given attribution
  18. * as the author of the parts of the library used.
  19. * This can be in the form of a textual message at program startup or
  20. * in documentation (online or textual) provided with the package.
  21. *
  22. * Redistribution and use in source and binary forms, with or without
  23. * modification, are permitted provided that the following conditions
  24. * are met:
  25. * 1. Redistributions of source code must retain the copyright
  26. * notice, this list of conditions and the following disclaimer.
  27. * 2. Redistributions in binary form must reproduce the above copyright
  28. * notice, this list of conditions and the following disclaimer in the
  29. * documentation and/or other materials provided with the distribution.
  30. * 3. All advertising materials mentioning features or use of this software
  31. * must display the following acknowledgement:
  32. * "This product includes cryptographic software written by
  33. * Eric Young (eay@cryptsoft.com)"
  34. * The word 'cryptographic' can be left out if the rouines from the library
  35. * being used are not cryptographic related :-).
  36. * 4. If you include any Windows specific code (or a derivative thereof) from
  37. * the apps directory (application code) you must include an acknowledgement:
  38. * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
  39. *
  40. * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  41. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  42. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  43. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  44. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  45. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  46. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  47. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  48. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  49. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  50. * SUCH DAMAGE.
  51. *
  52. * The licence and distribution terms for any publically available version or
  53. * derivative of this code cannot be changed. i.e. this code cannot simply be
  54. * copied and put under another distribution licence
  55. * [including the GNU Public Licence.] */
  56. #include <ctype.h>
  57. #include <string.h>
  58. #include <openssl/asn1.h>
  59. #include <openssl/asn1t.h>
  60. #include <openssl/buf.h>
  61. #include <openssl/err.h>
  62. #include <openssl/mem.h>
  63. #include <openssl/obj.h>
  64. #include <openssl/stack.h>
  65. #include <openssl/x509.h>
  66. #include "../asn1/asn1_locl.h"
  67. typedef STACK_OF(X509_NAME_ENTRY) STACK_OF_X509_NAME_ENTRY;
  68. DECLARE_STACK_OF(STACK_OF_X509_NAME_ENTRY)
  69. /*
  70. * Maximum length of X509_NAME: much larger than anything we should
  71. * ever see in practice.
  72. */
  73. #define X509_NAME_MAX (1024 * 1024)
  74. static int x509_name_ex_d2i(ASN1_VALUE **val,
  75. const unsigned char **in, long len,
  76. const ASN1_ITEM *it,
  77. int tag, int aclass, char opt, ASN1_TLC *ctx);
  78. static int x509_name_ex_i2d(ASN1_VALUE **val, unsigned char **out,
  79. const ASN1_ITEM *it, int tag, int aclass);
  80. static int x509_name_ex_new(ASN1_VALUE **val, const ASN1_ITEM *it);
  81. static void x509_name_ex_free(ASN1_VALUE **val, const ASN1_ITEM *it);
  82. static int x509_name_encode(X509_NAME *a);
  83. static int x509_name_canon(X509_NAME *a);
  84. static int asn1_string_canon(ASN1_STRING *out, ASN1_STRING *in);
  85. static int i2d_name_canon(STACK_OF(STACK_OF_X509_NAME_ENTRY) * intname,
  86. unsigned char **in);
  87. ASN1_SEQUENCE(X509_NAME_ENTRY) = {
  88. ASN1_SIMPLE(X509_NAME_ENTRY, object, ASN1_OBJECT),
  89. ASN1_SIMPLE(X509_NAME_ENTRY, value, ASN1_PRINTABLE)
  90. } ASN1_SEQUENCE_END(X509_NAME_ENTRY)
  91. IMPLEMENT_ASN1_FUNCTIONS(X509_NAME_ENTRY)
  92. IMPLEMENT_ASN1_DUP_FUNCTION(X509_NAME_ENTRY)
  93. /*
  94. * For the "Name" type we need a SEQUENCE OF { SET OF X509_NAME_ENTRY } so
  95. * declare two template wrappers for this
  96. */
  97. ASN1_ITEM_TEMPLATE(X509_NAME_ENTRIES) =
  98. ASN1_EX_TEMPLATE_TYPE(ASN1_TFLG_SET_OF, 0, RDNS, X509_NAME_ENTRY)
  99. ASN1_ITEM_TEMPLATE_END(X509_NAME_ENTRIES)
  100. ASN1_ITEM_TEMPLATE(X509_NAME_INTERNAL) =
  101. ASN1_EX_TEMPLATE_TYPE(ASN1_TFLG_SEQUENCE_OF, 0, Name, X509_NAME_ENTRIES)
  102. ASN1_ITEM_TEMPLATE_END(X509_NAME_INTERNAL)
  103. /*
  104. * Normally that's where it would end: we'd have two nested STACK structures
  105. * representing the ASN1. Unfortunately X509_NAME uses a completely different
  106. * form and caches encodings so we have to process the internal form and
  107. * convert to the external form.
  108. */
  109. static const ASN1_EXTERN_FUNCS x509_name_ff = {
  110. NULL,
  111. x509_name_ex_new,
  112. x509_name_ex_free,
  113. 0, /* Default clear behaviour is OK */
  114. x509_name_ex_d2i,
  115. x509_name_ex_i2d,
  116. NULL,
  117. };
  118. IMPLEMENT_EXTERN_ASN1(X509_NAME, V_ASN1_SEQUENCE, x509_name_ff)
  119. IMPLEMENT_ASN1_FUNCTIONS(X509_NAME)
  120. IMPLEMENT_ASN1_DUP_FUNCTION(X509_NAME)
  121. static int x509_name_ex_new(ASN1_VALUE **val, const ASN1_ITEM *it)
  122. {
  123. X509_NAME *ret = NULL;
  124. ret = OPENSSL_malloc(sizeof(X509_NAME));
  125. if (!ret)
  126. goto memerr;
  127. if ((ret->entries = sk_X509_NAME_ENTRY_new_null()) == NULL)
  128. goto memerr;
  129. if ((ret->bytes = BUF_MEM_new()) == NULL)
  130. goto memerr;
  131. ret->canon_enc = NULL;
  132. ret->canon_enclen = 0;
  133. ret->modified = 1;
  134. *val = (ASN1_VALUE *)ret;
  135. return 1;
  136. memerr:
  137. OPENSSL_PUT_ERROR(X509, ERR_R_MALLOC_FAILURE);
  138. if (ret) {
  139. if (ret->entries)
  140. sk_X509_NAME_ENTRY_free(ret->entries);
  141. OPENSSL_free(ret);
  142. }
  143. return 0;
  144. }
  145. static void x509_name_ex_free(ASN1_VALUE **pval, const ASN1_ITEM *it)
  146. {
  147. X509_NAME *a;
  148. if (!pval || !*pval)
  149. return;
  150. a = (X509_NAME *)*pval;
  151. BUF_MEM_free(a->bytes);
  152. sk_X509_NAME_ENTRY_pop_free(a->entries, X509_NAME_ENTRY_free);
  153. if (a->canon_enc)
  154. OPENSSL_free(a->canon_enc);
  155. OPENSSL_free(a);
  156. *pval = NULL;
  157. }
  158. static void local_sk_X509_NAME_ENTRY_free(STACK_OF(X509_NAME_ENTRY) *ne)
  159. {
  160. sk_X509_NAME_ENTRY_free(ne);
  161. }
  162. static void local_sk_X509_NAME_ENTRY_pop_free(STACK_OF(X509_NAME_ENTRY) *ne)
  163. {
  164. sk_X509_NAME_ENTRY_pop_free(ne, X509_NAME_ENTRY_free);
  165. }
  166. static int x509_name_ex_d2i(ASN1_VALUE **val,
  167. const unsigned char **in, long len,
  168. const ASN1_ITEM *it, int tag, int aclass,
  169. char opt, ASN1_TLC *ctx)
  170. {
  171. const unsigned char *p = *in, *q;
  172. union {
  173. STACK_OF(STACK_OF_X509_NAME_ENTRY) *s;
  174. ASN1_VALUE *a;
  175. } intname = {
  176. NULL
  177. };
  178. union {
  179. X509_NAME *x;
  180. ASN1_VALUE *a;
  181. } nm = {
  182. NULL
  183. };
  184. size_t i, j;
  185. int ret;
  186. STACK_OF(X509_NAME_ENTRY) *entries;
  187. X509_NAME_ENTRY *entry;
  188. /* Bound the size of an X509_NAME we are willing to parse. */
  189. if (len > X509_NAME_MAX) {
  190. len = X509_NAME_MAX;
  191. }
  192. q = p;
  193. /* Get internal representation of Name */
  194. ret = ASN1_item_ex_d2i(&intname.a,
  195. &p, len, ASN1_ITEM_rptr(X509_NAME_INTERNAL),
  196. tag, aclass, opt, ctx);
  197. if (ret <= 0)
  198. return ret;
  199. if (*val)
  200. x509_name_ex_free(val, NULL);
  201. /* We've decoded it: now cache encoding */
  202. if (!x509_name_ex_new(&nm.a, NULL) || !BUF_MEM_grow(nm.x->bytes, p - q)) {
  203. sk_STACK_OF_X509_NAME_ENTRY_pop_free(intname.s,
  204. local_sk_X509_NAME_ENTRY_pop_free);
  205. goto err;
  206. }
  207. memcpy(nm.x->bytes->data, q, p - q);
  208. /* Convert internal representation to X509_NAME structure */
  209. for (i = 0; i < sk_STACK_OF_X509_NAME_ENTRY_num(intname.s); i++) {
  210. entries = sk_STACK_OF_X509_NAME_ENTRY_value(intname.s, i);
  211. for (j = 0; j < sk_X509_NAME_ENTRY_num(entries); j++) {
  212. entry = sk_X509_NAME_ENTRY_value(entries, j);
  213. entry->set = i;
  214. if (!sk_X509_NAME_ENTRY_push(nm.x->entries, entry))
  215. goto err;
  216. }
  217. sk_X509_NAME_ENTRY_free(entries);
  218. }
  219. sk_STACK_OF_X509_NAME_ENTRY_free(intname.s);
  220. ret = x509_name_canon(nm.x);
  221. if (!ret)
  222. goto err;
  223. nm.x->modified = 0;
  224. *val = nm.a;
  225. *in = p;
  226. return ret;
  227. err:
  228. if (nm.x != NULL)
  229. X509_NAME_free(nm.x);
  230. OPENSSL_PUT_ERROR(X509, ERR_R_ASN1_LIB);
  231. return 0;
  232. }
  233. static int x509_name_ex_i2d(ASN1_VALUE **val, unsigned char **out,
  234. const ASN1_ITEM *it, int tag, int aclass)
  235. {
  236. int ret;
  237. X509_NAME *a = (X509_NAME *)*val;
  238. if (a->modified) {
  239. ret = x509_name_encode(a);
  240. if (ret < 0)
  241. return ret;
  242. ret = x509_name_canon(a);
  243. if (ret < 0)
  244. return ret;
  245. }
  246. ret = a->bytes->length;
  247. if (out != NULL) {
  248. memcpy(*out, a->bytes->data, ret);
  249. *out += ret;
  250. }
  251. return ret;
  252. }
  253. static int x509_name_encode(X509_NAME *a)
  254. {
  255. union {
  256. STACK_OF(STACK_OF_X509_NAME_ENTRY) *s;
  257. ASN1_VALUE *a;
  258. } intname = {
  259. NULL
  260. };
  261. int len;
  262. unsigned char *p;
  263. STACK_OF(X509_NAME_ENTRY) *entries = NULL;
  264. X509_NAME_ENTRY *entry;
  265. int set = -1;
  266. size_t i;
  267. intname.s = sk_STACK_OF_X509_NAME_ENTRY_new_null();
  268. if (!intname.s)
  269. goto memerr;
  270. for (i = 0; i < sk_X509_NAME_ENTRY_num(a->entries); i++) {
  271. entry = sk_X509_NAME_ENTRY_value(a->entries, i);
  272. if (entry->set != set) {
  273. entries = sk_X509_NAME_ENTRY_new_null();
  274. if (!entries)
  275. goto memerr;
  276. if (!sk_STACK_OF_X509_NAME_ENTRY_push(intname.s, entries))
  277. goto memerr;
  278. set = entry->set;
  279. }
  280. if (!sk_X509_NAME_ENTRY_push(entries, entry))
  281. goto memerr;
  282. }
  283. len = ASN1_item_ex_i2d(&intname.a, NULL,
  284. ASN1_ITEM_rptr(X509_NAME_INTERNAL), -1, -1);
  285. if (!BUF_MEM_grow(a->bytes, len))
  286. goto memerr;
  287. p = (unsigned char *)a->bytes->data;
  288. ASN1_item_ex_i2d(&intname.a,
  289. &p, ASN1_ITEM_rptr(X509_NAME_INTERNAL), -1, -1);
  290. sk_STACK_OF_X509_NAME_ENTRY_pop_free(intname.s,
  291. local_sk_X509_NAME_ENTRY_free);
  292. a->modified = 0;
  293. return len;
  294. memerr:
  295. sk_STACK_OF_X509_NAME_ENTRY_pop_free(intname.s,
  296. local_sk_X509_NAME_ENTRY_free);
  297. OPENSSL_PUT_ERROR(X509, ERR_R_MALLOC_FAILURE);
  298. return -1;
  299. }
  300. /*
  301. * This function generates the canonical encoding of the Name structure. In
  302. * it all strings are converted to UTF8, leading, trailing and multiple
  303. * spaces collapsed, converted to lower case and the leading SEQUENCE header
  304. * removed. In future we could also normalize the UTF8 too. By doing this
  305. * comparison of Name structures can be rapidly perfomed by just using
  306. * memcmp() of the canonical encoding. By omitting the leading SEQUENCE name
  307. * constraints of type dirName can also be checked with a simple memcmp().
  308. */
  309. static int x509_name_canon(X509_NAME *a)
  310. {
  311. unsigned char *p;
  312. STACK_OF(STACK_OF_X509_NAME_ENTRY) *intname = NULL;
  313. STACK_OF(X509_NAME_ENTRY) *entries = NULL;
  314. X509_NAME_ENTRY *entry, *tmpentry = NULL;
  315. int set = -1, ret = 0;
  316. size_t i;
  317. if (a->canon_enc) {
  318. OPENSSL_free(a->canon_enc);
  319. a->canon_enc = NULL;
  320. }
  321. /* Special case: empty X509_NAME => null encoding */
  322. if (sk_X509_NAME_ENTRY_num(a->entries) == 0) {
  323. a->canon_enclen = 0;
  324. return 1;
  325. }
  326. intname = sk_STACK_OF_X509_NAME_ENTRY_new_null();
  327. if (!intname)
  328. goto err;
  329. for (i = 0; i < sk_X509_NAME_ENTRY_num(a->entries); i++) {
  330. entry = sk_X509_NAME_ENTRY_value(a->entries, i);
  331. if (entry->set != set) {
  332. entries = sk_X509_NAME_ENTRY_new_null();
  333. if (!entries)
  334. goto err;
  335. if (!sk_STACK_OF_X509_NAME_ENTRY_push(intname, entries)) {
  336. sk_X509_NAME_ENTRY_free(entries);
  337. goto err;
  338. }
  339. set = entry->set;
  340. }
  341. tmpentry = X509_NAME_ENTRY_new();
  342. if (tmpentry == NULL)
  343. goto err;
  344. tmpentry->object = OBJ_dup(entry->object);
  345. if (!asn1_string_canon(tmpentry->value, entry->value))
  346. goto err;
  347. if (!sk_X509_NAME_ENTRY_push(entries, tmpentry))
  348. goto err;
  349. tmpentry = NULL;
  350. }
  351. /* Finally generate encoding */
  352. a->canon_enclen = i2d_name_canon(intname, NULL);
  353. p = OPENSSL_malloc(a->canon_enclen);
  354. if (!p)
  355. goto err;
  356. a->canon_enc = p;
  357. i2d_name_canon(intname, &p);
  358. ret = 1;
  359. err:
  360. if (tmpentry)
  361. X509_NAME_ENTRY_free(tmpentry);
  362. if (intname)
  363. sk_STACK_OF_X509_NAME_ENTRY_pop_free(intname,
  364. local_sk_X509_NAME_ENTRY_pop_free);
  365. return ret;
  366. }
  367. /* Bitmap of all the types of string that will be canonicalized. */
  368. #define ASN1_MASK_CANON \
  369. (B_ASN1_UTF8STRING | B_ASN1_BMPSTRING | B_ASN1_UNIVERSALSTRING \
  370. | B_ASN1_PRINTABLESTRING | B_ASN1_T61STRING | B_ASN1_IA5STRING \
  371. | B_ASN1_VISIBLESTRING)
  372. static int asn1_string_canon(ASN1_STRING *out, ASN1_STRING *in)
  373. {
  374. unsigned char *to, *from;
  375. int len, i;
  376. /* If type not in bitmask just copy string across */
  377. if (!(ASN1_tag2bit(in->type) & ASN1_MASK_CANON)) {
  378. if (!ASN1_STRING_copy(out, in))
  379. return 0;
  380. return 1;
  381. }
  382. out->type = V_ASN1_UTF8STRING;
  383. out->length = ASN1_STRING_to_UTF8(&out->data, in);
  384. if (out->length == -1)
  385. return 0;
  386. to = out->data;
  387. from = to;
  388. len = out->length;
  389. /*
  390. * Convert string in place to canonical form. Ultimately we may need to
  391. * handle a wider range of characters but for now ignore anything with
  392. * MSB set and rely on the isspace() and tolower() functions.
  393. */
  394. /* Ignore leading spaces */
  395. while ((len > 0) && !(*from & 0x80) && isspace(*from)) {
  396. from++;
  397. len--;
  398. }
  399. to = from + len;
  400. /* Ignore trailing spaces */
  401. while ((len > 0) && !(to[-1] & 0x80) && isspace(to[-1])) {
  402. to--;
  403. len--;
  404. }
  405. to = out->data;
  406. i = 0;
  407. while (i < len) {
  408. /* If MSB set just copy across */
  409. if (*from & 0x80) {
  410. *to++ = *from++;
  411. i++;
  412. }
  413. /* Collapse multiple spaces */
  414. else if (isspace(*from)) {
  415. /* Copy one space across */
  416. *to++ = ' ';
  417. /*
  418. * Ignore subsequent spaces. Note: don't need to check len here
  419. * because we know the last character is a non-space so we can't
  420. * overflow.
  421. */
  422. do {
  423. from++;
  424. i++;
  425. }
  426. while (!(*from & 0x80) && isspace(*from));
  427. } else {
  428. *to++ = tolower(*from);
  429. from++;
  430. i++;
  431. }
  432. }
  433. out->length = to - out->data;
  434. return 1;
  435. }
  436. static int i2d_name_canon(STACK_OF(STACK_OF_X509_NAME_ENTRY) * _intname,
  437. unsigned char **in)
  438. {
  439. int len, ltmp;
  440. size_t i;
  441. ASN1_VALUE *v;
  442. STACK_OF(ASN1_VALUE) *intname = (STACK_OF(ASN1_VALUE) *)_intname;
  443. len = 0;
  444. for (i = 0; i < sk_ASN1_VALUE_num(intname); i++) {
  445. v = sk_ASN1_VALUE_value(intname, i);
  446. ltmp = ASN1_item_ex_i2d(&v, in,
  447. ASN1_ITEM_rptr(X509_NAME_ENTRIES), -1, -1);
  448. if (ltmp < 0)
  449. return ltmp;
  450. len += ltmp;
  451. }
  452. return len;
  453. }
  454. int X509_NAME_set(X509_NAME **xn, X509_NAME *name)
  455. {
  456. X509_NAME *in;
  457. if (!xn || !name)
  458. return (0);
  459. if (*xn != name) {
  460. in = X509_NAME_dup(name);
  461. if (in != NULL) {
  462. X509_NAME_free(*xn);
  463. *xn = in;
  464. }
  465. }
  466. return (*xn != NULL);
  467. }
  468. IMPLEMENT_ASN1_SET_OF(X509_NAME_ENTRY)