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 "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 #define DEFAULT_SEPARATOR ':'
19 #define CH_ZERO '\0'
20
CRYPTO_strdup(const char * str,const char * file,int line)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
CRYPTO_strndup(const char * str,size_t s,const char * file,int line)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);
46 ret[maxlen] = CH_ZERO;
47 }
48 return ret;
49 }
50
CRYPTO_memdup(const void * data,size_t siz,const char * file,int line)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);
59 if (ret == NULL) {
60 ERR_raise(ERR_LIB_CRYPTO, ERR_R_MALLOC_FAILURE);
61 return NULL;
62 }
63 return memcpy(ret, data, siz);
64 }
65
OPENSSL_strnlen(const char * str,size_t maxlen)66 size_t OPENSSL_strnlen(const char *str, size_t maxlen)
67 {
68 const char *p;
69
70 for (p = str; maxlen-- != 0 && *p != CH_ZERO; ++p) ;
71
72 return p - str;
73 }
74
OPENSSL_strlcpy(char * dst,const char * src,size_t size)75 size_t OPENSSL_strlcpy(char *dst, const char *src, size_t size)
76 {
77 size_t l = 0;
78 for (; size > 1 && *src; size--) {
79 *dst++ = *src++;
80 l++;
81 }
82 if (size)
83 *dst = CH_ZERO;
84 return l + strlen(src);
85 }
86
OPENSSL_strlcat(char * dst,const char * src,size_t size)87 size_t OPENSSL_strlcat(char *dst, const char *src, size_t size)
88 {
89 size_t l = 0;
90 for (; size > 0 && *dst; size--, dst++)
91 l++;
92 return l + OPENSSL_strlcpy(dst, src, size);
93 }
94
OPENSSL_hexchar2int(unsigned char c)95 int OPENSSL_hexchar2int(unsigned char c)
96 {
97 #ifdef CHARSET_EBCDIC
98 c = os_toebcdic[c];
99 #endif
100
101 switch (c) {
102 case '0':
103 return 0;
104 case '1':
105 return 1;
106 case '2':
107 return 2;
108 case '3':
109 return 3;
110 case '4':
111 return 4;
112 case '5':
113 return 5;
114 case '6':
115 return 6;
116 case '7':
117 return 7;
118 case '8':
119 return 8;
120 case '9':
121 return 9;
122 case 'a': case 'A':
123 return 0x0A;
124 case 'b': case 'B':
125 return 0x0B;
126 case 'c': case 'C':
127 return 0x0C;
128 case 'd': case 'D':
129 return 0x0D;
130 case 'e': case 'E':
131 return 0x0E;
132 case 'f': case 'F':
133 return 0x0F;
134 }
135 return -1;
136 }
137
hexstr2buf_sep(unsigned char * buf,size_t buf_n,size_t * buflen,const char * str,const char sep)138 static int hexstr2buf_sep(unsigned char *buf, size_t buf_n, size_t *buflen,
139 const char *str, const char sep)
140 {
141 unsigned char *q;
142 unsigned char ch, cl;
143 int chi, cli;
144 const unsigned char *p;
145 size_t cnt;
146
147 for (p = (const unsigned char *)str, q = buf, cnt = 0; *p; ) {
148 ch = *p++;
149 /* A separator of CH_ZERO means there is no separator */
150 if (ch == sep && sep != CH_ZERO)
151 continue;
152 cl = *p++;
153 if (!cl) {
154 ERR_raise(ERR_LIB_CRYPTO, CRYPTO_R_ODD_NUMBER_OF_DIGITS);
155 return 0;
156 }
157 cli = OPENSSL_hexchar2int(cl);
158 chi = OPENSSL_hexchar2int(ch);
159 if (cli < 0 || chi < 0) {
160 ERR_raise(ERR_LIB_CRYPTO, CRYPTO_R_ILLEGAL_HEX_DIGIT);
161 return 0;
162 }
163 cnt++;
164 if (q != NULL) {
165 if (cnt > buf_n) {
166 ERR_raise(ERR_LIB_CRYPTO, CRYPTO_R_TOO_SMALL_BUFFER);
167 return 0;
168 }
169 *q++ = (unsigned char)((chi << 4) | cli);
170 }
171 }
172
173 if (buflen != NULL)
174 *buflen = cnt;
175 return 1;
176 }
177
178 /*
179 * Given a string of hex digits convert to a buffer
180 */
OPENSSL_hexstr2buf_ex(unsigned char * buf,size_t buf_n,size_t * buflen,const char * str,const char sep)181 int OPENSSL_hexstr2buf_ex(unsigned char *buf, size_t buf_n, size_t *buflen,
182 const char *str, const char sep)
183 {
184 return hexstr2buf_sep(buf, buf_n, buflen, str, sep);
185 }
186
ossl_hexstr2buf_sep(const char * str,long * buflen,const char sep)187 unsigned char *ossl_hexstr2buf_sep(const char *str, long *buflen,
188 const char sep)
189 {
190 unsigned char *buf;
191 size_t buf_n, tmp_buflen;
192
193 buf_n = strlen(str);
194 if (buf_n <= 1) {
195 ERR_raise(ERR_LIB_CRYPTO, CRYPTO_R_HEX_STRING_TOO_SHORT);
196 return NULL;
197 }
198 buf_n /= 2;
199 if ((buf = OPENSSL_malloc(buf_n)) == NULL) {
200 ERR_raise(ERR_LIB_CRYPTO, ERR_R_MALLOC_FAILURE);
201 return NULL;
202 }
203
204 if (buflen != NULL)
205 *buflen = 0;
206 tmp_buflen = 0;
207 if (hexstr2buf_sep(buf, buf_n, &tmp_buflen, str, sep)) {
208 if (buflen != NULL)
209 *buflen = (long)tmp_buflen;
210 return buf;
211 }
212 OPENSSL_free(buf);
213 return NULL;
214 }
215
OPENSSL_hexstr2buf(const char * str,long * buflen)216 unsigned char *OPENSSL_hexstr2buf(const char *str, long *buflen)
217 {
218 return ossl_hexstr2buf_sep(str, buflen, DEFAULT_SEPARATOR);
219 }
220
buf2hexstr_sep(char * str,size_t str_n,size_t * strlength,const unsigned char * buf,size_t buflen,const char sep)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 {
225 static const char hexdig[] = "0123456789ABCDEF";
226 const unsigned char *p;
227 char *q;
228 size_t i;
229 int has_sep = (sep != CH_ZERO);
230 size_t len = has_sep ? buflen * 3 : 1 + buflen * 2;
231
232 if (len == 0)
233 ++len;
234 if (strlength != NULL)
235 *strlength = len;
236 if (str == NULL)
237 return 1;
238
239 if (str_n < len) {
240 ERR_raise(ERR_LIB_CRYPTO, CRYPTO_R_TOO_SMALL_BUFFER);
241 return 0;
242 }
243
244 q = str;
245 for (i = 0, p = buf; i < buflen; i++, p++) {
246 *q++ = hexdig[(*p >> 4) & 0xf];
247 *q++ = hexdig[*p & 0xf];
248 if (has_sep)
249 *q++ = sep;
250 }
251 if (has_sep && buflen > 0)
252 --q;
253 *q = CH_ZERO;
254
255 #ifdef CHARSET_EBCDIC
256 ebcdic2ascii(str, str, q - str);
257 #endif
258 return 1;
259 }
260
OPENSSL_buf2hexstr_ex(char * str,size_t str_n,size_t * strlength,const unsigned char * buf,size_t buflen,const char sep)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 {
265 return buf2hexstr_sep(str, str_n, strlength, buf, buflen, sep);
266 }
267
ossl_buf2hexstr_sep(const unsigned char * buf,long buflen,char sep)268 char *ossl_buf2hexstr_sep(const unsigned char *buf, long buflen, char sep)
269 {
270 char *tmp;
271 size_t tmp_n;
272
273 if (buflen == 0)
274 return OPENSSL_zalloc(1);
275
276 tmp_n = (sep != CH_ZERO) ? buflen * 3 : 1 + buflen * 2;
277 if ((tmp = OPENSSL_malloc(tmp_n)) == NULL) {
278 ERR_raise(ERR_LIB_CRYPTO, ERR_R_MALLOC_FAILURE);
279 return NULL;
280 }
281
282 if (buf2hexstr_sep(tmp, tmp_n, NULL, buf, buflen, sep))
283 return tmp;
284 OPENSSL_free(tmp);
285 return NULL;
286 }
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 */
OPENSSL_buf2hexstr(const unsigned char * buf,long buflen)294 char *OPENSSL_buf2hexstr(const unsigned char *buf, long buflen)
295 {
296 return ossl_buf2hexstr_sep(buf, buflen, ':');
297 }
298
openssl_strerror_r(int errnum,char * buf,size_t buflen)299 int openssl_strerror_r(int errnum, char *buf, size_t buflen)
300 {
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 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 }
346
OPENSSL_strcasecmp(const char * s1,const char * s2)347 int OPENSSL_strcasecmp(const char *s1, const char *s2)
348 {
349 int t;
350
351 while ((t = ossl_tolower(*s1) - ossl_tolower(*s2++)) == 0)
352 if (*s1++ == '\0')
353 return 0;
354 return t;
355 }
356
OPENSSL_strncasecmp(const char * s1,const char * s2,size_t n)357 int OPENSSL_strncasecmp(const char *s1, const char *s2, size_t n)
358 {
359 int t;
360 size_t i;
361
362 for (i = 0; i < n; i++)
363 if ((t = ossl_tolower(*s1) - ossl_tolower(*s2++)) != 0)
364 return t;
365 else if (*s1++ == '\0')
366 return 0;
367 return 0;
368 }
369