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 <limits.h> 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 50 static struct sshbuf * 51 process_sign(struct sshbuf *req) 52 { 53 int r = SSH_ERR_INTERNAL_ERROR; 54 struct sshbuf *resp, *kbuf; 55 struct sshkey *key; 56 uint32_t compat; 57 const u_char *message; 58 u_char *sig; 59 size_t msglen, siglen; 60 char *provider; 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 148 int 149 main(int argc, char **argv) 150 { 151 SyslogFacility log_facility = SYSLOG_FACILITY_AUTH; 152 LogLevel log_level = SYSLOG_LEVEL_ERROR; 153 struct sshbuf *req, *resp; 154 int in, out, ch, r, log_stderr = 0; 155 u_int rtype; 156 uint8_t version; 157 158 sanitise_stdfd(); 159 log_init(__progname, log_level, log_facility, log_stderr); 160 161 while ((ch = getopt(argc, argv, "v")) != -1) { 162 switch (ch) { 163 case 'v': 164 log_stderr = 1; 165 if (log_level == SYSLOG_LEVEL_ERROR) 166 log_level = SYSLOG_LEVEL_DEBUG1; 167 else if (log_level < SYSLOG_LEVEL_DEBUG3) 168 log_level++; 169 break; 170 default: 171 fprintf(stderr, "usage: %s [-v]\n", __progname); 172 exit(1); 173 } 174 } 175 log_init(__progname, log_level, log_facility, log_stderr); 176 177 /* 178 * Rearrange our file descriptors a little; we don't trust the 179 * providers not to fiddle with stdin/out. 180 */ 181 closefrom(STDERR_FILENO + 1); 182 if ((in = dup(STDIN_FILENO)) == -1 || (out = dup(STDOUT_FILENO)) == -1) 183 fatal("%s: dup: %s", __progname, strerror(errno)); 184 close(STDIN_FILENO); 185 close(STDOUT_FILENO); 186 sanitise_stdfd(); /* resets to /dev/null */ 187 188 if ((req = sshbuf_new()) == NULL) 189 fatal("%s: sshbuf_new failed", __progname); 190 if (ssh_msg_recv(in, req) < 0) 191 fatal("ssh_msg_recv failed"); 192 close(in); 193 debug("%s: received message len %zu", __progname, sshbuf_len(req)); 194 195 if ((r = sshbuf_get_u8(req, &version)) != 0) 196 fatal("%s: buffer error: %s", __progname, ssh_err(r)); 197 if (version != SSH_SK_HELPER_VERSION) { 198 fatal("unsupported version: received %d, expected %d", 199 version, SSH_SK_HELPER_VERSION); 200 } 201 202 if ((r = sshbuf_get_u32(req, &rtype)) != 0) 203 fatal("%s: buffer error: %s", __progname, ssh_err(r)); 204 205 switch (rtype) { 206 case SSH_SK_HELPER_SIGN: 207 resp = process_sign(req); 208 break; 209 case SSH_SK_HELPER_ENROLL: 210 resp = process_enroll(req); 211 break; 212 default: 213 fatal("%s: unsupported request type %u", __progname, rtype); 214 } 215 sshbuf_free(req); 216 debug("%s: reply len %zu", __progname, sshbuf_len(resp)); 217 218 if (ssh_msg_send(out, SSH_SK_HELPER_VERSION, resp) == -1) 219 fatal("ssh_msg_send failed"); 220 sshbuf_free(resp); 221 close(out); 222 223 return (0); 224 } 225