Fixed the build error for gcc-14
[platform/upstream/openssh.git] / ssh-pkcs11-client.c
1 /* $OpenBSD: ssh-pkcs11-client.c,v 1.8 2018/02/05 05:37:46 tb 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 "includes.h"
19
20 #ifdef ENABLE_PKCS11
21
22 #include <sys/types.h>
23 #ifdef HAVE_SYS_TIME_H
24 # include <sys/time.h>
25 #endif
26 #include <sys/socket.h>
27
28 #include <stdarg.h>
29 #include <string.h>
30 #include <unistd.h>
31 #include <errno.h>
32
33 #include <openssl/rsa.h>
34
35 #include "pathnames.h"
36 #include "xmalloc.h"
37 #include "buffer.h"
38 #include "log.h"
39 #include "misc.h"
40 #include "key.h"
41 #include "authfd.h"
42 #include "atomicio.h"
43 #include "ssh-pkcs11.h"
44
45 /* borrows code from sftp-server and ssh-agent */
46
47 int fd = -1;
48 pid_t pid = -1;
49
50 static void
51 send_msg(Buffer *m)
52 {
53         u_char buf[4];
54         int mlen = buffer_len(m);
55
56         put_u32(buf, mlen);
57         if (atomicio(vwrite, fd, buf, 4) != 4 ||
58             atomicio(vwrite, fd, buffer_ptr(m),
59             buffer_len(m)) != buffer_len(m))
60                 error("write to helper failed");
61         buffer_consume(m, mlen);
62 }
63
64 static int
65 recv_msg(Buffer *m)
66 {
67         u_int l, len;
68         u_char buf[1024];
69
70         if ((len = atomicio(read, fd, buf, 4)) != 4) {
71                 error("read from helper failed: %u", len);
72                 return (0); /* XXX */
73         }
74         len = get_u32(buf);
75         if (len > 256 * 1024)
76                 fatal("response too long: %u", len);
77         /* read len bytes into m */
78         buffer_clear(m);
79         while (len > 0) {
80                 l = len;
81                 if (l > sizeof(buf))
82                         l = sizeof(buf);
83                 if (atomicio(read, fd, buf, l) != l) {
84                         error("response from helper failed.");
85                         return (0); /* XXX */
86                 }
87                 buffer_append(m, buf, l);
88                 len -= l;
89         }
90         return (buffer_get_char(m));
91 }
92
93 int
94 pkcs11_init(int interactive)
95 {
96         return (0);
97 }
98
99 void
100 pkcs11_terminate(void)
101 {
102         if (fd >= 0)
103                 close(fd);
104 }
105
106 static int
107 pkcs11_rsa_private_encrypt(int flen, const u_char *from, u_char *to, RSA *rsa,
108     int padding)
109 {
110         struct sshkey key;      /* XXX */
111         u_char *blob, *signature = NULL;
112         u_int blen, slen = 0;
113         int ret = -1;
114         Buffer msg;
115
116         if (padding != RSA_PKCS1_PADDING)
117                 return (-1);
118         key.type = KEY_RSA;
119         key.rsa = rsa;
120         if (key_to_blob(&key, &blob, &blen) == 0)
121                 return -1;
122         buffer_init(&msg);
123         buffer_put_char(&msg, SSH2_AGENTC_SIGN_REQUEST);
124         buffer_put_string(&msg, blob, blen);
125         buffer_put_string(&msg, from, flen);
126         buffer_put_int(&msg, 0);
127         free(blob);
128         send_msg(&msg);
129         buffer_clear(&msg);
130
131         if (recv_msg(&msg) == SSH2_AGENT_SIGN_RESPONSE) {
132                 signature = buffer_get_string(&msg, &slen);
133                 if (slen <= (u_int)RSA_size(rsa)) {
134                         memcpy(to, signature, slen);
135                         ret = slen;
136                 }
137                 free(signature);
138         }
139         buffer_free(&msg);
140         return (ret);
141 }
142
143 /* redirect the private key encrypt operation to the ssh-pkcs11-helper */
144 static int
145 wrap_key(RSA *rsa)
146 {
147         static RSA_METHOD *helper_rsa;
148
149         if ((helper_rsa = RSA_meth_dup(RSA_get_default_method())) == NULL)
150                 return (-1); /* XXX but caller isn't checking */
151         RSA_meth_set1_name(helper_rsa, "ssh-pkcs11-helper");
152         RSA_meth_set_priv_enc(helper_rsa, pkcs11_rsa_private_encrypt);
153         RSA_set_method(rsa, helper_rsa);
154         return (0);
155 }
156
157 static int
158 pkcs11_start_helper(void)
159 {
160         int pair[2];
161
162         if (socketpair(AF_UNIX, SOCK_STREAM, 0, pair) == -1) {
163                 error("socketpair: %s", strerror(errno));
164                 return (-1);
165         }
166         if ((pid = fork()) == -1) {
167                 error("fork: %s", strerror(errno));
168                 return (-1);
169         } else if (pid == 0) {
170                 if ((dup2(pair[1], STDIN_FILENO) == -1) ||
171                     (dup2(pair[1], STDOUT_FILENO) == -1)) {
172                         fprintf(stderr, "dup2: %s\n", strerror(errno));
173                         _exit(1);
174                 }
175                 close(pair[0]);
176                 close(pair[1]);
177                 execlp(_PATH_SSH_PKCS11_HELPER, _PATH_SSH_PKCS11_HELPER,
178                     (char *)NULL);
179                 fprintf(stderr, "exec: %s: %s\n", _PATH_SSH_PKCS11_HELPER,
180                     strerror(errno));
181                 _exit(1);
182         }
183         close(pair[1]);
184         fd = pair[0];
185         return (0);
186 }
187
188 int
189 pkcs11_add_provider(char *name, char *pin, Key ***keysp)
190 {
191         struct sshkey *k;
192         int i, nkeys;
193         u_char *blob;
194         u_int blen;
195         Buffer msg;
196
197         if (fd < 0 && pkcs11_start_helper() < 0)
198                 return (-1);
199
200         buffer_init(&msg);
201         buffer_put_char(&msg, SSH_AGENTC_ADD_SMARTCARD_KEY);
202         buffer_put_cstring(&msg, name);
203         buffer_put_cstring(&msg, pin);
204         send_msg(&msg);
205         buffer_clear(&msg);
206
207         if (recv_msg(&msg) == SSH2_AGENT_IDENTITIES_ANSWER) {
208                 nkeys = buffer_get_int(&msg);
209                 *keysp = xcalloc(nkeys, sizeof(Key *));
210                 for (i = 0; i < nkeys; i++) {
211                         blob = buffer_get_string(&msg, &blen);
212                         free(buffer_get_string(&msg, NULL));
213                         k = key_from_blob(blob, blen);
214                         wrap_key(k->rsa);
215                         (*keysp)[i] = k;
216                         free(blob);
217                 }
218         } else {
219                 nkeys = -1;
220         }
221         buffer_free(&msg);
222         return (nkeys);
223 }
224
225 int
226 pkcs11_del_provider(char *name)
227 {
228         int ret = -1;
229         Buffer msg;
230
231         buffer_init(&msg);
232         buffer_put_char(&msg, SSH_AGENTC_REMOVE_SMARTCARD_KEY);
233         buffer_put_cstring(&msg, name);
234         buffer_put_cstring(&msg, "");
235         send_msg(&msg);
236         buffer_clear(&msg);
237
238         if (recv_msg(&msg) == SSH_AGENT_SUCCESS)
239                 ret = 0;
240         buffer_free(&msg);
241         return (ret);
242 }
243
244 #endif /* ENABLE_PKCS11 */