blob: b1e3eda5c4f72fdce8eb2ced4fe2771c8589aabc [file] [log] [blame]
Damien Millerbf0423e2010-09-10 11:20:38 +10001/* $OpenBSD: authfile.c,v 1.84 2010/09/08 03:54:36 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
Ben Lindstromd0fca422001-03-26 13:44:06 +000072/* Version identification string for SSH v1 identity files. */
Ben Lindstrom1170d712001-01-29 07:51:26 +000073static const char authfile_id_string[] =
74 "SSH PRIVATE KEY FILE FORMAT 1.1\n";
Damien Millerd4a8b7e1999-10-27 13:42:43 +100075
Damien Miller5428f641999-11-25 11:54:57 +110076/*
77 * Saves the authentication (private) key in a file, encrypting it with
78 * passphrase. The identification of the file (lowest 64 bits of n) will
79 * precede the key to provide identification of the key without needing a
80 * passphrase.
81 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100082
Ben Lindstrombba81212001-06-25 05:01:22 +000083static int
Ben Lindstromd0fca422001-03-26 13:44:06 +000084key_save_private_rsa1(Key *key, const char *filename, const char *passphrase,
85 const char *comment)
Damien Millerd4a8b7e1999-10-27 13:42:43 +100086{
Damien Miller95def091999-11-25 00:26:21 +110087 Buffer buffer, encrypted;
Damien Miller708d21c2002-01-22 23:18:15 +110088 u_char buf[100], *cp;
Damien Miller963f6b22002-02-19 15:21:23 +110089 int fd, i, cipher_num;
Damien Miller874d77b2000-10-14 16:23:11 +110090 CipherContext ciphercontext;
91 Cipher *cipher;
Darren Tucker3f9fdc72004-06-22 12:56:01 +100092 u_int32_t rnd;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100093
Damien Miller5428f641999-11-25 11:54:57 +110094 /*
95 * If the passphrase is empty, use SSH_CIPHER_NONE to ease converting
96 * to another cipher; otherwise use SSH_AUTHFILE_CIPHER.
97 */
Damien Miller963f6b22002-02-19 15:21:23 +110098 cipher_num = (strcmp(passphrase, "") == 0) ?
99 SSH_CIPHER_NONE : SSH_AUTHFILE_CIPHER;
100 if ((cipher = cipher_by_number(cipher_num)) == NULL)
Damien Miller874d77b2000-10-14 16:23:11 +1100101 fatal("save_private_key_rsa: bad cipher");
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000102
Damien Miller95def091999-11-25 00:26:21 +1100103 /* This buffer is used to built the secret part of the private key. */
104 buffer_init(&buffer);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000105
Damien Miller95def091999-11-25 00:26:21 +1100106 /* Put checkbytes for checking passphrase validity. */
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000107 rnd = arc4random();
108 buf[0] = rnd & 0xff;
109 buf[1] = (rnd >> 8) & 0xff;
Damien Miller95def091999-11-25 00:26:21 +1100110 buf[2] = buf[0];
111 buf[3] = buf[1];
112 buffer_append(&buffer, buf, 4);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000113
Damien Miller5428f641999-11-25 11:54:57 +1100114 /*
115 * Store the private key (n and e will not be stored because they
116 * will be stored in plain text, and storing them also in encrypted
117 * format would just give known plaintext).
118 */
Ben Lindstromd0fca422001-03-26 13:44:06 +0000119 buffer_put_bignum(&buffer, key->rsa->d);
120 buffer_put_bignum(&buffer, key->rsa->iqmp);
121 buffer_put_bignum(&buffer, key->rsa->q); /* reverse from SSL p */
122 buffer_put_bignum(&buffer, key->rsa->p); /* reverse from SSL q */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000123
Damien Miller95def091999-11-25 00:26:21 +1100124 /* Pad the part to be encrypted until its size is a multiple of 8. */
125 while (buffer_len(&buffer) % 8 != 0)
126 buffer_put_char(&buffer, 0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000127
Damien Miller95def091999-11-25 00:26:21 +1100128 /* This buffer will be used to contain the data in the file. */
129 buffer_init(&encrypted);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000130
Damien Miller95def091999-11-25 00:26:21 +1100131 /* First store keyfile id string. */
Ben Lindstrom1170d712001-01-29 07:51:26 +0000132 for (i = 0; authfile_id_string[i]; i++)
133 buffer_put_char(&encrypted, authfile_id_string[i]);
Damien Miller95def091999-11-25 00:26:21 +1100134 buffer_put_char(&encrypted, 0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000135
Damien Miller95def091999-11-25 00:26:21 +1100136 /* Store cipher type. */
Damien Miller963f6b22002-02-19 15:21:23 +1100137 buffer_put_char(&encrypted, cipher_num);
Damien Miller95def091999-11-25 00:26:21 +1100138 buffer_put_int(&encrypted, 0); /* For future extension */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000139
Damien Miller95def091999-11-25 00:26:21 +1100140 /* Store public key. This will be in plain text. */
Ben Lindstromd0fca422001-03-26 13:44:06 +0000141 buffer_put_int(&encrypted, BN_num_bits(key->rsa->n));
142 buffer_put_bignum(&encrypted, key->rsa->n);
143 buffer_put_bignum(&encrypted, key->rsa->e);
Ben Lindstrom664408d2001-06-09 01:42:01 +0000144 buffer_put_cstring(&encrypted, comment);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000145
Damien Miller95def091999-11-25 00:26:21 +1100146 /* Allocate space for the private part of the key in the buffer. */
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100147 cp = buffer_append_space(&encrypted, buffer_len(&buffer));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000148
Damien Miller963f6b22002-02-19 15:21:23 +1100149 cipher_set_key_string(&ciphercontext, cipher, passphrase,
150 CIPHER_ENCRYPT);
151 cipher_crypt(&ciphercontext, cp,
Damien Miller708d21c2002-01-22 23:18:15 +1100152 buffer_ptr(&buffer), buffer_len(&buffer));
Damien Miller963f6b22002-02-19 15:21:23 +1100153 cipher_cleanup(&ciphercontext);
Damien Miller874d77b2000-10-14 16:23:11 +1100154 memset(&ciphercontext, 0, sizeof(ciphercontext));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000155
Damien Miller95def091999-11-25 00:26:21 +1100156 /* Destroy temporary data. */
157 memset(buf, 0, sizeof(buf));
158 buffer_free(&buffer);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000159
Damien Miller037a0dc1999-12-07 15:38:31 +1100160 fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0600);
Ben Lindstrom15f33862001-04-16 02:00:02 +0000161 if (fd < 0) {
162 error("open %s failed: %s.", filename, strerror(errno));
Darren Tuckerd1d41b32003-09-22 21:01:27 +1000163 buffer_free(&encrypted);
Damien Miller95def091999-11-25 00:26:21 +1100164 return 0;
Ben Lindstrom15f33862001-04-16 02:00:02 +0000165 }
Damien Millereccb9de2005-06-17 12:59:34 +1000166 if (atomicio(vwrite, fd, buffer_ptr(&encrypted),
167 buffer_len(&encrypted)) != buffer_len(&encrypted)) {
Ben Lindstrom15f33862001-04-16 02:00:02 +0000168 error("write to key file %s failed: %s", filename,
Damien Miller9f0f5c62001-12-21 14:45:46 +1100169 strerror(errno));
Damien Miller95def091999-11-25 00:26:21 +1100170 buffer_free(&encrypted);
Damien Miller037a0dc1999-12-07 15:38:31 +1100171 close(fd);
Damien Miller78315eb2000-09-29 23:01:36 +1100172 unlink(filename);
Damien Miller95def091999-11-25 00:26:21 +1100173 return 0;
174 }
Damien Miller037a0dc1999-12-07 15:38:31 +1100175 close(fd);
Damien Miller95def091999-11-25 00:26:21 +1100176 buffer_free(&encrypted);
177 return 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000178}
179
Ben Lindstromd0fca422001-03-26 13:44:06 +0000180/* save SSH v2 key in OpenSSL PEM format */
Ben Lindstrombba81212001-06-25 05:01:22 +0000181static int
Ben Lindstromd0fca422001-03-26 13:44:06 +0000182key_save_private_pem(Key *key, const char *filename, const char *_passphrase,
183 const char *comment)
Damien Millereba71ba2000-04-29 23:57:08 +1000184{
185 FILE *fp;
186 int fd;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100187 int success = 0;
188 int len = strlen(_passphrase);
Ben Lindstrom90fd8142002-02-26 18:09:42 +0000189 u_char *passphrase = (len > 0) ? (u_char *)_passphrase : NULL;
Darren Tuckerdf6578b2009-11-07 16:03:14 +1100190#if (OPENSSL_VERSION_NUMBER < 0x00907000L)
191 const EVP_CIPHER *cipher = (len > 0) ? EVP_des_ede3_cbc() : NULL;
192#else
Darren Tuckerdfb9b712009-10-24 11:46:43 +1100193 const EVP_CIPHER *cipher = (len > 0) ? EVP_aes_128_cbc() : NULL;
Darren Tuckerdf6578b2009-11-07 16:03:14 +1100194#endif
Damien Millereba71ba2000-04-29 23:57:08 +1000195
196 if (len > 0 && len <= 4) {
Ben Lindstrom15f33862001-04-16 02:00:02 +0000197 error("passphrase too short: have %d bytes, need > 4", len);
Damien Millereba71ba2000-04-29 23:57:08 +1000198 return 0;
199 }
200 fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0600);
201 if (fd < 0) {
Ben Lindstrom15f33862001-04-16 02:00:02 +0000202 error("open %s failed: %s.", filename, strerror(errno));
Damien Millereba71ba2000-04-29 23:57:08 +1000203 return 0;
204 }
205 fp = fdopen(fd, "w");
Damien Miller4dec5d72006-08-05 11:38:40 +1000206 if (fp == NULL) {
Ben Lindstrom15f33862001-04-16 02:00:02 +0000207 error("fdopen %s failed: %s.", filename, strerror(errno));
Damien Millereba71ba2000-04-29 23:57:08 +1000208 close(fd);
209 return 0;
210 }
Damien Miller0bc1bd82000-11-13 22:57:25 +1100211 switch (key->type) {
Ben Lindstromc1116602001-03-29 00:28:37 +0000212 case KEY_DSA:
213 success = PEM_write_DSAPrivateKey(fp, key->dsa,
214 cipher, passphrase, len, NULL, NULL);
215 break;
Damien Miller6af914a2010-09-10 11:39:26 +1000216#ifdef OPENSSL_HAS_ECC
Damien Millereb8b60e2010-08-31 22:41:14 +1000217 case KEY_ECDSA:
218 success = PEM_write_ECPrivateKey(fp, key->ecdsa,
219 cipher, passphrase, len, NULL, NULL);
220 break;
Damien Miller6af914a2010-09-10 11:39:26 +1000221#endif
Ben Lindstromc1116602001-03-29 00:28:37 +0000222 case KEY_RSA:
223 success = PEM_write_RSAPrivateKey(fp, key->rsa,
224 cipher, passphrase, len, NULL, NULL);
225 break;
Damien Millereba71ba2000-04-29 23:57:08 +1000226 }
227 fclose(fp);
228 return success;
229}
230
231int
Ben Lindstromd0fca422001-03-26 13:44:06 +0000232key_save_private(Key *key, const char *filename, const char *passphrase,
Damien Millereba71ba2000-04-29 23:57:08 +1000233 const char *comment)
234{
235 switch (key->type) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100236 case KEY_RSA1:
Ben Lindstromd0fca422001-03-26 13:44:06 +0000237 return key_save_private_rsa1(key, filename, passphrase,
238 comment);
Damien Millereba71ba2000-04-29 23:57:08 +1000239 case KEY_DSA:
Damien Millereb8b60e2010-08-31 22:41:14 +1000240 case KEY_ECDSA:
Damien Miller0bc1bd82000-11-13 22:57:25 +1100241 case KEY_RSA:
Ben Lindstromd0fca422001-03-26 13:44:06 +0000242 return key_save_private_pem(key, filename, passphrase,
243 comment);
Damien Millereba71ba2000-04-29 23:57:08 +1000244 default:
245 break;
246 }
Ben Lindstrom15f33862001-04-16 02:00:02 +0000247 error("key_save_private: cannot save key type %d", key->type);
Damien Millereba71ba2000-04-29 23:57:08 +1000248 return 0;
249}
250
Damien Miller5428f641999-11-25 11:54:57 +1100251/*
Ben Lindstromd0fca422001-03-26 13:44:06 +0000252 * Loads the public part of the ssh v1 key file. Returns NULL if an error was
253 * encountered (the file does not exist or is not readable), and the key
Damien Miller5428f641999-11-25 11:54:57 +1100254 * otherwise.
255 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000256
Ben Lindstrombba81212001-06-25 05:01:22 +0000257static Key *
Ben Lindstromd0fca422001-03-26 13:44:06 +0000258key_load_public_rsa1(int fd, const char *filename, char **commentp)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000259{
Damien Miller95def091999-11-25 00:26:21 +1100260 Buffer buffer;
Ben Lindstromd0fca422001-03-26 13:44:06 +0000261 Key *pub;
Ben Lindstrom44adb8f2002-12-23 02:00:23 +0000262 struct stat st;
Damien Miller95def091999-11-25 00:26:21 +1100263 char *cp;
Damien Millereccb9de2005-06-17 12:59:34 +1000264 u_int i;
Darren Tucker1f8311c2004-05-13 16:39:33 +1000265 size_t len;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000266
Ben Lindstrom44adb8f2002-12-23 02:00:23 +0000267 if (fstat(fd, &st) < 0) {
268 error("fstat for key file %.200s failed: %.100s",
269 filename, strerror(errno));
270 return NULL;
271 }
Darren Tuckerf4b43712004-08-29 16:28:39 +1000272 if (st.st_size > 1*1024*1024) {
273 error("key file %.200s too large", filename);
274 return NULL;
275 }
Darren Tucker1f8311c2004-05-13 16:39:33 +1000276 len = (size_t)st.st_size; /* truncated */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000277
Damien Miller95def091999-11-25 00:26:21 +1100278 buffer_init(&buffer);
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100279 cp = buffer_append_space(&buffer, len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000280
Damien Millereccb9de2005-06-17 12:59:34 +1000281 if (atomicio(read, fd, cp, len) != len) {
Damien Miller95def091999-11-25 00:26:21 +1100282 debug("Read from key file %.200s failed: %.100s", filename,
Damien Millereba71ba2000-04-29 23:57:08 +1000283 strerror(errno));
Damien Miller95def091999-11-25 00:26:21 +1100284 buffer_free(&buffer);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000285 return NULL;
Damien Miller95def091999-11-25 00:26:21 +1100286 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000287
Ben Lindstrom1170d712001-01-29 07:51:26 +0000288 /* Check that it is at least big enough to contain the ID string. */
289 if (len < sizeof(authfile_id_string)) {
Damien Miller058655c2001-10-10 15:03:36 +1000290 debug3("Not a RSA1 key file %.200s.", filename);
Damien Miller95def091999-11-25 00:26:21 +1100291 buffer_free(&buffer);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000292 return NULL;
Damien Miller95def091999-11-25 00:26:21 +1100293 }
Damien Miller5428f641999-11-25 11:54:57 +1100294 /*
295 * Make sure it begins with the id string. Consume the id string
296 * from the buffer.
297 */
Ben Lindstrom1170d712001-01-29 07:51:26 +0000298 for (i = 0; i < sizeof(authfile_id_string); i++)
299 if (buffer_get_char(&buffer) != authfile_id_string[i]) {
Damien Miller058655c2001-10-10 15:03:36 +1000300 debug3("Not a RSA1 key file %.200s.", filename);
Damien Miller95def091999-11-25 00:26:21 +1100301 buffer_free(&buffer);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000302 return NULL;
Damien Miller95def091999-11-25 00:26:21 +1100303 }
304 /* Skip cipher type and reserved data. */
305 (void) buffer_get_char(&buffer); /* cipher type */
306 (void) buffer_get_int(&buffer); /* reserved */
307
308 /* Read the public key from the buffer. */
Ben Lindstromc5a7f4f2002-06-25 23:19:13 +0000309 (void) buffer_get_int(&buffer);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000310 pub = key_new(KEY_RSA1);
311 buffer_get_bignum(&buffer, pub->rsa->n);
312 buffer_get_bignum(&buffer, pub->rsa->e);
313 if (commentp)
314 *commentp = buffer_get_string(&buffer, NULL);
Damien Miller95def091999-11-25 00:26:21 +1100315 /* The encrypted private part is not parsed by this function. */
316
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000317 buffer_free(&buffer);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000318 return pub;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000319}
320
Ben Lindstromd0fca422001-03-26 13:44:06 +0000321/* load public key from private-key file, works only for SSH v1 */
322Key *
323key_load_public_type(int type, const char *filename, char **commentp)
Damien Millereba71ba2000-04-29 23:57:08 +1000324{
Ben Lindstromd0fca422001-03-26 13:44:06 +0000325 Key *pub;
326 int fd;
327
328 if (type == KEY_RSA1) {
329 fd = open(filename, O_RDONLY);
330 if (fd < 0)
331 return NULL;
332 pub = key_load_public_rsa1(fd, filename, commentp);
333 close(fd);
334 return pub;
Damien Millereba71ba2000-04-29 23:57:08 +1000335 }
Ben Lindstromd0fca422001-03-26 13:44:06 +0000336 return NULL;
Damien Millereba71ba2000-04-29 23:57:08 +1000337}
338
Damien Miller5428f641999-11-25 11:54:57 +1100339/*
340 * Loads the private key from the file. Returns 0 if an error is encountered
341 * (file does not exist or is not readable, or passphrase is bad). This
342 * initializes the private key.
343 * Assumes we are called under uid of the owner of the file.
344 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000345
Ben Lindstrombba81212001-06-25 05:01:22 +0000346static Key *
Ben Lindstromd0fca422001-03-26 13:44:06 +0000347key_load_private_rsa1(int fd, const char *filename, const char *passphrase,
348 char **commentp)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000349{
Damien Millereccb9de2005-06-17 12:59:34 +1000350 u_int i;
351 int check1, check2, cipher_type;
Darren Tucker1f8311c2004-05-13 16:39:33 +1000352 size_t len;
Damien Miller95def091999-11-25 00:26:21 +1100353 Buffer buffer, decrypted;
Damien Miller708d21c2002-01-22 23:18:15 +1100354 u_char *cp;
Damien Miller874d77b2000-10-14 16:23:11 +1100355 CipherContext ciphercontext;
356 Cipher *cipher;
Ben Lindstromd0fca422001-03-26 13:44:06 +0000357 Key *prv = NULL;
Ben Lindstrom44adb8f2002-12-23 02:00:23 +0000358 struct stat st;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000359
Ben Lindstrom44adb8f2002-12-23 02:00:23 +0000360 if (fstat(fd, &st) < 0) {
361 error("fstat for key file %.200s failed: %.100s",
362 filename, strerror(errno));
363 close(fd);
364 return NULL;
365 }
Darren Tucker1f8311c2004-05-13 16:39:33 +1000366 if (st.st_size > 1*1024*1024) {
Darren Tuckerf4b43712004-08-29 16:28:39 +1000367 error("key file %.200s too large", filename);
Darren Tucker1f8311c2004-05-13 16:39:33 +1000368 close(fd);
369 return (NULL);
370 }
371 len = (size_t)st.st_size; /* truncated */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000372
Damien Miller95def091999-11-25 00:26:21 +1100373 buffer_init(&buffer);
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100374 cp = buffer_append_space(&buffer, len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000375
Damien Millereccb9de2005-06-17 12:59:34 +1000376 if (atomicio(read, fd, cp, len) != len) {
Damien Miller95def091999-11-25 00:26:21 +1100377 debug("Read from key file %.200s failed: %.100s", filename,
Damien Miller0bc1bd82000-11-13 22:57:25 +1100378 strerror(errno));
Damien Miller95def091999-11-25 00:26:21 +1100379 buffer_free(&buffer);
Damien Miller037a0dc1999-12-07 15:38:31 +1100380 close(fd);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000381 return NULL;
Damien Miller95def091999-11-25 00:26:21 +1100382 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000383
Ben Lindstrom1170d712001-01-29 07:51:26 +0000384 /* Check that it is at least big enough to contain the ID string. */
385 if (len < sizeof(authfile_id_string)) {
Damien Miller058655c2001-10-10 15:03:36 +1000386 debug3("Not a RSA1 key file %.200s.", filename);
Damien Miller95def091999-11-25 00:26:21 +1100387 buffer_free(&buffer);
Ben Lindstromb257cca2001-03-05 04:59:27 +0000388 close(fd);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000389 return NULL;
Damien Miller95def091999-11-25 00:26:21 +1100390 }
Damien Miller5428f641999-11-25 11:54:57 +1100391 /*
392 * Make sure it begins with the id string. Consume the id string
393 * from the buffer.
394 */
Ben Lindstrom1170d712001-01-29 07:51:26 +0000395 for (i = 0; i < sizeof(authfile_id_string); i++)
396 if (buffer_get_char(&buffer) != authfile_id_string[i]) {
Damien Miller058655c2001-10-10 15:03:36 +1000397 debug3("Not a RSA1 key file %.200s.", filename);
Damien Miller95def091999-11-25 00:26:21 +1100398 buffer_free(&buffer);
Ben Lindstromb257cca2001-03-05 04:59:27 +0000399 close(fd);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000400 return NULL;
Damien Miller95def091999-11-25 00:26:21 +1100401 }
Ben Lindstromb257cca2001-03-05 04:59:27 +0000402
Damien Miller95def091999-11-25 00:26:21 +1100403 /* Read cipher type. */
404 cipher_type = buffer_get_char(&buffer);
405 (void) buffer_get_int(&buffer); /* Reserved data. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000406
Damien Miller95def091999-11-25 00:26:21 +1100407 /* Read the public key from the buffer. */
Ben Lindstromc5a7f4f2002-06-25 23:19:13 +0000408 (void) buffer_get_int(&buffer);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000409 prv = key_new_private(KEY_RSA1);
410
411 buffer_get_bignum(&buffer, prv->rsa->n);
412 buffer_get_bignum(&buffer, prv->rsa->e);
413 if (commentp)
414 *commentp = buffer_get_string(&buffer, NULL);
Damien Miller95def091999-11-25 00:26:21 +1100415 else
416 xfree(buffer_get_string(&buffer, NULL));
417
418 /* Check that it is a supported cipher. */
Damien Miller874d77b2000-10-14 16:23:11 +1100419 cipher = cipher_by_number(cipher_type);
420 if (cipher == NULL) {
421 debug("Unsupported cipher %d used in key file %.200s.",
422 cipher_type, filename);
Damien Miller95def091999-11-25 00:26:21 +1100423 buffer_free(&buffer);
424 goto fail;
425 }
426 /* Initialize space for decrypted data. */
427 buffer_init(&decrypted);
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100428 cp = buffer_append_space(&decrypted, buffer_len(&buffer));
Damien Miller95def091999-11-25 00:26:21 +1100429
430 /* Rest of the buffer is encrypted. Decrypt it using the passphrase. */
Damien Miller963f6b22002-02-19 15:21:23 +1100431 cipher_set_key_string(&ciphercontext, cipher, passphrase,
432 CIPHER_DECRYPT);
433 cipher_crypt(&ciphercontext, cp,
Damien Miller708d21c2002-01-22 23:18:15 +1100434 buffer_ptr(&buffer), buffer_len(&buffer));
Damien Miller963f6b22002-02-19 15:21:23 +1100435 cipher_cleanup(&ciphercontext);
Damien Miller874d77b2000-10-14 16:23:11 +1100436 memset(&ciphercontext, 0, sizeof(ciphercontext));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000437 buffer_free(&buffer);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000438
Damien Miller95def091999-11-25 00:26:21 +1100439 check1 = buffer_get_char(&decrypted);
440 check2 = buffer_get_char(&decrypted);
441 if (check1 != buffer_get_char(&decrypted) ||
442 check2 != buffer_get_char(&decrypted)) {
443 if (strcmp(passphrase, "") != 0)
Ben Lindstromd0fca422001-03-26 13:44:06 +0000444 debug("Bad passphrase supplied for key file %.200s.",
445 filename);
Damien Miller95def091999-11-25 00:26:21 +1100446 /* Bad passphrase. */
447 buffer_free(&decrypted);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000448 goto fail;
Damien Miller95def091999-11-25 00:26:21 +1100449 }
450 /* Read the rest of the private key. */
Ben Lindstromd0fca422001-03-26 13:44:06 +0000451 buffer_get_bignum(&decrypted, prv->rsa->d);
452 buffer_get_bignum(&decrypted, prv->rsa->iqmp); /* u */
453 /* in SSL and SSH v1 p and q are exchanged */
454 buffer_get_bignum(&decrypted, prv->rsa->q); /* p */
455 buffer_get_bignum(&decrypted, prv->rsa->p); /* q */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000456
Ben Lindstromd0fca422001-03-26 13:44:06 +0000457 /* calculate p-1 and q-1 */
Damien Millerda755162002-01-22 23:09:22 +1100458 rsa_generate_additional_parameters(prv->rsa);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000459
Damien Miller95def091999-11-25 00:26:21 +1100460 buffer_free(&decrypted);
Damien Millered33d3b2003-03-15 11:36:18 +1100461
462 /* enable blinding */
463 if (RSA_blinding_on(prv->rsa, NULL) != 1) {
464 error("key_load_private_rsa1: RSA_blinding_on failed");
465 goto fail;
466 }
Ben Lindstromb257cca2001-03-05 04:59:27 +0000467 close(fd);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000468 return prv;
469
470fail:
471 if (commentp)
472 xfree(*commentp);
473 close(fd);
474 key_free(prv);
475 return NULL;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000476}
Damien Millereba71ba2000-04-29 23:57:08 +1000477
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000478Key *
Ben Lindstromd0fca422001-03-26 13:44:06 +0000479key_load_private_pem(int fd, int type, const char *passphrase,
480 char **commentp)
Damien Millereba71ba2000-04-29 23:57:08 +1000481{
Damien Millereba71ba2000-04-29 23:57:08 +1000482 FILE *fp;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100483 EVP_PKEY *pk = NULL;
Ben Lindstromd0fca422001-03-26 13:44:06 +0000484 Key *prv = NULL;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100485 char *name = "<no key>";
Damien Millereba71ba2000-04-29 23:57:08 +1000486
Damien Millereba71ba2000-04-29 23:57:08 +1000487 fp = fdopen(fd, "r");
488 if (fp == NULL) {
Ben Lindstrom15f33862001-04-16 02:00:02 +0000489 error("fdopen failed: %s", strerror(errno));
Ben Lindstromb257cca2001-03-05 04:59:27 +0000490 close(fd);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000491 return NULL;
Damien Millereba71ba2000-04-29 23:57:08 +1000492 }
Damien Miller0bc1bd82000-11-13 22:57:25 +1100493 pk = PEM_read_PrivateKey(fp, NULL, NULL, (char *)passphrase);
494 if (pk == NULL) {
Ben Lindstrom648772f2001-04-19 20:47:10 +0000495 debug("PEM_read_PrivateKey failed");
Damien Miller0bc1bd82000-11-13 22:57:25 +1100496 (void)ERR_get_error();
Ben Lindstromd0fca422001-03-26 13:44:06 +0000497 } else if (pk->type == EVP_PKEY_RSA &&
Damien Miller9f0f5c62001-12-21 14:45:46 +1100498 (type == KEY_UNSPEC||type==KEY_RSA)) {
Ben Lindstromd0fca422001-03-26 13:44:06 +0000499 prv = key_new(KEY_UNSPEC);
500 prv->rsa = EVP_PKEY_get1_RSA(pk);
501 prv->type = KEY_RSA;
502 name = "rsa w/o comment";
Damien Miller0bc1bd82000-11-13 22:57:25 +1100503#ifdef DEBUG_PK
Ben Lindstromd0fca422001-03-26 13:44:06 +0000504 RSA_print_fp(stderr, prv->rsa, 8);
Damien Millereba71ba2000-04-29 23:57:08 +1000505#endif
Damien Millered33d3b2003-03-15 11:36:18 +1100506 if (RSA_blinding_on(prv->rsa, NULL) != 1) {
507 error("key_load_private_pem: RSA_blinding_on failed");
508 key_free(prv);
509 prv = NULL;
510 }
Ben Lindstromd0fca422001-03-26 13:44:06 +0000511 } else if (pk->type == EVP_PKEY_DSA &&
Damien Miller9f0f5c62001-12-21 14:45:46 +1100512 (type == KEY_UNSPEC||type==KEY_DSA)) {
Ben Lindstromd0fca422001-03-26 13:44:06 +0000513 prv = key_new(KEY_UNSPEC);
514 prv->dsa = EVP_PKEY_get1_DSA(pk);
515 prv->type = KEY_DSA;
516 name = "dsa w/o comment";
Damien Miller0bc1bd82000-11-13 22:57:25 +1100517#ifdef DEBUG_PK
Ben Lindstromd0fca422001-03-26 13:44:06 +0000518 DSA_print_fp(stderr, prv->dsa, 8);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100519#endif
Damien Miller6af914a2010-09-10 11:39:26 +1000520#ifdef OPENSSL_HAS_ECC
Damien Millereb8b60e2010-08-31 22:41:14 +1000521 } else if (pk->type == EVP_PKEY_EC &&
522 (type == KEY_UNSPEC||type==KEY_ECDSA)) {
523 prv = key_new(KEY_UNSPEC);
524 prv->ecdsa = EVP_PKEY_get1_EC_KEY(pk);
525 prv->type = KEY_ECDSA;
526 prv->ecdsa_nid = key_ecdsa_group_to_nid(
527 EC_KEY_get0_group(prv->ecdsa));
528 if (key_curve_nid_to_name(prv->ecdsa_nid) == NULL) {
529 key_free(prv);
530 prv = NULL;
531 }
532 if (key_ec_validate_public(EC_KEY_get0_group(prv->ecdsa),
533 EC_KEY_get0_public_key(prv->ecdsa)) != 0 ||
534 key_ec_validate_private(prv->ecdsa) != 0) {
535 error("%s: bad ECDSA key", __func__);
536 key_free(prv);
537 prv = NULL;
538 }
Damien Millerbf0423e2010-09-10 11:20:38 +1000539 name = "ecdsa w/o comment";
Damien Millereb8b60e2010-08-31 22:41:14 +1000540#ifdef DEBUG_PK
541 if (prv->ecdsa != NULL)
542 key_dump_ec_key(prv->ecdsa);
543#endif
Damien Miller6af914a2010-09-10 11:39:26 +1000544#endif /* OPENSSL_HAS_ECC */
Damien Miller0bc1bd82000-11-13 22:57:25 +1100545 } else {
546 error("PEM_read_PrivateKey: mismatch or "
547 "unknown EVP_PKEY save_type %d", pk->save_type);
548 }
549 fclose(fp);
550 if (pk != NULL)
551 EVP_PKEY_free(pk);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000552 if (prv != NULL && commentp)
553 *commentp = xstrdup(name);
554 debug("read PEM private key done: type %s",
555 prv ? key_type(prv) : "<unknown>");
556 return prv;
Damien Millereba71ba2000-04-29 23:57:08 +1000557}
558
Damien Miller8275fad2006-03-15 12:06:23 +1100559int
Ben Lindstromd0fca422001-03-26 13:44:06 +0000560key_perm_ok(int fd, const char *filename)
Damien Millereba71ba2000-04-29 23:57:08 +1000561{
Damien Millereba71ba2000-04-29 23:57:08 +1000562 struct stat st;
563
Ben Lindstrom7aff2612001-09-23 13:53:22 +0000564 if (fstat(fd, &st) < 0)
565 return 0;
566 /*
567 * if a key owned by the user is accessed, then we check the
568 * permissions of the file. if the key owned by a different user,
569 * then we don't care.
570 */
Damien Millerb70b61f2000-09-16 16:25:12 +1100571#ifdef HAVE_CYGWIN
Damien Millercb5e44a2000-09-29 12:12:36 +1100572 if (check_ntsec(filename))
Damien Millerb70b61f2000-09-16 16:25:12 +1100573#endif
Ben Lindstrom7aff2612001-09-23 13:53:22 +0000574 if ((st.st_uid == getuid()) && (st.st_mode & 077) != 0) {
Damien Millereba71ba2000-04-29 23:57:08 +1000575 error("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
576 error("@ WARNING: UNPROTECTED PRIVATE KEY FILE! @");
577 error("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
Ben Lindstrom7aff2612001-09-23 13:53:22 +0000578 error("Permissions 0%3.3o for '%s' are too open.",
Damien Miller04bd8b02003-05-25 14:38:33 +1000579 (u_int)st.st_mode & 0777, filename);
Damien Millereba71ba2000-04-29 23:57:08 +1000580 error("It is recommended that your private key files are NOT accessible by others.");
Ben Lindstromd0fca422001-03-26 13:44:06 +0000581 error("This private key will be ignored.");
Damien Millereba71ba2000-04-29 23:57:08 +1000582 return 0;
583 }
Ben Lindstromd0fca422001-03-26 13:44:06 +0000584 return 1;
585}
Ben Lindstromb257cca2001-03-05 04:59:27 +0000586
Ben Lindstromd0fca422001-03-26 13:44:06 +0000587Key *
588key_load_private_type(int type, const char *filename, const char *passphrase,
Darren Tucker232b76f2006-05-06 17:41:51 +1000589 char **commentp, int *perm_ok)
Ben Lindstromd0fca422001-03-26 13:44:06 +0000590{
591 int fd;
592
593 fd = open(filename, O_RDONLY);
Darren Tuckerd4c86b12010-01-12 19:41:22 +1100594 if (fd < 0) {
595 debug("could not open key file '%s': %s", filename,
596 strerror(errno));
597 if (perm_ok != NULL)
598 *perm_ok = 0;
Ben Lindstromd0fca422001-03-26 13:44:06 +0000599 return NULL;
Darren Tucker69c01b12010-01-12 19:42:29 +1100600 }
Ben Lindstromd0fca422001-03-26 13:44:06 +0000601 if (!key_perm_ok(fd, filename)) {
Darren Tucker232b76f2006-05-06 17:41:51 +1000602 if (perm_ok != NULL)
603 *perm_ok = 0;
Ben Lindstrom15f33862001-04-16 02:00:02 +0000604 error("bad permissions: ignore key: %s", filename);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000605 close(fd);
606 return NULL;
607 }
Darren Tucker232b76f2006-05-06 17:41:51 +1000608 if (perm_ok != NULL)
609 *perm_ok = 1;
Ben Lindstromd0fca422001-03-26 13:44:06 +0000610 switch (type) {
611 case KEY_RSA1:
612 return key_load_private_rsa1(fd, filename, passphrase,
613 commentp);
614 /* closes fd */
Damien Millereba71ba2000-04-29 23:57:08 +1000615 case KEY_DSA:
Damien Millereb8b60e2010-08-31 22:41:14 +1000616 case KEY_ECDSA:
Damien Miller0bc1bd82000-11-13 22:57:25 +1100617 case KEY_RSA:
618 case KEY_UNSPEC:
Ben Lindstromd0fca422001-03-26 13:44:06 +0000619 return key_load_private_pem(fd, type, passphrase, commentp);
620 /* closes fd */
Damien Millereba71ba2000-04-29 23:57:08 +1000621 default:
Ben Lindstromb257cca2001-03-05 04:59:27 +0000622 close(fd);
Damien Millereba71ba2000-04-29 23:57:08 +1000623 break;
624 }
Ben Lindstromd0fca422001-03-26 13:44:06 +0000625 return NULL;
626}
627
628Key *
629key_load_private(const char *filename, const char *passphrase,
630 char **commentp)
631{
Ben Lindstrom322915d2001-06-05 20:46:32 +0000632 Key *pub, *prv;
Ben Lindstromd0fca422001-03-26 13:44:06 +0000633 int fd;
634
635 fd = open(filename, O_RDONLY);
Darren Tuckerd4c86b12010-01-12 19:41:22 +1100636 if (fd < 0) {
637 debug("could not open key file '%s': %s", filename,
638 strerror(errno));
Ben Lindstromd0fca422001-03-26 13:44:06 +0000639 return NULL;
Darren Tuckerd4c86b12010-01-12 19:41:22 +1100640 }
Ben Lindstromd0fca422001-03-26 13:44:06 +0000641 if (!key_perm_ok(fd, filename)) {
Ben Lindstrom15f33862001-04-16 02:00:02 +0000642 error("bad permissions: ignore key: %s", filename);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000643 close(fd);
644 return NULL;
645 }
646 pub = key_load_public_rsa1(fd, filename, commentp);
647 lseek(fd, (off_t) 0, SEEK_SET); /* rewind */
648 if (pub == NULL) {
649 /* closes fd */
Ben Lindstrom322915d2001-06-05 20:46:32 +0000650 prv = key_load_private_pem(fd, KEY_UNSPEC, passphrase, NULL);
651 /* use the filename as a comment for PEM */
652 if (commentp && prv)
Ben Lindstrom2d0356f2001-06-05 21:13:57 +0000653 *commentp = xstrdup(filename);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000654 } else {
655 /* it's a SSH v1 key if the public key part is readable */
656 key_free(pub);
657 /* closes fd */
Ben Lindstrom322915d2001-06-05 20:46:32 +0000658 prv = key_load_private_rsa1(fd, filename, passphrase, NULL);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000659 }
Ben Lindstrom322915d2001-06-05 20:46:32 +0000660 return prv;
Damien Millereba71ba2000-04-29 23:57:08 +1000661}
Damien Millere4340be2000-09-16 13:29:08 +1100662
Ben Lindstrombba81212001-06-25 05:01:22 +0000663static int
Ben Lindstromd0fca422001-03-26 13:44:06 +0000664key_try_load_public(Key *k, const char *filename, char **commentp)
Damien Millere4340be2000-09-16 13:29:08 +1100665{
666 FILE *f;
Darren Tucker22cc7412004-12-06 22:47:41 +1100667 char line[SSH_MAX_PUBKEY_BYTES];
Damien Millere4340be2000-09-16 13:29:08 +1100668 char *cp;
Darren Tuckerf0f90982004-12-11 13:39:50 +1100669 u_long linenum = 0;
Damien Millere4340be2000-09-16 13:29:08 +1100670
671 f = fopen(filename, "r");
672 if (f != NULL) {
Darren Tucker22cc7412004-12-06 22:47:41 +1100673 while (read_keyfile_line(f, filename, line, sizeof(line),
674 &linenum) != -1) {
Damien Millere4340be2000-09-16 13:29:08 +1100675 cp = line;
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000676 switch (*cp) {
Damien Millere4340be2000-09-16 13:29:08 +1100677 case '#':
678 case '\n':
679 case '\0':
680 continue;
681 }
682 /* Skip leading whitespace. */
683 for (; *cp && (*cp == ' ' || *cp == '\t'); cp++)
684 ;
685 if (*cp) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100686 if (key_read(k, &cp) == 1) {
Damien Millere4340be2000-09-16 13:29:08 +1100687 if (commentp)
688 *commentp=xstrdup(filename);
689 fclose(f);
690 return 1;
691 }
692 }
693 }
694 fclose(f);
695 }
696 return 0;
697}
698
Ben Lindstromd0fca422001-03-26 13:44:06 +0000699/* load public key from ssh v1 private or any pubkey file */
700Key *
701key_load_public(const char *filename, char **commentp)
Damien Millere4340be2000-09-16 13:29:08 +1100702{
Ben Lindstromd0fca422001-03-26 13:44:06 +0000703 Key *pub;
704 char file[MAXPATHLEN];
Damien Millere4340be2000-09-16 13:29:08 +1100705
Damien Millerdb274722003-05-14 13:45:22 +1000706 /* try rsa1 private key */
Ben Lindstromd0fca422001-03-26 13:44:06 +0000707 pub = key_load_public_type(KEY_RSA1, filename, commentp);
708 if (pub != NULL)
709 return pub;
Damien Millerdb274722003-05-14 13:45:22 +1000710
711 /* try rsa1 public key */
712 pub = key_new(KEY_RSA1);
713 if (key_try_load_public(pub, filename, commentp) == 1)
714 return pub;
715 key_free(pub);
716
717 /* try ssh2 public key */
Ben Lindstromd0fca422001-03-26 13:44:06 +0000718 pub = key_new(KEY_UNSPEC);
719 if (key_try_load_public(pub, filename, commentp) == 1)
720 return pub;
721 if ((strlcpy(file, filename, sizeof file) < sizeof(file)) &&
722 (strlcat(file, ".pub", sizeof file) < sizeof(file)) &&
723 (key_try_load_public(pub, file, commentp) == 1))
724 return pub;
725 key_free(pub);
726 return NULL;
Damien Millere4340be2000-09-16 13:29:08 +1100727}
Damien Miller1aed65e2010-03-04 21:53:35 +1100728
Damien Millerc1583312010-08-05 13:04:50 +1000729/* Load the certificate associated with the named private key */
730Key *
731key_load_cert(const char *filename)
732{
733 Key *pub;
Damien Miller5458c4d2010-08-05 13:05:15 +1000734 char *file;
Damien Millerc1583312010-08-05 13:04:50 +1000735
736 pub = key_new(KEY_UNSPEC);
Damien Miller5458c4d2010-08-05 13:05:15 +1000737 xasprintf(&file, "%s-cert.pub", filename);
738 if (key_try_load_public(pub, file, NULL) == 1) {
739 xfree(file);
Damien Millerc1583312010-08-05 13:04:50 +1000740 return pub;
Damien Miller5458c4d2010-08-05 13:05:15 +1000741 }
742 xfree(file);
Damien Millerc1583312010-08-05 13:04:50 +1000743 key_free(pub);
744 return NULL;
745}
746
747/* Load private key and certificate */
748Key *
749key_load_private_cert(int type, const char *filename, const char *passphrase,
750 int *perm_ok)
751{
752 Key *key, *pub;
753
754 switch (type) {
755 case KEY_RSA:
756 case KEY_DSA:
Damien Millereb8b60e2010-08-31 22:41:14 +1000757 case KEY_ECDSA:
Damien Millerc1583312010-08-05 13:04:50 +1000758 break;
759 default:
760 error("%s: unsupported key type", __func__);
761 return NULL;
762 }
763
764 if ((key = key_load_private_type(type, filename,
765 passphrase, NULL, perm_ok)) == NULL)
766 return NULL;
767
768 if ((pub = key_load_cert(filename)) == NULL) {
769 key_free(key);
770 return NULL;
771 }
772
773 /* Make sure the private key matches the certificate */
774 if (key_equal_public(key, pub) == 0) {
775 error("%s: certificate does not match private key %s",
776 __func__, filename);
777 } else if (key_to_certified(key, key_cert_is_legacy(pub)) != 0) {
778 error("%s: key_to_certified failed", __func__);
779 } else {
780 key_cert_copy(pub, key);
781 key_free(pub);
782 return key;
783 }
784
785 key_free(key);
786 key_free(pub);
787 return NULL;
788}
789
Damien Miller1aed65e2010-03-04 21:53:35 +1100790/*
791 * Returns 1 if the specified "key" is listed in the file "filename",
792 * 0 if the key is not listed or -1 on error.
793 * If strict_type is set then the key type must match exactly,
794 * otherwise a comparison that ignores certficiate data is performed.
795 */
796int
797key_in_file(Key *key, const char *filename, int strict_type)
798{
799 FILE *f;
800 char line[SSH_MAX_PUBKEY_BYTES];
801 char *cp;
802 u_long linenum = 0;
803 int ret = 0;
804 Key *pub;
805 int (*key_compare)(const Key *, const Key *) = strict_type ?
806 key_equal : key_equal_public;
807
808 if ((f = fopen(filename, "r")) == NULL) {
809 if (errno == ENOENT) {
810 debug("%s: keyfile \"%s\" missing", __func__, filename);
811 return 0;
812 } else {
813 error("%s: could not open keyfile \"%s\": %s", __func__,
814 filename, strerror(errno));
815 return -1;
816 }
817 }
818
819 while (read_keyfile_line(f, filename, line, sizeof(line),
820 &linenum) != -1) {
821 cp = line;
822
823 /* Skip leading whitespace. */
824 for (; *cp && (*cp == ' ' || *cp == '\t'); cp++)
825 ;
826
827 /* Skip comments and empty lines */
828 switch (*cp) {
829 case '#':
830 case '\n':
831 case '\0':
832 continue;
833 }
834
835 pub = key_new(KEY_UNSPEC);
836 if (key_read(pub, &cp) != 1) {
837 key_free(pub);
838 continue;
839 }
840 if (key_compare(key, pub)) {
841 ret = 1;
842 key_free(pub);
843 break;
844 }
845 key_free(pub);
846 }
847 fclose(f);
848 return ret;
849}
850