Coverage Report

Created: 2026-09-12 06:55

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