aruu

[MIRROR] Actual replacement for Busybox | suckless
git clone https://git.daat.foo/aruu.git
Log | Files | Refs | README | LICENSE

signify.c (3904B)


      1 /* see LICENSE file for copyright and license details */
      2 // ?man signify: make keys, sign and verify files
      3 // ?man arguments: -G [-n] [-c comment] -p pubkey -s seckey | -S -s seckey -m message [-x sigfile] | -V [-q] -p pubkey -m message [-x sigfile]
      4 // ?man signify signs a file with an ed25519 key and checks such a signature,
      5 // ?man in the files openbsd's signify reads and writes. a secret key is kept
      6 // ?man without a passphrase. the signature of message is message.sig
      7 // ?man unless another file is named
      8 #include "arg.h"
      9 #include "signify.h"
     10 #include "util.h"
     11 
     12 #include <stdio.h>
     13 #include <stdlib.h>
     14 #include <string.h>
     15 
     16 static void
     17 usage(void)
     18 {
     19   fprintf(
     20       stderr,
     21       "usage: %s -G [-n] [-c comment] -p pubkey -s seckey\n"
     22       "       %s -S -s seckey -m message [-x sigfile]\n"
     23       "       %s -V [-q] -p pubkey -m message [-x sigfile]\n",
     24       argv0,
     25       argv0,
     26       argv0
     27   );
     28   exit(1);
     29 }
     30 
     31 static uint8_t *
     32 slurp(const char *path, size_t *n)
     33 {
     34   FILE    *fp  = fopen(path, "rb");
     35   uint8_t *buf = NULL;
     36   size_t   cap = 0, r;
     37 
     38   if (!fp)
     39     eprintf("open %s:", path);
     40   *n = 0;
     41   do {
     42     if (*n == cap)
     43       buf = erealloc(buf, cap = cap ? cap * 2 : 65536);
     44     r = fread(buf + *n, 1, cap - *n, fp);
     45     *n += r;
     46   } while (r > 0);
     47   if (ferror(fp))
     48     eprintf("read %s:", path);
     49   fclose(fp);
     50   return buf;
     51 }
     52 
     53 int
     54 main(int argc, char *argv[])
     55 {
     56   const char *pub = NULL, *sec = NULL, *msg = NULL, *sig = NULL, *comment = "signify";
     57   char        sigbuf[4096];
     58   int         mode = 0, quiet = 0;
     59   uint8_t    *m;
     60   size_t      n;
     61 
     62   ARGBEGIN
     63   {
     64     // ?man -G: make a key pair
     65     case 'G':
     66     // ?man -S: sign message
     67     case 'S':
     68     // ?man -V: verify message
     69     case 'V':
     70       if (mode)
     71         usage();
     72       mode = ARGC();
     73       break;
     74     // ?man -c:str: -c comment: the comment of a new key pair
     75     case 'c':
     76       comment = EARGF(usage());
     77       break;
     78     // ?man -m:str: -m message: the file signed or checked
     79     case 'm':
     80       msg = EARGF(usage());
     81       break;
     82     // ?man -n: a key without a passphrase, which is the only kind made here
     83     case 'n':
     84       break;
     85     // ?man -p:str: -p pubkey: the public key
     86     case 'p':
     87       pub = EARGF(usage());
     88       break;
     89     // ?man -q: say nothing when the signature matches
     90     case 'q':
     91       quiet = 1;
     92       break;
     93     // ?man -s:str: -s seckey: the secret key
     94     case 's':
     95       sec = EARGF(usage());
     96       break;
     97     // ?man -x:str: -x sigfile: the signature, message.sig when not given
     98     case 'x':
     99       sig = EARGF(usage());
    100       break;
    101     default:
    102       usage();
    103   }
    104   ARGEND
    105 
    106   if (argc)
    107     usage();
    108   if (mode != 'G' && msg && !sig) {
    109     snprintf(sigbuf, sizeof(sigbuf), "%s.sig", msg);
    110     sig = sigbuf;
    111   }
    112   switch (mode) {
    113     case 'G':
    114       if (!pub || !sec)
    115         usage();
    116       return signify_generate(pub, sec, comment) != 0;
    117     case 'S': {
    118       struct signify_sec k;
    119       const char        *base;
    120       char               pubname[1024];
    121       size_t             l;
    122 
    123       if (!sec || !msg)
    124         usage();
    125       if (signify_read_sec(sec, &k))
    126         return 1;
    127       // the comment names the public key the way signify does: the secret
    128       // key's name with .pub for .sec
    129       base = strrchr(sec, '/');
    130       base = base ? base + 1 : sec;
    131       l    = strlen(base);
    132       if (l > 4 && !strcmp(base + l - 4, ".sec"))
    133         snprintf(pubname, sizeof(pubname), "%.*s.pub", (int)(l - 4), base);
    134       else
    135         snprintf(pubname, sizeof(pubname), "%s", base);
    136       m = slurp(msg, &n);
    137       return signify_sign(&k, m, n, pubname, sig) != 0;
    138     }
    139     case 'V': {
    140       struct signify_pub k;
    141 
    142       if (!pub || !msg)
    143         usage();
    144       if (signify_read_pub(pub, &k))
    145         return 1;
    146       m = slurp(msg, &n);
    147       if (signify_verify(&k, m, n, sig))
    148         return 1;
    149       if (!quiet)
    150         puts("Signature Verified");
    151       return 0;
    152     }
    153   }
    154   usage();
    155 }