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