2 * Copyright 2003-2022 The OpenSSL Project Authors. All Rights Reserved.
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
10 #include "internal/e_os.h"
13 #include <openssl/crypto.h>
14 #include "crypto/ctype.h"
15 #include "internal/cryptlib.h"
16 #include "internal/thread_once.h"
18 #define DEFAULT_SEPARATOR ':'
21 char *CRYPTO_strdup(const char *str, const char* file, int line)
27 ret = CRYPTO_malloc(strlen(str) + 1, file, line);
33 char *CRYPTO_strndup(const char *str, size_t s, const char* file, int line)
41 maxlen = OPENSSL_strnlen(str, s);
43 ret = CRYPTO_malloc(maxlen + 1, file, line);
45 memcpy(ret, str, maxlen);
46 ret[maxlen] = CH_ZERO;
51 void *CRYPTO_memdup(const void *data, size_t siz, const char* file, int line)
55 if (data == NULL || siz >= INT_MAX)
58 ret = CRYPTO_malloc(siz, file, line);
60 ERR_raise(ERR_LIB_CRYPTO, ERR_R_MALLOC_FAILURE);
63 return memcpy(ret, data, siz);
66 size_t OPENSSL_strnlen(const char *str, size_t maxlen)
70 for (p = str; maxlen-- != 0 && *p != CH_ZERO; ++p) ;
75 size_t OPENSSL_strlcpy(char *dst, const char *src, size_t size)
78 for (; size > 1 && *src; size--) {
84 return l + strlen(src);
87 size_t OPENSSL_strlcat(char *dst, const char *src, size_t size)
90 for (; size > 0 && *dst; size--, dst++)
92 return l + OPENSSL_strlcpy(dst, src, size);
95 int OPENSSL_hexchar2int(unsigned char c)
138 static int hexstr2buf_sep(unsigned char *buf, size_t buf_n, size_t *buflen,
139 const char *str, const char sep)
142 unsigned char ch, cl;
144 const unsigned char *p;
147 for (p = (const unsigned char *)str, q = buf, cnt = 0; *p; ) {
149 /* A separator of CH_ZERO means there is no separator */
150 if (ch == sep && sep != CH_ZERO)
154 ERR_raise(ERR_LIB_CRYPTO, CRYPTO_R_ODD_NUMBER_OF_DIGITS);
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);
166 ERR_raise(ERR_LIB_CRYPTO, CRYPTO_R_TOO_SMALL_BUFFER);
169 *q++ = (unsigned char)((chi << 4) | cli);
179 * Given a string of hex digits convert to a buffer
181 int OPENSSL_hexstr2buf_ex(unsigned char *buf, size_t buf_n, size_t *buflen,
182 const char *str, const char sep)
184 return hexstr2buf_sep(buf, buf_n, buflen, str, sep);
187 unsigned char *ossl_hexstr2buf_sep(const char *str, long *buflen,
191 size_t buf_n, tmp_buflen;
195 ERR_raise(ERR_LIB_CRYPTO, CRYPTO_R_HEX_STRING_TOO_SHORT);
199 if ((buf = OPENSSL_malloc(buf_n)) == NULL) {
200 ERR_raise(ERR_LIB_CRYPTO, ERR_R_MALLOC_FAILURE);
207 if (hexstr2buf_sep(buf, buf_n, &tmp_buflen, str, sep)) {
209 *buflen = (long)tmp_buflen;
216 unsigned char *OPENSSL_hexstr2buf(const char *str, long *buflen)
218 return ossl_hexstr2buf_sep(str, buflen, DEFAULT_SEPARATOR);
221 static int buf2hexstr_sep(char *str, size_t str_n, size_t *strlength,
222 const unsigned char *buf, size_t buflen,
225 static const char hexdig[] = "0123456789ABCDEF";
226 const unsigned char *p;
229 int has_sep = (sep != CH_ZERO);
230 size_t len = has_sep ? buflen * 3 : 1 + buflen * 2;
232 if (strlength != NULL)
237 if (str_n < (unsigned long)len) {
238 ERR_raise(ERR_LIB_CRYPTO, CRYPTO_R_TOO_SMALL_BUFFER);
243 for (i = 0, p = buf; i < buflen; i++, p++) {
244 *q++ = hexdig[(*p >> 4) & 0xf];
245 *q++ = hexdig[*p & 0xf];
253 #ifdef CHARSET_EBCDIC
254 ebcdic2ascii(str, str, q - str - 1);
259 int OPENSSL_buf2hexstr_ex(char *str, size_t str_n, size_t *strlength,
260 const unsigned char *buf, size_t buflen,
263 return buf2hexstr_sep(str, str_n, strlength, buf, buflen, sep);
266 char *ossl_buf2hexstr_sep(const unsigned char *buf, long buflen, char sep)
272 return OPENSSL_zalloc(1);
274 tmp_n = (sep != CH_ZERO) ? buflen * 3 : 1 + buflen * 2;
275 if ((tmp = OPENSSL_malloc(tmp_n)) == NULL) {
276 ERR_raise(ERR_LIB_CRYPTO, ERR_R_MALLOC_FAILURE);
280 if (buf2hexstr_sep(tmp, tmp_n, NULL, buf, buflen, sep))
288 * Given a buffer of length 'len' return a OPENSSL_malloc'ed string with its
289 * hex representation @@@ (Contents of buffer are always kept in ASCII, also
290 * on EBCDIC machines)
292 char *OPENSSL_buf2hexstr(const unsigned char *buf, long buflen)
294 return ossl_buf2hexstr_sep(buf, buflen, ':');
297 int openssl_strerror_r(int errnum, char *buf, size_t buflen)
299 #if defined(_MSC_VER) && _MSC_VER>=1400 && !defined(_WIN32_WCE)
300 return !strerror_s(buf, buflen, errnum);
301 #elif defined(_GNU_SOURCE)
305 * GNU strerror_r may not actually set buf.
306 * It can return a pointer to some (immutable) static string in which case
307 * buf is left unused.
309 err = strerror_r(errnum, buf, buflen);
310 if (err == NULL || buflen == 0)
313 * If err is statically allocated, err != buf and we need to copy the data.
314 * If err points somewhere inside buf, OPENSSL_strlcpy can handle this,
315 * since src and dest are not annotated with __restrict and the function
316 * reads src byte for byte and writes to dest.
317 * If err == buf we do not have to copy anything.
320 OPENSSL_strlcpy(buf, err, buflen);
322 #elif (defined(_POSIX_C_SOURCE) && _POSIX_C_SOURCE >= 200112L) || \
323 (defined(_XOPEN_SOURCE) && _XOPEN_SOURCE >= 600)
325 * We can use "real" strerror_r. The OpenSSL version differs in that it
326 * gives 1 on success and 0 on failure for consistency with other OpenSSL
327 * functions. Real strerror_r does it the other way around
329 return !strerror_r(errnum, buf, buflen);
333 /* Fall back to non-thread safe strerror()...its all we can do */
336 err = strerror(errnum);
337 /* Can this ever happen? */
340 OPENSSL_strlcpy(buf, err, buflen);
345 int OPENSSL_strcasecmp(const char *s1, const char *s2)
349 while ((t = ossl_tolower(*s1) - ossl_tolower(*s2++)) == 0)
355 int OPENSSL_strncasecmp(const char *s1, const char *s2, size_t n)
360 for (i = 0; i < n; i++)
361 if ((t = ossl_tolower(*s1) - ossl_tolower(*s2++)) != 0)
363 else if (*s1++ == '\0')