Debian Codegraph @

crt/s2n/stuffer/s2n_stuffer_base64.c

raw symbols SCIP
1 /*
2 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License").
5 * You may not use this file except in compliance with the License.
6 * A copy of the License is located at
7 *
8 * http://aws.amazon.com/apache2.0
9 *
10 * or in the "license" file accompanying this file. This file is distributed
11 * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12 * express or implied. See the License for the specific language governing
13 * permissions and limitations under the License.
14 */
15
16 #include <openssl/evp.h>
17 #include <string.h>
18
19 #include "error/s2n_errno.h"
20 #include "stuffer/s2n_stuffer.h"
21 #include "utils/s2n_safety.h"
22
23 bool s2n_is_base64_char(unsigned char c)
24 {
25 /* use bitwise operations to minimize branching */
26 uint8_t out = 0;
27 out ^= (c >= 'A') & (c <= 'Z');
28 out ^= (c >= 'a') & (c <= 'z');
29 out ^= (c >= '0') & (c <= '9');
30 out ^= c == '+';
31 out ^= c == '/';
32 out ^= c == '=';
33
34 return out == 1;
35 }
36
37 /* We use the base64 decoding implementation from the libcrypto to allow for
38 * sidechannel-resistant base64 decoding. While OpenSSL doesn't support this,
39 * AWS-LC does.
40 */
41 int s2n_stuffer_read_base64(struct s2n_stuffer *stuffer, struct s2n_stuffer *out)
42 {
43 POSIX_PRECONDITION(s2n_stuffer_validate(stuffer));
44 POSIX_PRECONDITION(s2n_stuffer_validate(out));
45
46 int base64_groups = s2n_stuffer_data_available(stuffer) / 4;
47 if (base64_groups == 0) {
48 return S2N_SUCCESS;
49 }
50 int base64_data_size = base64_groups * 4;
51 int binary_output_size = base64_groups * 3;
52
53 const uint32_t base64_data_offset = stuffer->read_cursor;
54 POSIX_GUARD(s2n_stuffer_skip_read(stuffer, base64_data_size));
55 const uint8_t *start_of_base64_data = stuffer->blob.data + base64_data_offset;
56
57 const uint32_t binary_output_offset = out->write_cursor;
58 POSIX_GUARD(s2n_stuffer_skip_write(out, binary_output_size));
59 uint8_t *start_of_binary_output = out->blob.data + binary_output_offset;
60
61 /* https://docs.openssl.org/master/man3/EVP_EncodeInit/
62 * > This function will return the length of the data decoded or -1 on error. */
63 int res = EVP_DecodeBlock(start_of_binary_output, start_of_base64_data, base64_data_size);
64 POSIX_ENSURE(res == binary_output_size, S2N_ERR_INVALID_BASE64);
65
66 /* https://docs.openssl.org/1.1.1/man3/EVP_EncodeInit/
67 * > The output will be padded with 0 bits if necessary to ensure that the
68 * > output is always 3 bytes for every 4 input bytes.
69 * FFFF -> 0x14 0x51 0x45
70 * FFF= -> 0x14 0x51 0x00
71 * FF== -> 0x14 0x00 0x00
72 * F=== -> INVALID
73 */
74 /* manually unrolled loop to prevent CBMC errors */
75 POSIX_ENSURE_GTE(stuffer->read_cursor, 2);
76 if (stuffer->blob.data[stuffer->read_cursor - 1] == '=') {
77 out->write_cursor -= 1;
78 }
79 if (stuffer->blob.data[stuffer->read_cursor - 2] == '=') {
80 out->write_cursor -= 1;
81 }
82
83 return S2N_SUCCESS;
84 }
85
86 int s2n_stuffer_write_base64(struct s2n_stuffer *stuffer, struct s2n_stuffer *in)
87 {
88 POSIX_PRECONDITION(s2n_stuffer_validate(stuffer));
89 POSIX_PRECONDITION(s2n_stuffer_validate(in));
90
91 int binary_data_size = s2n_stuffer_data_available(in);
92 if (binary_data_size == 0) {
93 return S2N_SUCCESS;
94 }
95
96 int base64_groups = binary_data_size / 3;
97 /* we will need to add a final padded block */
98 if (binary_data_size % 3 != 0) {
99 base64_groups++;
100 }
101
102 int base64_output_size = base64_groups * 4;
103 /* Null terminator is added */
104 base64_output_size += 1;
105
106 const uint32_t binary_data_offset = in->read_cursor;
107 POSIX_GUARD(s2n_stuffer_skip_read(in, binary_data_size));
108 const uint8_t *start_of_binary_data = in->blob.data + binary_data_offset;
109
110 const uint32_t base64_output_offset = stuffer->write_cursor;
111 POSIX_GUARD(s2n_stuffer_skip_write(stuffer, base64_output_size));
112 uint8_t *start_of_base64_output = stuffer->blob.data + base64_output_offset;
113
114 /* https://docs.openssl.org/master/man3/EVP_EncodeInit/
115 * > The length of the data generated without the NUL terminator is returned from the function. */
116 int res = EVP_EncodeBlock(start_of_base64_output, start_of_binary_data, binary_data_size);
117 POSIX_ENSURE(res == base64_output_size - 1, S2N_ERR_INVALID_BASE64);
118 POSIX_GUARD(s2n_stuffer_wipe_n(stuffer, 1));
119
120 return S2N_SUCCESS;
121 }