/src/bind9/lib/isc/base64.c
Line | Count | Source |
1 | | /* |
2 | | * Copyright (C) Internet Systems Consortium, Inc. ("ISC") |
3 | | * |
4 | | * SPDX-License-Identifier: MPL-2.0 |
5 | | * |
6 | | * This Source Code Form is subject to the terms of the Mozilla Public |
7 | | * License, v. 2.0. If a copy of the MPL was not distributed with this |
8 | | * file, you can obtain one at https://mozilla.org/MPL/2.0/. |
9 | | * |
10 | | * See the COPYRIGHT file distributed with this work for additional |
11 | | * information regarding copyright ownership. |
12 | | */ |
13 | | |
14 | | /*! \file */ |
15 | | |
16 | | #include <stdbool.h> |
17 | | |
18 | | #include <isc/base64.h> |
19 | | #include <isc/buffer.h> |
20 | | #include <isc/lex.h> |
21 | | #include <isc/string.h> |
22 | | #include <isc/util.h> |
23 | | |
24 | | /*@{*/ |
25 | | /*! |
26 | | * These static functions are also present in lib/dns/rdata.c. I'm not |
27 | | * sure where they should go. -- bwelling |
28 | | */ |
29 | | static isc_result_t |
30 | | str_totext(const char *source, isc_buffer_t *target); |
31 | | |
32 | | static isc_result_t |
33 | | mem_tobuffer(isc_buffer_t *target, void *base, unsigned int length); |
34 | | |
35 | | static const char base64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvw" |
36 | | "xyz0123456789+/="; |
37 | | /*@}*/ |
38 | | |
39 | | isc_result_t |
40 | | isc_base64_totext(isc_region_t *source, int wordlength, const char *wordbreak, |
41 | 0 | isc_buffer_t *target) { |
42 | 0 | char buf[5]; |
43 | 0 | unsigned int loops = 0; |
44 | |
|
45 | 0 | if (wordlength < 4) { |
46 | 0 | wordlength = 4; |
47 | 0 | } |
48 | |
|
49 | 0 | memset(buf, 0, sizeof(buf)); |
50 | 0 | while (source->length > 2) { |
51 | 0 | buf[0] = base64[(source->base[0] >> 2) & 0x3f]; |
52 | 0 | buf[1] = base64[((source->base[0] << 4) & 0x30) | |
53 | 0 | ((source->base[1] >> 4) & 0x0f)]; |
54 | 0 | buf[2] = base64[((source->base[1] << 2) & 0x3c) | |
55 | 0 | ((source->base[2] >> 6) & 0x03)]; |
56 | 0 | buf[3] = base64[source->base[2] & 0x3f]; |
57 | 0 | RETERR(str_totext(buf, target)); |
58 | 0 | isc_region_consume(source, 3); |
59 | |
|
60 | 0 | loops++; |
61 | 0 | if (source->length != 0 && (int)((loops + 1) * 4) >= wordlength) |
62 | 0 | { |
63 | 0 | loops = 0; |
64 | 0 | RETERR(str_totext(wordbreak, target)); |
65 | 0 | } |
66 | 0 | } |
67 | 0 | if (source->length == 2) { |
68 | 0 | buf[0] = base64[(source->base[0] >> 2) & 0x3f]; |
69 | 0 | buf[1] = base64[((source->base[0] << 4) & 0x30) | |
70 | 0 | ((source->base[1] >> 4) & 0x0f)]; |
71 | 0 | buf[2] = base64[((source->base[1] << 2) & 0x3c)]; |
72 | 0 | buf[3] = '='; |
73 | 0 | RETERR(str_totext(buf, target)); |
74 | 0 | isc_region_consume(source, 2); |
75 | 0 | } else if (source->length == 1) { |
76 | 0 | buf[0] = base64[(source->base[0] >> 2) & 0x3f]; |
77 | 0 | buf[1] = base64[((source->base[0] << 4) & 0x30)]; |
78 | 0 | buf[2] = buf[3] = '='; |
79 | 0 | RETERR(str_totext(buf, target)); |
80 | 0 | isc_region_consume(source, 1); |
81 | 0 | } |
82 | 0 | return ISC_R_SUCCESS; |
83 | 0 | } |
84 | | |
85 | | /*% |
86 | | * State of a base64 decoding process in progress. |
87 | | */ |
88 | | typedef struct { |
89 | | int length; /*%< Desired length of binary data or -1 */ |
90 | | isc_buffer_t *target; /*%< Buffer for resulting binary data */ |
91 | | int digits; /*%< Number of buffered base64 digits */ |
92 | | bool seen_end; /*%< True if "=" end marker seen */ |
93 | | int val[4]; |
94 | | } base64_decode_ctx_t; |
95 | | |
96 | | static void |
97 | 0 | base64_decode_init(base64_decode_ctx_t *ctx, int length, isc_buffer_t *target) { |
98 | 0 | ctx->digits = 0; |
99 | 0 | ctx->seen_end = false; |
100 | 0 | ctx->length = length; |
101 | 0 | ctx->target = target; |
102 | 0 | } |
103 | | |
104 | | static isc_result_t |
105 | 0 | base64_decode_char(base64_decode_ctx_t *ctx, int c) { |
106 | 0 | const char *s; |
107 | |
|
108 | 0 | if (ctx->seen_end) { |
109 | 0 | return ISC_R_BADBASE64; |
110 | 0 | } |
111 | 0 | if ((s = strchr(base64, c)) == NULL) { |
112 | 0 | return ISC_R_BADBASE64; |
113 | 0 | } |
114 | 0 | ctx->val[ctx->digits++] = (int)(s - base64); |
115 | 0 | if (ctx->digits == 4) { |
116 | 0 | int n; |
117 | 0 | unsigned char buf[3]; |
118 | 0 | if (ctx->val[0] == 64 || ctx->val[1] == 64) { |
119 | 0 | return ISC_R_BADBASE64; |
120 | 0 | } |
121 | 0 | if (ctx->val[2] == 64 && ctx->val[3] != 64) { |
122 | 0 | return ISC_R_BADBASE64; |
123 | 0 | } |
124 | | /* |
125 | | * Check that bits that should be zero are. |
126 | | */ |
127 | 0 | if (ctx->val[2] == 64 && (ctx->val[1] & 0xf) != 0) { |
128 | 0 | return ISC_R_BADBASE64; |
129 | 0 | } |
130 | | /* |
131 | | * We don't need to test for ctx->val[2] != 64 as |
132 | | * the bottom two bits of 64 are zero. |
133 | | */ |
134 | 0 | if (ctx->val[3] == 64 && (ctx->val[2] & 0x3) != 0) { |
135 | 0 | return ISC_R_BADBASE64; |
136 | 0 | } |
137 | 0 | n = (ctx->val[2] == 64) ? 1 : (ctx->val[3] == 64) ? 2 : 3; |
138 | 0 | if (n != 3) { |
139 | 0 | ctx->seen_end = true; |
140 | 0 | if (ctx->val[2] == 64) { |
141 | 0 | ctx->val[2] = 0; |
142 | 0 | } |
143 | 0 | if (ctx->val[3] == 64) { |
144 | 0 | ctx->val[3] = 0; |
145 | 0 | } |
146 | 0 | } |
147 | 0 | buf[0] = (ctx->val[0] << 2) | (ctx->val[1] >> 4); |
148 | 0 | buf[1] = (ctx->val[1] << 4) | (ctx->val[2] >> 2); |
149 | 0 | buf[2] = (ctx->val[2] << 6) | (ctx->val[3]); |
150 | 0 | RETERR(mem_tobuffer(ctx->target, buf, n)); |
151 | 0 | if (ctx->length >= 0) { |
152 | 0 | if (n > ctx->length) { |
153 | 0 | return ISC_R_BADBASE64; |
154 | 0 | } else { |
155 | 0 | ctx->length -= n; |
156 | 0 | } |
157 | 0 | } |
158 | 0 | ctx->digits = 0; |
159 | 0 | } |
160 | 0 | return ISC_R_SUCCESS; |
161 | 0 | } |
162 | | |
163 | | static isc_result_t |
164 | 0 | base64_decode_finish(base64_decode_ctx_t *ctx) { |
165 | 0 | if (ctx->length > 0) { |
166 | 0 | return ISC_R_UNEXPECTEDEND; |
167 | 0 | } |
168 | 0 | if (ctx->digits != 0) { |
169 | 0 | return ISC_R_BADBASE64; |
170 | 0 | } |
171 | 0 | return ISC_R_SUCCESS; |
172 | 0 | } |
173 | | |
174 | | isc_result_t |
175 | 0 | isc_base64_tobuffer(isc_lex_t *lexer, isc_buffer_t *target, int length) { |
176 | 0 | unsigned int before, after; |
177 | 0 | base64_decode_ctx_t ctx; |
178 | 0 | isc_textregion_t *tr; |
179 | 0 | isc_token_t token; |
180 | 0 | bool eol; |
181 | |
|
182 | 0 | REQUIRE(length >= -2); |
183 | |
|
184 | 0 | base64_decode_init(&ctx, length, target); |
185 | |
|
186 | 0 | before = isc_buffer_usedlength(target); |
187 | 0 | while (!ctx.seen_end && (ctx.length != 0)) { |
188 | 0 | unsigned int i; |
189 | |
|
190 | 0 | if (length > 0) { |
191 | 0 | eol = false; |
192 | 0 | } else { |
193 | 0 | eol = true; |
194 | 0 | } |
195 | 0 | RETERR(isc_lex_getmastertoken(lexer, &token, |
196 | 0 | isc_tokentype_string, eol)); |
197 | 0 | if (token.type != isc_tokentype_string) { |
198 | 0 | break; |
199 | 0 | } |
200 | 0 | tr = &token.value.as_textregion; |
201 | 0 | for (i = 0; i < tr->length; i++) { |
202 | 0 | RETERR(base64_decode_char(&ctx, tr->base[i])); |
203 | 0 | } |
204 | 0 | } |
205 | 0 | after = isc_buffer_usedlength(target); |
206 | 0 | if (ctx.length < 0 && !ctx.seen_end) { |
207 | 0 | isc_lex_ungettoken(lexer, &token); |
208 | 0 | } |
209 | 0 | RETERR(base64_decode_finish(&ctx)); |
210 | 0 | if (length == -2 && before == after) { |
211 | 0 | return ISC_R_UNEXPECTEDEND; |
212 | 0 | } |
213 | 0 | return ISC_R_SUCCESS; |
214 | 0 | } |
215 | | |
216 | | isc_result_t |
217 | 0 | isc_base64_decodestring(const char *cstr, isc_buffer_t *target) { |
218 | 0 | base64_decode_ctx_t ctx; |
219 | |
|
220 | 0 | base64_decode_init(&ctx, -1, target); |
221 | 0 | for (;;) { |
222 | 0 | int c = *cstr++; |
223 | 0 | if (c == '\0') { |
224 | 0 | break; |
225 | 0 | } |
226 | 0 | if (c == ' ' || c == '\t' || c == '\n' || c == '\r') { |
227 | 0 | continue; |
228 | 0 | } |
229 | 0 | RETERR(base64_decode_char(&ctx, c)); |
230 | 0 | } |
231 | 0 | RETERR(base64_decode_finish(&ctx)); |
232 | 0 | return ISC_R_SUCCESS; |
233 | 0 | } |
234 | | |
235 | | static isc_result_t |
236 | 0 | str_totext(const char *source, isc_buffer_t *target) { |
237 | 0 | unsigned int l; |
238 | 0 | isc_region_t region; |
239 | |
|
240 | 0 | isc_buffer_availableregion(target, ®ion); |
241 | 0 | l = strlen(source); |
242 | |
|
243 | 0 | if (l > region.length) { |
244 | 0 | return ISC_R_NOSPACE; |
245 | 0 | } |
246 | | |
247 | 0 | memmove(region.base, source, l); |
248 | 0 | isc_buffer_add(target, l); |
249 | 0 | return ISC_R_SUCCESS; |
250 | 0 | } |
251 | | |
252 | | static isc_result_t |
253 | 0 | mem_tobuffer(isc_buffer_t *target, void *base, unsigned int length) { |
254 | 0 | isc_region_t tr; |
255 | |
|
256 | 0 | isc_buffer_availableregion(target, &tr); |
257 | 0 | if (length > tr.length) { |
258 | 0 | return ISC_R_NOSPACE; |
259 | 0 | } |
260 | 0 | memmove(tr.base, base, length); |
261 | 0 | isc_buffer_add(target, length); |
262 | 0 | return ISC_R_SUCCESS; |
263 | 0 | } |