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 #define DEFAULT_SEPARATOR ':'
20 #define CH_ZERO '\0'
21
CRYPTO_strdup(const char * str,const char * file,int line)22 char *CRYPTO_strdup(const char *str, const char* file, int line)
23 {
24 char *ret;
25 size_t len;
26
27 if (str == NULL)
28 return NULL;
29
30 len = strlen(str) + 1;
31 ret = CRYPTO_malloc(len, file, line);
32 if (ret != NULL)
33 memcpy(ret, str, len);
34 return ret;
35 }
36
CRYPTO_strndup(const char * str,size_t s,const char * file,int line)37 char *CRYPTO_strndup(const char *str, size_t s, const char* file, int line)
38 {
39 size_t maxlen;
40 char *ret;
41
42 if (str == NULL)
43 return NULL;
44
45 maxlen = OPENSSL_strnlen(str, s);
46
47 ret = CRYPTO_malloc(maxlen + 1, file, line);
48 if (ret) {
49 memcpy(ret, str, maxlen);
50 ret[maxlen] = CH_ZERO;
51 }
52 return ret;
53 }
54
CRYPTO_memdup(const void * data,size_t siz,const char * file,int line)55 void *CRYPTO_memdup(const void *data, size_t siz, const char* file, int line)
56 {
57 void *ret;
58
59 if (data == NULL || siz >= INT_MAX)
60 return NULL;
61
62 ret = CRYPTO_malloc(siz, file, line);
63 if (ret == NULL)
64 return NULL;
65 return memcpy(ret, data, siz);
66 }
67
OPENSSL_strnlen(const char * str,size_t maxlen)68 size_t OPENSSL_strnlen(const char *str, size_t maxlen)
69 {
70 const char *p;
71
72 for (p = str; maxlen-- != 0 && *p != CH_ZERO; ++p) ;
73
74 return p - str;
75 }
76
OPENSSL_strlcpy(char * dst,const char * src,size_t size)77 size_t OPENSSL_strlcpy(char *dst, const char *src, size_t size)
78 {
79 size_t l = 0;
80 for (; size > 1 && *src; size--) {
81 *dst++ = *src++;
82 l++;
83 }
84 if (size)
85 *dst = CH_ZERO;
86 return l + strlen(src);
87 }
88
OPENSSL_strlcat(char * dst,const char * src,size_t size)89 size_t OPENSSL_strlcat(char *dst, const char *src, size_t size)
90 {
91 size_t l = 0;
92 for (; size > 0 && *dst; size--, dst++)
93 l++;
94 return l + OPENSSL_strlcpy(dst, src, size);
95 }
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 */
OPENSSL_strtoul(const char * str,char ** endptr,int base,unsigned long * num)126 int OPENSSL_strtoul(const char *str, char **endptr, int base,
127 unsigned long *num)
128 {
129 char *tmp_endptr;
130 char **internal_endptr = endptr == NULL ? &tmp_endptr : endptr;
131
132 errno = 0;
133
134 *internal_endptr = (char *)str;
135
136 if (num == NULL)
137 return 0;
138
139 if (str == NULL)
140 return 0;
141
142 /* Fail on negative input */
143 if (*str == '-')
144 return 0;
145
146 *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 if (errno != 0 ||
158 (endptr == NULL && **internal_endptr != '\0') ||
159 (str == *internal_endptr))
160 return 0;
161
162 return 1;
163 }
164
OPENSSL_hexchar2int(unsigned char c)165 int OPENSSL_hexchar2int(unsigned char c)
166 {
167 #ifdef CHARSET_EBCDIC
168 c = os_toebcdic[c];
169 #endif
170
171 switch (c) {
172 case '0':
173 return 0;
174 case '1':
175 return 1;
176 case '2':
177 return 2;
178 case '3':
179 return 3;
180 case '4':
181 return 4;
182 case '5':
183 return 5;
184 case '6':
185 return 6;
186 case '7':
187 return 7;
188 case '8':
189 return 8;
190 case '9':
191 return 9;
192 case 'a': case 'A':
193 return 0x0A;
194 case 'b': case 'B':
195 return 0x0B;
196 case 'c': case 'C':
197 return 0x0C;
198 case 'd': case 'D':
199 return 0x0D;
200 case 'e': case 'E':
201 return 0x0E;
202 case 'f': case 'F':
203 return 0x0F;
204 }
205 return -1;
206 }
207
hexstr2buf_sep(unsigned char * buf,size_t buf_n,size_t * buflen,const char * str,const char sep)208 static int hexstr2buf_sep(unsigned char *buf, size_t buf_n, size_t *buflen,
209 const char *str, const char sep)
210 {
211 unsigned char *q;
212 unsigned char ch, cl;
213 int chi, cli;
214 const unsigned char *p;
215 size_t cnt;
216
217 for (p = (const unsigned char *)str, q = buf, cnt = 0; *p; ) {
218 ch = *p++;
219 /* A separator of CH_ZERO means there is no separator */
220 if (ch == sep && sep != CH_ZERO)
221 continue;
222 cl = *p++;
223 if (!cl) {
224 ERR_raise(ERR_LIB_CRYPTO, CRYPTO_R_ODD_NUMBER_OF_DIGITS);
225 return 0;
226 }
227 cli = OPENSSL_hexchar2int(cl);
228 chi = OPENSSL_hexchar2int(ch);
229 if (cli < 0 || chi < 0) {
230 ERR_raise(ERR_LIB_CRYPTO, CRYPTO_R_ILLEGAL_HEX_DIGIT);
231 return 0;
232 }
233 cnt++;
234 if (q != NULL) {
235 if (cnt > buf_n) {
236 ERR_raise(ERR_LIB_CRYPTO, CRYPTO_R_TOO_SMALL_BUFFER);
237 return 0;
238 }
239 *q++ = (unsigned char)((chi << 4) | cli);
240 }
241 }
242
243 if (buflen != NULL)
244 *buflen = cnt;
245 return 1;
246 }
247
248 /*
249 * Given a string of hex digits convert to a buffer
250 */
OPENSSL_hexstr2buf_ex(unsigned char * buf,size_t buf_n,size_t * buflen,const char * str,const char sep)251 int OPENSSL_hexstr2buf_ex(unsigned char *buf, size_t buf_n, size_t *buflen,
252 const char *str, const char sep)
253 {
254 return hexstr2buf_sep(buf, buf_n, buflen, str, sep);
255 }
256
ossl_hexstr2buf_sep(const char * str,long * buflen,const char sep)257 unsigned char *ossl_hexstr2buf_sep(const char *str, long *buflen,
258 const char sep)
259 {
260 unsigned char *buf;
261 size_t buf_n, tmp_buflen;
262
263 buf_n = strlen(str);
264 if (buf_n <= 1) {
265 ERR_raise(ERR_LIB_CRYPTO, CRYPTO_R_HEX_STRING_TOO_SHORT);
266 return NULL;
267 }
268 buf_n /= 2;
269 if ((buf = OPENSSL_malloc(buf_n)) == NULL)
270 return NULL;
271
272 if (buflen != NULL)
273 *buflen = 0;
274 tmp_buflen = 0;
275 if (hexstr2buf_sep(buf, buf_n, &tmp_buflen, str, sep)) {
276 if (buflen != NULL)
277 *buflen = (long)tmp_buflen;
278 return buf;
279 }
280 OPENSSL_free(buf);
281 return NULL;
282 }
283
OPENSSL_hexstr2buf(const char * str,long * buflen)284 unsigned char *OPENSSL_hexstr2buf(const char *str, long *buflen)
285 {
286 return ossl_hexstr2buf_sep(str, buflen, DEFAULT_SEPARATOR);
287 }
288
buf2hexstr_sep(char * str,size_t str_n,size_t * strlength,const unsigned char * buf,size_t buflen,const char sep)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 {
293 char *q;
294 int has_sep = (sep != CH_ZERO);
295 size_t i, len = has_sep ? buflen * 3 : 1 + buflen * 2;
296
297 if (len == 0)
298 ++len;
299 if (strlength != NULL)
300 *strlength = len;
301 if (str == NULL)
302 return 1;
303
304 if (str_n < len) {
305 ERR_raise(ERR_LIB_CRYPTO, CRYPTO_R_TOO_SMALL_BUFFER);
306 return 0;
307 }
308
309 q = str;
310 for (i = 0; i < buflen; i++) {
311 q += ossl_to_hex(q, buf[i]);
312 if (has_sep)
313 *q++ = sep;
314 }
315 if (has_sep && buflen > 0)
316 --q;
317 *q = CH_ZERO;
318
319 #ifdef CHARSET_EBCDIC
320 ebcdic2ascii(str, str, q - str);
321 #endif
322 return 1;
323 }
324
OPENSSL_buf2hexstr_ex(char * str,size_t str_n,size_t * strlength,const unsigned char * buf,size_t buflen,const char sep)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 {
329 return buf2hexstr_sep(str, str_n, strlength, buf, buflen, sep);
330 }
331
ossl_buf2hexstr_sep(const unsigned char * buf,long buflen,char sep)332 char *ossl_buf2hexstr_sep(const unsigned char *buf, long buflen, char sep)
333 {
334 char *tmp;
335 size_t tmp_n;
336
337 if (buflen == 0)
338 return OPENSSL_zalloc(1);
339
340 tmp_n = (sep != CH_ZERO) ? buflen * 3 : 1 + buflen * 2;
341 if ((tmp = OPENSSL_malloc(tmp_n)) == NULL)
342 return NULL;
343
344 if (buf2hexstr_sep(tmp, tmp_n, NULL, buf, buflen, sep))
345 return tmp;
346 OPENSSL_free(tmp);
347 return NULL;
348 }
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 */
OPENSSL_buf2hexstr(const unsigned char * buf,long buflen)356 char *OPENSSL_buf2hexstr(const unsigned char *buf, long buflen)
357 {
358 return ossl_buf2hexstr_sep(buf, buflen, DEFAULT_SEPARATOR);
359 }
360
openssl_strerror_r(int errnum,char * buf,size_t buflen)361 int openssl_strerror_r(int errnum, char *buf, size_t buflen)
362 {
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 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 }
408
OPENSSL_strcasecmp(const char * s1,const char * s2)409 int OPENSSL_strcasecmp(const char *s1, const char *s2)
410 {
411 int t;
412
413 while ((t = ossl_tolower(*s1) - ossl_tolower(*s2++)) == 0)
414 if (*s1++ == '\0')
415 return 0;
416 return t;
417 }
418
OPENSSL_strncasecmp(const char * s1,const char * s2,size_t n)419 int OPENSSL_strncasecmp(const char *s1, const char *s2, size_t n)
420 {
421 int t;
422 size_t i;
423
424 for (i = 0; i < n; i++)
425 if ((t = ossl_tolower(*s1) - ossl_tolower(*s2++)) != 0)
426 return t;
427 else if (*s1++ == '\0')
428 return 0;
429 return 0;
430 }
431
ossl_to_hex(char * buf,uint8_t n)432 size_t ossl_to_hex(char *buf, uint8_t n)
433 {
434 static const char hexdig[] = "0123456789ABCDEF";
435
436 return to_hex(buf, n, hexdig);
437 }
438