chiark / gitweb /
pubkeys: Provide `fallback_skip' variable
[secnet.git] / pubkeys.fl.pl
1 #!/usr/bin/perl -w
2 # -*- C -*-
3 #
4 # secnet - pubkeys.fl.pl
5 #
6 # This file is part of secnet.
7 # See README for full list of copyright holders.
8 #
9 # secnet is free software; you can redistribute it and/or modify it
10 # under the terms of the GNU General Public License as published by
11 # the Free Software Foundation; either version 3 of the License, or
12 # (at your option) any later version.
13
14 # secnet is distributed in the hope that it will be useful, but
15 # WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17 # General Public License for more details.
18
19 # You should have received a copy of the GNU General Public License
20 # version 3 along with secnet; if not, see
21 # https://www.gnu.org/licenses/gpl.html.
22
23 # We process __DATA__ of this file first through the perl code,
24 # and then through flex.  We do it like this because directives
25 # with positional arguments are otherwise rather tedious to specify
26 # in flex.  Of course we could have used bison too but this seems
27 # better overall.
28
29 use strict;
30
31 our $do = '';
32 our $co = '';
33 our $kw;
34 our $kwid;
35 our @next_kw;
36 our $in_s;
37 our $data_off;
38
39 our %subst = (GRPIDSZ => 4, SERIALSZ => 4);
40
41 our $last_lno = -1;
42 sub lineno (;$$) {
43     my ($always, $delta) = @_;
44     my $o = '';
45     $delta //= 0;
46     if ($always || $. != $last_lno+1) {
47         $o .= sprintf "#line %d \"%s\"\n", $delta+$data_off+$., $0;
48     }
49     $last_lno = $.;
50     $o;
51 }
52
53 while (<DATA>) {
54         last if m/^\%\%\s*$/;
55         if (m/^!SUBSTCHECKS\s*$/) {
56                 foreach (keys %subst) {
57                         $do .= <<END
58 #if $_ != $subst{$_}
59 # error $_ value disagrees between pubkeys.fl.pl and C headers
60 #endif
61 END
62                 }
63                 next;
64         }
65         $do .= lineno();
66         $do .= $_;
67 }
68
69 sub inst ($) {
70         $do .= "%x $_[0]\n";
71         "<$_[0]>";
72 }
73
74 while (<DATA>) {
75     s#\{!2(\w+)\}# '{'.(2 * ($subst{$1}//die "$1 ?")).'}' #ge;
76     if (m/^!KEYWORD ([-0-9a-z]+)(\s*\{.*\})?$/) {
77         my $kwt=$2;
78         die if $kw;
79         $kw = $1;
80         my $xact = $3 // '';
81         $kwid = $kw; $kwid =~ y/-/_/;
82         $in_s = "HK_${kwid}";
83         $co .= "{L}$kwt { BEGIN($in_s); $xact }\n";
84         next;
85     }
86     if (m/^!ARG (\w+) (\S.*\S) \{\s*$/) {
87         die unless $kw;
88         die if @next_kw;
89         $co .= inst("$in_s")."{S} { BEGIN(D_${kwid}_$1); }\n";
90         $co .= inst("D_${kwid}_$1")."$2 {\n";
91         $in_s = "HA_${kwid}_$1";
92         $co .= "\tBEGIN($in_s);\n";
93         @next_kw = ($kw);
94         $co .= lineno(1,1);
95         next;
96     }
97     if (m/^!\}\s*$/) {
98         die unless @next_kw;
99         $co .= lineno(1,0);
100         $co .= "}\n";
101         $kw = shift @next_kw;
102         next;
103     }
104     if (m/^!FINAL \{\s*$/) {
105         die unless $kw;
106         die if @next_kw;
107         $co .= inst("FIN_$kwid")."\\n { BEGIN(0); c->lno++; }\n";
108         $co .= inst("$in_s")."{L}/\\n {\n";
109         $co .= "\tBEGIN(FIN_$kwid);\n";
110         $co .= lineno(1,1);
111         @next_kw = (undef);
112         next;
113     }
114     if (m/^!/) {
115         die;
116     }
117     $co .= $_;
118     if (m/^\%\%\s*$/) {
119         $co .= lineno(1,1);
120     }
121 }
122
123 print $do, "%%\n", $co or die $!;
124
125 BEGIN { $data_off = __LINE__ + 1; }
126 __DATA__
127
128 L       [ \t]*
129 S       [ \t]+
130 BASE91S []-~!#-&(-[]+
131 %x SKIPNL
132
133 %option yylineno
134 %option noyywrap
135 %option batch
136 %option 8bit
137 %option nodefault
138 %option never-interactive
139
140 %option prefix="pkyy"
141
142 %option warn
143
144 %{
145
146 #include "secnet.h"
147 #include "pubkeys.h"
148 #include "util.h"
149 #include "unaligned.h"
150 #include "base91s/base91.h"
151
152 !SUBSTCHECKS
153
154 struct pubkeyset_context {
155     /* filled in during setup: */
156     struct log_if *log;
157     struct buffer_if *data_buf;
158     struct peer_keyset *building;
159     /* runtime: */
160     bool_t had_serial;
161     int lno;
162     bool_t fallback_skip;
163     const struct sigscheme_info *scheme;
164     uint8_t grpid[GRPIDSZ];
165     serialt serial;
166 };
167
168 static struct pubkeyset_context c[1];
169
170 #define LI (c->log)
171 #define HEX2BIN(v,l) ({                                                 \
172         int32_t outlen;                                                 \
173         bool_t ok=hex_decode((v), ((l)), &outlen, yytext, False);       \
174         assert(ok);                                                     \
175         assert(outlen==((l)));                                          \
176     })
177 #define HEX2BIN_ARRAY(v) HEX2BIN((v),sizeof((v)))
178 #define DOSKIPQ ({                                      \
179         BEGIN(SKIPNL);                                  \
180         break;                                          \
181     })
182 #define DOSKIP(m) ({                                    \
183         slilog(LI,M_INFO,"l.%d: " m, c->lno);   \
184         DOSKIPQ;                                        \
185     })
186 #define FAIL(m) do{                                     \
187         slilog(LI,M_ERR,"l.%d: " m, c->lno);    \
188         return -1;                                      \
189     }while(0)
190
191 %}
192
193 %%
194
195 !KEYWORD pkg  { c->fallback_skip=0; }
196 !ARG id [0-9a-f]{!2GRPIDSZ} {
197     HEX2BIN_ARRAY(c->grpid);
198 !}
199 !FINAL {
200 !}
201 !KEYWORD pub
202 !ARG algo [-0-9a-z]+ {
203     if (c->fallback_skip) DOSKIP("fallback not needed");
204     c->scheme = sigscheme_lookup(yytext);
205     if (!c->scheme) DOSKIP("unknown pk algorithm");
206 !}
207 !ARG data {BASE91S} {
208     /* baseE91 and thus base91s can sometimes store 14 bits per
209      * character pair, so the max decode ratio is 14/16. */
210     size_t maxl = base91s_decode_maxlen(yyleng);
211     buffer_init(c->data_buf,0);
212     if (buf_remaining_space(c->data_buf) < maxl) DOSKIP("pk data too long");
213     struct base91s b91;
214     base91s_init(&b91);
215     size_t l = base91s_decode(&b91, yytext, yyleng, c->data_buf->start);
216     l += base91s_decode_end(&b91, c->data_buf->start + l);
217     assert(l <= maxl);
218     buf_append(c->data_buf,l);
219 !}
220 !FINAL {
221     if (c->building->nkeys >= MAX_SIG_KEYS) DOSKIP("too many public keys");
222     struct sigpubkey_if *pubkey;
223     bool_t ok=c->scheme->loadpub(c->scheme,c->data_buf,
224                                  &pubkey,c->log);
225     if (!ok) break;
226     memcpy(c->building->keys[c->building->nkeys].id.b,
227            c->grpid,
228            GRPIDSZ);
229     assert(ALGIDSZ==1); /* otherwise need htons or htonl or something */
230     c->building->keys[c->building->nkeys].id.b[GRPIDSZ]=
231       c->scheme->algid;
232     c->building->keys[c->building->nkeys++].pubkey=pubkey;
233 !}
234
235 !KEYWORD serial
236 !ARG id [0-9a-f]{!2SERIALSZ} {
237     if (c->had_serial) FAIL("`serial' repeated");
238     c->had_serial = 1;
239     uint8_t sb[SERIALSZ];
240     HEX2BIN_ARRAY(sb);
241     c->serial=get_uint32(sb);
242 !}
243 !FINAL {
244 !}
245
246 {L}[-0-9a-z]+ {
247     DOSKIP("unknown directive");
248 }
249
250 {L}\# {
251     BEGIN(SKIPNL);
252 }
253 {L}\n {
254     c->lno++;
255 }
256
257 <SKIPNL>.*\n {
258     c->lno++;
259     BEGIN(0);
260 }
261
262 <INITIAL><<EOF>>        { return 0; }
263
264 <*>. { FAIL("syntax error"); }
265 <*>\n { FAIL("syntax error - unexpected newline"); }
266 <<EOF>> { FAIL("syntax error - unexpected eof"); }
267
268 %%
269
270 extern struct peer_keyset *
271 keyset_load(const char *path, struct buffer_if *data_buf,
272             struct log_if *log, int logcl_enoent) {
273     assert(!c->building);
274     c->log=log;
275     pkyyin = fopen(path, "r");
276     if (!pkyyin) {
277         slilog(LI,
278                errno==ENOENT ? logcl_enoent : M_ERR,
279                "could not open keyset file %s: %s",
280                path,strerror(errno));
281         goto err;
282     }
283
284     pkyyrestart(pkyyin);
285     BEGIN(0);
286     c->data_buf=data_buf;
287     NEW(c->building);
288     c->building->nkeys=0;
289     c->building->refcount=1;
290     c->fallback_skip=0;
291     c->had_serial=0;
292     c->lno=1;
293     FILLZERO(c->grpid);
294     FILLZERO(c->serial);
295     int r=pkyylex();
296     if (r) goto err_bad;
297
298     if (!c->had_serial) {
299         slilog(LI,M_ERR,"missing serial number in %s",path);
300         goto err_bad;
301     }
302     if (!c->building->nkeys) {
303         slilog(LI,M_ERR,"no useable keys in %s",path);
304         goto err_bad;
305     }
306     fclose(pkyyin);
307     struct peer_keyset *built=c->building;
308     c->building=0;
309     return built;
310
311  err_bad:
312     errno=EBADMSG;
313  err:
314     if (c->building) { free(c->building); c->building=0; }
315     if (pkyyin) { fclose(pkyyin); pkyyin=0; }
316     return 0;
317 }
318