djm@openbsd.org | 89a8d45 | 2020-01-25 00:03:36 +0000 | [diff] [blame] | 1 | /* $OpenBSD: ssh-pkcs11-client.c,v 1.16 2020/01/25 00:03:36 djm Exp $ */ |
Damien Miller | 7ea845e | 2010-02-12 09:21:02 +1100 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2010 Markus Friedl. All rights reserved. |
djm@openbsd.org | 93f0210 | 2019-01-20 22:51:37 +0000 | [diff] [blame] | 4 | * Copyright (c) 2014 Pedro Martelletto. All rights reserved. |
Damien Miller | 7ea845e | 2010-02-12 09:21:02 +1100 | [diff] [blame] | 5 | * |
| 6 | * Permission to use, copy, modify, and distribute this software for any |
| 7 | * purpose with or without fee is hereby granted, provided that the above |
| 8 | * copyright notice and this permission notice appear in all copies. |
| 9 | * |
| 10 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
| 11 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
| 12 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
| 13 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 14 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 15 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
| 16 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 17 | */ |
| 18 | |
Damien Miller | 8ad0fbd | 2010-02-12 09:49:06 +1100 | [diff] [blame] | 19 | #include "includes.h" |
| 20 | |
Damien Miller | dfa4156 | 2010-02-12 10:06:28 +1100 | [diff] [blame] | 21 | #ifdef ENABLE_PKCS11 |
| 22 | |
Damien Miller | 7ea845e | 2010-02-12 09:21:02 +1100 | [diff] [blame] | 23 | #include <sys/types.h> |
Damien Miller | 8ad0fbd | 2010-02-12 09:49:06 +1100 | [diff] [blame] | 24 | #ifdef HAVE_SYS_TIME_H |
| 25 | # include <sys/time.h> |
| 26 | #endif |
Damien Miller | 7ea845e | 2010-02-12 09:21:02 +1100 | [diff] [blame] | 27 | #include <sys/socket.h> |
| 28 | |
| 29 | #include <stdarg.h> |
| 30 | #include <string.h> |
| 31 | #include <unistd.h> |
| 32 | #include <errno.h> |
| 33 | |
djm@openbsd.org | 93f0210 | 2019-01-20 22:51:37 +0000 | [diff] [blame] | 34 | #include <openssl/ecdsa.h> |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 35 | #include <openssl/rsa.h> |
| 36 | |
Damien Miller | 48f54b9 | 2018-09-13 12:13:50 +1000 | [diff] [blame] | 37 | #include "openbsd-compat/openssl-compat.h" |
| 38 | |
Damien Miller | 7ea845e | 2010-02-12 09:21:02 +1100 | [diff] [blame] | 39 | #include "pathnames.h" |
| 40 | #include "xmalloc.h" |
markus@openbsd.org | ff55f4a | 2018-07-09 20:39:28 +0000 | [diff] [blame] | 41 | #include "sshbuf.h" |
Damien Miller | 7ea845e | 2010-02-12 09:21:02 +1100 | [diff] [blame] | 42 | #include "log.h" |
| 43 | #include "misc.h" |
markus@openbsd.org | ff55f4a | 2018-07-09 20:39:28 +0000 | [diff] [blame] | 44 | #include "sshkey.h" |
Damien Miller | 7ea845e | 2010-02-12 09:21:02 +1100 | [diff] [blame] | 45 | #include "authfd.h" |
| 46 | #include "atomicio.h" |
| 47 | #include "ssh-pkcs11.h" |
markus@openbsd.org | ff55f4a | 2018-07-09 20:39:28 +0000 | [diff] [blame] | 48 | #include "ssherr.h" |
Damien Miller | 7ea845e | 2010-02-12 09:21:02 +1100 | [diff] [blame] | 49 | |
| 50 | /* borrows code from sftp-server and ssh-agent */ |
| 51 | |
djm@openbsd.org | c7670b0 | 2019-01-21 12:53:35 +0000 | [diff] [blame] | 52 | static int fd = -1; |
| 53 | static pid_t pid = -1; |
Damien Miller | 7ea845e | 2010-02-12 09:21:02 +1100 | [diff] [blame] | 54 | |
| 55 | static void |
markus@openbsd.org | ff55f4a | 2018-07-09 20:39:28 +0000 | [diff] [blame] | 56 | send_msg(struct sshbuf *m) |
Damien Miller | 7ea845e | 2010-02-12 09:21:02 +1100 | [diff] [blame] | 57 | { |
| 58 | u_char buf[4]; |
markus@openbsd.org | ff55f4a | 2018-07-09 20:39:28 +0000 | [diff] [blame] | 59 | size_t mlen = sshbuf_len(m); |
| 60 | int r; |
Damien Miller | 7ea845e | 2010-02-12 09:21:02 +1100 | [diff] [blame] | 61 | |
markus@openbsd.org | ff55f4a | 2018-07-09 20:39:28 +0000 | [diff] [blame] | 62 | POKE_U32(buf, mlen); |
Damien Miller | 7ea845e | 2010-02-12 09:21:02 +1100 | [diff] [blame] | 63 | if (atomicio(vwrite, fd, buf, 4) != 4 || |
markus@openbsd.org | 49f47e6 | 2018-07-09 21:59:10 +0000 | [diff] [blame] | 64 | atomicio(vwrite, fd, sshbuf_mutable_ptr(m), |
markus@openbsd.org | ff55f4a | 2018-07-09 20:39:28 +0000 | [diff] [blame] | 65 | sshbuf_len(m)) != sshbuf_len(m)) |
Damien Miller | 7ea845e | 2010-02-12 09:21:02 +1100 | [diff] [blame] | 66 | error("write to helper failed"); |
markus@openbsd.org | ff55f4a | 2018-07-09 20:39:28 +0000 | [diff] [blame] | 67 | if ((r = sshbuf_consume(m, mlen)) != 0) |
| 68 | fatal("%s: buffer error: %s", __func__, ssh_err(r)); |
Damien Miller | 7ea845e | 2010-02-12 09:21:02 +1100 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | static int |
markus@openbsd.org | ff55f4a | 2018-07-09 20:39:28 +0000 | [diff] [blame] | 72 | recv_msg(struct sshbuf *m) |
Damien Miller | 7ea845e | 2010-02-12 09:21:02 +1100 | [diff] [blame] | 73 | { |
| 74 | u_int l, len; |
markus@openbsd.org | ff55f4a | 2018-07-09 20:39:28 +0000 | [diff] [blame] | 75 | u_char c, buf[1024]; |
| 76 | int r; |
Damien Miller | 7ea845e | 2010-02-12 09:21:02 +1100 | [diff] [blame] | 77 | |
| 78 | if ((len = atomicio(read, fd, buf, 4)) != 4) { |
| 79 | error("read from helper failed: %u", len); |
| 80 | return (0); /* XXX */ |
| 81 | } |
markus@openbsd.org | ff55f4a | 2018-07-09 20:39:28 +0000 | [diff] [blame] | 82 | len = PEEK_U32(buf); |
Damien Miller | 7ea845e | 2010-02-12 09:21:02 +1100 | [diff] [blame] | 83 | if (len > 256 * 1024) |
| 84 | fatal("response too long: %u", len); |
| 85 | /* read len bytes into m */ |
markus@openbsd.org | ff55f4a | 2018-07-09 20:39:28 +0000 | [diff] [blame] | 86 | sshbuf_reset(m); |
Damien Miller | 7ea845e | 2010-02-12 09:21:02 +1100 | [diff] [blame] | 87 | while (len > 0) { |
| 88 | l = len; |
| 89 | if (l > sizeof(buf)) |
| 90 | l = sizeof(buf); |
| 91 | if (atomicio(read, fd, buf, l) != l) { |
| 92 | error("response from helper failed."); |
| 93 | return (0); /* XXX */ |
| 94 | } |
markus@openbsd.org | ff55f4a | 2018-07-09 20:39:28 +0000 | [diff] [blame] | 95 | if ((r = sshbuf_put(m, buf, l)) != 0) |
| 96 | fatal("%s: buffer error: %s", __func__, ssh_err(r)); |
Damien Miller | 7ea845e | 2010-02-12 09:21:02 +1100 | [diff] [blame] | 97 | len -= l; |
| 98 | } |
markus@openbsd.org | ff55f4a | 2018-07-09 20:39:28 +0000 | [diff] [blame] | 99 | if ((r = sshbuf_get_u8(m, &c)) != 0) |
| 100 | fatal("%s: buffer error: %s", __func__, ssh_err(r)); |
| 101 | return c; |
Damien Miller | 7ea845e | 2010-02-12 09:21:02 +1100 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | int |
| 105 | pkcs11_init(int interactive) |
| 106 | { |
| 107 | return (0); |
| 108 | } |
| 109 | |
| 110 | void |
| 111 | pkcs11_terminate(void) |
| 112 | { |
tb@openbsd.org | 3484380 | 2018-02-05 05:37:46 +0000 | [diff] [blame] | 113 | if (fd >= 0) |
| 114 | close(fd); |
Damien Miller | 7ea845e | 2010-02-12 09:21:02 +1100 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | static int |
djm@openbsd.org | 93f0210 | 2019-01-20 22:51:37 +0000 | [diff] [blame] | 118 | rsa_encrypt(int flen, const u_char *from, u_char *to, RSA *rsa, int padding) |
Damien Miller | 7ea845e | 2010-02-12 09:21:02 +1100 | [diff] [blame] | 119 | { |
djm@openbsd.org | 0c50992 | 2019-01-20 22:57:45 +0000 | [diff] [blame] | 120 | struct sshkey *key = NULL; |
| 121 | struct sshbuf *msg = NULL; |
| 122 | u_char *blob = NULL, *signature = NULL; |
markus@openbsd.org | ff55f4a | 2018-07-09 20:39:28 +0000 | [diff] [blame] | 123 | size_t blen, slen = 0; |
| 124 | int r, ret = -1; |
Damien Miller | 7ea845e | 2010-02-12 09:21:02 +1100 | [diff] [blame] | 125 | |
| 126 | if (padding != RSA_PKCS1_PADDING) |
djm@openbsd.org | 0c50992 | 2019-01-20 22:57:45 +0000 | [diff] [blame] | 127 | goto fail; |
| 128 | key = sshkey_new(KEY_UNSPEC); |
| 129 | if (key == NULL) { |
| 130 | error("%s: sshkey_new failed", __func__); |
| 131 | goto fail; |
| 132 | } |
| 133 | key->type = KEY_RSA; |
| 134 | RSA_up_ref(rsa); |
| 135 | key->rsa = rsa; |
| 136 | if ((r = sshkey_to_blob(key, &blob, &blen)) != 0) { |
markus@openbsd.org | ff55f4a | 2018-07-09 20:39:28 +0000 | [diff] [blame] | 137 | error("%s: sshkey_to_blob: %s", __func__, ssh_err(r)); |
djm@openbsd.org | 0c50992 | 2019-01-20 22:57:45 +0000 | [diff] [blame] | 138 | goto fail; |
markus@openbsd.org | ff55f4a | 2018-07-09 20:39:28 +0000 | [diff] [blame] | 139 | } |
| 140 | if ((msg = sshbuf_new()) == NULL) |
| 141 | fatal("%s: sshbuf_new failed", __func__); |
| 142 | if ((r = sshbuf_put_u8(msg, SSH2_AGENTC_SIGN_REQUEST)) != 0 || |
| 143 | (r = sshbuf_put_string(msg, blob, blen)) != 0 || |
| 144 | (r = sshbuf_put_string(msg, from, flen)) != 0 || |
| 145 | (r = sshbuf_put_u32(msg, 0)) != 0) |
| 146 | fatal("%s: buffer error: %s", __func__, ssh_err(r)); |
markus@openbsd.org | ff55f4a | 2018-07-09 20:39:28 +0000 | [diff] [blame] | 147 | send_msg(msg); |
| 148 | sshbuf_reset(msg); |
Damien Miller | 7ea845e | 2010-02-12 09:21:02 +1100 | [diff] [blame] | 149 | |
markus@openbsd.org | ff55f4a | 2018-07-09 20:39:28 +0000 | [diff] [blame] | 150 | if (recv_msg(msg) == SSH2_AGENT_SIGN_RESPONSE) { |
| 151 | if ((r = sshbuf_get_string(msg, &signature, &slen)) != 0) |
| 152 | fatal("%s: buffer error: %s", __func__, ssh_err(r)); |
| 153 | if (slen <= (size_t)RSA_size(rsa)) { |
Damien Miller | 7ea845e | 2010-02-12 09:21:02 +1100 | [diff] [blame] | 154 | memcpy(to, signature, slen); |
| 155 | ret = slen; |
| 156 | } |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 157 | free(signature); |
Damien Miller | 7ea845e | 2010-02-12 09:21:02 +1100 | [diff] [blame] | 158 | } |
djm@openbsd.org | 0c50992 | 2019-01-20 22:57:45 +0000 | [diff] [blame] | 159 | fail: |
| 160 | free(blob); |
| 161 | sshkey_free(key); |
markus@openbsd.org | ff55f4a | 2018-07-09 20:39:28 +0000 | [diff] [blame] | 162 | sshbuf_free(msg); |
Damien Miller | 7ea845e | 2010-02-12 09:21:02 +1100 | [diff] [blame] | 163 | return (ret); |
| 164 | } |
| 165 | |
Damien Miller | e2cb445 | 2019-01-21 11:32:28 +1100 | [diff] [blame] | 166 | #ifdef HAVE_EC_KEY_METHOD_NEW |
djm@openbsd.org | 93f0210 | 2019-01-20 22:51:37 +0000 | [diff] [blame] | 167 | static ECDSA_SIG * |
| 168 | ecdsa_do_sign(const unsigned char *dgst, int dgst_len, const BIGNUM *inv, |
| 169 | const BIGNUM *rp, EC_KEY *ec) |
Damien Miller | 7ea845e | 2010-02-12 09:21:02 +1100 | [diff] [blame] | 170 | { |
djm@openbsd.org | 0c50992 | 2019-01-20 22:57:45 +0000 | [diff] [blame] | 171 | struct sshkey *key = NULL; |
| 172 | struct sshbuf *msg = NULL; |
djm@openbsd.org | 93f0210 | 2019-01-20 22:51:37 +0000 | [diff] [blame] | 173 | ECDSA_SIG *ret = NULL; |
djm@openbsd.org | 0c50992 | 2019-01-20 22:57:45 +0000 | [diff] [blame] | 174 | const u_char *cp; |
| 175 | u_char *blob = NULL, *signature = NULL; |
| 176 | size_t blen, slen = 0; |
| 177 | int r, nid; |
djm@openbsd.org | 93f0210 | 2019-01-20 22:51:37 +0000 | [diff] [blame] | 178 | |
djm@openbsd.org | 0c50992 | 2019-01-20 22:57:45 +0000 | [diff] [blame] | 179 | nid = sshkey_ecdsa_key_to_nid(ec); |
| 180 | if (nid < 0) { |
djm@openbsd.org | 93f0210 | 2019-01-20 22:51:37 +0000 | [diff] [blame] | 181 | error("%s: couldn't get curve nid", __func__); |
djm@openbsd.org | 0c50992 | 2019-01-20 22:57:45 +0000 | [diff] [blame] | 182 | goto fail; |
djm@openbsd.org | 93f0210 | 2019-01-20 22:51:37 +0000 | [diff] [blame] | 183 | } |
djm@openbsd.org | 0c50992 | 2019-01-20 22:57:45 +0000 | [diff] [blame] | 184 | |
| 185 | key = sshkey_new(KEY_UNSPEC); |
| 186 | if (key == NULL) { |
| 187 | error("%s: sshkey_new failed", __func__); |
| 188 | goto fail; |
| 189 | } |
| 190 | key->ecdsa = ec; |
| 191 | key->ecdsa_nid = nid; |
| 192 | key->type = KEY_ECDSA; |
| 193 | EC_KEY_up_ref(ec); |
| 194 | |
| 195 | if ((r = sshkey_to_blob(key, &blob, &blen)) != 0) { |
djm@openbsd.org | 93f0210 | 2019-01-20 22:51:37 +0000 | [diff] [blame] | 196 | error("%s: sshkey_to_blob: %s", __func__, ssh_err(r)); |
djm@openbsd.org | 0c50992 | 2019-01-20 22:57:45 +0000 | [diff] [blame] | 197 | goto fail; |
djm@openbsd.org | 93f0210 | 2019-01-20 22:51:37 +0000 | [diff] [blame] | 198 | } |
| 199 | if ((msg = sshbuf_new()) == NULL) |
| 200 | fatal("%s: sshbuf_new failed", __func__); |
| 201 | if ((r = sshbuf_put_u8(msg, SSH2_AGENTC_SIGN_REQUEST)) != 0 || |
| 202 | (r = sshbuf_put_string(msg, blob, blen)) != 0 || |
| 203 | (r = sshbuf_put_string(msg, dgst, dgst_len)) != 0 || |
| 204 | (r = sshbuf_put_u32(msg, 0)) != 0) |
| 205 | fatal("%s: buffer error: %s", __func__, ssh_err(r)); |
djm@openbsd.org | 93f0210 | 2019-01-20 22:51:37 +0000 | [diff] [blame] | 206 | send_msg(msg); |
| 207 | sshbuf_reset(msg); |
| 208 | |
| 209 | if (recv_msg(msg) == SSH2_AGENT_SIGN_RESPONSE) { |
| 210 | if ((r = sshbuf_get_string(msg, &signature, &slen)) != 0) |
| 211 | fatal("%s: buffer error: %s", __func__, ssh_err(r)); |
| 212 | cp = signature; |
| 213 | ret = d2i_ECDSA_SIG(NULL, &cp, slen); |
| 214 | free(signature); |
| 215 | } |
| 216 | |
djm@openbsd.org | 0c50992 | 2019-01-20 22:57:45 +0000 | [diff] [blame] | 217 | fail: |
| 218 | free(blob); |
| 219 | sshkey_free(key); |
djm@openbsd.org | 93f0210 | 2019-01-20 22:51:37 +0000 | [diff] [blame] | 220 | sshbuf_free(msg); |
| 221 | return (ret); |
| 222 | } |
Damien Miller | e2cb445 | 2019-01-21 11:32:28 +1100 | [diff] [blame] | 223 | #endif /* HAVE_EC_KEY_METHOD_NEW */ |
djm@openbsd.org | 93f0210 | 2019-01-20 22:51:37 +0000 | [diff] [blame] | 224 | |
| 225 | static RSA_METHOD *helper_rsa; |
Damien Miller | e2cb445 | 2019-01-21 11:32:28 +1100 | [diff] [blame] | 226 | #ifdef HAVE_EC_KEY_METHOD_NEW |
djm@openbsd.org | 93f0210 | 2019-01-20 22:51:37 +0000 | [diff] [blame] | 227 | static EC_KEY_METHOD *helper_ecdsa; |
Damien Miller | e2cb445 | 2019-01-21 11:32:28 +1100 | [diff] [blame] | 228 | #endif /* HAVE_EC_KEY_METHOD_NEW */ |
djm@openbsd.org | 93f0210 | 2019-01-20 22:51:37 +0000 | [diff] [blame] | 229 | |
| 230 | /* redirect private key crypto operations to the ssh-pkcs11-helper */ |
| 231 | static void |
| 232 | wrap_key(struct sshkey *k) |
| 233 | { |
| 234 | if (k->type == KEY_RSA) |
| 235 | RSA_set_method(k->rsa, helper_rsa); |
Damien Miller | e2cb445 | 2019-01-21 11:32:28 +1100 | [diff] [blame] | 236 | #ifdef HAVE_EC_KEY_METHOD_NEW |
djm@openbsd.org | 93f0210 | 2019-01-20 22:51:37 +0000 | [diff] [blame] | 237 | else if (k->type == KEY_ECDSA) |
| 238 | EC_KEY_set_method(k->ecdsa, helper_ecdsa); |
Damien Miller | e2cb445 | 2019-01-21 11:32:28 +1100 | [diff] [blame] | 239 | #endif /* HAVE_EC_KEY_METHOD_NEW */ |
djm@openbsd.org | 93f0210 | 2019-01-20 22:51:37 +0000 | [diff] [blame] | 240 | else |
| 241 | fatal("%s: unknown key type", __func__); |
| 242 | } |
| 243 | |
| 244 | static int |
| 245 | pkcs11_start_helper_methods(void) |
| 246 | { |
Damien Miller | e2cb445 | 2019-01-21 11:32:28 +1100 | [diff] [blame] | 247 | if (helper_rsa != NULL) |
djm@openbsd.org | 93f0210 | 2019-01-20 22:51:37 +0000 | [diff] [blame] | 248 | return (0); |
| 249 | |
Damien Miller | e2cb445 | 2019-01-21 11:32:28 +1100 | [diff] [blame] | 250 | #ifdef HAVE_EC_KEY_METHOD_NEW |
djm@openbsd.org | 93f0210 | 2019-01-20 22:51:37 +0000 | [diff] [blame] | 251 | int (*orig_sign)(int, const unsigned char *, int, unsigned char *, |
| 252 | unsigned int *, const BIGNUM *, const BIGNUM *, EC_KEY *) = NULL; |
| 253 | if (helper_ecdsa != NULL) |
| 254 | return (0); |
| 255 | helper_ecdsa = EC_KEY_METHOD_new(EC_KEY_OpenSSL()); |
| 256 | if (helper_ecdsa == NULL) |
| 257 | return (-1); |
| 258 | EC_KEY_METHOD_get_sign(helper_ecdsa, &orig_sign, NULL, NULL); |
| 259 | EC_KEY_METHOD_set_sign(helper_ecdsa, orig_sign, NULL, ecdsa_do_sign); |
Damien Miller | e2cb445 | 2019-01-21 11:32:28 +1100 | [diff] [blame] | 260 | #endif /* HAVE_EC_KEY_METHOD_NEW */ |
Damien Miller | 7ea845e | 2010-02-12 09:21:02 +1100 | [diff] [blame] | 261 | |
djm@openbsd.org | 482d23b | 2018-09-13 02:08:33 +0000 | [diff] [blame] | 262 | if ((helper_rsa = RSA_meth_dup(RSA_get_default_method())) == NULL) |
| 263 | fatal("%s: RSA_meth_dup failed", __func__); |
| 264 | if (!RSA_meth_set1_name(helper_rsa, "ssh-pkcs11-helper") || |
djm@openbsd.org | 93f0210 | 2019-01-20 22:51:37 +0000 | [diff] [blame] | 265 | !RSA_meth_set_priv_enc(helper_rsa, rsa_encrypt)) |
djm@openbsd.org | 482d23b | 2018-09-13 02:08:33 +0000 | [diff] [blame] | 266 | fatal("%s: failed to prepare method", __func__); |
djm@openbsd.org | 93f0210 | 2019-01-20 22:51:37 +0000 | [diff] [blame] | 267 | |
Damien Miller | 7ea845e | 2010-02-12 09:21:02 +1100 | [diff] [blame] | 268 | return (0); |
| 269 | } |
| 270 | |
| 271 | static int |
| 272 | pkcs11_start_helper(void) |
| 273 | { |
| 274 | int pair[2]; |
djm@openbsd.org | c7670b0 | 2019-01-21 12:53:35 +0000 | [diff] [blame] | 275 | char *helper, *verbosity = NULL; |
| 276 | |
| 277 | if (log_level_get() >= SYSLOG_LEVEL_DEBUG1) |
| 278 | verbosity = "-vvv"; |
Damien Miller | 7ea845e | 2010-02-12 09:21:02 +1100 | [diff] [blame] | 279 | |
djm@openbsd.org | 93f0210 | 2019-01-20 22:51:37 +0000 | [diff] [blame] | 280 | if (pkcs11_start_helper_methods() == -1) { |
| 281 | error("pkcs11_start_helper_methods failed"); |
| 282 | return (-1); |
| 283 | } |
| 284 | |
Damien Miller | 7ea845e | 2010-02-12 09:21:02 +1100 | [diff] [blame] | 285 | if (socketpair(AF_UNIX, SOCK_STREAM, 0, pair) == -1) { |
| 286 | error("socketpair: %s", strerror(errno)); |
| 287 | return (-1); |
| 288 | } |
| 289 | if ((pid = fork()) == -1) { |
| 290 | error("fork: %s", strerror(errno)); |
| 291 | return (-1); |
| 292 | } else if (pid == 0) { |
| 293 | if ((dup2(pair[1], STDIN_FILENO) == -1) || |
| 294 | (dup2(pair[1], STDOUT_FILENO) == -1)) { |
| 295 | fprintf(stderr, "dup2: %s\n", strerror(errno)); |
| 296 | _exit(1); |
| 297 | } |
| 298 | close(pair[0]); |
| 299 | close(pair[1]); |
djm@openbsd.org | 854bd86 | 2019-01-20 22:54:30 +0000 | [diff] [blame] | 300 | helper = getenv("SSH_PKCS11_HELPER"); |
| 301 | if (helper == NULL || strlen(helper) == 0) |
| 302 | helper = _PATH_SSH_PKCS11_HELPER; |
djm@openbsd.org | c7670b0 | 2019-01-21 12:53:35 +0000 | [diff] [blame] | 303 | debug("%s: starting %s %s", __func__, helper, |
| 304 | verbosity == NULL ? "" : verbosity); |
| 305 | execlp(helper, helper, verbosity, (char *)NULL); |
djm@openbsd.org | 854bd86 | 2019-01-20 22:54:30 +0000 | [diff] [blame] | 306 | fprintf(stderr, "exec: %s: %s\n", helper, strerror(errno)); |
Damien Miller | 7ea845e | 2010-02-12 09:21:02 +1100 | [diff] [blame] | 307 | _exit(1); |
| 308 | } |
| 309 | close(pair[1]); |
| 310 | fd = pair[0]; |
| 311 | return (0); |
| 312 | } |
| 313 | |
| 314 | int |
djm@openbsd.org | 89a8d45 | 2020-01-25 00:03:36 +0000 | [diff] [blame] | 315 | pkcs11_add_provider(char *name, char *pin, struct sshkey ***keysp, |
| 316 | char ***labelsp) |
Damien Miller | 7ea845e | 2010-02-12 09:21:02 +1100 | [diff] [blame] | 317 | { |
markus@openbsd.org | 54d90ac | 2017-05-30 08:52:19 +0000 | [diff] [blame] | 318 | struct sshkey *k; |
djm@openbsd.org | 93f0210 | 2019-01-20 22:51:37 +0000 | [diff] [blame] | 319 | int r, type; |
Damien Miller | 7ea845e | 2010-02-12 09:21:02 +1100 | [diff] [blame] | 320 | u_char *blob; |
djm@openbsd.org | 89a8d45 | 2020-01-25 00:03:36 +0000 | [diff] [blame] | 321 | char *label; |
markus@openbsd.org | ff55f4a | 2018-07-09 20:39:28 +0000 | [diff] [blame] | 322 | size_t blen; |
| 323 | u_int nkeys, i; |
| 324 | struct sshbuf *msg; |
Damien Miller | 7ea845e | 2010-02-12 09:21:02 +1100 | [diff] [blame] | 325 | |
| 326 | if (fd < 0 && pkcs11_start_helper() < 0) |
| 327 | return (-1); |
| 328 | |
markus@openbsd.org | ff55f4a | 2018-07-09 20:39:28 +0000 | [diff] [blame] | 329 | if ((msg = sshbuf_new()) == NULL) |
| 330 | fatal("%s: sshbuf_new failed", __func__); |
| 331 | if ((r = sshbuf_put_u8(msg, SSH_AGENTC_ADD_SMARTCARD_KEY)) != 0 || |
| 332 | (r = sshbuf_put_cstring(msg, name)) != 0 || |
| 333 | (r = sshbuf_put_cstring(msg, pin)) != 0) |
| 334 | fatal("%s: buffer error: %s", __func__, ssh_err(r)); |
| 335 | send_msg(msg); |
| 336 | sshbuf_reset(msg); |
Damien Miller | 7ea845e | 2010-02-12 09:21:02 +1100 | [diff] [blame] | 337 | |
djm@openbsd.org | 93f0210 | 2019-01-20 22:51:37 +0000 | [diff] [blame] | 338 | type = recv_msg(msg); |
| 339 | if (type == SSH2_AGENT_IDENTITIES_ANSWER) { |
markus@openbsd.org | ff55f4a | 2018-07-09 20:39:28 +0000 | [diff] [blame] | 340 | if ((r = sshbuf_get_u32(msg, &nkeys)) != 0) |
| 341 | fatal("%s: buffer error: %s", __func__, ssh_err(r)); |
| 342 | *keysp = xcalloc(nkeys, sizeof(struct sshkey *)); |
djm@openbsd.org | 89a8d45 | 2020-01-25 00:03:36 +0000 | [diff] [blame] | 343 | if (labelsp) |
| 344 | *labelsp = xcalloc(nkeys, sizeof(char *)); |
Damien Miller | 7ea845e | 2010-02-12 09:21:02 +1100 | [diff] [blame] | 345 | for (i = 0; i < nkeys; i++) { |
markus@openbsd.org | ff55f4a | 2018-07-09 20:39:28 +0000 | [diff] [blame] | 346 | /* XXX clean up properly instead of fatal() */ |
| 347 | if ((r = sshbuf_get_string(msg, &blob, &blen)) != 0 || |
djm@openbsd.org | 89a8d45 | 2020-01-25 00:03:36 +0000 | [diff] [blame] | 348 | (r = sshbuf_get_cstring(msg, &label, NULL)) != 0) |
markus@openbsd.org | ff55f4a | 2018-07-09 20:39:28 +0000 | [diff] [blame] | 349 | fatal("%s: buffer error: %s", |
| 350 | __func__, ssh_err(r)); |
| 351 | if ((r = sshkey_from_blob(blob, blen, &k)) != 0) |
| 352 | fatal("%s: bad key: %s", __func__, ssh_err(r)); |
djm@openbsd.org | 93f0210 | 2019-01-20 22:51:37 +0000 | [diff] [blame] | 353 | wrap_key(k); |
Damien Miller | 7ea845e | 2010-02-12 09:21:02 +1100 | [diff] [blame] | 354 | (*keysp)[i] = k; |
djm@openbsd.org | 89a8d45 | 2020-01-25 00:03:36 +0000 | [diff] [blame] | 355 | if (labelsp) |
| 356 | (*labelsp)[i] = label; |
| 357 | else |
| 358 | free(label); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 359 | free(blob); |
Damien Miller | 7ea845e | 2010-02-12 09:21:02 +1100 | [diff] [blame] | 360 | } |
djm@openbsd.org | 93f0210 | 2019-01-20 22:51:37 +0000 | [diff] [blame] | 361 | } else if (type == SSH2_AGENT_FAILURE) { |
| 362 | if ((r = sshbuf_get_u32(msg, &nkeys)) != 0) |
| 363 | nkeys = -1; |
Damien Miller | 7ea845e | 2010-02-12 09:21:02 +1100 | [diff] [blame] | 364 | } else { |
| 365 | nkeys = -1; |
| 366 | } |
markus@openbsd.org | ff55f4a | 2018-07-09 20:39:28 +0000 | [diff] [blame] | 367 | sshbuf_free(msg); |
Damien Miller | 7ea845e | 2010-02-12 09:21:02 +1100 | [diff] [blame] | 368 | return (nkeys); |
| 369 | } |
| 370 | |
| 371 | int |
| 372 | pkcs11_del_provider(char *name) |
| 373 | { |
markus@openbsd.org | ff55f4a | 2018-07-09 20:39:28 +0000 | [diff] [blame] | 374 | int r, ret = -1; |
| 375 | struct sshbuf *msg; |
Damien Miller | 7ea845e | 2010-02-12 09:21:02 +1100 | [diff] [blame] | 376 | |
markus@openbsd.org | ff55f4a | 2018-07-09 20:39:28 +0000 | [diff] [blame] | 377 | if ((msg = sshbuf_new()) == NULL) |
| 378 | fatal("%s: sshbuf_new failed", __func__); |
| 379 | if ((r = sshbuf_put_u8(msg, SSH_AGENTC_REMOVE_SMARTCARD_KEY)) != 0 || |
| 380 | (r = sshbuf_put_cstring(msg, name)) != 0 || |
| 381 | (r = sshbuf_put_cstring(msg, "")) != 0) |
| 382 | fatal("%s: buffer error: %s", __func__, ssh_err(r)); |
| 383 | send_msg(msg); |
| 384 | sshbuf_reset(msg); |
Damien Miller | 7ea845e | 2010-02-12 09:21:02 +1100 | [diff] [blame] | 385 | |
markus@openbsd.org | ff55f4a | 2018-07-09 20:39:28 +0000 | [diff] [blame] | 386 | if (recv_msg(msg) == SSH_AGENT_SUCCESS) |
Damien Miller | 7ea845e | 2010-02-12 09:21:02 +1100 | [diff] [blame] | 387 | ret = 0; |
markus@openbsd.org | ff55f4a | 2018-07-09 20:39:28 +0000 | [diff] [blame] | 388 | sshbuf_free(msg); |
Damien Miller | 7ea845e | 2010-02-12 09:21:02 +1100 | [diff] [blame] | 389 | return (ret); |
| 390 | } |
Damien Miller | dfa4156 | 2010-02-12 10:06:28 +1100 | [diff] [blame] | 391 | |
| 392 | #endif /* ENABLE_PKCS11 */ |