/src/openssl/crypto/pkcs12/p12_utl.c
Line | Count | Source |
1 | | /* |
2 | | * Copyright 1999-2026 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 "internal/cryptlib.h" |
12 | | #include <openssl/pkcs12.h> |
13 | | #include "p12_local.h" |
14 | | #include "crypto/pkcs7/pk7_local.h" |
15 | | #include <crypto/asn1.h> |
16 | | |
17 | | /* Cheap and nasty Unicode stuff */ |
18 | | |
19 | | unsigned char *OPENSSL_asc2uni(const char *asc, int asclen, |
20 | | unsigned char **uni, int *unilen) |
21 | 0 | { |
22 | 0 | int ulen, i; |
23 | 0 | unsigned char *unitmp; |
24 | |
|
25 | 0 | if (asclen == -1) |
26 | 0 | asclen = (int)strlen(asc); |
27 | 0 | if (asclen < 0) |
28 | 0 | return NULL; |
29 | 0 | ulen = asclen * 2 + 2; |
30 | 0 | if ((unitmp = OPENSSL_malloc(ulen)) == NULL) |
31 | 0 | return NULL; |
32 | 0 | for (i = 0; i < ulen - 2; i += 2) { |
33 | 0 | unitmp[i] = 0; |
34 | 0 | unitmp[i + 1] = asc[i >> 1]; |
35 | 0 | } |
36 | | /* Make result double null terminated */ |
37 | 0 | unitmp[ulen - 2] = 0; |
38 | 0 | unitmp[ulen - 1] = 0; |
39 | 0 | if (unilen) |
40 | 0 | *unilen = ulen; |
41 | 0 | if (uni) |
42 | 0 | *uni = unitmp; |
43 | 0 | return unitmp; |
44 | 0 | } |
45 | | |
46 | | char *OPENSSL_uni2asc(const unsigned char *uni, int unilen) |
47 | 0 | { |
48 | 0 | int asclen, i; |
49 | 0 | char *asctmp; |
50 | | |
51 | | /* string must contain an even number of bytes */ |
52 | 0 | if (unilen & 1) |
53 | 0 | return NULL; |
54 | 0 | if (unilen < 0) |
55 | 0 | return NULL; |
56 | 0 | asclen = unilen / 2; |
57 | | /* If no terminating zero allow for one */ |
58 | 0 | if (!unilen || uni[unilen - 1]) |
59 | 0 | asclen++; |
60 | 0 | if ((asctmp = OPENSSL_malloc(asclen)) == NULL) |
61 | 0 | return NULL; |
62 | | /* |
63 | | * Take the low byte of each big-endian UTF-16 unit. Index from the |
64 | | * caller's pointer rather than incrementing it first, so that a zero |
65 | | * length input does not do pointer arithmetic on a NULL pointer. |
66 | | */ |
67 | 0 | for (i = 0; i < unilen; i += 2) |
68 | 0 | asctmp[i >> 1] = uni[i + 1]; |
69 | 0 | asctmp[asclen - 1] = 0; |
70 | 0 | return asctmp; |
71 | 0 | } |
72 | | |
73 | | /* |
74 | | * OPENSSL_{utf82uni|uni2utf8} perform conversion between UTF-8 and |
75 | | * PKCS#12 BMPString format, which is specified as big-endian UTF-16. |
76 | | * One should keep in mind that even though BMPString is passed as |
77 | | * unsigned char *, it's not the kind of string you can exercise e.g. |
78 | | * strlen on. Caller also has to keep in mind that its length is |
79 | | * expressed not in number of UTF-16 characters, but in number of |
80 | | * bytes the string occupies, and treat it, the length, accordingly. |
81 | | */ |
82 | | unsigned char *OPENSSL_utf82uni(const char *asc, int asclen, |
83 | | unsigned char **uni, int *unilen) |
84 | 0 | { |
85 | 0 | int ulen, i, j; |
86 | 0 | unsigned char *unitmp, *ret; |
87 | 0 | uint32_t utf32chr = 0; |
88 | |
|
89 | 0 | if (asclen == -1) |
90 | 0 | asclen = (int)strlen(asc); |
91 | |
|
92 | 0 | for (ulen = 0, i = 0; i < asclen; i += j) { |
93 | 0 | j = ossl_utf8_getc_internal((const unsigned char *)asc + i, asclen - i, |
94 | 0 | &utf32chr); |
95 | | |
96 | | /* |
97 | | * Following condition is somewhat opportunistic is sense that |
98 | | * decoding failure is used as *indirect* indication that input |
99 | | * string might in fact be extended ASCII/ANSI/ISO-8859-X. The |
100 | | * fallback is taken in hope that it would allow to process |
101 | | * files created with previous OpenSSL version, which used the |
102 | | * naive OPENSSL_asc2uni all along. It might be worth noting |
103 | | * that probability of false positive depends on language. In |
104 | | * cases covered by ISO Latin 1 probability is very low, because |
105 | | * any printable non-ASCII alphabet letter followed by another |
106 | | * or any ASCII character will trigger failure and fallback. |
107 | | * In other cases situation can be intensified by the fact that |
108 | | * English letters are not part of alternative keyboard layout, |
109 | | * but even then there should be plenty of pairs that trigger |
110 | | * decoding failure... |
111 | | */ |
112 | 0 | if (j < 0) |
113 | 0 | return OPENSSL_asc2uni(asc, asclen, uni, unilen); |
114 | | |
115 | 0 | if (utf32chr > 0x10FFFF) /* UTF-16 cap */ |
116 | 0 | return NULL; |
117 | | |
118 | 0 | if (utf32chr >= 0x10000) /* pair of UTF-16 characters */ |
119 | 0 | ulen += 2 * 2; |
120 | 0 | else /* or just one */ |
121 | 0 | ulen += 2; |
122 | 0 | } |
123 | | |
124 | 0 | ulen += 2; /* for trailing UTF16 zero */ |
125 | |
|
126 | 0 | if ((ret = OPENSSL_malloc(ulen)) == NULL) |
127 | 0 | return NULL; |
128 | | /* re-run the loop writing down UTF-16 characters in big-endian order */ |
129 | 0 | for (unitmp = ret, i = 0; i < asclen; i += j) { |
130 | 0 | j = ossl_utf8_getc_internal((const unsigned char *)asc + i, asclen - i, |
131 | 0 | &utf32chr); |
132 | 0 | if (utf32chr >= 0x10000) { /* pair if UTF-16 characters */ |
133 | 0 | unsigned int hi, lo; |
134 | |
|
135 | 0 | utf32chr -= 0x10000; |
136 | 0 | hi = 0xD800 + (utf32chr >> 10); |
137 | 0 | lo = 0xDC00 + (utf32chr & 0x3ff); |
138 | 0 | *unitmp++ = (unsigned char)(hi >> 8); |
139 | 0 | *unitmp++ = (unsigned char)(hi); |
140 | 0 | *unitmp++ = (unsigned char)(lo >> 8); |
141 | 0 | *unitmp++ = (unsigned char)(lo); |
142 | 0 | } else { /* or just one */ |
143 | 0 | *unitmp++ = (unsigned char)(utf32chr >> 8); |
144 | 0 | *unitmp++ = (unsigned char)(utf32chr); |
145 | 0 | } |
146 | 0 | } |
147 | | /* Make result double null terminated */ |
148 | 0 | *unitmp++ = 0; |
149 | 0 | *unitmp++ = 0; |
150 | 0 | if (unilen) |
151 | 0 | *unilen = ulen; |
152 | 0 | if (uni) |
153 | 0 | *uni = ret; |
154 | 0 | return ret; |
155 | 0 | } |
156 | | |
157 | | static int bmp_to_utf8(char *str, const unsigned char *utf16, int len) |
158 | 0 | { |
159 | 0 | uint32_t utf32chr; |
160 | |
|
161 | 0 | if (len == 0) |
162 | 0 | return 0; |
163 | | |
164 | 0 | if (len < 2) |
165 | 0 | return -1; |
166 | | |
167 | | /* pull UTF-16 character in big-endian order */ |
168 | 0 | utf32chr = (utf16[0] << 8) | utf16[1]; |
169 | |
|
170 | 0 | if (utf32chr >= 0xD800 && utf32chr < 0xE000) { /* two chars */ |
171 | 0 | unsigned int lo; |
172 | |
|
173 | 0 | if (len < 4) |
174 | 0 | return -1; |
175 | | |
176 | 0 | utf32chr -= 0xD800; |
177 | 0 | utf32chr <<= 10; |
178 | 0 | lo = (utf16[2] << 8) | utf16[3]; |
179 | 0 | if (lo < 0xDC00 || lo >= 0xE000) |
180 | 0 | return -1; |
181 | 0 | utf32chr |= lo - 0xDC00; |
182 | 0 | utf32chr += 0x10000; |
183 | 0 | } |
184 | | |
185 | 0 | return ossl_utf8_putc_internal((unsigned char *)str, 4, utf32chr); |
186 | 0 | } |
187 | | char *OPENSSL_uni2utf8(const unsigned char *uni, int unilen) |
188 | 0 | { |
189 | 0 | int asclen, i, j; |
190 | 0 | char *asctmp; |
191 | | |
192 | | /* string must contain an even number of bytes */ |
193 | 0 | if (unilen & 1) |
194 | 0 | return NULL; |
195 | 0 | if (unilen < 0) |
196 | 0 | return NULL; |
197 | | |
198 | 0 | for (asclen = 0, i = 0; i < unilen;) { |
199 | 0 | j = bmp_to_utf8(NULL, uni + i, unilen - i); |
200 | | /* |
201 | | * falling back to OPENSSL_uni2asc makes lesser sense [than |
202 | | * falling back to OPENSSL_asc2uni in OPENSSL_utf82uni above], |
203 | | * it's done rather to maintain symmetry... |
204 | | */ |
205 | 0 | if (j < 0) |
206 | 0 | return OPENSSL_uni2asc(uni, unilen); |
207 | 0 | if (j == 4) |
208 | 0 | i += 4; |
209 | 0 | else |
210 | 0 | i += 2; |
211 | 0 | asclen += j; |
212 | 0 | } |
213 | | |
214 | | /* If no terminating zero allow for one */ |
215 | 0 | if (!unilen || (uni[unilen - 2] || uni[unilen - 1])) |
216 | 0 | asclen++; |
217 | |
|
218 | 0 | if ((asctmp = OPENSSL_malloc(asclen)) == NULL) |
219 | 0 | return NULL; |
220 | | |
221 | | /* re-run the loop emitting UTF-8 string */ |
222 | 0 | for (asclen = 0, i = 0; i < unilen;) { |
223 | 0 | j = bmp_to_utf8(asctmp + asclen, uni + i, unilen - i); |
224 | | /* when UTF8_putc fails */ |
225 | 0 | if (j < 0) { |
226 | 0 | OPENSSL_free(asctmp); |
227 | 0 | return NULL; |
228 | 0 | } |
229 | 0 | if (j == 4) |
230 | 0 | i += 4; |
231 | 0 | else |
232 | 0 | i += 2; |
233 | 0 | asclen += j; |
234 | 0 | } |
235 | | |
236 | | /* If no terminating zero write one */ |
237 | 0 | if (!unilen || (uni[unilen - 2] || uni[unilen - 1])) |
238 | 0 | asctmp[asclen] = '\0'; |
239 | |
|
240 | 0 | return asctmp; |
241 | 0 | } |
242 | | |
243 | | int i2d_PKCS12_bio(BIO *bp, const PKCS12 *p12) |
244 | 0 | { |
245 | 0 | return ASN1_item_i2d_bio(ASN1_ITEM_rptr(PKCS12), bp, p12); |
246 | 0 | } |
247 | | |
248 | | #ifndef OPENSSL_NO_STDIO |
249 | | int i2d_PKCS12_fp(FILE *fp, const PKCS12 *p12) |
250 | 0 | { |
251 | 0 | return ASN1_item_i2d_fp(ASN1_ITEM_rptr(PKCS12), fp, p12); |
252 | 0 | } |
253 | | #endif |
254 | | |
255 | | PKCS12 *d2i_PKCS12_bio(BIO *bp, PKCS12 **p12) |
256 | 0 | { |
257 | 0 | OSSL_LIB_CTX *libctx = NULL; |
258 | 0 | const char *propq = NULL; |
259 | 0 | const PKCS7_CTX *p7ctx = NULL; |
260 | |
|
261 | 0 | if (p12 != NULL) { |
262 | 0 | p7ctx = ossl_pkcs12_get0_pkcs7ctx(*p12); |
263 | 0 | if (p7ctx != NULL) { |
264 | 0 | libctx = ossl_pkcs7_ctx_get0_libctx(p7ctx); |
265 | 0 | propq = ossl_pkcs7_ctx_get0_propq(p7ctx); |
266 | 0 | } |
267 | 0 | } |
268 | 0 | return ASN1_item_d2i_bio_ex(ASN1_ITEM_rptr(PKCS12), bp, p12, libctx, propq); |
269 | 0 | } |
270 | | |
271 | | #ifndef OPENSSL_NO_STDIO |
272 | | PKCS12 *d2i_PKCS12_fp(FILE *fp, PKCS12 **p12) |
273 | 0 | { |
274 | 0 | OSSL_LIB_CTX *libctx = NULL; |
275 | 0 | const char *propq = NULL; |
276 | 0 | const PKCS7_CTX *p7ctx = NULL; |
277 | |
|
278 | 0 | if (p12 != NULL) { |
279 | 0 | p7ctx = ossl_pkcs12_get0_pkcs7ctx(*p12); |
280 | 0 | if (p7ctx != NULL) { |
281 | 0 | libctx = ossl_pkcs7_ctx_get0_libctx(p7ctx); |
282 | 0 | propq = ossl_pkcs7_ctx_get0_propq(p7ctx); |
283 | 0 | } |
284 | 0 | } |
285 | 0 | return ASN1_item_d2i_fp_ex(ASN1_ITEM_rptr(PKCS12), fp, p12, libctx, propq); |
286 | 0 | } |
287 | | #endif |