/src/openssl/crypto/o_str.c
Line | Count | Source |
1 | | /* |
2 | | * Copyright 2003-2025 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 | | #include <openssl/crypto.h> |
14 | | #include "crypto/ctype.h" |
15 | | #include "internal/cryptlib.h" |
16 | | #include "internal/thread_once.h" |
17 | | #include "internal/to_hex.h" |
18 | | |
19 | 0 | #define DEFAULT_SEPARATOR ':' |
20 | 110k | #define CH_ZERO '\0' |
21 | | |
22 | | char *CRYPTO_strdup(const char *str, const char *file, int line) |
23 | 9.10k | { |
24 | 9.10k | char *ret; |
25 | 9.10k | size_t len; |
26 | | |
27 | 9.10k | if (str == NULL) |
28 | 0 | return NULL; |
29 | | |
30 | 9.10k | len = strlen(str) + 1; |
31 | 9.10k | ret = CRYPTO_malloc(len, file, line); |
32 | 9.10k | if (ret != NULL) |
33 | 9.10k | memcpy(ret, str, len); |
34 | 9.10k | return ret; |
35 | 9.10k | } |
36 | | |
37 | | char *CRYPTO_strndup(const char *str, size_t s, const char *file, int line) |
38 | 2.67k | { |
39 | 2.67k | size_t maxlen; |
40 | 2.67k | char *ret; |
41 | | |
42 | 2.67k | if (str == NULL) |
43 | 0 | return NULL; |
44 | | |
45 | 2.67k | maxlen = OPENSSL_strnlen(str, s); |
46 | | |
47 | 2.67k | ret = CRYPTO_malloc(maxlen + 1, file, line); |
48 | 2.67k | if (ret) { |
49 | 2.67k | memcpy(ret, str, maxlen); |
50 | 2.67k | ret[maxlen] = CH_ZERO; |
51 | 2.67k | } |
52 | 2.67k | return ret; |
53 | 2.67k | } |
54 | | |
55 | | void *CRYPTO_memdup(const void *data, size_t siz, const char *file, int line) |
56 | 0 | { |
57 | 0 | void *ret; |
58 | |
|
59 | 0 | if (data == NULL || siz >= INT_MAX) |
60 | 0 | return NULL; |
61 | | |
62 | 0 | ret = CRYPTO_malloc(siz, file, line); |
63 | 0 | if (ret == NULL) |
64 | 0 | return NULL; |
65 | 0 | return memcpy(ret, data, siz); |
66 | 0 | } |
67 | | |
68 | | size_t OPENSSL_strnlen(const char *str, size_t maxlen) |
69 | 2.67k | { |
70 | 2.67k | const char *p; |
71 | | |
72 | 35.3k | for (p = str; maxlen-- != 0 && *p != CH_ZERO; ++p) |
73 | 32.7k | ; |
74 | | |
75 | 2.67k | return p - str; |
76 | 2.67k | } |
77 | | |
78 | | size_t OPENSSL_strlcpy(char *dst, const char *src, size_t size) |
79 | 74.6k | { |
80 | 74.6k | size_t l = 0; |
81 | 529k | for (; size > 1 && *src; size--) { |
82 | 455k | *dst++ = *src++; |
83 | 455k | l++; |
84 | 455k | } |
85 | 74.6k | if (size) |
86 | 74.6k | *dst = CH_ZERO; |
87 | 74.6k | return l + strlen(src); |
88 | 74.6k | } |
89 | | |
90 | | size_t OPENSSL_strlcat(char *dst, const char *src, size_t size) |
91 | 52.8k | { |
92 | 52.8k | size_t l = 0; |
93 | 302k | for (; size > 0 && *dst; size--, dst++) |
94 | 249k | l++; |
95 | 52.8k | return l + OPENSSL_strlcpy(dst, src, size); |
96 | 52.8k | } |
97 | | |
98 | | /** |
99 | | * @brief Converts a string to an unsigned long integer. |
100 | | * |
101 | | * This function attempts to convert a string representation of a number |
102 | | * to an unsigned long integer, given a specified base. It also provides |
103 | | * error checking and reports whether the conversion was successful. |
104 | | * This function is just a wrapper around the POSIX strtoul function with |
105 | | * additional error checking. This implies that errno for the caller is set |
106 | | * on calls to this function. |
107 | | * |
108 | | * @param str The string containing the representation of the number. |
109 | | * @param endptr A pointer to a pointer to character. If not NULL, it is set |
110 | | * to the character immediately following the number in the |
111 | | * string. |
112 | | * @param base The base to use for the conversion, which must be between 2, |
113 | | * and 36 inclusive, or be the special value 0. If the base is 0, |
114 | | * the actual base is determined by the format of the initial |
115 | | * characters of the string. |
116 | | * @param num A pointer to an unsigned long where the result of the |
117 | | * conversion is stored. |
118 | | * |
119 | | * @return 1 if the conversion was successful, 0 otherwise. Conversion is |
120 | | * considered unsuccessful if no digits were consumed or if an error |
121 | | * occurred during conversion. |
122 | | * |
123 | | * @note It is the caller's responsibility to check if the conversion is |
124 | | * correct based on the expected consumption of the string as reported |
125 | | * by endptr. |
126 | | */ |
127 | | int OPENSSL_strtoul(const char *str, char **endptr, int base, |
128 | | unsigned long *num) |
129 | 0 | { |
130 | 0 | char *tmp_endptr; |
131 | 0 | char **internal_endptr = endptr == NULL ? &tmp_endptr : endptr; |
132 | |
|
133 | 0 | errno = 0; |
134 | |
|
135 | 0 | *internal_endptr = (char *)str; |
136 | |
|
137 | 0 | if (num == NULL) |
138 | 0 | return 0; |
139 | | |
140 | 0 | if (str == NULL) |
141 | 0 | return 0; |
142 | | |
143 | | /* Fail on negative input */ |
144 | 0 | if (*str == '-') |
145 | 0 | return 0; |
146 | | |
147 | 0 | *num = strtoul(str, internal_endptr, base); |
148 | | /* |
149 | | * We return error from this function under the following conditions |
150 | | * 1) If strtoul itself returned an error in translation |
151 | | * 2) If the caller didn't pass in an endptr value, and **internal_endptr |
152 | | * doesn't point to '\0'. The implication here is that if the caller |
153 | | * doesn't care how much of a string is consumed, they expect the entire |
154 | | * string to be consumed. As such, no pointing to the NULL terminator |
155 | | * means there was some part of the string left over after translation |
156 | | * 3) If no bytes of the string were consumed |
157 | | */ |
158 | 0 | if (errno != 0 || (endptr == NULL && **internal_endptr != '\0') || (str == *internal_endptr)) |
159 | 0 | return 0; |
160 | | |
161 | 0 | return 1; |
162 | 0 | } |
163 | | |
164 | | /* |
165 | | * Internal signed counterpart to OPENSSL_strtoul(). Parses a signed long with |
166 | | * the same validation rules: it fails on a conversion error (including |
167 | | * overflow/underflow), if no digits were consumed, or, when |endptr| is NULL, |
168 | | * if the whole string was not consumed. Returns 1 on success (storing the |
169 | | * result in |*result|), 0 otherwise. |
170 | | */ |
171 | | int ossl_strtol(const char *str, char **endptr, int base, long *result) |
172 | 0 | { |
173 | 0 | char *tmp_endptr; |
174 | 0 | char **internal_endptr = endptr == NULL ? &tmp_endptr : endptr; |
175 | |
|
176 | 0 | errno = 0; |
177 | |
|
178 | 0 | *internal_endptr = (char *)str; |
179 | |
|
180 | 0 | if (result == NULL || str == NULL) |
181 | 0 | return 0; |
182 | | |
183 | 0 | *result = strtol(str, internal_endptr, base); |
184 | 0 | if (errno != 0 |
185 | 0 | || (endptr == NULL && **internal_endptr != '\0') |
186 | 0 | || str == *internal_endptr) |
187 | 0 | return 0; |
188 | | |
189 | 0 | return 1; |
190 | 0 | } |
191 | | |
192 | | /* |
193 | | * As ossl_strtol() but stores the result in an int, additionally failing if |
194 | | * the parsed value does not fit in an int. Returns 1 on success (storing the |
195 | | * result in |*result|), 0 otherwise. |
196 | | */ |
197 | | int ossl_strtoint(const char *str, char **endptr, int base, int *result) |
198 | 0 | { |
199 | 0 | long l; |
200 | |
|
201 | 0 | if (result == NULL || !ossl_strtol(str, endptr, base, &l)) |
202 | 0 | return 0; |
203 | 0 | if (l < INT_MIN || l > INT_MAX) |
204 | 0 | return 0; |
205 | | |
206 | 0 | *result = (int)l; |
207 | 0 | return 1; |
208 | 0 | } |
209 | | |
210 | | int OPENSSL_hexchar2int(unsigned char c) |
211 | 12.2k | { |
212 | | #ifdef CHARSET_EBCDIC |
213 | | c = os_toebcdic[c]; |
214 | | #endif |
215 | | |
216 | 12.2k | switch (c) { |
217 | 528 | case '0': |
218 | 528 | return 0; |
219 | 704 | case '1': |
220 | 704 | return 1; |
221 | 768 | case '2': |
222 | 768 | return 2; |
223 | 896 | case '3': |
224 | 896 | return 3; |
225 | 704 | case '4': |
226 | 704 | return 4; |
227 | 672 | case '5': |
228 | 672 | return 5; |
229 | 816 | case '6': |
230 | 816 | return 6; |
231 | 560 | case '7': |
232 | 560 | return 7; |
233 | 688 | case '8': |
234 | 688 | return 8; |
235 | 512 | case '9': |
236 | 512 | return 9; |
237 | 0 | case 'a': |
238 | 624 | case 'A': |
239 | 624 | return 0x0A; |
240 | 0 | case 'b': |
241 | 704 | case 'B': |
242 | 704 | return 0x0B; |
243 | 0 | case 'c': |
244 | 736 | case 'C': |
245 | 736 | return 0x0C; |
246 | 0 | case 'd': |
247 | 672 | case 'D': |
248 | 672 | return 0x0D; |
249 | 0 | case 'e': |
250 | 848 | case 'E': |
251 | 848 | return 0x0E; |
252 | 0 | case 'f': |
253 | 1.85k | case 'F': |
254 | 1.85k | return 0x0F; |
255 | 12.2k | } |
256 | 0 | return -1; |
257 | 12.2k | } |
258 | | |
259 | | static int hexstr2buf_sep(unsigned char *buf, size_t buf_n, size_t *buflen, |
260 | | const char *str, const char sep) |
261 | 0 | { |
262 | 0 | unsigned char *q; |
263 | 0 | unsigned char ch, cl; |
264 | 0 | int chi, cli; |
265 | 0 | const unsigned char *p; |
266 | 0 | size_t cnt; |
267 | |
|
268 | 0 | for (p = (const unsigned char *)str, q = buf, cnt = 0; *p;) { |
269 | 0 | ch = *p++; |
270 | | /* A separator of CH_ZERO means there is no separator */ |
271 | 0 | if (ch == sep && sep != CH_ZERO) |
272 | 0 | continue; |
273 | 0 | cl = *p++; |
274 | 0 | if (!cl) { |
275 | 0 | ERR_raise(ERR_LIB_CRYPTO, CRYPTO_R_ODD_NUMBER_OF_DIGITS); |
276 | 0 | return 0; |
277 | 0 | } |
278 | 0 | cli = OPENSSL_hexchar2int(cl); |
279 | 0 | chi = OPENSSL_hexchar2int(ch); |
280 | 0 | if (cli < 0 || chi < 0) { |
281 | 0 | ERR_raise(ERR_LIB_CRYPTO, CRYPTO_R_ILLEGAL_HEX_DIGIT); |
282 | 0 | return 0; |
283 | 0 | } |
284 | 0 | cnt++; |
285 | 0 | if (q != NULL) { |
286 | 0 | if (cnt > buf_n) { |
287 | 0 | ERR_raise(ERR_LIB_CRYPTO, CRYPTO_R_TOO_SMALL_BUFFER); |
288 | 0 | return 0; |
289 | 0 | } |
290 | 0 | *q++ = (unsigned char)((chi << 4) | cli); |
291 | 0 | } |
292 | 0 | } |
293 | | |
294 | 0 | if (buflen != NULL) |
295 | 0 | *buflen = cnt; |
296 | 0 | return 1; |
297 | 0 | } |
298 | | |
299 | | /* |
300 | | * Given a string of hex digits convert to a buffer |
301 | | */ |
302 | | int OPENSSL_hexstr2buf_ex(unsigned char *buf, size_t buf_n, size_t *buflen, |
303 | | const char *str, const char sep) |
304 | 0 | { |
305 | 0 | return hexstr2buf_sep(buf, buf_n, buflen, str, sep); |
306 | 0 | } |
307 | | |
308 | | unsigned char *ossl_hexstr2buf_sep(const char *str, long *buflen, |
309 | | const char sep) |
310 | 0 | { |
311 | 0 | unsigned char *buf; |
312 | 0 | size_t buf_n, tmp_buflen; |
313 | |
|
314 | 0 | buf_n = strlen(str); |
315 | 0 | if (buf_n <= 1) { |
316 | 0 | ERR_raise(ERR_LIB_CRYPTO, CRYPTO_R_HEX_STRING_TOO_SHORT); |
317 | 0 | return NULL; |
318 | 0 | } |
319 | 0 | buf_n /= 2; |
320 | 0 | if ((buf = OPENSSL_malloc(buf_n)) == NULL) |
321 | 0 | return NULL; |
322 | | |
323 | 0 | if (buflen != NULL) |
324 | 0 | *buflen = 0; |
325 | 0 | tmp_buflen = 0; |
326 | 0 | if (hexstr2buf_sep(buf, buf_n, &tmp_buflen, str, sep)) { |
327 | 0 | if (buflen != NULL) |
328 | 0 | *buflen = (long)tmp_buflen; |
329 | 0 | return buf; |
330 | 0 | } |
331 | 0 | OPENSSL_free(buf); |
332 | 0 | return NULL; |
333 | 0 | } |
334 | | |
335 | | unsigned char *OPENSSL_hexstr2buf(const char *str, long *buflen) |
336 | 0 | { |
337 | 0 | return ossl_hexstr2buf_sep(str, buflen, DEFAULT_SEPARATOR); |
338 | 0 | } |
339 | | |
340 | | static int buf2hexstr_sep(char *str, size_t str_n, size_t *strlength, |
341 | | const unsigned char *buf, size_t buflen, |
342 | | const char sep) |
343 | 0 | { |
344 | 0 | char *q; |
345 | 0 | int has_sep = (sep != CH_ZERO); |
346 | 0 | size_t i, len = has_sep ? buflen * 3 : 1 + buflen * 2; |
347 | |
|
348 | 0 | if (buflen > (has_sep ? SIZE_MAX / 3 : (SIZE_MAX - 1) / 2)) { |
349 | 0 | ERR_raise(ERR_LIB_CRYPTO, CRYPTO_R_TOO_MANY_BYTES); |
350 | 0 | return 0; |
351 | 0 | } |
352 | | |
353 | 0 | if (len == 0) |
354 | 0 | ++len; |
355 | 0 | if (strlength != NULL) |
356 | 0 | *strlength = len; |
357 | 0 | if (str == NULL) |
358 | 0 | return 1; |
359 | | |
360 | 0 | if (str_n < len) { |
361 | 0 | ERR_raise(ERR_LIB_CRYPTO, CRYPTO_R_TOO_SMALL_BUFFER); |
362 | 0 | return 0; |
363 | 0 | } |
364 | | |
365 | 0 | q = str; |
366 | 0 | for (i = 0; i < buflen; i++) { |
367 | 0 | q += ossl_to_hex(q, buf[i]); |
368 | 0 | if (has_sep) |
369 | 0 | *q++ = sep; |
370 | 0 | } |
371 | 0 | if (has_sep && buflen > 0) |
372 | 0 | --q; |
373 | 0 | *q = CH_ZERO; |
374 | |
|
375 | | #ifdef CHARSET_EBCDIC |
376 | | ebcdic2ascii(str, str, q - str); |
377 | | #endif |
378 | 0 | return 1; |
379 | 0 | } |
380 | | |
381 | | int OPENSSL_buf2hexstr_ex(char *str, size_t str_n, size_t *strlength, |
382 | | const unsigned char *buf, size_t buflen, |
383 | | const char sep) |
384 | 0 | { |
385 | 0 | return buf2hexstr_sep(str, str_n, strlength, buf, buflen, sep); |
386 | 0 | } |
387 | | |
388 | | char *ossl_buf2hexstr_sep(const unsigned char *buf, long buflen, char sep) |
389 | 0 | { |
390 | 0 | char *tmp; |
391 | 0 | size_t tmp_n; |
392 | |
|
393 | 0 | if (buflen < 0) |
394 | 0 | return NULL; |
395 | 0 | if (buflen == 0) |
396 | 0 | return OPENSSL_zalloc(1); |
397 | | |
398 | 0 | if ((sep != CH_ZERO && (size_t)buflen > SIZE_MAX / 3) |
399 | 0 | || (sep == CH_ZERO && (size_t)buflen > (SIZE_MAX - 1) / 2)) { |
400 | 0 | ERR_raise(ERR_LIB_CRYPTO, CRYPTO_R_TOO_MANY_BYTES); |
401 | 0 | return NULL; |
402 | 0 | } |
403 | | |
404 | 0 | tmp_n = (sep != CH_ZERO) ? (size_t)buflen * 3 : 1 + (size_t)buflen * 2; |
405 | 0 | if ((tmp = OPENSSL_malloc(tmp_n)) == NULL) |
406 | 0 | return NULL; |
407 | | |
408 | 0 | if (buf2hexstr_sep(tmp, tmp_n, NULL, buf, buflen, sep)) |
409 | 0 | return tmp; |
410 | 0 | OPENSSL_free(tmp); |
411 | 0 | return NULL; |
412 | 0 | } |
413 | | |
414 | | /* |
415 | | * Given a buffer of length 'buflen' return a OPENSSL_malloc'ed string with |
416 | | * its hex representation @@@ (Contents of buffer are always kept in ASCII, |
417 | | * also on EBCDIC machines) |
418 | | */ |
419 | | char *OPENSSL_buf2hexstr(const unsigned char *buf, long buflen) |
420 | 0 | { |
421 | 0 | return ossl_buf2hexstr_sep(buf, buflen, DEFAULT_SEPARATOR); |
422 | 0 | } |
423 | | |
424 | | int openssl_strerror_r(int errnum, char *buf, size_t buflen) |
425 | 0 | { |
426 | | #if defined(_MSC_VER) && _MSC_VER >= 1400 |
427 | | return !strerror_s(buf, buflen, errnum); |
428 | | #elif defined(_GNU_SOURCE) |
429 | | char *err; |
430 | | |
431 | | /* |
432 | | * GNU strerror_r may not actually set buf. |
433 | | * It can return a pointer to some (immutable) static string in which case |
434 | | * buf is left unused. |
435 | | */ |
436 | | err = strerror_r(errnum, buf, buflen); |
437 | | if (err == NULL || buflen == 0) |
438 | | return 0; |
439 | | /* |
440 | | * If err is statically allocated, err != buf and we need to copy the data. |
441 | | * If err points somewhere inside buf, OPENSSL_strlcpy can handle this, |
442 | | * since src and dest are not annotated with __restrict and the function |
443 | | * reads src byte for byte and writes to dest. |
444 | | * If err == buf we do not have to copy anything. |
445 | | */ |
446 | | if (err != buf) |
447 | | OPENSSL_strlcpy(buf, err, buflen); |
448 | | return 1; |
449 | | #elif (defined(_POSIX_C_SOURCE) && _POSIX_C_SOURCE >= 200112L) || (defined(_XOPEN_SOURCE) && _XOPEN_SOURCE >= 600) |
450 | | /* |
451 | | * We can use "real" strerror_r. The OpenSSL version differs in that it |
452 | | * gives 1 on success and 0 on failure for consistency with other OpenSSL |
453 | | * functions. Real strerror_r does it the other way around |
454 | | */ |
455 | 0 | return !strerror_r(errnum, buf, buflen); |
456 | | #else |
457 | | char *err; |
458 | | |
459 | | /* Fall back to non-thread safe strerror()...its all we can do */ |
460 | | if (buflen < 2) |
461 | | return 0; |
462 | | err = strerror(errnum); |
463 | | /* Can this ever happen? */ |
464 | | if (err == NULL) |
465 | | return 0; |
466 | | OPENSSL_strlcpy(buf, err, buflen); |
467 | | return 1; |
468 | | #endif |
469 | 0 | } |
470 | | |
471 | | int OPENSSL_strcasecmp(const char *s1, const char *s2) |
472 | 10.8k | { |
473 | 10.8k | int t; |
474 | | |
475 | 129k | while ((t = ossl_tolower(*s1) - ossl_tolower(*s2++)) == 0) |
476 | 129k | if (*s1++ == '\0') |
477 | 10.8k | return 0; |
478 | 0 | return t; |
479 | 10.8k | } |
480 | | |
481 | | int OPENSSL_strncasecmp(const char *s1, const char *s2, size_t n) |
482 | 0 | { |
483 | 0 | int t; |
484 | 0 | size_t i; |
485 | |
|
486 | 0 | for (i = 0; i < n; i++) |
487 | 0 | if ((t = ossl_tolower(*s1) - ossl_tolower(*s2++)) != 0) |
488 | 0 | return t; |
489 | 0 | else if (*s1++ == '\0') |
490 | 0 | return 0; |
491 | 0 | return 0; |
492 | 0 | } |
493 | | |
494 | | size_t ossl_to_hex(char *buf, uint8_t n) |
495 | 0 | { |
496 | 0 | static const char hexdig[] = "0123456789ABCDEF"; |
497 | |
|
498 | 0 | return to_hex(buf, n, hexdig); |
499 | 0 | } |