Coverage Report

Created: 2018-08-29 13:53

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