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