chiark / gitweb /
sig: Move unmarshalling responsibility into algorithm
[secnet.git] / rsa.c
diff --git a/rsa.c b/rsa.c
index 068cf41dfc13dfb94a35ec1656799ce0cdbd96c4..49672c2511bb26a5ed38dfb841ba4b4f1ab0b755 100644 (file)
--- a/rsa.c
+++ b/rsa.c
@@ -35,6 +35,7 @@
 #include <gmp.h>
 #include "secnet.h"
 #include "util.h"
+#include "unaligned.h"
 
 #define AUTHFILE_ID_STRING "SSH PRIVATE KEY FILE FORMAT 1.1\n"
 
@@ -42,7 +43,7 @@
 
 struct rsapriv {
     closure_t cl;
-    struct rsaprivkey_if ops;
+    struct sigprivkey_if ops;
     struct cloc loc;
     MP_INT n;
     MP_INT p, dp;
@@ -51,7 +52,7 @@ struct rsapriv {
 };
 struct rsapub {
     closure_t cl;
-    struct rsapubkey_if ops;
+    struct sigpubkey_if ops;
     struct cloc loc;
     MP_INT e;
     MP_INT n;
@@ -114,11 +115,13 @@ static void emsa_pkcs1(MP_INT *n, MP_INT *m,
     mpz_set_str(m, buff, 16);
 }
 
-static string_t rsa_sign(void *sst, uint8_t *data, int32_t datalen)
+static bool_t rsa_sign(void *sst, uint8_t *data, int32_t datalen,
+                      struct buffer_if *msg)
 {
     struct rsapriv *st=sst;
     MP_INT a, b, u, v, tmp, tmp2;
-    string_t signature;
+    string_t signature = 0;
+    bool_t ok;
 
     mpz_init(&a);
     mpz_init(&b);
@@ -162,14 +165,45 @@ static string_t rsa_sign(void *sst, uint8_t *data, int32_t datalen)
 
     signature=write_mpstring(&b);
 
+    uint8_t *op = buf_append(msg,2);
+    if (!op) { ok=False; goto out; }
+    size_t l = strlen(signature);
+    assert(l < 65536);
+    put_uint16(op, l);
+    op = buf_append(msg,l);
+    if (!op) { ok=False; goto out; }
+    memcpy(op, signature, l);
+
+    ok = True;
+
+ out:
+    free(signature);
     mpz_clear(&b);
     mpz_clear(&a);
-    return signature;
+    return ok;
+}
+
+static bool_t rsa_sig_unpick(void *sst, struct buffer_if *msg,
+                            struct alg_msg_data *sig)
+{
+    uint8_t *lp = buf_unprepend(msg, 2);
+    if (!lp) return False;
+    sig->siglen = get_uint16(lp);
+    sig->sigstart = buf_unprepend(msg, sig->siglen);
+    if (!sig->sigstart) return False;
+
+    /* In `rsa_sig_check' below, we assume that we can write a nul
+     * terminator following the signature.  Make sure there's enough space.
+     */
+    if (msg->start >= msg->base + msg->alloclen)
+       return False;
+
+    return True;
 }
 
-static rsa_checksig_fn rsa_sig_check;
+static sig_checksig_fn rsa_sig_check;
 static bool_t rsa_sig_check(void *sst, uint8_t *data, int32_t datalen,
-                           cstring_t signature)
+                           const struct alg_msg_data *sig)
 {
     struct rsapub *st=sst;
     MP_INT a, b, c;
@@ -181,7 +215,11 @@ static bool_t rsa_sig_check(void *sst, uint8_t *data, int32_t datalen,
 
     emsa_pkcs1(&st->n, &a, data, datalen);
 
-    mpz_set_str(&b, signature, 16);
+    /* Terminate signature with a '0' - already checked that this will fit */
+    int save = sig->sigstart[sig->siglen];
+    sig->sigstart[sig->siglen] = 0;
+    mpz_set_str(&b, sig->sigstart, 16);
+    sig->sigstart[sig->siglen] = save;
 
     mpz_powm(&c, &b, &st->e, &st->n);
 
@@ -203,10 +241,11 @@ static list_t *rsapub_apply(closure_t *self, struct cloc loc, dict_t *context,
 
     NEW(st);
     st->cl.description="rsapub";
-    st->cl.type=CL_RSAPUBKEY;
+    st->cl.type=CL_SIGPUBKEY;
     st->cl.apply=NULL;
     st->cl.interface=&st->ops;
     st->ops.st=st;
+    st->ops.unpick=rsa_sig_unpick;
     st->ops.check=rsa_sig_check;
     st->loc=loc;
 
@@ -281,7 +320,7 @@ static list_t *rsapriv_apply(closure_t *self, struct cloc loc, dict_t *context,
 
     NEW(st);
     st->cl.description="rsapriv";
-    st->cl.type=CL_RSAPRIVKEY;
+    st->cl.type=CL_SIGPRIVKEY;
     st->cl.apply=NULL;
     st->cl.interface=&st->ops;
     st->ops.st=st;
@@ -292,7 +331,7 @@ static list_t *rsapriv_apply(closure_t *self, struct cloc loc, dict_t *context,
     i=list_elem(args,0);
     if (i) {
        if (i->type!=t_string) {
-           cfgfatal(i->loc,"rsa-public","first argument must be a string\n");
+           cfgfatal(i->loc,"rsa-private","first argument must be a string\n");
        }
        filename=i->data.string;
     } else {