blob: 608d1d06fb90f64de8df1a656715fd1218f2a315 [file] [log] [blame]
Damien Miller2ce12ef2011-05-05 14:17:18 +10001/* $OpenBSD: authfile.c,v 1.88 2011/05/04 21:15:29 djm Exp $ */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002/*
Damien Miller95def091999-11-25 00:26:21 +11003 * Author: Tatu Ylonen <ylo@cs.hut.fi>
Damien Miller95def091999-11-25 00:26:21 +11004 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
5 * All rights reserved
Damien Miller95def091999-11-25 00:26:21 +11006 * This file contains functions for reading and writing identity files, and
7 * for reading the passphrase from the user.
Damien Miller4af51302000-04-16 11:18:38 +10008 *
Damien Millere4340be2000-09-16 13:29:08 +11009 * As far as I am concerned, the code I have written for this software
10 * can be used freely for any purpose. Any derived versions of this
11 * software must be clearly marked as such, and if the derived work is
12 * incompatible with the protocol description in the RFC file, it must be
13 * called by a name other than "ssh" or "Secure Shell".
14 *
15 *
16 * Copyright (c) 2000 Markus Friedl. All rights reserved.
17 *
18 * Redistribution and use in source and binary forms, with or without
19 * modification, are permitted provided that the following conditions
20 * are met:
21 * 1. Redistributions of source code must retain the above copyright
22 * notice, this list of conditions and the following disclaimer.
23 * 2. Redistributions in binary form must reproduce the above copyright
24 * notice, this list of conditions and the following disclaimer in the
25 * documentation and/or other materials provided with the distribution.
26 *
27 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
28 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
29 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
30 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
31 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
32 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
33 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
34 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
35 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
36 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Damien Miller95def091999-11-25 00:26:21 +110037 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100038
39#include "includes.h"
Damien Millerf17883e2006-03-15 11:45:54 +110040
41#include <sys/types.h>
42#include <sys/stat.h>
Damien Miller8dbffe72006-08-05 11:02:17 +100043#include <sys/param.h>
Damien Millerd7834352006-08-05 12:39:39 +100044#include <sys/uio.h>
Damien Millerd4a8b7e1999-10-27 13:42:43 +100045
Damien Miller0bc1bd82000-11-13 22:57:25 +110046#include <openssl/err.h>
Damien Millereba71ba2000-04-29 23:57:08 +100047#include <openssl/evp.h>
Ben Lindstrom226cfa02001-01-22 05:34:40 +000048#include <openssl/pem.h>
Damien Millereba71ba2000-04-29 23:57:08 +100049
Darren Tuckere89ed1c2009-11-05 20:43:16 +110050/* compatibility with old or broken OpenSSL versions */
51#include "openbsd-compat/openssl-compat.h"
52
Darren Tuckerba724052006-07-12 22:24:22 +100053#include <errno.h>
Damien Miller57cf6382006-07-10 21:13:46 +100054#include <fcntl.h>
Damien Millerded319c2006-09-01 15:38:36 +100055#include <stdarg.h>
Damien Millera7a73ee2006-08-05 11:37:59 +100056#include <stdio.h>
Damien Millere7a1e5c2006-08-05 11:34:19 +100057#include <stdlib.h>
Damien Millere3476ed2006-07-24 14:13:33 +100058#include <string.h>
Damien Millere6b3b612006-07-24 14:01:23 +100059#include <unistd.h>
Damien Miller57cf6382006-07-10 21:13:46 +100060
Damien Millerd4a8b7e1999-10-27 13:42:43 +100061#include "xmalloc.h"
Damien Millerd7834352006-08-05 12:39:39 +100062#include "cipher.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100063#include "buffer.h"
Damien Millereba71ba2000-04-29 23:57:08 +100064#include "key.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000065#include "ssh.h"
66#include "log.h"
Ben Lindstrom31ca54a2001-02-09 02:11:24 +000067#include "authfile.h"
Damien Miller040b64f2002-01-22 23:10:04 +110068#include "rsa.h"
Darren Tuckerf0f90982004-12-11 13:39:50 +110069#include "misc.h"
Damien Millereccb9de2005-06-17 12:59:34 +100070#include "atomicio.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100071
Damien Miller2ce12ef2011-05-05 14:17:18 +100072#define MAX_KEY_FILE_SIZE (1024 * 1024)
73
Ben Lindstromd0fca422001-03-26 13:44:06 +000074/* Version identification string for SSH v1 identity files. */
Ben Lindstrom1170d712001-01-29 07:51:26 +000075static const char authfile_id_string[] =
76 "SSH PRIVATE KEY FILE FORMAT 1.1\n";
Damien Millerd4a8b7e1999-10-27 13:42:43 +100077
Damien Miller5428f641999-11-25 11:54:57 +110078/*
Damien Millera2327922010-12-01 12:01:21 +110079 * Serialises the authentication (private) key to a blob, encrypting it with
80 * passphrase. The identification of the blob (lowest 64 bits of n) will
Damien Miller5428f641999-11-25 11:54:57 +110081 * precede the key to provide identification of the key without needing a
82 * passphrase.
83 */
Ben Lindstrombba81212001-06-25 05:01:22 +000084static int
Damien Millera2327922010-12-01 12:01:21 +110085key_private_rsa1_to_blob(Key *key, Buffer *blob, const char *passphrase,
Ben Lindstromd0fca422001-03-26 13:44:06 +000086 const char *comment)
Damien Millerd4a8b7e1999-10-27 13:42:43 +100087{
Damien Miller95def091999-11-25 00:26:21 +110088 Buffer buffer, encrypted;
Damien Miller708d21c2002-01-22 23:18:15 +110089 u_char buf[100], *cp;
Damien Millera2327922010-12-01 12:01:21 +110090 int i, cipher_num;
Damien Miller874d77b2000-10-14 16:23:11 +110091 CipherContext ciphercontext;
92 Cipher *cipher;
Darren Tucker3f9fdc72004-06-22 12:56:01 +100093 u_int32_t rnd;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100094
Damien Miller5428f641999-11-25 11:54:57 +110095 /*
96 * If the passphrase is empty, use SSH_CIPHER_NONE to ease converting
97 * to another cipher; otherwise use SSH_AUTHFILE_CIPHER.
98 */
Damien Miller963f6b22002-02-19 15:21:23 +110099 cipher_num = (strcmp(passphrase, "") == 0) ?
100 SSH_CIPHER_NONE : SSH_AUTHFILE_CIPHER;
101 if ((cipher = cipher_by_number(cipher_num)) == NULL)
Damien Miller874d77b2000-10-14 16:23:11 +1100102 fatal("save_private_key_rsa: bad cipher");
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000103
Damien Miller95def091999-11-25 00:26:21 +1100104 /* This buffer is used to built the secret part of the private key. */
105 buffer_init(&buffer);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000106
Damien Miller95def091999-11-25 00:26:21 +1100107 /* Put checkbytes for checking passphrase validity. */
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000108 rnd = arc4random();
109 buf[0] = rnd & 0xff;
110 buf[1] = (rnd >> 8) & 0xff;
Damien Miller95def091999-11-25 00:26:21 +1100111 buf[2] = buf[0];
112 buf[3] = buf[1];
113 buffer_append(&buffer, buf, 4);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000114
Damien Miller5428f641999-11-25 11:54:57 +1100115 /*
116 * Store the private key (n and e will not be stored because they
117 * will be stored in plain text, and storing them also in encrypted
118 * format would just give known plaintext).
119 */
Ben Lindstromd0fca422001-03-26 13:44:06 +0000120 buffer_put_bignum(&buffer, key->rsa->d);
121 buffer_put_bignum(&buffer, key->rsa->iqmp);
122 buffer_put_bignum(&buffer, key->rsa->q); /* reverse from SSL p */
123 buffer_put_bignum(&buffer, key->rsa->p); /* reverse from SSL q */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000124
Damien Miller95def091999-11-25 00:26:21 +1100125 /* Pad the part to be encrypted until its size is a multiple of 8. */
126 while (buffer_len(&buffer) % 8 != 0)
127 buffer_put_char(&buffer, 0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000128
Damien Miller95def091999-11-25 00:26:21 +1100129 /* This buffer will be used to contain the data in the file. */
130 buffer_init(&encrypted);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000131
Damien Miller95def091999-11-25 00:26:21 +1100132 /* First store keyfile id string. */
Ben Lindstrom1170d712001-01-29 07:51:26 +0000133 for (i = 0; authfile_id_string[i]; i++)
134 buffer_put_char(&encrypted, authfile_id_string[i]);
Damien Miller95def091999-11-25 00:26:21 +1100135 buffer_put_char(&encrypted, 0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000136
Damien Miller95def091999-11-25 00:26:21 +1100137 /* Store cipher type. */
Damien Miller963f6b22002-02-19 15:21:23 +1100138 buffer_put_char(&encrypted, cipher_num);
Damien Miller95def091999-11-25 00:26:21 +1100139 buffer_put_int(&encrypted, 0); /* For future extension */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000140
Damien Miller95def091999-11-25 00:26:21 +1100141 /* Store public key. This will be in plain text. */
Ben Lindstromd0fca422001-03-26 13:44:06 +0000142 buffer_put_int(&encrypted, BN_num_bits(key->rsa->n));
143 buffer_put_bignum(&encrypted, key->rsa->n);
144 buffer_put_bignum(&encrypted, key->rsa->e);
Ben Lindstrom664408d2001-06-09 01:42:01 +0000145 buffer_put_cstring(&encrypted, comment);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000146
Damien Miller95def091999-11-25 00:26:21 +1100147 /* Allocate space for the private part of the key in the buffer. */
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100148 cp = buffer_append_space(&encrypted, buffer_len(&buffer));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000149
Damien Miller963f6b22002-02-19 15:21:23 +1100150 cipher_set_key_string(&ciphercontext, cipher, passphrase,
151 CIPHER_ENCRYPT);
152 cipher_crypt(&ciphercontext, cp,
Damien Miller708d21c2002-01-22 23:18:15 +1100153 buffer_ptr(&buffer), buffer_len(&buffer));
Damien Miller963f6b22002-02-19 15:21:23 +1100154 cipher_cleanup(&ciphercontext);
Damien Miller874d77b2000-10-14 16:23:11 +1100155 memset(&ciphercontext, 0, sizeof(ciphercontext));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000156
Damien Miller95def091999-11-25 00:26:21 +1100157 /* Destroy temporary data. */
158 memset(buf, 0, sizeof(buf));
159 buffer_free(&buffer);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000160
Damien Millera2327922010-12-01 12:01:21 +1100161 buffer_append(blob, buffer_ptr(&encrypted), buffer_len(&encrypted));
Damien Miller95def091999-11-25 00:26:21 +1100162 buffer_free(&encrypted);
Damien Millera2327922010-12-01 12:01:21 +1100163
Damien Miller95def091999-11-25 00:26:21 +1100164 return 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000165}
166
Damien Millera2327922010-12-01 12:01:21 +1100167/* convert SSH v2 key in OpenSSL PEM format */
Ben Lindstrombba81212001-06-25 05:01:22 +0000168static int
Damien Millera2327922010-12-01 12:01:21 +1100169key_private_pem_to_blob(Key *key, Buffer *blob, const char *_passphrase,
Ben Lindstromd0fca422001-03-26 13:44:06 +0000170 const char *comment)
Damien Millereba71ba2000-04-29 23:57:08 +1000171{
Damien Miller0bc1bd82000-11-13 22:57:25 +1100172 int success = 0;
Damien Millera2327922010-12-01 12:01:21 +1100173 int blen, len = strlen(_passphrase);
Ben Lindstrom90fd8142002-02-26 18:09:42 +0000174 u_char *passphrase = (len > 0) ? (u_char *)_passphrase : NULL;
Darren Tuckerdf6578b2009-11-07 16:03:14 +1100175#if (OPENSSL_VERSION_NUMBER < 0x00907000L)
176 const EVP_CIPHER *cipher = (len > 0) ? EVP_des_ede3_cbc() : NULL;
177#else
Darren Tuckerdfb9b712009-10-24 11:46:43 +1100178 const EVP_CIPHER *cipher = (len > 0) ? EVP_aes_128_cbc() : NULL;
Darren Tuckerdf6578b2009-11-07 16:03:14 +1100179#endif
Damien Millera2327922010-12-01 12:01:21 +1100180 const u_char *bptr;
181 BIO *bio;
Damien Millereba71ba2000-04-29 23:57:08 +1000182
183 if (len > 0 && len <= 4) {
Ben Lindstrom15f33862001-04-16 02:00:02 +0000184 error("passphrase too short: have %d bytes, need > 4", len);
Damien Millereba71ba2000-04-29 23:57:08 +1000185 return 0;
186 }
Damien Millera2327922010-12-01 12:01:21 +1100187 if ((bio = BIO_new(BIO_s_mem())) == NULL) {
188 error("%s: BIO_new failed", __func__);
Damien Millereba71ba2000-04-29 23:57:08 +1000189 return 0;
190 }
Damien Miller0bc1bd82000-11-13 22:57:25 +1100191 switch (key->type) {
Ben Lindstromc1116602001-03-29 00:28:37 +0000192 case KEY_DSA:
Damien Millera2327922010-12-01 12:01:21 +1100193 success = PEM_write_bio_DSAPrivateKey(bio, key->dsa,
Ben Lindstromc1116602001-03-29 00:28:37 +0000194 cipher, passphrase, len, NULL, NULL);
195 break;
Damien Miller6af914a2010-09-10 11:39:26 +1000196#ifdef OPENSSL_HAS_ECC
Damien Millereb8b60e2010-08-31 22:41:14 +1000197 case KEY_ECDSA:
Damien Millera2327922010-12-01 12:01:21 +1100198 success = PEM_write_bio_ECPrivateKey(bio, key->ecdsa,
Damien Millereb8b60e2010-08-31 22:41:14 +1000199 cipher, passphrase, len, NULL, NULL);
200 break;
Damien Miller6af914a2010-09-10 11:39:26 +1000201#endif
Ben Lindstromc1116602001-03-29 00:28:37 +0000202 case KEY_RSA:
Damien Millera2327922010-12-01 12:01:21 +1100203 success = PEM_write_bio_RSAPrivateKey(bio, key->rsa,
Ben Lindstromc1116602001-03-29 00:28:37 +0000204 cipher, passphrase, len, NULL, NULL);
205 break;
Damien Millereba71ba2000-04-29 23:57:08 +1000206 }
Damien Millera2327922010-12-01 12:01:21 +1100207 if (success) {
208 if ((blen = BIO_get_mem_data(bio, &bptr)) <= 0)
209 success = 0;
210 else
211 buffer_append(blob, bptr, blen);
212 }
213 BIO_free(bio);
Damien Millereba71ba2000-04-29 23:57:08 +1000214 return success;
215}
216
Damien Millera2327922010-12-01 12:01:21 +1100217/* Save a key blob to a file */
218static int
219key_save_private_blob(Buffer *keybuf, const char *filename)
220{
221 int fd;
222
223 if ((fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0600)) < 0) {
224 error("open %s failed: %s.", filename, strerror(errno));
225 return 0;
226 }
227 if (atomicio(vwrite, fd, buffer_ptr(keybuf),
228 buffer_len(keybuf)) != buffer_len(keybuf)) {
229 error("write to key file %s failed: %s", filename,
230 strerror(errno));
231 close(fd);
232 unlink(filename);
233 return 0;
234 }
235 close(fd);
236 return 1;
237}
238
239/* Serialise "key" to buffer "blob" */
240static int
241key_private_to_blob(Key *key, Buffer *blob, const char *passphrase,
242 const char *comment)
243{
244 switch (key->type) {
245 case KEY_RSA1:
246 return key_private_rsa1_to_blob(key, blob, passphrase, comment);
247 case KEY_DSA:
248 case KEY_ECDSA:
249 case KEY_RSA:
250 return key_private_pem_to_blob(key, blob, passphrase, comment);
251 default:
252 error("%s: cannot save key type %d", __func__, key->type);
253 return 0;
254 }
255}
256
Damien Millereba71ba2000-04-29 23:57:08 +1000257int
Ben Lindstromd0fca422001-03-26 13:44:06 +0000258key_save_private(Key *key, const char *filename, const char *passphrase,
Damien Millereba71ba2000-04-29 23:57:08 +1000259 const char *comment)
260{
Damien Millera2327922010-12-01 12:01:21 +1100261 Buffer keyblob;
262 int success = 0;
263
264 buffer_init(&keyblob);
265 if (!key_private_to_blob(key, &keyblob, passphrase, comment))
266 goto out;
267 if (!key_save_private_blob(&keyblob, filename))
268 goto out;
269 success = 1;
270 out:
271 buffer_free(&keyblob);
272 return success;
273}
274
275/*
276 * Parse the public, unencrypted portion of a RSA1 key.
277 */
278static Key *
279key_parse_public_rsa1(Buffer *blob, char **commentp)
280{
281 Key *pub;
282
283 /* Check that it is at least big enough to contain the ID string. */
284 if (buffer_len(blob) < sizeof(authfile_id_string)) {
285 debug3("Truncated RSA1 identifier");
286 return NULL;
Damien Millereba71ba2000-04-29 23:57:08 +1000287 }
Damien Millera2327922010-12-01 12:01:21 +1100288
289 /*
290 * Make sure it begins with the id string. Consume the id string
291 * from the buffer.
292 */
293 if (memcmp(buffer_ptr(blob), authfile_id_string,
294 sizeof(authfile_id_string)) != 0) {
295 debug3("Incorrect RSA1 identifier");
296 return NULL;
297 }
298 buffer_consume(blob, sizeof(authfile_id_string));
299
300 /* Skip cipher type and reserved data. */
301 (void) buffer_get_char(blob); /* cipher type */
302 (void) buffer_get_int(blob); /* reserved */
303
304 /* Read the public key from the buffer. */
305 (void) buffer_get_int(blob);
306 pub = key_new(KEY_RSA1);
307 buffer_get_bignum(blob, pub->rsa->n);
308 buffer_get_bignum(blob, pub->rsa->e);
309 if (commentp)
310 *commentp = buffer_get_string(blob, NULL);
311 /* The encrypted private part is not parsed by this function. */
312 buffer_clear(blob);
313
314 return pub;
315}
316
Damien Miller2ce12ef2011-05-05 14:17:18 +1000317/* Load a key from a fd into a buffer */
318int
Damien Millera2327922010-12-01 12:01:21 +1100319key_load_file(int fd, const char *filename, Buffer *blob)
320{
Damien Miller2ce12ef2011-05-05 14:17:18 +1000321 u_char buf[1024];
Damien Millera2327922010-12-01 12:01:21 +1100322 size_t len;
Damien Millera2327922010-12-01 12:01:21 +1100323 struct stat st;
324
325 if (fstat(fd, &st) < 0) {
326 error("%s: fstat of key file %.200s%sfailed: %.100s", __func__,
327 filename == NULL ? "" : filename,
328 filename == NULL ? "" : " ",
329 strerror(errno));
Damien Millera2327922010-12-01 12:01:21 +1100330 return 0;
331 }
Damien Miller2ce12ef2011-05-05 14:17:18 +1000332 if ((st.st_mode & (S_IFSOCK|S_IFCHR|S_IFIFO)) == 0 &&
333 st.st_size > MAX_KEY_FILE_SIZE) {
334 toobig:
Damien Millera2327922010-12-01 12:01:21 +1100335 error("%s: key file %.200s%stoo large", __func__,
336 filename == NULL ? "" : filename,
337 filename == NULL ? "" : " ");
Damien Millera2327922010-12-01 12:01:21 +1100338 return 0;
339 }
Damien Millera2327922010-12-01 12:01:21 +1100340 buffer_init(blob);
Damien Miller2ce12ef2011-05-05 14:17:18 +1000341 for (;;) {
342 if ((len = atomicio(read, fd, buf, sizeof(buf))) == 0) {
343 if (errno == EPIPE)
344 break;
345 debug("%s: read from key file %.200s%sfailed: %.100s",
346 __func__, filename == NULL ? "" : filename,
347 filename == NULL ? "" : " ", strerror(errno));
348 buffer_clear(blob);
349 bzero(buf, sizeof(buf));
350 return 0;
351 }
352 buffer_append(blob, buf, len);
353 if (buffer_len(blob) > MAX_KEY_FILE_SIZE) {
354 buffer_clear(blob);
355 bzero(buf, sizeof(buf));
356 goto toobig;
357 }
358 }
359 bzero(buf, sizeof(buf));
360 if ((st.st_mode & (S_IFSOCK|S_IFCHR|S_IFIFO)) == 0 &&
361 st.st_size != buffer_len(blob)) {
362 debug("%s: key file %.200s%schanged size while reading",
363 __func__, filename == NULL ? "" : filename,
364 filename == NULL ? "" : " ");
Damien Millera2327922010-12-01 12:01:21 +1100365 buffer_clear(blob);
Damien Millera2327922010-12-01 12:01:21 +1100366 return 0;
367 }
Damien Miller2ce12ef2011-05-05 14:17:18 +1000368
Damien Millera2327922010-12-01 12:01:21 +1100369 return 1;
Damien Millereba71ba2000-04-29 23:57:08 +1000370}
371
Damien Miller5428f641999-11-25 11:54:57 +1100372/*
Ben Lindstromd0fca422001-03-26 13:44:06 +0000373 * Loads the public part of the ssh v1 key file. Returns NULL if an error was
374 * encountered (the file does not exist or is not readable), and the key
Damien Miller5428f641999-11-25 11:54:57 +1100375 * otherwise.
376 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000377static Key *
Ben Lindstromd0fca422001-03-26 13:44:06 +0000378key_load_public_rsa1(int fd, const char *filename, char **commentp)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000379{
Damien Miller95def091999-11-25 00:26:21 +1100380 Buffer buffer;
Ben Lindstromd0fca422001-03-26 13:44:06 +0000381 Key *pub;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000382
Damien Miller95def091999-11-25 00:26:21 +1100383 buffer_init(&buffer);
Damien Millera2327922010-12-01 12:01:21 +1100384 if (!key_load_file(fd, filename, &buffer)) {
Damien Miller95def091999-11-25 00:26:21 +1100385 buffer_free(&buffer);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000386 return NULL;
Damien Miller95def091999-11-25 00:26:21 +1100387 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000388
Damien Millera2327922010-12-01 12:01:21 +1100389 pub = key_parse_public_rsa1(&buffer, commentp);
390 if (pub == NULL)
391 debug3("Could not load \"%s\" as a RSA1 public key", filename);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000392 buffer_free(&buffer);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000393 return pub;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000394}
395
Ben Lindstromd0fca422001-03-26 13:44:06 +0000396/* load public key from private-key file, works only for SSH v1 */
397Key *
398key_load_public_type(int type, const char *filename, char **commentp)
Damien Millereba71ba2000-04-29 23:57:08 +1000399{
Ben Lindstromd0fca422001-03-26 13:44:06 +0000400 Key *pub;
401 int fd;
402
403 if (type == KEY_RSA1) {
404 fd = open(filename, O_RDONLY);
405 if (fd < 0)
406 return NULL;
407 pub = key_load_public_rsa1(fd, filename, commentp);
408 close(fd);
409 return pub;
Damien Millereba71ba2000-04-29 23:57:08 +1000410 }
Ben Lindstromd0fca422001-03-26 13:44:06 +0000411 return NULL;
Damien Millereba71ba2000-04-29 23:57:08 +1000412}
413
Ben Lindstrombba81212001-06-25 05:01:22 +0000414static Key *
Damien Millera2327922010-12-01 12:01:21 +1100415key_parse_private_rsa1(Buffer *blob, const char *passphrase, char **commentp)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000416{
Damien Millereccb9de2005-06-17 12:59:34 +1000417 int check1, check2, cipher_type;
Damien Millera2327922010-12-01 12:01:21 +1100418 Buffer decrypted;
Damien Miller708d21c2002-01-22 23:18:15 +1100419 u_char *cp;
Damien Miller874d77b2000-10-14 16:23:11 +1100420 CipherContext ciphercontext;
421 Cipher *cipher;
Ben Lindstromd0fca422001-03-26 13:44:06 +0000422 Key *prv = NULL;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000423
Ben Lindstrom1170d712001-01-29 07:51:26 +0000424 /* Check that it is at least big enough to contain the ID string. */
Damien Millera2327922010-12-01 12:01:21 +1100425 if (buffer_len(blob) < sizeof(authfile_id_string)) {
426 debug3("Truncated RSA1 identifier");
Ben Lindstromd0fca422001-03-26 13:44:06 +0000427 return NULL;
Damien Miller95def091999-11-25 00:26:21 +1100428 }
Damien Millera2327922010-12-01 12:01:21 +1100429
Damien Miller5428f641999-11-25 11:54:57 +1100430 /*
431 * Make sure it begins with the id string. Consume the id string
432 * from the buffer.
433 */
Damien Millera2327922010-12-01 12:01:21 +1100434 if (memcmp(buffer_ptr(blob), authfile_id_string,
435 sizeof(authfile_id_string)) != 0) {
436 debug3("Incorrect RSA1 identifier");
437 return NULL;
438 }
439 buffer_consume(blob, sizeof(authfile_id_string));
Ben Lindstromb257cca2001-03-05 04:59:27 +0000440
Damien Miller95def091999-11-25 00:26:21 +1100441 /* Read cipher type. */
Damien Millera2327922010-12-01 12:01:21 +1100442 cipher_type = buffer_get_char(blob);
443 (void) buffer_get_int(blob); /* Reserved data. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000444
Damien Miller95def091999-11-25 00:26:21 +1100445 /* Read the public key from the buffer. */
Damien Millera2327922010-12-01 12:01:21 +1100446 (void) buffer_get_int(blob);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000447 prv = key_new_private(KEY_RSA1);
448
Damien Millera2327922010-12-01 12:01:21 +1100449 buffer_get_bignum(blob, prv->rsa->n);
450 buffer_get_bignum(blob, prv->rsa->e);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000451 if (commentp)
Damien Millera2327922010-12-01 12:01:21 +1100452 *commentp = buffer_get_string(blob, NULL);
Damien Miller95def091999-11-25 00:26:21 +1100453 else
Damien Millera2327922010-12-01 12:01:21 +1100454 (void)buffer_get_string_ptr(blob, NULL);
Damien Miller95def091999-11-25 00:26:21 +1100455
456 /* Check that it is a supported cipher. */
Damien Miller874d77b2000-10-14 16:23:11 +1100457 cipher = cipher_by_number(cipher_type);
458 if (cipher == NULL) {
Damien Millera2327922010-12-01 12:01:21 +1100459 debug("Unsupported RSA1 cipher %d", cipher_type);
Damien Miller95def091999-11-25 00:26:21 +1100460 goto fail;
461 }
462 /* Initialize space for decrypted data. */
463 buffer_init(&decrypted);
Damien Millera2327922010-12-01 12:01:21 +1100464 cp = buffer_append_space(&decrypted, buffer_len(blob));
Damien Miller95def091999-11-25 00:26:21 +1100465
466 /* Rest of the buffer is encrypted. Decrypt it using the passphrase. */
Damien Miller963f6b22002-02-19 15:21:23 +1100467 cipher_set_key_string(&ciphercontext, cipher, passphrase,
468 CIPHER_DECRYPT);
469 cipher_crypt(&ciphercontext, cp,
Damien Millera2327922010-12-01 12:01:21 +1100470 buffer_ptr(blob), buffer_len(blob));
Damien Miller963f6b22002-02-19 15:21:23 +1100471 cipher_cleanup(&ciphercontext);
Damien Miller874d77b2000-10-14 16:23:11 +1100472 memset(&ciphercontext, 0, sizeof(ciphercontext));
Damien Millera2327922010-12-01 12:01:21 +1100473 buffer_clear(blob);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000474
Damien Miller95def091999-11-25 00:26:21 +1100475 check1 = buffer_get_char(&decrypted);
476 check2 = buffer_get_char(&decrypted);
477 if (check1 != buffer_get_char(&decrypted) ||
478 check2 != buffer_get_char(&decrypted)) {
479 if (strcmp(passphrase, "") != 0)
Damien Millera2327922010-12-01 12:01:21 +1100480 debug("Bad passphrase supplied for RSA1 key");
Damien Miller95def091999-11-25 00:26:21 +1100481 /* Bad passphrase. */
482 buffer_free(&decrypted);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000483 goto fail;
Damien Miller95def091999-11-25 00:26:21 +1100484 }
485 /* Read the rest of the private key. */
Ben Lindstromd0fca422001-03-26 13:44:06 +0000486 buffer_get_bignum(&decrypted, prv->rsa->d);
487 buffer_get_bignum(&decrypted, prv->rsa->iqmp); /* u */
488 /* in SSL and SSH v1 p and q are exchanged */
489 buffer_get_bignum(&decrypted, prv->rsa->q); /* p */
490 buffer_get_bignum(&decrypted, prv->rsa->p); /* q */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000491
Ben Lindstromd0fca422001-03-26 13:44:06 +0000492 /* calculate p-1 and q-1 */
Damien Millerda755162002-01-22 23:09:22 +1100493 rsa_generate_additional_parameters(prv->rsa);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000494
Damien Miller95def091999-11-25 00:26:21 +1100495 buffer_free(&decrypted);
Damien Millered33d3b2003-03-15 11:36:18 +1100496
497 /* enable blinding */
498 if (RSA_blinding_on(prv->rsa, NULL) != 1) {
Damien Millera2327922010-12-01 12:01:21 +1100499 error("%s: RSA_blinding_on failed", __func__);
Damien Millered33d3b2003-03-15 11:36:18 +1100500 goto fail;
501 }
Ben Lindstromd0fca422001-03-26 13:44:06 +0000502 return prv;
503
504fail:
505 if (commentp)
506 xfree(*commentp);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000507 key_free(prv);
508 return NULL;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000509}
Damien Millereba71ba2000-04-29 23:57:08 +1000510
Damien Millera2327922010-12-01 12:01:21 +1100511static Key *
512key_parse_private_pem(Buffer *blob, int type, const char *passphrase,
Ben Lindstromd0fca422001-03-26 13:44:06 +0000513 char **commentp)
Damien Millereba71ba2000-04-29 23:57:08 +1000514{
Damien Miller0bc1bd82000-11-13 22:57:25 +1100515 EVP_PKEY *pk = NULL;
Ben Lindstromd0fca422001-03-26 13:44:06 +0000516 Key *prv = NULL;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100517 char *name = "<no key>";
Damien Millera2327922010-12-01 12:01:21 +1100518 BIO *bio;
Damien Millereba71ba2000-04-29 23:57:08 +1000519
Damien Millera2327922010-12-01 12:01:21 +1100520 if ((bio = BIO_new_mem_buf(buffer_ptr(blob),
521 buffer_len(blob))) == NULL) {
522 error("%s: BIO_new_mem_buf failed", __func__);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000523 return NULL;
Damien Millereba71ba2000-04-29 23:57:08 +1000524 }
Damien Millera2327922010-12-01 12:01:21 +1100525
526 pk = PEM_read_bio_PrivateKey(bio, NULL, NULL, (char *)passphrase);
527 BIO_free(bio);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100528 if (pk == NULL) {
Damien Millera2327922010-12-01 12:01:21 +1100529 debug("%s: PEM_read_PrivateKey failed", __func__);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100530 (void)ERR_get_error();
Ben Lindstromd0fca422001-03-26 13:44:06 +0000531 } else if (pk->type == EVP_PKEY_RSA &&
Damien Miller9f0f5c62001-12-21 14:45:46 +1100532 (type == KEY_UNSPEC||type==KEY_RSA)) {
Ben Lindstromd0fca422001-03-26 13:44:06 +0000533 prv = key_new(KEY_UNSPEC);
534 prv->rsa = EVP_PKEY_get1_RSA(pk);
535 prv->type = KEY_RSA;
536 name = "rsa w/o comment";
Damien Miller0bc1bd82000-11-13 22:57:25 +1100537#ifdef DEBUG_PK
Ben Lindstromd0fca422001-03-26 13:44:06 +0000538 RSA_print_fp(stderr, prv->rsa, 8);
Damien Millereba71ba2000-04-29 23:57:08 +1000539#endif
Damien Millered33d3b2003-03-15 11:36:18 +1100540 if (RSA_blinding_on(prv->rsa, NULL) != 1) {
Damien Millera2327922010-12-01 12:01:21 +1100541 error("%s: RSA_blinding_on failed", __func__);
Damien Millered33d3b2003-03-15 11:36:18 +1100542 key_free(prv);
543 prv = NULL;
544 }
Ben Lindstromd0fca422001-03-26 13:44:06 +0000545 } else if (pk->type == EVP_PKEY_DSA &&
Damien Miller9f0f5c62001-12-21 14:45:46 +1100546 (type == KEY_UNSPEC||type==KEY_DSA)) {
Ben Lindstromd0fca422001-03-26 13:44:06 +0000547 prv = key_new(KEY_UNSPEC);
548 prv->dsa = EVP_PKEY_get1_DSA(pk);
549 prv->type = KEY_DSA;
550 name = "dsa w/o comment";
Damien Miller0bc1bd82000-11-13 22:57:25 +1100551#ifdef DEBUG_PK
Ben Lindstromd0fca422001-03-26 13:44:06 +0000552 DSA_print_fp(stderr, prv->dsa, 8);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100553#endif
Damien Miller6af914a2010-09-10 11:39:26 +1000554#ifdef OPENSSL_HAS_ECC
Damien Millereb8b60e2010-08-31 22:41:14 +1000555 } else if (pk->type == EVP_PKEY_EC &&
556 (type == KEY_UNSPEC||type==KEY_ECDSA)) {
557 prv = key_new(KEY_UNSPEC);
558 prv->ecdsa = EVP_PKEY_get1_EC_KEY(pk);
559 prv->type = KEY_ECDSA;
Damien Millerb472a902010-11-05 10:19:49 +1100560 if ((prv->ecdsa_nid = key_ecdsa_key_to_nid(prv->ecdsa)) == -1 ||
561 key_curve_nid_to_name(prv->ecdsa_nid) == NULL ||
562 key_ec_validate_public(EC_KEY_get0_group(prv->ecdsa),
Damien Millereb8b60e2010-08-31 22:41:14 +1000563 EC_KEY_get0_public_key(prv->ecdsa)) != 0 ||
564 key_ec_validate_private(prv->ecdsa) != 0) {
565 error("%s: bad ECDSA key", __func__);
566 key_free(prv);
567 prv = NULL;
568 }
Damien Millerbf0423e2010-09-10 11:20:38 +1000569 name = "ecdsa w/o comment";
Damien Millereb8b60e2010-08-31 22:41:14 +1000570#ifdef DEBUG_PK
Damien Millerb472a902010-11-05 10:19:49 +1100571 if (prv != NULL && prv->ecdsa != NULL)
Damien Millereb8b60e2010-08-31 22:41:14 +1000572 key_dump_ec_key(prv->ecdsa);
573#endif
Damien Miller6af914a2010-09-10 11:39:26 +1000574#endif /* OPENSSL_HAS_ECC */
Damien Miller0bc1bd82000-11-13 22:57:25 +1100575 } else {
Damien Millera2327922010-12-01 12:01:21 +1100576 error("%s: PEM_read_PrivateKey: mismatch or "
577 "unknown EVP_PKEY save_type %d", __func__, pk->save_type);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100578 }
Damien Miller0bc1bd82000-11-13 22:57:25 +1100579 if (pk != NULL)
580 EVP_PKEY_free(pk);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000581 if (prv != NULL && commentp)
582 *commentp = xstrdup(name);
583 debug("read PEM private key done: type %s",
584 prv ? key_type(prv) : "<unknown>");
585 return prv;
Damien Millereba71ba2000-04-29 23:57:08 +1000586}
587
Damien Millera2327922010-12-01 12:01:21 +1100588Key *
589key_load_private_pem(int fd, int type, const char *passphrase,
590 char **commentp)
591{
592 Buffer buffer;
593 Key *prv;
594
595 buffer_init(&buffer);
596 if (!key_load_file(fd, NULL, &buffer)) {
597 buffer_free(&buffer);
598 return NULL;
599 }
600 prv = key_parse_private_pem(&buffer, type, passphrase, commentp);
601 buffer_free(&buffer);
602 return prv;
603}
604
Damien Miller8275fad2006-03-15 12:06:23 +1100605int
Ben Lindstromd0fca422001-03-26 13:44:06 +0000606key_perm_ok(int fd, const char *filename)
Damien Millereba71ba2000-04-29 23:57:08 +1000607{
Damien Millereba71ba2000-04-29 23:57:08 +1000608 struct stat st;
609
Ben Lindstrom7aff2612001-09-23 13:53:22 +0000610 if (fstat(fd, &st) < 0)
611 return 0;
612 /*
613 * if a key owned by the user is accessed, then we check the
614 * permissions of the file. if the key owned by a different user,
615 * then we don't care.
616 */
Damien Millerb70b61f2000-09-16 16:25:12 +1100617#ifdef HAVE_CYGWIN
Damien Millercb5e44a2000-09-29 12:12:36 +1100618 if (check_ntsec(filename))
Damien Millerb70b61f2000-09-16 16:25:12 +1100619#endif
Ben Lindstrom7aff2612001-09-23 13:53:22 +0000620 if ((st.st_uid == getuid()) && (st.st_mode & 077) != 0) {
Damien Millereba71ba2000-04-29 23:57:08 +1000621 error("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
622 error("@ WARNING: UNPROTECTED PRIVATE KEY FILE! @");
623 error("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
Ben Lindstrom7aff2612001-09-23 13:53:22 +0000624 error("Permissions 0%3.3o for '%s' are too open.",
Damien Miller04bd8b02003-05-25 14:38:33 +1000625 (u_int)st.st_mode & 0777, filename);
Damien Millera10abe92011-04-12 15:39:35 +1000626 error("It is required that your private key files are NOT accessible by others.");
Ben Lindstromd0fca422001-03-26 13:44:06 +0000627 error("This private key will be ignored.");
Damien Millereba71ba2000-04-29 23:57:08 +1000628 return 0;
629 }
Ben Lindstromd0fca422001-03-26 13:44:06 +0000630 return 1;
631}
Ben Lindstromb257cca2001-03-05 04:59:27 +0000632
Damien Millera2327922010-12-01 12:01:21 +1100633static Key *
634key_parse_private_type(Buffer *blob, int type, const char *passphrase,
635 char **commentp)
636{
637 switch (type) {
638 case KEY_RSA1:
639 return key_parse_private_rsa1(blob, passphrase, commentp);
640 case KEY_DSA:
641 case KEY_ECDSA:
642 case KEY_RSA:
643 case KEY_UNSPEC:
644 return key_parse_private_pem(blob, type, passphrase, commentp);
645 default:
646 break;
647 }
648 return NULL;
649}
650
Ben Lindstromd0fca422001-03-26 13:44:06 +0000651Key *
652key_load_private_type(int type, const char *filename, const char *passphrase,
Darren Tucker232b76f2006-05-06 17:41:51 +1000653 char **commentp, int *perm_ok)
Ben Lindstromd0fca422001-03-26 13:44:06 +0000654{
655 int fd;
Damien Millera2327922010-12-01 12:01:21 +1100656 Key *ret;
657 Buffer buffer;
Ben Lindstromd0fca422001-03-26 13:44:06 +0000658
659 fd = open(filename, O_RDONLY);
Darren Tuckerd4c86b12010-01-12 19:41:22 +1100660 if (fd < 0) {
661 debug("could not open key file '%s': %s", filename,
662 strerror(errno));
663 if (perm_ok != NULL)
664 *perm_ok = 0;
Ben Lindstromd0fca422001-03-26 13:44:06 +0000665 return NULL;
Darren Tucker69c01b12010-01-12 19:42:29 +1100666 }
Ben Lindstromd0fca422001-03-26 13:44:06 +0000667 if (!key_perm_ok(fd, filename)) {
Darren Tucker232b76f2006-05-06 17:41:51 +1000668 if (perm_ok != NULL)
669 *perm_ok = 0;
Ben Lindstrom15f33862001-04-16 02:00:02 +0000670 error("bad permissions: ignore key: %s", filename);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000671 close(fd);
672 return NULL;
673 }
Darren Tucker232b76f2006-05-06 17:41:51 +1000674 if (perm_ok != NULL)
675 *perm_ok = 1;
Damien Millera2327922010-12-01 12:01:21 +1100676
677 buffer_init(&buffer);
678 if (!key_load_file(fd, filename, &buffer)) {
679 buffer_free(&buffer);
Ben Lindstromb257cca2001-03-05 04:59:27 +0000680 close(fd);
Damien Millera2327922010-12-01 12:01:21 +1100681 return NULL;
Damien Millereba71ba2000-04-29 23:57:08 +1000682 }
Damien Millera2327922010-12-01 12:01:21 +1100683 close(fd);
684 ret = key_parse_private_type(&buffer, type, passphrase, commentp);
685 buffer_free(&buffer);
686 return ret;
Ben Lindstromd0fca422001-03-26 13:44:06 +0000687}
688
689Key *
Damien Miller2ce12ef2011-05-05 14:17:18 +1000690key_parse_private(Buffer *buffer, const char *filename,
691 const char *passphrase, char **commentp)
692{
693 Key *pub, *prv;
694 Buffer pubcopy;
695
696 buffer_init(&pubcopy);
697 buffer_append(&pubcopy, buffer_ptr(buffer), buffer_len(buffer));
698 /* it's a SSH v1 key if the public key part is readable */
699 pub = key_parse_public_rsa1(&pubcopy, commentp);
700 buffer_free(&pubcopy);
701 if (pub == NULL) {
702 prv = key_parse_private_type(buffer, KEY_UNSPEC,
703 passphrase, NULL);
704 /* use the filename as a comment for PEM */
705 if (commentp && prv)
706 *commentp = xstrdup(filename);
707 } else {
708 key_free(pub);
709 /* key_parse_public_rsa1() has already loaded the comment */
710 prv = key_parse_private_type(buffer, KEY_RSA1, passphrase,
711 NULL);
712 }
713 return prv;
714}
715
716Key *
Ben Lindstromd0fca422001-03-26 13:44:06 +0000717key_load_private(const char *filename, const char *passphrase,
718 char **commentp)
719{
Damien Miller2ce12ef2011-05-05 14:17:18 +1000720 Key *prv;
721 Buffer buffer;
Ben Lindstromd0fca422001-03-26 13:44:06 +0000722 int fd;
723
724 fd = open(filename, O_RDONLY);
Darren Tuckerd4c86b12010-01-12 19:41:22 +1100725 if (fd < 0) {
726 debug("could not open key file '%s': %s", filename,
727 strerror(errno));
Ben Lindstromd0fca422001-03-26 13:44:06 +0000728 return NULL;
Darren Tuckerd4c86b12010-01-12 19:41:22 +1100729 }
Ben Lindstromd0fca422001-03-26 13:44:06 +0000730 if (!key_perm_ok(fd, filename)) {
Ben Lindstrom15f33862001-04-16 02:00:02 +0000731 error("bad permissions: ignore key: %s", filename);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000732 close(fd);
733 return NULL;
734 }
Damien Millera2327922010-12-01 12:01:21 +1100735
736 buffer_init(&buffer);
737 if (!key_load_file(fd, filename, &buffer)) {
738 buffer_free(&buffer);
739 close(fd);
740 return NULL;
741 }
742 close(fd);
743
Damien Miller2ce12ef2011-05-05 14:17:18 +1000744 prv = key_parse_private(&buffer, filename, passphrase, commentp);
Damien Millera2327922010-12-01 12:01:21 +1100745 buffer_free(&buffer);
Ben Lindstrom322915d2001-06-05 20:46:32 +0000746 return prv;
Damien Millereba71ba2000-04-29 23:57:08 +1000747}
Damien Millere4340be2000-09-16 13:29:08 +1100748
Ben Lindstrombba81212001-06-25 05:01:22 +0000749static int
Ben Lindstromd0fca422001-03-26 13:44:06 +0000750key_try_load_public(Key *k, const char *filename, char **commentp)
Damien Millere4340be2000-09-16 13:29:08 +1100751{
752 FILE *f;
Darren Tucker22cc7412004-12-06 22:47:41 +1100753 char line[SSH_MAX_PUBKEY_BYTES];
Damien Millere4340be2000-09-16 13:29:08 +1100754 char *cp;
Darren Tuckerf0f90982004-12-11 13:39:50 +1100755 u_long linenum = 0;
Damien Millere4340be2000-09-16 13:29:08 +1100756
757 f = fopen(filename, "r");
758 if (f != NULL) {
Darren Tucker22cc7412004-12-06 22:47:41 +1100759 while (read_keyfile_line(f, filename, line, sizeof(line),
760 &linenum) != -1) {
Damien Millere4340be2000-09-16 13:29:08 +1100761 cp = line;
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000762 switch (*cp) {
Damien Millere4340be2000-09-16 13:29:08 +1100763 case '#':
764 case '\n':
765 case '\0':
766 continue;
767 }
768 /* Skip leading whitespace. */
769 for (; *cp && (*cp == ' ' || *cp == '\t'); cp++)
770 ;
771 if (*cp) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100772 if (key_read(k, &cp) == 1) {
Damien Millere4340be2000-09-16 13:29:08 +1100773 if (commentp)
774 *commentp=xstrdup(filename);
775 fclose(f);
776 return 1;
777 }
778 }
779 }
780 fclose(f);
781 }
782 return 0;
783}
784
Ben Lindstromd0fca422001-03-26 13:44:06 +0000785/* load public key from ssh v1 private or any pubkey file */
786Key *
787key_load_public(const char *filename, char **commentp)
Damien Millere4340be2000-09-16 13:29:08 +1100788{
Ben Lindstromd0fca422001-03-26 13:44:06 +0000789 Key *pub;
790 char file[MAXPATHLEN];
Damien Millere4340be2000-09-16 13:29:08 +1100791
Damien Millerdb274722003-05-14 13:45:22 +1000792 /* try rsa1 private key */
Ben Lindstromd0fca422001-03-26 13:44:06 +0000793 pub = key_load_public_type(KEY_RSA1, filename, commentp);
794 if (pub != NULL)
795 return pub;
Damien Millerdb274722003-05-14 13:45:22 +1000796
797 /* try rsa1 public key */
798 pub = key_new(KEY_RSA1);
799 if (key_try_load_public(pub, filename, commentp) == 1)
800 return pub;
801 key_free(pub);
802
803 /* try ssh2 public key */
Ben Lindstromd0fca422001-03-26 13:44:06 +0000804 pub = key_new(KEY_UNSPEC);
805 if (key_try_load_public(pub, filename, commentp) == 1)
806 return pub;
807 if ((strlcpy(file, filename, sizeof file) < sizeof(file)) &&
808 (strlcat(file, ".pub", sizeof file) < sizeof(file)) &&
809 (key_try_load_public(pub, file, commentp) == 1))
810 return pub;
811 key_free(pub);
812 return NULL;
Damien Millere4340be2000-09-16 13:29:08 +1100813}
Damien Miller1aed65e2010-03-04 21:53:35 +1100814
Damien Millerc1583312010-08-05 13:04:50 +1000815/* Load the certificate associated with the named private key */
816Key *
817key_load_cert(const char *filename)
818{
819 Key *pub;
Damien Miller5458c4d2010-08-05 13:05:15 +1000820 char *file;
Damien Millerc1583312010-08-05 13:04:50 +1000821
822 pub = key_new(KEY_UNSPEC);
Damien Miller5458c4d2010-08-05 13:05:15 +1000823 xasprintf(&file, "%s-cert.pub", filename);
824 if (key_try_load_public(pub, file, NULL) == 1) {
825 xfree(file);
Damien Millerc1583312010-08-05 13:04:50 +1000826 return pub;
Damien Miller5458c4d2010-08-05 13:05:15 +1000827 }
828 xfree(file);
Damien Millerc1583312010-08-05 13:04:50 +1000829 key_free(pub);
830 return NULL;
831}
832
833/* Load private key and certificate */
834Key *
835key_load_private_cert(int type, const char *filename, const char *passphrase,
836 int *perm_ok)
837{
838 Key *key, *pub;
839
840 switch (type) {
841 case KEY_RSA:
842 case KEY_DSA:
Damien Millereb8b60e2010-08-31 22:41:14 +1000843 case KEY_ECDSA:
Damien Millerc1583312010-08-05 13:04:50 +1000844 break;
845 default:
846 error("%s: unsupported key type", __func__);
847 return NULL;
848 }
849
850 if ((key = key_load_private_type(type, filename,
851 passphrase, NULL, perm_ok)) == NULL)
852 return NULL;
853
854 if ((pub = key_load_cert(filename)) == NULL) {
855 key_free(key);
856 return NULL;
857 }
858
859 /* Make sure the private key matches the certificate */
860 if (key_equal_public(key, pub) == 0) {
861 error("%s: certificate does not match private key %s",
862 __func__, filename);
863 } else if (key_to_certified(key, key_cert_is_legacy(pub)) != 0) {
864 error("%s: key_to_certified failed", __func__);
865 } else {
866 key_cert_copy(pub, key);
867 key_free(pub);
868 return key;
869 }
870
871 key_free(key);
872 key_free(pub);
873 return NULL;
874}
875
Damien Miller1aed65e2010-03-04 21:53:35 +1100876/*
877 * Returns 1 if the specified "key" is listed in the file "filename",
878 * 0 if the key is not listed or -1 on error.
879 * If strict_type is set then the key type must match exactly,
880 * otherwise a comparison that ignores certficiate data is performed.
881 */
882int
883key_in_file(Key *key, const char *filename, int strict_type)
884{
885 FILE *f;
886 char line[SSH_MAX_PUBKEY_BYTES];
887 char *cp;
888 u_long linenum = 0;
889 int ret = 0;
890 Key *pub;
891 int (*key_compare)(const Key *, const Key *) = strict_type ?
892 key_equal : key_equal_public;
893
894 if ((f = fopen(filename, "r")) == NULL) {
895 if (errno == ENOENT) {
896 debug("%s: keyfile \"%s\" missing", __func__, filename);
897 return 0;
898 } else {
899 error("%s: could not open keyfile \"%s\": %s", __func__,
900 filename, strerror(errno));
901 return -1;
902 }
903 }
904
905 while (read_keyfile_line(f, filename, line, sizeof(line),
906 &linenum) != -1) {
907 cp = line;
908
909 /* Skip leading whitespace. */
910 for (; *cp && (*cp == ' ' || *cp == '\t'); cp++)
911 ;
912
913 /* Skip comments and empty lines */
914 switch (*cp) {
915 case '#':
916 case '\n':
917 case '\0':
918 continue;
919 }
920
921 pub = key_new(KEY_UNSPEC);
922 if (key_read(pub, &cp) != 1) {
923 key_free(pub);
924 continue;
925 }
926 if (key_compare(key, pub)) {
927 ret = 1;
928 key_free(pub);
929 break;
930 }
931 key_free(pub);
932 }
933 fclose(f);
934 return ret;
935}
936