xref: /openssh-portable/ssh-sk-helper.c (revision 611073fb)
1 /* $OpenBSD: ssh-sk-helper.c,v 1.4 2019/12/13 19:11:14 djm Exp $ */
2 /*
3  * Copyright (c) 2019 Google LLC
4  *
5  * Permission to use, copy, modify, and distribute this software for any
6  * purpose with or without fee is hereby granted, provided that the above
7  * copyright notice and this permission notice appear in all copies.
8  *
9  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16  */
17 
18 /*
19  * This is a tiny program used to isolate the address space used for
20  * security key middleware signing operations from ssh-agent. It is similar
21  * to ssh-pkcs11-helper.c but considerably simpler as the operations for
22  * security keys are stateless.
23  *
24  * Please crank SSH_SK_HELPER_VERSION in sshkey.h for any incompatible
25  * protocol changes.
26  */
27 
28 #include "includes.h"
29 
30 #include <limits.h>
31 #include <stdarg.h>
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <string.h>
35 #include <unistd.h>
36 #include <errno.h>
37 
38 #include "xmalloc.h"
39 #include "log.h"
40 #include "sshkey.h"
41 #include "authfd.h"
42 #include "misc.h"
43 #include "sshbuf.h"
44 #include "msg.h"
45 #include "uidswap.h"
46 #include "sshkey.h"
47 #include "ssherr.h"
48 #include "ssh-sk.h"
49 
50 #ifdef ENABLE_SK
51 extern char *__progname;
52 
53 static struct sshbuf *
54 process_sign(struct sshbuf *req)
55 {
56 	int r = SSH_ERR_INTERNAL_ERROR;
57 	struct sshbuf *resp, *kbuf;
58 	struct sshkey *key;
59 	uint32_t compat;
60 	const u_char *message;
61 	u_char *sig;
62 	size_t msglen, siglen;
63 	char *provider;
64 
65 	if ((r = sshbuf_froms(req, &kbuf)) != 0 ||
66 	    (r = sshbuf_get_cstring(req, &provider, NULL)) != 0 ||
67 	    (r = sshbuf_get_string_direct(req, &message, &msglen)) != 0 ||
68 	    (r = sshbuf_get_cstring(req, NULL, NULL)) != 0 || /* alg */
69 	    (r = sshbuf_get_u32(req, &compat)) != 0)
70 		fatal("%s: buffer error: %s", __progname, ssh_err(r));
71 	if (sshbuf_len(req) != 0)
72 		fatal("%s: trailing data in request", __progname);
73 
74 	if ((r = sshkey_private_deserialize(kbuf, &key)) != 0)
75 		fatal("Unable to parse private key: %s", ssh_err(r));
76 	if (!sshkey_is_sk(key))
77 		fatal("Unsupported key type %s", sshkey_ssh_name(key));
78 
79 	debug("%s: ready to sign with key %s, provider %s: "
80 	    "msg len %zu, compat 0x%lx", __progname, sshkey_type(key),
81 	    provider, msglen, (u_long)compat);
82 
83 	if ((r = sshsk_sign(provider, key, &sig, &siglen,
84 	    message, msglen, compat)) != 0)
85 		fatal("Signing failed: %s", ssh_err(r));
86 
87 	if ((resp = sshbuf_new()) == NULL)
88 		fatal("%s: sshbuf_new failed", __progname);
89 
90 	if ((r = sshbuf_put_string(resp, sig, siglen)) != 0)
91 		fatal("%s: buffer error: %s", __progname, ssh_err(r));
92 
93 	sshbuf_free(kbuf);
94 	free(provider);
95 
96 	return resp;
97 }
98 
99 static struct sshbuf *
100 process_enroll(struct sshbuf *req)
101 {
102 	int r;
103 	u_int type;
104 	char *provider;
105 	char *application;
106 	uint8_t flags;
107 	struct sshbuf *challenge, *attest, *kbuf, *resp;
108 	struct sshkey *key;
109 
110 	if ((resp = sshbuf_new()) == NULL ||
111 	    (attest = sshbuf_new()) == NULL ||
112 	    (kbuf = sshbuf_new()) == NULL)
113 		fatal("%s: sshbuf_new failed", __progname);
114 
115 	if ((r = sshbuf_get_u32(req, &type)) != 0 ||
116 	    (r = sshbuf_get_cstring(req, &provider, NULL)) != 0 ||
117 	    (r = sshbuf_get_cstring(req, &application, NULL)) != 0 ||
118 	    (r = sshbuf_get_u8(req, &flags)) != 0 ||
119 	    (r = sshbuf_froms(req, &challenge)) != 0)
120 		fatal("%s: buffer error: %s", __progname, ssh_err(r));
121 	if (sshbuf_len(req) != 0)
122 		fatal("%s: trailing data in request", __progname);
123 
124 	if (type > INT_MAX)
125 		fatal("%s: bad type %u", __progname, type);
126 	if (sshbuf_len(challenge) == 0) {
127 		sshbuf_free(challenge);
128 		challenge = NULL;
129 	}
130 
131 	if ((r = sshsk_enroll((int)type, provider, application, flags,
132 	    challenge, &key, attest)) != 0)
133 		fatal("%s: sshsk_enroll failed: %s", __progname, ssh_err(r));
134 
135 	if ((r = sshkey_private_serialize(key, kbuf)) != 0)
136 		fatal("%s: serialize private key: %s", __progname, ssh_err(r));
137 	if ((r = sshbuf_put_stringb(resp, kbuf)) != 0 ||
138 	    (r = sshbuf_put_stringb(resp, attest)) != 0)
139 		fatal("%s: buffer error: %s", __progname, ssh_err(r));
140 
141 	sshkey_free(key);
142 	sshbuf_free(kbuf);
143 	sshbuf_free(attest);
144 	sshbuf_free(challenge);
145 	free(provider);
146 	free(application);
147 
148 	return resp;
149 }
150 
151 int
152 main(int argc, char **argv)
153 {
154 	SyslogFacility log_facility = SYSLOG_FACILITY_AUTH;
155 	LogLevel log_level = SYSLOG_LEVEL_ERROR;
156 	int in, out, ch, r, log_stderr = 0;
157 	u_int rtype;
158 	uint8_t version;
159 
160 	sanitise_stdfd();
161 	log_init(__progname, log_level, log_facility, log_stderr);
162 
163 	while ((ch = getopt(argc, argv, "v")) != -1) {
164 		switch (ch) {
165 		case 'v':
166 			log_stderr = 1;
167 			if (log_level == SYSLOG_LEVEL_ERROR)
168 				log_level = SYSLOG_LEVEL_DEBUG1;
169 			else if (log_level < SYSLOG_LEVEL_DEBUG3)
170 				log_level++;
171 			break;
172 		default:
173 			fprintf(stderr, "usage: %s [-v]\n", __progname);
174 			exit(1);
175 		}
176 	}
177 	log_init(__progname, log_level, log_facility, log_stderr);
178 
179 	/*
180 	 * Rearrange our file descriptors a little; we don't trust the
181 	 * providers not to fiddle with stdin/out.
182 	 */
183 	closefrom(STDERR_FILENO + 1);
184 	if ((in = dup(STDIN_FILENO)) == -1 || (out = dup(STDOUT_FILENO)) == -1)
185 		fatal("%s: dup: %s", __progname, strerror(errno));
186 	close(STDIN_FILENO);
187 	close(STDOUT_FILENO);
188 	sanitise_stdfd(); /* resets to /dev/null */
189 
190 	if ((req = sshbuf_new()) == NULL)
191 		fatal("%s: sshbuf_new failed", __progname);
192 	if (ssh_msg_recv(in, req) < 0)
193 		fatal("ssh_msg_recv failed");
194 	close(in);
195 	debug("%s: received message len %zu", __progname, sshbuf_len(req));
196 
197 	if ((r = sshbuf_get_u8(req, &version)) != 0)
198 		fatal("%s: buffer error: %s", __progname, ssh_err(r));
199 	if (version != SSH_SK_HELPER_VERSION) {
200 		fatal("unsupported version: received %d, expected %d",
201 		    version, SSH_SK_HELPER_VERSION);
202 	}
203 
204 	if ((r = sshbuf_get_u32(req, &rtype)) != 0)
205 		fatal("%s: buffer error: %s", __progname, ssh_err(r));
206 
207 	switch (rtype) {
208 	case SSH_SK_HELPER_SIGN:
209 		resp = process_sign(req);
210 		break;
211 	case SSH_SK_HELPER_ENROLL:
212 		resp = process_enroll(req);
213 		break;
214 	default:
215 		fatal("%s: unsupported request type %u", __progname, rtype);
216 	}
217 	sshbuf_free(req);
218 	debug("%s: reply len %zu", __progname, sshbuf_len(resp));
219 
220 	if (ssh_msg_send(out, SSH_SK_HELPER_VERSION, resp) == -1)
221 		fatal("ssh_msg_send failed");
222 	sshbuf_free(resp);
223 	close(out);
224 
225 	return (0);
226 }
227 #else /* ENABLE_SK */
228 #include <stdio.h>
229 
230 int
231 main(int argc, char **argv)
232 {
233 	fprintf(stderr, "ssh-sk-helper: disabled at compile time\n");
234 	return -1;
235 }
236 #endif /* ENABLE_SK */
237