Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 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 | |
David Benjamin | 0dcbc6e | 2021-08-08 17:33:57 -0400 | [diff] [blame] | 57 | #include <openssl/asn1.h> |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 58 | |
David Benjamin | 8b988b8 | 2022-04-13 18:57:49 +0200 | [diff] [blame] | 59 | #include <assert.h> |
David Benjamin | 07a6628 | 2021-08-08 17:45:05 -0400 | [diff] [blame] | 60 | #include <ctype.h> |
David Benjamin | 2b63add | 2017-12-04 17:05:54 -0500 | [diff] [blame] | 61 | #include <inttypes.h> |
David Benjamin | 1e63b0c | 2022-12-03 17:08:13 -0500 | [diff] [blame] | 62 | #include <limits.h> |
Adam Langley | 2b2d66d | 2015-01-30 17:08:37 -0800 | [diff] [blame] | 63 | #include <string.h> |
David Benjamin | fdeb4aa | 2022-06-22 14:18:20 -0400 | [diff] [blame] | 64 | #include <time.h> |
Adam Langley | 2b2d66d | 2015-01-30 17:08:37 -0800 | [diff] [blame] | 65 | |
David Benjamin | 0dcbc6e | 2021-08-08 17:33:57 -0400 | [diff] [blame] | 66 | #include <openssl/bio.h> |
David Benjamin | fdeb4aa | 2022-06-22 14:18:20 -0400 | [diff] [blame] | 67 | #include <openssl/bytestring.h> |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 68 | #include <openssl/mem.h> |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 69 | |
David Benjamin | 695b2e1 | 2022-06-23 16:38:15 -0400 | [diff] [blame] | 70 | #include "../bytestring/internal.h" |
David Benjamin | 23d5842 | 2023-12-13 10:33:17 -0500 | [diff] [blame] | 71 | #include "../internal.h" |
David Benjamin | 0dcbc6e | 2021-08-08 17:33:57 -0400 | [diff] [blame] | 72 | #include "internal.h" |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 73 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 74 | |
David Benjamin | ac57319 | 2022-05-10 20:46:03 -0400 | [diff] [blame] | 75 | #define ESC_FLAGS \ |
| 76 | (ASN1_STRFLGS_ESC_2253 | ASN1_STRFLGS_ESC_QUOTE | ASN1_STRFLGS_ESC_CTRL | \ |
| 77 | ASN1_STRFLGS_ESC_MSB) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 78 | |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 79 | static int maybe_write(BIO *out, const void *buf, int len) { |
David Benjamin | 4635048 | 2022-06-16 14:03:12 -0400 | [diff] [blame] | 80 | // If |out| is NULL, ignore the output but report the length. |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 81 | return out == NULL || BIO_write(out, buf, len) == len; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 82 | } |
| 83 | |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 84 | static int is_control_character(unsigned char c) { return c < 32 || c == 127; } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 85 | |
David Benjamin | ac57319 | 2022-05-10 20:46:03 -0400 | [diff] [blame] | 86 | static int do_esc_char(uint32_t c, unsigned long flags, char *do_quotes, |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 87 | BIO *out, int is_first, int is_last) { |
David Benjamin | 4635048 | 2022-06-16 14:03:12 -0400 | [diff] [blame] | 88 | // |c| is a |uint32_t| because, depending on |ASN1_STRFLGS_UTF8_CONVERT|, |
| 89 | // we may be escaping bytes or Unicode codepoints. |
| 90 | char buf[16]; // Large enough for "\\W01234567". |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 91 | unsigned char u8 = (unsigned char)c; |
| 92 | if (c > 0xffff) { |
David Benjamin | 23d6e4c | 2023-07-15 02:44:54 -0400 | [diff] [blame] | 93 | snprintf(buf, sizeof(buf), "\\W%08" PRIX32, c); |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 94 | } else if (c > 0xff) { |
David Benjamin | 23d6e4c | 2023-07-15 02:44:54 -0400 | [diff] [blame] | 95 | snprintf(buf, sizeof(buf), "\\U%04" PRIX32, c); |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 96 | } else if ((flags & ASN1_STRFLGS_ESC_MSB) && c > 0x7f) { |
David Benjamin | 23d6e4c | 2023-07-15 02:44:54 -0400 | [diff] [blame] | 97 | snprintf(buf, sizeof(buf), "\\%02X", c); |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 98 | } else if ((flags & ASN1_STRFLGS_ESC_CTRL) && is_control_character(c)) { |
David Benjamin | 23d6e4c | 2023-07-15 02:44:54 -0400 | [diff] [blame] | 99 | snprintf(buf, sizeof(buf), "\\%02X", c); |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 100 | } else if (flags & ASN1_STRFLGS_ESC_2253) { |
David Benjamin | 4635048 | 2022-06-16 14:03:12 -0400 | [diff] [blame] | 101 | // See RFC 2253, sections 2.4 and 4. |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 102 | if (c == '\\' || c == '"') { |
David Benjamin | 4635048 | 2022-06-16 14:03:12 -0400 | [diff] [blame] | 103 | // Quotes and backslashes are always escaped, quoted or not. |
David Benjamin | 23d6e4c | 2023-07-15 02:44:54 -0400 | [diff] [blame] | 104 | snprintf(buf, sizeof(buf), "\\%c", (int)c); |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 105 | } else if (c == ',' || c == '+' || c == '<' || c == '>' || c == ';' || |
| 106 | (is_first && (c == ' ' || c == '#')) || |
| 107 | (is_last && (c == ' '))) { |
| 108 | if (flags & ASN1_STRFLGS_ESC_QUOTE) { |
David Benjamin | 4635048 | 2022-06-16 14:03:12 -0400 | [diff] [blame] | 109 | // No need to escape, just tell the caller to quote. |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 110 | if (do_quotes != NULL) { |
| 111 | *do_quotes = 1; |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 112 | } |
David Benjamin | ac57319 | 2022-05-10 20:46:03 -0400 | [diff] [blame] | 113 | return maybe_write(out, &u8, 1) ? 1 : -1; |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 114 | } |
David Benjamin | 23d6e4c | 2023-07-15 02:44:54 -0400 | [diff] [blame] | 115 | snprintf(buf, sizeof(buf), "\\%c", (int)c); |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 116 | } else { |
| 117 | return maybe_write(out, &u8, 1) ? 1 : -1; |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 118 | } |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 119 | } else if ((flags & ESC_FLAGS) && c == '\\') { |
David Benjamin | 4635048 | 2022-06-16 14:03:12 -0400 | [diff] [blame] | 120 | // If any escape flags are set, also escape backslashes. |
David Benjamin | 23d6e4c | 2023-07-15 02:44:54 -0400 | [diff] [blame] | 121 | snprintf(buf, sizeof(buf), "\\%c", (int)c); |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 122 | } else { |
| 123 | return maybe_write(out, &u8, 1) ? 1 : -1; |
| 124 | } |
David Benjamin | ac57319 | 2022-05-10 20:46:03 -0400 | [diff] [blame] | 125 | |
David Benjamin | 1e63b0c | 2022-12-03 17:08:13 -0500 | [diff] [blame] | 126 | static_assert(sizeof(buf) < INT_MAX, "len may not fit in int"); |
| 127 | int len = (int)strlen(buf); |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 128 | return maybe_write(out, buf, len) ? len : -1; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 129 | } |
| 130 | |
David Benjamin | 4635048 | 2022-06-16 14:03:12 -0400 | [diff] [blame] | 131 | // This function sends each character in a buffer to do_esc_char(). It |
| 132 | // interprets the content formats and converts to or from UTF8 as |
| 133 | // appropriate. |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 134 | |
David Benjamin | 8b988b8 | 2022-04-13 18:57:49 +0200 | [diff] [blame] | 135 | static int do_buf(const unsigned char *buf, int buflen, int encoding, |
David Benjamin | 695b2e1 | 2022-06-23 16:38:15 -0400 | [diff] [blame] | 136 | unsigned long flags, char *quotes, BIO *out) { |
| 137 | int (*get_char)(CBS *cbs, uint32_t *out); |
| 138 | int get_char_error; |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 139 | switch (encoding) { |
David Benjamin | 8b988b8 | 2022-04-13 18:57:49 +0200 | [diff] [blame] | 140 | case MBSTRING_UNIV: |
David Benjamin | 4325d8c | 2023-08-24 18:17:14 -0400 | [diff] [blame] | 141 | get_char = CBS_get_utf32_be; |
David Benjamin | 695b2e1 | 2022-06-23 16:38:15 -0400 | [diff] [blame] | 142 | get_char_error = ASN1_R_INVALID_UNIVERSALSTRING; |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 143 | break; |
David Benjamin | 8b988b8 | 2022-04-13 18:57:49 +0200 | [diff] [blame] | 144 | case MBSTRING_BMP: |
David Benjamin | 4325d8c | 2023-08-24 18:17:14 -0400 | [diff] [blame] | 145 | get_char = CBS_get_ucs2_be; |
David Benjamin | 695b2e1 | 2022-06-23 16:38:15 -0400 | [diff] [blame] | 146 | get_char_error = ASN1_R_INVALID_BMPSTRING; |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 147 | break; |
David Benjamin | 695b2e1 | 2022-06-23 16:38:15 -0400 | [diff] [blame] | 148 | case MBSTRING_ASC: |
David Benjamin | 4325d8c | 2023-08-24 18:17:14 -0400 | [diff] [blame] | 149 | get_char = CBS_get_latin1; |
David Benjamin | 695b2e1 | 2022-06-23 16:38:15 -0400 | [diff] [blame] | 150 | get_char_error = ERR_R_INTERNAL_ERROR; // Should not be possible. |
| 151 | break; |
| 152 | case MBSTRING_UTF8: |
David Benjamin | 4325d8c | 2023-08-24 18:17:14 -0400 | [diff] [blame] | 153 | get_char = CBS_get_utf8; |
David Benjamin | 695b2e1 | 2022-06-23 16:38:15 -0400 | [diff] [blame] | 154 | get_char_error = ASN1_R_INVALID_UTF8STRING; |
| 155 | break; |
| 156 | default: |
| 157 | assert(0); |
| 158 | return -1; |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 159 | } |
| 160 | |
David Benjamin | 695b2e1 | 2022-06-23 16:38:15 -0400 | [diff] [blame] | 161 | CBS cbs; |
| 162 | CBS_init(&cbs, buf, buflen); |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 163 | int outlen = 0; |
David Benjamin | 695b2e1 | 2022-06-23 16:38:15 -0400 | [diff] [blame] | 164 | while (CBS_len(&cbs) != 0) { |
| 165 | const int is_first = CBS_data(&cbs) == buf; |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 166 | uint32_t c; |
David Benjamin | 695b2e1 | 2022-06-23 16:38:15 -0400 | [diff] [blame] | 167 | if (!get_char(&cbs, &c)) { |
| 168 | OPENSSL_PUT_ERROR(ASN1, get_char_error); |
| 169 | return -1; |
Steven Valdez | 537553f | 2018-05-04 14:03:44 -0400 | [diff] [blame] | 170 | } |
David Benjamin | 695b2e1 | 2022-06-23 16:38:15 -0400 | [diff] [blame] | 171 | const int is_last = CBS_len(&cbs) == 0; |
| 172 | if (flags & ASN1_STRFLGS_UTF8_CONVERT) { |
David Benjamin | 1783227 | 2022-06-24 16:58:53 -0400 | [diff] [blame] | 173 | uint8_t utf8_buf[6]; |
| 174 | CBB utf8_cbb; |
| 175 | CBB_init_fixed(&utf8_cbb, utf8_buf, sizeof(utf8_buf)); |
David Benjamin | 4325d8c | 2023-08-24 18:17:14 -0400 | [diff] [blame] | 176 | if (!CBB_add_utf8(&utf8_cbb, c)) { |
David Benjamin | 1783227 | 2022-06-24 16:58:53 -0400 | [diff] [blame] | 177 | OPENSSL_PUT_ERROR(ASN1, ERR_R_INTERNAL_ERROR); |
| 178 | return 1; |
| 179 | } |
| 180 | size_t utf8_len = CBB_len(&utf8_cbb); |
| 181 | for (size_t i = 0; i < utf8_len; i++) { |
| 182 | int len = do_esc_char(utf8_buf[i], flags, quotes, out, |
| 183 | is_first && i == 0, is_last && i == utf8_len - 1); |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 184 | if (len < 0) { |
| 185 | return -1; |
David Benjamin | 8b988b8 | 2022-04-13 18:57:49 +0200 | [diff] [blame] | 186 | } |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 187 | outlen += len; |
| 188 | } |
| 189 | } else { |
| 190 | int len = do_esc_char(c, flags, quotes, out, is_first, is_last); |
| 191 | if (len < 0) { |
| 192 | return -1; |
| 193 | } |
| 194 | outlen += len; |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 195 | } |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 196 | } |
| 197 | return outlen; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 198 | } |
| 199 | |
David Benjamin | 4635048 | 2022-06-16 14:03:12 -0400 | [diff] [blame] | 200 | // This function hex dumps a buffer of characters |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 201 | |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 202 | static int do_hex_dump(BIO *out, unsigned char *buf, int buflen) { |
| 203 | static const char hexdig[] = "0123456789ABCDEF"; |
| 204 | unsigned char *p, *q; |
| 205 | char hextmp[2]; |
| 206 | if (out) { |
| 207 | p = buf; |
| 208 | q = buf + buflen; |
| 209 | while (p != q) { |
| 210 | hextmp[0] = hexdig[*p >> 4]; |
| 211 | hextmp[1] = hexdig[*p & 0xf]; |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 212 | if (!maybe_write(out, hextmp, 2)) { |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 213 | return -1; |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 214 | } |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 215 | p++; |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 216 | } |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 217 | } |
| 218 | return buflen << 1; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 219 | } |
| 220 | |
David Benjamin | 4635048 | 2022-06-16 14:03:12 -0400 | [diff] [blame] | 221 | // "dump" a string. This is done when the type is unknown, or the flags |
| 222 | // request it. We can either dump the content octets or the entire DER |
| 223 | // encoding. This uses the RFC 2253 #01234 format. |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 224 | |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 225 | static int do_dump(unsigned long flags, BIO *out, const ASN1_STRING *str) { |
| 226 | if (!maybe_write(out, "#", 1)) { |
| 227 | return -1; |
| 228 | } |
David Benjamin | e3a3655 | 2021-08-09 00:28:09 -0400 | [diff] [blame] | 229 | |
David Benjamin | 4635048 | 2022-06-16 14:03:12 -0400 | [diff] [blame] | 230 | // If we don't dump DER encoding just dump content octets |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 231 | if (!(flags & ASN1_STRFLGS_DUMP_DER)) { |
| 232 | int outlen = do_hex_dump(out, str->data, str->length); |
David Benjamin | e3a3655 | 2021-08-09 00:28:09 -0400 | [diff] [blame] | 233 | if (outlen < 0) { |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 234 | return -1; |
David Benjamin | e3a3655 | 2021-08-09 00:28:09 -0400 | [diff] [blame] | 235 | } |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 236 | return outlen + 1; |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 237 | } |
| 238 | |
David Benjamin | 4635048 | 2022-06-16 14:03:12 -0400 | [diff] [blame] | 239 | // Placing the ASN1_STRING in a temporary ASN1_TYPE allows the DER encoding |
| 240 | // to readily obtained. |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 241 | ASN1_TYPE t; |
David Benjamin | 23d5842 | 2023-12-13 10:33:17 -0500 | [diff] [blame] | 242 | OPENSSL_memset(&t, 0, sizeof(ASN1_TYPE)); |
| 243 | asn1_type_set0_string(&t, (ASN1_STRING *)str); |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 244 | unsigned char *der_buf = NULL; |
| 245 | int der_len = i2d_ASN1_TYPE(&t, &der_buf); |
| 246 | if (der_len < 0) { |
| 247 | return -1; |
| 248 | } |
| 249 | int outlen = do_hex_dump(out, der_buf, der_len); |
| 250 | OPENSSL_free(der_buf); |
| 251 | if (outlen < 0) { |
| 252 | return -1; |
| 253 | } |
| 254 | return outlen + 1; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 255 | } |
| 256 | |
David Benjamin | 4635048 | 2022-06-16 14:03:12 -0400 | [diff] [blame] | 257 | // string_type_to_encoding returns the |MBSTRING_*| constant for the encoding |
| 258 | // used by the |ASN1_STRING| type |type|, or -1 if |tag| is not a string |
| 259 | // type. |
David Benjamin | 8b988b8 | 2022-04-13 18:57:49 +0200 | [diff] [blame] | 260 | static int string_type_to_encoding(int type) { |
David Benjamin | 4635048 | 2022-06-16 14:03:12 -0400 | [diff] [blame] | 261 | // This function is sometimes passed ASN.1 universal types and sometimes |
| 262 | // passed |ASN1_STRING| type values |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 263 | switch (type) { |
David Benjamin | 8b988b8 | 2022-04-13 18:57:49 +0200 | [diff] [blame] | 264 | case V_ASN1_UTF8STRING: |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 265 | return MBSTRING_UTF8; |
David Benjamin | 8b988b8 | 2022-04-13 18:57:49 +0200 | [diff] [blame] | 266 | case V_ASN1_NUMERICSTRING: |
| 267 | case V_ASN1_PRINTABLESTRING: |
| 268 | case V_ASN1_T61STRING: |
| 269 | case V_ASN1_IA5STRING: |
| 270 | case V_ASN1_UTCTIME: |
| 271 | case V_ASN1_GENERALIZEDTIME: |
| 272 | case V_ASN1_ISO64STRING: |
David Benjamin | 4635048 | 2022-06-16 14:03:12 -0400 | [diff] [blame] | 273 | // |MBSTRING_ASC| refers to Latin-1, not ASCII. |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 274 | return MBSTRING_ASC; |
David Benjamin | 8b988b8 | 2022-04-13 18:57:49 +0200 | [diff] [blame] | 275 | case V_ASN1_UNIVERSALSTRING: |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 276 | return MBSTRING_UNIV; |
David Benjamin | 8b988b8 | 2022-04-13 18:57:49 +0200 | [diff] [blame] | 277 | case V_ASN1_BMPSTRING: |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 278 | return MBSTRING_BMP; |
| 279 | } |
| 280 | return -1; |
David Benjamin | 8b988b8 | 2022-04-13 18:57:49 +0200 | [diff] [blame] | 281 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 282 | |
David Benjamin | 4635048 | 2022-06-16 14:03:12 -0400 | [diff] [blame] | 283 | // This is the main function, print out an ASN1_STRING taking note of various |
| 284 | // escape and display options. Returns number of characters written or -1 if |
| 285 | // an error occurred. |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 286 | |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 287 | int ASN1_STRING_print_ex(BIO *out, const ASN1_STRING *str, |
| 288 | unsigned long flags) { |
| 289 | int type = str->type; |
| 290 | int outlen = 0; |
| 291 | if (flags & ASN1_STRFLGS_SHOW_TYPE) { |
| 292 | const char *tagname = ASN1_tag2str(type); |
| 293 | outlen += strlen(tagname); |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 294 | if (!maybe_write(out, tagname, outlen) || !maybe_write(out, ":", 1)) { |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 295 | return -1; |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 296 | } |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 297 | outlen++; |
| 298 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 299 | |
David Benjamin | 4635048 | 2022-06-16 14:03:12 -0400 | [diff] [blame] | 300 | // Decide what to do with |str|, either dump the contents or display it. |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 301 | int encoding; |
| 302 | if (flags & ASN1_STRFLGS_DUMP_ALL) { |
David Benjamin | 4635048 | 2022-06-16 14:03:12 -0400 | [diff] [blame] | 303 | // Dump everything. |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 304 | encoding = -1; |
| 305 | } else if (flags & ASN1_STRFLGS_IGNORE_TYPE) { |
David Benjamin | 4635048 | 2022-06-16 14:03:12 -0400 | [diff] [blame] | 306 | // Ignore the string type and interpret the contents as Latin-1. |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 307 | encoding = MBSTRING_ASC; |
| 308 | } else { |
| 309 | encoding = string_type_to_encoding(type); |
| 310 | if (encoding == -1 && (flags & ASN1_STRFLGS_DUMP_UNKNOWN) == 0) { |
| 311 | encoding = MBSTRING_ASC; |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 312 | } |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 313 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 314 | |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 315 | if (encoding == -1) { |
| 316 | int len = do_dump(flags, out, str); |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 317 | if (len < 0) { |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 318 | return -1; |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 319 | } |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 320 | outlen += len; |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 321 | return outlen; |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 322 | } |
| 323 | |
David Benjamin | 4635048 | 2022-06-16 14:03:12 -0400 | [diff] [blame] | 324 | // Measure the length. |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 325 | char quotes = 0; |
David Benjamin | 695b2e1 | 2022-06-23 16:38:15 -0400 | [diff] [blame] | 326 | int len = do_buf(str->data, str->length, encoding, flags, "es, NULL); |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 327 | if (len < 0) { |
| 328 | return -1; |
| 329 | } |
| 330 | outlen += len; |
| 331 | if (quotes) { |
| 332 | outlen += 2; |
| 333 | } |
| 334 | if (!out) { |
| 335 | return outlen; |
| 336 | } |
| 337 | |
David Benjamin | 4635048 | 2022-06-16 14:03:12 -0400 | [diff] [blame] | 338 | // Encode the value. |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 339 | if ((quotes && !maybe_write(out, "\"", 1)) || |
David Benjamin | 695b2e1 | 2022-06-23 16:38:15 -0400 | [diff] [blame] | 340 | do_buf(str->data, str->length, encoding, flags, NULL, out) < 0 || |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 341 | (quotes && !maybe_write(out, "\"", 1))) { |
| 342 | return -1; |
| 343 | } |
| 344 | return outlen; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 345 | } |
| 346 | |
David Benjamin | 1201c9a | 2021-08-08 17:28:12 -0400 | [diff] [blame] | 347 | int ASN1_STRING_print_ex_fp(FILE *fp, const ASN1_STRING *str, |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 348 | unsigned long flags) { |
| 349 | BIO *bio = NULL; |
| 350 | if (fp != NULL) { |
David Benjamin | 4635048 | 2022-06-16 14:03:12 -0400 | [diff] [blame] | 351 | // If |fp| is NULL, this function returns the number of bytes without |
| 352 | // writing. |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 353 | bio = BIO_new_fp(fp, BIO_NOCLOSE); |
| 354 | if (bio == NULL) { |
| 355 | return -1; |
David Benjamin | 1201c9a | 2021-08-08 17:28:12 -0400 | [diff] [blame] | 356 | } |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 357 | } |
| 358 | int ret = ASN1_STRING_print_ex(bio, str, flags); |
| 359 | BIO_free(bio); |
| 360 | return ret; |
| 361 | } |
| 362 | |
| 363 | int ASN1_STRING_to_UTF8(unsigned char **out, const ASN1_STRING *in) { |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 364 | if (!in) { |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 365 | return -1; |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 366 | } |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 367 | int mbflag = string_type_to_encoding(in->type); |
| 368 | if (mbflag == -1) { |
| 369 | OPENSSL_PUT_ERROR(ASN1, ASN1_R_UNKNOWN_TAG); |
| 370 | return -1; |
| 371 | } |
| 372 | ASN1_STRING stmp, *str = &stmp; |
| 373 | stmp.data = NULL; |
| 374 | stmp.length = 0; |
| 375 | stmp.flags = 0; |
| 376 | int ret = |
| 377 | ASN1_mbstring_copy(&str, in->data, in->length, mbflag, B_ASN1_UTF8STRING); |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 378 | if (ret < 0) { |
David Benjamin | 1201c9a | 2021-08-08 17:28:12 -0400 | [diff] [blame] | 379 | return ret; |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 380 | } |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 381 | *out = stmp.data; |
| 382 | return stmp.length; |
David Benjamin | 1201c9a | 2021-08-08 17:28:12 -0400 | [diff] [blame] | 383 | } |
| 384 | |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 385 | int ASN1_STRING_print(BIO *bp, const ASN1_STRING *v) { |
| 386 | int i, n; |
| 387 | char buf[80]; |
| 388 | const char *p; |
David Benjamin | 07a6628 | 2021-08-08 17:45:05 -0400 | [diff] [blame] | 389 | |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 390 | if (v == NULL) { |
David Benjamin | 56eeb20 | 2022-06-22 13:59:05 -0400 | [diff] [blame] | 391 | return 0; |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 392 | } |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 393 | n = 0; |
| 394 | p = (const char *)v->data; |
| 395 | for (i = 0; i < v->length; i++) { |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 396 | if ((p[i] > '~') || ((p[i] < ' ') && (p[i] != '\n') && (p[i] != '\r'))) { |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 397 | buf[n] = '.'; |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 398 | } else { |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 399 | buf[n] = p[i]; |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 400 | } |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 401 | n++; |
| 402 | if (n >= 80) { |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 403 | if (BIO_write(bp, buf, n) <= 0) { |
David Benjamin | 56eeb20 | 2022-06-22 13:59:05 -0400 | [diff] [blame] | 404 | return 0; |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 405 | } |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 406 | n = 0; |
| 407 | } |
| 408 | } |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 409 | if (n > 0) { |
| 410 | if (BIO_write(bp, buf, n) <= 0) { |
David Benjamin | 56eeb20 | 2022-06-22 13:59:05 -0400 | [diff] [blame] | 411 | return 0; |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 412 | } |
| 413 | } |
David Benjamin | 56eeb20 | 2022-06-22 13:59:05 -0400 | [diff] [blame] | 414 | return 1; |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 415 | } |
| 416 | |
| 417 | int ASN1_TIME_print(BIO *bp, const ASN1_TIME *tm) { |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 418 | if (tm->type == V_ASN1_UTCTIME) { |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 419 | return ASN1_UTCTIME_print(bp, tm); |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 420 | } |
| 421 | if (tm->type == V_ASN1_GENERALIZEDTIME) { |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 422 | return ASN1_GENERALIZEDTIME_print(bp, tm); |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 423 | } |
David Benjamin | fdeb4aa | 2022-06-22 14:18:20 -0400 | [diff] [blame] | 424 | BIO_puts(bp, "Bad time value"); |
David Benjamin | 56eeb20 | 2022-06-22 13:59:05 -0400 | [diff] [blame] | 425 | return 0; |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 426 | } |
| 427 | |
| 428 | static const char *const mon[12] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", |
| 429 | "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"}; |
| 430 | |
| 431 | int ASN1_GENERALIZEDTIME_print(BIO *bp, const ASN1_GENERALIZEDTIME *tm) { |
David Benjamin | fdeb4aa | 2022-06-22 14:18:20 -0400 | [diff] [blame] | 432 | CBS cbs; |
| 433 | CBS_init(&cbs, tm->data, tm->length); |
| 434 | struct tm utc; |
| 435 | if (!CBS_parse_generalized_time(&cbs, &utc, /*allow_timezone_offset=*/0)) { |
| 436 | BIO_puts(bp, "Bad time value"); |
David Benjamin | 07a6628 | 2021-08-08 17:45:05 -0400 | [diff] [blame] | 437 | return 0; |
| 438 | } |
| 439 | |
David Benjamin | fdeb4aa | 2022-06-22 14:18:20 -0400 | [diff] [blame] | 440 | return BIO_printf(bp, "%s %2d %02d:%02d:%02d %d GMT", mon[utc.tm_mon], |
| 441 | utc.tm_mday, utc.tm_hour, utc.tm_min, utc.tm_sec, |
| 442 | utc.tm_year + 1900) > 0; |
David Benjamin | 07a6628 | 2021-08-08 17:45:05 -0400 | [diff] [blame] | 443 | } |
| 444 | |
| 445 | int ASN1_UTCTIME_print(BIO *bp, const ASN1_UTCTIME *tm) { |
David Benjamin | fdeb4aa | 2022-06-22 14:18:20 -0400 | [diff] [blame] | 446 | CBS cbs; |
| 447 | CBS_init(&cbs, tm->data, tm->length); |
| 448 | struct tm utc; |
| 449 | if (!CBS_parse_utc_time(&cbs, &utc, /*allow_timezone_offset=*/0)) { |
| 450 | BIO_puts(bp, "Bad time value"); |
| 451 | return 0; |
David Benjamin | 07a6628 | 2021-08-08 17:45:05 -0400 | [diff] [blame] | 452 | } |
| 453 | |
David Benjamin | fdeb4aa | 2022-06-22 14:18:20 -0400 | [diff] [blame] | 454 | return BIO_printf(bp, "%s %2d %02d:%02d:%02d %d GMT", mon[utc.tm_mon], |
| 455 | utc.tm_mday, utc.tm_hour, utc.tm_min, utc.tm_sec, |
| 456 | utc.tm_year + 1900) > 0; |
David Benjamin | 07a6628 | 2021-08-08 17:45:05 -0400 | [diff] [blame] | 457 | } |