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