Always try locale initialization from OPENSSL_strcasecmp
[openssl.git] / crypto / o_str.c
1 /*
2  * Copyright 2003-2022 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 "internal/e_os.h"
11 #include <string.h>
12 #include <limits.h>
13 #ifndef OPENSSL_NO_LOCALE
14 # include <locale.h>
15 # ifdef OPENSSL_SYS_MACOSX
16 #  include <xlocale.h>
17 # endif
18 #endif
19 #include <openssl/crypto.h>
20 #include "internal/cryptlib.h"
21 #include "internal/thread_once.h"
22
23 #define DEFAULT_SEPARATOR ':'
24 #define CH_ZERO '\0'
25
26 char *CRYPTO_strdup(const char *str, const char* file, int line)
27 {
28     char *ret;
29
30     if (str == NULL)
31         return NULL;
32     ret = CRYPTO_malloc(strlen(str) + 1, file, line);
33     if (ret != NULL)
34         strcpy(ret, str);
35     return ret;
36 }
37
38 char *CRYPTO_strndup(const char *str, size_t s, const char* file, int line)
39 {
40     size_t maxlen;
41     char *ret;
42
43     if (str == NULL)
44         return NULL;
45
46     maxlen = OPENSSL_strnlen(str, s);
47
48     ret = CRYPTO_malloc(maxlen + 1, file, line);
49     if (ret) {
50         memcpy(ret, str, maxlen);
51         ret[maxlen] = CH_ZERO;
52     }
53     return ret;
54 }
55
56 void *CRYPTO_memdup(const void *data, size_t siz, const char* file, int line)
57 {
58     void *ret;
59
60     if (data == NULL || siz >= INT_MAX)
61         return NULL;
62
63     ret = CRYPTO_malloc(siz, file, line);
64     if (ret == NULL) {
65         ERR_raise(ERR_LIB_CRYPTO, ERR_R_MALLOC_FAILURE);
66         return NULL;
67     }
68     return memcpy(ret, data, siz);
69 }
70
71 size_t OPENSSL_strnlen(const char *str, size_t maxlen)
72 {
73     const char *p;
74
75     for (p = str; maxlen-- != 0 && *p != CH_ZERO; ++p) ;
76
77     return p - str;
78 }
79
80 size_t OPENSSL_strlcpy(char *dst, const char *src, size_t size)
81 {
82     size_t l = 0;
83     for (; size > 1 && *src; size--) {
84         *dst++ = *src++;
85         l++;
86     }
87     if (size)
88         *dst = CH_ZERO;
89     return l + strlen(src);
90 }
91
92 size_t OPENSSL_strlcat(char *dst, const char *src, size_t size)
93 {
94     size_t l = 0;
95     for (; size > 0 && *dst; size--, dst++)
96         l++;
97     return l + OPENSSL_strlcpy(dst, src, size);
98 }
99
100 int OPENSSL_hexchar2int(unsigned char c)
101 {
102 #ifdef CHARSET_EBCDIC
103     c = os_toebcdic[c];
104 #endif
105
106     switch (c) {
107     case '0':
108         return 0;
109     case '1':
110         return 1;
111     case '2':
112         return 2;
113     case '3':
114         return 3;
115     case '4':
116           return 4;
117     case '5':
118           return 5;
119     case '6':
120           return 6;
121     case '7':
122           return 7;
123     case '8':
124           return 8;
125     case '9':
126           return 9;
127     case 'a': case 'A':
128           return 0x0A;
129     case 'b': case 'B':
130           return 0x0B;
131     case 'c': case 'C':
132           return 0x0C;
133     case 'd': case 'D':
134           return 0x0D;
135     case 'e': case 'E':
136           return 0x0E;
137     case 'f': case 'F':
138           return 0x0F;
139     }
140     return -1;
141 }
142
143 static int hexstr2buf_sep(unsigned char *buf, size_t buf_n, size_t *buflen,
144                           const char *str, const char sep)
145 {
146     unsigned char *q;
147     unsigned char ch, cl;
148     int chi, cli;
149     const unsigned char *p;
150     size_t cnt;
151
152     for (p = (const unsigned char *)str, q = buf, cnt = 0; *p; ) {
153         ch = *p++;
154         /* A separator of CH_ZERO means there is no separator */
155         if (ch == sep && sep != CH_ZERO)
156             continue;
157         cl = *p++;
158         if (!cl) {
159             ERR_raise(ERR_LIB_CRYPTO, CRYPTO_R_ODD_NUMBER_OF_DIGITS);
160             return 0;
161         }
162         cli = OPENSSL_hexchar2int(cl);
163         chi = OPENSSL_hexchar2int(ch);
164         if (cli < 0 || chi < 0) {
165             ERR_raise(ERR_LIB_CRYPTO, CRYPTO_R_ILLEGAL_HEX_DIGIT);
166             return 0;
167         }
168         cnt++;
169         if (q != NULL) {
170             if (cnt > buf_n) {
171                 ERR_raise(ERR_LIB_CRYPTO, CRYPTO_R_TOO_SMALL_BUFFER);
172                 return 0;
173             }
174             *q++ = (unsigned char)((chi << 4) | cli);
175         }
176     }
177
178     if (buflen != NULL)
179         *buflen = cnt;
180     return 1;
181 }
182
183 /*
184  * Given a string of hex digits convert to a buffer
185  */
186 int OPENSSL_hexstr2buf_ex(unsigned char *buf, size_t buf_n, size_t *buflen,
187                           const char *str, const char sep)
188 {
189     return hexstr2buf_sep(buf, buf_n, buflen, str, sep);
190 }
191
192 unsigned char *ossl_hexstr2buf_sep(const char *str, long *buflen,
193                                    const char sep)
194 {
195     unsigned char *buf;
196     size_t buf_n, tmp_buflen;
197
198     buf_n = strlen(str);
199     if (buf_n <= 1) {
200         ERR_raise(ERR_LIB_CRYPTO, CRYPTO_R_HEX_STRING_TOO_SHORT);
201         return NULL;
202     }
203     buf_n /= 2;
204     if ((buf = OPENSSL_malloc(buf_n)) == NULL) {
205         ERR_raise(ERR_LIB_CRYPTO, ERR_R_MALLOC_FAILURE);
206         return NULL;
207     }
208
209     if (buflen != NULL)
210         *buflen = 0;
211     tmp_buflen = 0;
212     if (hexstr2buf_sep(buf, buf_n, &tmp_buflen, str, sep)) {
213         if (buflen != NULL)
214             *buflen = (long)tmp_buflen;
215         return buf;
216     }
217     OPENSSL_free(buf);
218     return NULL;
219 }
220
221 unsigned char *OPENSSL_hexstr2buf(const char *str, long *buflen)
222 {
223     return ossl_hexstr2buf_sep(str, buflen, DEFAULT_SEPARATOR);
224 }
225
226 static int buf2hexstr_sep(char *str, size_t str_n, size_t *strlength,
227                           const unsigned char *buf, size_t buflen,
228                           const char sep)
229 {
230     static const char hexdig[] = "0123456789ABCDEF";
231     const unsigned char *p;
232     char *q;
233     size_t i;
234     int has_sep = (sep != CH_ZERO);
235     size_t len = has_sep ? buflen * 3 : 1 + buflen * 2;
236
237     if (strlength != NULL)
238         *strlength = len;
239     if (str == NULL)
240         return 1;
241
242     if (str_n < (unsigned long)len) {
243         ERR_raise(ERR_LIB_CRYPTO, CRYPTO_R_TOO_SMALL_BUFFER);
244         return 0;
245     }
246
247     q = str;
248     for (i = 0, p = buf; i < buflen; i++, p++) {
249         *q++ = hexdig[(*p >> 4) & 0xf];
250         *q++ = hexdig[*p & 0xf];
251         if (has_sep)
252             *q++ = sep;
253     }
254     if (has_sep)
255         --q;
256     *q = CH_ZERO;
257
258 #ifdef CHARSET_EBCDIC
259     ebcdic2ascii(str, str, q - str - 1);
260 #endif
261     return 1;
262 }
263
264 int OPENSSL_buf2hexstr_ex(char *str, size_t str_n, size_t *strlength,
265                           const unsigned char *buf, size_t buflen,
266                           const char sep)
267 {
268     return buf2hexstr_sep(str, str_n, strlength, buf, buflen, sep);
269 }
270
271 char *ossl_buf2hexstr_sep(const unsigned char *buf, long buflen, char sep)
272 {
273     char *tmp;
274     size_t tmp_n;
275
276     if (buflen == 0)
277         return OPENSSL_zalloc(1);
278
279     tmp_n = (sep != CH_ZERO) ? buflen * 3 : 1 + buflen * 2;
280     if ((tmp = OPENSSL_malloc(tmp_n)) == NULL) {
281         ERR_raise(ERR_LIB_CRYPTO, ERR_R_MALLOC_FAILURE);
282         return NULL;
283     }
284
285     if (buf2hexstr_sep(tmp, tmp_n, NULL, buf, buflen, sep))
286         return tmp;
287     OPENSSL_free(tmp);
288     return NULL;
289 }
290
291
292 /*
293  * Given a buffer of length 'len' return a OPENSSL_malloc'ed string with its
294  * hex representation @@@ (Contents of buffer are always kept in ASCII, also
295  * on EBCDIC machines)
296  */
297 char *OPENSSL_buf2hexstr(const unsigned char *buf, long buflen)
298 {
299     return ossl_buf2hexstr_sep(buf, buflen, ':');
300 }
301
302 int openssl_strerror_r(int errnum, char *buf, size_t buflen)
303 {
304 #if defined(_MSC_VER) && _MSC_VER>=1400 && !defined(_WIN32_WCE)
305     return !strerror_s(buf, buflen, errnum);
306 #elif defined(_GNU_SOURCE)
307     char *err;
308
309     /*
310      * GNU strerror_r may not actually set buf.
311      * It can return a pointer to some (immutable) static string in which case
312      * buf is left unused.
313      */
314     err = strerror_r(errnum, buf, buflen);
315     if (err == NULL || buflen == 0)
316         return 0;
317     /*
318      * If err is statically allocated, err != buf and we need to copy the data.
319      * If err points somewhere inside buf, OPENSSL_strlcpy can handle this,
320      * since src and dest are not annotated with __restrict and the function
321      * reads src byte for byte and writes to dest.
322      * If err == buf we do not have to copy anything.
323      */
324     if (err != buf)
325         OPENSSL_strlcpy(buf, err, buflen);
326     return 1;
327 #elif (defined(_POSIX_C_SOURCE) && _POSIX_C_SOURCE >= 200112L) || \
328       (defined(_XOPEN_SOURCE) && _XOPEN_SOURCE >= 600)
329     /*
330      * We can use "real" strerror_r. The OpenSSL version differs in that it
331      * gives 1 on success and 0 on failure for consistency with other OpenSSL
332      * functions. Real strerror_r does it the other way around
333      */
334     return !strerror_r(errnum, buf, buflen);
335 #else
336     char *err;
337
338     /* Fall back to non-thread safe strerror()...its all we can do */
339     if (buflen < 2)
340         return 0;
341     err = strerror(errnum);
342     /* Can this ever happen? */
343     if (err == NULL)
344         return 0;
345     OPENSSL_strlcpy(buf, err, buflen);
346     return 1;
347 #endif
348 }
349
350 #ifndef OPENSSL_NO_LOCALE
351 # ifndef FIPS_MODULE
352 static CRYPTO_ONCE casecmp = CRYPTO_ONCE_STATIC_INIT;
353 DEFINE_RUN_ONCE_STATIC(init_casecmp)
354 {
355     int ret = ossl_init_casecmp_int();
356
357     return ret;
358 }
359
360 int ossl_init_casecmp(void)
361 {
362     if (!RUN_ONCE(&casecmp, init_casecmp))
363         return 0;
364     return 1;
365 }
366 # endif
367
368 static locale_t loc;
369
370 static locale_t ossl_c_locale(void)
371 {
372 # ifndef FIPS_MODULE
373     if (!ossl_init_casecmp())
374         return (locale_t)0;
375 # endif
376     return loc;
377 }
378
379 int ossl_init_casecmp_int(void)
380 {
381 # ifdef OPENSSL_SYS_WINDOWS
382     loc = _create_locale(LC_COLLATE, "C");
383 # else
384     loc = newlocale(LC_COLLATE_MASK, "C", (locale_t) 0);
385 # endif
386     return (loc == (locale_t)0) ? 0 : 1;
387 }
388
389 void ossl_deinit_casecmp(void)
390 {
391     if (loc != (locale_t)0) {
392         freelocale(loc);
393         loc = (locale_t)0;
394     }
395 }
396
397 int OPENSSL_strcasecmp(const char *s1, const char *s2)
398 {
399     locale_t l = ossl_c_locale();
400
401     /* Fallback in case of locale initialization failure */
402     if (l == (locale_t)0)
403         return strcasecmp(s1, s2);
404     return strcasecmp_l(s1, s2, l);
405 }
406
407 int OPENSSL_strncasecmp(const char *s1, const char *s2, size_t n)
408 {
409     locale_t l = ossl_c_locale();
410
411     /* Fallback in case of locale initialization failure */
412     if (l == (locale_t)0)
413         return strncasecmp(s1, s2, n);
414     return strncasecmp_l(s1, s2, n, l);
415 }
416 #else
417 int ossl_init_casecmp(void)
418 {
419     return 1;
420 }
421
422 int ossl_init_casecmp_int(void)
423 {
424     return 1;
425 }
426
427 void ossl_deinit_casecmp(void)
428 {
429 }
430
431 int OPENSSL_strcasecmp(const char *s1, const char *s2)
432 {
433     return strcasecmp(s1, s2);
434 }
435
436 int OPENSSL_strncasecmp(const char *s1, const char *s2, size_t n)
437 {
438     return strncasecmp(s1, s2, n);
439 }
440 #endif