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