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