xref: /onnv-gate/usr/src/cmd/ssh/libssh/common/authfile.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * Author: Tatu Ylonen <ylo@cs.hut.fi>
3*0Sstevel@tonic-gate  * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
4*0Sstevel@tonic-gate  *                    All rights reserved
5*0Sstevel@tonic-gate  * This file contains functions for reading and writing identity files, and
6*0Sstevel@tonic-gate  * for reading the passphrase from the user.
7*0Sstevel@tonic-gate  *
8*0Sstevel@tonic-gate  * As far as I am concerned, the code I have written for this software
9*0Sstevel@tonic-gate  * can be used freely for any purpose.  Any derived versions of this
10*0Sstevel@tonic-gate  * software must be clearly marked as such, and if the derived work is
11*0Sstevel@tonic-gate  * incompatible with the protocol description in the RFC file, it must be
12*0Sstevel@tonic-gate  * called by a name other than "ssh" or "Secure Shell".
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  *
15*0Sstevel@tonic-gate  * Copyright (c) 2000 Markus Friedl.  All rights reserved.
16*0Sstevel@tonic-gate  *
17*0Sstevel@tonic-gate  * Redistribution and use in source and binary forms, with or without
18*0Sstevel@tonic-gate  * modification, are permitted provided that the following conditions
19*0Sstevel@tonic-gate  * are met:
20*0Sstevel@tonic-gate  * 1. Redistributions of source code must retain the above copyright
21*0Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer.
22*0Sstevel@tonic-gate  * 2. Redistributions in binary form must reproduce the above copyright
23*0Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer in the
24*0Sstevel@tonic-gate  *    documentation and/or other materials provided with the distribution.
25*0Sstevel@tonic-gate  *
26*0Sstevel@tonic-gate  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
27*0Sstevel@tonic-gate  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
28*0Sstevel@tonic-gate  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
29*0Sstevel@tonic-gate  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
30*0Sstevel@tonic-gate  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
31*0Sstevel@tonic-gate  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32*0Sstevel@tonic-gate  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33*0Sstevel@tonic-gate  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34*0Sstevel@tonic-gate  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
35*0Sstevel@tonic-gate  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36*0Sstevel@tonic-gate  */
37*0Sstevel@tonic-gate 
38*0Sstevel@tonic-gate #include "includes.h"
39*0Sstevel@tonic-gate RCSID("$OpenBSD: authfile.c,v 1.50 2002/06/24 14:55:38 markus Exp $");
40*0Sstevel@tonic-gate 
41*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
42*0Sstevel@tonic-gate 
43*0Sstevel@tonic-gate #include <openssl/err.h>
44*0Sstevel@tonic-gate #include <openssl/evp.h>
45*0Sstevel@tonic-gate #include <openssl/pem.h>
46*0Sstevel@tonic-gate 
47*0Sstevel@tonic-gate #include "cipher.h"
48*0Sstevel@tonic-gate #include "xmalloc.h"
49*0Sstevel@tonic-gate #include "buffer.h"
50*0Sstevel@tonic-gate #include "bufaux.h"
51*0Sstevel@tonic-gate #include "key.h"
52*0Sstevel@tonic-gate #include "ssh.h"
53*0Sstevel@tonic-gate #include "log.h"
54*0Sstevel@tonic-gate #include "authfile.h"
55*0Sstevel@tonic-gate #include "rsa.h"
56*0Sstevel@tonic-gate 
57*0Sstevel@tonic-gate /* Version identification string for SSH v1 identity files. */
58*0Sstevel@tonic-gate static const char authfile_id_string[] =
59*0Sstevel@tonic-gate     "SSH PRIVATE KEY FILE FORMAT 1.1\n";
60*0Sstevel@tonic-gate 
61*0Sstevel@tonic-gate /*
62*0Sstevel@tonic-gate  * Saves the authentication (private) key in a file, encrypting it with
63*0Sstevel@tonic-gate  * passphrase.  The identification of the file (lowest 64 bits of n) will
64*0Sstevel@tonic-gate  * precede the key to provide identification of the key without needing a
65*0Sstevel@tonic-gate  * passphrase.
66*0Sstevel@tonic-gate  */
67*0Sstevel@tonic-gate 
68*0Sstevel@tonic-gate static int
key_save_private_rsa1(Key * key,const char * filename,const char * passphrase,const char * comment)69*0Sstevel@tonic-gate key_save_private_rsa1(Key *key, const char *filename, const char *passphrase,
70*0Sstevel@tonic-gate     const char *comment)
71*0Sstevel@tonic-gate {
72*0Sstevel@tonic-gate 	Buffer buffer, encrypted;
73*0Sstevel@tonic-gate 	u_char buf[100], *cp;
74*0Sstevel@tonic-gate 	int fd, i, cipher_num;
75*0Sstevel@tonic-gate 	CipherContext ciphercontext;
76*0Sstevel@tonic-gate 	Cipher *cipher;
77*0Sstevel@tonic-gate 	u_int32_t rand;
78*0Sstevel@tonic-gate 
79*0Sstevel@tonic-gate 	/*
80*0Sstevel@tonic-gate 	 * If the passphrase is empty, use SSH_CIPHER_NONE to ease converting
81*0Sstevel@tonic-gate 	 * to another cipher; otherwise use SSH_AUTHFILE_CIPHER.
82*0Sstevel@tonic-gate 	 */
83*0Sstevel@tonic-gate 	cipher_num = (strcmp(passphrase, "") == 0) ?
84*0Sstevel@tonic-gate 	    SSH_CIPHER_NONE : SSH_AUTHFILE_CIPHER;
85*0Sstevel@tonic-gate 	if ((cipher = cipher_by_number(cipher_num)) == NULL)
86*0Sstevel@tonic-gate 		fatal("save_private_key_rsa: bad cipher");
87*0Sstevel@tonic-gate 
88*0Sstevel@tonic-gate 	/* This buffer is used to built the secret part of the private key. */
89*0Sstevel@tonic-gate 	buffer_init(&buffer);
90*0Sstevel@tonic-gate 
91*0Sstevel@tonic-gate 	/* Put checkbytes for checking passphrase validity. */
92*0Sstevel@tonic-gate 	rand = arc4random();
93*0Sstevel@tonic-gate 	buf[0] = rand & 0xff;
94*0Sstevel@tonic-gate 	buf[1] = (rand >> 8) & 0xff;
95*0Sstevel@tonic-gate 	buf[2] = buf[0];
96*0Sstevel@tonic-gate 	buf[3] = buf[1];
97*0Sstevel@tonic-gate 	buffer_append(&buffer, buf, 4);
98*0Sstevel@tonic-gate 
99*0Sstevel@tonic-gate 	/*
100*0Sstevel@tonic-gate 	 * Store the private key (n and e will not be stored because they
101*0Sstevel@tonic-gate 	 * will be stored in plain text, and storing them also in encrypted
102*0Sstevel@tonic-gate 	 * format would just give known plaintext).
103*0Sstevel@tonic-gate 	 */
104*0Sstevel@tonic-gate 	buffer_put_bignum(&buffer, key->rsa->d);
105*0Sstevel@tonic-gate 	buffer_put_bignum(&buffer, key->rsa->iqmp);
106*0Sstevel@tonic-gate 	buffer_put_bignum(&buffer, key->rsa->q);	/* reverse from SSL p */
107*0Sstevel@tonic-gate 	buffer_put_bignum(&buffer, key->rsa->p);	/* reverse from SSL q */
108*0Sstevel@tonic-gate 
109*0Sstevel@tonic-gate 	/* Pad the part to be encrypted until its size is a multiple of 8. */
110*0Sstevel@tonic-gate 	while (buffer_len(&buffer) % 8 != 0)
111*0Sstevel@tonic-gate 		buffer_put_char(&buffer, 0);
112*0Sstevel@tonic-gate 
113*0Sstevel@tonic-gate 	/* This buffer will be used to contain the data in the file. */
114*0Sstevel@tonic-gate 	buffer_init(&encrypted);
115*0Sstevel@tonic-gate 
116*0Sstevel@tonic-gate 	/* First store keyfile id string. */
117*0Sstevel@tonic-gate 	for (i = 0; authfile_id_string[i]; i++)
118*0Sstevel@tonic-gate 		buffer_put_char(&encrypted, authfile_id_string[i]);
119*0Sstevel@tonic-gate 	buffer_put_char(&encrypted, 0);
120*0Sstevel@tonic-gate 
121*0Sstevel@tonic-gate 	/* Store cipher type. */
122*0Sstevel@tonic-gate 	buffer_put_char(&encrypted, cipher_num);
123*0Sstevel@tonic-gate 	buffer_put_int(&encrypted, 0);	/* For future extension */
124*0Sstevel@tonic-gate 
125*0Sstevel@tonic-gate 	/* Store public key.  This will be in plain text. */
126*0Sstevel@tonic-gate 	buffer_put_int(&encrypted, BN_num_bits(key->rsa->n));
127*0Sstevel@tonic-gate 	buffer_put_bignum(&encrypted, key->rsa->n);
128*0Sstevel@tonic-gate 	buffer_put_bignum(&encrypted, key->rsa->e);
129*0Sstevel@tonic-gate 	buffer_put_cstring(&encrypted, comment);
130*0Sstevel@tonic-gate 
131*0Sstevel@tonic-gate 	/* Allocate space for the private part of the key in the buffer. */
132*0Sstevel@tonic-gate 	cp = buffer_append_space(&encrypted, buffer_len(&buffer));
133*0Sstevel@tonic-gate 
134*0Sstevel@tonic-gate 	cipher_set_key_string(&ciphercontext, cipher, passphrase,
135*0Sstevel@tonic-gate 	    CIPHER_ENCRYPT);
136*0Sstevel@tonic-gate 	cipher_crypt(&ciphercontext, cp,
137*0Sstevel@tonic-gate 	    buffer_ptr(&buffer), buffer_len(&buffer));
138*0Sstevel@tonic-gate 	cipher_cleanup(&ciphercontext);
139*0Sstevel@tonic-gate 	memset(&ciphercontext, 0, sizeof(ciphercontext));
140*0Sstevel@tonic-gate 
141*0Sstevel@tonic-gate 	/* Destroy temporary data. */
142*0Sstevel@tonic-gate 	memset(buf, 0, sizeof(buf));
143*0Sstevel@tonic-gate 	buffer_free(&buffer);
144*0Sstevel@tonic-gate 
145*0Sstevel@tonic-gate 	fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0600);
146*0Sstevel@tonic-gate 	if (fd < 0) {
147*0Sstevel@tonic-gate 		error("open %s failed: %s.", filename, strerror(errno));
148*0Sstevel@tonic-gate 		return 0;
149*0Sstevel@tonic-gate 	}
150*0Sstevel@tonic-gate 	if (write(fd, buffer_ptr(&encrypted), buffer_len(&encrypted)) !=
151*0Sstevel@tonic-gate 	    buffer_len(&encrypted)) {
152*0Sstevel@tonic-gate 		error("write to key file %s failed: %s", filename,
153*0Sstevel@tonic-gate 		    strerror(errno));
154*0Sstevel@tonic-gate 		buffer_free(&encrypted);
155*0Sstevel@tonic-gate 		close(fd);
156*0Sstevel@tonic-gate 		unlink(filename);
157*0Sstevel@tonic-gate 		return 0;
158*0Sstevel@tonic-gate 	}
159*0Sstevel@tonic-gate 	close(fd);
160*0Sstevel@tonic-gate 	buffer_free(&encrypted);
161*0Sstevel@tonic-gate 	return 1;
162*0Sstevel@tonic-gate }
163*0Sstevel@tonic-gate 
164*0Sstevel@tonic-gate /* save SSH v2 key in OpenSSL PEM format */
165*0Sstevel@tonic-gate static int
key_save_private_pem(Key * key,const char * filename,const char * _passphrase,const char * comment)166*0Sstevel@tonic-gate key_save_private_pem(Key *key, const char *filename, const char *_passphrase,
167*0Sstevel@tonic-gate     const char *comment)
168*0Sstevel@tonic-gate {
169*0Sstevel@tonic-gate 	FILE *fp;
170*0Sstevel@tonic-gate 	int fd;
171*0Sstevel@tonic-gate 	int success = 0;
172*0Sstevel@tonic-gate 	int len = strlen(_passphrase);
173*0Sstevel@tonic-gate 	u_char *passphrase = (len > 0) ? (u_char *)_passphrase : NULL;
174*0Sstevel@tonic-gate 	const EVP_CIPHER *cipher = (len > 0) ? EVP_des_ede3_cbc() : NULL;
175*0Sstevel@tonic-gate 
176*0Sstevel@tonic-gate 	if (len > 0 && len <= 4) {
177*0Sstevel@tonic-gate 		error("passphrase too short: have %d bytes, need > 4", len);
178*0Sstevel@tonic-gate 		return 0;
179*0Sstevel@tonic-gate 	}
180*0Sstevel@tonic-gate 	fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0600);
181*0Sstevel@tonic-gate 	if (fd < 0) {
182*0Sstevel@tonic-gate 		error("open %s failed: %s.", filename, strerror(errno));
183*0Sstevel@tonic-gate 		return 0;
184*0Sstevel@tonic-gate 	}
185*0Sstevel@tonic-gate 	fp = fdopen(fd, "w");
186*0Sstevel@tonic-gate 	if (fp == NULL ) {
187*0Sstevel@tonic-gate 		error("fdopen %s failed: %s.", filename, strerror(errno));
188*0Sstevel@tonic-gate 		close(fd);
189*0Sstevel@tonic-gate 		return 0;
190*0Sstevel@tonic-gate 	}
191*0Sstevel@tonic-gate 	switch (key->type) {
192*0Sstevel@tonic-gate 	case KEY_DSA:
193*0Sstevel@tonic-gate 		success = PEM_write_DSAPrivateKey(fp, key->dsa,
194*0Sstevel@tonic-gate 		    cipher, passphrase, len, NULL, NULL);
195*0Sstevel@tonic-gate 		break;
196*0Sstevel@tonic-gate 	case KEY_RSA:
197*0Sstevel@tonic-gate 		success = PEM_write_RSAPrivateKey(fp, key->rsa,
198*0Sstevel@tonic-gate 		    cipher, passphrase, len, NULL, NULL);
199*0Sstevel@tonic-gate 		break;
200*0Sstevel@tonic-gate 	}
201*0Sstevel@tonic-gate 	fclose(fp);
202*0Sstevel@tonic-gate 	return success;
203*0Sstevel@tonic-gate }
204*0Sstevel@tonic-gate 
205*0Sstevel@tonic-gate int
key_save_private(Key * key,const char * filename,const char * passphrase,const char * comment)206*0Sstevel@tonic-gate key_save_private(Key *key, const char *filename, const char *passphrase,
207*0Sstevel@tonic-gate     const char *comment)
208*0Sstevel@tonic-gate {
209*0Sstevel@tonic-gate 	switch (key->type) {
210*0Sstevel@tonic-gate 	case KEY_RSA1:
211*0Sstevel@tonic-gate 		return key_save_private_rsa1(key, filename, passphrase,
212*0Sstevel@tonic-gate 		    comment);
213*0Sstevel@tonic-gate 		break;
214*0Sstevel@tonic-gate 	case KEY_DSA:
215*0Sstevel@tonic-gate 	case KEY_RSA:
216*0Sstevel@tonic-gate 		return key_save_private_pem(key, filename, passphrase,
217*0Sstevel@tonic-gate 		    comment);
218*0Sstevel@tonic-gate 		break;
219*0Sstevel@tonic-gate 	default:
220*0Sstevel@tonic-gate 		break;
221*0Sstevel@tonic-gate 	}
222*0Sstevel@tonic-gate 	error("key_save_private: cannot save key type %d", key->type);
223*0Sstevel@tonic-gate 	return 0;
224*0Sstevel@tonic-gate }
225*0Sstevel@tonic-gate 
226*0Sstevel@tonic-gate /*
227*0Sstevel@tonic-gate  * Loads the public part of the ssh v1 key file.  Returns NULL if an error was
228*0Sstevel@tonic-gate  * encountered (the file does not exist or is not readable), and the key
229*0Sstevel@tonic-gate  * otherwise.
230*0Sstevel@tonic-gate  */
231*0Sstevel@tonic-gate 
232*0Sstevel@tonic-gate static Key *
key_load_public_rsa1(int fd,const char * filename,char ** commentp)233*0Sstevel@tonic-gate key_load_public_rsa1(int fd, const char *filename, char **commentp)
234*0Sstevel@tonic-gate {
235*0Sstevel@tonic-gate 	Buffer buffer;
236*0Sstevel@tonic-gate 	Key *pub;
237*0Sstevel@tonic-gate 	char *cp;
238*0Sstevel@tonic-gate 	int i;
239*0Sstevel@tonic-gate 	off_t len;
240*0Sstevel@tonic-gate 
241*0Sstevel@tonic-gate 	len = lseek(fd, (off_t) 0, SEEK_END);
242*0Sstevel@tonic-gate 	lseek(fd, (off_t) 0, SEEK_SET);
243*0Sstevel@tonic-gate 
244*0Sstevel@tonic-gate 	buffer_init(&buffer);
245*0Sstevel@tonic-gate 	cp = buffer_append_space(&buffer, len);
246*0Sstevel@tonic-gate 
247*0Sstevel@tonic-gate 	if (read(fd, cp, (size_t) len) != (size_t) len) {
248*0Sstevel@tonic-gate 		debug("Read from key file %.200s failed: %.100s", filename,
249*0Sstevel@tonic-gate 		    strerror(errno));
250*0Sstevel@tonic-gate 		buffer_free(&buffer);
251*0Sstevel@tonic-gate 		return NULL;
252*0Sstevel@tonic-gate 	}
253*0Sstevel@tonic-gate 
254*0Sstevel@tonic-gate 	/* Check that it is at least big enough to contain the ID string. */
255*0Sstevel@tonic-gate 	if (len < sizeof(authfile_id_string)) {
256*0Sstevel@tonic-gate 		debug3("Not a RSA1 key file %.200s.", filename);
257*0Sstevel@tonic-gate 		buffer_free(&buffer);
258*0Sstevel@tonic-gate 		return NULL;
259*0Sstevel@tonic-gate 	}
260*0Sstevel@tonic-gate 	/*
261*0Sstevel@tonic-gate 	 * Make sure it begins with the id string.  Consume the id string
262*0Sstevel@tonic-gate 	 * from the buffer.
263*0Sstevel@tonic-gate 	 */
264*0Sstevel@tonic-gate 	for (i = 0; i < sizeof(authfile_id_string); i++)
265*0Sstevel@tonic-gate 		if (buffer_get_char(&buffer) != authfile_id_string[i]) {
266*0Sstevel@tonic-gate 			debug3("Not a RSA1 key file %.200s.", filename);
267*0Sstevel@tonic-gate 			buffer_free(&buffer);
268*0Sstevel@tonic-gate 			return NULL;
269*0Sstevel@tonic-gate 		}
270*0Sstevel@tonic-gate 	/* Skip cipher type and reserved data. */
271*0Sstevel@tonic-gate 	(void) buffer_get_char(&buffer);	/* cipher type */
272*0Sstevel@tonic-gate 	(void) buffer_get_int(&buffer);		/* reserved */
273*0Sstevel@tonic-gate 
274*0Sstevel@tonic-gate 	/* Read the public key from the buffer. */
275*0Sstevel@tonic-gate 	(void) buffer_get_int(&buffer);
276*0Sstevel@tonic-gate 	pub = key_new(KEY_RSA1);
277*0Sstevel@tonic-gate 	buffer_get_bignum(&buffer, pub->rsa->n);
278*0Sstevel@tonic-gate 	buffer_get_bignum(&buffer, pub->rsa->e);
279*0Sstevel@tonic-gate 	if (commentp)
280*0Sstevel@tonic-gate 		*commentp = buffer_get_string(&buffer, NULL);
281*0Sstevel@tonic-gate 	/* The encrypted private part is not parsed by this function. */
282*0Sstevel@tonic-gate 
283*0Sstevel@tonic-gate 	buffer_free(&buffer);
284*0Sstevel@tonic-gate 	return pub;
285*0Sstevel@tonic-gate }
286*0Sstevel@tonic-gate 
287*0Sstevel@tonic-gate /* load public key from private-key file, works only for SSH v1 */
288*0Sstevel@tonic-gate Key *
key_load_public_type(int type,const char * filename,char ** commentp)289*0Sstevel@tonic-gate key_load_public_type(int type, const char *filename, char **commentp)
290*0Sstevel@tonic-gate {
291*0Sstevel@tonic-gate 	Key *pub;
292*0Sstevel@tonic-gate 	int fd;
293*0Sstevel@tonic-gate 
294*0Sstevel@tonic-gate 	if (type == KEY_RSA1) {
295*0Sstevel@tonic-gate 		fd = open(filename, O_RDONLY);
296*0Sstevel@tonic-gate 		if (fd < 0)
297*0Sstevel@tonic-gate 			return NULL;
298*0Sstevel@tonic-gate 		pub = key_load_public_rsa1(fd, filename, commentp);
299*0Sstevel@tonic-gate 		close(fd);
300*0Sstevel@tonic-gate 		return pub;
301*0Sstevel@tonic-gate 	}
302*0Sstevel@tonic-gate 	return NULL;
303*0Sstevel@tonic-gate }
304*0Sstevel@tonic-gate 
305*0Sstevel@tonic-gate /*
306*0Sstevel@tonic-gate  * Loads the private key from the file.  Returns 0 if an error is encountered
307*0Sstevel@tonic-gate  * (file does not exist or is not readable, or passphrase is bad). This
308*0Sstevel@tonic-gate  * initializes the private key.
309*0Sstevel@tonic-gate  * Assumes we are called under uid of the owner of the file.
310*0Sstevel@tonic-gate  */
311*0Sstevel@tonic-gate 
312*0Sstevel@tonic-gate static Key *
key_load_private_rsa1(int fd,const char * filename,const char * passphrase,char ** commentp)313*0Sstevel@tonic-gate key_load_private_rsa1(int fd, const char *filename, const char *passphrase,
314*0Sstevel@tonic-gate     char **commentp)
315*0Sstevel@tonic-gate {
316*0Sstevel@tonic-gate 	int i, check1, check2, cipher_type;
317*0Sstevel@tonic-gate 	off_t len;
318*0Sstevel@tonic-gate 	Buffer buffer, decrypted;
319*0Sstevel@tonic-gate 	u_char *cp;
320*0Sstevel@tonic-gate 	CipherContext ciphercontext;
321*0Sstevel@tonic-gate 	Cipher *cipher;
322*0Sstevel@tonic-gate 	Key *prv = NULL;
323*0Sstevel@tonic-gate 
324*0Sstevel@tonic-gate 	len = lseek(fd, (off_t) 0, SEEK_END);
325*0Sstevel@tonic-gate 	lseek(fd, (off_t) 0, SEEK_SET);
326*0Sstevel@tonic-gate 
327*0Sstevel@tonic-gate 	buffer_init(&buffer);
328*0Sstevel@tonic-gate 	cp = buffer_append_space(&buffer, len);
329*0Sstevel@tonic-gate 
330*0Sstevel@tonic-gate 	if (read(fd, cp, (size_t) len) != (size_t) len) {
331*0Sstevel@tonic-gate 		debug("Read from key file %.200s failed: %.100s", filename,
332*0Sstevel@tonic-gate 		    strerror(errno));
333*0Sstevel@tonic-gate 		buffer_free(&buffer);
334*0Sstevel@tonic-gate 		close(fd);
335*0Sstevel@tonic-gate 		return NULL;
336*0Sstevel@tonic-gate 	}
337*0Sstevel@tonic-gate 
338*0Sstevel@tonic-gate 	/* Check that it is at least big enough to contain the ID string. */
339*0Sstevel@tonic-gate 	if (len < sizeof(authfile_id_string)) {
340*0Sstevel@tonic-gate 		debug3("Not a RSA1 key file %.200s.", filename);
341*0Sstevel@tonic-gate 		buffer_free(&buffer);
342*0Sstevel@tonic-gate 		close(fd);
343*0Sstevel@tonic-gate 		return NULL;
344*0Sstevel@tonic-gate 	}
345*0Sstevel@tonic-gate 	/*
346*0Sstevel@tonic-gate 	 * Make sure it begins with the id string.  Consume the id string
347*0Sstevel@tonic-gate 	 * from the buffer.
348*0Sstevel@tonic-gate 	 */
349*0Sstevel@tonic-gate 	for (i = 0; i < sizeof(authfile_id_string); i++)
350*0Sstevel@tonic-gate 		if (buffer_get_char(&buffer) != authfile_id_string[i]) {
351*0Sstevel@tonic-gate 			debug3("Not a RSA1 key file %.200s.", filename);
352*0Sstevel@tonic-gate 			buffer_free(&buffer);
353*0Sstevel@tonic-gate 			close(fd);
354*0Sstevel@tonic-gate 			return NULL;
355*0Sstevel@tonic-gate 		}
356*0Sstevel@tonic-gate 
357*0Sstevel@tonic-gate 	/* Read cipher type. */
358*0Sstevel@tonic-gate 	cipher_type = buffer_get_char(&buffer);
359*0Sstevel@tonic-gate 	(void) buffer_get_int(&buffer);	/* Reserved data. */
360*0Sstevel@tonic-gate 
361*0Sstevel@tonic-gate 	/* Read the public key from the buffer. */
362*0Sstevel@tonic-gate 	(void) buffer_get_int(&buffer);
363*0Sstevel@tonic-gate 	prv = key_new_private(KEY_RSA1);
364*0Sstevel@tonic-gate 
365*0Sstevel@tonic-gate 	buffer_get_bignum(&buffer, prv->rsa->n);
366*0Sstevel@tonic-gate 	buffer_get_bignum(&buffer, prv->rsa->e);
367*0Sstevel@tonic-gate 	if (commentp)
368*0Sstevel@tonic-gate 		*commentp = buffer_get_string(&buffer, NULL);
369*0Sstevel@tonic-gate 	else
370*0Sstevel@tonic-gate 		xfree(buffer_get_string(&buffer, NULL));
371*0Sstevel@tonic-gate 
372*0Sstevel@tonic-gate 	/* Check that it is a supported cipher. */
373*0Sstevel@tonic-gate 	cipher = cipher_by_number(cipher_type);
374*0Sstevel@tonic-gate 	if (cipher == NULL) {
375*0Sstevel@tonic-gate 		debug("Unsupported cipher %d used in key file %.200s.",
376*0Sstevel@tonic-gate 		    cipher_type, filename);
377*0Sstevel@tonic-gate 		buffer_free(&buffer);
378*0Sstevel@tonic-gate 		goto fail;
379*0Sstevel@tonic-gate 	}
380*0Sstevel@tonic-gate 	/* Initialize space for decrypted data. */
381*0Sstevel@tonic-gate 	buffer_init(&decrypted);
382*0Sstevel@tonic-gate 	cp = buffer_append_space(&decrypted, buffer_len(&buffer));
383*0Sstevel@tonic-gate 
384*0Sstevel@tonic-gate 	/* Rest of the buffer is encrypted.  Decrypt it using the passphrase. */
385*0Sstevel@tonic-gate 	cipher_set_key_string(&ciphercontext, cipher, passphrase,
386*0Sstevel@tonic-gate 	    CIPHER_DECRYPT);
387*0Sstevel@tonic-gate 	cipher_crypt(&ciphercontext, cp,
388*0Sstevel@tonic-gate 	    buffer_ptr(&buffer), buffer_len(&buffer));
389*0Sstevel@tonic-gate 	cipher_cleanup(&ciphercontext);
390*0Sstevel@tonic-gate 	memset(&ciphercontext, 0, sizeof(ciphercontext));
391*0Sstevel@tonic-gate 	buffer_free(&buffer);
392*0Sstevel@tonic-gate 
393*0Sstevel@tonic-gate 	check1 = buffer_get_char(&decrypted);
394*0Sstevel@tonic-gate 	check2 = buffer_get_char(&decrypted);
395*0Sstevel@tonic-gate 	if (check1 != buffer_get_char(&decrypted) ||
396*0Sstevel@tonic-gate 	    check2 != buffer_get_char(&decrypted)) {
397*0Sstevel@tonic-gate 		if (strcmp(passphrase, "") != 0)
398*0Sstevel@tonic-gate 			debug("Bad passphrase supplied for key file %.200s.",
399*0Sstevel@tonic-gate 			    filename);
400*0Sstevel@tonic-gate 		/* Bad passphrase. */
401*0Sstevel@tonic-gate 		buffer_free(&decrypted);
402*0Sstevel@tonic-gate 		goto fail;
403*0Sstevel@tonic-gate 	}
404*0Sstevel@tonic-gate 	/* Read the rest of the private key. */
405*0Sstevel@tonic-gate 	buffer_get_bignum(&decrypted, prv->rsa->d);
406*0Sstevel@tonic-gate 	buffer_get_bignum(&decrypted, prv->rsa->iqmp);		/* u */
407*0Sstevel@tonic-gate 	/* in SSL and SSH v1 p and q are exchanged */
408*0Sstevel@tonic-gate 	buffer_get_bignum(&decrypted, prv->rsa->q);		/* p */
409*0Sstevel@tonic-gate 	buffer_get_bignum(&decrypted, prv->rsa->p);		/* q */
410*0Sstevel@tonic-gate 
411*0Sstevel@tonic-gate 	/* calculate p-1 and q-1 */
412*0Sstevel@tonic-gate 	rsa_generate_additional_parameters(prv->rsa);
413*0Sstevel@tonic-gate 
414*0Sstevel@tonic-gate 	buffer_free(&decrypted);
415*0Sstevel@tonic-gate 	close(fd);
416*0Sstevel@tonic-gate 	return prv;
417*0Sstevel@tonic-gate 
418*0Sstevel@tonic-gate fail:
419*0Sstevel@tonic-gate 	if (commentp)
420*0Sstevel@tonic-gate 		xfree(*commentp);
421*0Sstevel@tonic-gate 	close(fd);
422*0Sstevel@tonic-gate 	key_free(prv);
423*0Sstevel@tonic-gate 	return NULL;
424*0Sstevel@tonic-gate }
425*0Sstevel@tonic-gate 
426*0Sstevel@tonic-gate Key *
key_load_private_pem(int fd,int type,const char * passphrase,char ** commentp)427*0Sstevel@tonic-gate key_load_private_pem(int fd, int type, const char *passphrase,
428*0Sstevel@tonic-gate     char **commentp)
429*0Sstevel@tonic-gate {
430*0Sstevel@tonic-gate 	FILE *fp;
431*0Sstevel@tonic-gate 	EVP_PKEY *pk = NULL;
432*0Sstevel@tonic-gate 	Key *prv = NULL;
433*0Sstevel@tonic-gate 	char *name = "<no key>";
434*0Sstevel@tonic-gate 
435*0Sstevel@tonic-gate 	fp = fdopen(fd, "r");
436*0Sstevel@tonic-gate 	if (fp == NULL) {
437*0Sstevel@tonic-gate 		error("fdopen failed: %s", strerror(errno));
438*0Sstevel@tonic-gate 		close(fd);
439*0Sstevel@tonic-gate 		return NULL;
440*0Sstevel@tonic-gate 	}
441*0Sstevel@tonic-gate 	pk = PEM_read_PrivateKey(fp, NULL, NULL, (char *)passphrase);
442*0Sstevel@tonic-gate 	if (pk == NULL) {
443*0Sstevel@tonic-gate 		debug("PEM_read_PrivateKey failed");
444*0Sstevel@tonic-gate 		(void)ERR_get_error();
445*0Sstevel@tonic-gate 	} else if (pk->type == EVP_PKEY_RSA &&
446*0Sstevel@tonic-gate 	    (type == KEY_UNSPEC||type==KEY_RSA)) {
447*0Sstevel@tonic-gate 		prv = key_new(KEY_UNSPEC);
448*0Sstevel@tonic-gate 		prv->rsa = EVP_PKEY_get1_RSA(pk);
449*0Sstevel@tonic-gate 		prv->type = KEY_RSA;
450*0Sstevel@tonic-gate 		name = "rsa w/o comment";
451*0Sstevel@tonic-gate #ifdef DEBUG_PK
452*0Sstevel@tonic-gate 		RSA_print_fp(stderr, prv->rsa, 8);
453*0Sstevel@tonic-gate #endif
454*0Sstevel@tonic-gate 	} else if (pk->type == EVP_PKEY_DSA &&
455*0Sstevel@tonic-gate 	    (type == KEY_UNSPEC||type==KEY_DSA)) {
456*0Sstevel@tonic-gate 		prv = key_new(KEY_UNSPEC);
457*0Sstevel@tonic-gate 		prv->dsa = EVP_PKEY_get1_DSA(pk);
458*0Sstevel@tonic-gate 		prv->type = KEY_DSA;
459*0Sstevel@tonic-gate 		name = "dsa w/o comment";
460*0Sstevel@tonic-gate #ifdef DEBUG_PK
461*0Sstevel@tonic-gate 		DSA_print_fp(stderr, prv->dsa, 8);
462*0Sstevel@tonic-gate #endif
463*0Sstevel@tonic-gate 	} else {
464*0Sstevel@tonic-gate 		error("PEM_read_PrivateKey: mismatch or "
465*0Sstevel@tonic-gate 		    "unknown EVP_PKEY save_type %d", pk->save_type);
466*0Sstevel@tonic-gate 	}
467*0Sstevel@tonic-gate 	fclose(fp);
468*0Sstevel@tonic-gate 	if (pk != NULL)
469*0Sstevel@tonic-gate 		EVP_PKEY_free(pk);
470*0Sstevel@tonic-gate 	if (prv != NULL && commentp)
471*0Sstevel@tonic-gate 		*commentp = xstrdup(name);
472*0Sstevel@tonic-gate 	debug("read PEM private key done: type %s",
473*0Sstevel@tonic-gate 	    prv ? key_type(prv) : "<unknown>");
474*0Sstevel@tonic-gate 	return prv;
475*0Sstevel@tonic-gate }
476*0Sstevel@tonic-gate 
477*0Sstevel@tonic-gate static int
key_perm_ok(int fd,const char * filename)478*0Sstevel@tonic-gate key_perm_ok(int fd, const char *filename)
479*0Sstevel@tonic-gate {
480*0Sstevel@tonic-gate 	struct stat st;
481*0Sstevel@tonic-gate 
482*0Sstevel@tonic-gate 	if (fstat(fd, &st) < 0)
483*0Sstevel@tonic-gate 		return 0;
484*0Sstevel@tonic-gate 	/*
485*0Sstevel@tonic-gate 	 * if a key owned by the user is accessed, then we check the
486*0Sstevel@tonic-gate 	 * permissions of the file. if the key owned by a different user,
487*0Sstevel@tonic-gate 	 * then we don't care.
488*0Sstevel@tonic-gate 	 */
489*0Sstevel@tonic-gate #ifdef HAVE_CYGWIN
490*0Sstevel@tonic-gate 	if (check_ntsec(filename))
491*0Sstevel@tonic-gate #endif
492*0Sstevel@tonic-gate 	if ((st.st_uid == getuid()) && (st.st_mode & 077) != 0) {
493*0Sstevel@tonic-gate 		error("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
494*0Sstevel@tonic-gate 		error("@         WARNING: UNPROTECTED PRIVATE KEY FILE!          @");
495*0Sstevel@tonic-gate 		error("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
496*0Sstevel@tonic-gate 		error("Permissions 0%3.3o for '%s' are too open.",
497*0Sstevel@tonic-gate 		    (int)(st.st_mode & 0777), filename);
498*0Sstevel@tonic-gate 		error("It is recommended that your private key files are NOT accessible by others.");
499*0Sstevel@tonic-gate 		error("This private key will be ignored.");
500*0Sstevel@tonic-gate 		return 0;
501*0Sstevel@tonic-gate 	}
502*0Sstevel@tonic-gate 	return 1;
503*0Sstevel@tonic-gate }
504*0Sstevel@tonic-gate 
505*0Sstevel@tonic-gate Key *
key_load_private_type(int type,const char * filename,const char * passphrase,char ** commentp)506*0Sstevel@tonic-gate key_load_private_type(int type, const char *filename, const char *passphrase,
507*0Sstevel@tonic-gate     char **commentp)
508*0Sstevel@tonic-gate {
509*0Sstevel@tonic-gate 	int fd;
510*0Sstevel@tonic-gate 
511*0Sstevel@tonic-gate 	fd = open(filename, O_RDONLY);
512*0Sstevel@tonic-gate 	if (fd < 0)
513*0Sstevel@tonic-gate 		return NULL;
514*0Sstevel@tonic-gate 	if (!key_perm_ok(fd, filename)) {
515*0Sstevel@tonic-gate 		error("bad permissions: ignore key: %s", filename);
516*0Sstevel@tonic-gate 		close(fd);
517*0Sstevel@tonic-gate 		return NULL;
518*0Sstevel@tonic-gate 	}
519*0Sstevel@tonic-gate 	switch (type) {
520*0Sstevel@tonic-gate 	case KEY_RSA1:
521*0Sstevel@tonic-gate 		return key_load_private_rsa1(fd, filename, passphrase,
522*0Sstevel@tonic-gate 		    commentp);
523*0Sstevel@tonic-gate 		/* closes fd */
524*0Sstevel@tonic-gate 		break;
525*0Sstevel@tonic-gate 	case KEY_DSA:
526*0Sstevel@tonic-gate 	case KEY_RSA:
527*0Sstevel@tonic-gate 	case KEY_UNSPEC:
528*0Sstevel@tonic-gate 		return key_load_private_pem(fd, type, passphrase, commentp);
529*0Sstevel@tonic-gate 		/* closes fd */
530*0Sstevel@tonic-gate 		break;
531*0Sstevel@tonic-gate 	default:
532*0Sstevel@tonic-gate 		close(fd);
533*0Sstevel@tonic-gate 		break;
534*0Sstevel@tonic-gate 	}
535*0Sstevel@tonic-gate 	return NULL;
536*0Sstevel@tonic-gate }
537*0Sstevel@tonic-gate 
538*0Sstevel@tonic-gate Key *
key_load_private(const char * filename,const char * passphrase,char ** commentp)539*0Sstevel@tonic-gate key_load_private(const char *filename, const char *passphrase,
540*0Sstevel@tonic-gate     char **commentp)
541*0Sstevel@tonic-gate {
542*0Sstevel@tonic-gate 	Key *pub, *prv;
543*0Sstevel@tonic-gate 	int fd;
544*0Sstevel@tonic-gate 
545*0Sstevel@tonic-gate 	fd = open(filename, O_RDONLY);
546*0Sstevel@tonic-gate 	if (fd < 0)
547*0Sstevel@tonic-gate 		return NULL;
548*0Sstevel@tonic-gate 	if (!key_perm_ok(fd, filename)) {
549*0Sstevel@tonic-gate 		error("bad permissions: ignore key: %s", filename);
550*0Sstevel@tonic-gate 		close(fd);
551*0Sstevel@tonic-gate 		return NULL;
552*0Sstevel@tonic-gate 	}
553*0Sstevel@tonic-gate 	pub = key_load_public_rsa1(fd, filename, commentp);
554*0Sstevel@tonic-gate 	lseek(fd, (off_t) 0, SEEK_SET);		/* rewind */
555*0Sstevel@tonic-gate 	if (pub == NULL) {
556*0Sstevel@tonic-gate 		/* closes fd */
557*0Sstevel@tonic-gate 		prv = key_load_private_pem(fd, KEY_UNSPEC, passphrase, NULL);
558*0Sstevel@tonic-gate 		/* use the filename as a comment for PEM */
559*0Sstevel@tonic-gate 		if (commentp && prv)
560*0Sstevel@tonic-gate 			*commentp = xstrdup(filename);
561*0Sstevel@tonic-gate 	} else {
562*0Sstevel@tonic-gate 		/* it's a SSH v1 key if the public key part is readable */
563*0Sstevel@tonic-gate 		key_free(pub);
564*0Sstevel@tonic-gate 		/* closes fd */
565*0Sstevel@tonic-gate 		prv = key_load_private_rsa1(fd, filename, passphrase, NULL);
566*0Sstevel@tonic-gate 	}
567*0Sstevel@tonic-gate 	return prv;
568*0Sstevel@tonic-gate }
569*0Sstevel@tonic-gate 
570*0Sstevel@tonic-gate static int
key_try_load_public(Key * k,const char * filename,char ** commentp)571*0Sstevel@tonic-gate key_try_load_public(Key *k, const char *filename, char **commentp)
572*0Sstevel@tonic-gate {
573*0Sstevel@tonic-gate 	FILE *f;
574*0Sstevel@tonic-gate 	char line[4096];
575*0Sstevel@tonic-gate 	char *cp;
576*0Sstevel@tonic-gate 
577*0Sstevel@tonic-gate 	f = fopen(filename, "r");
578*0Sstevel@tonic-gate 	if (f != NULL) {
579*0Sstevel@tonic-gate 		while (fgets(line, sizeof(line), f)) {
580*0Sstevel@tonic-gate 			line[sizeof(line)-1] = '\0';
581*0Sstevel@tonic-gate 			cp = line;
582*0Sstevel@tonic-gate 			switch (*cp) {
583*0Sstevel@tonic-gate 			case '#':
584*0Sstevel@tonic-gate 			case '\n':
585*0Sstevel@tonic-gate 			case '\0':
586*0Sstevel@tonic-gate 				continue;
587*0Sstevel@tonic-gate 			}
588*0Sstevel@tonic-gate 			/* Skip leading whitespace. */
589*0Sstevel@tonic-gate 			for (; *cp && (*cp == ' ' || *cp == '\t'); cp++)
590*0Sstevel@tonic-gate 				;
591*0Sstevel@tonic-gate 			if (*cp) {
592*0Sstevel@tonic-gate 				if (key_read(k, &cp) == 1) {
593*0Sstevel@tonic-gate 					if (commentp)
594*0Sstevel@tonic-gate 						*commentp=xstrdup(filename);
595*0Sstevel@tonic-gate 					fclose(f);
596*0Sstevel@tonic-gate 					return 1;
597*0Sstevel@tonic-gate 				}
598*0Sstevel@tonic-gate 			}
599*0Sstevel@tonic-gate 		}
600*0Sstevel@tonic-gate 		fclose(f);
601*0Sstevel@tonic-gate 	}
602*0Sstevel@tonic-gate 	return 0;
603*0Sstevel@tonic-gate }
604*0Sstevel@tonic-gate 
605*0Sstevel@tonic-gate /* load public key from ssh v1 private or any pubkey file */
606*0Sstevel@tonic-gate Key *
key_load_public(const char * filename,char ** commentp)607*0Sstevel@tonic-gate key_load_public(const char *filename, char **commentp)
608*0Sstevel@tonic-gate {
609*0Sstevel@tonic-gate 	Key *pub;
610*0Sstevel@tonic-gate 	char file[MAXPATHLEN];
611*0Sstevel@tonic-gate 
612*0Sstevel@tonic-gate 	pub = key_load_public_type(KEY_RSA1, filename, commentp);
613*0Sstevel@tonic-gate 	if (pub != NULL)
614*0Sstevel@tonic-gate 		return pub;
615*0Sstevel@tonic-gate 	pub = key_new(KEY_UNSPEC);
616*0Sstevel@tonic-gate 	if (key_try_load_public(pub, filename, commentp) == 1)
617*0Sstevel@tonic-gate 		return pub;
618*0Sstevel@tonic-gate 	if ((strlcpy(file, filename, sizeof file) < sizeof(file)) &&
619*0Sstevel@tonic-gate 	    (strlcat(file, ".pub", sizeof file) < sizeof(file)) &&
620*0Sstevel@tonic-gate 	    (key_try_load_public(pub, file, commentp) == 1))
621*0Sstevel@tonic-gate 		return pub;
622*0Sstevel@tonic-gate 	key_free(pub);
623*0Sstevel@tonic-gate 	return NULL;
624*0Sstevel@tonic-gate }
625