blob: 516f7df54d5a87b1b7efbe0a045d807f3b6f0ceb [file] [log] [blame]
Adam Langley95c29f32014-06-20 12:00:00 -07001/* 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 Benjamin0dcbc6e2021-08-08 17:33:57 -040057#include <openssl/asn1.h>
Adam Langley95c29f32014-06-20 12:00:00 -070058
David Benjamin8b988b82022-04-13 18:57:49 +020059#include <assert.h>
David Benjamin07a66282021-08-08 17:45:05 -040060#include <ctype.h>
David Benjamin2b63add2017-12-04 17:05:54 -050061#include <inttypes.h>
David Benjamin1e63b0c2022-12-03 17:08:13 -050062#include <limits.h>
Adam Langley2b2d66d2015-01-30 17:08:37 -080063#include <string.h>
David Benjaminfdeb4aa2022-06-22 14:18:20 -040064#include <time.h>
Adam Langley2b2d66d2015-01-30 17:08:37 -080065
David Benjamin0dcbc6e2021-08-08 17:33:57 -040066#include <openssl/bio.h>
David Benjaminfdeb4aa2022-06-22 14:18:20 -040067#include <openssl/bytestring.h>
Adam Langley95c29f32014-06-20 12:00:00 -070068#include <openssl/mem.h>
Adam Langley95c29f32014-06-20 12:00:00 -070069
David Benjamin695b2e12022-06-23 16:38:15 -040070#include "../bytestring/internal.h"
David Benjamin23d58422023-12-13 10:33:17 -050071#include "../internal.h"
David Benjamin0dcbc6e2021-08-08 17:33:57 -040072#include "internal.h"
Adam Langley95c29f32014-06-20 12:00:00 -070073
Adam Langley95c29f32014-06-20 12:00:00 -070074
David Benjaminac573192022-05-10 20:46:03 -040075#define ESC_FLAGS \
76 (ASN1_STRFLGS_ESC_2253 | ASN1_STRFLGS_ESC_QUOTE | ASN1_STRFLGS_ESC_CTRL | \
77 ASN1_STRFLGS_ESC_MSB)
Adam Langley95c29f32014-06-20 12:00:00 -070078
David Benjamin260a10c2022-06-16 13:58:28 -040079static int maybe_write(BIO *out, const void *buf, int len) {
David Benjamin46350482022-06-16 14:03:12 -040080 // If |out| is NULL, ignore the output but report the length.
David Benjamin260a10c2022-06-16 13:58:28 -040081 return out == NULL || BIO_write(out, buf, len) == len;
Adam Langley95c29f32014-06-20 12:00:00 -070082}
83
David Benjamin260a10c2022-06-16 13:58:28 -040084static int is_control_character(unsigned char c) { return c < 32 || c == 127; }
Adam Langley95c29f32014-06-20 12:00:00 -070085
David Benjaminac573192022-05-10 20:46:03 -040086static int do_esc_char(uint32_t c, unsigned long flags, char *do_quotes,
David Benjamin260a10c2022-06-16 13:58:28 -040087 BIO *out, int is_first, int is_last) {
David Benjamin46350482022-06-16 14:03:12 -040088 // |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 Benjamin260a10c2022-06-16 13:58:28 -040091 unsigned char u8 = (unsigned char)c;
92 if (c > 0xffff) {
David Benjamin23d6e4c2023-07-15 02:44:54 -040093 snprintf(buf, sizeof(buf), "\\W%08" PRIX32, c);
David Benjamin260a10c2022-06-16 13:58:28 -040094 } else if (c > 0xff) {
David Benjamin23d6e4c2023-07-15 02:44:54 -040095 snprintf(buf, sizeof(buf), "\\U%04" PRIX32, c);
David Benjamin260a10c2022-06-16 13:58:28 -040096 } else if ((flags & ASN1_STRFLGS_ESC_MSB) && c > 0x7f) {
David Benjamin23d6e4c2023-07-15 02:44:54 -040097 snprintf(buf, sizeof(buf), "\\%02X", c);
David Benjamin260a10c2022-06-16 13:58:28 -040098 } else if ((flags & ASN1_STRFLGS_ESC_CTRL) && is_control_character(c)) {
David Benjamin23d6e4c2023-07-15 02:44:54 -040099 snprintf(buf, sizeof(buf), "\\%02X", c);
David Benjamin260a10c2022-06-16 13:58:28 -0400100 } else if (flags & ASN1_STRFLGS_ESC_2253) {
David Benjamin46350482022-06-16 14:03:12 -0400101 // See RFC 2253, sections 2.4 and 4.
David Benjamin260a10c2022-06-16 13:58:28 -0400102 if (c == '\\' || c == '"') {
David Benjamin46350482022-06-16 14:03:12 -0400103 // Quotes and backslashes are always escaped, quoted or not.
David Benjamin23d6e4c2023-07-15 02:44:54 -0400104 snprintf(buf, sizeof(buf), "\\%c", (int)c);
David Benjamin260a10c2022-06-16 13:58:28 -0400105 } else if (c == ',' || c == '+' || c == '<' || c == '>' || c == ';' ||
106 (is_first && (c == ' ' || c == '#')) ||
107 (is_last && (c == ' '))) {
108 if (flags & ASN1_STRFLGS_ESC_QUOTE) {
David Benjamin46350482022-06-16 14:03:12 -0400109 // No need to escape, just tell the caller to quote.
David Benjamin260a10c2022-06-16 13:58:28 -0400110 if (do_quotes != NULL) {
111 *do_quotes = 1;
Adam Langley57707c72016-01-14 11:25:12 -0800112 }
David Benjaminac573192022-05-10 20:46:03 -0400113 return maybe_write(out, &u8, 1) ? 1 : -1;
David Benjamin260a10c2022-06-16 13:58:28 -0400114 }
David Benjamin23d6e4c2023-07-15 02:44:54 -0400115 snprintf(buf, sizeof(buf), "\\%c", (int)c);
David Benjamin260a10c2022-06-16 13:58:28 -0400116 } else {
117 return maybe_write(out, &u8, 1) ? 1 : -1;
Adam Langley57707c72016-01-14 11:25:12 -0800118 }
David Benjamin260a10c2022-06-16 13:58:28 -0400119 } else if ((flags & ESC_FLAGS) && c == '\\') {
David Benjamin46350482022-06-16 14:03:12 -0400120 // If any escape flags are set, also escape backslashes.
David Benjamin23d6e4c2023-07-15 02:44:54 -0400121 snprintf(buf, sizeof(buf), "\\%c", (int)c);
David Benjamin260a10c2022-06-16 13:58:28 -0400122 } else {
123 return maybe_write(out, &u8, 1) ? 1 : -1;
124 }
David Benjaminac573192022-05-10 20:46:03 -0400125
David Benjamin1e63b0c2022-12-03 17:08:13 -0500126 static_assert(sizeof(buf) < INT_MAX, "len may not fit in int");
127 int len = (int)strlen(buf);
David Benjamin260a10c2022-06-16 13:58:28 -0400128 return maybe_write(out, buf, len) ? len : -1;
Adam Langley95c29f32014-06-20 12:00:00 -0700129}
130
David Benjamin46350482022-06-16 14:03:12 -0400131// 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 Langley95c29f32014-06-20 12:00:00 -0700134
David Benjamin8b988b82022-04-13 18:57:49 +0200135static int do_buf(const unsigned char *buf, int buflen, int encoding,
David Benjamin695b2e12022-06-23 16:38:15 -0400136 unsigned long flags, char *quotes, BIO *out) {
137 int (*get_char)(CBS *cbs, uint32_t *out);
138 int get_char_error;
David Benjamin260a10c2022-06-16 13:58:28 -0400139 switch (encoding) {
David Benjamin8b988b82022-04-13 18:57:49 +0200140 case MBSTRING_UNIV:
David Benjamin4325d8c2023-08-24 18:17:14 -0400141 get_char = CBS_get_utf32_be;
David Benjamin695b2e12022-06-23 16:38:15 -0400142 get_char_error = ASN1_R_INVALID_UNIVERSALSTRING;
David Benjamin260a10c2022-06-16 13:58:28 -0400143 break;
David Benjamin8b988b82022-04-13 18:57:49 +0200144 case MBSTRING_BMP:
David Benjamin4325d8c2023-08-24 18:17:14 -0400145 get_char = CBS_get_ucs2_be;
David Benjamin695b2e12022-06-23 16:38:15 -0400146 get_char_error = ASN1_R_INVALID_BMPSTRING;
David Benjamin260a10c2022-06-16 13:58:28 -0400147 break;
David Benjamin695b2e12022-06-23 16:38:15 -0400148 case MBSTRING_ASC:
David Benjamin4325d8c2023-08-24 18:17:14 -0400149 get_char = CBS_get_latin1;
David Benjamin695b2e12022-06-23 16:38:15 -0400150 get_char_error = ERR_R_INTERNAL_ERROR; // Should not be possible.
151 break;
152 case MBSTRING_UTF8:
David Benjamin4325d8c2023-08-24 18:17:14 -0400153 get_char = CBS_get_utf8;
David Benjamin695b2e12022-06-23 16:38:15 -0400154 get_char_error = ASN1_R_INVALID_UTF8STRING;
155 break;
156 default:
157 assert(0);
158 return -1;
David Benjamin260a10c2022-06-16 13:58:28 -0400159 }
160
David Benjamin695b2e12022-06-23 16:38:15 -0400161 CBS cbs;
162 CBS_init(&cbs, buf, buflen);
David Benjamin260a10c2022-06-16 13:58:28 -0400163 int outlen = 0;
David Benjamin695b2e12022-06-23 16:38:15 -0400164 while (CBS_len(&cbs) != 0) {
165 const int is_first = CBS_data(&cbs) == buf;
David Benjamin260a10c2022-06-16 13:58:28 -0400166 uint32_t c;
David Benjamin695b2e12022-06-23 16:38:15 -0400167 if (!get_char(&cbs, &c)) {
168 OPENSSL_PUT_ERROR(ASN1, get_char_error);
169 return -1;
Steven Valdez537553f2018-05-04 14:03:44 -0400170 }
David Benjamin695b2e12022-06-23 16:38:15 -0400171 const int is_last = CBS_len(&cbs) == 0;
172 if (flags & ASN1_STRFLGS_UTF8_CONVERT) {
David Benjamin17832272022-06-24 16:58:53 -0400173 uint8_t utf8_buf[6];
174 CBB utf8_cbb;
175 CBB_init_fixed(&utf8_cbb, utf8_buf, sizeof(utf8_buf));
David Benjamin4325d8c2023-08-24 18:17:14 -0400176 if (!CBB_add_utf8(&utf8_cbb, c)) {
David Benjamin17832272022-06-24 16:58:53 -0400177 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 Benjamin260a10c2022-06-16 13:58:28 -0400184 if (len < 0) {
185 return -1;
David Benjamin8b988b82022-04-13 18:57:49 +0200186 }
David Benjamin260a10c2022-06-16 13:58:28 -0400187 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 Langley57707c72016-01-14 11:25:12 -0800195 }
David Benjamin260a10c2022-06-16 13:58:28 -0400196 }
197 return outlen;
Adam Langley95c29f32014-06-20 12:00:00 -0700198}
199
David Benjamin46350482022-06-16 14:03:12 -0400200// This function hex dumps a buffer of characters
Adam Langley95c29f32014-06-20 12:00:00 -0700201
David Benjamin260a10c2022-06-16 13:58:28 -0400202static 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 Benjaminc0b87a02022-06-16 14:02:15 -0400212 if (!maybe_write(out, hextmp, 2)) {
David Benjamin260a10c2022-06-16 13:58:28 -0400213 return -1;
David Benjaminc0b87a02022-06-16 14:02:15 -0400214 }
David Benjamin260a10c2022-06-16 13:58:28 -0400215 p++;
Adam Langley57707c72016-01-14 11:25:12 -0800216 }
David Benjamin260a10c2022-06-16 13:58:28 -0400217 }
218 return buflen << 1;
Adam Langley95c29f32014-06-20 12:00:00 -0700219}
220
David Benjamin46350482022-06-16 14:03:12 -0400221// "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 Langley95c29f32014-06-20 12:00:00 -0700224
David Benjamin260a10c2022-06-16 13:58:28 -0400225static int do_dump(unsigned long flags, BIO *out, const ASN1_STRING *str) {
226 if (!maybe_write(out, "#", 1)) {
227 return -1;
228 }
David Benjamine3a36552021-08-09 00:28:09 -0400229
David Benjamin46350482022-06-16 14:03:12 -0400230 // If we don't dump DER encoding just dump content octets
David Benjamin260a10c2022-06-16 13:58:28 -0400231 if (!(flags & ASN1_STRFLGS_DUMP_DER)) {
232 int outlen = do_hex_dump(out, str->data, str->length);
David Benjamine3a36552021-08-09 00:28:09 -0400233 if (outlen < 0) {
David Benjamin260a10c2022-06-16 13:58:28 -0400234 return -1;
David Benjamine3a36552021-08-09 00:28:09 -0400235 }
Adam Langley57707c72016-01-14 11:25:12 -0800236 return outlen + 1;
David Benjamin260a10c2022-06-16 13:58:28 -0400237 }
238
David Benjamin46350482022-06-16 14:03:12 -0400239 // Placing the ASN1_STRING in a temporary ASN1_TYPE allows the DER encoding
240 // to readily obtained.
David Benjamin260a10c2022-06-16 13:58:28 -0400241 ASN1_TYPE t;
David Benjamin23d58422023-12-13 10:33:17 -0500242 OPENSSL_memset(&t, 0, sizeof(ASN1_TYPE));
243 asn1_type_set0_string(&t, (ASN1_STRING *)str);
David Benjamin260a10c2022-06-16 13:58:28 -0400244 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 Langley95c29f32014-06-20 12:00:00 -0700255}
256
David Benjamin46350482022-06-16 14:03:12 -0400257// 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 Benjamin8b988b82022-04-13 18:57:49 +0200260static int string_type_to_encoding(int type) {
David Benjamin46350482022-06-16 14:03:12 -0400261 // This function is sometimes passed ASN.1 universal types and sometimes
262 // passed |ASN1_STRING| type values
David Benjamin260a10c2022-06-16 13:58:28 -0400263 switch (type) {
David Benjamin8b988b82022-04-13 18:57:49 +0200264 case V_ASN1_UTF8STRING:
David Benjamin260a10c2022-06-16 13:58:28 -0400265 return MBSTRING_UTF8;
David Benjamin8b988b82022-04-13 18:57:49 +0200266 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 Benjamin46350482022-06-16 14:03:12 -0400273 // |MBSTRING_ASC| refers to Latin-1, not ASCII.
David Benjamin260a10c2022-06-16 13:58:28 -0400274 return MBSTRING_ASC;
David Benjamin8b988b82022-04-13 18:57:49 +0200275 case V_ASN1_UNIVERSALSTRING:
David Benjamin260a10c2022-06-16 13:58:28 -0400276 return MBSTRING_UNIV;
David Benjamin8b988b82022-04-13 18:57:49 +0200277 case V_ASN1_BMPSTRING:
David Benjamin260a10c2022-06-16 13:58:28 -0400278 return MBSTRING_BMP;
279 }
280 return -1;
David Benjamin8b988b82022-04-13 18:57:49 +0200281}
Adam Langley95c29f32014-06-20 12:00:00 -0700282
David Benjamin46350482022-06-16 14:03:12 -0400283// 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 Langley95c29f32014-06-20 12:00:00 -0700286
David Benjamin260a10c2022-06-16 13:58:28 -0400287int 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 Benjaminc0b87a02022-06-16 14:02:15 -0400294 if (!maybe_write(out, tagname, outlen) || !maybe_write(out, ":", 1)) {
David Benjamin260a10c2022-06-16 13:58:28 -0400295 return -1;
David Benjaminc0b87a02022-06-16 14:02:15 -0400296 }
David Benjamin260a10c2022-06-16 13:58:28 -0400297 outlen++;
298 }
Adam Langley95c29f32014-06-20 12:00:00 -0700299
David Benjamin46350482022-06-16 14:03:12 -0400300 // Decide what to do with |str|, either dump the contents or display it.
David Benjamin260a10c2022-06-16 13:58:28 -0400301 int encoding;
302 if (flags & ASN1_STRFLGS_DUMP_ALL) {
David Benjamin46350482022-06-16 14:03:12 -0400303 // Dump everything.
David Benjamin260a10c2022-06-16 13:58:28 -0400304 encoding = -1;
305 } else if (flags & ASN1_STRFLGS_IGNORE_TYPE) {
David Benjamin46350482022-06-16 14:03:12 -0400306 // Ignore the string type and interpret the contents as Latin-1.
David Benjamin260a10c2022-06-16 13:58:28 -0400307 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 Langley57707c72016-01-14 11:25:12 -0800312 }
David Benjamin260a10c2022-06-16 13:58:28 -0400313 }
Adam Langley95c29f32014-06-20 12:00:00 -0700314
David Benjamin260a10c2022-06-16 13:58:28 -0400315 if (encoding == -1) {
316 int len = do_dump(flags, out, str);
David Benjaminc0b87a02022-06-16 14:02:15 -0400317 if (len < 0) {
David Benjamin260a10c2022-06-16 13:58:28 -0400318 return -1;
David Benjaminc0b87a02022-06-16 14:02:15 -0400319 }
Adam Langley57707c72016-01-14 11:25:12 -0800320 outlen += len;
Adam Langley57707c72016-01-14 11:25:12 -0800321 return outlen;
David Benjamin260a10c2022-06-16 13:58:28 -0400322 }
323
David Benjamin46350482022-06-16 14:03:12 -0400324 // Measure the length.
David Benjamin260a10c2022-06-16 13:58:28 -0400325 char quotes = 0;
David Benjamin695b2e12022-06-23 16:38:15 -0400326 int len = do_buf(str->data, str->length, encoding, flags, &quotes, NULL);
David Benjamin260a10c2022-06-16 13:58:28 -0400327 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 Benjamin46350482022-06-16 14:03:12 -0400338 // Encode the value.
David Benjamin260a10c2022-06-16 13:58:28 -0400339 if ((quotes && !maybe_write(out, "\"", 1)) ||
David Benjamin695b2e12022-06-23 16:38:15 -0400340 do_buf(str->data, str->length, encoding, flags, NULL, out) < 0 ||
David Benjamin260a10c2022-06-16 13:58:28 -0400341 (quotes && !maybe_write(out, "\"", 1))) {
342 return -1;
343 }
344 return outlen;
Adam Langley95c29f32014-06-20 12:00:00 -0700345}
346
David Benjamin1201c9a2021-08-08 17:28:12 -0400347int ASN1_STRING_print_ex_fp(FILE *fp, const ASN1_STRING *str,
David Benjamin260a10c2022-06-16 13:58:28 -0400348 unsigned long flags) {
349 BIO *bio = NULL;
350 if (fp != NULL) {
David Benjamin46350482022-06-16 14:03:12 -0400351 // If |fp| is NULL, this function returns the number of bytes without
352 // writing.
David Benjamin260a10c2022-06-16 13:58:28 -0400353 bio = BIO_new_fp(fp, BIO_NOCLOSE);
354 if (bio == NULL) {
355 return -1;
David Benjamin1201c9a2021-08-08 17:28:12 -0400356 }
David Benjamin260a10c2022-06-16 13:58:28 -0400357 }
358 int ret = ASN1_STRING_print_ex(bio, str, flags);
359 BIO_free(bio);
360 return ret;
361}
362
363int ASN1_STRING_to_UTF8(unsigned char **out, const ASN1_STRING *in) {
David Benjaminc0b87a02022-06-16 14:02:15 -0400364 if (!in) {
David Benjamin260a10c2022-06-16 13:58:28 -0400365 return -1;
David Benjaminc0b87a02022-06-16 14:02:15 -0400366 }
David Benjamin260a10c2022-06-16 13:58:28 -0400367 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 Benjaminc0b87a02022-06-16 14:02:15 -0400378 if (ret < 0) {
David Benjamin1201c9a2021-08-08 17:28:12 -0400379 return ret;
David Benjaminc0b87a02022-06-16 14:02:15 -0400380 }
David Benjamin260a10c2022-06-16 13:58:28 -0400381 *out = stmp.data;
382 return stmp.length;
David Benjamin1201c9a2021-08-08 17:28:12 -0400383}
384
David Benjamin260a10c2022-06-16 13:58:28 -0400385int ASN1_STRING_print(BIO *bp, const ASN1_STRING *v) {
386 int i, n;
387 char buf[80];
388 const char *p;
David Benjamin07a66282021-08-08 17:45:05 -0400389
David Benjaminc0b87a02022-06-16 14:02:15 -0400390 if (v == NULL) {
David Benjamin56eeb202022-06-22 13:59:05 -0400391 return 0;
David Benjaminc0b87a02022-06-16 14:02:15 -0400392 }
David Benjamin260a10c2022-06-16 13:58:28 -0400393 n = 0;
394 p = (const char *)v->data;
395 for (i = 0; i < v->length; i++) {
David Benjaminc0b87a02022-06-16 14:02:15 -0400396 if ((p[i] > '~') || ((p[i] < ' ') && (p[i] != '\n') && (p[i] != '\r'))) {
David Benjamin260a10c2022-06-16 13:58:28 -0400397 buf[n] = '.';
David Benjaminc0b87a02022-06-16 14:02:15 -0400398 } else {
David Benjamin260a10c2022-06-16 13:58:28 -0400399 buf[n] = p[i];
David Benjaminc0b87a02022-06-16 14:02:15 -0400400 }
David Benjamin260a10c2022-06-16 13:58:28 -0400401 n++;
402 if (n >= 80) {
David Benjaminc0b87a02022-06-16 14:02:15 -0400403 if (BIO_write(bp, buf, n) <= 0) {
David Benjamin56eeb202022-06-22 13:59:05 -0400404 return 0;
David Benjaminc0b87a02022-06-16 14:02:15 -0400405 }
David Benjamin260a10c2022-06-16 13:58:28 -0400406 n = 0;
407 }
408 }
David Benjaminc0b87a02022-06-16 14:02:15 -0400409 if (n > 0) {
410 if (BIO_write(bp, buf, n) <= 0) {
David Benjamin56eeb202022-06-22 13:59:05 -0400411 return 0;
David Benjaminc0b87a02022-06-16 14:02:15 -0400412 }
413 }
David Benjamin56eeb202022-06-22 13:59:05 -0400414 return 1;
David Benjamin260a10c2022-06-16 13:58:28 -0400415}
416
417int ASN1_TIME_print(BIO *bp, const ASN1_TIME *tm) {
David Benjaminc0b87a02022-06-16 14:02:15 -0400418 if (tm->type == V_ASN1_UTCTIME) {
David Benjamin260a10c2022-06-16 13:58:28 -0400419 return ASN1_UTCTIME_print(bp, tm);
David Benjaminc0b87a02022-06-16 14:02:15 -0400420 }
421 if (tm->type == V_ASN1_GENERALIZEDTIME) {
David Benjamin260a10c2022-06-16 13:58:28 -0400422 return ASN1_GENERALIZEDTIME_print(bp, tm);
David Benjaminc0b87a02022-06-16 14:02:15 -0400423 }
David Benjaminfdeb4aa2022-06-22 14:18:20 -0400424 BIO_puts(bp, "Bad time value");
David Benjamin56eeb202022-06-22 13:59:05 -0400425 return 0;
David Benjamin260a10c2022-06-16 13:58:28 -0400426}
427
428static const char *const mon[12] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
429 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
430
431int ASN1_GENERALIZEDTIME_print(BIO *bp, const ASN1_GENERALIZEDTIME *tm) {
David Benjaminfdeb4aa2022-06-22 14:18:20 -0400432 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 Benjamin07a66282021-08-08 17:45:05 -0400437 return 0;
438 }
439
David Benjaminfdeb4aa2022-06-22 14:18:20 -0400440 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 Benjamin07a66282021-08-08 17:45:05 -0400443}
444
445int ASN1_UTCTIME_print(BIO *bp, const ASN1_UTCTIME *tm) {
David Benjaminfdeb4aa2022-06-22 14:18:20 -0400446 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 Benjamin07a66282021-08-08 17:45:05 -0400452 }
453
David Benjaminfdeb4aa2022-06-22 14:18:20 -0400454 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 Benjamin07a66282021-08-08 17:45:05 -0400457}