This repository has been archived by the owner on Oct 2, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
openssh-key.c
288 lines (234 loc) · 8.48 KB
/
openssh-key.c
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
/*
* This file is governed by Licenses which are listed in
* the LICENSE file, which shall be included in all copies
* and redistributions of this project.
*/
#include "openssh-key.h"
/* opaque EC_KEY + ECDSA NIDs to make it compile for now .. */
typedef struct EC_KEY EC_KEY;
enum ecdsa_nids { NID_X9_62_prime256v1, NID_secp384r1, NID_secp521r1 };
/* +-------------------+ */
/* | structs and types | */
/* +-------------------+ */
/* openssh key struct */
struct opensshkey {
int type;
/* ecdsa nist curves */
int ecdsa_nid;
EC_KEY *ecdsa_key;
/* ed25519 curve */
unsigned char *ed25519_sk;
unsigned char *ed25519_pk;
};
/* supported key types */
static const struct {
const char *name;
const char *shortname;
int type;
int nid;
int iscert;
} supported_keytypes[] = {
/* name shortname type ecdsa_nid cert */
{ "ssh-ed25519", "ED25519", KEY_ED25519, -1, 0 },
{ "[email protected]", "ED25519-CERT", KEY_ED25519_CERT, -1, 1 },
{ "ecdsa-sha2-nistp256", "ECDSA", KEY_ECDSA, NID_X9_62_prime256v1, 0 },
{ "[email protected]", "ECDSA-CERT", KEY_ECDSA_CERT, NID_X9_62_prime256v1, 1 },
{ "ecdsa-sha2-nistp384", "ECDSA", KEY_ECDSA, NID_secp384r1, 0 },
{ "[email protected]", "ECDSA-CERT", KEY_ECDSA_CERT, NID_secp384r1, 1 },
{ "ecdsa-sha2-nistp521", "ECDSA", KEY_ECDSA, NID_secp521r1, 0 },
{ "[email protected]", "ECDSA-CERT", KEY_ECDSA_CERT, NID_secp521r1, 1 },
{ "unknown", "UNKNOWN", KEY_UNKNOWN, -1, 0 },
};
#define n_supported_keytypes sizeof supported_keytypes / sizeof *supported_keytypes
/* +--------------------------------+ */
/* | newly allocate or cleanly free | */
/* +--------------------------------+ */
/* allocate new*/
struct opensshkey *newopensshkey (int type)
{
struct opensshkey *newkey;
if (type > KEY_UNKNOWN)
return NULL;
if ((newkey = zalloc(sizeof *newkey)) == NULL)
return NULL;
newkey->type = type;
newkey->ecdsa_key = NULL;
newkey->ecdsa_nid = -1;
newkey->ed25519_sk = NULL;
newkey->ed25519_pk = NULL;
return newkey;
}
/* free with explicit zeroing */
void freeopensshkey (struct opensshkey *key)
{
if (key == NULL)
return;
switch (key->type) {
case KEY_ED25519:
case KEY_ED25519_CERT:
/* memzero() pointers and free */
nullpointer(key->ed25519_pk, ED25519_PUBLICKEY_SIZE);
nullpointer(key->ed25519_sk, ED25519_SECRETKEY_SIZE);
break;
case KEY_ECDSA:
case KEY_ECDSA_CERT:
/* requires openssl or libressl ..
if (key->ecdsa_key != NULL)
EC_KEY_free(key->ecdsa_key);
key->ecdsa_key = NULL;
break; */
case KEY_UNKNOWN:
default:
break;
}
/* free struct itself */
nullpointer(key, sizeof *key);
return;
}
/* +--------------------+ */
/* | keytype operations | */
/* +--------------------+ */
/* detect key type from given string */
int opensshkey_detect_type (const unsigned char *name)
{
for (int i = 0; i < n_supported_keytypes; i++) {
/* the supported keytype has invalid names */
if (supported_keytypes[i].name == NULL || supported_keytypes[i].shortname == NULL)
continue;
/* match long name */
if (strcmp(name, supported_keytypes[i].name) == 0 ||
/* or match short name */
strcasecmp(name, supported_keytypes[i].shortname) == 0)
return supported_keytypes[i].type;
}
return KEY_UNKNOWN;
}
int opensshkey_get_type (const struct opensshkey *key)
{
if (key == NULL)
return KEY_UNSPECIFIED;
return key->type;
}
const unsigned char *opensshkey_get_typename (const struct opensshkey *key)
{
if (key == NULL)
return NULL;
for (int i = 0; i < n_supported_keytypes; i++) {
/* the supported keytype has invalid names */
if (supported_keytypes[i].name == NULL || supported_keytypes[i].shortname == NULL)
continue;
/* match keytype */
if (key->type == supported_keytypes[i].type &&
/* and match nid */
key->ecdsa_nid == supported_keytypes[i].nid)
return supported_keytypes[i].name;
}
return NULL;
}
/* +----------------------------+ */
/* | operations on key material | */
/* +----------------------------+ */
/* set pk and sk on ed25519 key */
int opensshkey_set_ed25519_keys (struct opensshkey *key, unsigned char *pk, unsigned char *sk)
{
if (key == NULL)
return ERR_NULLPTR;
/* key must be ed25519 key */
if ( !(key->type == KEY_ED25519 || key->type == KEY_ED25519_CERT) )
return OPENSSH_KEY_INCOMPATIBLE;
/* set new public key */
if (pk != NULL)
key->ed25519_pk = pk;
/* set new privatekey */
if (sk != NULL)
key->ed25519_sk = sk;
return SUCCESS;
}
/* return public and private part of elliptic curve keys */
int opensshkey_save_to_tinyssh (const struct opensshkey *key, const unsigned char *dir)
{
if (key == NULL)
return ERR_NULLPTR;
int e = FAILURE;
/* pointers to data to be written */
unsigned char *seckey = NULL, *pubkey = NULL;
size_t seckey_len = 0, pubkey_len = 0;
/* strings to construct the filenames in */
unsigned char pubkey_file[1024 + 64] = "", seckey_file[1024 + 64] = "";
strncat(pubkey_file, dir, 1023);
strncat(seckey_file, dir, 1023);
/* if last character is not a slash, append it */
if (strncmp(seckey_file + strlen(seckey_file) - 1, "/", 1) != 0) {
strncat(pubkey_file, "/", 1);
strncat(seckey_file, "/", 1);
}
/* decide by key type */
switch (key->type) {
case KEY_ED25519:
case KEY_ED25519_CERT:
/* we need a private key but could use
the embedded public key there. use public
key anyway, as it is easier pointer math */
if (key->ed25519_sk == NULL || key->ed25519_pk == NULL)
cleanreturn(ERR_NULLPTR);
/* set pointers and lengths */
seckey = key->ed25519_sk;
seckey_len = ED25519_SECRETKEY_SIZE;
pubkey = key->ed25519_pk;
pubkey_len = ED25519_PUBLICKEY_SIZE;
/* construct the filenames */
strncat(pubkey_file, ED25519_PUBLIC_TINYSSH_NAME, sizeof ED25519_PUBLIC_TINYSSH_NAME);
strncat(seckey_file, ED25519_SECRET_TINYSSH_NAME, sizeof ED25519_SECRET_TINYSSH_NAME);
break;
case KEY_ECDSA:
case KEY_ECDSA_CERT:
/* not supported yet, requires ssl library */
case KEY_UNKNOWN:
case KEY_UNSPECIFIED:
default:
return OPENSSH_KEY_UNKNOWN_KEYTYPE;
}
/* write keys */
if (seckey != NULL && pubkey != NULL) {
/* secret key */
printf("writing seckey to: %s ...\n", seckey_file);
if ((e = savestring(seckey_file, seckey, seckey_len)) != SUCCESS)
cleanreturn(e);
/* public key */
printf("writing pubkey to: %s ...\n", pubkey_file);
if ((e = savestring(pubkey_file, pubkey, pubkey_len)) != SUCCESS)
cleanreturn(e);
}
/* housekeeping .. */
cleanup:
seckey = NULL;
pubkey = NULL;
return e;
}
/* +-----------+ */
/* | debugging | */
/* +-----------+ */
void opensshkey_dump (const struct opensshkey *key)
{
if (key == NULL) {
printf("Passed key pointer is NULL!\n");
return;
}
printf("Dumping opensshkey ...\n");
switch (key->type) {
case KEY_ECDSA:
case KEY_ECDSA_CERT:
printf("Keytype is: ECDSA (unsupported)\n");
break;
case KEY_ED25519:
case KEY_ED25519_CERT:
printf("Keytype is: ed25519\n");
debugbuf("ed25519 public key", key->ed25519_pk, ED25519_PUBLICKEY_SIZE);
debugbuf("ed25519 secret key", key->ed25519_sk, ED25519_SECRETKEY_SIZE);
break;
case KEY_UNKNOWN:
default:
printf("Keytype is UNKNOWN!\n");
break;
}
}