1 /* $OpenBSD: ssh-sk-helper.c,v 1.10 2020/05/26 01:59:46 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 *reply_error(int r, char *fmt, ...) 51 __attribute__((__format__ (printf, 2, 3))); 52 53 static struct sshbuf * 54 reply_error(int r, char *fmt, ...) 55 { 56 char *msg; 57 va_list ap; 58 struct sshbuf *resp; 59 60 va_start(ap, fmt); 61 xvasprintf(&msg, fmt, ap); 62 va_end(ap); 63 debug("%s: %s", __progname, msg); 64 free(msg); 65 66 if (r >= 0) 67 fatal("%s: invalid error code %d", __func__, r); 68 69 if ((resp = sshbuf_new()) == NULL) 70 fatal("%s: sshbuf_new failed", __progname); 71 if (sshbuf_put_u32(resp, SSH_SK_HELPER_ERROR) != 0 || 72 sshbuf_put_u32(resp, (u_int)-r) != 0) 73 fatal("%s: buffer error", __progname); 74 return resp; 75 } 76 77 /* If the specified string is zero length, then free it and replace with NULL */ 78 static void 79 null_empty(char **s) 80 { 81 if (s == NULL || *s == NULL || **s != '\0') 82 return; 83 84 free(*s); 85 *s = NULL; 86 } 87 88 static struct sshbuf * 89 process_sign(struct sshbuf *req) 90 { 91 int r = SSH_ERR_INTERNAL_ERROR; 92 struct sshbuf *resp, *kbuf; 93 struct sshkey *key = NULL; 94 uint32_t compat; 95 const u_char *message; 96 u_char *sig = NULL; 97 size_t msglen, siglen = 0; 98 char *provider = NULL, *pin = NULL; 99 100 if ((r = sshbuf_froms(req, &kbuf)) != 0 || 101 (r = sshbuf_get_cstring(req, &provider, NULL)) != 0 || 102 (r = sshbuf_get_string_direct(req, &message, &msglen)) != 0 || 103 (r = sshbuf_get_cstring(req, NULL, NULL)) != 0 || /* alg */ 104 (r = sshbuf_get_u32(req, &compat)) != 0 || 105 (r = sshbuf_get_cstring(req, &pin, NULL)) != 0) 106 fatal("%s: buffer error: %s", __progname, ssh_err(r)); 107 if (sshbuf_len(req) != 0) 108 fatal("%s: trailing data in request", __progname); 109 110 if ((r = sshkey_private_deserialize(kbuf, &key)) != 0) 111 fatal("Unable to parse private key: %s", ssh_err(r)); 112 if (!sshkey_is_sk(key)) 113 fatal("Unsupported key type %s", sshkey_ssh_name(key)); 114 115 debug("%s: ready to sign with key %s, provider %s: " 116 "msg len %zu, compat 0x%lx", __progname, sshkey_type(key), 117 provider, msglen, (u_long)compat); 118 119 null_empty(&pin); 120 121 if ((r = sshsk_sign(provider, key, &sig, &siglen, 122 message, msglen, compat, pin)) != 0) { 123 resp = reply_error(r, "Signing failed: %s", ssh_err(r)); 124 goto out; 125 } 126 127 if ((resp = sshbuf_new()) == NULL) 128 fatal("%s: sshbuf_new failed", __progname); 129 130 if ((r = sshbuf_put_u32(resp, SSH_SK_HELPER_SIGN)) != 0 || 131 (r = sshbuf_put_string(resp, sig, siglen)) != 0) 132 fatal("%s: buffer error: %s", __progname, ssh_err(r)); 133 out: 134 sshkey_free(key); 135 sshbuf_free(kbuf); 136 free(provider); 137 if (sig != NULL) 138 freezero(sig, siglen); 139 if (pin != NULL) 140 freezero(pin, strlen(pin)); 141 return resp; 142 } 143 144 static struct sshbuf * 145 process_enroll(struct sshbuf *req) 146 { 147 int r; 148 u_int type; 149 char *provider, *application, *pin, *device, *userid; 150 uint8_t flags; 151 struct sshbuf *challenge, *attest, *kbuf, *resp; 152 struct sshkey *key; 153 154 if ((attest = sshbuf_new()) == NULL || 155 (kbuf = sshbuf_new()) == NULL) 156 fatal("%s: sshbuf_new failed", __progname); 157 158 if ((r = sshbuf_get_u32(req, &type)) != 0 || 159 (r = sshbuf_get_cstring(req, &provider, NULL)) != 0 || 160 (r = sshbuf_get_cstring(req, &device, NULL)) != 0 || 161 (r = sshbuf_get_cstring(req, &application, NULL)) != 0 || 162 (r = sshbuf_get_cstring(req, &userid, NULL)) != 0 || 163 (r = sshbuf_get_u8(req, &flags)) != 0 || 164 (r = sshbuf_get_cstring(req, &pin, NULL)) != 0 || 165 (r = sshbuf_froms(req, &challenge)) != 0) 166 fatal("%s: buffer error: %s", __progname, ssh_err(r)); 167 if (sshbuf_len(req) != 0) 168 fatal("%s: trailing data in request", __progname); 169 170 if (type > INT_MAX) 171 fatal("%s: bad type %u", __progname, type); 172 if (sshbuf_len(challenge) == 0) { 173 sshbuf_free(challenge); 174 challenge = NULL; 175 } 176 null_empty(&device); 177 null_empty(&userid); 178 null_empty(&pin); 179 180 if ((r = sshsk_enroll((int)type, provider, device, application, userid, 181 flags, pin, challenge, &key, attest)) != 0) { 182 resp = reply_error(r, "Enrollment failed: %s", ssh_err(r)); 183 goto out; 184 } 185 186 if ((resp = sshbuf_new()) == NULL) 187 fatal("%s: sshbuf_new failed", __progname); 188 if ((r = sshkey_private_serialize(key, kbuf)) != 0) 189 fatal("%s: serialize private key: %s", __progname, ssh_err(r)); 190 if ((r = sshbuf_put_u32(resp, SSH_SK_HELPER_ENROLL)) != 0 || 191 (r = sshbuf_put_stringb(resp, kbuf)) != 0 || 192 (r = sshbuf_put_stringb(resp, attest)) != 0) 193 fatal("%s: buffer error: %s", __progname, ssh_err(r)); 194 195 out: 196 sshkey_free(key); 197 sshbuf_free(kbuf); 198 sshbuf_free(attest); 199 sshbuf_free(challenge); 200 free(provider); 201 free(application); 202 if (pin != NULL) 203 freezero(pin, strlen(pin)); 204 205 return resp; 206 } 207 208 static struct sshbuf * 209 process_load_resident(struct sshbuf *req) 210 { 211 int r; 212 char *provider, *pin, *device; 213 struct sshbuf *kbuf, *resp; 214 struct sshkey **keys = NULL; 215 size_t nkeys = 0, i; 216 217 if ((kbuf = sshbuf_new()) == NULL) 218 fatal("%s: sshbuf_new failed", __progname); 219 220 if ((r = sshbuf_get_cstring(req, &provider, NULL)) != 0 || 221 (r = sshbuf_get_cstring(req, &device, NULL)) != 0 || 222 (r = sshbuf_get_cstring(req, &pin, NULL)) != 0) 223 fatal("%s: buffer error: %s", __progname, ssh_err(r)); 224 if (sshbuf_len(req) != 0) 225 fatal("%s: trailing data in request", __progname); 226 227 null_empty(&device); 228 null_empty(&pin); 229 230 if ((r = sshsk_load_resident(provider, device, pin, 231 &keys, &nkeys)) != 0) { 232 resp = reply_error(r, " sshsk_load_resident failed: %s", 233 ssh_err(r)); 234 goto out; 235 } 236 237 if ((resp = sshbuf_new()) == NULL) 238 fatal("%s: sshbuf_new failed", __progname); 239 240 if ((r = sshbuf_put_u32(resp, SSH_SK_HELPER_LOAD_RESIDENT)) != 0) 241 fatal("%s: buffer error: %s", __progname, ssh_err(r)); 242 243 for (i = 0; i < nkeys; i++) { 244 debug("%s: key %zu %s %s", __func__, i, 245 sshkey_type(keys[i]), keys[i]->sk_application); 246 sshbuf_reset(kbuf); 247 if ((r = sshkey_private_serialize(keys[i], kbuf)) != 0) 248 fatal("%s: serialize private key: %s", 249 __progname, ssh_err(r)); 250 if ((r = sshbuf_put_stringb(resp, kbuf)) != 0 || 251 (r = sshbuf_put_cstring(resp, "")) != 0) /* comment */ 252 fatal("%s: buffer error: %s", __progname, ssh_err(r)); 253 } 254 255 out: 256 for (i = 0; i < nkeys; i++) 257 sshkey_free(keys[i]); 258 free(keys); 259 sshbuf_free(kbuf); 260 free(provider); 261 if (pin != NULL) 262 freezero(pin, strlen(pin)); 263 return resp; 264 } 265 266 int 267 main(int argc, char **argv) 268 { 269 SyslogFacility log_facility = SYSLOG_FACILITY_AUTH; 270 LogLevel log_level = SYSLOG_LEVEL_ERROR; 271 struct sshbuf *req, *resp; 272 int in, out, ch, r, vflag = 0; 273 u_int rtype, ll = 0; 274 uint8_t version, log_stderr = 0; 275 276 sanitise_stdfd(); 277 log_init(__progname, log_level, log_facility, log_stderr); 278 279 while ((ch = getopt(argc, argv, "v")) != -1) { 280 switch (ch) { 281 case 'v': 282 vflag = 1; 283 if (log_level == SYSLOG_LEVEL_ERROR) 284 log_level = SYSLOG_LEVEL_DEBUG1; 285 else if (log_level < SYSLOG_LEVEL_DEBUG3) 286 log_level++; 287 break; 288 default: 289 fprintf(stderr, "usage: %s [-v]\n", __progname); 290 exit(1); 291 } 292 } 293 log_init(__progname, log_level, log_facility, vflag); 294 295 /* 296 * Rearrange our file descriptors a little; we don't trust the 297 * providers not to fiddle with stdin/out. 298 */ 299 closefrom(STDERR_FILENO + 1); 300 if ((in = dup(STDIN_FILENO)) == -1 || (out = dup(STDOUT_FILENO)) == -1) 301 fatal("%s: dup: %s", __progname, strerror(errno)); 302 close(STDIN_FILENO); 303 close(STDOUT_FILENO); 304 sanitise_stdfd(); /* resets to /dev/null */ 305 306 if ((req = sshbuf_new()) == NULL) 307 fatal("%s: sshbuf_new failed", __progname); 308 if (ssh_msg_recv(in, req) < 0) 309 fatal("ssh_msg_recv failed"); 310 close(in); 311 debug("%s: received message len %zu", __progname, sshbuf_len(req)); 312 313 if ((r = sshbuf_get_u8(req, &version)) != 0) 314 fatal("%s: buffer error: %s", __progname, ssh_err(r)); 315 if (version != SSH_SK_HELPER_VERSION) { 316 fatal("unsupported version: received %d, expected %d", 317 version, SSH_SK_HELPER_VERSION); 318 } 319 320 if ((r = sshbuf_get_u32(req, &rtype)) != 0 || 321 (r = sshbuf_get_u8(req, &log_stderr)) != 0 || 322 (r = sshbuf_get_u32(req, &ll)) != 0) 323 fatal("%s: buffer error: %s", __progname, ssh_err(r)); 324 325 if (!vflag && log_level_name((LogLevel)ll) != NULL) 326 log_init(__progname, (LogLevel)ll, log_facility, log_stderr); 327 328 switch (rtype) { 329 case SSH_SK_HELPER_SIGN: 330 resp = process_sign(req); 331 break; 332 case SSH_SK_HELPER_ENROLL: 333 resp = process_enroll(req); 334 break; 335 case SSH_SK_HELPER_LOAD_RESIDENT: 336 resp = process_load_resident(req); 337 break; 338 default: 339 fatal("%s: unsupported request type %u", __progname, rtype); 340 } 341 sshbuf_free(req); 342 debug("%s: reply len %zu", __progname, sshbuf_len(resp)); 343 344 if (ssh_msg_send(out, SSH_SK_HELPER_VERSION, resp) == -1) 345 fatal("ssh_msg_send failed"); 346 sshbuf_free(resp); 347 close(out); 348 349 return (0); 350 } 351