chiark / gitweb /
privcache; uncached_load_file: Introduce error_out
[secnet.git] / privcache.c
index 81b04fcc65f456e3585001fd7425236b37d66ba5..c01af9dbc412cbc218256ac01e3919fb8066a1a5 100644 (file)
@@ -37,71 +37,101 @@ struct privcache {
     struct hash_if *defhash;
 };
 
+static struct sigprivkey_if *uncached_load_file(
+                          const struct sigscheme_info *scheme,
+                          const char *path,
+                          struct buffer_if *databuf,
+                          struct hash_if *defhash,
+                          struct log_if *log);
+
 static struct sigprivkey_if *uncached_get(struct privcache *st,
                           const struct sigkeyid *id, struct log_if *log)
+{
+    sprintf(st->path.write_here, SIGKEYID_PR_FMT, SIGKEYID_PR_VAL(id));
+
+    const char *path=st->path.buffer;
+    const struct sigscheme_info *scheme;
+    for (scheme=sigschemes;
+        scheme->name;
+        scheme++)
+       if (scheme->algid == id->b[GRPIDSZ])
+           goto found;
+
+    slilog(log,M_ERR,"private key file %s not loaded (unknown algid)",
+          path);
+    return 0;
+
+ found:
+    return uncached_load_file(scheme,
+                             path,
+                             &st->databuf,
+                             st->defhash,
+                             log);
+}
+
+static struct sigprivkey_if *uncached_load_file(
+                          const struct sigscheme_info *scheme,
+                          const char *path,
+                          struct buffer_if *databuf,
+                          struct hash_if *defhash,
+                          struct log_if *log)
 {
     bool_t ok=False;
     FILE *f=0;
+    struct sigprivkey_if *sigpriv=0;
 
-    sprintf(st->path.write_here, SIGKEYID_PR_FMT, SIGKEYID_PR_VAL(id));
-
-    f = fopen(st->path.buffer,"rb");
+    f = fopen(path,"rb");
     if (!f) {
        if (errno == ENOENT) {
            slilog(log,M_DEBUG,"private key %s not found",
-                  st->path.buffer);
+                  path);
        } else {
            slilog(log,M_ERR,"failed to open private key file %s",
-                  st->path.buffer);
+                  path);
        }
-       goto out;
+       goto error_out;
     }
 
     setbuf(f,0);
-    buffer_init(&st->databuf,0);
-    ssize_t got=fread(st->databuf.base,1,st->databuf.alloclen,f);
+    buffer_init(databuf,0);
+    ssize_t got=fread(databuf->base,1,databuf->alloclen,f);
     if (ferror(f)) {
        slilog(log,M_ERR,"failed to read private-key file %s",
-              st->path.buffer);
-       goto out;
+              path);
+       goto error_out;
     }
     if (!feof(f)) {
        slilog(log,M_ERR,"private key file %s longer than max %d",
-              st->path.buffer, (int)st->databuf.alloclen);
-       goto out;
+              path, (int)databuf->alloclen);
+       goto error_out;
     }
     fclose(f); f=0;
 
-    struct sigprivkey_if *sigpriv=0;
-    for (const struct sigscheme_info *scheme=sigschemes;
-        scheme->name;
-        scheme++) {
-       st->databuf.start=st->databuf.base;
-       st->databuf.size=got;
-       struct cloc loc = { .file=st->path.buffer, .line=0 };
-       ok=scheme->loadpriv(scheme, &st->databuf, &sigpriv, log, loc);
-       if (ok) {
-           if (sigpriv->sethash) {
-               if (!st->defhash) {
-                   slilog(log,M_ERR,
+    databuf->start=databuf->base;
+    databuf->size=got;
+    struct cloc loc = { .file=path, .line=0 };
+    ok=scheme->loadpriv(scheme, databuf, &sigpriv, log, loc);
+    if (!ok) goto error_out; /* loadpriv will have logged */
+
+    if (sigpriv->sethash) {
+       if (!defhash) {
+           slilog(log,M_ERR,
  "private key %s requires `hash' config key for privcache to load",
-                          st->path.buffer);
-                   sigpriv->dispose(sigpriv->st);
-                   sigpriv=0;
-                   goto out;
-               }
-               sigpriv->sethash(sigpriv->st,st->defhash);
-           }
-           goto out;
+                  path);
+           sigpriv->dispose(sigpriv->st);
+           sigpriv=0;
+           goto error_out;
        }
+       sigpriv->sethash(sigpriv->st,defhash);
     }
 
-    slilog(log,M_ERR,"private key file %s not loaded (not recognised?)",
-          st->path.buffer);
-
   out:
     if (f) fclose(f);
     return ok ? sigpriv : 0;
+
+ error_out:
+    ok=False;
+    goto out;
 }
 
 static struct sigprivkey_if *privcache_lookup(void *sst,