Coverage Report

Created: 2026-04-09 06:50

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl34/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
101k
#define DEFAULT_SEPARATOR ':'
20
2.40G
#define CH_ZERO '\0'
21
22
char *CRYPTO_strdup(const char *str, const char *file, int line)
23
91.8M
{
24
91.8M
    char *ret;
25
26
91.8M
    if (str == NULL)
27
0
        return NULL;
28
91.8M
    ret = CRYPTO_malloc(strlen(str) + 1, file, line);
29
91.8M
    if (ret != NULL)
30
91.8M
        strcpy(ret, str);
31
91.8M
    return ret;
32
91.8M
}
33
34
char *CRYPTO_strndup(const char *str, size_t s, const char *file, int line)
35
217M
{
36
217M
    size_t maxlen;
37
217M
    char *ret;
38
39
217M
    if (str == NULL)
40
0
        return NULL;
41
42
217M
    maxlen = OPENSSL_strnlen(str, s);
43
44
217M
    ret = CRYPTO_malloc(maxlen + 1, file, line);
45
217M
    if (ret) {
46
217M
        memcpy(ret, str, maxlen);
47
217M
        ret[maxlen] = CH_ZERO;
48
217M
    }
49
217M
    return ret;
50
217M
}
51
52
void *CRYPTO_memdup(const void *data, size_t siz, const char *file, int line)
53
57.2M
{
54
57.2M
    void *ret;
55
56
57.2M
    if (data == NULL || siz >= INT_MAX)
57
0
        return NULL;
58
59
57.2M
    ret = CRYPTO_malloc(siz, file, line);
60
57.2M
    if (ret == NULL)
61
0
        return NULL;
62
57.2M
    return memcpy(ret, data, siz);
63
57.2M
}
64
65
size_t OPENSSL_strnlen(const char *str, size_t maxlen)
66
443M
{
67
443M
    const char *p;
68
69
2.33G
    for (p = str; maxlen-- != 0 && *p != CH_ZERO; ++p)
70
1.88G
        ;
71
72
443M
    return p - str;
73
443M
}
74
75
size_t OPENSSL_strlcpy(char *dst, const char *src, size_t size)
76
74.7M
{
77
74.7M
    size_t l = 0;
78
847M
    for (; size > 1 && *src; size--) {
79
772M
        *dst++ = *src++;
80
772M
        l++;
81
772M
    }
82
74.7M
    if (size)
83
74.7M
        *dst = CH_ZERO;
84
74.7M
    return l + strlen(src);
85
74.7M
}
86
87
size_t OPENSSL_strlcat(char *dst, const char *src, size_t size)
88
56.2M
{
89
56.2M
    size_t l = 0;
90
725M
    for (; size > 0 && *dst; size--, dst++)
91
669M
        l++;
92
56.2M
    return l + OPENSSL_strlcpy(dst, src, size);
93
56.2M
}
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
362
{
163
#ifdef CHARSET_EBCDIC
164
    c = os_toebcdic[c];
165
#endif
166
167
362
    switch (c) {
168
9
    case '0':
169
9
        return 0;
170
33
    case '1':
171
33
        return 1;
172
10
    case '2':
173
10
        return 2;
174
12
    case '3':
175
12
        return 3;
176
6
    case '4':
177
6
        return 4;
178
7
    case '5':
179
7
        return 5;
180
19
    case '6':
181
19
        return 6;
182
10
    case '7':
183
10
        return 7;
184
26
    case '8':
185
26
        return 8;
186
8
    case '9':
187
8
        return 9;
188
60
    case 'a':
189
70
    case 'A':
190
70
        return 0x0A;
191
4
    case 'b':
192
5
    case 'B':
193
5
        return 0x0B;
194
25
    case 'c':
195
36
    case 'C':
196
36
        return 0x0C;
197
19
    case 'd':
198
21
    case 'D':
199
21
        return 0x0D;
200
21
    case 'e':
201
36
    case 'E':
202
36
        return 0x0E;
203
8
    case 'f':
204
19
    case 'F':
205
19
        return 0x0F;
206
362
    }
207
35
    return -1;
208
362
}
209
210
static int hexstr2buf_sep(unsigned char *buf, size_t buf_n, size_t *buflen,
211
    const char *str, const char sep)
212
0
{
213
0
    unsigned char *q;
214
0
    unsigned char ch, cl;
215
0
    int chi, cli;
216
0
    const unsigned char *p;
217
0
    size_t cnt;
218
219
0
    for (p = (const unsigned char *)str, q = buf, cnt = 0; *p;) {
220
0
        ch = *p++;
221
        /* A separator of CH_ZERO means there is no separator */
222
0
        if (ch == sep && sep != CH_ZERO)
223
0
            continue;
224
0
        cl = *p++;
225
0
        if (!cl) {
226
0
            ERR_raise(ERR_LIB_CRYPTO, CRYPTO_R_ODD_NUMBER_OF_DIGITS);
227
0
            return 0;
228
0
        }
229
0
        cli = OPENSSL_hexchar2int(cl);
230
0
        chi = OPENSSL_hexchar2int(ch);
231
0
        if (cli < 0 || chi < 0) {
232
0
            ERR_raise(ERR_LIB_CRYPTO, CRYPTO_R_ILLEGAL_HEX_DIGIT);
233
0
            return 0;
234
0
        }
235
0
        cnt++;
236
0
        if (q != NULL) {
237
0
            if (cnt > buf_n) {
238
0
                ERR_raise(ERR_LIB_CRYPTO, CRYPTO_R_TOO_SMALL_BUFFER);
239
0
                return 0;
240
0
            }
241
0
            *q++ = (unsigned char)((chi << 4) | cli);
242
0
        }
243
0
    }
244
245
0
    if (buflen != NULL)
246
0
        *buflen = cnt;
247
0
    return 1;
248
0
}
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
0
{
262
0
    unsigned char *buf;
263
0
    size_t buf_n, tmp_buflen;
264
265
0
    buf_n = strlen(str);
266
0
    if (buf_n <= 1) {
267
0
        ERR_raise(ERR_LIB_CRYPTO, CRYPTO_R_HEX_STRING_TOO_SHORT);
268
0
        return NULL;
269
0
    }
270
0
    buf_n /= 2;
271
0
    if ((buf = OPENSSL_malloc(buf_n)) == NULL)
272
0
        return NULL;
273
274
0
    if (buflen != NULL)
275
0
        *buflen = 0;
276
0
    tmp_buflen = 0;
277
0
    if (hexstr2buf_sep(buf, buf_n, &tmp_buflen, str, sep)) {
278
0
        if (buflen != NULL)
279
0
            *buflen = (long)tmp_buflen;
280
0
        return buf;
281
0
    }
282
0
    OPENSSL_free(buf);
283
0
    return NULL;
284
0
}
285
286
unsigned char *OPENSSL_hexstr2buf(const char *str, long *buflen)
287
0
{
288
0
    return ossl_hexstr2buf_sep(str, buflen, DEFAULT_SEPARATOR);
289
0
}
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
123k
{
295
123k
    char *q;
296
123k
    int has_sep = (sep != CH_ZERO);
297
123k
    size_t i, len = has_sep ? buflen * 3 : 1 + buflen * 2;
298
299
123k
    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
123k
    if (len == 0)
305
0
        ++len;
306
123k
    if (strlength != NULL)
307
0
        *strlength = len;
308
123k
    if (str == NULL)
309
0
        return 1;
310
311
123k
    if (str_n < len) {
312
0
        ERR_raise(ERR_LIB_CRYPTO, CRYPTO_R_TOO_SMALL_BUFFER);
313
0
        return 0;
314
0
    }
315
316
123k
    q = str;
317
17.9M
    for (i = 0; i < buflen; i++) {
318
17.8M
        q += ossl_to_hex(q, buf[i]);
319
17.8M
        if (has_sep)
320
17.5M
            *q++ = sep;
321
17.8M
    }
322
123k
    if (has_sep && buflen > 0)
323
91.7k
        --q;
324
123k
    *q = CH_ZERO;
325
326
#ifdef CHARSET_EBCDIC
327
    ebcdic2ascii(str, str, q - str);
328
#endif
329
123k
    return 1;
330
123k
}
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
78.7k
{
341
78.7k
    char *tmp;
342
78.7k
    size_t tmp_n;
343
344
78.7k
    if (buflen == 0)
345
5.94k
        return OPENSSL_zalloc(1);
346
347
72.7k
    if ((sep != CH_ZERO && (size_t)buflen > SIZE_MAX / 3)
348
72.7k
        || (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
72.7k
    tmp_n = (sep != CH_ZERO) ? (size_t)buflen * 3 : 1 + (size_t)buflen * 2;
354
72.7k
    if ((tmp = OPENSSL_malloc(tmp_n)) == NULL)
355
0
        return NULL;
356
357
72.7k
    if (buf2hexstr_sep(tmp, tmp_n, NULL, buf, buflen, sep))
358
72.7k
        return tmp;
359
0
    OPENSSL_free(tmp);
360
0
    return NULL;
361
72.7k
}
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
101k
{
370
101k
    return ossl_buf2hexstr_sep(buf, buflen, DEFAULT_SEPARATOR);
371
101k
}
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
235M
{
422
235M
    int t;
423
424
1.36G
    while ((t = ossl_tolower(*s1) - ossl_tolower(*s2++)) == 0)
425
1.33G
        if (*s1++ == '\0')
426
199M
            return 0;
427
35.5M
    return t;
428
235M
}
429
430
int OPENSSL_strncasecmp(const char *s1, const char *s2, size_t n)
431
43.2M
{
432
43.2M
    int t;
433
43.2M
    size_t i;
434
435
54.8M
    for (i = 0; i < n; i++)
436
52.7M
        if ((t = ossl_tolower(*s1) - ossl_tolower(*s2++)) != 0)
437
41.1M
            return t;
438
11.5M
        else if (*s1++ == '\0')
439
0
            return 0;
440
2.16M
    return 0;
441
43.2M
}
442
443
size_t ossl_to_hex(char *buf, uint8_t n)
444
249M
{
445
249M
    static const char hexdig[] = "0123456789ABCDEF";
446
447
249M
    return to_hex(buf, n, hexdig);
448
249M
}