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