-
Notifications
You must be signed in to change notification settings - Fork 0
/
ssh-sk-helper.c
280 lines (241 loc) · 7.78 KB
/
ssh-sk-helper.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
/* $OpenBSD: ssh-sk-helper.c,v 1.12 2021/10/28 02:54:18 djm Exp $ */
/*
* Copyright (c) 2019 Google LLC
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
/*
* This is a tiny program used to isolate the address space used for
* security key middleware signing operations from ssh-agent. It is similar
* to ssh-pkcs11-helper.c but considerably simpler as the operations for
* security keys are stateless.
*
* Please crank SSH_SK_HELPER_VERSION in sshkey.h for any incompatible
* protocol changes.
*/
#include "includes.h"
#include <limits.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include "xmalloc.h"
#include "log.h"
#include "sshkey.h"
#include "authfd.h"
#include "misc.h"
#include "sshbuf.h"
#include "msg.h"
#include "uidswap.h"
#include "sshkey.h"
#include "ssherr.h"
#include "ssh-sk.h"
#ifdef ENABLE_SK
extern char *blink__progname;
static struct sshbuf *reply_error(int r, char *fmt, ...)
__attribute__((__format__ (printf, 2, 3)));
static struct sshbuf *
reply_error(int r, char *fmt, ...)
{
char *msg;
va_list ap;
struct sshbuf *resp;
va_start(ap, fmt);
xvasprintf(&msg, fmt, ap);
va_end(ap);
debug("%s: %s", blink__progname, msg);
free(msg);
if (r >= 0)
fatal_f("invalid error code %d", r);
if ((resp = sshbuf_new()) == NULL)
fatal("%s: sshbuf_new failed", blink__progname);
if (sshbuf_put_u32(resp, SSH_SK_HELPER_ERROR) != 0 ||
sshbuf_put_u32(resp, (u_int)-r) != 0)
fatal("%s: buffer error", blink__progname);
return resp;
}
/* If the specified string is zero length, then free it and replace with NULL */
static void
null_empty(char **s)
{
if (s == NULL || *s == NULL || **s != '\0')
return;
free(*s);
*s = NULL;
}
static struct sshbuf *
process_sign(struct sshbuf *req)
{
int r = SSH_ERR_INTERNAL_ERROR;
struct sshbuf *resp, *kbuf;
struct sshkey *key = NULL;
uint32_t compat;
const u_char *message;
u_char *sig = NULL;
size_t msglen, siglen = 0;
char *provider = NULL, *pin = NULL;
if ((r = sshbuf_froms(req, &kbuf)) != 0 ||
(r = sshbuf_get_cstring(req, &provider, NULL)) != 0 ||
(r = sshbuf_get_string_direct(req, &message, &msglen)) != 0 ||
(r = sshbuf_get_cstring(req, NULL, NULL)) != 0 || /* alg */
(r = sshbuf_get_u32(req, &compat)) != 0 ||
(r = sshbuf_get_cstring(req, &pin, NULL)) != 0)
fatal_r(r, "%s: parse", blink__progname);
if (sshbuf_len(req) != 0)
fatal("%s: trailing data in request", blink__progname);
if ((r = sshkey_private_deserialize(kbuf, &key)) != 0)
fatal_r(r, "%s: Unable to parse private key", blink__progname);
if (!sshkey_is_sk(key)) {
fatal("%s: Unsupported key type %s",
blink__progname, sshkey_ssh_name(key));
}
debug_f("ready to sign with key %s, provider %s: "
"msg len %zu, compat 0x%lx", sshkey_type(key),
provider, msglen, (u_long)compat);
null_empty(&pin);
if ((r = sshsk_sign(provider, key, &sig, &siglen,
message, msglen, compat, pin)) != 0) {
resp = reply_error(r, "Signing failed: %s", ssh_err(r));
goto out;
}
if ((resp = sshbuf_new()) == NULL)
fatal("%s: sshbuf_new failed", blink__progname);
if ((r = sshbuf_put_u32(resp, SSH_SK_HELPER_SIGN)) != 0 ||
(r = sshbuf_put_string(resp, sig, siglen)) != 0)
fatal_r(r, "%s: compose", blink__progname);
out:
sshkey_free(key);
sshbuf_free(kbuf);
free(provider);
if (sig != NULL)
freezero(sig, siglen);
if (pin != NULL)
freezero(pin, strlen(pin));
return resp;
}
static struct sshbuf *
process_enroll(struct sshbuf *req)
{
int r;
u_int type;
char *provider, *application, *pin, *device, *userid;
uint8_t flags;
struct sshbuf *challenge, *attest, *kbuf, *resp;
struct sshkey *key;
if ((attest = sshbuf_new()) == NULL ||
(kbuf = sshbuf_new()) == NULL)
fatal("%s: sshbuf_new failed", blink__progname);
if ((r = sshbuf_get_u32(req, &type)) != 0 ||
(r = sshbuf_get_cstring(req, &provider, NULL)) != 0 ||
(r = sshbuf_get_cstring(req, &device, NULL)) != 0 ||
(r = sshbuf_get_cstring(req, &application, NULL)) != 0 ||
(r = sshbuf_get_cstring(req, &userid, NULL)) != 0 ||
(r = sshbuf_get_u8(req, &flags)) != 0 ||
(r = sshbuf_get_cstring(req, &pin, NULL)) != 0 ||
(r = sshbuf_froms(req, &challenge)) != 0)
fatal_r(r, "%s: parse", blink__progname);
if (sshbuf_len(req) != 0)
fatal("%s: trailing data in request", blink__progname);
if (type > INT_MAX)
fatal("%s: bad type %u", blink__progname, type);
if (sshbuf_len(challenge) == 0) {
sshbuf_free(challenge);
challenge = NULL;
}
null_empty(&device);
null_empty(&userid);
null_empty(&pin);
if ((r = sshsk_enroll((int)type, provider, device, application, userid,
flags, pin, challenge, &key, attest)) != 0) {
resp = reply_error(r, "Enrollment failed: %s", ssh_err(r));
goto out;
}
if ((resp = sshbuf_new()) == NULL)
fatal("%s: sshbuf_new failed", blink__progname);
if ((r = sshkey_private_serialize(key, kbuf)) != 0)
fatal_r(r, "%s: encode key", blink__progname);
if ((r = sshbuf_put_u32(resp, SSH_SK_HELPER_ENROLL)) != 0 ||
(r = sshbuf_put_stringb(resp, kbuf)) != 0 ||
(r = sshbuf_put_stringb(resp, attest)) != 0)
fatal_r(r, "%s: compose", blink__progname);
out:
sshkey_free(key);
sshbuf_free(kbuf);
sshbuf_free(attest);
sshbuf_free(challenge);
free(provider);
free(application);
if (pin != NULL)
freezero(pin, strlen(pin));
return resp;
}
static struct sshbuf *
process_load_resident(struct sshbuf *req)
{
int r;
char *provider, *pin, *device;
struct sshbuf *kbuf, *resp;
struct sshsk_resident_key **srks = NULL;
size_t nsrks = 0, i;
u_int flags;
if ((kbuf = sshbuf_new()) == NULL)
fatal("%s: sshbuf_new failed", blink__progname);
if ((r = sshbuf_get_cstring(req, &provider, NULL)) != 0 ||
(r = sshbuf_get_cstring(req, &device, NULL)) != 0 ||
(r = sshbuf_get_cstring(req, &pin, NULL)) != 0 ||
(r = sshbuf_get_u32(req, &flags)) != 0)
fatal_r(r, "%s: parse", blink__progname);
if (sshbuf_len(req) != 0)
fatal("%s: trailing data in request", blink__progname);
null_empty(&device);
null_empty(&pin);
if ((r = sshsk_load_resident(provider, device, pin, flags,
&srks, &nsrks)) != 0) {
resp = reply_error(r, "sshsk_load_resident failed: %s",
ssh_err(r));
goto out;
}
if ((resp = sshbuf_new()) == NULL)
fatal("%s: sshbuf_new failed", blink__progname);
if ((r = sshbuf_put_u32(resp, SSH_SK_HELPER_LOAD_RESIDENT)) != 0)
fatal_r(r, "%s: compose", blink__progname);
for (i = 0; i < nsrks; i++) {
debug_f("key %zu %s %s uidlen %zu", i,
sshkey_type(srks[i]->key), srks[i]->key->sk_application,
srks[i]->user_id_len);
sshbuf_reset(kbuf);
if ((r = sshkey_private_serialize(srks[i]->key, kbuf)) != 0)
fatal_r(r, "%s: encode key", blink__progname);
if ((r = sshbuf_put_stringb(resp, kbuf)) != 0 ||
(r = sshbuf_put_cstring(resp, "")) != 0 || /* comment */
(r = sshbuf_put_string(resp, srks[i]->user_id,
srks[i]->user_id_len)) != 0)
fatal_r(r, "%s: compose key", blink__progname);
}
out:
sshsk_free_resident_keys(srks, nsrks);
sshbuf_free(kbuf);
free(provider);
if (pin != NULL)
freezero(pin, strlen(pin));
return resp;
}
int
main(int argc, char **argv)
{
// Blink disabled
return (0);
}
#endif