a_strex.c 17 KB

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