djm@openbsd.org | d214347 | 2019-12-13 20:16:56 +0000 | [diff] [blame] | 1 | /* $OpenBSD: ssh-sk-client.c,v 1.1 2019/12/13 20:16:56 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 | |
Damien Miller | a33ab16 | 2019-12-14 09:15:06 +1100 | [diff] [blame] | 18 | #include "includes.h" |
| 19 | |
djm@openbsd.org | d214347 | 2019-12-13 20:16:56 +0000 | [diff] [blame] | 20 | #include <sys/types.h> |
| 21 | #include <sys/socket.h> |
| 22 | #include <sys/wait.h> |
| 23 | |
| 24 | #include <errno.h> |
| 25 | #include <signal.h> |
| 26 | #include <stdarg.h> |
| 27 | #include <stdio.h> |
| 28 | #include <stdlib.h> |
| 29 | #include <string.h> |
| 30 | #include <unistd.h> |
| 31 | |
| 32 | #include "log.h" |
| 33 | #include "ssherr.h" |
| 34 | #include "sshbuf.h" |
| 35 | #include "sshkey.h" |
| 36 | #include "msg.h" |
| 37 | #include "digest.h" |
| 38 | #include "pathnames.h" |
| 39 | #include "ssh-sk.h" |
| 40 | |
| 41 | /* #define DEBUG_SK 1 */ |
| 42 | |
| 43 | static int |
| 44 | start_helper(int *fdp, pid_t *pidp, void (**osigchldp)(int)) |
| 45 | { |
| 46 | void (*osigchld)(int); |
| 47 | int oerrno, pair[2], r = SSH_ERR_INTERNAL_ERROR; |
| 48 | pid_t pid; |
| 49 | char *helper, *verbosity = NULL; |
| 50 | |
| 51 | *fdp = -1; |
| 52 | *pidp = 0; |
| 53 | *osigchldp = SIG_DFL; |
| 54 | |
| 55 | helper = getenv("SSH_SK_HELPER"); |
| 56 | if (helper == NULL || strlen(helper) == 0) |
| 57 | helper = _PATH_SSH_SK_HELPER; |
| 58 | #ifdef DEBUG_SK |
| 59 | verbosity = "-vvv"; |
| 60 | #endif |
| 61 | |
| 62 | /* Start helper */ |
| 63 | if (socketpair(AF_UNIX, SOCK_STREAM, 0, pair) == -1) { |
| 64 | error("socketpair: %s", strerror(errno)); |
| 65 | return SSH_ERR_SYSTEM_ERROR; |
| 66 | } |
| 67 | osigchld = signal(SIGCHLD, SIG_DFL); |
| 68 | if ((pid = fork()) == -1) { |
| 69 | oerrno = errno; |
| 70 | error("fork: %s", strerror(errno)); |
| 71 | close(pair[0]); |
| 72 | close(pair[1]); |
| 73 | signal(SIGCHLD, osigchld); |
| 74 | errno = oerrno; |
| 75 | return SSH_ERR_SYSTEM_ERROR; |
| 76 | } |
| 77 | if (pid == 0) { |
| 78 | if ((dup2(pair[1], STDIN_FILENO) == -1) || |
| 79 | (dup2(pair[1], STDOUT_FILENO) == -1)) { |
| 80 | error("%s: dup2: %s", __func__, ssh_err(r)); |
| 81 | _exit(1); |
| 82 | } |
| 83 | close(pair[0]); |
| 84 | close(pair[1]); |
| 85 | closefrom(STDERR_FILENO + 1); |
| 86 | debug("%s: starting %s %s", __func__, helper, |
| 87 | verbosity == NULL ? "" : verbosity); |
| 88 | execlp(helper, helper, verbosity, (char *)NULL); |
| 89 | error("%s: execlp: %s", __func__, strerror(errno)); |
| 90 | _exit(1); |
| 91 | } |
| 92 | close(pair[1]); |
| 93 | |
| 94 | /* success */ |
| 95 | debug3("%s: started pid=%ld", __func__, (long)pid); |
| 96 | *fdp = pair[0]; |
| 97 | *pidp = pid; |
| 98 | *osigchldp = osigchld; |
| 99 | return 0; |
| 100 | } |
| 101 | |
| 102 | static int |
| 103 | reap_helper(pid_t pid) |
| 104 | { |
| 105 | int status, oerrno; |
| 106 | |
| 107 | debug3("%s: pid=%ld", __func__, (long)pid); |
| 108 | |
| 109 | errno = 0; |
| 110 | while (waitpid(pid, &status, 0) == -1) { |
| 111 | if (errno == EINTR) { |
| 112 | errno = 0; |
| 113 | continue; |
| 114 | } |
| 115 | oerrno = errno; |
| 116 | error("%s: waitpid: %s", __func__, strerror(errno)); |
| 117 | errno = oerrno; |
| 118 | return SSH_ERR_SYSTEM_ERROR; |
| 119 | } |
| 120 | if (!WIFEXITED(status)) { |
| 121 | error("%s: helper exited abnormally", __func__); |
| 122 | return SSH_ERR_AGENT_FAILURE; |
| 123 | } else if (WEXITSTATUS(status) != 0) { |
| 124 | error("%s: helper exited with non-zero exit status", __func__); |
| 125 | return SSH_ERR_AGENT_FAILURE; |
| 126 | } |
| 127 | return 0; |
| 128 | } |
| 129 | |
| 130 | static int |
| 131 | client_converse(struct sshbuf *req, struct sshbuf **respp) |
| 132 | { |
| 133 | int oerrno, fd, r2, r = SSH_ERR_INTERNAL_ERROR; |
| 134 | pid_t pid; |
| 135 | u_char version; |
| 136 | void (*osigchld)(int); |
| 137 | struct sshbuf *resp = NULL; |
| 138 | |
| 139 | *respp = NULL; |
| 140 | |
| 141 | if ((r = start_helper(&fd, &pid, &osigchld)) != 0) |
| 142 | return r; |
| 143 | |
| 144 | if ((resp = sshbuf_new()) == NULL) { |
| 145 | r = SSH_ERR_ALLOC_FAIL; |
| 146 | goto out; |
| 147 | } |
| 148 | |
| 149 | if ((r = ssh_msg_send(fd, SSH_SK_HELPER_VERSION, req)) != 0) { |
| 150 | error("%s: send: %s", __func__, ssh_err(r)); |
| 151 | goto out; |
| 152 | } |
| 153 | if ((r = ssh_msg_recv(fd, resp)) != 0) { |
| 154 | error("%s: receive: %s", __func__, ssh_err(r)); |
| 155 | goto out; |
| 156 | } |
| 157 | if ((r = sshbuf_get_u8(resp, &version)) != 0) { |
| 158 | error("%s: parse version: %s", __func__, ssh_err(r)); |
| 159 | goto out; |
| 160 | } |
| 161 | if (version != SSH_SK_HELPER_VERSION) { |
| 162 | error("%s: unsupported version: got %u, expected %u", |
| 163 | __func__, version, SSH_SK_HELPER_VERSION); |
| 164 | r = SSH_ERR_INVALID_FORMAT; |
| 165 | goto out; |
| 166 | } |
| 167 | /* success */ |
| 168 | r = 0; |
| 169 | out: |
| 170 | oerrno = errno; |
| 171 | close(fd); |
| 172 | if ((r2 = reap_helper(pid)) != 0) { |
| 173 | if (r == 0) { |
| 174 | r = r2; |
| 175 | oerrno = errno; |
| 176 | } |
| 177 | } |
| 178 | if (r == 0) { |
| 179 | *respp = resp; |
| 180 | resp = NULL; |
| 181 | } |
| 182 | sshbuf_free(resp); |
| 183 | signal(SIGCHLD, osigchld); |
| 184 | errno = oerrno; |
| 185 | return r; |
| 186 | |
| 187 | } |
| 188 | |
| 189 | int |
| 190 | sshsk_sign(const char *provider, struct sshkey *key, |
| 191 | u_char **sigp, size_t *lenp, const u_char *data, size_t datalen, |
| 192 | u_int compat) |
| 193 | { |
| 194 | int oerrno, r = SSH_ERR_INTERNAL_ERROR; |
| 195 | char *fp = NULL; |
| 196 | struct sshbuf *kbuf = NULL, *req = NULL, *resp = NULL; |
| 197 | |
| 198 | *sigp = NULL; |
| 199 | *lenp = 0; |
| 200 | |
Damien Miller | 9244990 | 2019-12-14 09:21:46 +1100 | [diff] [blame^] | 201 | #ifndef ENABLE_SK |
| 202 | return SSH_ERR_KEY_TYPE_UNKNOWN; |
| 203 | #endif |
| 204 | |
djm@openbsd.org | d214347 | 2019-12-13 20:16:56 +0000 | [diff] [blame] | 205 | if ((kbuf = sshbuf_new()) == NULL || |
| 206 | (req = sshbuf_new()) == NULL) { |
| 207 | r = SSH_ERR_ALLOC_FAIL; |
| 208 | goto out; |
| 209 | } |
| 210 | |
| 211 | if ((r = sshkey_private_serialize(key, kbuf)) != 0) { |
| 212 | error("%s: serialize private key: %s", __func__, ssh_err(r)); |
| 213 | goto out; |
| 214 | } |
| 215 | if ((r = sshbuf_put_u32(req, SSH_SK_HELPER_SIGN)) != 0 || |
| 216 | (r = sshbuf_put_stringb(req, kbuf)) != 0 || |
| 217 | (r = sshbuf_put_cstring(req, provider)) != 0 || |
| 218 | (r = sshbuf_put_string(req, data, datalen)) != 0 || |
| 219 | (r = sshbuf_put_cstring(req, NULL)) != 0 || /* alg */ |
| 220 | (r = sshbuf_put_u32(req, compat)) != 0) { |
| 221 | error("%s: compose: %s", __func__, ssh_err(r)); |
| 222 | goto out; |
| 223 | } |
| 224 | |
| 225 | if ((fp = sshkey_fingerprint(key, SSH_FP_HASH_DEFAULT, |
| 226 | SSH_FP_DEFAULT)) == NULL) { |
| 227 | error("%s: sshkey_fingerprint failed", __func__); |
| 228 | r = SSH_ERR_ALLOC_FAIL; |
| 229 | goto out; |
| 230 | } |
| 231 | if ((r = client_converse(req, &resp)) != 0) |
| 232 | goto out; |
| 233 | |
| 234 | if ((r = sshbuf_get_string(resp, sigp, lenp)) != 0) { |
| 235 | error("%s: parse signature: %s", __func__, ssh_err(r)); |
| 236 | r = SSH_ERR_INVALID_FORMAT; |
| 237 | goto out; |
| 238 | } |
| 239 | if (sshbuf_len(resp) != 0) { |
| 240 | error("%s: trailing data in response", __func__); |
| 241 | r = SSH_ERR_INVALID_FORMAT; |
| 242 | goto out; |
| 243 | } |
| 244 | /* success */ |
| 245 | r = 0; |
| 246 | out: |
| 247 | oerrno = errno; |
| 248 | if (r != 0) { |
| 249 | freezero(*sigp, *lenp); |
| 250 | *sigp = NULL; |
| 251 | *lenp = 0; |
| 252 | } |
| 253 | sshbuf_free(kbuf); |
| 254 | sshbuf_free(req); |
| 255 | sshbuf_free(resp); |
| 256 | errno = oerrno; |
| 257 | return r; |
| 258 | } |
| 259 | |
| 260 | int |
| 261 | sshsk_enroll(int type, const char *provider_path, const char *application, |
| 262 | uint8_t flags, struct sshbuf *challenge_buf, struct sshkey **keyp, |
| 263 | struct sshbuf *attest) |
| 264 | { |
| 265 | int oerrno, r = SSH_ERR_INTERNAL_ERROR; |
| 266 | struct sshbuf *kbuf = NULL, *abuf = NULL, *req = NULL, *resp = NULL; |
| 267 | struct sshkey *key = NULL; |
| 268 | |
| 269 | *keyp = NULL; |
| 270 | if (attest != NULL) |
| 271 | sshbuf_reset(attest); |
| 272 | |
Damien Miller | 9244990 | 2019-12-14 09:21:46 +1100 | [diff] [blame^] | 273 | #ifndef ENABLE_SK |
| 274 | return SSH_ERR_KEY_TYPE_UNKNOWN; |
| 275 | #endif |
| 276 | |
djm@openbsd.org | d214347 | 2019-12-13 20:16:56 +0000 | [diff] [blame] | 277 | if (type < 0) |
| 278 | return SSH_ERR_INVALID_ARGUMENT; |
| 279 | |
| 280 | if ((abuf = sshbuf_new()) == NULL || |
| 281 | (kbuf = sshbuf_new()) == NULL || |
| 282 | (req = sshbuf_new()) == NULL) { |
| 283 | r = SSH_ERR_ALLOC_FAIL; |
| 284 | goto out; |
| 285 | } |
| 286 | |
| 287 | if ((r = sshbuf_put_u32(req, SSH_SK_HELPER_ENROLL)) != 0 || |
| 288 | (r = sshbuf_put_u32(req, (u_int)type)) != 0 || |
| 289 | (r = sshbuf_put_cstring(req, provider_path)) != 0 || |
| 290 | (r = sshbuf_put_cstring(req, application)) != 0 || |
| 291 | (r = sshbuf_put_u8(req, flags)) != 0 || |
| 292 | (r = sshbuf_put_stringb(req, challenge_buf)) != 0) { |
| 293 | error("%s: compose: %s", __func__, ssh_err(r)); |
| 294 | goto out; |
| 295 | } |
| 296 | |
| 297 | if ((r = client_converse(req, &resp)) != 0) |
| 298 | goto out; |
| 299 | |
| 300 | if ((r = sshbuf_get_stringb(resp, kbuf)) != 0 || |
| 301 | (r = sshbuf_get_stringb(resp, abuf)) != 0) { |
| 302 | error("%s: parse signature: %s", __func__, ssh_err(r)); |
| 303 | r = SSH_ERR_INVALID_FORMAT; |
| 304 | goto out; |
| 305 | } |
| 306 | if (sshbuf_len(resp) != 0) { |
| 307 | error("%s: trailing data in response", __func__); |
| 308 | r = SSH_ERR_INVALID_FORMAT; |
| 309 | goto out; |
| 310 | } |
| 311 | if ((r = sshkey_private_deserialize(kbuf, &key)) != 0) { |
| 312 | error("Unable to parse private key: %s", ssh_err(r)); |
| 313 | goto out; |
| 314 | } |
| 315 | if (attest != NULL && (r = sshbuf_putb(attest, abuf)) != 0) { |
| 316 | error("%s: buffer error: %s", __func__, ssh_err(r)); |
| 317 | goto out; |
| 318 | } |
| 319 | |
| 320 | /* success */ |
| 321 | r = 0; |
| 322 | *keyp = key; |
| 323 | key = NULL; |
| 324 | out: |
| 325 | oerrno = errno; |
| 326 | sshkey_free(key); |
| 327 | sshbuf_free(kbuf); |
| 328 | sshbuf_free(abuf); |
| 329 | sshbuf_free(req); |
| 330 | sshbuf_free(resp); |
| 331 | errno = oerrno; |
| 332 | return r; |
| 333 | } |