Coverage Report

Created: 2024-07-27 06:36

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