ssh-pkcs11-client.c 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  1. /* $OpenBSD: ssh-pkcs11-client.c,v 1.16 2020/01/25 00:03:36 djm Exp $ */
  2. /*
  3. * Copyright (c) 2010 Markus Friedl. All rights reserved.
  4. * Copyright (c) 2014 Pedro Martelletto. All rights reserved.
  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. #include "includes.h"
  19. #ifdef ENABLE_PKCS11
  20. #include <sys/types.h>
  21. #ifdef HAVE_SYS_TIME_H
  22. # include <sys/time.h>
  23. #endif
  24. #include <sys/socket.h>
  25. #include <stdarg.h>
  26. #include <string.h>
  27. #include <unistd.h>
  28. #include <errno.h>
  29. #include <openssl/ecdsa.h>
  30. #include <openssl/rsa.h>
  31. #include "openbsd-compat/openssl-compat.h"
  32. #include "pathnames.h"
  33. #include "xmalloc.h"
  34. #include "sshbuf.h"
  35. #include "log.h"
  36. #include "misc.h"
  37. #include "sshkey.h"
  38. #include "authfd.h"
  39. #include "atomicio.h"
  40. #include "ssh-pkcs11.h"
  41. #include "ssherr.h"
  42. /* borrows code from sftp-server and ssh-agent */
  43. static int fd = -1;
  44. static pid_t pid = -1;
  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. POKE_U32(buf, mlen);
  52. if (atomicio(vwrite, fd, buf, 4) != 4 ||
  53. atomicio(vwrite, fd, sshbuf_mutable_ptr(m),
  54. sshbuf_len(m)) != sshbuf_len(m))
  55. error("write to helper failed");
  56. if ((r = sshbuf_consume(m, mlen)) != 0)
  57. fatal("%s: buffer error: %s", __func__, ssh_err(r));
  58. }
  59. static int
  60. recv_msg(struct sshbuf *m)
  61. {
  62. u_int l, len;
  63. u_char c, buf[1024];
  64. int r;
  65. if ((len = atomicio(read, fd, buf, 4)) != 4) {
  66. error("read from helper failed: %u", len);
  67. return (0); /* XXX */
  68. }
  69. len = PEEK_U32(buf);
  70. if (len > 256 * 1024)
  71. fatal("response too long: %u", len);
  72. /* read len bytes into m */
  73. sshbuf_reset(m);
  74. while (len > 0) {
  75. l = len;
  76. if (l > sizeof(buf))
  77. l = sizeof(buf);
  78. if (atomicio(read, fd, buf, l) != l) {
  79. error("response from helper failed.");
  80. return (0); /* XXX */
  81. }
  82. if ((r = sshbuf_put(m, buf, l)) != 0)
  83. fatal("%s: buffer error: %s", __func__, ssh_err(r));
  84. len -= l;
  85. }
  86. if ((r = sshbuf_get_u8(m, &c)) != 0)
  87. fatal("%s: buffer error: %s", __func__, ssh_err(r));
  88. return c;
  89. }
  90. int
  91. pkcs11_init(int interactive)
  92. {
  93. return (0);
  94. }
  95. void
  96. pkcs11_terminate(void)
  97. {
  98. if (fd >= 0)
  99. close(fd);
  100. }
  101. static int
  102. rsa_encrypt(int flen, const u_char *from, u_char *to, RSA *rsa, int padding)
  103. {
  104. struct sshkey *key = NULL;
  105. struct sshbuf *msg = NULL;
  106. u_char *blob = NULL, *signature = NULL;
  107. size_t blen, slen = 0;
  108. int r, ret = -1;
  109. if (padding != RSA_PKCS1_PADDING)
  110. goto fail;
  111. key = sshkey_new(KEY_UNSPEC);
  112. if (key == NULL) {
  113. error("%s: sshkey_new failed", __func__);
  114. goto fail;
  115. }
  116. key->type = KEY_RSA;
  117. RSA_up_ref(rsa);
  118. key->rsa = rsa;
  119. if ((r = sshkey_to_blob(key, &blob, &blen)) != 0) {
  120. error("%s: sshkey_to_blob: %s", __func__, ssh_err(r));
  121. goto fail;
  122. }
  123. if ((msg = sshbuf_new()) == NULL)
  124. fatal("%s: sshbuf_new failed", __func__);
  125. if ((r = sshbuf_put_u8(msg, SSH2_AGENTC_SIGN_REQUEST)) != 0 ||
  126. (r = sshbuf_put_string(msg, blob, blen)) != 0 ||
  127. (r = sshbuf_put_string(msg, from, flen)) != 0 ||
  128. (r = sshbuf_put_u32(msg, 0)) != 0)
  129. fatal("%s: buffer error: %s", __func__, ssh_err(r));
  130. send_msg(msg);
  131. sshbuf_reset(msg);
  132. if (recv_msg(msg) == SSH2_AGENT_SIGN_RESPONSE) {
  133. if ((r = sshbuf_get_string(msg, &signature, &slen)) != 0)
  134. fatal("%s: buffer error: %s", __func__, ssh_err(r));
  135. if (slen <= (size_t)RSA_size(rsa)) {
  136. memcpy(to, signature, slen);
  137. ret = slen;
  138. }
  139. free(signature);
  140. }
  141. fail:
  142. free(blob);
  143. sshkey_free(key);
  144. sshbuf_free(msg);
  145. return (ret);
  146. }
  147. #ifdef HAVE_EC_KEY_METHOD_NEW
  148. static ECDSA_SIG *
  149. ecdsa_do_sign(const unsigned char *dgst, int dgst_len, const BIGNUM *inv,
  150. const BIGNUM *rp, EC_KEY *ec)
  151. {
  152. struct sshkey *key = NULL;
  153. struct sshbuf *msg = NULL;
  154. ECDSA_SIG *ret = NULL;
  155. const u_char *cp;
  156. u_char *blob = NULL, *signature = NULL;
  157. size_t blen, slen = 0;
  158. int r, nid;
  159. nid = sshkey_ecdsa_key_to_nid(ec);
  160. if (nid < 0) {
  161. error("%s: couldn't get curve nid", __func__);
  162. goto fail;
  163. }
  164. key = sshkey_new(KEY_UNSPEC);
  165. if (key == NULL) {
  166. error("%s: sshkey_new failed", __func__);
  167. goto fail;
  168. }
  169. key->ecdsa = ec;
  170. key->ecdsa_nid = nid;
  171. key->type = KEY_ECDSA;
  172. EC_KEY_up_ref(ec);
  173. if ((r = sshkey_to_blob(key, &blob, &blen)) != 0) {
  174. error("%s: sshkey_to_blob: %s", __func__, ssh_err(r));
  175. goto fail;
  176. }
  177. if ((msg = sshbuf_new()) == NULL)
  178. fatal("%s: sshbuf_new failed", __func__);
  179. if ((r = sshbuf_put_u8(msg, SSH2_AGENTC_SIGN_REQUEST)) != 0 ||
  180. (r = sshbuf_put_string(msg, blob, blen)) != 0 ||
  181. (r = sshbuf_put_string(msg, dgst, dgst_len)) != 0 ||
  182. (r = sshbuf_put_u32(msg, 0)) != 0)
  183. fatal("%s: buffer error: %s", __func__, ssh_err(r));
  184. send_msg(msg);
  185. sshbuf_reset(msg);
  186. if (recv_msg(msg) == SSH2_AGENT_SIGN_RESPONSE) {
  187. if ((r = sshbuf_get_string(msg, &signature, &slen)) != 0)
  188. fatal("%s: buffer error: %s", __func__, ssh_err(r));
  189. cp = signature;
  190. ret = d2i_ECDSA_SIG(NULL, &cp, slen);
  191. free(signature);
  192. }
  193. fail:
  194. free(blob);
  195. sshkey_free(key);
  196. sshbuf_free(msg);
  197. return (ret);
  198. }
  199. #endif /* HAVE_EC_KEY_METHOD_NEW */
  200. static RSA_METHOD *helper_rsa;
  201. #ifdef HAVE_EC_KEY_METHOD_NEW
  202. static EC_KEY_METHOD *helper_ecdsa;
  203. #endif /* HAVE_EC_KEY_METHOD_NEW */
  204. /* redirect private key crypto operations to the ssh-pkcs11-helper */
  205. static void
  206. wrap_key(struct sshkey *k)
  207. {
  208. if (k->type == KEY_RSA)
  209. RSA_set_method(k->rsa, helper_rsa);
  210. #ifdef HAVE_EC_KEY_METHOD_NEW
  211. else if (k->type == KEY_ECDSA)
  212. EC_KEY_set_method(k->ecdsa, helper_ecdsa);
  213. #endif /* HAVE_EC_KEY_METHOD_NEW */
  214. else
  215. fatal("%s: unknown key type", __func__);
  216. }
  217. static int
  218. pkcs11_start_helper_methods(void)
  219. {
  220. if (helper_rsa != NULL)
  221. return (0);
  222. #ifdef HAVE_EC_KEY_METHOD_NEW
  223. int (*orig_sign)(int, const unsigned char *, int, unsigned char *,
  224. unsigned int *, const BIGNUM *, const BIGNUM *, EC_KEY *) = NULL;
  225. if (helper_ecdsa != NULL)
  226. return (0);
  227. helper_ecdsa = EC_KEY_METHOD_new(EC_KEY_OpenSSL());
  228. if (helper_ecdsa == NULL)
  229. return (-1);
  230. EC_KEY_METHOD_get_sign(helper_ecdsa, &orig_sign, NULL, NULL);
  231. EC_KEY_METHOD_set_sign(helper_ecdsa, orig_sign, NULL, ecdsa_do_sign);
  232. #endif /* HAVE_EC_KEY_METHOD_NEW */
  233. if ((helper_rsa = RSA_meth_dup(RSA_get_default_method())) == NULL)
  234. fatal("%s: RSA_meth_dup failed", __func__);
  235. if (!RSA_meth_set1_name(helper_rsa, "ssh-pkcs11-helper") ||
  236. !RSA_meth_set_priv_enc(helper_rsa, rsa_encrypt))
  237. fatal("%s: failed to prepare method", __func__);
  238. return (0);
  239. }
  240. static int
  241. pkcs11_start_helper(void)
  242. {
  243. int pair[2];
  244. char *helper, *verbosity = NULL;
  245. if (log_level_get() >= SYSLOG_LEVEL_DEBUG1)
  246. verbosity = "-vvv";
  247. if (pkcs11_start_helper_methods() == -1) {
  248. error("pkcs11_start_helper_methods failed");
  249. return (-1);
  250. }
  251. if (socketpair(AF_UNIX, SOCK_STREAM, 0, pair) == -1) {
  252. error("socketpair: %s", strerror(errno));
  253. return (-1);
  254. }
  255. if ((pid = fork()) == -1) {
  256. error("fork: %s", strerror(errno));
  257. return (-1);
  258. } else if (pid == 0) {
  259. if ((dup2(pair[1], STDIN_FILENO) == -1) ||
  260. (dup2(pair[1], STDOUT_FILENO) == -1)) {
  261. fprintf(stderr, "dup2: %s\n", strerror(errno));
  262. _exit(1);
  263. }
  264. close(pair[0]);
  265. close(pair[1]);
  266. helper = getenv("SSH_PKCS11_HELPER");
  267. if (helper == NULL || strlen(helper) == 0)
  268. helper = _PATH_SSH_PKCS11_HELPER;
  269. debug("%s: starting %s %s", __func__, helper,
  270. verbosity == NULL ? "" : verbosity);
  271. execlp(helper, helper, verbosity, (char *)NULL);
  272. fprintf(stderr, "exec: %s: %s\n", helper, strerror(errno));
  273. _exit(1);
  274. }
  275. close(pair[1]);
  276. fd = pair[0];
  277. return (0);
  278. }
  279. int
  280. pkcs11_add_provider(char *name, char *pin, struct sshkey ***keysp,
  281. char ***labelsp)
  282. {
  283. struct sshkey *k;
  284. int r, type;
  285. u_char *blob;
  286. char *label;
  287. size_t blen;
  288. u_int nkeys, i;
  289. struct sshbuf *msg;
  290. debug("%s: called, name = %s", __func__, name);
  291. if (fd < 0 && pkcs11_start_helper() < 0)
  292. return (-1);
  293. if ((msg = sshbuf_new()) == NULL)
  294. fatal("%s: sshbuf_new failed", __func__);
  295. if ((r = sshbuf_put_u8(msg, SSH_AGENTC_ADD_SMARTCARD_KEY)) != 0 ||
  296. (r = sshbuf_put_cstring(msg, name)) != 0 ||
  297. (r = sshbuf_put_cstring(msg, pin)) != 0)
  298. fatal("%s: buffer error: %s", __func__, ssh_err(r));
  299. send_msg(msg);
  300. sshbuf_reset(msg);
  301. type = recv_msg(msg);
  302. if (type == SSH2_AGENT_IDENTITIES_ANSWER) {
  303. if ((r = sshbuf_get_u32(msg, &nkeys)) != 0)
  304. fatal("%s: buffer error: %s", __func__, ssh_err(r));
  305. *keysp = xcalloc(nkeys, sizeof(struct sshkey *));
  306. if (labelsp)
  307. *labelsp = xcalloc(nkeys, sizeof(char *));
  308. debug("%s: nkeys = %u", __func__, nkeys);
  309. for (i = 0; i < nkeys; i++) {
  310. /* XXX clean up properly instead of fatal() */
  311. if ((r = sshbuf_get_string(msg, &blob, &blen)) != 0 ||
  312. (r = sshbuf_get_cstring(msg, &label, NULL)) != 0)
  313. fatal("%s: buffer error: %s",
  314. __func__, ssh_err(r));
  315. if ((r = sshkey_from_blob(blob, blen, &k)) != 0)
  316. fatal("%s: bad key: %s", __func__, ssh_err(r));
  317. wrap_key(k);
  318. (*keysp)[i] = k;
  319. if (labelsp)
  320. (*labelsp)[i] = label;
  321. else
  322. free(label);
  323. free(blob);
  324. }
  325. } else if (type == SSH2_AGENT_FAILURE) {
  326. if ((r = sshbuf_get_u32(msg, &nkeys)) != 0)
  327. nkeys = -1;
  328. } else {
  329. nkeys = -1;
  330. }
  331. sshbuf_free(msg);
  332. return (nkeys);
  333. }
  334. int
  335. pkcs11_del_provider(char *name)
  336. {
  337. int r, ret = -1;
  338. struct sshbuf *msg;
  339. if ((msg = sshbuf_new()) == NULL)
  340. fatal("%s: sshbuf_new failed", __func__);
  341. if ((r = sshbuf_put_u8(msg, SSH_AGENTC_REMOVE_SMARTCARD_KEY)) != 0 ||
  342. (r = sshbuf_put_cstring(msg, name)) != 0 ||
  343. (r = sshbuf_put_cstring(msg, "")) != 0)
  344. fatal("%s: buffer error: %s", __func__, ssh_err(r));
  345. send_msg(msg);
  346. sshbuf_reset(msg);
  347. if (recv_msg(msg) == SSH_AGENT_SUCCESS)
  348. ret = 0;
  349. sshbuf_free(msg);
  350. return (ret);
  351. }
  352. #endif /* ENABLE_PKCS11 */