Coverage Report

Created: 2023-06-08 06:40

/src/openssl30/crypto/o_str.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright 2003-2022 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 "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
0
#define DEFAULT_SEPARATOR ':'
19
1.15M
#define CH_ZERO '\0'
20
21
char *CRYPTO_strdup(const char *str, const char* file, int line)
22
5.05k
{
23
5.05k
    char *ret;
24
25
5.05k
    if (str == NULL)
26
0
        return NULL;
27
5.05k
    ret = CRYPTO_malloc(strlen(str) + 1, file, line);
28
5.05k
    if (ret != NULL)
29
5.05k
        strcpy(ret, str);
30
5.05k
    return ret;
31
5.05k
}
32
33
char *CRYPTO_strndup(const char *str, size_t s, const char* file, int line)
34
0
{
35
0
    size_t maxlen;
36
0
    char *ret;
37
38
0
    if (str == NULL)
39
0
        return NULL;
40
41
0
    maxlen = OPENSSL_strnlen(str, s);
42
43
0
    ret = CRYPTO_malloc(maxlen + 1, file, line);
44
0
    if (ret) {
45
0
        memcpy(ret, str, maxlen);
46
0
        ret[maxlen] = CH_ZERO;
47
0
    }
48
0
    return ret;
49
0
}
50
51
void *CRYPTO_memdup(const void *data, size_t siz, const char* file, int line)
52
183k
{
53
183k
    void *ret;
54
55
183k
    if (data == NULL || siz >= INT_MAX)
56
0
        return NULL;
57
58
183k
    ret = CRYPTO_malloc(siz, file, line);
59
183k
    if (ret == NULL) {
60
0
        ERR_raise(ERR_LIB_CRYPTO, ERR_R_MALLOC_FAILURE);
61
0
        return NULL;
62
0
    }
63
183k
    return memcpy(ret, data, siz);
64
183k
}
65
66
size_t OPENSSL_strnlen(const char *str, size_t maxlen)
67
736k
{
68
736k
    const char *p;
69
70
1.16M
    for (p = str; maxlen-- != 0 && *p != CH_ZERO; ++p) ;
71
72
736k
    return p - str;
73
736k
}
74
75
size_t OPENSSL_strlcpy(char *dst, const char *src, size_t size)
76
0
{
77
0
    size_t l = 0;
78
0
    for (; size > 1 && *src; size--) {
79
0
        *dst++ = *src++;
80
0
        l++;
81
0
    }
82
0
    if (size)
83
0
        *dst = CH_ZERO;
84
0
    return l + strlen(src);
85
0
}
86
87
size_t OPENSSL_strlcat(char *dst, const char *src, size_t size)
88
0
{
89
0
    size_t l = 0;
90
0
    for (; size > 0 && *dst; size--, dst++)
91
0
        l++;
92
0
    return l + OPENSSL_strlcpy(dst, src, size);
93
0
}
94
95
int OPENSSL_hexchar2int(unsigned char c)
96
0
{
97
#ifdef CHARSET_EBCDIC
98
    c = os_toebcdic[c];
99
#endif
100
101
0
    switch (c) {
102
0
    case '0':
103
0
        return 0;
104
0
    case '1':
105
0
        return 1;
106
0
    case '2':
107
0
        return 2;
108
0
    case '3':
109
0
        return 3;
110
0
    case '4':
111
0
          return 4;
112
0
    case '5':
113
0
          return 5;
114
0
    case '6':
115
0
          return 6;
116
0
    case '7':
117
0
          return 7;
118
0
    case '8':
119
0
          return 8;
120
0
    case '9':
121
0
          return 9;
122
0
    case 'a': case 'A':
123
0
          return 0x0A;
124
0
    case 'b': case 'B':
125
0
          return 0x0B;
126
0
    case 'c': case 'C':
127
0
          return 0x0C;
128
0
    case 'd': case 'D':
129
0
          return 0x0D;
130
0
    case 'e': case 'E':
131
0
          return 0x0E;
132
0
    case 'f': case 'F':
133
0
          return 0x0F;
134
0
    }
135
0
    return -1;
136
0
}
137
138
static int hexstr2buf_sep(unsigned char *buf, size_t buf_n, size_t *buflen,
139
                          const char *str, const char sep)
140
0
{
141
0
    unsigned char *q;
142
0
    unsigned char ch, cl;
143
0
    int chi, cli;
144
0
    const unsigned char *p;
145
0
    size_t cnt;
146
147
0
    for (p = (const unsigned char *)str, q = buf, cnt = 0; *p; ) {
148
0
        ch = *p++;
149
        /* A separator of CH_ZERO means there is no separator */
150
0
        if (ch == sep && sep != CH_ZERO)
151
0
            continue;
152
0
        cl = *p++;
153
0
        if (!cl) {
154
0
            ERR_raise(ERR_LIB_CRYPTO, CRYPTO_R_ODD_NUMBER_OF_DIGITS);
155
0
            return 0;
156
0
        }
157
0
        cli = OPENSSL_hexchar2int(cl);
158
0
        chi = OPENSSL_hexchar2int(ch);
159
0
        if (cli < 0 || chi < 0) {
160
0
            ERR_raise(ERR_LIB_CRYPTO, CRYPTO_R_ILLEGAL_HEX_DIGIT);
161
0
            return 0;
162
0
        }
163
0
        cnt++;
164
0
        if (q != NULL) {
165
0
            if (cnt > buf_n) {
166
0
                ERR_raise(ERR_LIB_CRYPTO, CRYPTO_R_TOO_SMALL_BUFFER);
167
0
                return 0;
168
0
            }
169
0
            *q++ = (unsigned char)((chi << 4) | cli);
170
0
        }
171
0
    }
172
173
0
    if (buflen != NULL)
174
0
        *buflen = cnt;
175
0
    return 1;
176
0
}
177
178
/*
179
 * Given a string of hex digits convert to a buffer
180
 */
181
int OPENSSL_hexstr2buf_ex(unsigned char *buf, size_t buf_n, size_t *buflen,
182
                          const char *str, const char sep)
183
0
{
184
0
    return hexstr2buf_sep(buf, buf_n, buflen, str, sep);
185
0
}
186
187
unsigned char *ossl_hexstr2buf_sep(const char *str, long *buflen,
188
                                   const char sep)
189
0
{
190
0
    unsigned char *buf;
191
0
    size_t buf_n, tmp_buflen;
192
193
0
    buf_n = strlen(str);
194
0
    if (buf_n <= 1) {
195
0
        ERR_raise(ERR_LIB_CRYPTO, CRYPTO_R_HEX_STRING_TOO_SHORT);
196
0
        return NULL;
197
0
    }
198
0
    buf_n /= 2;
199
0
    if ((buf = OPENSSL_malloc(buf_n)) == NULL) {
200
0
        ERR_raise(ERR_LIB_CRYPTO, ERR_R_MALLOC_FAILURE);
201
0
        return NULL;
202
0
    }
203
204
0
    if (buflen != NULL)
205
0
        *buflen = 0;
206
0
    tmp_buflen = 0;
207
0
    if (hexstr2buf_sep(buf, buf_n, &tmp_buflen, str, sep)) {
208
0
        if (buflen != NULL)
209
0
            *buflen = (long)tmp_buflen;
210
0
        return buf;
211
0
    }
212
0
    OPENSSL_free(buf);
213
0
    return NULL;
214
0
}
215
216
unsigned char *OPENSSL_hexstr2buf(const char *str, long *buflen)
217
0
{
218
0
    return ossl_hexstr2buf_sep(str, buflen, DEFAULT_SEPARATOR);
219
0
}
220
221
static int buf2hexstr_sep(char *str, size_t str_n, size_t *strlength,
222
                          const unsigned char *buf, size_t buflen,
223
                          const char sep)
224
0
{
225
0
    static const char hexdig[] = "0123456789ABCDEF";
226
0
    const unsigned char *p;
227
0
    char *q;
228
0
    size_t i;
229
0
    int has_sep = (sep != CH_ZERO);
230
0
    size_t len = has_sep ? buflen * 3 : 1 + buflen * 2;
231
232
0
    if (strlength != NULL)
233
0
        *strlength = len;
234
0
    if (str == NULL)
235
0
        return 1;
236
237
0
    if (str_n < (unsigned long)len) {
238
0
        ERR_raise(ERR_LIB_CRYPTO, CRYPTO_R_TOO_SMALL_BUFFER);
239
0
        return 0;
240
0
    }
241
242
0
    q = str;
243
0
    for (i = 0, p = buf; i < buflen; i++, p++) {
244
0
        *q++ = hexdig[(*p >> 4) & 0xf];
245
0
        *q++ = hexdig[*p & 0xf];
246
0
        if (has_sep)
247
0
            *q++ = sep;
248
0
    }
249
0
    if (has_sep)
250
0
        --q;
251
0
    *q = CH_ZERO;
252
253
#ifdef CHARSET_EBCDIC
254
    ebcdic2ascii(str, str, q - str - 1);
255
#endif
256
0
    return 1;
257
0
}
258
259
int OPENSSL_buf2hexstr_ex(char *str, size_t str_n, size_t *strlength,
260
                          const unsigned char *buf, size_t buflen,
261
                          const char sep)
262
0
{
263
0
    return buf2hexstr_sep(str, str_n, strlength, buf, buflen, sep);
264
0
}
265
266
char *ossl_buf2hexstr_sep(const unsigned char *buf, long buflen, char sep)
267
0
{
268
0
    char *tmp;
269
0
    size_t tmp_n;
270
271
0
    if (buflen == 0)
272
0
        return OPENSSL_zalloc(1);
273
274
0
    tmp_n = (sep != CH_ZERO) ? buflen * 3 : 1 + buflen * 2;
275
0
    if ((tmp = OPENSSL_malloc(tmp_n)) == NULL) {
276
0
        ERR_raise(ERR_LIB_CRYPTO, ERR_R_MALLOC_FAILURE);
277
0
        return NULL;
278
0
    }
279
280
0
    if (buf2hexstr_sep(tmp, tmp_n, NULL, buf, buflen, sep))
281
0
        return tmp;
282
0
    OPENSSL_free(tmp);
283
0
    return NULL;
284
0
}
285
286
287
/*
288
 * Given a buffer of length 'len' return a OPENSSL_malloc'ed string with its
289
 * hex representation @@@ (Contents of buffer are always kept in ASCII, also
290
 * on EBCDIC machines)
291
 */
292
char *OPENSSL_buf2hexstr(const unsigned char *buf, long buflen)
293
0
{
294
0
    return ossl_buf2hexstr_sep(buf, buflen, ':');
295
0
}
296
297
int openssl_strerror_r(int errnum, char *buf, size_t buflen)
298
0
{
299
#if defined(_MSC_VER) && _MSC_VER>=1400 && !defined(_WIN32_WCE)
300
    return !strerror_s(buf, buflen, errnum);
301
#elif defined(_GNU_SOURCE)
302
    char *err;
303
304
    /*
305
     * GNU strerror_r may not actually set buf.
306
     * It can return a pointer to some (immutable) static string in which case
307
     * buf is left unused.
308
     */
309
    err = strerror_r(errnum, buf, buflen);
310
    if (err == NULL || buflen == 0)
311
        return 0;
312
    /*
313
     * If err is statically allocated, err != buf and we need to copy the data.
314
     * If err points somewhere inside buf, OPENSSL_strlcpy can handle this,
315
     * since src and dest are not annotated with __restrict and the function
316
     * reads src byte for byte and writes to dest.
317
     * If err == buf we do not have to copy anything.
318
     */
319
    if (err != buf)
320
        OPENSSL_strlcpy(buf, err, buflen);
321
    return 1;
322
#elif (defined(_POSIX_C_SOURCE) && _POSIX_C_SOURCE >= 200112L) || \
323
      (defined(_XOPEN_SOURCE) && _XOPEN_SOURCE >= 600)
324
    /*
325
     * We can use "real" strerror_r. The OpenSSL version differs in that it
326
     * gives 1 on success and 0 on failure for consistency with other OpenSSL
327
     * functions. Real strerror_r does it the other way around
328
     */
329
0
    return !strerror_r(errnum, buf, buflen);
330
#else
331
    char *err;
332
333
    /* Fall back to non-thread safe strerror()...its all we can do */
334
    if (buflen < 2)
335
        return 0;
336
    err = strerror(errnum);
337
    /* Can this ever happen? */
338
    if (err == NULL)
339
        return 0;
340
    OPENSSL_strlcpy(buf, err, buflen);
341
    return 1;
342
#endif
343
0
}
344
345
int OPENSSL_strcasecmp(const char *s1, const char *s2)
346
0
{
347
0
    int t;
348
349
0
    while ((t = ossl_tolower(*s1) - ossl_tolower(*s2++)) == 0)
350
0
        if (*s1++ == '\0')
351
0
            return 0;
352
0
    return t;
353
0
}
354
355
int OPENSSL_strncasecmp(const char *s1, const char *s2, size_t n)
356
0
{
357
0
    int t;
358
0
    size_t i;
359
360
0
    for (i = 0; i < n; i++)
361
0
        if ((t = ossl_tolower(*s1) - ossl_tolower(*s2++)) != 0)
362
0
            return t;
363
0
        else if (*s1++ == '\0')
364
0
            return 0;
365
0
    return 0;
366
0
}