blob: 55c66d1e36ac5f1660ba6916b07fa7011fed8b04 [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"
Damien Millerf17883e2006-03-15 11:45:54 +110039
40#include <sys/types.h>
41#include <sys/stat.h>
Damien Millerd4a8b7e1999-10-27 13:42:43 +100042
Damien Miller0bc1bd82000-11-13 22:57:25 +110043#include <openssl/err.h>
Damien Millereba71ba2000-04-29 23:57:08 +100044#include <openssl/evp.h>
Ben Lindstrom226cfa02001-01-22 05:34:40 +000045#include <openssl/pem.h>
Damien Millereba71ba2000-04-29 23:57:08 +100046
Ben Lindstrom226cfa02001-01-22 05:34:40 +000047#include "cipher.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100048#include "xmalloc.h"
49#include "buffer.h"
50#include "bufaux.h"
Damien Millereba71ba2000-04-29 23:57:08 +100051#include "key.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000052#include "ssh.h"
53#include "log.h"
Ben Lindstrom31ca54a2001-02-09 02:11:24 +000054#include "authfile.h"
Damien Miller040b64f2002-01-22 23:10:04 +110055#include "rsa.h"
Darren Tuckerf0f90982004-12-11 13:39:50 +110056#include "misc.h"
Damien Millereccb9de2005-06-17 12:59:34 +100057#include "atomicio.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100058
Ben Lindstromd0fca422001-03-26 13:44:06 +000059/* Version identification string for SSH v1 identity files. */
Ben Lindstrom1170d712001-01-29 07:51:26 +000060static const char authfile_id_string[] =
61 "SSH PRIVATE KEY FILE FORMAT 1.1\n";
Damien Millerd4a8b7e1999-10-27 13:42:43 +100062
Damien Miller5428f641999-11-25 11:54:57 +110063/*
64 * Saves the authentication (private) key in a file, encrypting it with
65 * passphrase. The identification of the file (lowest 64 bits of n) will
66 * precede the key to provide identification of the key without needing a
67 * passphrase.
68 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100069
Ben Lindstrombba81212001-06-25 05:01:22 +000070static int
Ben Lindstromd0fca422001-03-26 13:44:06 +000071key_save_private_rsa1(Key *key, const char *filename, const char *passphrase,
72 const char *comment)
Damien Millerd4a8b7e1999-10-27 13:42:43 +100073{
Damien Miller95def091999-11-25 00:26:21 +110074 Buffer buffer, encrypted;
Damien Miller708d21c2002-01-22 23:18:15 +110075 u_char buf[100], *cp;
Damien Miller963f6b22002-02-19 15:21:23 +110076 int fd, i, cipher_num;
Damien Miller874d77b2000-10-14 16:23:11 +110077 CipherContext ciphercontext;
78 Cipher *cipher;
Darren Tucker3f9fdc72004-06-22 12:56:01 +100079 u_int32_t rnd;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100080
Damien Miller5428f641999-11-25 11:54:57 +110081 /*
82 * If the passphrase is empty, use SSH_CIPHER_NONE to ease converting
83 * to another cipher; otherwise use SSH_AUTHFILE_CIPHER.
84 */
Damien Miller963f6b22002-02-19 15:21:23 +110085 cipher_num = (strcmp(passphrase, "") == 0) ?
86 SSH_CIPHER_NONE : SSH_AUTHFILE_CIPHER;
87 if ((cipher = cipher_by_number(cipher_num)) == NULL)
Damien Miller874d77b2000-10-14 16:23:11 +110088 fatal("save_private_key_rsa: bad cipher");
Damien Millerd4a8b7e1999-10-27 13:42:43 +100089
Damien Miller95def091999-11-25 00:26:21 +110090 /* This buffer is used to built the secret part of the private key. */
91 buffer_init(&buffer);
Damien Millerd4a8b7e1999-10-27 13:42:43 +100092
Damien Miller95def091999-11-25 00:26:21 +110093 /* Put checkbytes for checking passphrase validity. */
Darren Tucker3f9fdc72004-06-22 12:56:01 +100094 rnd = arc4random();
95 buf[0] = rnd & 0xff;
96 buf[1] = (rnd >> 8) & 0xff;
Damien Miller95def091999-11-25 00:26:21 +110097 buf[2] = buf[0];
98 buf[3] = buf[1];
99 buffer_append(&buffer, buf, 4);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000100
Damien Miller5428f641999-11-25 11:54:57 +1100101 /*
102 * Store the private key (n and e will not be stored because they
103 * will be stored in plain text, and storing them also in encrypted
104 * format would just give known plaintext).
105 */
Ben Lindstromd0fca422001-03-26 13:44:06 +0000106 buffer_put_bignum(&buffer, key->rsa->d);
107 buffer_put_bignum(&buffer, key->rsa->iqmp);
108 buffer_put_bignum(&buffer, key->rsa->q); /* reverse from SSL p */
109 buffer_put_bignum(&buffer, key->rsa->p); /* reverse from SSL q */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000110
Damien Miller95def091999-11-25 00:26:21 +1100111 /* Pad the part to be encrypted until its size is a multiple of 8. */
112 while (buffer_len(&buffer) % 8 != 0)
113 buffer_put_char(&buffer, 0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000114
Damien Miller95def091999-11-25 00:26:21 +1100115 /* This buffer will be used to contain the data in the file. */
116 buffer_init(&encrypted);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000117
Damien Miller95def091999-11-25 00:26:21 +1100118 /* First store keyfile id string. */
Ben Lindstrom1170d712001-01-29 07:51:26 +0000119 for (i = 0; authfile_id_string[i]; i++)
120 buffer_put_char(&encrypted, authfile_id_string[i]);
Damien Miller95def091999-11-25 00:26:21 +1100121 buffer_put_char(&encrypted, 0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000122
Damien Miller95def091999-11-25 00:26:21 +1100123 /* Store cipher type. */
Damien Miller963f6b22002-02-19 15:21:23 +1100124 buffer_put_char(&encrypted, cipher_num);
Damien Miller95def091999-11-25 00:26:21 +1100125 buffer_put_int(&encrypted, 0); /* For future extension */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000126
Damien Miller95def091999-11-25 00:26:21 +1100127 /* Store public key. This will be in plain text. */
Ben Lindstromd0fca422001-03-26 13:44:06 +0000128 buffer_put_int(&encrypted, BN_num_bits(key->rsa->n));
129 buffer_put_bignum(&encrypted, key->rsa->n);
130 buffer_put_bignum(&encrypted, key->rsa->e);
Ben Lindstrom664408d2001-06-09 01:42:01 +0000131 buffer_put_cstring(&encrypted, comment);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000132
Damien Miller95def091999-11-25 00:26:21 +1100133 /* Allocate space for the private part of the key in the buffer. */
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100134 cp = buffer_append_space(&encrypted, buffer_len(&buffer));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000135
Damien Miller963f6b22002-02-19 15:21:23 +1100136 cipher_set_key_string(&ciphercontext, cipher, passphrase,
137 CIPHER_ENCRYPT);
138 cipher_crypt(&ciphercontext, cp,
Damien Miller708d21c2002-01-22 23:18:15 +1100139 buffer_ptr(&buffer), buffer_len(&buffer));
Damien Miller963f6b22002-02-19 15:21:23 +1100140 cipher_cleanup(&ciphercontext);
Damien Miller874d77b2000-10-14 16:23:11 +1100141 memset(&ciphercontext, 0, sizeof(ciphercontext));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000142
Damien Miller95def091999-11-25 00:26:21 +1100143 /* Destroy temporary data. */
144 memset(buf, 0, sizeof(buf));
145 buffer_free(&buffer);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000146
Damien Miller037a0dc1999-12-07 15:38:31 +1100147 fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0600);
Ben Lindstrom15f33862001-04-16 02:00:02 +0000148 if (fd < 0) {
149 error("open %s failed: %s.", filename, strerror(errno));
Darren Tuckerd1d41b32003-09-22 21:01:27 +1000150 buffer_free(&encrypted);
Damien Miller95def091999-11-25 00:26:21 +1100151 return 0;
Ben Lindstrom15f33862001-04-16 02:00:02 +0000152 }
Damien Millereccb9de2005-06-17 12:59:34 +1000153 if (atomicio(vwrite, fd, buffer_ptr(&encrypted),
154 buffer_len(&encrypted)) != buffer_len(&encrypted)) {
Ben Lindstrom15f33862001-04-16 02:00:02 +0000155 error("write to key file %s failed: %s", filename,
Damien Miller9f0f5c62001-12-21 14:45:46 +1100156 strerror(errno));
Damien Miller95def091999-11-25 00:26:21 +1100157 buffer_free(&encrypted);
Damien Miller037a0dc1999-12-07 15:38:31 +1100158 close(fd);
Damien Miller78315eb2000-09-29 23:01:36 +1100159 unlink(filename);
Damien Miller95def091999-11-25 00:26:21 +1100160 return 0;
161 }
Damien Miller037a0dc1999-12-07 15:38:31 +1100162 close(fd);
Damien Miller95def091999-11-25 00:26:21 +1100163 buffer_free(&encrypted);
164 return 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000165}
166
Ben Lindstromd0fca422001-03-26 13:44:06 +0000167/* save SSH v2 key in OpenSSL PEM format */
Ben Lindstrombba81212001-06-25 05:01:22 +0000168static int
Ben Lindstromd0fca422001-03-26 13:44:06 +0000169key_save_private_pem(Key *key, const char *filename, const char *_passphrase,
170 const char *comment)
Damien Millereba71ba2000-04-29 23:57:08 +1000171{
172 FILE *fp;
173 int fd;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100174 int success = 0;
175 int len = strlen(_passphrase);
Ben Lindstrom90fd8142002-02-26 18:09:42 +0000176 u_char *passphrase = (len > 0) ? (u_char *)_passphrase : NULL;
Ben Lindstrom80cb27d2002-03-05 01:33:36 +0000177 const EVP_CIPHER *cipher = (len > 0) ? EVP_des_ede3_cbc() : NULL;
Damien Millereba71ba2000-04-29 23:57:08 +1000178
179 if (len > 0 && len <= 4) {
Ben Lindstrom15f33862001-04-16 02:00:02 +0000180 error("passphrase too short: have %d bytes, need > 4", len);
Damien Millereba71ba2000-04-29 23:57:08 +1000181 return 0;
182 }
183 fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0600);
184 if (fd < 0) {
Ben Lindstrom15f33862001-04-16 02:00:02 +0000185 error("open %s failed: %s.", filename, strerror(errno));
Damien Millereba71ba2000-04-29 23:57:08 +1000186 return 0;
187 }
188 fp = fdopen(fd, "w");
189 if (fp == NULL ) {
Ben Lindstrom15f33862001-04-16 02:00:02 +0000190 error("fdopen %s failed: %s.", filename, strerror(errno));
Damien Millereba71ba2000-04-29 23:57:08 +1000191 close(fd);
192 return 0;
193 }
Damien Miller0bc1bd82000-11-13 22:57:25 +1100194 switch (key->type) {
Ben Lindstromc1116602001-03-29 00:28:37 +0000195 case KEY_DSA:
196 success = PEM_write_DSAPrivateKey(fp, key->dsa,
197 cipher, passphrase, len, NULL, NULL);
198 break;
199 case KEY_RSA:
200 success = PEM_write_RSAPrivateKey(fp, key->rsa,
201 cipher, passphrase, len, NULL, NULL);
202 break;
Damien Millereba71ba2000-04-29 23:57:08 +1000203 }
204 fclose(fp);
205 return success;
206}
207
208int
Ben Lindstromd0fca422001-03-26 13:44:06 +0000209key_save_private(Key *key, const char *filename, const char *passphrase,
Damien Millereba71ba2000-04-29 23:57:08 +1000210 const char *comment)
211{
212 switch (key->type) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100213 case KEY_RSA1:
Ben Lindstromd0fca422001-03-26 13:44:06 +0000214 return key_save_private_rsa1(key, filename, passphrase,
215 comment);
Damien Millereba71ba2000-04-29 23:57:08 +1000216 case KEY_DSA:
Damien Miller0bc1bd82000-11-13 22:57:25 +1100217 case KEY_RSA:
Ben Lindstromd0fca422001-03-26 13:44:06 +0000218 return key_save_private_pem(key, filename, passphrase,
219 comment);
Damien Millereba71ba2000-04-29 23:57:08 +1000220 default:
221 break;
222 }
Ben Lindstrom15f33862001-04-16 02:00:02 +0000223 error("key_save_private: cannot save key type %d", key->type);
Damien Millereba71ba2000-04-29 23:57:08 +1000224 return 0;
225}
226
Damien Miller5428f641999-11-25 11:54:57 +1100227/*
Ben Lindstromd0fca422001-03-26 13:44:06 +0000228 * Loads the public part of the ssh v1 key file. Returns NULL if an error was
229 * encountered (the file does not exist or is not readable), and the key
Damien Miller5428f641999-11-25 11:54:57 +1100230 * otherwise.
231 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000232
Ben Lindstrombba81212001-06-25 05:01:22 +0000233static Key *
Ben Lindstromd0fca422001-03-26 13:44:06 +0000234key_load_public_rsa1(int fd, const char *filename, char **commentp)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000235{
Damien Miller95def091999-11-25 00:26:21 +1100236 Buffer buffer;
Ben Lindstromd0fca422001-03-26 13:44:06 +0000237 Key *pub;
Ben Lindstrom44adb8f2002-12-23 02:00:23 +0000238 struct stat st;
Damien Miller95def091999-11-25 00:26:21 +1100239 char *cp;
Damien Millereccb9de2005-06-17 12:59:34 +1000240 u_int i;
Darren Tucker1f8311c2004-05-13 16:39:33 +1000241 size_t len;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000242
Ben Lindstrom44adb8f2002-12-23 02:00:23 +0000243 if (fstat(fd, &st) < 0) {
244 error("fstat for key file %.200s failed: %.100s",
245 filename, strerror(errno));
246 return NULL;
247 }
Darren Tuckerf4b43712004-08-29 16:28:39 +1000248 if (st.st_size > 1*1024*1024) {
249 error("key file %.200s too large", filename);
250 return NULL;
251 }
Darren Tucker1f8311c2004-05-13 16:39:33 +1000252 len = (size_t)st.st_size; /* truncated */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000253
Damien Miller95def091999-11-25 00:26:21 +1100254 buffer_init(&buffer);
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100255 cp = buffer_append_space(&buffer, len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000256
Damien Millereccb9de2005-06-17 12:59:34 +1000257 if (atomicio(read, fd, cp, len) != len) {
Damien Miller95def091999-11-25 00:26:21 +1100258 debug("Read from key file %.200s failed: %.100s", filename,
Damien Millereba71ba2000-04-29 23:57:08 +1000259 strerror(errno));
Damien Miller95def091999-11-25 00:26:21 +1100260 buffer_free(&buffer);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000261 return NULL;
Damien Miller95def091999-11-25 00:26:21 +1100262 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000263
Ben Lindstrom1170d712001-01-29 07:51:26 +0000264 /* Check that it is at least big enough to contain the ID string. */
265 if (len < sizeof(authfile_id_string)) {
Damien Miller058655c2001-10-10 15:03:36 +1000266 debug3("Not a RSA1 key file %.200s.", filename);
Damien Miller95def091999-11-25 00:26:21 +1100267 buffer_free(&buffer);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000268 return NULL;
Damien Miller95def091999-11-25 00:26:21 +1100269 }
Damien Miller5428f641999-11-25 11:54:57 +1100270 /*
271 * Make sure it begins with the id string. Consume the id string
272 * from the buffer.
273 */
Ben Lindstrom1170d712001-01-29 07:51:26 +0000274 for (i = 0; i < sizeof(authfile_id_string); i++)
275 if (buffer_get_char(&buffer) != authfile_id_string[i]) {
Damien Miller058655c2001-10-10 15:03:36 +1000276 debug3("Not a RSA1 key file %.200s.", filename);
Damien Miller95def091999-11-25 00:26:21 +1100277 buffer_free(&buffer);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000278 return NULL;
Damien Miller95def091999-11-25 00:26:21 +1100279 }
280 /* Skip cipher type and reserved data. */
281 (void) buffer_get_char(&buffer); /* cipher type */
282 (void) buffer_get_int(&buffer); /* reserved */
283
284 /* Read the public key from the buffer. */
Ben Lindstromc5a7f4f2002-06-25 23:19:13 +0000285 (void) buffer_get_int(&buffer);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000286 pub = key_new(KEY_RSA1);
287 buffer_get_bignum(&buffer, pub->rsa->n);
288 buffer_get_bignum(&buffer, pub->rsa->e);
289 if (commentp)
290 *commentp = buffer_get_string(&buffer, NULL);
Damien Miller95def091999-11-25 00:26:21 +1100291 /* The encrypted private part is not parsed by this function. */
292
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000293 buffer_free(&buffer);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000294 return pub;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000295}
296
Ben Lindstromd0fca422001-03-26 13:44:06 +0000297/* load public key from private-key file, works only for SSH v1 */
298Key *
299key_load_public_type(int type, const char *filename, char **commentp)
Damien Millereba71ba2000-04-29 23:57:08 +1000300{
Ben Lindstromd0fca422001-03-26 13:44:06 +0000301 Key *pub;
302 int fd;
303
304 if (type == KEY_RSA1) {
305 fd = open(filename, O_RDONLY);
306 if (fd < 0)
307 return NULL;
308 pub = key_load_public_rsa1(fd, filename, commentp);
309 close(fd);
310 return pub;
Damien Millereba71ba2000-04-29 23:57:08 +1000311 }
Ben Lindstromd0fca422001-03-26 13:44:06 +0000312 return NULL;
Damien Millereba71ba2000-04-29 23:57:08 +1000313}
314
Damien Miller5428f641999-11-25 11:54:57 +1100315/*
316 * Loads the private key from the file. Returns 0 if an error is encountered
317 * (file does not exist or is not readable, or passphrase is bad). This
318 * initializes the private key.
319 * Assumes we are called under uid of the owner of the file.
320 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000321
Ben Lindstrombba81212001-06-25 05:01:22 +0000322static Key *
Ben Lindstromd0fca422001-03-26 13:44:06 +0000323key_load_private_rsa1(int fd, const char *filename, const char *passphrase,
324 char **commentp)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000325{
Damien Millereccb9de2005-06-17 12:59:34 +1000326 u_int i;
327 int check1, check2, cipher_type;
Darren Tucker1f8311c2004-05-13 16:39:33 +1000328 size_t len;
Damien Miller95def091999-11-25 00:26:21 +1100329 Buffer buffer, decrypted;
Damien Miller708d21c2002-01-22 23:18:15 +1100330 u_char *cp;
Damien Miller874d77b2000-10-14 16:23:11 +1100331 CipherContext ciphercontext;
332 Cipher *cipher;
Ben Lindstromd0fca422001-03-26 13:44:06 +0000333 Key *prv = NULL;
Ben Lindstrom44adb8f2002-12-23 02:00:23 +0000334 struct stat st;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000335
Ben Lindstrom44adb8f2002-12-23 02:00:23 +0000336 if (fstat(fd, &st) < 0) {
337 error("fstat for key file %.200s failed: %.100s",
338 filename, strerror(errno));
339 close(fd);
340 return NULL;
341 }
Darren Tucker1f8311c2004-05-13 16:39:33 +1000342 if (st.st_size > 1*1024*1024) {
Darren Tuckerf4b43712004-08-29 16:28:39 +1000343 error("key file %.200s too large", filename);
Darren Tucker1f8311c2004-05-13 16:39:33 +1000344 close(fd);
345 return (NULL);
346 }
347 len = (size_t)st.st_size; /* truncated */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000348
Damien Miller95def091999-11-25 00:26:21 +1100349 buffer_init(&buffer);
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100350 cp = buffer_append_space(&buffer, len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000351
Damien Millereccb9de2005-06-17 12:59:34 +1000352 if (atomicio(read, fd, cp, len) != len) {
Damien Miller95def091999-11-25 00:26:21 +1100353 debug("Read from key file %.200s failed: %.100s", filename,
Damien Miller0bc1bd82000-11-13 22:57:25 +1100354 strerror(errno));
Damien Miller95def091999-11-25 00:26:21 +1100355 buffer_free(&buffer);
Damien Miller037a0dc1999-12-07 15:38:31 +1100356 close(fd);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000357 return NULL;
Damien Miller95def091999-11-25 00:26:21 +1100358 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000359
Ben Lindstrom1170d712001-01-29 07:51:26 +0000360 /* Check that it is at least big enough to contain the ID string. */
361 if (len < sizeof(authfile_id_string)) {
Damien Miller058655c2001-10-10 15:03:36 +1000362 debug3("Not a RSA1 key file %.200s.", filename);
Damien Miller95def091999-11-25 00:26:21 +1100363 buffer_free(&buffer);
Ben Lindstromb257cca2001-03-05 04:59:27 +0000364 close(fd);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000365 return NULL;
Damien Miller95def091999-11-25 00:26:21 +1100366 }
Damien Miller5428f641999-11-25 11:54:57 +1100367 /*
368 * Make sure it begins with the id string. Consume the id string
369 * from the buffer.
370 */
Ben Lindstrom1170d712001-01-29 07:51:26 +0000371 for (i = 0; i < sizeof(authfile_id_string); i++)
372 if (buffer_get_char(&buffer) != authfile_id_string[i]) {
Damien Miller058655c2001-10-10 15:03:36 +1000373 debug3("Not a RSA1 key file %.200s.", filename);
Damien Miller95def091999-11-25 00:26:21 +1100374 buffer_free(&buffer);
Ben Lindstromb257cca2001-03-05 04:59:27 +0000375 close(fd);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000376 return NULL;
Damien Miller95def091999-11-25 00:26:21 +1100377 }
Ben Lindstromb257cca2001-03-05 04:59:27 +0000378
Damien Miller95def091999-11-25 00:26:21 +1100379 /* Read cipher type. */
380 cipher_type = buffer_get_char(&buffer);
381 (void) buffer_get_int(&buffer); /* Reserved data. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000382
Damien Miller95def091999-11-25 00:26:21 +1100383 /* Read the public key from the buffer. */
Ben Lindstromc5a7f4f2002-06-25 23:19:13 +0000384 (void) buffer_get_int(&buffer);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000385 prv = key_new_private(KEY_RSA1);
386
387 buffer_get_bignum(&buffer, prv->rsa->n);
388 buffer_get_bignum(&buffer, prv->rsa->e);
389 if (commentp)
390 *commentp = buffer_get_string(&buffer, NULL);
Damien Miller95def091999-11-25 00:26:21 +1100391 else
392 xfree(buffer_get_string(&buffer, NULL));
393
394 /* Check that it is a supported cipher. */
Damien Miller874d77b2000-10-14 16:23:11 +1100395 cipher = cipher_by_number(cipher_type);
396 if (cipher == NULL) {
397 debug("Unsupported cipher %d used in key file %.200s.",
398 cipher_type, filename);
Damien Miller95def091999-11-25 00:26:21 +1100399 buffer_free(&buffer);
400 goto fail;
401 }
402 /* Initialize space for decrypted data. */
403 buffer_init(&decrypted);
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100404 cp = buffer_append_space(&decrypted, buffer_len(&buffer));
Damien Miller95def091999-11-25 00:26:21 +1100405
406 /* Rest of the buffer is encrypted. Decrypt it using the passphrase. */
Damien Miller963f6b22002-02-19 15:21:23 +1100407 cipher_set_key_string(&ciphercontext, cipher, passphrase,
408 CIPHER_DECRYPT);
409 cipher_crypt(&ciphercontext, cp,
Damien Miller708d21c2002-01-22 23:18:15 +1100410 buffer_ptr(&buffer), buffer_len(&buffer));
Damien Miller963f6b22002-02-19 15:21:23 +1100411 cipher_cleanup(&ciphercontext);
Damien Miller874d77b2000-10-14 16:23:11 +1100412 memset(&ciphercontext, 0, sizeof(ciphercontext));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000413 buffer_free(&buffer);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000414
Damien Miller95def091999-11-25 00:26:21 +1100415 check1 = buffer_get_char(&decrypted);
416 check2 = buffer_get_char(&decrypted);
417 if (check1 != buffer_get_char(&decrypted) ||
418 check2 != buffer_get_char(&decrypted)) {
419 if (strcmp(passphrase, "") != 0)
Ben Lindstromd0fca422001-03-26 13:44:06 +0000420 debug("Bad passphrase supplied for key file %.200s.",
421 filename);
Damien Miller95def091999-11-25 00:26:21 +1100422 /* Bad passphrase. */
423 buffer_free(&decrypted);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000424 goto fail;
Damien Miller95def091999-11-25 00:26:21 +1100425 }
426 /* Read the rest of the private key. */
Ben Lindstromd0fca422001-03-26 13:44:06 +0000427 buffer_get_bignum(&decrypted, prv->rsa->d);
428 buffer_get_bignum(&decrypted, prv->rsa->iqmp); /* u */
429 /* in SSL and SSH v1 p and q are exchanged */
430 buffer_get_bignum(&decrypted, prv->rsa->q); /* p */
431 buffer_get_bignum(&decrypted, prv->rsa->p); /* q */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000432
Ben Lindstromd0fca422001-03-26 13:44:06 +0000433 /* calculate p-1 and q-1 */
Damien Millerda755162002-01-22 23:09:22 +1100434 rsa_generate_additional_parameters(prv->rsa);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000435
Damien Miller95def091999-11-25 00:26:21 +1100436 buffer_free(&decrypted);
Damien Millered33d3b2003-03-15 11:36:18 +1100437
438 /* enable blinding */
439 if (RSA_blinding_on(prv->rsa, NULL) != 1) {
440 error("key_load_private_rsa1: RSA_blinding_on failed");
441 goto fail;
442 }
Ben Lindstromb257cca2001-03-05 04:59:27 +0000443 close(fd);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000444 return prv;
445
446fail:
447 if (commentp)
448 xfree(*commentp);
449 close(fd);
450 key_free(prv);
451 return NULL;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000452}
Damien Millereba71ba2000-04-29 23:57:08 +1000453
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000454Key *
Ben Lindstromd0fca422001-03-26 13:44:06 +0000455key_load_private_pem(int fd, int type, const char *passphrase,
456 char **commentp)
Damien Millereba71ba2000-04-29 23:57:08 +1000457{
Damien Millereba71ba2000-04-29 23:57:08 +1000458 FILE *fp;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100459 EVP_PKEY *pk = NULL;
Ben Lindstromd0fca422001-03-26 13:44:06 +0000460 Key *prv = NULL;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100461 char *name = "<no key>";
Damien Millereba71ba2000-04-29 23:57:08 +1000462
Damien Millereba71ba2000-04-29 23:57:08 +1000463 fp = fdopen(fd, "r");
464 if (fp == NULL) {
Ben Lindstrom15f33862001-04-16 02:00:02 +0000465 error("fdopen failed: %s", strerror(errno));
Ben Lindstromb257cca2001-03-05 04:59:27 +0000466 close(fd);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000467 return NULL;
Damien Millereba71ba2000-04-29 23:57:08 +1000468 }
Damien Miller0bc1bd82000-11-13 22:57:25 +1100469 pk = PEM_read_PrivateKey(fp, NULL, NULL, (char *)passphrase);
470 if (pk == NULL) {
Ben Lindstrom648772f2001-04-19 20:47:10 +0000471 debug("PEM_read_PrivateKey failed");
Damien Miller0bc1bd82000-11-13 22:57:25 +1100472 (void)ERR_get_error();
Ben Lindstromd0fca422001-03-26 13:44:06 +0000473 } else if (pk->type == EVP_PKEY_RSA &&
Damien Miller9f0f5c62001-12-21 14:45:46 +1100474 (type == KEY_UNSPEC||type==KEY_RSA)) {
Ben Lindstromd0fca422001-03-26 13:44:06 +0000475 prv = key_new(KEY_UNSPEC);
476 prv->rsa = EVP_PKEY_get1_RSA(pk);
477 prv->type = KEY_RSA;
478 name = "rsa w/o comment";
Damien Miller0bc1bd82000-11-13 22:57:25 +1100479#ifdef DEBUG_PK
Ben Lindstromd0fca422001-03-26 13:44:06 +0000480 RSA_print_fp(stderr, prv->rsa, 8);
Damien Millereba71ba2000-04-29 23:57:08 +1000481#endif
Damien Millered33d3b2003-03-15 11:36:18 +1100482 if (RSA_blinding_on(prv->rsa, NULL) != 1) {
483 error("key_load_private_pem: RSA_blinding_on failed");
484 key_free(prv);
485 prv = NULL;
486 }
Ben Lindstromd0fca422001-03-26 13:44:06 +0000487 } else if (pk->type == EVP_PKEY_DSA &&
Damien Miller9f0f5c62001-12-21 14:45:46 +1100488 (type == KEY_UNSPEC||type==KEY_DSA)) {
Ben Lindstromd0fca422001-03-26 13:44:06 +0000489 prv = key_new(KEY_UNSPEC);
490 prv->dsa = EVP_PKEY_get1_DSA(pk);
491 prv->type = KEY_DSA;
492 name = "dsa w/o comment";
Damien Miller0bc1bd82000-11-13 22:57:25 +1100493#ifdef DEBUG_PK
Ben Lindstromd0fca422001-03-26 13:44:06 +0000494 DSA_print_fp(stderr, prv->dsa, 8);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100495#endif
Damien Miller0bc1bd82000-11-13 22:57:25 +1100496 } else {
497 error("PEM_read_PrivateKey: mismatch or "
498 "unknown EVP_PKEY save_type %d", pk->save_type);
499 }
500 fclose(fp);
501 if (pk != NULL)
502 EVP_PKEY_free(pk);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000503 if (prv != NULL && commentp)
504 *commentp = xstrdup(name);
505 debug("read PEM private key done: type %s",
506 prv ? key_type(prv) : "<unknown>");
507 return prv;
Damien Millereba71ba2000-04-29 23:57:08 +1000508}
509
Damien Miller8275fad2006-03-15 12:06:23 +1100510int
Ben Lindstromd0fca422001-03-26 13:44:06 +0000511key_perm_ok(int fd, const char *filename)
Damien Millereba71ba2000-04-29 23:57:08 +1000512{
Damien Millereba71ba2000-04-29 23:57:08 +1000513 struct stat st;
514
Ben Lindstrom7aff2612001-09-23 13:53:22 +0000515 if (fstat(fd, &st) < 0)
516 return 0;
517 /*
518 * if a key owned by the user is accessed, then we check the
519 * permissions of the file. if the key owned by a different user,
520 * then we don't care.
521 */
Damien Millerb70b61f2000-09-16 16:25:12 +1100522#ifdef HAVE_CYGWIN
Damien Millercb5e44a2000-09-29 12:12:36 +1100523 if (check_ntsec(filename))
Damien Millerb70b61f2000-09-16 16:25:12 +1100524#endif
Ben Lindstrom7aff2612001-09-23 13:53:22 +0000525 if ((st.st_uid == getuid()) && (st.st_mode & 077) != 0) {
Damien Millereba71ba2000-04-29 23:57:08 +1000526 error("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
527 error("@ WARNING: UNPROTECTED PRIVATE KEY FILE! @");
528 error("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
Ben Lindstrom7aff2612001-09-23 13:53:22 +0000529 error("Permissions 0%3.3o for '%s' are too open.",
Damien Miller04bd8b02003-05-25 14:38:33 +1000530 (u_int)st.st_mode & 0777, filename);
Damien Millereba71ba2000-04-29 23:57:08 +1000531 error("It is recommended that your private key files are NOT accessible by others.");
Ben Lindstromd0fca422001-03-26 13:44:06 +0000532 error("This private key will be ignored.");
Damien Millereba71ba2000-04-29 23:57:08 +1000533 return 0;
534 }
Ben Lindstromd0fca422001-03-26 13:44:06 +0000535 return 1;
536}
Ben Lindstromb257cca2001-03-05 04:59:27 +0000537
Ben Lindstromd0fca422001-03-26 13:44:06 +0000538Key *
539key_load_private_type(int type, const char *filename, const char *passphrase,
540 char **commentp)
541{
542 int fd;
543
544 fd = open(filename, O_RDONLY);
545 if (fd < 0)
546 return NULL;
547 if (!key_perm_ok(fd, filename)) {
Ben Lindstrom15f33862001-04-16 02:00:02 +0000548 error("bad permissions: ignore key: %s", filename);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000549 close(fd);
550 return NULL;
551 }
552 switch (type) {
553 case KEY_RSA1:
554 return key_load_private_rsa1(fd, filename, passphrase,
555 commentp);
556 /* closes fd */
Damien Millereba71ba2000-04-29 23:57:08 +1000557 case KEY_DSA:
Damien Miller0bc1bd82000-11-13 22:57:25 +1100558 case KEY_RSA:
559 case KEY_UNSPEC:
Ben Lindstromd0fca422001-03-26 13:44:06 +0000560 return key_load_private_pem(fd, type, passphrase, commentp);
561 /* closes fd */
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}