Coverage Report

Created: 2025-12-31 06:58

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