Coverage Report

Created: 2025-06-22 06:56

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