VirtualBox

source: vbox/trunk/src/libs/openssl-3.3.2/crypto/asn1/a_strex.c@ 108358

最後變更 在這個檔案從108358是 108206,由 vboxsync 提交於 6 週 前

openssl-3.3.2: Exported all files to OSE and removed .scm-settings ​bugref:10757

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

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette