Added support for TLS_FALLBACK_SCSV.
[BearSSL] / src / codec / pemdec.t0
1 \ Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
2 \
3 \ Permission is hereby granted, free of charge, to any person obtaining
4 \ a copy of this software and associated documentation files (the
5 \ "Software"), to deal in the Software without restriction, including
6 \ without limitation the rights to use, copy, modify, merge, publish,
7 \ distribute, sublicense, and/or sell copies of the Software, and to
8 \ permit persons to whom the Software is furnished to do so, subject to
9 \ the following conditions:
10 \
11 \ The above copyright notice and this permission notice shall be
12 \ included in all copies or substantial portions of the Software.
13 \
14 \ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15 \ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16 \ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17 \ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
18 \ BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
19 \ ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20 \ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 \ SOFTWARE.
22
23 preamble {
24
25 #include "inner.h"
26
27 #define CTX ((br_pem_decoder_context *)((unsigned char *)t0ctx - offsetof(br_pem_decoder_context, cpu)))
28
29 /* see bearssl_pem.h */
30 void
31 br_pem_decoder_init(br_pem_decoder_context *ctx)
32 {
33 memset(ctx, 0, sizeof *ctx);
34 ctx->cpu.dp = &ctx->dp_stack[0];
35 ctx->cpu.rp = &ctx->rp_stack[0];
36 br_pem_decoder_init_main(&ctx->cpu);
37 br_pem_decoder_run(&ctx->cpu);
38 }
39
40 /* see bearssl_pem.h */
41 size_t
42 br_pem_decoder_push(br_pem_decoder_context *ctx,
43 const void *data, size_t len)
44 {
45 if (ctx->event) {
46 return 0;
47 }
48 ctx->hbuf = data;
49 ctx->hlen = len;
50 br_pem_decoder_run(&ctx->cpu);
51 return len - ctx->hlen;
52 }
53
54 /* see bearssl_pem.h */
55 int
56 br_pem_decoder_event(br_pem_decoder_context *ctx)
57 {
58 int event;
59
60 event = ctx->event;
61 ctx->event = 0;
62 return event;
63 }
64
65 }
66
67 \ Define a word that evaluates to the address of a field within the
68 \ decoder context.
69 : addr:
70 next-word { field }
71 "addr-" field + 0 1 define-word
72 0 8191 "offsetof(br_pem_decoder_context, " field + ")" + make-CX
73 postpone literal postpone ; ;
74
75 addr: event
76 addr: name
77 addr: buf
78 addr: ptr
79
80 \ Set a byte at a specific address (offset within the context).
81 cc: set8 ( value addr -- ) {
82 size_t addr = T0_POP();
83 unsigned x = T0_POP();
84 *((unsigned char *)CTX + addr) = x;
85 }
86
87 \ Get a byte at a specific address (offset within the context).
88 cc: get8 ( addr -- value ) {
89 size_t addr = T0_POP();
90 T0_PUSH(*((unsigned char *)CTX + addr));
91 }
92
93 \ Send an event.
94 : send-event ( event -- )
95 addr-event set8 co ;
96
97 \ Low-level function to read a single byte. Returned value is the byte
98 \ (0 to 255), or -1 if there is no available data.
99 cc: read8-native ( -- x ) {
100 if (CTX->hlen > 0) {
101 T0_PUSH(*CTX->hbuf ++);
102 CTX->hlen --;
103 } else {
104 T0_PUSHi(-1);
105 }
106 }
107
108 \ Read next byte. Block until the next byte is available.
109 : read8 ( -- x )
110 begin read8-native dup 0< ifnot ret then drop co again ;
111
112 \ Read bytes until next end-of-line.
113 : skip-newline ( -- )
114 begin read8 `\n <> while repeat ;
115
116 \ Read bytes until next end-of-line; verify that they are all whitespace.
117 \ This returns -1 if they were all whitespace, 0 otherwise.
118 : skip-newline-ws ( -- bool )
119 -1 { r }
120 begin read8 dup `\n <> while ws? ifnot 0 >r then repeat
121 drop r ;
122
123 \ Normalise a byte to uppercase (ASCII only).
124 : norm-upper ( x -- x )
125 dup dup `a >= swap `z <= and if 32 - then ;
126
127 \ Read bytes and compare with the provided string. On mismatch, the
128 \ rest of the line is consumed. Matching is not case sensitive.
129 : match-string ( str -- bool )
130 begin
131 dup data-get8 norm-upper dup ifnot 2drop -1 ret then
132 read8 norm-upper dup `\n = if drop 2drop 0 ret then
133 = ifnot drop skip-newline 0 ret then
134 1+
135 again ;
136
137 \ Read bytes into the provided buffer, but no more than the provided
138 \ count. Reading stops when end-of-line is reached. Returned value
139 \ is the count of bytes written to the buffer, or 0 if the buffer size
140 \ was exceeded. All bytes are normalised to uppercase (ASCII only).
141 : read-bytes ( addr len -- len )
142 dup { orig-len }
143 swap
144 begin
145 over ifnot 2drop skip-newline 0 ret then
146 read8 dup `\n = if 2drop orig-len swap - ret then
147 norm-upper over set8 1+ swap 1- swap
148 again ;
149
150 \ Remove trailing dashes from the name buffer.
151 : trim-dashes ( len -- )
152 begin dup while
153 1-
154 dup addr-name + get8 `- <> if
155 addr-name + 1+ 0 swap set8 ret
156 then
157 repeat
158 addr-name set8 ;
159
160 \ Scan input for next "begin" banner.
161 : next-banner-begin ( -- )
162 begin
163 "-----BEGIN " match-string if
164 addr-name 127 read-bytes
165 dup if trim-dashes ret then
166 drop
167 then
168 again ;
169
170 \ Convert a Base64 character to its numerical value. Returned value is
171 \ 0 to 63 for Base64 characters, -1 for '=', and -2 for all other characters.
172 : from-base64 ( char -- x )
173 dup dup `A >= swap `Z <= and if 65 - ret then
174 dup dup `a >= swap `z <= and if 71 - ret then
175 dup dup `0 >= swap `9 <= and if 4 + ret then
176 dup `+ = if drop 62 ret then
177 dup `/ = if drop 63 ret then
178 `= <> 1- ;
179
180 \ Test whether a character is whitespace (but not a newline).
181 : ws? ( x -- bool )
182 dup `\n <> swap 32 <= and ;
183
184 \ Read next character, skipping whitespace (except newline).
185 : next-nonws ( -- x )
186 begin
187 read8 dup ws? ifnot ret then
188 drop
189 again ;
190
191 \ Write one byte in the output buffer.
192 cc: write8 ( x -- ) {
193 unsigned char x = (unsigned char)T0_POP();
194 CTX->buf[CTX->ptr ++] = x;
195 if (CTX->ptr == sizeof CTX->buf) {
196 if (CTX->dest) {
197 CTX->dest(CTX->dest_ctx, CTX->buf, sizeof CTX->buf);
198 }
199 CTX->ptr = 0;
200 }
201 }
202
203 \ Flush the output buffer.
204 cc: flush-buf ( -- ) {
205 if (CTX->ptr > 0) {
206 CTX->dest(CTX->dest_ctx, CTX->buf, CTX->ptr);
207 CTX->ptr = 0;
208 }
209 }
210
211 \ Decode the four next Base64 characters. Returned value is:
212 \ 0 quartet processed, three bytes produced.
213 \ -1 dash encountered as first character (no leading whitespace).
214 \ 1 quartet processed, one or two bytes produced, terminator reached.
215 \ 2 end-of-line reached.
216 \ 3 error.
217 \ For all positive return values, the remaining of the current line has been
218 \ consumed.
219 : decode-next-quartet ( -- r )
220 \ Process first character. It may be a dash.
221 read8 dup `- = if drop -1 ret then
222 dup ws? if drop next-nonws then
223 dup `\n = if drop 2 ret then
224 from-base64 dup 0< if drop skip-newline 3 ret then
225 { acc }
226
227 \ Second character.
228 next-nonws dup `\n = if drop 3 ret then
229 from-base64 dup 0< if drop skip-newline 3 ret then
230 acc 6 << + >acc
231
232 \ Third character: may be an equal sign.
233 next-nonws dup `\n = if drop 3 ret then
234 dup `= = if
235 \ Fourth character must be an equal sign.
236 drop
237 next-nonws dup `\n = if drop 3 ret then
238 skip-newline-ws ifnot drop 3 ret then
239 `= <> if 3 ret then
240 acc 0x0F and if 3 ret then
241 acc 4 >> write8
242 1 ret
243 then
244 from-base64 dup 0< if drop skip-newline 3 ret then
245 acc 6 << + >acc
246
247 \ Fourth character: may be an equal sign.
248 next-nonws dup `\n = if drop 3 ret then
249 dup `= = if
250 drop skip-newline-ws ifnot 3 ret then
251 acc 0x03 and if 3 ret then
252 acc 10 >> write8
253 acc 2 >> write8
254 1 ret
255 then
256 from-base64 dup 0< if drop skip-newline 3 ret then
257 acc 6 << + >acc
258 acc 16 >> write8
259 acc 8 >> write8
260 acc write8
261 0 ;
262
263 \ Check trailer line (possibly, the leading dash has been read). This
264 \ sends the appropriate event.
265 : check-trailer ( bool -- )
266 ifnot
267 begin read8 dup `\n = while drop repeat
268 `- <> if skip-newline 3 send-event ret then
269 then
270 "----END " match-string ifnot 3 send-event ret then
271 flush-buf
272 skip-newline 2 send-event ;
273
274 \ Decode one line worth of characters. Returned value is 0 if the end of the
275 \ object is reached, -1 otherwise. The end of object or error event is sent.
276 : decode-line ( -- bool )
277 -1 { first }
278 begin
279 decode-next-quartet
280 case
281 0 of endof
282 -1 of
283 first ifnot
284 skip-newline 3 send-event
285 else
286 -1 check-trailer
287 then
288 0 ret
289 endof
290 1 of 0 check-trailer 0 ret endof
291 2 of -1 ret endof
292
293 \ On decoding error
294 drop 3 send-event 0 ret
295 endcase
296 0 >first
297 again ;
298
299 : main ( -- ! )
300 begin
301 next-banner-begin 1 send-event
302 begin decode-line while repeat
303 again ;