Coverage Report

Created: 2020-08-14 21:15

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