1 /* $NetBSD: ssh-sk-helper.c,v 1.4 2021/03/05 17:47:16 christos Exp $ */ 2 /* $OpenBSD: ssh-sk-helper.c,v 1.11 2020/10/18 11:32:02 djm Exp $ */ 3 4 /* 5 * Copyright (c) 2019 Google LLC 6 * 7 * Permission to use, copy, modify, and distribute this software for any 8 * purpose with or without fee is hereby granted, provided that the above 9 * copyright notice and this permission notice appear in all copies. 10 * 11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 17 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 18 */ 19 20 /* 21 * This is a tiny program used to isolate the address space used for 22 * security key middleware signing operations from ssh-agent. It is similar 23 * to ssh-pkcs11-helper.c but considerably simpler as the operations for 24 * security keys are stateless. 25 * 26 * Please crank SSH_SK_HELPER_VERSION in sshkey.h for any incompatible 27 * protocol changes. 28 */ 29 #include "includes.h" 30 __RCSID("$NetBSD: ssh-sk-helper.c,v 1.4 2021/03/05 17:47:16 christos Exp $"); 31 32 #include <limits.h> 33 #include <stdarg.h> 34 #include <stdio.h> 35 #include <stdlib.h> 36 #include <string.h> 37 #include <unistd.h> 38 #include <errno.h> 39 40 #include "xmalloc.h" 41 #include "log.h" 42 #include "sshkey.h" 43 #include "authfd.h" 44 #include "misc.h" 45 #include "sshbuf.h" 46 #include "msg.h" 47 #include "uidswap.h" 48 #include "sshkey.h" 49 #include "ssherr.h" 50 #include "ssh-sk.h" 51 52 extern char *__progname; 53 54 static struct sshbuf *reply_error(int r, const char *fmt, ...) 55 __attribute__((__format__ (__printf__, 2, 3))); 56 57 static struct sshbuf * 58 reply_error(int r, const char *fmt, ...) 59 { 60 char *msg; 61 va_list ap; 62 struct sshbuf *resp; 63 64 va_start(ap, fmt); 65 xvasprintf(&msg, fmt, ap); 66 va_end(ap); 67 debug("%s: %s", __progname, msg); 68 free(msg); 69 70 if (r >= 0) 71 fatal_f("invalid error code %d", r); 72 73 if ((resp = sshbuf_new()) == NULL) 74 fatal("%s: sshbuf_new failed", __progname); 75 if (sshbuf_put_u32(resp, SSH_SK_HELPER_ERROR) != 0 || 76 sshbuf_put_u32(resp, (u_int)-r) != 0) 77 fatal("%s: buffer error", __progname); 78 return resp; 79 } 80 81 /* If the specified string is zero length, then free it and replace with NULL */ 82 static void 83 null_empty(char **s) 84 { 85 if (s == NULL || *s == NULL || **s != '\0') 86 return; 87 88 free(*s); 89 *s = NULL; 90 } 91 92 static struct sshbuf * 93 process_sign(struct sshbuf *req) 94 { 95 int r = SSH_ERR_INTERNAL_ERROR; 96 struct sshbuf *resp, *kbuf; 97 struct sshkey *key = NULL; 98 uint32_t compat; 99 const u_char *message; 100 u_char *sig = NULL; 101 size_t msglen, siglen = 0; 102 char *provider = NULL, *pin = NULL; 103 104 if ((r = sshbuf_froms(req, &kbuf)) != 0 || 105 (r = sshbuf_get_cstring(req, &provider, NULL)) != 0 || 106 (r = sshbuf_get_string_direct(req, &message, &msglen)) != 0 || 107 (r = sshbuf_get_cstring(req, NULL, NULL)) != 0 || /* alg */ 108 (r = sshbuf_get_u32(req, &compat)) != 0 || 109 (r = sshbuf_get_cstring(req, &pin, NULL)) != 0) 110 fatal_r(r, "%s: parse", __progname); 111 if (sshbuf_len(req) != 0) 112 fatal("%s: trailing data in request", __progname); 113 114 if ((r = sshkey_private_deserialize(kbuf, &key)) != 0) 115 fatal_r(r, "%s: Unable to parse private key", __progname); 116 if (!sshkey_is_sk(key)) { 117 fatal("%s: Unsupported key type %s", 118 __progname, sshkey_ssh_name(key)); 119 } 120 121 debug_f("ready to sign with key %s, provider %s: " 122 "msg len %zu, compat 0x%lx", sshkey_type(key), 123 provider, msglen, (u_long)compat); 124 125 null_empty(&pin); 126 127 if ((r = sshsk_sign(provider, key, &sig, &siglen, 128 message, msglen, compat, pin)) != 0) { 129 resp = reply_error(r, "Signing failed: %s", ssh_err(r)); 130 goto out; 131 } 132 133 if ((resp = sshbuf_new()) == NULL) 134 fatal("%s: sshbuf_new failed", __progname); 135 136 if ((r = sshbuf_put_u32(resp, SSH_SK_HELPER_SIGN)) != 0 || 137 (r = sshbuf_put_string(resp, sig, siglen)) != 0) 138 fatal_r(r, "%s: compose", __progname); 139 out: 140 sshkey_free(key); 141 sshbuf_free(kbuf); 142 free(provider); 143 if (sig != NULL) 144 freezero(sig, siglen); 145 if (pin != NULL) 146 freezero(pin, strlen(pin)); 147 return resp; 148 } 149 150 static struct sshbuf * 151 process_enroll(struct sshbuf *req) 152 { 153 int r; 154 u_int type; 155 char *provider, *application, *pin, *device, *userid; 156 uint8_t flags; 157 struct sshbuf *challenge, *attest, *kbuf, *resp; 158 struct sshkey *key; 159 160 if ((attest = sshbuf_new()) == NULL || 161 (kbuf = sshbuf_new()) == NULL) 162 fatal("%s: sshbuf_new failed", __progname); 163 164 if ((r = sshbuf_get_u32(req, &type)) != 0 || 165 (r = sshbuf_get_cstring(req, &provider, NULL)) != 0 || 166 (r = sshbuf_get_cstring(req, &device, NULL)) != 0 || 167 (r = sshbuf_get_cstring(req, &application, NULL)) != 0 || 168 (r = sshbuf_get_cstring(req, &userid, NULL)) != 0 || 169 (r = sshbuf_get_u8(req, &flags)) != 0 || 170 (r = sshbuf_get_cstring(req, &pin, NULL)) != 0 || 171 (r = sshbuf_froms(req, &challenge)) != 0) 172 fatal_r(r, "%s: parse", __progname); 173 if (sshbuf_len(req) != 0) 174 fatal("%s: trailing data in request", __progname); 175 176 if (type > INT_MAX) 177 fatal("%s: bad type %u", __progname, type); 178 if (sshbuf_len(challenge) == 0) { 179 sshbuf_free(challenge); 180 challenge = NULL; 181 } 182 null_empty(&device); 183 null_empty(&userid); 184 null_empty(&pin); 185 186 if ((r = sshsk_enroll((int)type, provider, device, application, userid, 187 flags, pin, challenge, &key, attest)) != 0) { 188 resp = reply_error(r, "Enrollment failed: %s", ssh_err(r)); 189 goto out; 190 } 191 192 if ((resp = sshbuf_new()) == NULL) 193 fatal("%s: sshbuf_new failed", __progname); 194 if ((r = sshkey_private_serialize(key, kbuf)) != 0) 195 fatal_r(r, "%s: encode key", __progname); 196 if ((r = sshbuf_put_u32(resp, SSH_SK_HELPER_ENROLL)) != 0 || 197 (r = sshbuf_put_stringb(resp, kbuf)) != 0 || 198 (r = sshbuf_put_stringb(resp, attest)) != 0) 199 fatal_r(r, "%s: compose", __progname); 200 201 out: 202 sshkey_free(key); 203 sshbuf_free(kbuf); 204 sshbuf_free(attest); 205 sshbuf_free(challenge); 206 free(provider); 207 free(application); 208 if (pin != NULL) 209 freezero(pin, strlen(pin)); 210 211 return resp; 212 } 213 214 static struct sshbuf * 215 process_load_resident(struct sshbuf *req) 216 { 217 int r; 218 char *provider, *pin, *device; 219 struct sshbuf *kbuf, *resp; 220 struct sshkey **keys = NULL; 221 size_t nkeys = 0, i; 222 223 if ((kbuf = sshbuf_new()) == NULL) 224 fatal("%s: sshbuf_new failed", __progname); 225 226 if ((r = sshbuf_get_cstring(req, &provider, NULL)) != 0 || 227 (r = sshbuf_get_cstring(req, &device, NULL)) != 0 || 228 (r = sshbuf_get_cstring(req, &pin, NULL)) != 0) 229 fatal_r(r, "%s: parse", __progname); 230 if (sshbuf_len(req) != 0) 231 fatal("%s: trailing data in request", __progname); 232 233 null_empty(&device); 234 null_empty(&pin); 235 236 if ((r = sshsk_load_resident(provider, device, pin, 237 &keys, &nkeys)) != 0) { 238 resp = reply_error(r, " sshsk_load_resident failed: %s", 239 ssh_err(r)); 240 goto out; 241 } 242 243 if ((resp = sshbuf_new()) == NULL) 244 fatal("%s: sshbuf_new failed", __progname); 245 246 if ((r = sshbuf_put_u32(resp, SSH_SK_HELPER_LOAD_RESIDENT)) != 0) 247 fatal_r(r, "%s: compose", __progname); 248 249 for (i = 0; i < nkeys; i++) { 250 debug_f("key %zu %s %s", i, sshkey_type(keys[i]), 251 keys[i]->sk_application); 252 sshbuf_reset(kbuf); 253 if ((r = sshkey_private_serialize(keys[i], kbuf)) != 0) 254 fatal_r(r, "%s: encode key", __progname); 255 if ((r = sshbuf_put_stringb(resp, kbuf)) != 0 || 256 (r = sshbuf_put_cstring(resp, "")) != 0) /* comment */ 257 fatal_r(r, "%s: compose key", __progname); 258 } 259 260 out: 261 for (i = 0; i < nkeys; i++) 262 sshkey_free(keys[i]); 263 free(keys); 264 sshbuf_free(kbuf); 265 free(provider); 266 if (pin != NULL) 267 freezero(pin, strlen(pin)); 268 return resp; 269 } 270 271 int 272 main(int argc, char **argv) 273 { 274 SyslogFacility log_facility = SYSLOG_FACILITY_AUTH; 275 LogLevel log_level = SYSLOG_LEVEL_ERROR; 276 struct sshbuf *req, *resp; 277 int in, out, ch, r, vflag = 0; 278 u_int rtype, ll = 0; 279 uint8_t version, log_stderr = 0; 280 281 sanitise_stdfd(); 282 log_init(__progname, log_level, log_facility, log_stderr); 283 284 while ((ch = getopt(argc, argv, "v")) != -1) { 285 switch (ch) { 286 case 'v': 287 vflag = 1; 288 if (log_level == SYSLOG_LEVEL_ERROR) 289 log_level = SYSLOG_LEVEL_DEBUG1; 290 else if (log_level < SYSLOG_LEVEL_DEBUG3) 291 log_level++; 292 break; 293 default: 294 fprintf(stderr, "usage: %s [-v]\n", __progname); 295 exit(1); 296 } 297 } 298 log_init(__progname, log_level, log_facility, vflag); 299 300 /* 301 * Rearrange our file descriptors a little; we don't trust the 302 * providers not to fiddle with stdin/out. 303 */ 304 closefrom(STDERR_FILENO + 1); 305 if ((in = dup(STDIN_FILENO)) == -1 || (out = dup(STDOUT_FILENO)) == -1) 306 fatal("%s: dup: %s", __progname, strerror(errno)); 307 close(STDIN_FILENO); 308 close(STDOUT_FILENO); 309 sanitise_stdfd(); /* resets to /dev/null */ 310 311 if ((req = sshbuf_new()) == NULL) 312 fatal("%s: sshbuf_new failed", __progname); 313 if (ssh_msg_recv(in, req) < 0) 314 fatal("ssh_msg_recv failed"); 315 close(in); 316 debug_f("received message len %zu", sshbuf_len(req)); 317 318 if ((r = sshbuf_get_u8(req, &version)) != 0) 319 fatal_r(r, "%s: parse version", __progname); 320 if (version != SSH_SK_HELPER_VERSION) { 321 fatal("unsupported version: received %d, expected %d", 322 version, SSH_SK_HELPER_VERSION); 323 } 324 325 if ((r = sshbuf_get_u32(req, &rtype)) != 0 || 326 (r = sshbuf_get_u8(req, &log_stderr)) != 0 || 327 (r = sshbuf_get_u32(req, &ll)) != 0) 328 fatal_r(r, "%s: parse", __progname); 329 330 if (!vflag && log_level_name((LogLevel)ll) != NULL) 331 log_init(__progname, (LogLevel)ll, log_facility, log_stderr); 332 333 switch (rtype) { 334 case SSH_SK_HELPER_SIGN: 335 resp = process_sign(req); 336 break; 337 case SSH_SK_HELPER_ENROLL: 338 resp = process_enroll(req); 339 break; 340 case SSH_SK_HELPER_LOAD_RESIDENT: 341 resp = process_load_resident(req); 342 break; 343 default: 344 fatal("%s: unsupported request type %u", __progname, rtype); 345 } 346 sshbuf_free(req); 347 debug_f("reply len %zu", sshbuf_len(resp)); 348 349 if (ssh_msg_send(out, SSH_SK_HELPER_VERSION, resp) == -1) 350 fatal("ssh_msg_send failed"); 351 sshbuf_free(resp); 352 close(out); 353 354 return (0); 355 } 356