Annotation of src/usr.bin/ssh/ssh-sk-helper.c, Revision 1.5
1.5 ! djm 1: /* $OpenBSD: ssh-sk-helper.c,v 1.4 2019/12/13 19:11:14 djm Exp $ */
1.1 djm 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
1.4 djm 21: * to ssh-pkcs11-helper.c but considerably simpler as the operations for
22: * security keys are stateless.
1.1 djm 23: *
1.4 djm 24: * Please crank SSH_SK_HELPER_VERSION in sshkey.h for any incompatible
25: * protocol changes.
1.1 djm 26: */
27:
1.4 djm 28: #include <limits.h>
1.1 djm 29: #include <stdarg.h>
30: #include <stdio.h>
31: #include <stdlib.h>
32: #include <string.h>
33: #include <unistd.h>
34: #include <errno.h>
35:
36: #include "xmalloc.h"
37: #include "log.h"
38: #include "sshkey.h"
39: #include "authfd.h"
40: #include "misc.h"
41: #include "sshbuf.h"
42: #include "msg.h"
43: #include "uidswap.h"
44: #include "sshkey.h"
45: #include "ssherr.h"
46: #include "ssh-sk.h"
47:
48: extern char *__progname;
49:
1.4 djm 50: static struct sshbuf *
51: process_sign(struct sshbuf *req)
1.1 djm 52: {
1.4 djm 53: int r = SSH_ERR_INTERNAL_ERROR;
54: struct sshbuf *resp, *kbuf;
1.1 djm 55: struct sshkey *key;
56: uint32_t compat;
57: const u_char *message;
1.4 djm 58: u_char *sig;
1.1 djm 59: size_t msglen, siglen;
60: char *provider;
1.4 djm 61:
62: if ((r = sshbuf_froms(req, &kbuf)) != 0 ||
63: (r = sshbuf_get_cstring(req, &provider, NULL)) != 0 ||
64: (r = sshbuf_get_string_direct(req, &message, &msglen)) != 0 ||
65: (r = sshbuf_get_cstring(req, NULL, NULL)) != 0 || /* alg */
66: (r = sshbuf_get_u32(req, &compat)) != 0)
67: fatal("%s: buffer error: %s", __progname, ssh_err(r));
68: if (sshbuf_len(req) != 0)
69: fatal("%s: trailing data in request", __progname);
70:
71: if ((r = sshkey_private_deserialize(kbuf, &key)) != 0)
72: fatal("Unable to parse private key: %s", ssh_err(r));
73: if (!sshkey_is_sk(key))
74: fatal("Unsupported key type %s", sshkey_ssh_name(key));
75:
76: debug("%s: ready to sign with key %s, provider %s: "
77: "msg len %zu, compat 0x%lx", __progname, sshkey_type(key),
78: provider, msglen, (u_long)compat);
79:
80: if ((r = sshsk_sign(provider, key, &sig, &siglen,
81: message, msglen, compat)) != 0)
82: fatal("Signing failed: %s", ssh_err(r));
83:
84: if ((resp = sshbuf_new()) == NULL)
85: fatal("%s: sshbuf_new failed", __progname);
86:
87: if ((r = sshbuf_put_string(resp, sig, siglen)) != 0)
88: fatal("%s: buffer error: %s", __progname, ssh_err(r));
89:
90: sshbuf_free(kbuf);
91: free(provider);
92:
93: return resp;
94: }
95:
96: static struct sshbuf *
97: process_enroll(struct sshbuf *req)
98: {
99: int r;
100: u_int type;
101: char *provider;
102: char *application;
103: uint8_t flags;
104: struct sshbuf *challenge, *attest, *kbuf, *resp;
105: struct sshkey *key;
106:
107: if ((resp = sshbuf_new()) == NULL ||
108: (attest = sshbuf_new()) == NULL ||
109: (kbuf = sshbuf_new()) == NULL)
110: fatal("%s: sshbuf_new failed", __progname);
111:
112: if ((r = sshbuf_get_u32(req, &type)) != 0 ||
113: (r = sshbuf_get_cstring(req, &provider, NULL)) != 0 ||
114: (r = sshbuf_get_cstring(req, &application, NULL)) != 0 ||
115: (r = sshbuf_get_u8(req, &flags)) != 0 ||
116: (r = sshbuf_froms(req, &challenge)) != 0)
117: fatal("%s: buffer error: %s", __progname, ssh_err(r));
118: if (sshbuf_len(req) != 0)
119: fatal("%s: trailing data in request", __progname);
120:
121: if (type > INT_MAX)
122: fatal("%s: bad type %u", __progname, type);
123: if (sshbuf_len(challenge) == 0) {
124: sshbuf_free(challenge);
125: challenge = NULL;
126: }
127:
128: if ((r = sshsk_enroll((int)type, provider, application, flags,
129: challenge, &key, attest)) != 0)
130: fatal("%s: sshsk_enroll failed: %s", __progname, ssh_err(r));
131:
132: if ((r = sshkey_private_serialize(key, kbuf)) != 0)
133: fatal("%s: serialize private key: %s", __progname, ssh_err(r));
134: if ((r = sshbuf_put_stringb(resp, kbuf)) != 0 ||
135: (r = sshbuf_put_stringb(resp, attest)) != 0)
136: fatal("%s: buffer error: %s", __progname, ssh_err(r));
137:
138: sshkey_free(key);
139: sshbuf_free(kbuf);
140: sshbuf_free(attest);
141: sshbuf_free(challenge);
142: free(provider);
143: free(application);
144:
145: return resp;
146: }
147:
1.5 ! djm 148: static struct sshbuf *
! 149: process_load_resident(struct sshbuf *req)
! 150: {
! 151: int r;
! 152: char *provider, *pin;
! 153: struct sshbuf *kbuf, *resp;
! 154: struct sshkey **keys = NULL;
! 155: size_t nkeys = 0, i;
! 156:
! 157: if ((resp = sshbuf_new()) == NULL ||
! 158: (kbuf = sshbuf_new()) == NULL)
! 159: fatal("%s: sshbuf_new failed", __progname);
! 160:
! 161: if ((r = sshbuf_get_cstring(req, &provider, NULL)) != 0 ||
! 162: (r = sshbuf_get_cstring(req, &pin, NULL)) != 0)
! 163: fatal("%s: buffer error: %s", __progname, ssh_err(r));
! 164: if (sshbuf_len(req) != 0)
! 165: fatal("%s: trailing data in request", __progname);
! 166:
! 167: if ((r = sshsk_load_resident(provider, pin, &keys, &nkeys)) != 0)
! 168: fatal("%s: sshsk_load_resident failed: %s",
! 169: __progname, ssh_err(r));
! 170:
! 171: for (i = 0; i < nkeys; i++) {
! 172: debug("%s: key %zu %s %s", __func__, i,
! 173: sshkey_type(keys[i]), keys[i]->sk_application);
! 174: sshbuf_reset(kbuf);
! 175: if ((r = sshkey_private_serialize(keys[i], kbuf)) != 0)
! 176: fatal("%s: serialize private key: %s",
! 177: __progname, ssh_err(r));
! 178: if ((r = sshbuf_put_stringb(resp, kbuf)) != 0 ||
! 179: (r = sshbuf_put_cstring(resp, "")) != 0) /* comment */
! 180: fatal("%s: buffer error: %s", __progname, ssh_err(r));
! 181: }
! 182:
! 183: for (i = 0; i < nkeys; i++)
! 184: sshkey_free(keys[i]);
! 185: free(keys);
! 186: sshbuf_free(kbuf);
! 187: free(provider);
! 188: freezero(pin, strlen(pin));
! 189: return resp;
! 190: }
! 191:
1.4 djm 192: int
193: main(int argc, char **argv)
194: {
195: SyslogFacility log_facility = SYSLOG_FACILITY_AUTH;
196: LogLevel log_level = SYSLOG_LEVEL_ERROR;
197: struct sshbuf *req, *resp;
1.1 djm 198: int in, out, ch, r, log_stderr = 0;
1.4 djm 199: u_int rtype;
200: uint8_t version;
1.1 djm 201:
202: sanitise_stdfd();
203: log_init(__progname, log_level, log_facility, log_stderr);
204:
205: while ((ch = getopt(argc, argv, "v")) != -1) {
206: switch (ch) {
207: case 'v':
208: log_stderr = 1;
209: if (log_level == SYSLOG_LEVEL_ERROR)
210: log_level = SYSLOG_LEVEL_DEBUG1;
211: else if (log_level < SYSLOG_LEVEL_DEBUG3)
212: log_level++;
213: break;
214: default:
215: fprintf(stderr, "usage: %s [-v]\n", __progname);
216: exit(1);
217: }
218: }
219: log_init(__progname, log_level, log_facility, log_stderr);
220:
221: /*
222: * Rearrange our file descriptors a little; we don't trust the
223: * providers not to fiddle with stdin/out.
224: */
225: closefrom(STDERR_FILENO + 1);
226: if ((in = dup(STDIN_FILENO)) == -1 || (out = dup(STDOUT_FILENO)) == -1)
227: fatal("%s: dup: %s", __progname, strerror(errno));
228: close(STDIN_FILENO);
229: close(STDOUT_FILENO);
230: sanitise_stdfd(); /* resets to /dev/null */
231:
1.4 djm 232: if ((req = sshbuf_new()) == NULL)
1.1 djm 233: fatal("%s: sshbuf_new failed", __progname);
234: if (ssh_msg_recv(in, req) < 0)
235: fatal("ssh_msg_recv failed");
236: close(in);
237: debug("%s: received message len %zu", __progname, sshbuf_len(req));
238:
239: if ((r = sshbuf_get_u8(req, &version)) != 0)
240: fatal("%s: buffer error: %s", __progname, ssh_err(r));
241: if (version != SSH_SK_HELPER_VERSION) {
242: fatal("unsupported version: received %d, expected %d",
243: version, SSH_SK_HELPER_VERSION);
244: }
245:
1.4 djm 246: if ((r = sshbuf_get_u32(req, &rtype)) != 0)
1.1 djm 247: fatal("%s: buffer error: %s", __progname, ssh_err(r));
248:
1.4 djm 249: switch (rtype) {
250: case SSH_SK_HELPER_SIGN:
251: resp = process_sign(req);
252: break;
253: case SSH_SK_HELPER_ENROLL:
254: resp = process_enroll(req);
1.5 ! djm 255: break;
! 256: case SSH_SK_HELPER_LOAD_RESIDENT:
! 257: resp = process_load_resident(req);
1.4 djm 258: break;
259: default:
260: fatal("%s: unsupported request type %u", __progname, rtype);
261: }
262: sshbuf_free(req);
263: debug("%s: reply len %zu", __progname, sshbuf_len(resp));
1.1 djm 264:
265: if (ssh_msg_send(out, SSH_SK_HELPER_VERSION, resp) == -1)
266: fatal("ssh_msg_send failed");
1.4 djm 267: sshbuf_free(resp);
1.1 djm 268: close(out);
269:
270: return (0);
271: }