/src/openssl31/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 | | |
18 | 0 | #define DEFAULT_SEPARATOR ':' |
19 | 2.18G | #define CH_ZERO '\0' |
20 | | |
21 | | char *CRYPTO_strdup(const char *str, const char* file, int line) |
22 | 112M | { |
23 | 112M | char *ret; |
24 | | |
25 | 112M | if (str == NULL) |
26 | 0 | return NULL; |
27 | 112M | ret = CRYPTO_malloc(strlen(str) + 1, file, line); |
28 | 112M | if (ret != NULL) |
29 | 112M | strcpy(ret, str); |
30 | 112M | return ret; |
31 | 112M | } |
32 | | |
33 | | char *CRYPTO_strndup(const char *str, size_t s, const char* file, int line) |
34 | 232M | { |
35 | 232M | size_t maxlen; |
36 | 232M | char *ret; |
37 | | |
38 | 232M | if (str == NULL) |
39 | 0 | return NULL; |
40 | | |
41 | 232M | maxlen = OPENSSL_strnlen(str, s); |
42 | | |
43 | 232M | ret = CRYPTO_malloc(maxlen + 1, file, line); |
44 | 232M | if (ret) { |
45 | 232M | memcpy(ret, str, maxlen); |
46 | 232M | ret[maxlen] = CH_ZERO; |
47 | 232M | } |
48 | 232M | return ret; |
49 | 232M | } |
50 | | |
51 | | void *CRYPTO_memdup(const void *data, size_t siz, const char* file, int line) |
52 | 20.0M | { |
53 | 20.0M | void *ret; |
54 | | |
55 | 20.0M | if (data == NULL || siz >= INT_MAX) |
56 | 0 | return NULL; |
57 | | |
58 | 20.0M | ret = CRYPTO_malloc(siz, file, line); |
59 | 20.0M | if (ret == NULL) { |
60 | 0 | ERR_raise(ERR_LIB_CRYPTO, ERR_R_MALLOC_FAILURE); |
61 | 0 | return NULL; |
62 | 0 | } |
63 | 20.0M | return memcpy(ret, data, siz); |
64 | 20.0M | } |
65 | | |
66 | | size_t OPENSSL_strnlen(const char *str, size_t maxlen) |
67 | 407M | { |
68 | 407M | const char *p; |
69 | | |
70 | 2.13G | for (p = str; maxlen-- != 0 && *p != CH_ZERO; ++p) ; |
71 | | |
72 | 407M | return p - str; |
73 | 407M | } |
74 | | |
75 | | size_t OPENSSL_strlcpy(char *dst, const char *src, size_t size) |
76 | 51.2M | { |
77 | 51.2M | size_t l = 0; |
78 | 551M | for (; size > 1 && *src; size--) { |
79 | 500M | *dst++ = *src++; |
80 | 500M | l++; |
81 | 500M | } |
82 | 51.2M | if (size) |
83 | 51.1M | *dst = CH_ZERO; |
84 | 51.2M | return l + strlen(src); |
85 | 51.2M | } |
86 | | |
87 | | size_t OPENSSL_strlcat(char *dst, const char *src, size_t size) |
88 | 38.5M | { |
89 | 38.5M | size_t l = 0; |
90 | 490M | for (; size > 0 && *dst; size--, dst++) |
91 | 452M | l++; |
92 | 38.5M | return l + OPENSSL_strlcpy(dst, src, size); |
93 | 38.5M | } |
94 | | |
95 | | int OPENSSL_hexchar2int(unsigned char c) |
96 | 102 | { |
97 | | #ifdef CHARSET_EBCDIC |
98 | | c = os_toebcdic[c]; |
99 | | #endif |
100 | | |
101 | 102 | switch (c) { |
102 | 3 | case '0': |
103 | 3 | return 0; |
104 | 6 | case '1': |
105 | 6 | return 1; |
106 | 6 | case '2': |
107 | 6 | return 2; |
108 | 6 | case '3': |
109 | 6 | return 3; |
110 | 13 | case '4': |
111 | 13 | return 4; |
112 | 5 | case '5': |
113 | 5 | return 5; |
114 | 5 | case '6': |
115 | 5 | return 6; |
116 | 7 | case '7': |
117 | 7 | return 7; |
118 | 7 | case '8': |
119 | 7 | return 8; |
120 | 8 | case '9': |
121 | 8 | return 9; |
122 | 10 | case 'a': case 'A': |
123 | 10 | return 0x0A; |
124 | 2 | case 'b': case 'B': |
125 | 2 | return 0x0B; |
126 | 1 | case 'c': case 'C': |
127 | 1 | return 0x0C; |
128 | 6 | case 'd': case 'D': |
129 | 6 | return 0x0D; |
130 | 3 | case 'e': case 'E': |
131 | 3 | return 0x0E; |
132 | 4 | case 'f': case 'F': |
133 | 4 | return 0x0F; |
134 | 102 | } |
135 | 10 | return -1; |
136 | 102 | } |
137 | | |
138 | | static int hexstr2buf_sep(unsigned char *buf, size_t buf_n, size_t *buflen, |
139 | | const char *str, const char sep) |
140 | 0 | { |
141 | 0 | unsigned char *q; |
142 | 0 | unsigned char ch, cl; |
143 | 0 | int chi, cli; |
144 | 0 | const unsigned char *p; |
145 | 0 | size_t cnt; |
146 | |
|
147 | 0 | for (p = (const unsigned char *)str, q = buf, cnt = 0; *p; ) { |
148 | 0 | ch = *p++; |
149 | | /* A separator of CH_ZERO means there is no separator */ |
150 | 0 | if (ch == sep && sep != CH_ZERO) |
151 | 0 | continue; |
152 | 0 | cl = *p++; |
153 | 0 | if (!cl) { |
154 | 0 | ERR_raise(ERR_LIB_CRYPTO, CRYPTO_R_ODD_NUMBER_OF_DIGITS); |
155 | 0 | return 0; |
156 | 0 | } |
157 | 0 | cli = OPENSSL_hexchar2int(cl); |
158 | 0 | chi = OPENSSL_hexchar2int(ch); |
159 | 0 | if (cli < 0 || chi < 0) { |
160 | 0 | ERR_raise(ERR_LIB_CRYPTO, CRYPTO_R_ILLEGAL_HEX_DIGIT); |
161 | 0 | return 0; |
162 | 0 | } |
163 | 0 | cnt++; |
164 | 0 | if (q != NULL) { |
165 | 0 | if (cnt > buf_n) { |
166 | 0 | ERR_raise(ERR_LIB_CRYPTO, CRYPTO_R_TOO_SMALL_BUFFER); |
167 | 0 | return 0; |
168 | 0 | } |
169 | 0 | *q++ = (unsigned char)((chi << 4) | cli); |
170 | 0 | } |
171 | 0 | } |
172 | | |
173 | 0 | if (buflen != NULL) |
174 | 0 | *buflen = cnt; |
175 | 0 | return 1; |
176 | 0 | } |
177 | | |
178 | | /* |
179 | | * Given a string of hex digits convert to a buffer |
180 | | */ |
181 | | int OPENSSL_hexstr2buf_ex(unsigned char *buf, size_t buf_n, size_t *buflen, |
182 | | const char *str, const char sep) |
183 | 0 | { |
184 | 0 | return hexstr2buf_sep(buf, buf_n, buflen, str, sep); |
185 | 0 | } |
186 | | |
187 | | unsigned char *ossl_hexstr2buf_sep(const char *str, long *buflen, |
188 | | const char sep) |
189 | 0 | { |
190 | 0 | unsigned char *buf; |
191 | 0 | size_t buf_n, tmp_buflen; |
192 | |
|
193 | 0 | buf_n = strlen(str); |
194 | 0 | if (buf_n <= 1) { |
195 | 0 | ERR_raise(ERR_LIB_CRYPTO, CRYPTO_R_HEX_STRING_TOO_SHORT); |
196 | 0 | return NULL; |
197 | 0 | } |
198 | 0 | buf_n /= 2; |
199 | 0 | if ((buf = OPENSSL_malloc(buf_n)) == NULL) { |
200 | 0 | ERR_raise(ERR_LIB_CRYPTO, ERR_R_MALLOC_FAILURE); |
201 | 0 | return NULL; |
202 | 0 | } |
203 | | |
204 | 0 | if (buflen != NULL) |
205 | 0 | *buflen = 0; |
206 | 0 | tmp_buflen = 0; |
207 | 0 | if (hexstr2buf_sep(buf, buf_n, &tmp_buflen, str, sep)) { |
208 | 0 | if (buflen != NULL) |
209 | 0 | *buflen = (long)tmp_buflen; |
210 | 0 | return buf; |
211 | 0 | } |
212 | 0 | OPENSSL_free(buf); |
213 | 0 | return NULL; |
214 | 0 | } |
215 | | |
216 | | unsigned char *OPENSSL_hexstr2buf(const char *str, long *buflen) |
217 | 0 | { |
218 | 0 | return ossl_hexstr2buf_sep(str, buflen, DEFAULT_SEPARATOR); |
219 | 0 | } |
220 | | |
221 | | static int buf2hexstr_sep(char *str, size_t str_n, size_t *strlength, |
222 | | const unsigned char *buf, size_t buflen, |
223 | | const char sep) |
224 | 74.6k | { |
225 | 74.6k | static const char hexdig[] = "0123456789ABCDEF"; |
226 | 74.6k | const unsigned char *p; |
227 | 74.6k | char *q; |
228 | 74.6k | size_t i; |
229 | 74.6k | int has_sep = (sep != CH_ZERO); |
230 | 74.6k | size_t len = has_sep ? buflen * 3 : 1 + buflen * 2; |
231 | | |
232 | 74.6k | if (len == 0) |
233 | 0 | ++len; |
234 | 74.6k | if (strlength != NULL) |
235 | 0 | *strlength = len; |
236 | 74.6k | if (str == NULL) |
237 | 0 | return 1; |
238 | | |
239 | 74.6k | if (str_n < len) { |
240 | 0 | ERR_raise(ERR_LIB_CRYPTO, CRYPTO_R_TOO_SMALL_BUFFER); |
241 | 0 | return 0; |
242 | 0 | } |
243 | | |
244 | 74.6k | q = str; |
245 | 19.0M | for (i = 0, p = buf; i < buflen; i++, p++) { |
246 | 19.0M | *q++ = hexdig[(*p >> 4) & 0xf]; |
247 | 19.0M | *q++ = hexdig[*p & 0xf]; |
248 | 19.0M | if (has_sep) |
249 | 18.8M | *q++ = sep; |
250 | 19.0M | } |
251 | 74.6k | if (has_sep && buflen > 0) |
252 | 51.4k | --q; |
253 | 74.6k | *q = CH_ZERO; |
254 | | |
255 | | #ifdef CHARSET_EBCDIC |
256 | | ebcdic2ascii(str, str, q - str); |
257 | | #endif |
258 | 74.6k | return 1; |
259 | 74.6k | } |
260 | | |
261 | | int OPENSSL_buf2hexstr_ex(char *str, size_t str_n, size_t *strlength, |
262 | | const unsigned char *buf, size_t buflen, |
263 | | const char sep) |
264 | 0 | { |
265 | 0 | return buf2hexstr_sep(str, str_n, strlength, buf, buflen, sep); |
266 | 0 | } |
267 | | |
268 | | char *ossl_buf2hexstr_sep(const unsigned char *buf, long buflen, char sep) |
269 | 81.8k | { |
270 | 81.8k | char *tmp; |
271 | 81.8k | size_t tmp_n; |
272 | | |
273 | 81.8k | if (buflen == 0) |
274 | 7.13k | return OPENSSL_zalloc(1); |
275 | | |
276 | 74.6k | tmp_n = (sep != CH_ZERO) ? buflen * 3 : 1 + buflen * 2; |
277 | 74.6k | if ((tmp = OPENSSL_malloc(tmp_n)) == NULL) { |
278 | 0 | ERR_raise(ERR_LIB_CRYPTO, ERR_R_MALLOC_FAILURE); |
279 | 0 | return NULL; |
280 | 0 | } |
281 | | |
282 | 74.6k | if (buf2hexstr_sep(tmp, tmp_n, NULL, buf, buflen, sep)) |
283 | 74.6k | return tmp; |
284 | 0 | OPENSSL_free(tmp); |
285 | 0 | return NULL; |
286 | 74.6k | } |
287 | | |
288 | | |
289 | | /* |
290 | | * Given a buffer of length 'len' return a OPENSSL_malloc'ed string with its |
291 | | * hex representation @@@ (Contents of buffer are always kept in ASCII, also |
292 | | * on EBCDIC machines) |
293 | | */ |
294 | | char *OPENSSL_buf2hexstr(const unsigned char *buf, long buflen) |
295 | 58.5k | { |
296 | 58.5k | return ossl_buf2hexstr_sep(buf, buflen, ':'); |
297 | 58.5k | } |
298 | | |
299 | | int openssl_strerror_r(int errnum, char *buf, size_t buflen) |
300 | 0 | { |
301 | | #if defined(_MSC_VER) && _MSC_VER>=1400 && !defined(_WIN32_WCE) |
302 | | return !strerror_s(buf, buflen, errnum); |
303 | | #elif defined(_GNU_SOURCE) |
304 | | char *err; |
305 | | |
306 | | /* |
307 | | * GNU strerror_r may not actually set buf. |
308 | | * It can return a pointer to some (immutable) static string in which case |
309 | | * buf is left unused. |
310 | | */ |
311 | | err = strerror_r(errnum, buf, buflen); |
312 | | if (err == NULL || buflen == 0) |
313 | | return 0; |
314 | | /* |
315 | | * If err is statically allocated, err != buf and we need to copy the data. |
316 | | * If err points somewhere inside buf, OPENSSL_strlcpy can handle this, |
317 | | * since src and dest are not annotated with __restrict and the function |
318 | | * reads src byte for byte and writes to dest. |
319 | | * If err == buf we do not have to copy anything. |
320 | | */ |
321 | | if (err != buf) |
322 | | OPENSSL_strlcpy(buf, err, buflen); |
323 | | return 1; |
324 | | #elif (defined(_POSIX_C_SOURCE) && _POSIX_C_SOURCE >= 200112L) || \ |
325 | | (defined(_XOPEN_SOURCE) && _XOPEN_SOURCE >= 600) |
326 | | /* |
327 | | * We can use "real" strerror_r. The OpenSSL version differs in that it |
328 | | * gives 1 on success and 0 on failure for consistency with other OpenSSL |
329 | | * functions. Real strerror_r does it the other way around |
330 | | */ |
331 | 0 | return !strerror_r(errnum, buf, buflen); |
332 | | #else |
333 | | char *err; |
334 | | |
335 | | /* Fall back to non-thread safe strerror()...its all we can do */ |
336 | | if (buflen < 2) |
337 | | return 0; |
338 | | err = strerror(errnum); |
339 | | /* Can this ever happen? */ |
340 | | if (err == NULL) |
341 | | return 0; |
342 | | OPENSSL_strlcpy(buf, err, buflen); |
343 | | return 1; |
344 | | #endif |
345 | 0 | } |
346 | | |
347 | | int OPENSSL_strcasecmp(const char *s1, const char *s2) |
348 | 241M | { |
349 | 241M | int t; |
350 | | |
351 | 1.40G | while ((t = ossl_tolower(*s1) - ossl_tolower(*s2++)) == 0) |
352 | 1.37G | if (*s1++ == '\0') |
353 | 210M | return 0; |
354 | 30.9M | return t; |
355 | 241M | } |
356 | | |
357 | | int OPENSSL_strncasecmp(const char *s1, const char *s2, size_t n) |
358 | 5.15M | { |
359 | 5.15M | int t; |
360 | 5.15M | size_t i; |
361 | | |
362 | 9.18M | for (i = 0; i < n; i++) |
363 | 8.03M | if ((t = ossl_tolower(*s1) - ossl_tolower(*s2++)) != 0) |
364 | 4.00M | return t; |
365 | 4.02M | else if (*s1++ == '\0') |
366 | 0 | return 0; |
367 | 1.14M | return 0; |
368 | 5.15M | } |