Coverage Report

Created: 2026-12-01 06:14

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/cjose/src/base64.c
Line
Count
Source
1
/*
2
 * Copyrights
3
 *
4
 * Portions created or assigned to Cisco Systems, Inc. are
5
 * Copyright (c) 2014-2016 Cisco Systems, Inc.  All Rights Reserved.
6
 */
7
8
#include <cjose/base64.h>
9
#include <cjose/util.h>
10
11
#include <errno.h>
12
#include <string.h>
13
#include <stdlib.h>
14
15
// defines
16
#define B64_BYTE1(ptr) (((*ptr) & 0xfc) >> 2)
17
#define B64_BYTE2(ptr) ((((*ptr) & 0x03) << 4) | ((*(ptr + 1) & 0xf0) >> 4))
18
#define B64_BYTE3(ptr) (((*(ptr + 1) & 0x0f) << 2) | ((*(ptr + 2) & 0xc0) >> 6))
19
#define B64_BYTE4(ptr) (*(ptr + 2) & 0x3f)
20
21
// internal data
22
23
static const char *ALPHABET_B64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
24
static const char *ALPHABET_B64U = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";
25
26
static const uint8_t TEBAHPLA_B64[]
27
    = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
28
        0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
29
        0xff, 0xff, 0xff, 0x3e, 0xff, 0x3e, 0xff, 0x3f, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0xff, 0xff,
30
        0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e,
31
        0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0xff, 0xff, 0xff, 0xff, 0x3f, 0xff, 0x1a, 0x1b, 0x1c,
32
        0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, 0x30,
33
        0x31, 0x32, 0x33, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
34
        0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
35
        0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
36
        0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
37
        0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
38
        0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
39
        0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
40
41
// internal functions
42
43
static inline bool
44
_cjose_base64_decode_impl(const char *input, size_t inlen, uint8_t **output, size_t *outlen, bool url, cjose_err *err)
45
30.0k
{
46
30.0k
    if ((NULL == input) || (NULL == output) || (NULL == outlen))
47
0
    {
48
0
        CJOSE_ERROR(err, CJOSE_ERR_INVALID_ARG);
49
0
        return false;
50
0
    }
51
52
    // return empty string on 0 length input
53
30.0k
    if (0 == inlen)
54
4.07k
    {
55
4.07k
        uint8_t *retVal = (uint8_t *)cjose_get_alloc()(sizeof(uint8_t));
56
4.07k
        if (NULL == retVal)
57
0
        {
58
0
            CJOSE_ERROR(err, CJOSE_ERR_NO_MEMORY);
59
0
            return false;
60
0
        }
61
62
4.07k
        retVal[0] = 0;
63
4.07k
        *output = retVal;
64
4.07k
        *outlen = 0;
65
4.07k
        return true;
66
4.07k
    }
67
68
    // extra validation -- inlen is a multiple of 4
69
25.9k
    if ((!url && 0 != (inlen % 4)) || (inlen % 4 == 1))
70
53
    {
71
53
        CJOSE_ERROR(err, CJOSE_ERR_INVALID_ARG);
72
53
        return false;
73
53
    }
74
75
    // rlen takes a best guess on size;
76
    // might be too large for base64url, but never too small.
77
25.8k
    if (inlen > SIZE_MAX / 3)
78
0
    {
79
0
        CJOSE_ERROR(err, CJOSE_ERR_INVALID_ARG);
80
0
        return false;
81
0
    }
82
83
25.8k
    size_t rlen = ((inlen * 3) >> 2) + 3;
84
25.8k
    uint8_t *buffer = cjose_get_alloc()(sizeof(uint8_t) * rlen);
85
25.8k
    if (NULL == buffer)
86
0
    {
87
0
        CJOSE_ERROR(err, CJOSE_ERR_NO_MEMORY);
88
0
        return false;
89
0
    }
90
91
25.8k
    size_t idx = 0;
92
25.8k
    size_t pos = 0;
93
25.8k
    size_t shift = 0;
94
25.8k
    uint32_t packed = 0;
95
168M
    while (inlen > idx)
96
168M
    {
97
168M
        uint8_t val;
98
168M
        val = input[idx];
99
168M
        if ('=' == val)
100
102
        {
101
102
            break;
102
102
        }
103
168M
        else if (url && ('+' == val || '/' == val))
104
18
        {
105
18
            CJOSE_ERROR(err, CJOSE_ERR_INVALID_ARG);
106
18
            goto b64_decode_failed;
107
18
        }
108
168M
        else if (!url && ('-' == val || '_' == val))
109
0
        {
110
0
            CJOSE_ERROR(err, CJOSE_ERR_INVALID_ARG);
111
0
            goto b64_decode_failed;
112
0
        }
113
114
168M
        val = TEBAHPLA_B64[val];
115
168M
        if (0xff == val)
116
94
        {
117
94
            CJOSE_ERROR(err, CJOSE_ERR_INVALID_ARG);
118
94
            cjose_get_dealloc()(buffer);
119
94
            return false;
120
94
        }
121
168M
        idx++;
122
123
168M
        packed = packed | (val << (18 - (6 * shift++)));
124
168M
        if (4 == shift)
125
42.0M
        {
126
42.0M
            buffer[pos++] = (packed >> 16) & 0xff;
127
42.0M
            buffer[pos++] = (packed >> 8) & 0xff;
128
42.0M
            buffer[pos++] = packed & 0xff;
129
42.0M
            shift = 0;
130
42.0M
            packed = 0;
131
42.0M
        }
132
168M
    }
133
134
25.7k
    if ((shift == 1) || (shift == 4))
135
5
    {
136
5
        CJOSE_ERROR(err, CJOSE_ERR_INVALID_STATE);
137
5
        goto b64_decode_failed;
138
5
    }
139
140
25.7k
    if (shift == 3)
141
8.95k
    {
142
8.95k
        buffer[pos++] = (packed >> 16) & 0xff;
143
8.95k
        buffer[pos++] = (packed >> 8) & 0xff;
144
8.95k
    }
145
146
25.7k
    if (shift == 2)
147
9.47k
    {
148
9.47k
        buffer[pos++] = (packed >> 16) & 0xff;
149
9.47k
    }
150
151
    // validate before publishing the out-params: the failure path frees buffer,
152
    // which would otherwise leave *output dangling
153
25.7k
    if (pos > rlen)
154
0
    {
155
0
        CJOSE_ERROR(err, CJOSE_ERR_INVALID_STATE);
156
0
        goto b64_decode_failed;
157
0
    }
158
159
25.7k
    *output = buffer;
160
25.7k
    *outlen = pos;
161
162
25.7k
    return true;
163
164
23
b64_decode_failed:
165
23
    if (NULL != buffer)
166
23
    {
167
23
        cjose_get_dealloc()(buffer);
168
23
    }
169
23
    return false;
170
25.7k
}
171
172
static inline bool
173
_cjose_base64_encode_impl(const uint8_t *input, size_t inlen, char **output, size_t *outlen, const char *alphabet, cjose_err *err)
174
34.7k
{
175
34.7k
    if ((inlen > 0 && NULL == input) || (NULL == output) || (NULL == outlen))
176
0
    {
177
0
        CJOSE_ERROR(err, CJOSE_ERR_INVALID_ARG);
178
0
        return false;
179
0
    }
180
181
    // return empty string on 0 length input
182
34.7k
    if (!inlen)
183
5.16k
    {
184
5.16k
        char *retVal = (char *)cjose_get_alloc()(sizeof(char));
185
5.16k
        if (!retVal)
186
0
        {
187
0
            CJOSE_ERROR(err, CJOSE_ERR_NO_MEMORY);
188
0
            return false;
189
0
        }
190
5.16k
        retVal[0] = '\0';
191
5.16k
        *output = retVal;
192
5.16k
        *outlen = 0;
193
5.16k
        return true;
194
5.16k
    }
195
196
    // guard the ~4/3 size expansion (the +2, the <<2, and the +1 below) against
197
    // size_t overflow, mirroring the SIZE_MAX/3 guard on the decode side
198
29.6k
    if (inlen > (SIZE_MAX - 4) / 4 * 3)
199
0
    {
200
0
        CJOSE_ERROR(err, CJOSE_ERR_INVALID_ARG);
201
0
        return false;
202
0
    }
203
204
29.6k
    const bool padit = (ALPHABET_B64 == alphabet);
205
29.6k
    size_t rlen = (((inlen + 2) / 3) << 2);
206
29.6k
    char *base;
207
208
29.6k
    base = (char *)cjose_get_alloc()(sizeof(char) * (rlen + 1));
209
29.6k
    if (NULL == base)
210
0
    {
211
0
        CJOSE_ERROR(err, CJOSE_ERR_NO_MEMORY);
212
0
        return false;
213
0
    }
214
215
29.6k
    size_t pos = 0, idx = 0;
216
27.7M
    while ((idx + 2) < inlen)
217
27.7M
    {
218
27.7M
        base[pos++] = alphabet[0x3f & (input[idx] >> 2)];
219
27.7M
        base[pos++] = alphabet[(0x3f & (input[idx] << 4)) | (0x3f & (input[idx + 1] >> 4))];
220
27.7M
        base[pos++] = alphabet[(0x3f & (input[idx + 1] << 2)) | (0x3f & (input[idx + 2] >> 6))];
221
27.7M
        base[pos++] = alphabet[0x3f & input[idx + 2]];
222
27.7M
        idx += 3;
223
27.7M
    }
224
225
29.6k
    if (idx < inlen)
226
19.7k
    {
227
19.7k
        if ((inlen - 1) == idx)
228
8.26k
        {
229
8.26k
            base[pos++] = alphabet[0x3f & (input[idx] >> 2)];
230
8.26k
            base[pos++] = alphabet[0x3f & (input[idx] << 4)];
231
8.26k
            if (padit)
232
0
            {
233
0
                base[pos++] = '=';
234
0
                base[pos++] = '=';
235
0
            }
236
8.26k
        }
237
11.4k
        else
238
11.4k
        {
239
11.4k
            base[pos++] = alphabet[0x3f & (input[idx] >> 2)];
240
11.4k
            base[pos++] = alphabet[(0x3f & (input[idx] << 4)) | (0x3f & (input[idx + 1] >> 4))];
241
11.4k
            base[pos++] = alphabet[0x3f & (input[idx + 1] << 2)];
242
11.4k
            if (padit)
243
0
            {
244
0
                base[pos++] = '=';
245
0
            }
246
11.4k
        }
247
19.7k
        rlen = pos;
248
19.7k
    }
249
29.6k
    base[rlen] = '\0';
250
251
29.6k
    *output = base;
252
29.6k
    *outlen = rlen;
253
29.6k
    return true;
254
29.6k
}
255
256
// interface functions
257
258
bool cjose_base64_encode(const uint8_t *input, size_t inlen, char **output, size_t *outlen, cjose_err *err)
259
0
{
260
0
    return _cjose_base64_encode_impl(input, inlen, output, outlen, ALPHABET_B64, err);
261
0
}
262
bool cjose_base64url_encode(const uint8_t *input, size_t inlen, char **output, size_t *outlen, cjose_err *err)
263
34.7k
{
264
34.7k
    return _cjose_base64_encode_impl(input, inlen, output, outlen, ALPHABET_B64U, err);
265
34.7k
}
266
267
bool cjose_base64_decode(const char *input, size_t inlen, uint8_t **output, size_t *outlen, cjose_err *err)
268
0
{
269
0
    return _cjose_base64_decode_impl(input, inlen, output, outlen, false, err);
270
0
}
271
bool cjose_base64url_decode(const char *input, size_t inlen, uint8_t **output, size_t *outlen, cjose_err *err)
272
30.0k
{
273
    return _cjose_base64_decode_impl(input, inlen, output, outlen, true, err);
274
30.0k
}