blob: 420813f37f86be30ed6016e589b692fcef11e627 [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 Millereccb9de2005-06-17 12:59:34 +100039RCSID("$OpenBSD: authfile.c,v 1.61 2005/06/17 02:44:32 djm 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 Millereccb9de2005-06-17 12:59:34 +100055#include "atomicio.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100056
Ben Lindstromd0fca422001-03-26 13:44:06 +000057/* Version identification string for SSH v1 identity files. */
Ben Lindstrom1170d712001-01-29 07:51:26 +000058static const char authfile_id_string[] =
59 "SSH PRIVATE KEY FILE FORMAT 1.1\n";
Damien Millerd4a8b7e1999-10-27 13:42:43 +100060
Damien Miller5428f641999-11-25 11:54:57 +110061/*
62 * Saves the authentication (private) key in a file, encrypting it with
63 * passphrase. The identification of the file (lowest 64 bits of n) will
64 * precede the key to provide identification of the key without needing a
65 * passphrase.
66 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100067
Ben Lindstrombba81212001-06-25 05:01:22 +000068static int
Ben Lindstromd0fca422001-03-26 13:44:06 +000069key_save_private_rsa1(Key *key, const char *filename, const char *passphrase,
70 const char *comment)
Damien Millerd4a8b7e1999-10-27 13:42:43 +100071{
Damien Miller95def091999-11-25 00:26:21 +110072 Buffer buffer, encrypted;
Damien Miller708d21c2002-01-22 23:18:15 +110073 u_char buf[100], *cp;
Damien Miller963f6b22002-02-19 15:21:23 +110074 int fd, i, cipher_num;
Damien Miller874d77b2000-10-14 16:23:11 +110075 CipherContext ciphercontext;
76 Cipher *cipher;
Darren Tucker3f9fdc72004-06-22 12:56:01 +100077 u_int32_t rnd;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100078
Damien Miller5428f641999-11-25 11:54:57 +110079 /*
80 * If the passphrase is empty, use SSH_CIPHER_NONE to ease converting
81 * to another cipher; otherwise use SSH_AUTHFILE_CIPHER.
82 */
Damien Miller963f6b22002-02-19 15:21:23 +110083 cipher_num = (strcmp(passphrase, "") == 0) ?
84 SSH_CIPHER_NONE : SSH_AUTHFILE_CIPHER;
85 if ((cipher = cipher_by_number(cipher_num)) == NULL)
Damien Miller874d77b2000-10-14 16:23:11 +110086 fatal("save_private_key_rsa: bad cipher");
Damien Millerd4a8b7e1999-10-27 13:42:43 +100087
Damien Miller95def091999-11-25 00:26:21 +110088 /* This buffer is used to built the secret part of the private key. */
89 buffer_init(&buffer);
Damien Millerd4a8b7e1999-10-27 13:42:43 +100090
Damien Miller95def091999-11-25 00:26:21 +110091 /* Put checkbytes for checking passphrase validity. */
Darren Tucker3f9fdc72004-06-22 12:56:01 +100092 rnd = arc4random();
93 buf[0] = rnd & 0xff;
94 buf[1] = (rnd >> 8) & 0xff;
Damien Miller95def091999-11-25 00:26:21 +110095 buf[2] = buf[0];
96 buf[3] = buf[1];
97 buffer_append(&buffer, buf, 4);
Damien Millerd4a8b7e1999-10-27 13:42:43 +100098
Damien Miller5428f641999-11-25 11:54:57 +110099 /*
100 * Store the private key (n and e will not be stored because they
101 * will be stored in plain text, and storing them also in encrypted
102 * format would just give known plaintext).
103 */
Ben Lindstromd0fca422001-03-26 13:44:06 +0000104 buffer_put_bignum(&buffer, key->rsa->d);
105 buffer_put_bignum(&buffer, key->rsa->iqmp);
106 buffer_put_bignum(&buffer, key->rsa->q); /* reverse from SSL p */
107 buffer_put_bignum(&buffer, key->rsa->p); /* reverse from SSL q */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000108
Damien Miller95def091999-11-25 00:26:21 +1100109 /* Pad the part to be encrypted until its size is a multiple of 8. */
110 while (buffer_len(&buffer) % 8 != 0)
111 buffer_put_char(&buffer, 0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000112
Damien Miller95def091999-11-25 00:26:21 +1100113 /* This buffer will be used to contain the data in the file. */
114 buffer_init(&encrypted);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000115
Damien Miller95def091999-11-25 00:26:21 +1100116 /* First store keyfile id string. */
Ben Lindstrom1170d712001-01-29 07:51:26 +0000117 for (i = 0; authfile_id_string[i]; i++)
118 buffer_put_char(&encrypted, authfile_id_string[i]);
Damien Miller95def091999-11-25 00:26:21 +1100119 buffer_put_char(&encrypted, 0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000120
Damien Miller95def091999-11-25 00:26:21 +1100121 /* Store cipher type. */
Damien Miller963f6b22002-02-19 15:21:23 +1100122 buffer_put_char(&encrypted, cipher_num);
Damien Miller95def091999-11-25 00:26:21 +1100123 buffer_put_int(&encrypted, 0); /* For future extension */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000124
Damien Miller95def091999-11-25 00:26:21 +1100125 /* Store public key. This will be in plain text. */
Ben Lindstromd0fca422001-03-26 13:44:06 +0000126 buffer_put_int(&encrypted, BN_num_bits(key->rsa->n));
127 buffer_put_bignum(&encrypted, key->rsa->n);
128 buffer_put_bignum(&encrypted, key->rsa->e);
Ben Lindstrom664408d2001-06-09 01:42:01 +0000129 buffer_put_cstring(&encrypted, comment);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000130
Damien Miller95def091999-11-25 00:26:21 +1100131 /* Allocate space for the private part of the key in the buffer. */
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100132 cp = buffer_append_space(&encrypted, buffer_len(&buffer));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000133
Damien Miller963f6b22002-02-19 15:21:23 +1100134 cipher_set_key_string(&ciphercontext, cipher, passphrase,
135 CIPHER_ENCRYPT);
136 cipher_crypt(&ciphercontext, cp,
Damien Miller708d21c2002-01-22 23:18:15 +1100137 buffer_ptr(&buffer), buffer_len(&buffer));
Damien Miller963f6b22002-02-19 15:21:23 +1100138 cipher_cleanup(&ciphercontext);
Damien Miller874d77b2000-10-14 16:23:11 +1100139 memset(&ciphercontext, 0, sizeof(ciphercontext));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000140
Damien Miller95def091999-11-25 00:26:21 +1100141 /* Destroy temporary data. */
142 memset(buf, 0, sizeof(buf));
143 buffer_free(&buffer);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000144
Damien Miller037a0dc1999-12-07 15:38:31 +1100145 fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0600);
Ben Lindstrom15f33862001-04-16 02:00:02 +0000146 if (fd < 0) {
147 error("open %s failed: %s.", filename, strerror(errno));
Darren Tuckerd1d41b32003-09-22 21:01:27 +1000148 buffer_free(&encrypted);
Damien Miller95def091999-11-25 00:26:21 +1100149 return 0;
Ben Lindstrom15f33862001-04-16 02:00:02 +0000150 }
Damien Millereccb9de2005-06-17 12:59:34 +1000151 if (atomicio(vwrite, fd, buffer_ptr(&encrypted),
152 buffer_len(&encrypted)) != buffer_len(&encrypted)) {
Ben Lindstrom15f33862001-04-16 02:00:02 +0000153 error("write to key file %s failed: %s", filename,
Damien Miller9f0f5c62001-12-21 14:45:46 +1100154 strerror(errno));
Damien Miller95def091999-11-25 00:26:21 +1100155 buffer_free(&encrypted);
Damien Miller037a0dc1999-12-07 15:38:31 +1100156 close(fd);
Damien Miller78315eb2000-09-29 23:01:36 +1100157 unlink(filename);
Damien Miller95def091999-11-25 00:26:21 +1100158 return 0;
159 }
Damien Miller037a0dc1999-12-07 15:38:31 +1100160 close(fd);
Damien Miller95def091999-11-25 00:26:21 +1100161 buffer_free(&encrypted);
162 return 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000163}
164
Ben Lindstromd0fca422001-03-26 13:44:06 +0000165/* save SSH v2 key in OpenSSL PEM format */
Ben Lindstrombba81212001-06-25 05:01:22 +0000166static int
Ben Lindstromd0fca422001-03-26 13:44:06 +0000167key_save_private_pem(Key *key, const char *filename, const char *_passphrase,
168 const char *comment)
Damien Millereba71ba2000-04-29 23:57:08 +1000169{
170 FILE *fp;
171 int fd;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100172 int success = 0;
173 int len = strlen(_passphrase);
Ben Lindstrom90fd8142002-02-26 18:09:42 +0000174 u_char *passphrase = (len > 0) ? (u_char *)_passphrase : NULL;
Ben Lindstrom80cb27d2002-03-05 01:33:36 +0000175 const EVP_CIPHER *cipher = (len > 0) ? EVP_des_ede3_cbc() : NULL;
Damien Millereba71ba2000-04-29 23:57:08 +1000176
177 if (len > 0 && len <= 4) {
Ben Lindstrom15f33862001-04-16 02:00:02 +0000178 error("passphrase too short: have %d bytes, need > 4", len);
Damien Millereba71ba2000-04-29 23:57:08 +1000179 return 0;
180 }
181 fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0600);
182 if (fd < 0) {
Ben Lindstrom15f33862001-04-16 02:00:02 +0000183 error("open %s failed: %s.", filename, strerror(errno));
Damien Millereba71ba2000-04-29 23:57:08 +1000184 return 0;
185 }
186 fp = fdopen(fd, "w");
187 if (fp == NULL ) {
Ben Lindstrom15f33862001-04-16 02:00:02 +0000188 error("fdopen %s failed: %s.", filename, strerror(errno));
Damien Millereba71ba2000-04-29 23:57:08 +1000189 close(fd);
190 return 0;
191 }
Damien Miller0bc1bd82000-11-13 22:57:25 +1100192 switch (key->type) {
Ben Lindstromc1116602001-03-29 00:28:37 +0000193 case KEY_DSA:
194 success = PEM_write_DSAPrivateKey(fp, key->dsa,
195 cipher, passphrase, len, NULL, NULL);
196 break;
197 case KEY_RSA:
198 success = PEM_write_RSAPrivateKey(fp, key->rsa,
199 cipher, passphrase, len, NULL, NULL);
200 break;
Damien Millereba71ba2000-04-29 23:57:08 +1000201 }
202 fclose(fp);
203 return success;
204}
205
206int
Ben Lindstromd0fca422001-03-26 13:44:06 +0000207key_save_private(Key *key, const char *filename, const char *passphrase,
Damien Millereba71ba2000-04-29 23:57:08 +1000208 const char *comment)
209{
210 switch (key->type) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100211 case KEY_RSA1:
Ben Lindstromd0fca422001-03-26 13:44:06 +0000212 return key_save_private_rsa1(key, filename, passphrase,
213 comment);
Damien Millereba71ba2000-04-29 23:57:08 +1000214 break;
215 case KEY_DSA:
Damien Miller0bc1bd82000-11-13 22:57:25 +1100216 case KEY_RSA:
Ben Lindstromd0fca422001-03-26 13:44:06 +0000217 return key_save_private_pem(key, filename, passphrase,
218 comment);
Damien Millereba71ba2000-04-29 23:57:08 +1000219 break;
220 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
Ben Lindstrombba81212001-06-25 05:01:22 +0000510static int
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 break;
558 case KEY_DSA:
Damien Miller0bc1bd82000-11-13 22:57:25 +1100559 case KEY_RSA:
560 case KEY_UNSPEC:
Ben Lindstromd0fca422001-03-26 13:44:06 +0000561 return key_load_private_pem(fd, type, passphrase, commentp);
562 /* closes fd */
Ben Lindstromb257cca2001-03-05 04:59:27 +0000563 break;
Damien Millereba71ba2000-04-29 23:57:08 +1000564 default:
Ben Lindstromb257cca2001-03-05 04:59:27 +0000565 close(fd);
Damien Millereba71ba2000-04-29 23:57:08 +1000566 break;
567 }
Ben Lindstromd0fca422001-03-26 13:44:06 +0000568 return NULL;
569}
570
571Key *
572key_load_private(const char *filename, const char *passphrase,
573 char **commentp)
574{
Ben Lindstrom322915d2001-06-05 20:46:32 +0000575 Key *pub, *prv;
Ben Lindstromd0fca422001-03-26 13:44:06 +0000576 int fd;
577
578 fd = open(filename, O_RDONLY);
579 if (fd < 0)
580 return NULL;
581 if (!key_perm_ok(fd, filename)) {
Ben Lindstrom15f33862001-04-16 02:00:02 +0000582 error("bad permissions: ignore key: %s", filename);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000583 close(fd);
584 return NULL;
585 }
586 pub = key_load_public_rsa1(fd, filename, commentp);
587 lseek(fd, (off_t) 0, SEEK_SET); /* rewind */
588 if (pub == NULL) {
589 /* closes fd */
Ben Lindstrom322915d2001-06-05 20:46:32 +0000590 prv = key_load_private_pem(fd, KEY_UNSPEC, passphrase, NULL);
591 /* use the filename as a comment for PEM */
592 if (commentp && prv)
Ben Lindstrom2d0356f2001-06-05 21:13:57 +0000593 *commentp = xstrdup(filename);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000594 } else {
595 /* it's a SSH v1 key if the public key part is readable */
596 key_free(pub);
597 /* closes fd */
Ben Lindstrom322915d2001-06-05 20:46:32 +0000598 prv = key_load_private_rsa1(fd, filename, passphrase, NULL);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000599 }
Ben Lindstrom322915d2001-06-05 20:46:32 +0000600 return prv;
Damien Millereba71ba2000-04-29 23:57:08 +1000601}
Damien Millere4340be2000-09-16 13:29:08 +1100602
Ben Lindstrombba81212001-06-25 05:01:22 +0000603static int
Ben Lindstromd0fca422001-03-26 13:44:06 +0000604key_try_load_public(Key *k, const char *filename, char **commentp)
Damien Millere4340be2000-09-16 13:29:08 +1100605{
606 FILE *f;
Darren Tucker22cc7412004-12-06 22:47:41 +1100607 char line[SSH_MAX_PUBKEY_BYTES];
Damien Millere4340be2000-09-16 13:29:08 +1100608 char *cp;
Darren Tuckerf0f90982004-12-11 13:39:50 +1100609 u_long linenum = 0;
Damien Millere4340be2000-09-16 13:29:08 +1100610
611 f = fopen(filename, "r");
612 if (f != NULL) {
Darren Tucker22cc7412004-12-06 22:47:41 +1100613 while (read_keyfile_line(f, filename, line, sizeof(line),
614 &linenum) != -1) {
Damien Millere4340be2000-09-16 13:29:08 +1100615 cp = line;
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000616 switch (*cp) {
Damien Millere4340be2000-09-16 13:29:08 +1100617 case '#':
618 case '\n':
619 case '\0':
620 continue;
621 }
622 /* Skip leading whitespace. */
623 for (; *cp && (*cp == ' ' || *cp == '\t'); cp++)
624 ;
625 if (*cp) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100626 if (key_read(k, &cp) == 1) {
Damien Millere4340be2000-09-16 13:29:08 +1100627 if (commentp)
628 *commentp=xstrdup(filename);
629 fclose(f);
630 return 1;
631 }
632 }
633 }
634 fclose(f);
635 }
636 return 0;
637}
638
Ben Lindstromd0fca422001-03-26 13:44:06 +0000639/* load public key from ssh v1 private or any pubkey file */
640Key *
641key_load_public(const char *filename, char **commentp)
Damien Millere4340be2000-09-16 13:29:08 +1100642{
Ben Lindstromd0fca422001-03-26 13:44:06 +0000643 Key *pub;
644 char file[MAXPATHLEN];
Damien Millere4340be2000-09-16 13:29:08 +1100645
Damien Millerdb274722003-05-14 13:45:22 +1000646 /* try rsa1 private key */
Ben Lindstromd0fca422001-03-26 13:44:06 +0000647 pub = key_load_public_type(KEY_RSA1, filename, commentp);
648 if (pub != NULL)
649 return pub;
Damien Millerdb274722003-05-14 13:45:22 +1000650
651 /* try rsa1 public key */
652 pub = key_new(KEY_RSA1);
653 if (key_try_load_public(pub, filename, commentp) == 1)
654 return pub;
655 key_free(pub);
656
657 /* try ssh2 public key */
Ben Lindstromd0fca422001-03-26 13:44:06 +0000658 pub = key_new(KEY_UNSPEC);
659 if (key_try_load_public(pub, filename, commentp) == 1)
660 return pub;
661 if ((strlcpy(file, filename, sizeof file) < sizeof(file)) &&
662 (strlcat(file, ".pub", sizeof file) < sizeof(file)) &&
663 (key_try_load_public(pub, file, commentp) == 1))
664 return pub;
665 key_free(pub);
666 return NULL;
Damien Millere4340be2000-09-16 13:29:08 +1100667}