chiark / gitweb /
rsa: Break out rsa_loadpub_core
authorIan Jackson <ijackson@chiark.greenend.org.uk>
Sat, 23 Nov 2019 13:25:01 +0000 (13:25 +0000)
committerIan Jackson <ijackson@chiark.greenend.org.uk>
Sat, 15 Feb 2020 21:56:50 +0000 (21:56 +0000)
This involves some faff:

We provide macros for helping define and call loadpub_core.
We split RSAPUB_LOADCORE_DEFBN out of RSAPUB_APPLY_GETBN.
RSAPUB_APPLY_GETBN is reworked to do the missing parameter
check first, which lets us introduce e_loc and n_loc.

A side effect is that when we complain about "implausibly large" we
mention the specific rather than general loc.

Signed-off-by: Ian Jackson <ijackson@chiark.greenend.org.uk>
rsa.c

diff --git a/rsa.c b/rsa.c
index 9443115493a18aff2f2dc2812ae3a554741c6243..9261ab30c93bdd42558a27a01796233b416a595f 100644 (file)
--- a/rsa.c
+++ b/rsa.c
@@ -93,6 +93,9 @@ struct rsapriv {
     each(0,e,"public exponent")                        \
     each(1,n,"modulus")
 
+#define RSAPUB_LOADCORE_PASSBN(ix,en,what) \
+    en##s, en##_loc,
+
 struct rsapub {
     closure_t cl;
     struct sigpubkey_if ops;
@@ -309,11 +312,13 @@ static void rsapub_dispose(void *sst) {
     free(st);
 }
 
-static list_t *rsapub_apply(closure_t *self, struct cloc loc, dict_t *context,
-                           list_t *args)
+#define RSAPUB_LOADCORE_DEFBN(ix,en,what) \
+    const char *en##s, struct cloc en##_loc,
+
+static struct rsapub *rsa_loadpub_core(RSAPUB_BNS(RSAPUB_LOADCORE_DEFBN)
+                                      struct cloc overall_loc)
 {
     struct rsapub *st;
-    item_t *i;
 
     NEW(st);
     st->cl.description="rsapub";
@@ -327,29 +332,44 @@ static list_t *rsapub_apply(closure_t *self, struct cloc loc, dict_t *context,
     st->ops.check=rsa_sig_check;
     st->ops.hash=0;
     st->ops.dispose=rsapub_dispose;
-    st->loc=loc;
+    st->loc=overall_loc;
 
-#define RSAPUB_APPLY_GETBN(ix,en,what)                                 \
-    char *en;                                                          \
-    i=list_elem(args,ix);                                              \
-    if (i) {                                                           \
-       if (i->type!=t_string) {                                        \
-           cfgfatal(i->loc,"rsa-public",what " must be a string\n");   \
-       }                                                               \
-       en=i->data.string;                                              \
-       if (mpz_init_set_str(&st->en,en,10)!=0) {                       \
-           cfgfatal(i->loc,"rsa-public", what " \"%s\" is not a "      \
-                    "decimal number string\n",en);                     \
-       }                                                               \
-    } else {                                                           \
-       cfgfatal(loc,"rsa-public","you must provide the " what "\n");   \
+#define RSAPUB_LOADCORE_GETBN(ix,en,what)                              \
+    if (mpz_init_set_str(&st->en,en##s,10)!=0) {                       \
+       cfgfatal(en##_loc,"rsa-public", what " \"%s\" is not a "        \
+                "decimal number string\n",en##s);                      \
     }                                                                  \
     if (mpz_sizeinbase(&st->en, 256) > RSA_MAX_MODBYTES) {             \
-       cfgfatal(loc, "rsa-public", "implausibly large " what "\n");    \
+       cfgfatal(en##_loc, "rsa-public", "implausibly large " what "\n"); \
     }
 
+    RSAPUB_BNS(RSAPUB_LOADCORE_GETBN)
+
+    return st;
+}
+
+static list_t *rsapub_apply(closure_t *self, struct cloc loc, dict_t *context,
+                           list_t *args)
+{
+
+#define RSAPUB_APPLY_GETBN(ix,en,what)                         \
+    item_t *en##i;                                             \
+    const char *en##s;                                         \
+    en##i=list_elem(args,ix);                                  \
+    if (!en##i)                                                        \
+        cfgfatal(loc,"rsa-public",                             \
+                 "you must provide an encryption key\n");      \
+    struct cloc en##_loc=en##i->loc;                           \
+    if (en##i->type!=t_string)                                 \
+       cfgfatal(en##_loc,"rsa-public",                         \
+                "first argument must be a string\n");          \
+    en##s=en##i->data.string;
+
     RSAPUB_BNS(RSAPUB_APPLY_GETBN)
 
+    struct rsapub *st=rsa_loadpub_core(RSAPUB_BNS(RSAPUB_LOADCORE_PASSBN)
+                                      loc);
+
     return new_closure(&st->cl);
 }