2 * This file is part of secnet.
3 * See README for full list of copyright holders.
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.
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.
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.
23 #define DEFAULT_SIZE 5
24 #define DEFAULT_MAXPRIV_BYTES 4095
28 struct sigprivkey_if *sigpriv; /* 0 means none such */
33 struct privcache_if ops;
35 struct pathprefix_template path;
37 struct buffer_if databuf;
38 struct hash_if *defhash;
41 static bool_t uncached_load_file(
42 const struct sigscheme_info *scheme,
44 struct buffer_if *databuf,
45 struct hash_if *defhash,
46 struct sigprivkey_if **sigpriv_r,
47 closure_t **closure_r,
50 static struct sigprivkey_if *uncached_get(struct privcache *st,
51 const struct sigkeyid *id, struct log_if *log)
53 sprintf(st->path.write_here, SIGKEYID_PR_FMT, SIGKEYID_PR_VAL(id));
55 const char *path=st->path.buffer;
56 const struct sigscheme_info *scheme;
57 for (scheme=sigschemes;
60 if (scheme->algid == id->b[GRPIDSZ])
63 slilog(log,M_ERR,"private key file %s not loaded (unknown algid)",
68 struct sigprivkey_if *sigpriv;
70 bool_t ok=uncached_load_file(scheme,
77 return ok ? sigpriv : 0;
80 static bool_t uncached_load_file(
81 const struct sigscheme_info *scheme,
83 struct buffer_if *databuf,
84 struct hash_if *defhash,
85 struct sigprivkey_if **sigpriv_r,
86 closure_t **closure_r,
91 struct sigprivkey_if *sigpriv=0;
95 if (errno == ENOENT) {
96 slilog(log,M_DEBUG,"private key %s not found",
99 slilog(log,M_ERR,"failed to open private key file %s",
106 buffer_init(databuf,0);
107 ssize_t got=fread(databuf->base,1,databuf->alloclen,f);
109 slilog(log,M_ERR,"failed to read private-key file %s",
114 slilog(log,M_ERR,"private key file %s longer than max %d",
115 path, (int)databuf->alloclen);
120 databuf->start=databuf->base;
122 struct cloc loc = { .file=path, .line=0 };
123 ok=scheme->loadpriv(scheme, databuf, &sigpriv, closure_r, log, loc);
124 if (!ok) goto error_out; /* loadpriv will have logged */
126 if (sigpriv->sethash) {
129 "private key %s requires `hash' config key to load",
133 sigpriv->sethash(sigpriv->st,defhash);
142 if (sigpriv) sigpriv->dispose(sigpriv->st);
147 static struct sigprivkey_if *privcache_lookup(void *sst,
148 const struct sigkeyid *id,
149 struct log_if *log) {
150 struct privcache *st=sst;
154 for (was=0; was<st->used; was++) {
155 if (sigkeyid_equal(id, &st->ents[was].id)) {
156 result = st->ents[was];
161 if (st->used < st->alloc) {
166 if (st->ents[was].sigpriv) {
167 st->ents[was].sigpriv->dispose(st->ents[was].sigpriv->st);
171 COPY_OBJ(result.id, *id);
172 result.sigpriv=uncached_get(st,id,log);
175 memmove(&st->ents[1], &st->ents[0], sizeof(st->ents[0]) * was);
177 return result.sigpriv;
180 static list_t *privcache_apply(closure_t *self, struct cloc loc,
181 dict_t *context, list_t *args)
183 struct privcache *st;
188 st->cl.description="privcache";
189 st->cl.type=CL_PRIVCACHE;
191 st->cl.interface=&st->ops;
193 st->ops.lookup=privcache_lookup;
196 st->used=st->alloc=0;
199 item=list_elem(args,0);
200 if (!item || item->type!=t_dict)
201 cfgfatal(loc,"privcache","parameter must be a dictionary\n");
203 dict=item->data.dict;
205 st->alloc=dict_read_number(dict,"privcache-size",False,"privcache",loc,
207 NEW_ARY(st->ents,st->alloc);
210 int32_t buflen=dict_read_number(dict,"privkey-max",False,"privcache",loc,
211 DEFAULT_MAXPRIV_BYTES);
212 buffer_new(&st->databuf,buflen+1);
214 const char *path=dict_read_string(dict,"privkeys",True,"privcache",loc);
215 pathprefix_template_init(&st->path,path,KEYIDSZ*2);
217 st->defhash=find_cl_if(dict,"hash",CL_HASH,False,"site",loc);
219 return new_closure(&st->cl);
222 static list_t *loadprivate_apply(closure_t *self, struct cloc loc,
223 dict_t *context, list_t *args)
225 CL_GET_STR_ARG(0,algname,"algorithm name");
226 CL_GET_STR_ARG(1,path,"private key path");
228 const struct sigscheme_info *sch=sigscheme_lookup(algname);
229 if (!sch) cfgfatal(algname_i->loc,"load-private",
230 "unknown algorithm `%s'",algname);
232 struct buffer_if databuf;
233 buffer_new(&databuf,DEFAULT_MAXPRIV_BYTES);
234 BUF_ALLOC(&databuf,"load-private data buf");
236 struct hash_if *defhash=
237 find_cl_if(context,"hash",CL_HASH,False,"load-private",loc);
239 struct cfgfile_log log;
240 cfgfile_log_init(&log,loc,"load-private");
242 struct sigprivkey_if *sigpriv;
245 uncached_load_file(sch,path,&databuf,defhash,&sigpriv,&cl,&log.log);
246 if (!ok) cfgfatal(loc,"load-private","private key loading failed");
249 buffer_destroy(&databuf);
250 return new_closure(cl);
253 void privcache_module(dict_t *dict)
255 add_closure(dict,"priv-cache",privcache_apply);
256 add_closure(dict,"load-private",loadprivate_apply);