File: main_tinysshd_printkey.c

package info (click to toggle)
tinyssh 20250501-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,388 kB
  • sloc: ansic: 20,245; sh: 1,582; python: 1,449; makefile: 913
file content (74 lines) | stat: -rw-r--r-- 2,087 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/*
20150124
20241212 - reformated using clang-format
Jan Mojzis
Public domain.
*/

#include <unistd.h>
#include "e.h"
#include "load.h"
#include "sshcrypto.h"
#include "crypto.h"
#include "global.h"
#include "buf.h"
#include "log.h"
#include "writeall.h"
#include "die.h"
#include "main.h"

static struct buf b1 = {global_bspace1, 0,
                        sizeof global_bspace1}; /* reusing global buffer */
static struct buf b2 = {global_bspace2, 0,
                        sizeof global_bspace2}; /* reusing global buffer */
static unsigned char pk[sshcrypto_sign_PUBLICKEYMAX];

#define USAGE "usage: tinysshd-printkey keydir"

int main_tinysshd_printkey(int argc, char **argv) {

    char *x;
    long long i;

    log_init(3, "tinysshd-printkey", 0, 0);

    if (argc < 2) die_usage(USAGE);
    if (!argv[0]) die_usage(USAGE);
    for (;;) {
        if (!argv[1]) break;
        if (argv[1][0] != '-') break;
        x = *++argv;
        if (x[0] == '-' && x[1] == 0) break;
        if (x[0] == '-' && x[1] == '-' && x[2] == 0) break;
        while (*++x) { die_usage(USAGE); }
    }
    x = *++argv;
    if (!x) die_usage(USAGE);

    if (chdir(x) == -1) die_fatal("unable to chdir to directory", x, 0);

    /* read public keys */
    for (i = 0; sshcrypto_keys[i].name; ++i) {
        if (load(sshcrypto_keys[i].sign_publickeyfilename, pk,
                 sshcrypto_keys[i].sign_publickeybytes) == -1) {
            if (errno == ENOENT) continue;
            die_fatal("unable to read public key from file", x,
                      sshcrypto_keys[i].sign_publickeyfilename);
        }

        buf_purge(&b1);
        sshcrypto_keys[i].buf_putsignpk(&b1, pk);
        if (b1.len < 4) die_fatal("unable to get publickey", 0, 0);

        /* base64 */
        buf_purge(&b2);
        buf_puts(&b2, sshcrypto_keys[i].name);
        buf_puts(&b2, " ");
        buf_putbase64(&b2, b1.buf + 4, b1.len - 4);
        buf_puts(&b2, "\n");
        if (writeall(1, b2.buf, b2.len) == -1)
            die_fatal("unable to write output", 0, 0);
    }
    global_die(0);
    return 111;
}