1 /* $OpenBSD: ssh-pkcs11-client.c,v 1.11 2018/09/13 02:08:33 djm Exp $ */ 2 /* 3 * Copyright (c) 2010 Markus Friedl. All rights reserved. 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 #include <sys/types.h> 19 #include <sys/time.h> 20 #include <sys/socket.h> 21 22 #include <stdarg.h> 23 #include <string.h> 24 #include <unistd.h> 25 #include <errno.h> 26 27 #include <openssl/rsa.h> 28 29 #include "pathnames.h" 30 #include "xmalloc.h" 31 #include "sshbuf.h" 32 #include "log.h" 33 #include "misc.h" 34 #include "sshkey.h" 35 #include "authfd.h" 36 #include "atomicio.h" 37 #include "ssh-pkcs11.h" 38 #include "ssherr.h" 39 40 /* borrows code from sftp-server and ssh-agent */ 41 42 int fd = -1; 43 pid_t pid = -1; 44 45 static void 46 send_msg(struct sshbuf *m) 47 { 48 u_char buf[4]; 49 size_t mlen = sshbuf_len(m); 50 int r; 51 52 POKE_U32(buf, mlen); 53 if (atomicio(vwrite, fd, buf, 4) != 4 || 54 atomicio(vwrite, fd, sshbuf_mutable_ptr(m), 55 sshbuf_len(m)) != sshbuf_len(m)) 56 error("write to helper failed"); 57 if ((r = sshbuf_consume(m, mlen)) != 0) 58 fatal("%s: buffer error: %s", __func__, ssh_err(r)); 59 } 60 61 static int 62 recv_msg(struct sshbuf *m) 63 { 64 u_int l, len; 65 u_char c, buf[1024]; 66 int r; 67 68 if ((len = atomicio(read, fd, buf, 4)) != 4) { 69 error("read from helper failed: %u", len); 70 return (0); /* XXX */ 71 } 72 len = PEEK_U32(buf); 73 if (len > 256 * 1024) 74 fatal("response too long: %u", len); 75 /* read len bytes into m */ 76 sshbuf_reset(m); 77 while (len > 0) { 78 l = len; 79 if (l > sizeof(buf)) 80 l = sizeof(buf); 81 if (atomicio(read, fd, buf, l) != l) { 82 error("response from helper failed."); 83 return (0); /* XXX */ 84 } 85 if ((r = sshbuf_put(m, buf, l)) != 0) 86 fatal("%s: buffer error: %s", __func__, ssh_err(r)); 87 len -= l; 88 } 89 if ((r = sshbuf_get_u8(m, &c)) != 0) 90 fatal("%s: buffer error: %s", __func__, ssh_err(r)); 91 return c; 92 } 93 94 int 95 pkcs11_init(int interactive) 96 { 97 return (0); 98 } 99 100 void 101 pkcs11_terminate(void) 102 { 103 if (fd >= 0) 104 close(fd); 105 } 106 107 static int 108 pkcs11_rsa_private_encrypt(int flen, const u_char *from, u_char *to, RSA *rsa, 109 int padding) 110 { 111 struct sshkey key; /* XXX */ 112 u_char *blob, *signature = NULL; 113 size_t blen, slen = 0; 114 int r, ret = -1; 115 struct sshbuf *msg; 116 117 if (padding != RSA_PKCS1_PADDING) 118 return (-1); 119 key.type = KEY_RSA; 120 key.rsa = rsa; 121 if ((r = sshkey_to_blob(&key, &blob, &blen)) != 0) { 122 error("%s: sshkey_to_blob: %s", __func__, ssh_err(r)); 123 return -1; 124 } 125 if ((msg = sshbuf_new()) == NULL) 126 fatal("%s: sshbuf_new failed", __func__); 127 if ((r = sshbuf_put_u8(msg, SSH2_AGENTC_SIGN_REQUEST)) != 0 || 128 (r = sshbuf_put_string(msg, blob, blen)) != 0 || 129 (r = sshbuf_put_string(msg, from, flen)) != 0 || 130 (r = sshbuf_put_u32(msg, 0)) != 0) 131 fatal("%s: buffer error: %s", __func__, ssh_err(r)); 132 free(blob); 133 send_msg(msg); 134 sshbuf_reset(msg); 135 136 if (recv_msg(msg) == SSH2_AGENT_SIGN_RESPONSE) { 137 if ((r = sshbuf_get_string(msg, &signature, &slen)) != 0) 138 fatal("%s: buffer error: %s", __func__, ssh_err(r)); 139 if (slen <= (size_t)RSA_size(rsa)) { 140 memcpy(to, signature, slen); 141 ret = slen; 142 } 143 free(signature); 144 } 145 sshbuf_free(msg); 146 return (ret); 147 } 148 149 /* redirect the private key encrypt operation to the ssh-pkcs11-helper */ 150 static int 151 wrap_key(RSA *rsa) 152 { 153 static RSA_METHOD *helper_rsa; 154 155 if ((helper_rsa = RSA_meth_dup(RSA_get_default_method())) == NULL) 156 fatal("%s: RSA_meth_dup failed", __func__); 157 if (!RSA_meth_set1_name(helper_rsa, "ssh-pkcs11-helper") || 158 !RSA_meth_set_priv_enc(helper_rsa, pkcs11_rsa_private_encrypt)) 159 fatal("%s: failed to prepare method", __func__); 160 RSA_set_method(rsa, helper_rsa); 161 return (0); 162 } 163 164 static int 165 pkcs11_start_helper(void) 166 { 167 int pair[2]; 168 169 if (socketpair(AF_UNIX, SOCK_STREAM, 0, pair) == -1) { 170 error("socketpair: %s", strerror(errno)); 171 return (-1); 172 } 173 if ((pid = fork()) == -1) { 174 error("fork: %s", strerror(errno)); 175 return (-1); 176 } else if (pid == 0) { 177 if ((dup2(pair[1], STDIN_FILENO) == -1) || 178 (dup2(pair[1], STDOUT_FILENO) == -1)) { 179 fprintf(stderr, "dup2: %s\n", strerror(errno)); 180 _exit(1); 181 } 182 close(pair[0]); 183 close(pair[1]); 184 execlp(_PATH_SSH_PKCS11_HELPER, _PATH_SSH_PKCS11_HELPER, 185 (char *)NULL); 186 fprintf(stderr, "exec: %s: %s\n", _PATH_SSH_PKCS11_HELPER, 187 strerror(errno)); 188 _exit(1); 189 } 190 close(pair[1]); 191 fd = pair[0]; 192 return (0); 193 } 194 195 int 196 pkcs11_add_provider(char *name, char *pin, struct sshkey ***keysp) 197 { 198 struct sshkey *k; 199 int r; 200 u_char *blob; 201 size_t blen; 202 u_int nkeys, i; 203 struct sshbuf *msg; 204 205 if (fd < 0 && pkcs11_start_helper() < 0) 206 return (-1); 207 208 if ((msg = sshbuf_new()) == NULL) 209 fatal("%s: sshbuf_new failed", __func__); 210 if ((r = sshbuf_put_u8(msg, SSH_AGENTC_ADD_SMARTCARD_KEY)) != 0 || 211 (r = sshbuf_put_cstring(msg, name)) != 0 || 212 (r = sshbuf_put_cstring(msg, pin)) != 0) 213 fatal("%s: buffer error: %s", __func__, ssh_err(r)); 214 send_msg(msg); 215 sshbuf_reset(msg); 216 217 if (recv_msg(msg) == SSH2_AGENT_IDENTITIES_ANSWER) { 218 if ((r = sshbuf_get_u32(msg, &nkeys)) != 0) 219 fatal("%s: buffer error: %s", __func__, ssh_err(r)); 220 *keysp = xcalloc(nkeys, sizeof(struct sshkey *)); 221 for (i = 0; i < nkeys; i++) { 222 /* XXX clean up properly instead of fatal() */ 223 if ((r = sshbuf_get_string(msg, &blob, &blen)) != 0 || 224 (r = sshbuf_skip_string(msg)) != 0) 225 fatal("%s: buffer error: %s", 226 __func__, ssh_err(r)); 227 if ((r = sshkey_from_blob(blob, blen, &k)) != 0) 228 fatal("%s: bad key: %s", __func__, ssh_err(r)); 229 wrap_key(k->rsa); 230 (*keysp)[i] = k; 231 free(blob); 232 } 233 } else { 234 nkeys = -1; 235 } 236 sshbuf_free(msg); 237 return (nkeys); 238 } 239 240 int 241 pkcs11_del_provider(char *name) 242 { 243 int r, ret = -1; 244 struct sshbuf *msg; 245 246 if ((msg = sshbuf_new()) == NULL) 247 fatal("%s: sshbuf_new failed", __func__); 248 if ((r = sshbuf_put_u8(msg, SSH_AGENTC_REMOVE_SMARTCARD_KEY)) != 0 || 249 (r = sshbuf_put_cstring(msg, name)) != 0 || 250 (r = sshbuf_put_cstring(msg, "")) != 0) 251 fatal("%s: buffer error: %s", __func__, ssh_err(r)); 252 send_msg(msg); 253 sshbuf_reset(msg); 254 255 if (recv_msg(msg) == SSH_AGENT_SUCCESS) 256 ret = 0; 257 sshbuf_free(msg); 258 return (ret); 259 } 260