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