a_strex.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645
  1. /*
  2. * Copyright 2000-2018 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the OpenSSL license (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #include <stdio.h>
  10. #include <string.h>
  11. #include "internal/cryptlib.h"
  12. #include "internal/asn1_int.h"
  13. #include <openssl/crypto.h>
  14. #include <openssl/x509.h>
  15. #include <openssl/asn1.h>
  16. #include "charmap.h"
  17. /*
  18. * ASN1_STRING_print_ex() and X509_NAME_print_ex(). Enhanced string and name
  19. * printing routines handling multibyte characters, RFC2253 and a host of
  20. * other options.
  21. */
  22. #define CHARTYPE_BS_ESC (ASN1_STRFLGS_ESC_2253 | CHARTYPE_FIRST_ESC_2253 | CHARTYPE_LAST_ESC_2253)
  23. #define ESC_FLAGS (ASN1_STRFLGS_ESC_2253 | \
  24. ASN1_STRFLGS_ESC_2254 | \
  25. ASN1_STRFLGS_ESC_QUOTE | \
  26. ASN1_STRFLGS_ESC_CTRL | \
  27. ASN1_STRFLGS_ESC_MSB)
  28. /*
  29. * Three IO functions for sending data to memory, a BIO and and a FILE
  30. * pointer.
  31. */
  32. static int send_bio_chars(void *arg, const void *buf, int len)
  33. {
  34. if (!arg)
  35. return 1;
  36. if (BIO_write(arg, buf, len) != len)
  37. return 0;
  38. return 1;
  39. }
  40. #ifndef OPENSSL_NO_STDIO
  41. static int send_fp_chars(void *arg, const void *buf, int len)
  42. {
  43. if (!arg)
  44. return 1;
  45. if (fwrite(buf, 1, len, arg) != (unsigned int)len)
  46. return 0;
  47. return 1;
  48. }
  49. #endif
  50. typedef int char_io (void *arg, const void *buf, int len);
  51. /*
  52. * This function handles display of strings, one character at a time. It is
  53. * passed an unsigned long for each character because it could come from 2 or
  54. * even 4 byte forms.
  55. */
  56. static int do_esc_char(unsigned long c, unsigned short flags, char *do_quotes,
  57. char_io *io_ch, void *arg)
  58. {
  59. unsigned short chflgs;
  60. unsigned char chtmp;
  61. char tmphex[HEX_SIZE(long) + 3];
  62. if (c > 0xffffffffL)
  63. return -1;
  64. if (c > 0xffff) {
  65. BIO_snprintf(tmphex, sizeof(tmphex), "\\W%08lX", c);
  66. if (!io_ch(arg, tmphex, 10))
  67. return -1;
  68. return 10;
  69. }
  70. if (c > 0xff) {
  71. BIO_snprintf(tmphex, sizeof(tmphex), "\\U%04lX", c);
  72. if (!io_ch(arg, tmphex, 6))
  73. return -1;
  74. return 6;
  75. }
  76. chtmp = (unsigned char)c;
  77. if (chtmp > 0x7f)
  78. chflgs = flags & ASN1_STRFLGS_ESC_MSB;
  79. else
  80. chflgs = char_type[chtmp] & flags;
  81. if (chflgs & CHARTYPE_BS_ESC) {
  82. /* If we don't escape with quotes, signal we need quotes */
  83. if (chflgs & ASN1_STRFLGS_ESC_QUOTE) {
  84. if (do_quotes)
  85. *do_quotes = 1;
  86. if (!io_ch(arg, &chtmp, 1))
  87. return -1;
  88. return 1;
  89. }
  90. if (!io_ch(arg, "\\", 1))
  91. return -1;
  92. if (!io_ch(arg, &chtmp, 1))
  93. return -1;
  94. return 2;
  95. }
  96. if (chflgs & (ASN1_STRFLGS_ESC_CTRL
  97. | ASN1_STRFLGS_ESC_MSB
  98. | ASN1_STRFLGS_ESC_2254)) {
  99. BIO_snprintf(tmphex, 11, "\\%02X", chtmp);
  100. if (!io_ch(arg, tmphex, 3))
  101. return -1;
  102. return 3;
  103. }
  104. /*
  105. * If we get this far and do any escaping at all must escape the escape
  106. * character itself: backslash.
  107. */
  108. if (chtmp == '\\' && (flags & ESC_FLAGS)) {
  109. if (!io_ch(arg, "\\\\", 2))
  110. return -1;
  111. return 2;
  112. }
  113. if (!io_ch(arg, &chtmp, 1))
  114. return -1;
  115. return 1;
  116. }
  117. #define BUF_TYPE_WIDTH_MASK 0x7
  118. #define BUF_TYPE_CONVUTF8 0x8
  119. /*
  120. * This function sends each character in a buffer to do_esc_char(). It
  121. * interprets the content formats and converts to or from UTF8 as
  122. * appropriate.
  123. */
  124. static int do_buf(unsigned char *buf, int buflen,
  125. int type, unsigned short flags, char *quotes, char_io *io_ch,
  126. void *arg)
  127. {
  128. int i, outlen, len;
  129. unsigned short orflags;
  130. unsigned char *p, *q;
  131. unsigned long c;
  132. p = buf;
  133. q = buf + buflen;
  134. outlen = 0;
  135. while (p != q) {
  136. if (p == buf && flags & ASN1_STRFLGS_ESC_2253)
  137. orflags = CHARTYPE_FIRST_ESC_2253;
  138. else
  139. orflags = 0;
  140. switch (type & BUF_TYPE_WIDTH_MASK) {
  141. case 4:
  142. c = ((unsigned long)*p++) << 24;
  143. c |= ((unsigned long)*p++) << 16;
  144. c |= ((unsigned long)*p++) << 8;
  145. c |= *p++;
  146. break;
  147. case 2:
  148. c = ((unsigned long)*p++) << 8;
  149. c |= *p++;
  150. break;
  151. case 1:
  152. c = *p++;
  153. break;
  154. case 0:
  155. i = UTF8_getc(p, buflen, &c);
  156. if (i < 0)
  157. return -1; /* Invalid UTF8String */
  158. p += i;
  159. break;
  160. default:
  161. return -1; /* invalid width */
  162. }
  163. if (p == q && flags & ASN1_STRFLGS_ESC_2253)
  164. orflags = CHARTYPE_LAST_ESC_2253;
  165. if (type & BUF_TYPE_CONVUTF8) {
  166. unsigned char utfbuf[6];
  167. int utflen;
  168. utflen = UTF8_putc(utfbuf, sizeof(utfbuf), c);
  169. for (i = 0; i < utflen; i++) {
  170. /*
  171. * We don't need to worry about setting orflags correctly
  172. * because if utflen==1 its value will be correct anyway
  173. * otherwise each character will be > 0x7f and so the
  174. * character will never be escaped on first and last.
  175. */
  176. len = do_esc_char(utfbuf[i], flags | orflags, quotes,
  177. io_ch, arg);
  178. if (len < 0)
  179. return -1;
  180. outlen += len;
  181. }
  182. } else {
  183. len = do_esc_char(c, flags | orflags, quotes,
  184. io_ch, arg);
  185. if (len < 0)
  186. return -1;
  187. outlen += len;
  188. }
  189. }
  190. return outlen;
  191. }
  192. /* This function hex dumps a buffer of characters */
  193. static int do_hex_dump(char_io *io_ch, void *arg, unsigned char *buf,
  194. int buflen)
  195. {
  196. static const char hexdig[] = "0123456789ABCDEF";
  197. unsigned char *p, *q;
  198. char hextmp[2];
  199. if (arg) {
  200. p = buf;
  201. q = buf + buflen;
  202. while (p != q) {
  203. hextmp[0] = hexdig[*p >> 4];
  204. hextmp[1] = hexdig[*p & 0xf];
  205. if (!io_ch(arg, hextmp, 2))
  206. return -1;
  207. p++;
  208. }
  209. }
  210. return buflen << 1;
  211. }
  212. /*
  213. * "dump" a string. This is done when the type is unknown, or the flags
  214. * request it. We can either dump the content octets or the entire DER
  215. * encoding. This uses the RFC2253 #01234 format.
  216. */
  217. static int do_dump(unsigned long lflags, char_io *io_ch, void *arg,
  218. const ASN1_STRING *str)
  219. {
  220. /*
  221. * Placing the ASN1_STRING in a temp ASN1_TYPE allows the DER encoding to
  222. * readily obtained
  223. */
  224. ASN1_TYPE t;
  225. unsigned char *der_buf, *p;
  226. int outlen, der_len;
  227. if (!io_ch(arg, "#", 1))
  228. return -1;
  229. /* If we don't dump DER encoding just dump content octets */
  230. if (!(lflags & ASN1_STRFLGS_DUMP_DER)) {
  231. outlen = do_hex_dump(io_ch, arg, str->data, str->length);
  232. if (outlen < 0)
  233. return -1;
  234. return outlen + 1;
  235. }
  236. t.type = str->type;
  237. t.value.ptr = (char *)str;
  238. der_len = i2d_ASN1_TYPE(&t, NULL);
  239. if ((der_buf = OPENSSL_malloc(der_len)) == NULL) {
  240. ASN1err(ASN1_F_DO_DUMP, ERR_R_MALLOC_FAILURE);
  241. return -1;
  242. }
  243. p = der_buf;
  244. i2d_ASN1_TYPE(&t, &p);
  245. outlen = do_hex_dump(io_ch, arg, der_buf, der_len);
  246. OPENSSL_free(der_buf);
  247. if (outlen < 0)
  248. return -1;
  249. return outlen + 1;
  250. }
  251. /*
  252. * Lookup table to convert tags to character widths, 0 = UTF8 encoded, -1 is
  253. * used for non string types otherwise it is the number of bytes per
  254. * character
  255. */
  256. static const signed char tag2nbyte[] = {
  257. -1, -1, -1, -1, -1, /* 0-4 */
  258. -1, -1, -1, -1, -1, /* 5-9 */
  259. -1, -1, 0, -1, /* 10-13 */
  260. -1, -1, -1, -1, /* 15-17 */
  261. 1, 1, 1, /* 18-20 */
  262. -1, 1, 1, 1, /* 21-24 */
  263. -1, 1, -1, /* 25-27 */
  264. 4, -1, 2 /* 28-30 */
  265. };
  266. /*
  267. * This is the main function, print out an ASN1_STRING taking note of various
  268. * escape and display options. Returns number of characters written or -1 if
  269. * an error occurred.
  270. */
  271. static int do_print_ex(char_io *io_ch, void *arg, unsigned long lflags,
  272. const ASN1_STRING *str)
  273. {
  274. int outlen, len;
  275. int type;
  276. char quotes;
  277. unsigned short flags;
  278. quotes = 0;
  279. /* Keep a copy of escape flags */
  280. flags = (unsigned short)(lflags & ESC_FLAGS);
  281. type = str->type;
  282. outlen = 0;
  283. if (lflags & ASN1_STRFLGS_SHOW_TYPE) {
  284. const char *tagname;
  285. tagname = ASN1_tag2str(type);
  286. outlen += strlen(tagname);
  287. if (!io_ch(arg, tagname, outlen) || !io_ch(arg, ":", 1))
  288. return -1;
  289. outlen++;
  290. }
  291. /* Decide what to do with type, either dump content or display it */
  292. /* Dump everything */
  293. if (lflags & ASN1_STRFLGS_DUMP_ALL)
  294. type = -1;
  295. /* Ignore the string type */
  296. else if (lflags & ASN1_STRFLGS_IGNORE_TYPE)
  297. type = 1;
  298. else {
  299. /* Else determine width based on type */
  300. if ((type > 0) && (type < 31))
  301. type = tag2nbyte[type];
  302. else
  303. type = -1;
  304. if ((type == -1) && !(lflags & ASN1_STRFLGS_DUMP_UNKNOWN))
  305. type = 1;
  306. }
  307. if (type == -1) {
  308. len = do_dump(lflags, io_ch, arg, str);
  309. if (len < 0)
  310. return -1;
  311. outlen += len;
  312. return outlen;
  313. }
  314. if (lflags & ASN1_STRFLGS_UTF8_CONVERT) {
  315. /*
  316. * Note: if string is UTF8 and we want to convert to UTF8 then we
  317. * just interpret it as 1 byte per character to avoid converting
  318. * twice.
  319. */
  320. if (!type)
  321. type = 1;
  322. else
  323. type |= BUF_TYPE_CONVUTF8;
  324. }
  325. len = do_buf(str->data, str->length, type, flags, &quotes, io_ch, NULL);
  326. if (len < 0)
  327. return -1;
  328. outlen += len;
  329. if (quotes)
  330. outlen += 2;
  331. if (!arg)
  332. return outlen;
  333. if (quotes && !io_ch(arg, "\"", 1))
  334. return -1;
  335. if (do_buf(str->data, str->length, type, flags, NULL, io_ch, arg) < 0)
  336. return -1;
  337. if (quotes && !io_ch(arg, "\"", 1))
  338. return -1;
  339. return outlen;
  340. }
  341. /* Used for line indenting: print 'indent' spaces */
  342. static int do_indent(char_io *io_ch, void *arg, int indent)
  343. {
  344. int i;
  345. for (i = 0; i < indent; i++)
  346. if (!io_ch(arg, " ", 1))
  347. return 0;
  348. return 1;
  349. }
  350. #define FN_WIDTH_LN 25
  351. #define FN_WIDTH_SN 10
  352. static int do_name_ex(char_io *io_ch, void *arg, const X509_NAME *n,
  353. int indent, unsigned long flags)
  354. {
  355. int i, prev = -1, orflags, cnt;
  356. int fn_opt, fn_nid;
  357. ASN1_OBJECT *fn;
  358. const ASN1_STRING *val;
  359. const X509_NAME_ENTRY *ent;
  360. char objtmp[80];
  361. const char *objbuf;
  362. int outlen, len;
  363. char *sep_dn, *sep_mv, *sep_eq;
  364. int sep_dn_len, sep_mv_len, sep_eq_len;
  365. if (indent < 0)
  366. indent = 0;
  367. outlen = indent;
  368. if (!do_indent(io_ch, arg, indent))
  369. return -1;
  370. switch (flags & XN_FLAG_SEP_MASK) {
  371. case XN_FLAG_SEP_MULTILINE:
  372. sep_dn = "\n";
  373. sep_dn_len = 1;
  374. sep_mv = " + ";
  375. sep_mv_len = 3;
  376. break;
  377. case XN_FLAG_SEP_COMMA_PLUS:
  378. sep_dn = ",";
  379. sep_dn_len = 1;
  380. sep_mv = "+";
  381. sep_mv_len = 1;
  382. indent = 0;
  383. break;
  384. case XN_FLAG_SEP_CPLUS_SPC:
  385. sep_dn = ", ";
  386. sep_dn_len = 2;
  387. sep_mv = " + ";
  388. sep_mv_len = 3;
  389. indent = 0;
  390. break;
  391. case XN_FLAG_SEP_SPLUS_SPC:
  392. sep_dn = "; ";
  393. sep_dn_len = 2;
  394. sep_mv = " + ";
  395. sep_mv_len = 3;
  396. indent = 0;
  397. break;
  398. default:
  399. return -1;
  400. }
  401. if (flags & XN_FLAG_SPC_EQ) {
  402. sep_eq = " = ";
  403. sep_eq_len = 3;
  404. } else {
  405. sep_eq = "=";
  406. sep_eq_len = 1;
  407. }
  408. fn_opt = flags & XN_FLAG_FN_MASK;
  409. cnt = X509_NAME_entry_count(n);
  410. for (i = 0; i < cnt; i++) {
  411. if (flags & XN_FLAG_DN_REV)
  412. ent = X509_NAME_get_entry(n, cnt - i - 1);
  413. else
  414. ent = X509_NAME_get_entry(n, i);
  415. if (prev != -1) {
  416. if (prev == X509_NAME_ENTRY_set(ent)) {
  417. if (!io_ch(arg, sep_mv, sep_mv_len))
  418. return -1;
  419. outlen += sep_mv_len;
  420. } else {
  421. if (!io_ch(arg, sep_dn, sep_dn_len))
  422. return -1;
  423. outlen += sep_dn_len;
  424. if (!do_indent(io_ch, arg, indent))
  425. return -1;
  426. outlen += indent;
  427. }
  428. }
  429. prev = X509_NAME_ENTRY_set(ent);
  430. fn = X509_NAME_ENTRY_get_object(ent);
  431. val = X509_NAME_ENTRY_get_data(ent);
  432. fn_nid = OBJ_obj2nid(fn);
  433. if (fn_opt != XN_FLAG_FN_NONE) {
  434. int objlen, fld_len;
  435. if ((fn_opt == XN_FLAG_FN_OID) || (fn_nid == NID_undef)) {
  436. OBJ_obj2txt(objtmp, sizeof(objtmp), fn, 1);
  437. fld_len = 0; /* XXX: what should this be? */
  438. objbuf = objtmp;
  439. } else {
  440. if (fn_opt == XN_FLAG_FN_SN) {
  441. fld_len = FN_WIDTH_SN;
  442. objbuf = OBJ_nid2sn(fn_nid);
  443. } else if (fn_opt == XN_FLAG_FN_LN) {
  444. fld_len = FN_WIDTH_LN;
  445. objbuf = OBJ_nid2ln(fn_nid);
  446. } else {
  447. fld_len = 0; /* XXX: what should this be? */
  448. objbuf = "";
  449. }
  450. }
  451. objlen = strlen(objbuf);
  452. if (!io_ch(arg, objbuf, objlen))
  453. return -1;
  454. if ((objlen < fld_len) && (flags & XN_FLAG_FN_ALIGN)) {
  455. if (!do_indent(io_ch, arg, fld_len - objlen))
  456. return -1;
  457. outlen += fld_len - objlen;
  458. }
  459. if (!io_ch(arg, sep_eq, sep_eq_len))
  460. return -1;
  461. outlen += objlen + sep_eq_len;
  462. }
  463. /*
  464. * If the field name is unknown then fix up the DER dump flag. We
  465. * might want to limit this further so it will DER dump on anything
  466. * other than a few 'standard' fields.
  467. */
  468. if ((fn_nid == NID_undef) && (flags & XN_FLAG_DUMP_UNKNOWN_FIELDS))
  469. orflags = ASN1_STRFLGS_DUMP_ALL;
  470. else
  471. orflags = 0;
  472. len = do_print_ex(io_ch, arg, flags | orflags, val);
  473. if (len < 0)
  474. return -1;
  475. outlen += len;
  476. }
  477. return outlen;
  478. }
  479. /* Wrappers round the main functions */
  480. int X509_NAME_print_ex(BIO *out, const X509_NAME *nm, int indent,
  481. unsigned long flags)
  482. {
  483. if (flags == XN_FLAG_COMPAT)
  484. return X509_NAME_print(out, nm, indent);
  485. return do_name_ex(send_bio_chars, out, nm, indent, flags);
  486. }
  487. #ifndef OPENSSL_NO_STDIO
  488. int X509_NAME_print_ex_fp(FILE *fp, const X509_NAME *nm, int indent,
  489. unsigned long flags)
  490. {
  491. if (flags == XN_FLAG_COMPAT) {
  492. BIO *btmp;
  493. int ret;
  494. btmp = BIO_new_fp(fp, BIO_NOCLOSE);
  495. if (!btmp)
  496. return -1;
  497. ret = X509_NAME_print(btmp, nm, indent);
  498. BIO_free(btmp);
  499. return ret;
  500. }
  501. return do_name_ex(send_fp_chars, fp, nm, indent, flags);
  502. }
  503. #endif
  504. int ASN1_STRING_print_ex(BIO *out, const ASN1_STRING *str, unsigned long flags)
  505. {
  506. return do_print_ex(send_bio_chars, out, flags, str);
  507. }
  508. #ifndef OPENSSL_NO_STDIO
  509. int ASN1_STRING_print_ex_fp(FILE *fp, const ASN1_STRING *str, unsigned long flags)
  510. {
  511. return do_print_ex(send_fp_chars, fp, flags, str);
  512. }
  513. #endif
  514. /*
  515. * Utility function: convert any string type to UTF8, returns number of bytes
  516. * in output string or a negative error code
  517. */
  518. int ASN1_STRING_to_UTF8(unsigned char **out, const ASN1_STRING *in)
  519. {
  520. ASN1_STRING stmp, *str = &stmp;
  521. int mbflag, type, ret;
  522. if (!in)
  523. return -1;
  524. type = in->type;
  525. if ((type < 0) || (type > 30))
  526. return -1;
  527. mbflag = tag2nbyte[type];
  528. if (mbflag == -1)
  529. return -1;
  530. mbflag |= MBSTRING_FLAG;
  531. stmp.data = NULL;
  532. stmp.length = 0;
  533. stmp.flags = 0;
  534. ret =
  535. ASN1_mbstring_copy(&str, in->data, in->length, mbflag,
  536. B_ASN1_UTF8STRING);
  537. if (ret < 0)
  538. return ret;
  539. *out = stmp.data;
  540. return stmp.length;
  541. }
  542. /* Return 1 if host is a valid hostname and 0 otherwise */
  543. int asn1_valid_host(const ASN1_STRING *host)
  544. {
  545. int hostlen = host->length;
  546. const unsigned char *hostptr = host->data;
  547. int type = host->type;
  548. int i;
  549. signed char width = -1;
  550. unsigned short chflags = 0, prevchflags;
  551. if (type > 0 && type < 31)
  552. width = tag2nbyte[type];
  553. if (width == -1 || hostlen == 0)
  554. return 0;
  555. /* Treat UTF8String as width 1 as any MSB set is invalid */
  556. if (width == 0)
  557. width = 1;
  558. for (i = 0 ; i < hostlen; i+= width) {
  559. prevchflags = chflags;
  560. /* Value must be <= 0x7F: check upper bytes are all zeroes */
  561. if (width == 4) {
  562. if (*hostptr++ != 0 || *hostptr++ != 0 || *hostptr++ != 0)
  563. return 0;
  564. } else if (width == 2) {
  565. if (*hostptr++ != 0)
  566. return 0;
  567. }
  568. if (*hostptr > 0x7f)
  569. return 0;
  570. chflags = char_type[*hostptr++];
  571. if (!(chflags & (CHARTYPE_HOST_ANY | CHARTYPE_HOST_WILD))) {
  572. /* Nothing else allowed at start or end of string */
  573. if (i == 0 || i == hostlen - 1)
  574. return 0;
  575. /* Otherwise invalid if not dot or hyphen */
  576. if (!(chflags & (CHARTYPE_HOST_DOT | CHARTYPE_HOST_HYPHEN)))
  577. return 0;
  578. /*
  579. * If previous is dot or hyphen then illegal unless both
  580. * are hyphens: as .- -. .. are all illegal
  581. */
  582. if (prevchflags & (CHARTYPE_HOST_DOT | CHARTYPE_HOST_HYPHEN)
  583. && ((prevchflags & CHARTYPE_HOST_DOT)
  584. || (chflags & CHARTYPE_HOST_DOT)))
  585. return 0;
  586. }
  587. }
  588. return 1;
  589. }