blob: 6a04cd7a95a290caa9b806b221a374fd3d478ed0 [file] [log] [blame]
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001/*
Damien Miller95def091999-11-25 00:26:21 +11002 * Author: Tatu Ylonen <ylo@cs.hut.fi>
Damien Miller95def091999-11-25 00:26:21 +11003 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
4 * All rights reserved
Damien Miller95def091999-11-25 00:26:21 +11005 * This file contains functions for reading and writing identity files, and
6 * for reading the passphrase from the user.
Damien Miller4af51302000-04-16 11:18:38 +10007 *
Damien Millere4340be2000-09-16 13:29:08 +11008 * As far as I am concerned, the code I have written for this software
9 * can be used freely for any purpose. Any derived versions of this
10 * software must be clearly marked as such, and if the derived work is
11 * incompatible with the protocol description in the RFC file, it must be
12 * called by a name other than "ssh" or "Secure Shell".
13 *
14 *
15 * Copyright (c) 2000 Markus Friedl. All rights reserved.
16 *
17 * Redistribution and use in source and binary forms, with or without
18 * modification, are permitted provided that the following conditions
19 * are met:
20 * 1. Redistributions of source code must retain the above copyright
21 * notice, this list of conditions and the following disclaimer.
22 * 2. Redistributions in binary form must reproduce the above copyright
23 * notice, this list of conditions and the following disclaimer in the
24 * documentation and/or other materials provided with the distribution.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
27 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
28 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
29 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
30 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
31 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
35 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Damien Miller95def091999-11-25 00:26:21 +110036 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100037
38#include "includes.h"
Darren Tuckerf0f90982004-12-11 13:39:50 +110039RCSID("$OpenBSD: authfile.c,v 1.60 2004/12/11 01:48:56 dtucker Exp $");
Damien Millerd4a8b7e1999-10-27 13:42:43 +100040
Damien Miller0bc1bd82000-11-13 22:57:25 +110041#include <openssl/err.h>
Damien Millereba71ba2000-04-29 23:57:08 +100042#include <openssl/evp.h>
Ben Lindstrom226cfa02001-01-22 05:34:40 +000043#include <openssl/pem.h>
Damien Millereba71ba2000-04-29 23:57:08 +100044
Ben Lindstrom226cfa02001-01-22 05:34:40 +000045#include "cipher.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100046#include "xmalloc.h"
47#include "buffer.h"
48#include "bufaux.h"
Damien Millereba71ba2000-04-29 23:57:08 +100049#include "key.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000050#include "ssh.h"
51#include "log.h"
Ben Lindstrom31ca54a2001-02-09 02:11:24 +000052#include "authfile.h"
Damien Miller040b64f2002-01-22 23:10:04 +110053#include "rsa.h"
Darren Tuckerf0f90982004-12-11 13:39:50 +110054#include "misc.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100055
Ben Lindstromd0fca422001-03-26 13:44:06 +000056/* Version identification string for SSH v1 identity files. */
Ben Lindstrom1170d712001-01-29 07:51:26 +000057static const char authfile_id_string[] =
58 "SSH PRIVATE KEY FILE FORMAT 1.1\n";
Damien Millerd4a8b7e1999-10-27 13:42:43 +100059
Damien Miller5428f641999-11-25 11:54:57 +110060/*
61 * Saves the authentication (private) key in a file, encrypting it with
62 * passphrase. The identification of the file (lowest 64 bits of n) will
63 * precede the key to provide identification of the key without needing a
64 * passphrase.
65 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100066
Ben Lindstrombba81212001-06-25 05:01:22 +000067static int
Ben Lindstromd0fca422001-03-26 13:44:06 +000068key_save_private_rsa1(Key *key, const char *filename, const char *passphrase,
69 const char *comment)
Damien Millerd4a8b7e1999-10-27 13:42:43 +100070{
Damien Miller95def091999-11-25 00:26:21 +110071 Buffer buffer, encrypted;
Damien Miller708d21c2002-01-22 23:18:15 +110072 u_char buf[100], *cp;
Damien Miller963f6b22002-02-19 15:21:23 +110073 int fd, i, cipher_num;
Damien Miller874d77b2000-10-14 16:23:11 +110074 CipherContext ciphercontext;
75 Cipher *cipher;
Darren Tucker3f9fdc72004-06-22 12:56:01 +100076 u_int32_t rnd;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100077
Damien Miller5428f641999-11-25 11:54:57 +110078 /*
79 * If the passphrase is empty, use SSH_CIPHER_NONE to ease converting
80 * to another cipher; otherwise use SSH_AUTHFILE_CIPHER.
81 */
Damien Miller963f6b22002-02-19 15:21:23 +110082 cipher_num = (strcmp(passphrase, "") == 0) ?
83 SSH_CIPHER_NONE : SSH_AUTHFILE_CIPHER;
84 if ((cipher = cipher_by_number(cipher_num)) == NULL)
Damien Miller874d77b2000-10-14 16:23:11 +110085 fatal("save_private_key_rsa: bad cipher");
Damien Millerd4a8b7e1999-10-27 13:42:43 +100086
Damien Miller95def091999-11-25 00:26:21 +110087 /* This buffer is used to built the secret part of the private key. */
88 buffer_init(&buffer);
Damien Millerd4a8b7e1999-10-27 13:42:43 +100089
Damien Miller95def091999-11-25 00:26:21 +110090 /* Put checkbytes for checking passphrase validity. */
Darren Tucker3f9fdc72004-06-22 12:56:01 +100091 rnd = arc4random();
92 buf[0] = rnd & 0xff;
93 buf[1] = (rnd >> 8) & 0xff;
Damien Miller95def091999-11-25 00:26:21 +110094 buf[2] = buf[0];
95 buf[3] = buf[1];
96 buffer_append(&buffer, buf, 4);
Damien Millerd4a8b7e1999-10-27 13:42:43 +100097
Damien Miller5428f641999-11-25 11:54:57 +110098 /*
99 * Store the private key (n and e will not be stored because they
100 * will be stored in plain text, and storing them also in encrypted
101 * format would just give known plaintext).
102 */
Ben Lindstromd0fca422001-03-26 13:44:06 +0000103 buffer_put_bignum(&buffer, key->rsa->d);
104 buffer_put_bignum(&buffer, key->rsa->iqmp);
105 buffer_put_bignum(&buffer, key->rsa->q); /* reverse from SSL p */
106 buffer_put_bignum(&buffer, key->rsa->p); /* reverse from SSL q */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000107
Damien Miller95def091999-11-25 00:26:21 +1100108 /* Pad the part to be encrypted until its size is a multiple of 8. */
109 while (buffer_len(&buffer) % 8 != 0)
110 buffer_put_char(&buffer, 0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000111
Damien Miller95def091999-11-25 00:26:21 +1100112 /* This buffer will be used to contain the data in the file. */
113 buffer_init(&encrypted);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000114
Damien Miller95def091999-11-25 00:26:21 +1100115 /* First store keyfile id string. */
Ben Lindstrom1170d712001-01-29 07:51:26 +0000116 for (i = 0; authfile_id_string[i]; i++)
117 buffer_put_char(&encrypted, authfile_id_string[i]);
Damien Miller95def091999-11-25 00:26:21 +1100118 buffer_put_char(&encrypted, 0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000119
Damien Miller95def091999-11-25 00:26:21 +1100120 /* Store cipher type. */
Damien Miller963f6b22002-02-19 15:21:23 +1100121 buffer_put_char(&encrypted, cipher_num);
Damien Miller95def091999-11-25 00:26:21 +1100122 buffer_put_int(&encrypted, 0); /* For future extension */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000123
Damien Miller95def091999-11-25 00:26:21 +1100124 /* Store public key. This will be in plain text. */
Ben Lindstromd0fca422001-03-26 13:44:06 +0000125 buffer_put_int(&encrypted, BN_num_bits(key->rsa->n));
126 buffer_put_bignum(&encrypted, key->rsa->n);
127 buffer_put_bignum(&encrypted, key->rsa->e);
Ben Lindstrom664408d2001-06-09 01:42:01 +0000128 buffer_put_cstring(&encrypted, comment);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000129
Damien Miller95def091999-11-25 00:26:21 +1100130 /* Allocate space for the private part of the key in the buffer. */
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100131 cp = buffer_append_space(&encrypted, buffer_len(&buffer));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000132
Damien Miller963f6b22002-02-19 15:21:23 +1100133 cipher_set_key_string(&ciphercontext, cipher, passphrase,
134 CIPHER_ENCRYPT);
135 cipher_crypt(&ciphercontext, cp,
Damien Miller708d21c2002-01-22 23:18:15 +1100136 buffer_ptr(&buffer), buffer_len(&buffer));
Damien Miller963f6b22002-02-19 15:21:23 +1100137 cipher_cleanup(&ciphercontext);
Damien Miller874d77b2000-10-14 16:23:11 +1100138 memset(&ciphercontext, 0, sizeof(ciphercontext));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000139
Damien Miller95def091999-11-25 00:26:21 +1100140 /* Destroy temporary data. */
141 memset(buf, 0, sizeof(buf));
142 buffer_free(&buffer);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000143
Damien Miller037a0dc1999-12-07 15:38:31 +1100144 fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0600);
Ben Lindstrom15f33862001-04-16 02:00:02 +0000145 if (fd < 0) {
146 error("open %s failed: %s.", filename, strerror(errno));
Darren Tuckerd1d41b32003-09-22 21:01:27 +1000147 buffer_free(&encrypted);
Damien Miller95def091999-11-25 00:26:21 +1100148 return 0;
Ben Lindstrom15f33862001-04-16 02:00:02 +0000149 }
Damien Miller037a0dc1999-12-07 15:38:31 +1100150 if (write(fd, buffer_ptr(&encrypted), buffer_len(&encrypted)) !=
Damien Miller95def091999-11-25 00:26:21 +1100151 buffer_len(&encrypted)) {
Ben Lindstrom15f33862001-04-16 02:00:02 +0000152 error("write to key file %s failed: %s", filename,
Damien Miller9f0f5c62001-12-21 14:45:46 +1100153 strerror(errno));
Damien Miller95def091999-11-25 00:26:21 +1100154 buffer_free(&encrypted);
Damien Miller037a0dc1999-12-07 15:38:31 +1100155 close(fd);
Damien Miller78315eb2000-09-29 23:01:36 +1100156 unlink(filename);
Damien Miller95def091999-11-25 00:26:21 +1100157 return 0;
158 }
Damien Miller037a0dc1999-12-07 15:38:31 +1100159 close(fd);
Damien Miller95def091999-11-25 00:26:21 +1100160 buffer_free(&encrypted);
161 return 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000162}
163
Ben Lindstromd0fca422001-03-26 13:44:06 +0000164/* save SSH v2 key in OpenSSL PEM format */
Ben Lindstrombba81212001-06-25 05:01:22 +0000165static int
Ben Lindstromd0fca422001-03-26 13:44:06 +0000166key_save_private_pem(Key *key, const char *filename, const char *_passphrase,
167 const char *comment)
Damien Millereba71ba2000-04-29 23:57:08 +1000168{
169 FILE *fp;
170 int fd;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100171 int success = 0;
172 int len = strlen(_passphrase);
Ben Lindstrom90fd8142002-02-26 18:09:42 +0000173 u_char *passphrase = (len > 0) ? (u_char *)_passphrase : NULL;
Ben Lindstrom80cb27d2002-03-05 01:33:36 +0000174 const EVP_CIPHER *cipher = (len > 0) ? EVP_des_ede3_cbc() : NULL;
Damien Millereba71ba2000-04-29 23:57:08 +1000175
176 if (len > 0 && len <= 4) {
Ben Lindstrom15f33862001-04-16 02:00:02 +0000177 error("passphrase too short: have %d bytes, need > 4", len);
Damien Millereba71ba2000-04-29 23:57:08 +1000178 return 0;
179 }
180 fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0600);
181 if (fd < 0) {
Ben Lindstrom15f33862001-04-16 02:00:02 +0000182 error("open %s failed: %s.", filename, strerror(errno));
Damien Millereba71ba2000-04-29 23:57:08 +1000183 return 0;
184 }
185 fp = fdopen(fd, "w");
186 if (fp == NULL ) {
Ben Lindstrom15f33862001-04-16 02:00:02 +0000187 error("fdopen %s failed: %s.", filename, strerror(errno));
Damien Millereba71ba2000-04-29 23:57:08 +1000188 close(fd);
189 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:
193 success = PEM_write_DSAPrivateKey(fp, key->dsa,
194 cipher, passphrase, len, NULL, NULL);
195 break;
196 case KEY_RSA:
197 success = PEM_write_RSAPrivateKey(fp, key->rsa,
198 cipher, passphrase, len, NULL, NULL);
199 break;
Damien Millereba71ba2000-04-29 23:57:08 +1000200 }
201 fclose(fp);
202 return success;
203}
204
205int
Ben Lindstromd0fca422001-03-26 13:44:06 +0000206key_save_private(Key *key, const char *filename, const char *passphrase,
Damien Millereba71ba2000-04-29 23:57:08 +1000207 const char *comment)
208{
209 switch (key->type) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100210 case KEY_RSA1:
Ben Lindstromd0fca422001-03-26 13:44:06 +0000211 return key_save_private_rsa1(key, filename, passphrase,
212 comment);
Damien Millereba71ba2000-04-29 23:57:08 +1000213 break;
214 case KEY_DSA:
Damien Miller0bc1bd82000-11-13 22:57:25 +1100215 case KEY_RSA:
Ben Lindstromd0fca422001-03-26 13:44:06 +0000216 return key_save_private_pem(key, filename, passphrase,
217 comment);
Damien Millereba71ba2000-04-29 23:57:08 +1000218 break;
219 default:
220 break;
221 }
Ben Lindstrom15f33862001-04-16 02:00:02 +0000222 error("key_save_private: cannot save key type %d", key->type);
Damien Millereba71ba2000-04-29 23:57:08 +1000223 return 0;
224}
225
Damien Miller5428f641999-11-25 11:54:57 +1100226/*
Ben Lindstromd0fca422001-03-26 13:44:06 +0000227 * Loads the public part of the ssh v1 key file. Returns NULL if an error was
228 * encountered (the file does not exist or is not readable), and the key
Damien Miller5428f641999-11-25 11:54:57 +1100229 * otherwise.
230 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000231
Ben Lindstrombba81212001-06-25 05:01:22 +0000232static Key *
Ben Lindstromd0fca422001-03-26 13:44:06 +0000233key_load_public_rsa1(int fd, const char *filename, char **commentp)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000234{
Damien Miller95def091999-11-25 00:26:21 +1100235 Buffer buffer;
Ben Lindstromd0fca422001-03-26 13:44:06 +0000236 Key *pub;
Ben Lindstrom44adb8f2002-12-23 02:00:23 +0000237 struct stat st;
Damien Miller95def091999-11-25 00:26:21 +1100238 char *cp;
Ben Lindstromd0fca422001-03-26 13:44:06 +0000239 int i;
Darren Tucker1f8311c2004-05-13 16:39:33 +1000240 size_t len;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000241
Ben Lindstrom44adb8f2002-12-23 02:00:23 +0000242 if (fstat(fd, &st) < 0) {
243 error("fstat for key file %.200s failed: %.100s",
244 filename, strerror(errno));
245 return NULL;
246 }
Darren Tuckerf4b43712004-08-29 16:28:39 +1000247 if (st.st_size > 1*1024*1024) {
248 error("key file %.200s too large", filename);
249 return NULL;
250 }
Darren Tucker1f8311c2004-05-13 16:39:33 +1000251 len = (size_t)st.st_size; /* truncated */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000252
Damien Miller95def091999-11-25 00:26:21 +1100253 buffer_init(&buffer);
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100254 cp = buffer_append_space(&buffer, len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000255
Damien Miller037a0dc1999-12-07 15:38:31 +1100256 if (read(fd, cp, (size_t) len) != (size_t) len) {
Damien Miller95def091999-11-25 00:26:21 +1100257 debug("Read from key file %.200s failed: %.100s", filename,
Damien Millereba71ba2000-04-29 23:57:08 +1000258 strerror(errno));
Damien Miller95def091999-11-25 00:26:21 +1100259 buffer_free(&buffer);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000260 return NULL;
Damien Miller95def091999-11-25 00:26:21 +1100261 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000262
Ben Lindstrom1170d712001-01-29 07:51:26 +0000263 /* Check that it is at least big enough to contain the ID string. */
264 if (len < sizeof(authfile_id_string)) {
Damien Miller058655c2001-10-10 15:03:36 +1000265 debug3("Not a RSA1 key file %.200s.", filename);
Damien Miller95def091999-11-25 00:26:21 +1100266 buffer_free(&buffer);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000267 return NULL;
Damien Miller95def091999-11-25 00:26:21 +1100268 }
Damien Miller5428f641999-11-25 11:54:57 +1100269 /*
270 * Make sure it begins with the id string. Consume the id string
271 * from the buffer.
272 */
Ben Lindstrom1170d712001-01-29 07:51:26 +0000273 for (i = 0; i < sizeof(authfile_id_string); i++)
274 if (buffer_get_char(&buffer) != authfile_id_string[i]) {
Damien Miller058655c2001-10-10 15:03:36 +1000275 debug3("Not a RSA1 key file %.200s.", filename);
Damien Miller95def091999-11-25 00:26:21 +1100276 buffer_free(&buffer);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000277 return NULL;
Damien Miller95def091999-11-25 00:26:21 +1100278 }
279 /* Skip cipher type and reserved data. */
280 (void) buffer_get_char(&buffer); /* cipher type */
281 (void) buffer_get_int(&buffer); /* reserved */
282
283 /* Read the public key from the buffer. */
Ben Lindstromc5a7f4f2002-06-25 23:19:13 +0000284 (void) buffer_get_int(&buffer);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000285 pub = key_new(KEY_RSA1);
286 buffer_get_bignum(&buffer, pub->rsa->n);
287 buffer_get_bignum(&buffer, pub->rsa->e);
288 if (commentp)
289 *commentp = buffer_get_string(&buffer, NULL);
Damien Miller95def091999-11-25 00:26:21 +1100290 /* The encrypted private part is not parsed by this function. */
291
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000292 buffer_free(&buffer);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000293 return pub;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000294}
295
Ben Lindstromd0fca422001-03-26 13:44:06 +0000296/* load public key from private-key file, works only for SSH v1 */
297Key *
298key_load_public_type(int type, const char *filename, char **commentp)
Damien Millereba71ba2000-04-29 23:57:08 +1000299{
Ben Lindstromd0fca422001-03-26 13:44:06 +0000300 Key *pub;
301 int fd;
302
303 if (type == KEY_RSA1) {
304 fd = open(filename, O_RDONLY);
305 if (fd < 0)
306 return NULL;
307 pub = key_load_public_rsa1(fd, filename, commentp);
308 close(fd);
309 return pub;
Damien Millereba71ba2000-04-29 23:57:08 +1000310 }
Ben Lindstromd0fca422001-03-26 13:44:06 +0000311 return NULL;
Damien Millereba71ba2000-04-29 23:57:08 +1000312}
313
Damien Miller5428f641999-11-25 11:54:57 +1100314/*
315 * Loads the private key from the file. Returns 0 if an error is encountered
316 * (file does not exist or is not readable, or passphrase is bad). This
317 * initializes the private key.
318 * Assumes we are called under uid of the owner of the file.
319 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000320
Ben Lindstrombba81212001-06-25 05:01:22 +0000321static Key *
Ben Lindstromd0fca422001-03-26 13:44:06 +0000322key_load_private_rsa1(int fd, const char *filename, const char *passphrase,
323 char **commentp)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000324{
Damien Millereba71ba2000-04-29 23:57:08 +1000325 int i, check1, check2, cipher_type;
Darren Tucker1f8311c2004-05-13 16:39:33 +1000326 size_t len;
Damien Miller95def091999-11-25 00:26:21 +1100327 Buffer buffer, decrypted;
Damien Miller708d21c2002-01-22 23:18:15 +1100328 u_char *cp;
Damien Miller874d77b2000-10-14 16:23:11 +1100329 CipherContext ciphercontext;
330 Cipher *cipher;
Ben Lindstromd0fca422001-03-26 13:44:06 +0000331 Key *prv = NULL;
Ben Lindstrom44adb8f2002-12-23 02:00:23 +0000332 struct stat st;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000333
Ben Lindstrom44adb8f2002-12-23 02:00:23 +0000334 if (fstat(fd, &st) < 0) {
335 error("fstat for key file %.200s failed: %.100s",
336 filename, strerror(errno));
337 close(fd);
338 return NULL;
339 }
Darren Tucker1f8311c2004-05-13 16:39:33 +1000340 if (st.st_size > 1*1024*1024) {
Darren Tuckerf4b43712004-08-29 16:28:39 +1000341 error("key file %.200s too large", filename);
Darren Tucker1f8311c2004-05-13 16:39:33 +1000342 close(fd);
343 return (NULL);
344 }
345 len = (size_t)st.st_size; /* truncated */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000346
Damien Miller95def091999-11-25 00:26:21 +1100347 buffer_init(&buffer);
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100348 cp = buffer_append_space(&buffer, len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000349
Damien Miller037a0dc1999-12-07 15:38:31 +1100350 if (read(fd, cp, (size_t) len) != (size_t) len) {
Damien Miller95def091999-11-25 00:26:21 +1100351 debug("Read from key file %.200s failed: %.100s", filename,
Damien Miller0bc1bd82000-11-13 22:57:25 +1100352 strerror(errno));
Damien Miller95def091999-11-25 00:26:21 +1100353 buffer_free(&buffer);
Damien Miller037a0dc1999-12-07 15:38:31 +1100354 close(fd);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000355 return NULL;
Damien Miller95def091999-11-25 00:26:21 +1100356 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000357
Ben Lindstrom1170d712001-01-29 07:51:26 +0000358 /* Check that it is at least big enough to contain the ID string. */
359 if (len < sizeof(authfile_id_string)) {
Damien Miller058655c2001-10-10 15:03:36 +1000360 debug3("Not a RSA1 key file %.200s.", filename);
Damien Miller95def091999-11-25 00:26:21 +1100361 buffer_free(&buffer);
Ben Lindstromb257cca2001-03-05 04:59:27 +0000362 close(fd);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000363 return NULL;
Damien Miller95def091999-11-25 00:26:21 +1100364 }
Damien Miller5428f641999-11-25 11:54:57 +1100365 /*
366 * Make sure it begins with the id string. Consume the id string
367 * from the buffer.
368 */
Ben Lindstrom1170d712001-01-29 07:51:26 +0000369 for (i = 0; i < sizeof(authfile_id_string); i++)
370 if (buffer_get_char(&buffer) != authfile_id_string[i]) {
Damien Miller058655c2001-10-10 15:03:36 +1000371 debug3("Not a RSA1 key file %.200s.", filename);
Damien Miller95def091999-11-25 00:26:21 +1100372 buffer_free(&buffer);
Ben Lindstromb257cca2001-03-05 04:59:27 +0000373 close(fd);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000374 return NULL;
Damien Miller95def091999-11-25 00:26:21 +1100375 }
Ben Lindstromb257cca2001-03-05 04:59:27 +0000376
Damien Miller95def091999-11-25 00:26:21 +1100377 /* Read cipher type. */
378 cipher_type = buffer_get_char(&buffer);
379 (void) buffer_get_int(&buffer); /* Reserved data. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000380
Damien Miller95def091999-11-25 00:26:21 +1100381 /* Read the public key from the buffer. */
Ben Lindstromc5a7f4f2002-06-25 23:19:13 +0000382 (void) buffer_get_int(&buffer);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000383 prv = key_new_private(KEY_RSA1);
384
385 buffer_get_bignum(&buffer, prv->rsa->n);
386 buffer_get_bignum(&buffer, prv->rsa->e);
387 if (commentp)
388 *commentp = buffer_get_string(&buffer, NULL);
Damien Miller95def091999-11-25 00:26:21 +1100389 else
390 xfree(buffer_get_string(&buffer, NULL));
391
392 /* Check that it is a supported cipher. */
Damien Miller874d77b2000-10-14 16:23:11 +1100393 cipher = cipher_by_number(cipher_type);
394 if (cipher == NULL) {
395 debug("Unsupported cipher %d used in key file %.200s.",
396 cipher_type, filename);
Damien Miller95def091999-11-25 00:26:21 +1100397 buffer_free(&buffer);
398 goto fail;
399 }
400 /* Initialize space for decrypted data. */
401 buffer_init(&decrypted);
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100402 cp = buffer_append_space(&decrypted, buffer_len(&buffer));
Damien Miller95def091999-11-25 00:26:21 +1100403
404 /* Rest of the buffer is encrypted. Decrypt it using the passphrase. */
Damien Miller963f6b22002-02-19 15:21:23 +1100405 cipher_set_key_string(&ciphercontext, cipher, passphrase,
406 CIPHER_DECRYPT);
407 cipher_crypt(&ciphercontext, cp,
Damien Miller708d21c2002-01-22 23:18:15 +1100408 buffer_ptr(&buffer), buffer_len(&buffer));
Damien Miller963f6b22002-02-19 15:21:23 +1100409 cipher_cleanup(&ciphercontext);
Damien Miller874d77b2000-10-14 16:23:11 +1100410 memset(&ciphercontext, 0, sizeof(ciphercontext));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000411 buffer_free(&buffer);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000412
Damien Miller95def091999-11-25 00:26:21 +1100413 check1 = buffer_get_char(&decrypted);
414 check2 = buffer_get_char(&decrypted);
415 if (check1 != buffer_get_char(&decrypted) ||
416 check2 != buffer_get_char(&decrypted)) {
417 if (strcmp(passphrase, "") != 0)
Ben Lindstromd0fca422001-03-26 13:44:06 +0000418 debug("Bad passphrase supplied for key file %.200s.",
419 filename);
Damien Miller95def091999-11-25 00:26:21 +1100420 /* Bad passphrase. */
421 buffer_free(&decrypted);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000422 goto fail;
Damien Miller95def091999-11-25 00:26:21 +1100423 }
424 /* Read the rest of the private key. */
Ben Lindstromd0fca422001-03-26 13:44:06 +0000425 buffer_get_bignum(&decrypted, prv->rsa->d);
426 buffer_get_bignum(&decrypted, prv->rsa->iqmp); /* u */
427 /* in SSL and SSH v1 p and q are exchanged */
428 buffer_get_bignum(&decrypted, prv->rsa->q); /* p */
429 buffer_get_bignum(&decrypted, prv->rsa->p); /* q */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000430
Ben Lindstromd0fca422001-03-26 13:44:06 +0000431 /* calculate p-1 and q-1 */
Damien Millerda755162002-01-22 23:09:22 +1100432 rsa_generate_additional_parameters(prv->rsa);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000433
Damien Miller95def091999-11-25 00:26:21 +1100434 buffer_free(&decrypted);
Damien Millered33d3b2003-03-15 11:36:18 +1100435
436 /* enable blinding */
437 if (RSA_blinding_on(prv->rsa, NULL) != 1) {
438 error("key_load_private_rsa1: RSA_blinding_on failed");
439 goto fail;
440 }
Ben Lindstromb257cca2001-03-05 04:59:27 +0000441 close(fd);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000442 return prv;
443
444fail:
445 if (commentp)
446 xfree(*commentp);
447 close(fd);
448 key_free(prv);
449 return NULL;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000450}
Damien Millereba71ba2000-04-29 23:57:08 +1000451
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000452Key *
Ben Lindstromd0fca422001-03-26 13:44:06 +0000453key_load_private_pem(int fd, int type, const char *passphrase,
454 char **commentp)
Damien Millereba71ba2000-04-29 23:57:08 +1000455{
Damien Millereba71ba2000-04-29 23:57:08 +1000456 FILE *fp;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100457 EVP_PKEY *pk = NULL;
Ben Lindstromd0fca422001-03-26 13:44:06 +0000458 Key *prv = NULL;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100459 char *name = "<no key>";
Damien Millereba71ba2000-04-29 23:57:08 +1000460
Damien Millereba71ba2000-04-29 23:57:08 +1000461 fp = fdopen(fd, "r");
462 if (fp == NULL) {
Ben Lindstrom15f33862001-04-16 02:00:02 +0000463 error("fdopen failed: %s", strerror(errno));
Ben Lindstromb257cca2001-03-05 04:59:27 +0000464 close(fd);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000465 return NULL;
Damien Millereba71ba2000-04-29 23:57:08 +1000466 }
Damien Miller0bc1bd82000-11-13 22:57:25 +1100467 pk = PEM_read_PrivateKey(fp, NULL, NULL, (char *)passphrase);
468 if (pk == NULL) {
Ben Lindstrom648772f2001-04-19 20:47:10 +0000469 debug("PEM_read_PrivateKey failed");
Damien Miller0bc1bd82000-11-13 22:57:25 +1100470 (void)ERR_get_error();
Ben Lindstromd0fca422001-03-26 13:44:06 +0000471 } else if (pk->type == EVP_PKEY_RSA &&
Damien Miller9f0f5c62001-12-21 14:45:46 +1100472 (type == KEY_UNSPEC||type==KEY_RSA)) {
Ben Lindstromd0fca422001-03-26 13:44:06 +0000473 prv = key_new(KEY_UNSPEC);
474 prv->rsa = EVP_PKEY_get1_RSA(pk);
475 prv->type = KEY_RSA;
476 name = "rsa w/o comment";
Damien Miller0bc1bd82000-11-13 22:57:25 +1100477#ifdef DEBUG_PK
Ben Lindstromd0fca422001-03-26 13:44:06 +0000478 RSA_print_fp(stderr, prv->rsa, 8);
Damien Millereba71ba2000-04-29 23:57:08 +1000479#endif
Damien Millered33d3b2003-03-15 11:36:18 +1100480 if (RSA_blinding_on(prv->rsa, NULL) != 1) {
481 error("key_load_private_pem: RSA_blinding_on failed");
482 key_free(prv);
483 prv = NULL;
484 }
Ben Lindstromd0fca422001-03-26 13:44:06 +0000485 } else if (pk->type == EVP_PKEY_DSA &&
Damien Miller9f0f5c62001-12-21 14:45:46 +1100486 (type == KEY_UNSPEC||type==KEY_DSA)) {
Ben Lindstromd0fca422001-03-26 13:44:06 +0000487 prv = key_new(KEY_UNSPEC);
488 prv->dsa = EVP_PKEY_get1_DSA(pk);
489 prv->type = KEY_DSA;
490 name = "dsa w/o comment";
Damien Miller0bc1bd82000-11-13 22:57:25 +1100491#ifdef DEBUG_PK
Ben Lindstromd0fca422001-03-26 13:44:06 +0000492 DSA_print_fp(stderr, prv->dsa, 8);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100493#endif
Damien Miller0bc1bd82000-11-13 22:57:25 +1100494 } else {
495 error("PEM_read_PrivateKey: mismatch or "
496 "unknown EVP_PKEY save_type %d", pk->save_type);
497 }
498 fclose(fp);
499 if (pk != NULL)
500 EVP_PKEY_free(pk);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000501 if (prv != NULL && commentp)
502 *commentp = xstrdup(name);
503 debug("read PEM private key done: type %s",
504 prv ? key_type(prv) : "<unknown>");
505 return prv;
Damien Millereba71ba2000-04-29 23:57:08 +1000506}
507
Ben Lindstrombba81212001-06-25 05:01:22 +0000508static int
Ben Lindstromd0fca422001-03-26 13:44:06 +0000509key_perm_ok(int fd, const char *filename)
Damien Millereba71ba2000-04-29 23:57:08 +1000510{
Damien Millereba71ba2000-04-29 23:57:08 +1000511 struct stat st;
512
Ben Lindstrom7aff2612001-09-23 13:53:22 +0000513 if (fstat(fd, &st) < 0)
514 return 0;
515 /*
516 * if a key owned by the user is accessed, then we check the
517 * permissions of the file. if the key owned by a different user,
518 * then we don't care.
519 */
Damien Millerb70b61f2000-09-16 16:25:12 +1100520#ifdef HAVE_CYGWIN
Damien Millercb5e44a2000-09-29 12:12:36 +1100521 if (check_ntsec(filename))
Damien Millerb70b61f2000-09-16 16:25:12 +1100522#endif
Ben Lindstrom7aff2612001-09-23 13:53:22 +0000523 if ((st.st_uid == getuid()) && (st.st_mode & 077) != 0) {
Damien Millereba71ba2000-04-29 23:57:08 +1000524 error("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
525 error("@ WARNING: UNPROTECTED PRIVATE KEY FILE! @");
526 error("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
Ben Lindstrom7aff2612001-09-23 13:53:22 +0000527 error("Permissions 0%3.3o for '%s' are too open.",
Damien Miller04bd8b02003-05-25 14:38:33 +1000528 (u_int)st.st_mode & 0777, filename);
Damien Millereba71ba2000-04-29 23:57:08 +1000529 error("It is recommended that your private key files are NOT accessible by others.");
Ben Lindstromd0fca422001-03-26 13:44:06 +0000530 error("This private key will be ignored.");
Damien Millereba71ba2000-04-29 23:57:08 +1000531 return 0;
532 }
Ben Lindstromd0fca422001-03-26 13:44:06 +0000533 return 1;
534}
Ben Lindstromb257cca2001-03-05 04:59:27 +0000535
Ben Lindstromd0fca422001-03-26 13:44:06 +0000536Key *
537key_load_private_type(int type, const char *filename, const char *passphrase,
538 char **commentp)
539{
540 int fd;
541
542 fd = open(filename, O_RDONLY);
543 if (fd < 0)
544 return NULL;
545 if (!key_perm_ok(fd, filename)) {
Ben Lindstrom15f33862001-04-16 02:00:02 +0000546 error("bad permissions: ignore key: %s", filename);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000547 close(fd);
548 return NULL;
549 }
550 switch (type) {
551 case KEY_RSA1:
552 return key_load_private_rsa1(fd, filename, passphrase,
553 commentp);
554 /* closes fd */
Damien Millereba71ba2000-04-29 23:57:08 +1000555 break;
556 case KEY_DSA:
Damien Miller0bc1bd82000-11-13 22:57:25 +1100557 case KEY_RSA:
558 case KEY_UNSPEC:
Ben Lindstromd0fca422001-03-26 13:44:06 +0000559 return key_load_private_pem(fd, type, passphrase, commentp);
560 /* closes fd */
Ben Lindstromb257cca2001-03-05 04:59:27 +0000561 break;
Damien Millereba71ba2000-04-29 23:57:08 +1000562 default:
Ben Lindstromb257cca2001-03-05 04:59:27 +0000563 close(fd);
Damien Millereba71ba2000-04-29 23:57:08 +1000564 break;
565 }
Ben Lindstromd0fca422001-03-26 13:44:06 +0000566 return NULL;
567}
568
569Key *
570key_load_private(const char *filename, const char *passphrase,
571 char **commentp)
572{
Ben Lindstrom322915d2001-06-05 20:46:32 +0000573 Key *pub, *prv;
Ben Lindstromd0fca422001-03-26 13:44:06 +0000574 int fd;
575
576 fd = open(filename, O_RDONLY);
577 if (fd < 0)
578 return NULL;
579 if (!key_perm_ok(fd, filename)) {
Ben Lindstrom15f33862001-04-16 02:00:02 +0000580 error("bad permissions: ignore key: %s", filename);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000581 close(fd);
582 return NULL;
583 }
584 pub = key_load_public_rsa1(fd, filename, commentp);
585 lseek(fd, (off_t) 0, SEEK_SET); /* rewind */
586 if (pub == NULL) {
587 /* closes fd */
Ben Lindstrom322915d2001-06-05 20:46:32 +0000588 prv = key_load_private_pem(fd, KEY_UNSPEC, passphrase, NULL);
589 /* use the filename as a comment for PEM */
590 if (commentp && prv)
Ben Lindstrom2d0356f2001-06-05 21:13:57 +0000591 *commentp = xstrdup(filename);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000592 } else {
593 /* it's a SSH v1 key if the public key part is readable */
594 key_free(pub);
595 /* closes fd */
Ben Lindstrom322915d2001-06-05 20:46:32 +0000596 prv = key_load_private_rsa1(fd, filename, passphrase, NULL);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000597 }
Ben Lindstrom322915d2001-06-05 20:46:32 +0000598 return prv;
Damien Millereba71ba2000-04-29 23:57:08 +1000599}
Damien Millere4340be2000-09-16 13:29:08 +1100600
Ben Lindstrombba81212001-06-25 05:01:22 +0000601static int
Ben Lindstromd0fca422001-03-26 13:44:06 +0000602key_try_load_public(Key *k, const char *filename, char **commentp)
Damien Millere4340be2000-09-16 13:29:08 +1100603{
604 FILE *f;
Darren Tucker22cc7412004-12-06 22:47:41 +1100605 char line[SSH_MAX_PUBKEY_BYTES];
Damien Millere4340be2000-09-16 13:29:08 +1100606 char *cp;
Darren Tuckerf0f90982004-12-11 13:39:50 +1100607 u_long linenum = 0;
Damien Millere4340be2000-09-16 13:29:08 +1100608
609 f = fopen(filename, "r");
610 if (f != NULL) {
Darren Tucker22cc7412004-12-06 22:47:41 +1100611 while (read_keyfile_line(f, filename, line, sizeof(line),
612 &linenum) != -1) {
Damien Millere4340be2000-09-16 13:29:08 +1100613 cp = line;
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000614 switch (*cp) {
Damien Millere4340be2000-09-16 13:29:08 +1100615 case '#':
616 case '\n':
617 case '\0':
618 continue;
619 }
620 /* Skip leading whitespace. */
621 for (; *cp && (*cp == ' ' || *cp == '\t'); cp++)
622 ;
623 if (*cp) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100624 if (key_read(k, &cp) == 1) {
Damien Millere4340be2000-09-16 13:29:08 +1100625 if (commentp)
626 *commentp=xstrdup(filename);
627 fclose(f);
628 return 1;
629 }
630 }
631 }
632 fclose(f);
633 }
634 return 0;
635}
636
Ben Lindstromd0fca422001-03-26 13:44:06 +0000637/* load public key from ssh v1 private or any pubkey file */
638Key *
639key_load_public(const char *filename, char **commentp)
Damien Millere4340be2000-09-16 13:29:08 +1100640{
Ben Lindstromd0fca422001-03-26 13:44:06 +0000641 Key *pub;
642 char file[MAXPATHLEN];
Damien Millere4340be2000-09-16 13:29:08 +1100643
Damien Millerdb274722003-05-14 13:45:22 +1000644 /* try rsa1 private key */
Ben Lindstromd0fca422001-03-26 13:44:06 +0000645 pub = key_load_public_type(KEY_RSA1, filename, commentp);
646 if (pub != NULL)
647 return pub;
Damien Millerdb274722003-05-14 13:45:22 +1000648
649 /* try rsa1 public key */
650 pub = key_new(KEY_RSA1);
651 if (key_try_load_public(pub, filename, commentp) == 1)
652 return pub;
653 key_free(pub);
654
655 /* try ssh2 public key */
Ben Lindstromd0fca422001-03-26 13:44:06 +0000656 pub = key_new(KEY_UNSPEC);
657 if (key_try_load_public(pub, filename, commentp) == 1)
658 return pub;
659 if ((strlcpy(file, filename, sizeof file) < sizeof(file)) &&
660 (strlcat(file, ".pub", sizeof file) < sizeof(file)) &&
661 (key_try_load_public(pub, file, commentp) == 1))
662 return pub;
663 key_free(pub);
664 return NULL;
Damien Millere4340be2000-09-16 13:29:08 +1100665}