/src/openssl/crypto/asn1/a_print.c
Line | Count | Source |
1 | | /* |
2 | | * Copyright 1995-2025 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 "crypto/ctype.h" |
12 | | #include "internal/cryptlib.h" |
13 | | #include <openssl/asn1.h> |
14 | | |
15 | | #include <crypto/asn1.h> |
16 | | |
17 | | int ASN1_PRINTABLE_type(const unsigned char *s, int len) |
18 | 0 | { |
19 | 0 | int c; |
20 | 0 | int ia5 = 0; |
21 | 0 | int t61 = 0; |
22 | |
|
23 | 0 | if (s == NULL) |
24 | 0 | return V_ASN1_PRINTABLESTRING; |
25 | | |
26 | 0 | if (len < 0) |
27 | 0 | len = (int)strlen((const char *)s); |
28 | |
|
29 | 0 | while (len-- > 0) { |
30 | 0 | c = *(s++); |
31 | 0 | if (!ossl_isasn1print(c)) |
32 | 0 | ia5 = 1; |
33 | 0 | if (!ossl_isascii(c)) |
34 | 0 | t61 = 1; |
35 | 0 | } |
36 | 0 | if (t61) |
37 | 0 | return V_ASN1_T61STRING; |
38 | 0 | if (ia5) |
39 | 0 | return V_ASN1_IA5STRING; |
40 | 0 | return V_ASN1_PRINTABLESTRING; |
41 | 0 | } |
42 | | |
43 | | int ASN1_UNIVERSALSTRING_to_string(ASN1_UNIVERSALSTRING *s) |
44 | 0 | { |
45 | 0 | int i; |
46 | 0 | unsigned char *p; |
47 | |
|
48 | 0 | if (s->type != V_ASN1_UNIVERSALSTRING) |
49 | 0 | return 0; |
50 | 0 | if ((s->length % 4) != 0) |
51 | 0 | return 0; |
52 | 0 | p = s->data; |
53 | 0 | for (i = 0; i < s->length; i += 4) { |
54 | 0 | if ((p[0] != '\0') || (p[1] != '\0') || (p[2] != '\0')) |
55 | 0 | break; |
56 | 0 | else |
57 | 0 | p += 4; |
58 | 0 | } |
59 | 0 | if (i < s->length) |
60 | 0 | return 0; |
61 | 0 | p = s->data; |
62 | 0 | for (i = 3; i < s->length; i += 4) { |
63 | 0 | *(p++) = s->data[i]; |
64 | 0 | } |
65 | 0 | *(p) = '\0'; |
66 | 0 | s->length /= 4; |
67 | 0 | s->type = ASN1_PRINTABLE_type(s->data, s->length); |
68 | 0 | return 1; |
69 | 0 | } |
70 | | |
71 | | int ASN1_STRING_print(BIO *bp, const ASN1_STRING *v) |
72 | 0 | { |
73 | 0 | int i, n; |
74 | 0 | char buf[80]; |
75 | 0 | const char *p; |
76 | |
|
77 | 0 | if (v == NULL) |
78 | 0 | return 0; |
79 | 0 | n = 0; |
80 | 0 | p = (const char *)v->data; |
81 | 0 | for (i = 0; i < v->length; i++) { |
82 | 0 | if ((p[i] > '~') || ((p[i] < ' ') && (p[i] != '\n') && (p[i] != '\r'))) |
83 | 0 | buf[n] = '.'; |
84 | 0 | else |
85 | 0 | buf[n] = p[i]; |
86 | 0 | n++; |
87 | 0 | if (n >= 80) { |
88 | 0 | if (BIO_write(bp, buf, n) <= 0) |
89 | 0 | return 0; |
90 | 0 | n = 0; |
91 | 0 | } |
92 | 0 | } |
93 | 0 | if (n > 0) |
94 | 0 | if (BIO_write(bp, buf, n) <= 0) |
95 | 0 | return 0; |
96 | 0 | return 1; |
97 | 0 | } |