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);
61 return memcpy(ret, data, siz);
64 size_t OPENSSL_strnlen(const char *str, size_t maxlen)
68 for (p = str; maxlen-- != 0 && *p != CH_ZERO; ++p) ;
73 size_t OPENSSL_strlcpy(char *dst, const char *src, size_t size)
76 for (; size > 1 && *src; size--) {
82 return l + strlen(src);
85 size_t OPENSSL_strlcat(char *dst, const char *src, size_t size)
88 for (; size > 0 && *dst; size--, dst++)
90 return l + OPENSSL_strlcpy(dst, src, size);
93 int OPENSSL_hexchar2int(unsigned char c)
136 static int hexstr2buf_sep(unsigned char *buf, size_t buf_n, size_t *buflen,
137 const char *str, const char sep)
140 unsigned char ch, cl;
142 const unsigned char *p;
145 for (p = (const unsigned char *)str, q = buf, cnt = 0; *p; ) {
147 /* A separator of CH_ZERO means there is no separator */
148 if (ch == sep && sep != CH_ZERO)
152 ERR_raise(ERR_LIB_CRYPTO, CRYPTO_R_ODD_NUMBER_OF_DIGITS);
155 cli = OPENSSL_hexchar2int(cl);
156 chi = OPENSSL_hexchar2int(ch);
157 if (cli < 0 || chi < 0) {
158 ERR_raise(ERR_LIB_CRYPTO, CRYPTO_R_ILLEGAL_HEX_DIGIT);
164 ERR_raise(ERR_LIB_CRYPTO, CRYPTO_R_TOO_SMALL_BUFFER);
167 *q++ = (unsigned char)((chi << 4) | cli);
177 * Given a string of hex digits convert to a buffer
179 int OPENSSL_hexstr2buf_ex(unsigned char *buf, size_t buf_n, size_t *buflen,
180 const char *str, const char sep)
182 return hexstr2buf_sep(buf, buf_n, buflen, str, sep);
185 unsigned char *ossl_hexstr2buf_sep(const char *str, long *buflen,
189 size_t buf_n, tmp_buflen;
193 ERR_raise(ERR_LIB_CRYPTO, CRYPTO_R_HEX_STRING_TOO_SHORT);
197 if ((buf = OPENSSL_malloc(buf_n)) == NULL)
203 if (hexstr2buf_sep(buf, buf_n, &tmp_buflen, str, sep)) {
205 *buflen = (long)tmp_buflen;
212 unsigned char *OPENSSL_hexstr2buf(const char *str, long *buflen)
214 return ossl_hexstr2buf_sep(str, buflen, DEFAULT_SEPARATOR);
217 static int buf2hexstr_sep(char *str, size_t str_n, size_t *strlength,
218 const unsigned char *buf, size_t buflen,
221 static const char hexdig[] = "0123456789ABCDEF";
222 const unsigned char *p;
225 int has_sep = (sep != CH_ZERO);
226 size_t len = has_sep ? buflen * 3 : 1 + buflen * 2;
228 if (strlength != NULL)
233 if (str_n < (unsigned long)len) {
234 ERR_raise(ERR_LIB_CRYPTO, CRYPTO_R_TOO_SMALL_BUFFER);
239 for (i = 0, p = buf; i < buflen; i++, p++) {
240 *q++ = hexdig[(*p >> 4) & 0xf];
241 *q++ = hexdig[*p & 0xf];
249 #ifdef CHARSET_EBCDIC
250 ebcdic2ascii(str, str, q - str - 1);
255 int OPENSSL_buf2hexstr_ex(char *str, size_t str_n, size_t *strlength,
256 const unsigned char *buf, size_t buflen,
259 return buf2hexstr_sep(str, str_n, strlength, buf, buflen, sep);
262 char *ossl_buf2hexstr_sep(const unsigned char *buf, long buflen, char sep)
268 return OPENSSL_zalloc(1);
270 tmp_n = (sep != CH_ZERO) ? buflen * 3 : 1 + buflen * 2;
271 if ((tmp = OPENSSL_malloc(tmp_n)) == NULL)
274 if (buf2hexstr_sep(tmp, tmp_n, NULL, buf, buflen, sep))
282 * Given a buffer of length 'len' return a OPENSSL_malloc'ed string with its
283 * hex representation @@@ (Contents of buffer are always kept in ASCII, also
284 * on EBCDIC machines)
286 char *OPENSSL_buf2hexstr(const unsigned char *buf, long buflen)
288 return ossl_buf2hexstr_sep(buf, buflen, ':');
291 int openssl_strerror_r(int errnum, char *buf, size_t buflen)
293 #if defined(_MSC_VER) && _MSC_VER>=1400 && !defined(_WIN32_WCE)
294 return !strerror_s(buf, buflen, errnum);
295 #elif defined(_GNU_SOURCE)
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.
303 err = strerror_r(errnum, buf, buflen);
304 if (err == NULL || buflen == 0)
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.
314 OPENSSL_strlcpy(buf, err, buflen);
316 #elif (defined(_POSIX_C_SOURCE) && _POSIX_C_SOURCE >= 200112L) || \
317 (defined(_XOPEN_SOURCE) && _XOPEN_SOURCE >= 600)
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
323 return !strerror_r(errnum, buf, buflen);
327 /* Fall back to non-thread safe strerror()...its all we can do */
330 err = strerror(errnum);
331 /* Can this ever happen? */
334 OPENSSL_strlcpy(buf, err, buflen);
339 int OPENSSL_strcasecmp(const char *s1, const char *s2)
343 while ((t = ossl_tolower(*s1) - ossl_tolower(*s2++)) == 0)
349 int OPENSSL_strncasecmp(const char *s1, const char *s2, size_t n)
354 for (i = 0; i < n; i++)
355 if ((t = ossl_tolower(*s1) - ossl_tolower(*s2++)) != 0)
357 else if (*s1++ == '\0')