chiark / gitweb /
privcache: New closure for signature key handling
[secnet.git] / privcache.c
1 /*
2  * This file is part of secnet.
3  * See README for full list of copyright holders.
4  *
5  * secnet is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 3 of the License, or
8  * (at your option) any later version.
9  * 
10  * secnet is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License for more details.
14  * 
15  * You should have received a copy of the GNU General Public License
16  * version 3 along with secnet; if not, see
17  * https://www.gnu.org/licenses/gpl.html.
18  */
19
20 #include "secnet.h"
21 #include "util.h"
22
23 #define DEFAULT_SIZE 5
24
25 struct ent {
26     struct sigkeyid id;
27     struct sigprivkey_if *sigpriv; /* 0 means none such */
28 };
29
30 struct privcache {
31     closure_t cl;
32     struct privcache_if ops;
33     int used, alloc;
34     struct pathprefix_template path;
35     struct ent *ents;
36     struct buffer_if databuf;
37     struct hash_if *defhash;
38 };
39
40 static struct sigprivkey_if *uncached_get(struct privcache *st,
41                            const struct sigkeyid *id, struct log_if *log)
42 {
43     bool_t ok=False;
44     FILE *f=0;
45
46     sprintf(st->path.write_here, SIGKEYID_PR_FMT, SIGKEYID_PR_VAL(id));
47
48     f = fopen(st->path.buffer,"rb");
49     if (!f) {
50         if (errno == ENOENT) {
51             slilog(log,M_DEBUG,"private key %s not found\n",
52                    st->path.write_here);
53         } else {
54             slilog(log,M_ERR,"failed to open private key file %s\n",
55                    st->path.buffer);
56         }
57         goto out;
58     }
59
60     setbuf(f,0);
61     buffer_init(&st->databuf,0);
62     ssize_t got=fread(st->databuf.base,1,st->databuf.alloclen,f);
63     if (ferror(f)) {
64         slilog(log,M_ERR,"failed to read private-key file %s\n",
65                st->path.buffer);
66         goto out;
67     }
68     if (!feof(f)) {
69         slilog(log,M_ERR,"private key file %s longer than max %d\n",
70                st->path.buffer, (int)st->databuf.alloclen);
71         goto out;
72     }
73     fclose(f); f=0;
74
75     struct sigprivkey_if *sigpriv=0;
76     for (const struct sigscheme_info *scheme=sigschemes;
77          scheme->name;
78          scheme++) {
79         st->databuf.start=st->databuf.base;
80         st->databuf.size=got;
81         ok=scheme->loadpriv(scheme, &st->databuf, &sigpriv, log);
82         if (ok) {
83             if (sigpriv->sethash) {
84                 if (!st->defhash) {
85                     slilog(log,M_ERR,
86  "private key %s requires `hash' config key for privcache to load",
87                            st->path.buffer);
88                     sigpriv->dispose(sigpriv->st);
89                     sigpriv=0;
90                     goto out;
91                 }
92                 sigpriv->sethash(sigpriv->st,st->defhash);
93             }
94             goto out;
95         }
96     }
97
98     slilog(log,M_ERR,"private key file %s not loaded (not recognised?)\n",
99            st->path.buffer);
100
101   out:
102     if (f) fclose(f);
103     return ok ? sigpriv : 0;
104 }
105
106 static struct sigprivkey_if *privcache_lookup(void *sst,
107                                               const struct sigkeyid *id,
108                                               struct log_if *log) {
109     struct privcache *st = sst;
110     int was;
111     struct ent result;
112
113     for (was=0; was<st->used; was++) {
114         if (sigkeyid_equal(id, &st->ents[was].id)) {
115             result = st->ents[was];
116             goto found;
117         }
118     }
119
120     if (st->used < st->alloc) {
121         was = st->used;
122         st->used++;
123     } else {
124         was = st->used-1;
125         if (st->ents[was].sigpriv) {
126             st->ents[was].sigpriv->dispose(st->ents[was].sigpriv->st);
127         }
128     }
129
130     COPY_OBJ(result.id, *id);
131     result.sigpriv=uncached_get(st,id,log);
132
133  found:
134     memmove(&st->ents[1], &st->ents[0], sizeof(st->ents[0]) * was);
135     st->ents[0] = result;
136     return result.sigpriv;
137 }
138
139 static list_t *privcache_apply(closure_t *self, struct cloc loc,
140                                dict_t *context, list_t *args)
141 {
142     struct privcache *st;
143     item_t *item;
144     dict_t *dict;
145
146     NEW(st);
147     st->cl.description="privcache";
148     st->cl.type=CL_PRIVCACHE;
149     st->cl.apply=NULL;
150     st->cl.interface=&st->ops;
151     st->ops.st=st;
152     st->ops.lookup=privcache_lookup;
153     st->ents=0;
154     st->path.buffer=0;
155     st->used=st->alloc=0;
156     st->defhash=0;
157
158     item=list_elem(args,0);
159     if (!item || item->type!=t_dict)
160         cfgfatal(loc,"privcache","parameter must be a dictionary\n");
161     
162     dict=item->data.dict;
163
164     st->alloc=dict_read_number(dict,"privcache-size",False,"privcache",loc,
165                                DEFAULT_SIZE);
166     NEW_ARY(st->ents,st->alloc);
167     st->used=0;
168
169     int32_t buflen=dict_read_number(dict,"privkey-max",False,"privcache",loc,
170                                     4095);
171     buffer_new(&st->databuf,buflen+1);
172
173     const char *path=dict_read_string(dict,"privkeys",True,"privcache",loc);
174     int l=strlen(path);
175     NEW_ARY(st->path.buffer,l+KEYIDSZ*2+1);
176     strcpy(st->path.buffer,path);
177     st->path.write_here=st->path.buffer+l;
178
179     st->defhash=find_cl_if(dict,"hash",CL_HASH,False,"site",loc);
180
181     return new_closure(&st->cl);
182 }
183
184 void privcache_module(dict_t *dict)
185 {
186     add_closure(dict,"priv-cache",privcache_apply);
187 }