File: sshcrypto_key.c

package info (click to toggle)
tinyssh 20250501-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,388 kB
  • sloc: ansic: 20,245; sh: 1,582; python: 1,449; makefile: 913
file content (158 lines) | stat: -rw-r--r-- 4,357 bytes parent folder | download
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/*
20140204
20241210 - reformated using clang-format
Jan Mojzis
Public domain.
*/

#include "crypto.h"
#include "packetparser.h"
#include "stringparser.h"
#include "str.h"
#include "byte.h"
#include "e.h"
#include "log.h"
#include "bug.h"
#include "purge.h"
#include "sshcrypto.h"

struct sshcrypto_key sshcrypto_keys[] = {
    {
        "ssh-ed25519",
        crypto_sign_ed25519,
        crypto_sign_ed25519_open,
        crypto_sign_ed25519_keypair,
        {0},
        crypto_sign_ed25519_PUBLICKEYBYTES,
        crypto_sign_ed25519_SECRETKEYBYTES,
        crypto_sign_ed25519_BYTES,
        "ed25519.pk",
        ".ed25519.sk",
        sshcrypto_TYPENEWCRYPTO,
        0,
        0,
        ed25519_putsignature,
        ed25519_putsignpk,
        ed25519_putsignpkbase64,
        ed25519_parsesignature,
        ed25519_parsesignpk,
    },
#if 0
    {   "pqkeyTODO",
        crypto_sign_ed25519,
        crypto_sign_ed25519_open,
        crypto_sign_ed25519_keypair,
        {0},
        crypto_sign_ed25519_PUBLICKEYBYTES,
        crypto_sign_ed25519_SECRETKEYBYTES,
        crypto_sign_ed25519_BYTES,
        "pqkeyTODO.pk",
        ".pqkeyTODO.sk",
        sshcrypto_TYPEPQCRYPTO,
        0,
        0,
        ed25519_putsignature,
        ed25519_putsignpk,
        ed25519_putsignpkbase64,
        ed25519_parsesignature,
        ed25519_parsesignpk,
    },
#endif
    {
        0,
        0,
        0,
        0,
        {0},
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
    },
};

const char *sshcrypto_key_name = 0;
int (*sshcrypto_sign)(unsigned char *, unsigned long long *,
                      const unsigned char *, unsigned long long,
                      const unsigned char *) = 0;
unsigned char *sshcrypto_sign_publickey = 0;
long long sshcrypto_sign_publickeybytes = 0;
long long sshcrypto_sign_secretkeybytes = 0;
long long sshcrypto_sign_bytes = 0;
const char *sshcrypto_sign_secretkeyfilename = 0;
void (*sshcrypto_buf_putsignature)(struct buf *, const unsigned char *) = 0;
void (*sshcrypto_buf_putsignpk)(struct buf *, const unsigned char *) = 0;

int sshcrypto_key_select(const unsigned char *buf, long long len) {

    long long i, pos = 0;
    const unsigned char *x;
    long long xlen;

    if (sshcrypto_key_name) return 1;

    if (buf[len] != 0) bug_proto();
    log_d2("kex: client: key algorithms: ", (const char *) buf);

    for (;;) {
        pos = stringparser(buf, len, pos, &x, &xlen);
        if (!pos) break;

        for (i = 0; sshcrypto_keys[i].name; ++i) {
            if (!sshcrypto_keys[i].sign_flagserver) continue;
            if (str_equaln((const char *) x, xlen, sshcrypto_keys[i].name)) {
                sshcrypto_key_name = sshcrypto_keys[i].name;
                sshcrypto_sign = sshcrypto_keys[i].sign;
                sshcrypto_sign_publickey = sshcrypto_keys[i].sign_publickey;
                sshcrypto_sign_publickeybytes =
                    sshcrypto_keys[i].sign_publickeybytes;
                sshcrypto_sign_secretkeybytes =
                    sshcrypto_keys[i].sign_secretkeybytes;
                sshcrypto_sign_bytes = sshcrypto_keys[i].sign_bytes;
                sshcrypto_sign_secretkeyfilename =
                    sshcrypto_keys[i].sign_secretkeyfilename;
                sshcrypto_buf_putsignature = sshcrypto_keys[i].buf_putsignature;
                sshcrypto_buf_putsignpk = sshcrypto_keys[i].buf_putsignpk;
                log_d2("kex: key selected: ", sshcrypto_keys[i].name);
                return 1;
            }
        }
    }
    log_d2("kex: key not available ", (const char *) buf);
    errno = EPROTO;
    return 0;
}

void sshcrypto_key_put(struct buf *b) {

    crypto_uint32 len = 0;
    long long i, j, start;

    j = 0;
    for (i = 0; sshcrypto_keys[i].name; ++i) {
        if (!sshcrypto_keys[i].sign_flagserver) continue;
        if (j++) ++len;
        len += str_len(sshcrypto_keys[i].name);
    }

    buf_putnum32(b, len);
    start = b->len;

    j = 0;
    for (i = 0; sshcrypto_keys[i].name; ++i) {
        if (!sshcrypto_keys[i].sign_flagserver) continue;
        if (j++) buf_puts(b, ",");
        buf_puts(b, sshcrypto_keys[i].name);
    }
    b->buf[b->len] = 0;
    log_d2("kex: server: key algorithms: ", (char *) b->buf + start);
}