]>
git.ipfire.org Git - thirdparty/openssl.git/blob - crypto/pkcs12/p12_utl.c
2 * Copyright 1999-2020 The OpenSSL Project Authors. All Rights Reserved.
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
11 #include "internal/cryptlib.h"
12 #include <openssl/pkcs12.h>
14 /* Cheap and nasty Unicode stuff */
16 unsigned char *OPENSSL_asc2uni(const char *asc
, int asclen
,
17 unsigned char **uni
, int *unilen
)
20 unsigned char *unitmp
;
24 ulen
= asclen
* 2 + 2;
25 if ((unitmp
= OPENSSL_malloc(ulen
)) == NULL
) {
26 ERR_raise(ERR_LIB_PKCS12
, ERR_R_MALLOC_FAILURE
);
29 for (i
= 0; i
< ulen
- 2; i
+= 2) {
31 unitmp
[i
+ 1] = asc
[i
>> 1];
33 /* Make result double null terminated */
43 char *OPENSSL_uni2asc(const unsigned char *uni
, int unilen
)
47 /* string must contain an even number of bytes */
51 /* If no terminating zero allow for one */
52 if (!unilen
|| uni
[unilen
- 1])
55 if ((asctmp
= OPENSSL_malloc(asclen
)) == NULL
) {
56 ERR_raise(ERR_LIB_PKCS12
, ERR_R_MALLOC_FAILURE
);
59 for (i
= 0; i
< unilen
; i
+= 2)
60 asctmp
[i
>> 1] = uni
[i
];
61 asctmp
[asclen
- 1] = 0;
66 * OPENSSL_{utf82uni|uni2utf8} perform conversion between UTF-8 and
67 * PKCS#12 BMPString format, which is specified as big-endian UTF-16.
68 * One should keep in mind that even though BMPString is passed as
69 * unsigned char *, it's not the kind of string you can exercise e.g.
70 * strlen on. Caller also has to keep in mind that its length is
71 * expressed not in number of UTF-16 characters, but in number of
72 * bytes the string occupies, and treat it, the length, accordingly.
74 unsigned char *OPENSSL_utf82uni(const char *asc
, int asclen
,
75 unsigned char **uni
, int *unilen
)
78 unsigned char *unitmp
, *ret
;
79 unsigned long utf32chr
= 0;
84 for (ulen
= 0, i
= 0; i
< asclen
; i
+= j
) {
85 j
= UTF8_getc((const unsigned char *)asc
+i
, asclen
-i
, &utf32chr
);
88 * Following condition is somewhat opportunistic is sense that
89 * decoding failure is used as *indirect* indication that input
90 * string might in fact be extended ASCII/ANSI/ISO-8859-X. The
91 * fallback is taken in hope that it would allow to process
92 * files created with previous OpenSSL version, which used the
93 * naive OPENSSL_asc2uni all along. It might be worth noting
94 * that probability of false positive depends on language. In
95 * cases covered by ISO Latin 1 probability is very low, because
96 * any printable non-ASCII alphabet letter followed by another
97 * or any ASCII character will trigger failure and fallback.
98 * In other cases situation can be intensified by the fact that
99 * English letters are not part of alternative keyboard layout,
100 * but even then there should be plenty of pairs that trigger
101 * decoding failure...
104 return OPENSSL_asc2uni(asc
, asclen
, uni
, unilen
);
106 if (utf32chr
> 0x10FFFF) /* UTF-16 cap */
109 if (utf32chr
>= 0x10000) /* pair of UTF-16 characters */
111 else /* or just one */
115 ulen
+= 2; /* for trailing UTF16 zero */
117 if ((ret
= OPENSSL_malloc(ulen
)) == NULL
) {
118 ERR_raise(ERR_LIB_PKCS12
, ERR_R_MALLOC_FAILURE
);
121 /* re-run the loop writing down UTF-16 characters in big-endian order */
122 for (unitmp
= ret
, i
= 0; i
< asclen
; i
+= j
) {
123 j
= UTF8_getc((const unsigned char *)asc
+i
, asclen
-i
, &utf32chr
);
124 if (utf32chr
>= 0x10000) { /* pair if UTF-16 characters */
128 hi
= 0xD800 + (utf32chr
>>10);
129 lo
= 0xDC00 + (utf32chr
&0x3ff);
130 *unitmp
++ = (unsigned char)(hi
>>8);
131 *unitmp
++ = (unsigned char)(hi
);
132 *unitmp
++ = (unsigned char)(lo
>>8);
133 *unitmp
++ = (unsigned char)(lo
);
134 } else { /* or just one */
135 *unitmp
++ = (unsigned char)(utf32chr
>>8);
136 *unitmp
++ = (unsigned char)(utf32chr
);
139 /* Make result double null terminated */
149 static int bmp_to_utf8(char *str
, const unsigned char *utf16
, int len
)
151 unsigned long utf32chr
;
153 if (len
== 0) return 0;
155 if (len
< 2) return -1;
157 /* pull UTF-16 character in big-endian order */
158 utf32chr
= (utf16
[0]<<8) | utf16
[1];
160 if (utf32chr
>= 0xD800 && utf32chr
< 0xE000) { /* two chars */
163 if (len
< 4) return -1;
167 lo
= (utf16
[2]<<8) | utf16
[3];
168 if (lo
< 0xDC00 || lo
>= 0xE000) return -1;
169 utf32chr
|= lo
-0xDC00;
173 return UTF8_putc((unsigned char *)str
, len
> 4 ? 4 : len
, utf32chr
);
176 char *OPENSSL_uni2utf8(const unsigned char *uni
, int unilen
)
181 /* string must contain an even number of bytes */
185 for (asclen
= 0, i
= 0; i
< unilen
; ) {
186 j
= bmp_to_utf8(NULL
, uni
+i
, unilen
-i
);
188 * falling back to OPENSSL_uni2asc makes lesser sense [than
189 * falling back to OPENSSL_asc2uni in OPENSSL_utf82uni above],
190 * it's done rather to maintain symmetry...
192 if (j
< 0) return OPENSSL_uni2asc(uni
, unilen
);
198 /* If no terminating zero allow for one */
199 if (!unilen
|| (uni
[unilen
-2]||uni
[unilen
- 1]))
202 if ((asctmp
= OPENSSL_malloc(asclen
)) == NULL
) {
203 ERR_raise(ERR_LIB_PKCS12
, ERR_R_MALLOC_FAILURE
);
207 /* re-run the loop emitting UTF-8 string */
208 for (asclen
= 0, i
= 0; i
< unilen
; ) {
209 j
= bmp_to_utf8(asctmp
+asclen
, uni
+i
, unilen
-i
);
215 /* If no terminating zero write one */
216 if (!unilen
|| (uni
[unilen
-2]||uni
[unilen
- 1]))
217 asctmp
[asclen
] = '\0';
222 int i2d_PKCS12_bio(BIO
*bp
, const PKCS12
*p12
)
224 return ASN1_item_i2d_bio(ASN1_ITEM_rptr(PKCS12
), bp
, p12
);
227 #ifndef OPENSSL_NO_STDIO
228 int i2d_PKCS12_fp(FILE *fp
, const PKCS12
*p12
)
230 return ASN1_item_i2d_fp(ASN1_ITEM_rptr(PKCS12
), fp
, p12
);
234 PKCS12
*d2i_PKCS12_bio(BIO
*bp
, PKCS12
**p12
)
236 return ASN1_item_d2i_bio(ASN1_ITEM_rptr(PKCS12
), bp
, p12
);
239 #ifndef OPENSSL_NO_STDIO
240 PKCS12
*d2i_PKCS12_fp(FILE *fp
, PKCS12
**p12
)
242 return ASN1_item_d2i_fp(ASN1_ITEM_rptr(PKCS12
), fp
, p12
);