| 1 |
/* |
| 2 |
* Copyright 2003-2026 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 75 |
return p - str; |
| 76 |
} |
| 77 |
|
| 78 |
size_t OPENSSL_strlcpy(char *dst, const char *src, size_t size) |
| 79 |
{ |
| 80 |
size_t l = 0; |
| 81 |
for (; size > 1 && *src; size--) { |
| 82 |
*dst++ = *src++; |
| 83 |
l++; |
| 84 |
} |
| 85 |
if (size) |
| 86 |
*dst = CH_ZERO; |
| 87 |
return l + strlen(src); |
| 88 |
} |
| 89 |
|
| 90 |
size_t OPENSSL_strlcat(char *dst, const char *src, size_t size) |
| 91 |
{ |
| 92 |
size_t l = 0; |
| 93 |
for (; size > 0 && *dst; size--, dst++) |
| 94 |
l++; |
| 95 |
return l + OPENSSL_strlcpy(dst, src, size); |
| 96 |
} |
| 97 |
|
| 98 |
/** |
| 99 |
* @brief Converts a string to an unsigned long integer. |
| 100 |
* |
| 101 |
* This function attempts to convert a string representation of a number |
| 102 |
* to an unsigned long integer, given a specified base. It also provides |
| 103 |
* error checking and reports whether the conversion was successful. |
| 104 |
* This function is just a wrapper around the POSIX strtoul function with |
| 105 |
* additional error checking. This implies that errno for the caller is set |
| 106 |
* on calls to this function. |
| 107 |
* |
| 108 |
* @param str The string containing the representation of the number. |
| 109 |
* @param endptr A pointer to a pointer to character. If not NULL, it is set |
| 110 |
* to the character immediately following the number in the |
| 111 |
* string. |
| 112 |
* @param base The base to use for the conversion, which must be between 2, |
| 113 |
* and 36 inclusive, or be the special value 0. If the base is 0, |
| 114 |
* the actual base is determined by the format of the initial |
| 115 |
* characters of the string. |
| 116 |
* @param num A pointer to an unsigned long where the result of the |
| 117 |
* conversion is stored. |
| 118 |
* |
| 119 |
* @return 1 if the conversion was successful, 0 otherwise. Conversion is |
| 120 |
* considered unsuccessful if no digits were consumed or if an error |
| 121 |
* occurred during conversion. |
| 122 |
* |
| 123 |
* @note It is the caller's responsibility to check if the conversion is |
| 124 |
* correct based on the expected consumption of the string as reported |
| 125 |
* by endptr. |
| 126 |
*/ |
| 127 |
int OPENSSL_strtoul(const char *str, char **endptr, int base, |
| 128 |
unsigned long *num) |
| 129 |
{ |
| 130 |
char *tmp_endptr; |
| 131 |
char **internal_endptr = endptr == NULL ? &tmp_endptr : endptr; |
| 132 |
|
| 133 |
errno = 0; |
| 134 |
|
| 135 |
*internal_endptr = (char *)str; |
| 136 |
|
| 137 |
if (num == NULL) |
| 138 |
return 0; |
| 139 |
|
| 140 |
if (str == NULL) |
| 141 |
return 0; |
| 142 |
|
| 143 |
/* Fail on negative input */ |
| 144 |
if (*str == '-') |
| 145 |
return 0; |
| 146 |
|
| 147 |
*num = strtoul(str, internal_endptr, base); |
| 148 |
/* |
| 149 |
* We return error from this function under the following conditions |
| 150 |
* 1) If strtoul itself returned an error in translation |
| 151 |
* 2) If the caller didn't pass in an endptr value, and **internal_endptr |
| 152 |
* doesn't point to '\0'. The implication here is that if the caller |
| 153 |
* doesn't care how much of a string is consumed, they expect the entire |
| 154 |
* string to be consumed. As such, no pointing to the NULL terminator |
| 155 |
* means there was some part of the string left over after translation |
| 156 |
* 3) If no bytes of the string were consumed |
| 157 |
*/ |
| 158 |
if (errno != 0 || (endptr == NULL && **internal_endptr != '\0') || (str == *internal_endptr)) |
| 159 |
return 0; |
| 160 |
|
| 161 |
return 1; |
| 162 |
} |
| 163 |
|
| 164 |
int OPENSSL_hexchar2int(unsigned char c) |
| 165 |
{ |
| 166 |
#ifdef CHARSET_EBCDIC |
| 167 |
c = os_toebcdic[c]; |
| 168 |
#endif |
| 169 |
|
| 170 |
switch (c) { |
| 171 |
case '0': |
| 172 |
return 0; |
| 173 |
case '1': |
| 174 |
return 1; |
| 175 |
case '2': |
| 176 |
return 2; |
| 177 |
case '3': |
| 178 |
return 3; |
| 179 |
case '4': |
| 180 |
return 4; |
| 181 |
case '5': |
| 182 |
return 5; |
| 183 |
case '6': |
| 184 |
return 6; |
| 185 |
case '7': |
| 186 |
return 7; |
| 187 |
case '8': |
| 188 |
return 8; |
| 189 |
case '9': |
| 190 |
return 9; |
| 191 |
case 'a': |
| 192 |
case 'A': |
| 193 |
return 0x0A; |
| 194 |
case 'b': |
| 195 |
case 'B': |
| 196 |
return 0x0B; |
| 197 |
case 'c': |
| 198 |
case 'C': |
| 199 |
return 0x0C; |
| 200 |
case 'd': |
| 201 |
case 'D': |
| 202 |
return 0x0D; |
| 203 |
case 'e': |
| 204 |
case 'E': |
| 205 |
return 0x0E; |
| 206 |
case 'f': |
| 207 |
case 'F': |
| 208 |
return 0x0F; |
| 209 |
} |
| 210 |
return -1; |
| 211 |
} |
| 212 |
|
| 213 |
static int hexstr2buf_sep(unsigned char *buf, size_t buf_n, size_t *buflen, |
| 214 |
const char *str, const char sep) |
| 215 |
{ |
| 216 |
unsigned char *q; |
| 217 |
unsigned char ch, cl; |
| 218 |
int chi, cli; |
| 219 |
const unsigned char *p; |
| 220 |
size_t cnt; |
| 221 |
|
| 222 |
for (p = (const unsigned char *)str, q = buf, cnt = 0; *p;) { |
| 223 |
ch = *p++; |
| 224 |
/* A separator of CH_ZERO means there is no separator */ |
| 225 |
if (ch == sep && sep != CH_ZERO) |
| 226 |
continue; |
| 227 |
cl = *p++; |
| 228 |
if (!cl) { |
| 229 |
ERR_raise(ERR_LIB_CRYPTO, CRYPTO_R_ODD_NUMBER_OF_DIGITS); |
| 230 |
return 0; |
| 231 |
} |
| 232 |
cli = OPENSSL_hexchar2int(cl); |
| 233 |
chi = OPENSSL_hexchar2int(ch); |
| 234 |
if (cli < 0 || chi < 0) { |
| 235 |
ERR_raise(ERR_LIB_CRYPTO, CRYPTO_R_ILLEGAL_HEX_DIGIT); |
| 236 |
return 0; |
| 237 |
} |
| 238 |
cnt++; |
| 239 |
if (q != NULL) { |
| 240 |
if (cnt > buf_n) { |
| 241 |
ERR_raise(ERR_LIB_CRYPTO, CRYPTO_R_TOO_SMALL_BUFFER); |
| 242 |
return 0; |
| 243 |
} |
| 244 |
*q++ = (unsigned char)((chi << 4) | cli); |
| 245 |
} |
| 246 |
} |
| 247 |
|
| 248 |
if (buflen != NULL) |
| 249 |
*buflen = cnt; |
| 250 |
return 1; |
| 251 |
} |
| 252 |
|
| 253 |
/* |
| 254 |
* Given a string of hex digits convert to a buffer |
| 255 |
*/ |
| 256 |
int OPENSSL_hexstr2buf_ex(unsigned char *buf, size_t buf_n, size_t *buflen, |
| 257 |
const char *str, const char sep) |
| 258 |
{ |
| 259 |
return hexstr2buf_sep(buf, buf_n, buflen, str, sep); |
| 260 |
} |
| 261 |
|
| 262 |
unsigned char *ossl_hexstr2buf_sep(const char *str, long *buflen, |
| 263 |
const char sep) |
| 264 |
{ |
| 265 |
unsigned char *buf; |
| 266 |
size_t buf_n, tmp_buflen; |
| 267 |
|
| 268 |
buf_n = strlen(str); |
| 269 |
if (buf_n <= 1) { |
| 270 |
ERR_raise(ERR_LIB_CRYPTO, CRYPTO_R_HEX_STRING_TOO_SHORT); |
| 271 |
return NULL; |
| 272 |
} |
| 273 |
buf_n /= 2; |
| 274 |
if ((buf = OPENSSL_malloc(buf_n)) == NULL) |
| 275 |
return NULL; |
| 276 |
|
| 277 |
if (buflen != NULL) |
| 278 |
*buflen = 0; |
| 279 |
tmp_buflen = 0; |
| 280 |
if (hexstr2buf_sep(buf, buf_n, &tmp_buflen, str, sep)) { |
| 281 |
if (buflen != NULL) |
| 282 |
*buflen = (long)tmp_buflen; |
| 283 |
return buf; |
| 284 |
} |
| 285 |
OPENSSL_free(buf); |
| 286 |
return NULL; |
| 287 |
} |
| 288 |
|
| 289 |
unsigned char *OPENSSL_hexstr2buf(const char *str, long *buflen) |
| 290 |
{ |
| 291 |
return ossl_hexstr2buf_sep(str, buflen, DEFAULT_SEPARATOR); |
| 292 |
} |
| 293 |
|
| 294 |
static int buf2hexstr_sep(char *str, size_t str_n, size_t *strlength, |
| 295 |
const unsigned char *buf, size_t buflen, |
| 296 |
const char sep) |
| 297 |
{ |
| 298 |
char *q; |
| 299 |
int has_sep = (sep != CH_ZERO); |
| 300 |
size_t i, len = has_sep ? buflen * 3 : 1 + buflen * 2; |
| 301 |
|
| 302 |
if (buflen > (has_sep ? SIZE_MAX / 3 : (SIZE_MAX - 1) / 2)) { |
| 303 |
ERR_raise(ERR_LIB_CRYPTO, CRYPTO_R_TOO_MANY_BYTES); |
| 304 |
return 0; |
| 305 |
} |
| 306 |
|
| 307 |
if (len == 0) |
| 308 |
++len; |
| 309 |
if (strlength != NULL) |
| 310 |
*strlength = len; |
| 311 |
if (str == NULL) |
| 312 |
return 1; |
| 313 |
|
| 314 |
if (str_n < len) { |
| 315 |
ERR_raise(ERR_LIB_CRYPTO, CRYPTO_R_TOO_SMALL_BUFFER); |
| 316 |
return 0; |
| 317 |
} |
| 318 |
|
| 319 |
q = str; |
| 320 |
for (i = 0; i < buflen; i++) { |
| 321 |
q += ossl_to_hex(q, buf[i]); |
| 322 |
if (has_sep) |
| 323 |
*q++ = sep; |
| 324 |
} |
| 325 |
if (has_sep && buflen > 0) |
| 326 |
--q; |
| 327 |
*q = CH_ZERO; |
| 328 |
|
| 329 |
#ifdef CHARSET_EBCDIC |
| 330 |
ebcdic2ascii(str, str, q - str); |
| 331 |
#endif |
| 332 |
return 1; |
| 333 |
} |
| 334 |
|
| 335 |
int OPENSSL_buf2hexstr_ex(char *str, size_t str_n, size_t *strlength, |
| 336 |
const unsigned char *buf, size_t buflen, |
| 337 |
const char sep) |
| 338 |
{ |
| 339 |
return buf2hexstr_sep(str, str_n, strlength, buf, buflen, sep); |
| 340 |
} |
| 341 |
|
| 342 |
char *ossl_buf2hexstr_sep(const unsigned char *buf, long buflen, char sep) |
| 343 |
{ |
| 344 |
char *tmp; |
| 345 |
size_t tmp_n; |
| 346 |
|
| 347 |
if (buflen == 0) |
| 348 |
return OPENSSL_zalloc(1); |
| 349 |
|
| 350 |
if ((sep != CH_ZERO && (size_t)buflen > SIZE_MAX / 3) |
| 351 |
|| (sep == CH_ZERO && (size_t)buflen > (SIZE_MAX - 1) / 2)) { |
| 352 |
ERR_raise(ERR_LIB_CRYPTO, CRYPTO_R_TOO_MANY_BYTES); |
| 353 |
return NULL; |
| 354 |
} |
| 355 |
|
| 356 |
tmp_n = (sep != CH_ZERO) ? (size_t)buflen * 3 : 1 + (size_t)buflen * 2; |
| 357 |
if ((tmp = OPENSSL_malloc(tmp_n)) == NULL) |
| 358 |
return NULL; |
| 359 |
|
| 360 |
if (buf2hexstr_sep(tmp, tmp_n, NULL, buf, buflen, sep)) |
| 361 |
return tmp; |
| 362 |
OPENSSL_free(tmp); |
| 363 |
return NULL; |
| 364 |
} |
| 365 |
|
| 366 |
/* |
| 367 |
* Given a buffer of length 'buflen' return a OPENSSL_malloc'ed string with |
| 368 |
* its hex representation @@@ (Contents of buffer are always kept in ASCII, |
| 369 |
* also on EBCDIC machines) |
| 370 |
*/ |
| 371 |
char *OPENSSL_buf2hexstr(const unsigned char *buf, long buflen) |
| 372 |
{ |
| 373 |
return ossl_buf2hexstr_sep(buf, buflen, DEFAULT_SEPARATOR); |
| 374 |
} |
| 375 |
|
| 376 |
int openssl_strerror_r(int errnum, char *buf, size_t buflen) |
| 377 |
{ |
| 378 |
#if defined(_MSC_VER) && _MSC_VER >= 1400 && !defined(_WIN32_WCE) |
| 379 |
return !strerror_s(buf, buflen, errnum); |
| 380 |
#elif defined(_GNU_SOURCE) |
| 381 |
char *err; |
| 382 |
|
| 383 |
/* |
| 384 |
* GNU strerror_r may not actually set buf. |
| 385 |
* It can return a pointer to some (immutable) static string in which case |
| 386 |
* buf is left unused. |
| 387 |
*/ |
| 388 |
err = strerror_r(errnum, buf, buflen); |
| 389 |
if (err == NULL || buflen == 0) |
| 390 |
return 0; |
| 391 |
/* |
| 392 |
* If err is statically allocated, err != buf and we need to copy the data. |
| 393 |
* If err points somewhere inside buf, OPENSSL_strlcpy can handle this, |
| 394 |
* since src and dest are not annotated with __restrict and the function |
| 395 |
* reads src byte for byte and writes to dest. |
| 396 |
* If err == buf we do not have to copy anything. |
| 397 |
*/ |
| 398 |
if (err != buf) |
| 399 |
OPENSSL_strlcpy(buf, err, buflen); |
| 400 |
return 1; |
| 401 |
#elif (defined(_POSIX_C_SOURCE) && _POSIX_C_SOURCE >= 200112L) || (defined(_XOPEN_SOURCE) && _XOPEN_SOURCE >= 600) |
| 402 |
/* |
| 403 |
* We can use "real" strerror_r. The OpenSSL version differs in that it |
| 404 |
* gives 1 on success and 0 on failure for consistency with other OpenSSL |
| 405 |
* functions. Real strerror_r does it the other way around |
| 406 |
*/ |
| 407 |
return !strerror_r(errnum, buf, buflen); |
| 408 |
#else |
| 409 |
char *err; |
| 410 |
|
| 411 |
/* Fall back to non-thread safe strerror()...its all we can do */ |
| 412 |
if (buflen < 2) |
| 413 |
return 0; |
| 414 |
err = strerror(errnum); |
| 415 |
/* Can this ever happen? */ |
| 416 |
if (err == NULL) |
| 417 |
return 0; |
| 418 |
OPENSSL_strlcpy(buf, err, buflen); |
| 419 |
return 1; |
| 420 |
#endif |
| 421 |
} |
| 422 |
|
| 423 |
int OPENSSL_strcasecmp(const char *s1, const char *s2) |
| 424 |
{ |
| 425 |
int t; |
| 426 |
|
| 427 |
while ((t = ossl_tolower(*s1) - ossl_tolower(*s2++)) == 0) |
| 428 |
if (*s1++ == '\0') |
| 429 |
return 0; |
| 430 |
return t; |
| 431 |
} |
| 432 |
|
| 433 |
int OPENSSL_strncasecmp(const char *s1, const char *s2, size_t n) |
| 434 |
{ |
| 435 |
int t; |
| 436 |
size_t i; |
| 437 |
|
| 438 |
for (i = 0; i < n; i++) |
| 439 |
if ((t = ossl_tolower(*s1) - ossl_tolower(*s2++)) != 0) |
| 440 |
return t; |
| 441 |
else if (*s1++ == '\0') |
| 442 |
return 0; |
| 443 |
return 0; |
| 444 |
} |
| 445 |
|
| 446 |
size_t ossl_to_hex(char *buf, uint8_t n) |
| 447 |
{ |
| 448 |
static const char hexdig[] = "0123456789ABCDEF"; |
| 449 |
|
| 450 |
return to_hex(buf, n, hexdig); |
| 451 |
} |