1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
3*0Sstevel@tonic-gate * Use is subject to license terms.
4*0Sstevel@tonic-gate */
5*0Sstevel@tonic-gate /*
6*0Sstevel@tonic-gate * Copyright (c) 2002 Markus Friedl. All rights reserved.
7*0Sstevel@tonic-gate *
8*0Sstevel@tonic-gate * Redistribution and use in source and binary forms, with or without
9*0Sstevel@tonic-gate * modification, are permitted provided that the following conditions
10*0Sstevel@tonic-gate * are met:
11*0Sstevel@tonic-gate * 1. Redistributions of source code must retain the above copyright
12*0Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer.
13*0Sstevel@tonic-gate * 2. Redistributions in binary form must reproduce the above copyright
14*0Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer in the
15*0Sstevel@tonic-gate * documentation and/or other materials provided with the distribution.
16*0Sstevel@tonic-gate *
17*0Sstevel@tonic-gate * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18*0Sstevel@tonic-gate * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19*0Sstevel@tonic-gate * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20*0Sstevel@tonic-gate * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21*0Sstevel@tonic-gate * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22*0Sstevel@tonic-gate * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23*0Sstevel@tonic-gate * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24*0Sstevel@tonic-gate * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25*0Sstevel@tonic-gate * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26*0Sstevel@tonic-gate * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27*0Sstevel@tonic-gate */
28*0Sstevel@tonic-gate #include "includes.h"
29*0Sstevel@tonic-gate RCSID("$OpenBSD: ssh-keysign.c,v 1.7 2002/07/03 14:21:05 markus Exp $");
30*0Sstevel@tonic-gate
31*0Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
32*0Sstevel@tonic-gate
33*0Sstevel@tonic-gate #include <unistd.h>
34*0Sstevel@tonic-gate
35*0Sstevel@tonic-gate #include <openssl/evp.h>
36*0Sstevel@tonic-gate #include <openssl/rand.h>
37*0Sstevel@tonic-gate #include <openssl/rsa.h>
38*0Sstevel@tonic-gate
39*0Sstevel@tonic-gate #include "log.h"
40*0Sstevel@tonic-gate #include "key.h"
41*0Sstevel@tonic-gate #include "ssh.h"
42*0Sstevel@tonic-gate #include "ssh2.h"
43*0Sstevel@tonic-gate #include "misc.h"
44*0Sstevel@tonic-gate #include "xmalloc.h"
45*0Sstevel@tonic-gate #include "buffer.h"
46*0Sstevel@tonic-gate #include "bufaux.h"
47*0Sstevel@tonic-gate #include "authfile.h"
48*0Sstevel@tonic-gate #include "msg.h"
49*0Sstevel@tonic-gate #include "canohost.h"
50*0Sstevel@tonic-gate #include "pathnames.h"
51*0Sstevel@tonic-gate #include "readconf.h"
52*0Sstevel@tonic-gate
53*0Sstevel@tonic-gate uid_t original_real_uid; /* XXX readconf.c needs this */
54*0Sstevel@tonic-gate
55*0Sstevel@tonic-gate #ifdef HAVE___PROGNAME
56*0Sstevel@tonic-gate extern char *__progname;
57*0Sstevel@tonic-gate #else
58*0Sstevel@tonic-gate #ifndef lint
59*0Sstevel@tonic-gate char *__progname;
60*0Sstevel@tonic-gate #endif /* lint */
61*0Sstevel@tonic-gate #endif
62*0Sstevel@tonic-gate
63*0Sstevel@tonic-gate static int
valid_request(struct passwd * pw,char * host,Key ** ret,u_char * data,u_int datalen)64*0Sstevel@tonic-gate valid_request(struct passwd *pw, char *host, Key **ret, u_char *data,
65*0Sstevel@tonic-gate u_int datalen)
66*0Sstevel@tonic-gate {
67*0Sstevel@tonic-gate Buffer b;
68*0Sstevel@tonic-gate Key *key;
69*0Sstevel@tonic-gate u_char *pkblob;
70*0Sstevel@tonic-gate u_int blen, len;
71*0Sstevel@tonic-gate char *pkalg, *p;
72*0Sstevel@tonic-gate int pktype, fail;
73*0Sstevel@tonic-gate
74*0Sstevel@tonic-gate fail = 0;
75*0Sstevel@tonic-gate
76*0Sstevel@tonic-gate buffer_init(&b);
77*0Sstevel@tonic-gate buffer_append(&b, data, datalen);
78*0Sstevel@tonic-gate
79*0Sstevel@tonic-gate /* session id, currently limited to SHA1 (20 bytes) */
80*0Sstevel@tonic-gate p = buffer_get_string(&b, &len);
81*0Sstevel@tonic-gate if (len != 20)
82*0Sstevel@tonic-gate fail++;
83*0Sstevel@tonic-gate xfree(p);
84*0Sstevel@tonic-gate
85*0Sstevel@tonic-gate if (buffer_get_char(&b) != SSH2_MSG_USERAUTH_REQUEST)
86*0Sstevel@tonic-gate fail++;
87*0Sstevel@tonic-gate
88*0Sstevel@tonic-gate /* server user */
89*0Sstevel@tonic-gate buffer_skip_string(&b);
90*0Sstevel@tonic-gate
91*0Sstevel@tonic-gate /* service */
92*0Sstevel@tonic-gate p = buffer_get_string(&b, NULL);
93*0Sstevel@tonic-gate if (strcmp("ssh-connection", p) != 0)
94*0Sstevel@tonic-gate fail++;
95*0Sstevel@tonic-gate xfree(p);
96*0Sstevel@tonic-gate
97*0Sstevel@tonic-gate /* method */
98*0Sstevel@tonic-gate p = buffer_get_string(&b, NULL);
99*0Sstevel@tonic-gate if (strcmp("hostbased", p) != 0)
100*0Sstevel@tonic-gate fail++;
101*0Sstevel@tonic-gate xfree(p);
102*0Sstevel@tonic-gate
103*0Sstevel@tonic-gate /* pubkey */
104*0Sstevel@tonic-gate pkalg = buffer_get_string(&b, NULL);
105*0Sstevel@tonic-gate pkblob = buffer_get_string(&b, &blen);
106*0Sstevel@tonic-gate
107*0Sstevel@tonic-gate pktype = key_type_from_name(pkalg);
108*0Sstevel@tonic-gate if (pktype == KEY_UNSPEC)
109*0Sstevel@tonic-gate fail++;
110*0Sstevel@tonic-gate else if ((key = key_from_blob(pkblob, blen)) == NULL)
111*0Sstevel@tonic-gate fail++;
112*0Sstevel@tonic-gate else if (key->type != pktype)
113*0Sstevel@tonic-gate fail++;
114*0Sstevel@tonic-gate xfree(pkalg);
115*0Sstevel@tonic-gate xfree(pkblob);
116*0Sstevel@tonic-gate
117*0Sstevel@tonic-gate /* client host name, handle trailing dot */
118*0Sstevel@tonic-gate p = buffer_get_string(&b, &len);
119*0Sstevel@tonic-gate debug2("valid_request: check expect chost %s got %s", host, p);
120*0Sstevel@tonic-gate if (strlen(host) != len - 1)
121*0Sstevel@tonic-gate fail++;
122*0Sstevel@tonic-gate else if (p[len - 1] != '.')
123*0Sstevel@tonic-gate fail++;
124*0Sstevel@tonic-gate else if (strncasecmp(host, p, len - 1) != 0)
125*0Sstevel@tonic-gate fail++;
126*0Sstevel@tonic-gate xfree(p);
127*0Sstevel@tonic-gate
128*0Sstevel@tonic-gate /* local user */
129*0Sstevel@tonic-gate p = buffer_get_string(&b, NULL);
130*0Sstevel@tonic-gate
131*0Sstevel@tonic-gate if (strcmp(pw->pw_name, p) != 0)
132*0Sstevel@tonic-gate fail++;
133*0Sstevel@tonic-gate xfree(p);
134*0Sstevel@tonic-gate
135*0Sstevel@tonic-gate /* end of message */
136*0Sstevel@tonic-gate if (buffer_len(&b) != 0)
137*0Sstevel@tonic-gate fail++;
138*0Sstevel@tonic-gate
139*0Sstevel@tonic-gate debug3("valid_request: fail %d", fail);
140*0Sstevel@tonic-gate
141*0Sstevel@tonic-gate if (fail && key != NULL)
142*0Sstevel@tonic-gate key_free(key);
143*0Sstevel@tonic-gate else
144*0Sstevel@tonic-gate *ret = key;
145*0Sstevel@tonic-gate
146*0Sstevel@tonic-gate return (fail ? -1 : 0);
147*0Sstevel@tonic-gate }
148*0Sstevel@tonic-gate
149*0Sstevel@tonic-gate int
main(int argc,char ** argv)150*0Sstevel@tonic-gate main(int argc, char **argv)
151*0Sstevel@tonic-gate {
152*0Sstevel@tonic-gate Buffer b;
153*0Sstevel@tonic-gate Options options;
154*0Sstevel@tonic-gate Key *keys[2], *key;
155*0Sstevel@tonic-gate struct passwd *pw;
156*0Sstevel@tonic-gate int key_fd[2], i, found, version = 2, fd;
157*0Sstevel@tonic-gate u_char *signature, *data;
158*0Sstevel@tonic-gate char *host;
159*0Sstevel@tonic-gate u_int slen, dlen;
160*0Sstevel@tonic-gate u_int32_t rnd[256];
161*0Sstevel@tonic-gate
162*0Sstevel@tonic-gate /*
163*0Sstevel@tonic-gate * Since these two open()s are all that's done here before
164*0Sstevel@tonic-gate * dropping privileges with setreuid(), and since having been
165*0Sstevel@tonic-gate * privileged protects ssh-keysign from core dumps and tracing,
166*0Sstevel@tonic-gate * there's no need to use Least Privilege interfaces like
167*0Sstevel@tonic-gate * setppriv(2).
168*0Sstevel@tonic-gate */
169*0Sstevel@tonic-gate key_fd[0] = open(_PATH_HOST_RSA_KEY_FILE, O_RDONLY);
170*0Sstevel@tonic-gate key_fd[1] = open(_PATH_HOST_DSA_KEY_FILE, O_RDONLY);
171*0Sstevel@tonic-gate
172*0Sstevel@tonic-gate (void) setreuid(getuid(), getuid());
173*0Sstevel@tonic-gate
174*0Sstevel@tonic-gate (void) g11n_setlocale(LC_ALL, "");
175*0Sstevel@tonic-gate
176*0Sstevel@tonic-gate init_rng();
177*0Sstevel@tonic-gate seed_rng();
178*0Sstevel@tonic-gate arc4random_stir();
179*0Sstevel@tonic-gate
180*0Sstevel@tonic-gate #ifdef DEBUG_SSH_KEYSIGN
181*0Sstevel@tonic-gate log_init("ssh-keysign", SYSLOG_LEVEL_DEBUG3, SYSLOG_FACILITY_AUTH, 0);
182*0Sstevel@tonic-gate #endif
183*0Sstevel@tonic-gate
184*0Sstevel@tonic-gate /* verify that ssh-keysign is enabled by the admin */
185*0Sstevel@tonic-gate original_real_uid = getuid(); /* XXX readconf.c needs this */
186*0Sstevel@tonic-gate initialize_options(&options);
187*0Sstevel@tonic-gate (void)read_config_file(_PATH_HOST_CONFIG_FILE, "", &options);
188*0Sstevel@tonic-gate fill_default_options(&options);
189*0Sstevel@tonic-gate if (options.hostbased_authentication != 1)
190*0Sstevel@tonic-gate fatal("Hostbased authentication not enabled in %s",
191*0Sstevel@tonic-gate _PATH_HOST_CONFIG_FILE);
192*0Sstevel@tonic-gate
193*0Sstevel@tonic-gate if (key_fd[0] == -1 && key_fd[1] == -1)
194*0Sstevel@tonic-gate fatal("could not open any host key");
195*0Sstevel@tonic-gate
196*0Sstevel@tonic-gate if ((pw = getpwuid(getuid())) == NULL)
197*0Sstevel@tonic-gate fatal("getpwuid failed");
198*0Sstevel@tonic-gate pw = pwcopy(pw);
199*0Sstevel@tonic-gate
200*0Sstevel@tonic-gate SSLeay_add_all_algorithms();
201*0Sstevel@tonic-gate for (i = 0; i < 256; i++)
202*0Sstevel@tonic-gate rnd[i] = arc4random();
203*0Sstevel@tonic-gate RAND_seed(rnd, sizeof(rnd));
204*0Sstevel@tonic-gate
205*0Sstevel@tonic-gate found = 0;
206*0Sstevel@tonic-gate for (i = 0; i < 2; i++) {
207*0Sstevel@tonic-gate keys[i] = NULL;
208*0Sstevel@tonic-gate if (key_fd[i] == -1)
209*0Sstevel@tonic-gate continue;
210*0Sstevel@tonic-gate keys[i] = key_load_private_pem(key_fd[i], KEY_UNSPEC,
211*0Sstevel@tonic-gate NULL, NULL);
212*0Sstevel@tonic-gate (void) close(key_fd[i]);
213*0Sstevel@tonic-gate if (keys[i] != NULL && keys[i]->type == KEY_RSA) {
214*0Sstevel@tonic-gate if (RSA_blinding_on(keys[i]->rsa, NULL) != 1) {
215*0Sstevel@tonic-gate error("RSA_blinding_on failed");
216*0Sstevel@tonic-gate key_free(keys[i]);
217*0Sstevel@tonic-gate keys[i] = NULL;
218*0Sstevel@tonic-gate }
219*0Sstevel@tonic-gate }
220*0Sstevel@tonic-gate if (keys[i] != NULL)
221*0Sstevel@tonic-gate found = 1;
222*0Sstevel@tonic-gate }
223*0Sstevel@tonic-gate if (!found)
224*0Sstevel@tonic-gate fatal("no hostkey found");
225*0Sstevel@tonic-gate
226*0Sstevel@tonic-gate buffer_init(&b);
227*0Sstevel@tonic-gate if (ssh_msg_recv(STDIN_FILENO, &b) < 0)
228*0Sstevel@tonic-gate fatal("ssh_msg_recv failed");
229*0Sstevel@tonic-gate if (buffer_get_char(&b) != version)
230*0Sstevel@tonic-gate fatal("bad version");
231*0Sstevel@tonic-gate fd = buffer_get_int(&b);
232*0Sstevel@tonic-gate if ((fd == STDIN_FILENO) || (fd == STDOUT_FILENO))
233*0Sstevel@tonic-gate fatal("bad fd");
234*0Sstevel@tonic-gate if ((host = get_local_name(fd)) == NULL)
235*0Sstevel@tonic-gate fatal("cannot get sockname for fd");
236*0Sstevel@tonic-gate
237*0Sstevel@tonic-gate data = buffer_get_string(&b, &dlen);
238*0Sstevel@tonic-gate if (valid_request(pw, host, &key, data, dlen) < 0)
239*0Sstevel@tonic-gate fatal("not a valid request");
240*0Sstevel@tonic-gate xfree(host);
241*0Sstevel@tonic-gate
242*0Sstevel@tonic-gate found = 0;
243*0Sstevel@tonic-gate for (i = 0; i < 2; i++) {
244*0Sstevel@tonic-gate if (keys[i] != NULL &&
245*0Sstevel@tonic-gate key_equal(key, keys[i])) {
246*0Sstevel@tonic-gate found = 1;
247*0Sstevel@tonic-gate break;
248*0Sstevel@tonic-gate }
249*0Sstevel@tonic-gate }
250*0Sstevel@tonic-gate if (!found)
251*0Sstevel@tonic-gate fatal("no matching hostkey found");
252*0Sstevel@tonic-gate
253*0Sstevel@tonic-gate if (key_sign(keys[i], &signature, &slen, data, dlen) != 0)
254*0Sstevel@tonic-gate fatal("key_sign failed");
255*0Sstevel@tonic-gate xfree(data);
256*0Sstevel@tonic-gate
257*0Sstevel@tonic-gate /* send reply */
258*0Sstevel@tonic-gate buffer_clear(&b);
259*0Sstevel@tonic-gate buffer_put_string(&b, signature, slen);
260*0Sstevel@tonic-gate ssh_msg_send(STDOUT_FILENO, version, &b);
261*0Sstevel@tonic-gate
262*0Sstevel@tonic-gate return (0);
263*0Sstevel@tonic-gate }
264