blob: b95b9470bbbe3544bb7ce1b976b0e27ba4e1eb25 [file] [log] [blame]
Damien Miller57c30112006-03-26 14:24:48 +11001/* $OpenBSD: authfile.c,v 1.66 2006/03/25 13:17:01 djm Exp $ */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002/*
Damien Miller95def091999-11-25 00:26:21 +11003 * Author: Tatu Ylonen <ylo@cs.hut.fi>
Damien Miller95def091999-11-25 00:26:21 +11004 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
5 * All rights reserved
Damien Miller95def091999-11-25 00:26:21 +11006 * This file contains functions for reading and writing identity files, and
7 * for reading the passphrase from the user.
Damien Miller4af51302000-04-16 11:18:38 +10008 *
Damien Millere4340be2000-09-16 13:29:08 +11009 * As far as I am concerned, the code I have written for this software
10 * can be used freely for any purpose. Any derived versions of this
11 * software must be clearly marked as such, and if the derived work is
12 * incompatible with the protocol description in the RFC file, it must be
13 * called by a name other than "ssh" or "Secure Shell".
14 *
15 *
16 * Copyright (c) 2000 Markus Friedl. All rights reserved.
17 *
18 * Redistribution and use in source and binary forms, with or without
19 * modification, are permitted provided that the following conditions
20 * are met:
21 * 1. Redistributions of source code must retain the above copyright
22 * notice, this list of conditions and the following disclaimer.
23 * 2. Redistributions in binary form must reproduce the above copyright
24 * notice, this list of conditions and the following disclaimer in the
25 * documentation and/or other materials provided with the distribution.
26 *
27 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
28 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
29 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
30 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
31 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
32 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
33 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
34 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
35 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
36 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Damien Miller95def091999-11-25 00:26:21 +110037 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100038
39#include "includes.h"
Damien Millerf17883e2006-03-15 11:45:54 +110040
41#include <sys/types.h>
42#include <sys/stat.h>
Damien Millerd4a8b7e1999-10-27 13:42:43 +100043
Damien Miller0bc1bd82000-11-13 22:57:25 +110044#include <openssl/err.h>
Damien Millereba71ba2000-04-29 23:57:08 +100045#include <openssl/evp.h>
Ben Lindstrom226cfa02001-01-22 05:34:40 +000046#include <openssl/pem.h>
Damien Millereba71ba2000-04-29 23:57:08 +100047
Ben Lindstrom226cfa02001-01-22 05:34:40 +000048#include "cipher.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100049#include "xmalloc.h"
50#include "buffer.h"
51#include "bufaux.h"
Damien Millereba71ba2000-04-29 23:57:08 +100052#include "key.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000053#include "ssh.h"
54#include "log.h"
Ben Lindstrom31ca54a2001-02-09 02:11:24 +000055#include "authfile.h"
Damien Miller040b64f2002-01-22 23:10:04 +110056#include "rsa.h"
Darren Tuckerf0f90982004-12-11 13:39:50 +110057#include "misc.h"
Damien Millereccb9de2005-06-17 12:59:34 +100058#include "atomicio.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100059
Ben Lindstromd0fca422001-03-26 13:44:06 +000060/* Version identification string for SSH v1 identity files. */
Ben Lindstrom1170d712001-01-29 07:51:26 +000061static const char authfile_id_string[] =
62 "SSH PRIVATE KEY FILE FORMAT 1.1\n";
Damien Millerd4a8b7e1999-10-27 13:42:43 +100063
Damien Miller5428f641999-11-25 11:54:57 +110064/*
65 * Saves the authentication (private) key in a file, encrypting it with
66 * passphrase. The identification of the file (lowest 64 bits of n) will
67 * precede the key to provide identification of the key without needing a
68 * passphrase.
69 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100070
Ben Lindstrombba81212001-06-25 05:01:22 +000071static int
Ben Lindstromd0fca422001-03-26 13:44:06 +000072key_save_private_rsa1(Key *key, const char *filename, const char *passphrase,
73 const char *comment)
Damien Millerd4a8b7e1999-10-27 13:42:43 +100074{
Damien Miller95def091999-11-25 00:26:21 +110075 Buffer buffer, encrypted;
Damien Miller708d21c2002-01-22 23:18:15 +110076 u_char buf[100], *cp;
Damien Miller963f6b22002-02-19 15:21:23 +110077 int fd, i, cipher_num;
Damien Miller874d77b2000-10-14 16:23:11 +110078 CipherContext ciphercontext;
79 Cipher *cipher;
Darren Tucker3f9fdc72004-06-22 12:56:01 +100080 u_int32_t rnd;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100081
Damien Miller5428f641999-11-25 11:54:57 +110082 /*
83 * If the passphrase is empty, use SSH_CIPHER_NONE to ease converting
84 * to another cipher; otherwise use SSH_AUTHFILE_CIPHER.
85 */
Damien Miller963f6b22002-02-19 15:21:23 +110086 cipher_num = (strcmp(passphrase, "") == 0) ?
87 SSH_CIPHER_NONE : SSH_AUTHFILE_CIPHER;
88 if ((cipher = cipher_by_number(cipher_num)) == NULL)
Damien Miller874d77b2000-10-14 16:23:11 +110089 fatal("save_private_key_rsa: bad cipher");
Damien Millerd4a8b7e1999-10-27 13:42:43 +100090
Damien Miller95def091999-11-25 00:26:21 +110091 /* This buffer is used to built the secret part of the private key. */
92 buffer_init(&buffer);
Damien Millerd4a8b7e1999-10-27 13:42:43 +100093
Damien Miller95def091999-11-25 00:26:21 +110094 /* Put checkbytes for checking passphrase validity. */
Darren Tucker3f9fdc72004-06-22 12:56:01 +100095 rnd = arc4random();
96 buf[0] = rnd & 0xff;
97 buf[1] = (rnd >> 8) & 0xff;
Damien Miller95def091999-11-25 00:26:21 +110098 buf[2] = buf[0];
99 buf[3] = buf[1];
100 buffer_append(&buffer, buf, 4);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000101
Damien Miller5428f641999-11-25 11:54:57 +1100102 /*
103 * Store the private key (n and e will not be stored because they
104 * will be stored in plain text, and storing them also in encrypted
105 * format would just give known plaintext).
106 */
Ben Lindstromd0fca422001-03-26 13:44:06 +0000107 buffer_put_bignum(&buffer, key->rsa->d);
108 buffer_put_bignum(&buffer, key->rsa->iqmp);
109 buffer_put_bignum(&buffer, key->rsa->q); /* reverse from SSL p */
110 buffer_put_bignum(&buffer, key->rsa->p); /* reverse from SSL q */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000111
Damien Miller95def091999-11-25 00:26:21 +1100112 /* Pad the part to be encrypted until its size is a multiple of 8. */
113 while (buffer_len(&buffer) % 8 != 0)
114 buffer_put_char(&buffer, 0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000115
Damien Miller95def091999-11-25 00:26:21 +1100116 /* This buffer will be used to contain the data in the file. */
117 buffer_init(&encrypted);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000118
Damien Miller95def091999-11-25 00:26:21 +1100119 /* First store keyfile id string. */
Ben Lindstrom1170d712001-01-29 07:51:26 +0000120 for (i = 0; authfile_id_string[i]; i++)
121 buffer_put_char(&encrypted, authfile_id_string[i]);
Damien Miller95def091999-11-25 00:26:21 +1100122 buffer_put_char(&encrypted, 0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000123
Damien Miller95def091999-11-25 00:26:21 +1100124 /* Store cipher type. */
Damien Miller963f6b22002-02-19 15:21:23 +1100125 buffer_put_char(&encrypted, cipher_num);
Damien Miller95def091999-11-25 00:26:21 +1100126 buffer_put_int(&encrypted, 0); /* For future extension */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000127
Damien Miller95def091999-11-25 00:26:21 +1100128 /* Store public key. This will be in plain text. */
Ben Lindstromd0fca422001-03-26 13:44:06 +0000129 buffer_put_int(&encrypted, BN_num_bits(key->rsa->n));
130 buffer_put_bignum(&encrypted, key->rsa->n);
131 buffer_put_bignum(&encrypted, key->rsa->e);
Ben Lindstrom664408d2001-06-09 01:42:01 +0000132 buffer_put_cstring(&encrypted, comment);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000133
Damien Miller95def091999-11-25 00:26:21 +1100134 /* Allocate space for the private part of the key in the buffer. */
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100135 cp = buffer_append_space(&encrypted, buffer_len(&buffer));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000136
Damien Miller963f6b22002-02-19 15:21:23 +1100137 cipher_set_key_string(&ciphercontext, cipher, passphrase,
138 CIPHER_ENCRYPT);
139 cipher_crypt(&ciphercontext, cp,
Damien Miller708d21c2002-01-22 23:18:15 +1100140 buffer_ptr(&buffer), buffer_len(&buffer));
Damien Miller963f6b22002-02-19 15:21:23 +1100141 cipher_cleanup(&ciphercontext);
Damien Miller874d77b2000-10-14 16:23:11 +1100142 memset(&ciphercontext, 0, sizeof(ciphercontext));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000143
Damien Miller95def091999-11-25 00:26:21 +1100144 /* Destroy temporary data. */
145 memset(buf, 0, sizeof(buf));
146 buffer_free(&buffer);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000147
Damien Miller037a0dc1999-12-07 15:38:31 +1100148 fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0600);
Ben Lindstrom15f33862001-04-16 02:00:02 +0000149 if (fd < 0) {
150 error("open %s failed: %s.", filename, strerror(errno));
Darren Tuckerd1d41b32003-09-22 21:01:27 +1000151 buffer_free(&encrypted);
Damien Miller95def091999-11-25 00:26:21 +1100152 return 0;
Ben Lindstrom15f33862001-04-16 02:00:02 +0000153 }
Damien Millereccb9de2005-06-17 12:59:34 +1000154 if (atomicio(vwrite, fd, buffer_ptr(&encrypted),
155 buffer_len(&encrypted)) != buffer_len(&encrypted)) {
Ben Lindstrom15f33862001-04-16 02:00:02 +0000156 error("write to key file %s failed: %s", filename,
Damien Miller9f0f5c62001-12-21 14:45:46 +1100157 strerror(errno));
Damien Miller95def091999-11-25 00:26:21 +1100158 buffer_free(&encrypted);
Damien Miller037a0dc1999-12-07 15:38:31 +1100159 close(fd);
Damien Miller78315eb2000-09-29 23:01:36 +1100160 unlink(filename);
Damien Miller95def091999-11-25 00:26:21 +1100161 return 0;
162 }
Damien Miller037a0dc1999-12-07 15:38:31 +1100163 close(fd);
Damien Miller95def091999-11-25 00:26:21 +1100164 buffer_free(&encrypted);
165 return 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000166}
167
Ben Lindstromd0fca422001-03-26 13:44:06 +0000168/* save SSH v2 key in OpenSSL PEM format */
Ben Lindstrombba81212001-06-25 05:01:22 +0000169static int
Ben Lindstromd0fca422001-03-26 13:44:06 +0000170key_save_private_pem(Key *key, const char *filename, const char *_passphrase,
171 const char *comment)
Damien Millereba71ba2000-04-29 23:57:08 +1000172{
173 FILE *fp;
174 int fd;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100175 int success = 0;
176 int len = strlen(_passphrase);
Ben Lindstrom90fd8142002-02-26 18:09:42 +0000177 u_char *passphrase = (len > 0) ? (u_char *)_passphrase : NULL;
Ben Lindstrom80cb27d2002-03-05 01:33:36 +0000178 const EVP_CIPHER *cipher = (len > 0) ? EVP_des_ede3_cbc() : NULL;
Damien Millereba71ba2000-04-29 23:57:08 +1000179
180 if (len > 0 && len <= 4) {
Ben Lindstrom15f33862001-04-16 02:00:02 +0000181 error("passphrase too short: have %d bytes, need > 4", len);
Damien Millereba71ba2000-04-29 23:57:08 +1000182 return 0;
183 }
184 fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0600);
185 if (fd < 0) {
Ben Lindstrom15f33862001-04-16 02:00:02 +0000186 error("open %s failed: %s.", filename, strerror(errno));
Damien Millereba71ba2000-04-29 23:57:08 +1000187 return 0;
188 }
189 fp = fdopen(fd, "w");
190 if (fp == NULL ) {
Ben Lindstrom15f33862001-04-16 02:00:02 +0000191 error("fdopen %s failed: %s.", filename, strerror(errno));
Damien Millereba71ba2000-04-29 23:57:08 +1000192 close(fd);
193 return 0;
194 }
Damien Miller0bc1bd82000-11-13 22:57:25 +1100195 switch (key->type) {
Ben Lindstromc1116602001-03-29 00:28:37 +0000196 case KEY_DSA:
197 success = PEM_write_DSAPrivateKey(fp, key->dsa,
198 cipher, passphrase, len, NULL, NULL);
199 break;
200 case KEY_RSA:
201 success = PEM_write_RSAPrivateKey(fp, key->rsa,
202 cipher, passphrase, len, NULL, NULL);
203 break;
Damien Millereba71ba2000-04-29 23:57:08 +1000204 }
205 fclose(fp);
206 return success;
207}
208
209int
Ben Lindstromd0fca422001-03-26 13:44:06 +0000210key_save_private(Key *key, const char *filename, const char *passphrase,
Damien Millereba71ba2000-04-29 23:57:08 +1000211 const char *comment)
212{
213 switch (key->type) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100214 case KEY_RSA1:
Ben Lindstromd0fca422001-03-26 13:44:06 +0000215 return key_save_private_rsa1(key, filename, passphrase,
216 comment);
Damien Millereba71ba2000-04-29 23:57:08 +1000217 case KEY_DSA:
Damien Miller0bc1bd82000-11-13 22:57:25 +1100218 case KEY_RSA:
Ben Lindstromd0fca422001-03-26 13:44:06 +0000219 return key_save_private_pem(key, filename, passphrase,
220 comment);
Damien Millereba71ba2000-04-29 23:57:08 +1000221 default:
222 break;
223 }
Ben Lindstrom15f33862001-04-16 02:00:02 +0000224 error("key_save_private: cannot save key type %d", key->type);
Damien Millereba71ba2000-04-29 23:57:08 +1000225 return 0;
226}
227
Damien Miller5428f641999-11-25 11:54:57 +1100228/*
Ben Lindstromd0fca422001-03-26 13:44:06 +0000229 * Loads the public part of the ssh v1 key file. Returns NULL if an error was
230 * encountered (the file does not exist or is not readable), and the key
Damien Miller5428f641999-11-25 11:54:57 +1100231 * otherwise.
232 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000233
Ben Lindstrombba81212001-06-25 05:01:22 +0000234static Key *
Ben Lindstromd0fca422001-03-26 13:44:06 +0000235key_load_public_rsa1(int fd, const char *filename, char **commentp)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000236{
Damien Miller95def091999-11-25 00:26:21 +1100237 Buffer buffer;
Ben Lindstromd0fca422001-03-26 13:44:06 +0000238 Key *pub;
Ben Lindstrom44adb8f2002-12-23 02:00:23 +0000239 struct stat st;
Damien Miller95def091999-11-25 00:26:21 +1100240 char *cp;
Damien Millereccb9de2005-06-17 12:59:34 +1000241 u_int i;
Darren Tucker1f8311c2004-05-13 16:39:33 +1000242 size_t len;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000243
Ben Lindstrom44adb8f2002-12-23 02:00:23 +0000244 if (fstat(fd, &st) < 0) {
245 error("fstat for key file %.200s failed: %.100s",
246 filename, strerror(errno));
247 return NULL;
248 }
Darren Tuckerf4b43712004-08-29 16:28:39 +1000249 if (st.st_size > 1*1024*1024) {
250 error("key file %.200s too large", filename);
251 return NULL;
252 }
Darren Tucker1f8311c2004-05-13 16:39:33 +1000253 len = (size_t)st.st_size; /* truncated */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000254
Damien Miller95def091999-11-25 00:26:21 +1100255 buffer_init(&buffer);
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100256 cp = buffer_append_space(&buffer, len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000257
Damien Millereccb9de2005-06-17 12:59:34 +1000258 if (atomicio(read, fd, cp, len) != len) {
Damien Miller95def091999-11-25 00:26:21 +1100259 debug("Read from key file %.200s failed: %.100s", filename,
Damien Millereba71ba2000-04-29 23:57:08 +1000260 strerror(errno));
Damien Miller95def091999-11-25 00:26:21 +1100261 buffer_free(&buffer);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000262 return NULL;
Damien Miller95def091999-11-25 00:26:21 +1100263 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000264
Ben Lindstrom1170d712001-01-29 07:51:26 +0000265 /* Check that it is at least big enough to contain the ID string. */
266 if (len < sizeof(authfile_id_string)) {
Damien Miller058655c2001-10-10 15:03:36 +1000267 debug3("Not a RSA1 key file %.200s.", filename);
Damien Miller95def091999-11-25 00:26:21 +1100268 buffer_free(&buffer);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000269 return NULL;
Damien Miller95def091999-11-25 00:26:21 +1100270 }
Damien Miller5428f641999-11-25 11:54:57 +1100271 /*
272 * Make sure it begins with the id string. Consume the id string
273 * from the buffer.
274 */
Ben Lindstrom1170d712001-01-29 07:51:26 +0000275 for (i = 0; i < sizeof(authfile_id_string); i++)
276 if (buffer_get_char(&buffer) != authfile_id_string[i]) {
Damien Miller058655c2001-10-10 15:03:36 +1000277 debug3("Not a RSA1 key file %.200s.", filename);
Damien Miller95def091999-11-25 00:26:21 +1100278 buffer_free(&buffer);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000279 return NULL;
Damien Miller95def091999-11-25 00:26:21 +1100280 }
281 /* Skip cipher type and reserved data. */
282 (void) buffer_get_char(&buffer); /* cipher type */
283 (void) buffer_get_int(&buffer); /* reserved */
284
285 /* Read the public key from the buffer. */
Ben Lindstromc5a7f4f2002-06-25 23:19:13 +0000286 (void) buffer_get_int(&buffer);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000287 pub = key_new(KEY_RSA1);
288 buffer_get_bignum(&buffer, pub->rsa->n);
289 buffer_get_bignum(&buffer, pub->rsa->e);
290 if (commentp)
291 *commentp = buffer_get_string(&buffer, NULL);
Damien Miller95def091999-11-25 00:26:21 +1100292 /* The encrypted private part is not parsed by this function. */
293
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000294 buffer_free(&buffer);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000295 return pub;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000296}
297
Ben Lindstromd0fca422001-03-26 13:44:06 +0000298/* load public key from private-key file, works only for SSH v1 */
299Key *
300key_load_public_type(int type, const char *filename, char **commentp)
Damien Millereba71ba2000-04-29 23:57:08 +1000301{
Ben Lindstromd0fca422001-03-26 13:44:06 +0000302 Key *pub;
303 int fd;
304
305 if (type == KEY_RSA1) {
306 fd = open(filename, O_RDONLY);
307 if (fd < 0)
308 return NULL;
309 pub = key_load_public_rsa1(fd, filename, commentp);
310 close(fd);
311 return pub;
Damien Millereba71ba2000-04-29 23:57:08 +1000312 }
Ben Lindstromd0fca422001-03-26 13:44:06 +0000313 return NULL;
Damien Millereba71ba2000-04-29 23:57:08 +1000314}
315
Damien Miller5428f641999-11-25 11:54:57 +1100316/*
317 * Loads the private key from the file. Returns 0 if an error is encountered
318 * (file does not exist or is not readable, or passphrase is bad). This
319 * initializes the private key.
320 * Assumes we are called under uid of the owner of the file.
321 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000322
Ben Lindstrombba81212001-06-25 05:01:22 +0000323static Key *
Ben Lindstromd0fca422001-03-26 13:44:06 +0000324key_load_private_rsa1(int fd, const char *filename, const char *passphrase,
325 char **commentp)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000326{
Damien Millereccb9de2005-06-17 12:59:34 +1000327 u_int i;
328 int check1, check2, cipher_type;
Darren Tucker1f8311c2004-05-13 16:39:33 +1000329 size_t len;
Damien Miller95def091999-11-25 00:26:21 +1100330 Buffer buffer, decrypted;
Damien Miller708d21c2002-01-22 23:18:15 +1100331 u_char *cp;
Damien Miller874d77b2000-10-14 16:23:11 +1100332 CipherContext ciphercontext;
333 Cipher *cipher;
Ben Lindstromd0fca422001-03-26 13:44:06 +0000334 Key *prv = NULL;
Ben Lindstrom44adb8f2002-12-23 02:00:23 +0000335 struct stat st;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000336
Ben Lindstrom44adb8f2002-12-23 02:00:23 +0000337 if (fstat(fd, &st) < 0) {
338 error("fstat for key file %.200s failed: %.100s",
339 filename, strerror(errno));
340 close(fd);
341 return NULL;
342 }
Darren Tucker1f8311c2004-05-13 16:39:33 +1000343 if (st.st_size > 1*1024*1024) {
Darren Tuckerf4b43712004-08-29 16:28:39 +1000344 error("key file %.200s too large", filename);
Darren Tucker1f8311c2004-05-13 16:39:33 +1000345 close(fd);
346 return (NULL);
347 }
348 len = (size_t)st.st_size; /* truncated */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000349
Damien Miller95def091999-11-25 00:26:21 +1100350 buffer_init(&buffer);
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100351 cp = buffer_append_space(&buffer, len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000352
Damien Millereccb9de2005-06-17 12:59:34 +1000353 if (atomicio(read, fd, cp, len) != len) {
Damien Miller95def091999-11-25 00:26:21 +1100354 debug("Read from key file %.200s failed: %.100s", filename,
Damien Miller0bc1bd82000-11-13 22:57:25 +1100355 strerror(errno));
Damien Miller95def091999-11-25 00:26:21 +1100356 buffer_free(&buffer);
Damien Miller037a0dc1999-12-07 15:38:31 +1100357 close(fd);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000358 return NULL;
Damien Miller95def091999-11-25 00:26:21 +1100359 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000360
Ben Lindstrom1170d712001-01-29 07:51:26 +0000361 /* Check that it is at least big enough to contain the ID string. */
362 if (len < sizeof(authfile_id_string)) {
Damien Miller058655c2001-10-10 15:03:36 +1000363 debug3("Not a RSA1 key file %.200s.", filename);
Damien Miller95def091999-11-25 00:26:21 +1100364 buffer_free(&buffer);
Ben Lindstromb257cca2001-03-05 04:59:27 +0000365 close(fd);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000366 return NULL;
Damien Miller95def091999-11-25 00:26:21 +1100367 }
Damien Miller5428f641999-11-25 11:54:57 +1100368 /*
369 * Make sure it begins with the id string. Consume the id string
370 * from the buffer.
371 */
Ben Lindstrom1170d712001-01-29 07:51:26 +0000372 for (i = 0; i < sizeof(authfile_id_string); i++)
373 if (buffer_get_char(&buffer) != authfile_id_string[i]) {
Damien Miller058655c2001-10-10 15:03:36 +1000374 debug3("Not a RSA1 key file %.200s.", filename);
Damien Miller95def091999-11-25 00:26:21 +1100375 buffer_free(&buffer);
Ben Lindstromb257cca2001-03-05 04:59:27 +0000376 close(fd);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000377 return NULL;
Damien Miller95def091999-11-25 00:26:21 +1100378 }
Ben Lindstromb257cca2001-03-05 04:59:27 +0000379
Damien Miller95def091999-11-25 00:26:21 +1100380 /* Read cipher type. */
381 cipher_type = buffer_get_char(&buffer);
382 (void) buffer_get_int(&buffer); /* Reserved data. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000383
Damien Miller95def091999-11-25 00:26:21 +1100384 /* Read the public key from the buffer. */
Ben Lindstromc5a7f4f2002-06-25 23:19:13 +0000385 (void) buffer_get_int(&buffer);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000386 prv = key_new_private(KEY_RSA1);
387
388 buffer_get_bignum(&buffer, prv->rsa->n);
389 buffer_get_bignum(&buffer, prv->rsa->e);
390 if (commentp)
391 *commentp = buffer_get_string(&buffer, NULL);
Damien Miller95def091999-11-25 00:26:21 +1100392 else
393 xfree(buffer_get_string(&buffer, NULL));
394
395 /* Check that it is a supported cipher. */
Damien Miller874d77b2000-10-14 16:23:11 +1100396 cipher = cipher_by_number(cipher_type);
397 if (cipher == NULL) {
398 debug("Unsupported cipher %d used in key file %.200s.",
399 cipher_type, filename);
Damien Miller95def091999-11-25 00:26:21 +1100400 buffer_free(&buffer);
401 goto fail;
402 }
403 /* Initialize space for decrypted data. */
404 buffer_init(&decrypted);
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100405 cp = buffer_append_space(&decrypted, buffer_len(&buffer));
Damien Miller95def091999-11-25 00:26:21 +1100406
407 /* Rest of the buffer is encrypted. Decrypt it using the passphrase. */
Damien Miller963f6b22002-02-19 15:21:23 +1100408 cipher_set_key_string(&ciphercontext, cipher, passphrase,
409 CIPHER_DECRYPT);
410 cipher_crypt(&ciphercontext, cp,
Damien Miller708d21c2002-01-22 23:18:15 +1100411 buffer_ptr(&buffer), buffer_len(&buffer));
Damien Miller963f6b22002-02-19 15:21:23 +1100412 cipher_cleanup(&ciphercontext);
Damien Miller874d77b2000-10-14 16:23:11 +1100413 memset(&ciphercontext, 0, sizeof(ciphercontext));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000414 buffer_free(&buffer);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000415
Damien Miller95def091999-11-25 00:26:21 +1100416 check1 = buffer_get_char(&decrypted);
417 check2 = buffer_get_char(&decrypted);
418 if (check1 != buffer_get_char(&decrypted) ||
419 check2 != buffer_get_char(&decrypted)) {
420 if (strcmp(passphrase, "") != 0)
Ben Lindstromd0fca422001-03-26 13:44:06 +0000421 debug("Bad passphrase supplied for key file %.200s.",
422 filename);
Damien Miller95def091999-11-25 00:26:21 +1100423 /* Bad passphrase. */
424 buffer_free(&decrypted);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000425 goto fail;
Damien Miller95def091999-11-25 00:26:21 +1100426 }
427 /* Read the rest of the private key. */
Ben Lindstromd0fca422001-03-26 13:44:06 +0000428 buffer_get_bignum(&decrypted, prv->rsa->d);
429 buffer_get_bignum(&decrypted, prv->rsa->iqmp); /* u */
430 /* in SSL and SSH v1 p and q are exchanged */
431 buffer_get_bignum(&decrypted, prv->rsa->q); /* p */
432 buffer_get_bignum(&decrypted, prv->rsa->p); /* q */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000433
Ben Lindstromd0fca422001-03-26 13:44:06 +0000434 /* calculate p-1 and q-1 */
Damien Millerda755162002-01-22 23:09:22 +1100435 rsa_generate_additional_parameters(prv->rsa);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000436
Damien Miller95def091999-11-25 00:26:21 +1100437 buffer_free(&decrypted);
Damien Millered33d3b2003-03-15 11:36:18 +1100438
439 /* enable blinding */
440 if (RSA_blinding_on(prv->rsa, NULL) != 1) {
441 error("key_load_private_rsa1: RSA_blinding_on failed");
442 goto fail;
443 }
Ben Lindstromb257cca2001-03-05 04:59:27 +0000444 close(fd);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000445 return prv;
446
447fail:
448 if (commentp)
449 xfree(*commentp);
450 close(fd);
451 key_free(prv);
452 return NULL;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000453}
Damien Millereba71ba2000-04-29 23:57:08 +1000454
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000455Key *
Ben Lindstromd0fca422001-03-26 13:44:06 +0000456key_load_private_pem(int fd, int type, const char *passphrase,
457 char **commentp)
Damien Millereba71ba2000-04-29 23:57:08 +1000458{
Damien Millereba71ba2000-04-29 23:57:08 +1000459 FILE *fp;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100460 EVP_PKEY *pk = NULL;
Ben Lindstromd0fca422001-03-26 13:44:06 +0000461 Key *prv = NULL;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100462 char *name = "<no key>";
Damien Millereba71ba2000-04-29 23:57:08 +1000463
Damien Millereba71ba2000-04-29 23:57:08 +1000464 fp = fdopen(fd, "r");
465 if (fp == NULL) {
Ben Lindstrom15f33862001-04-16 02:00:02 +0000466 error("fdopen failed: %s", strerror(errno));
Ben Lindstromb257cca2001-03-05 04:59:27 +0000467 close(fd);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000468 return NULL;
Damien Millereba71ba2000-04-29 23:57:08 +1000469 }
Damien Miller0bc1bd82000-11-13 22:57:25 +1100470 pk = PEM_read_PrivateKey(fp, NULL, NULL, (char *)passphrase);
471 if (pk == NULL) {
Ben Lindstrom648772f2001-04-19 20:47:10 +0000472 debug("PEM_read_PrivateKey failed");
Damien Miller0bc1bd82000-11-13 22:57:25 +1100473 (void)ERR_get_error();
Ben Lindstromd0fca422001-03-26 13:44:06 +0000474 } else if (pk->type == EVP_PKEY_RSA &&
Damien Miller9f0f5c62001-12-21 14:45:46 +1100475 (type == KEY_UNSPEC||type==KEY_RSA)) {
Ben Lindstromd0fca422001-03-26 13:44:06 +0000476 prv = key_new(KEY_UNSPEC);
477 prv->rsa = EVP_PKEY_get1_RSA(pk);
478 prv->type = KEY_RSA;
479 name = "rsa w/o comment";
Damien Miller0bc1bd82000-11-13 22:57:25 +1100480#ifdef DEBUG_PK
Ben Lindstromd0fca422001-03-26 13:44:06 +0000481 RSA_print_fp(stderr, prv->rsa, 8);
Damien Millereba71ba2000-04-29 23:57:08 +1000482#endif
Damien Millered33d3b2003-03-15 11:36:18 +1100483 if (RSA_blinding_on(prv->rsa, NULL) != 1) {
484 error("key_load_private_pem: RSA_blinding_on failed");
485 key_free(prv);
486 prv = NULL;
487 }
Ben Lindstromd0fca422001-03-26 13:44:06 +0000488 } else if (pk->type == EVP_PKEY_DSA &&
Damien Miller9f0f5c62001-12-21 14:45:46 +1100489 (type == KEY_UNSPEC||type==KEY_DSA)) {
Ben Lindstromd0fca422001-03-26 13:44:06 +0000490 prv = key_new(KEY_UNSPEC);
491 prv->dsa = EVP_PKEY_get1_DSA(pk);
492 prv->type = KEY_DSA;
493 name = "dsa w/o comment";
Damien Miller0bc1bd82000-11-13 22:57:25 +1100494#ifdef DEBUG_PK
Ben Lindstromd0fca422001-03-26 13:44:06 +0000495 DSA_print_fp(stderr, prv->dsa, 8);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100496#endif
Damien Miller0bc1bd82000-11-13 22:57:25 +1100497 } else {
498 error("PEM_read_PrivateKey: mismatch or "
499 "unknown EVP_PKEY save_type %d", pk->save_type);
500 }
501 fclose(fp);
502 if (pk != NULL)
503 EVP_PKEY_free(pk);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000504 if (prv != NULL && commentp)
505 *commentp = xstrdup(name);
506 debug("read PEM private key done: type %s",
507 prv ? key_type(prv) : "<unknown>");
508 return prv;
Damien Millereba71ba2000-04-29 23:57:08 +1000509}
510
Damien Miller8275fad2006-03-15 12:06:23 +1100511int
Ben Lindstromd0fca422001-03-26 13:44:06 +0000512key_perm_ok(int fd, const char *filename)
Damien Millereba71ba2000-04-29 23:57:08 +1000513{
Damien Millereba71ba2000-04-29 23:57:08 +1000514 struct stat st;
515
Ben Lindstrom7aff2612001-09-23 13:53:22 +0000516 if (fstat(fd, &st) < 0)
517 return 0;
518 /*
519 * if a key owned by the user is accessed, then we check the
520 * permissions of the file. if the key owned by a different user,
521 * then we don't care.
522 */
Damien Millerb70b61f2000-09-16 16:25:12 +1100523#ifdef HAVE_CYGWIN
Damien Millercb5e44a2000-09-29 12:12:36 +1100524 if (check_ntsec(filename))
Damien Millerb70b61f2000-09-16 16:25:12 +1100525#endif
Ben Lindstrom7aff2612001-09-23 13:53:22 +0000526 if ((st.st_uid == getuid()) && (st.st_mode & 077) != 0) {
Damien Millereba71ba2000-04-29 23:57:08 +1000527 error("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
528 error("@ WARNING: UNPROTECTED PRIVATE KEY FILE! @");
529 error("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
Ben Lindstrom7aff2612001-09-23 13:53:22 +0000530 error("Permissions 0%3.3o for '%s' are too open.",
Damien Miller04bd8b02003-05-25 14:38:33 +1000531 (u_int)st.st_mode & 0777, filename);
Damien Millereba71ba2000-04-29 23:57:08 +1000532 error("It is recommended that your private key files are NOT accessible by others.");
Ben Lindstromd0fca422001-03-26 13:44:06 +0000533 error("This private key will be ignored.");
Damien Millereba71ba2000-04-29 23:57:08 +1000534 return 0;
535 }
Ben Lindstromd0fca422001-03-26 13:44:06 +0000536 return 1;
537}
Ben Lindstromb257cca2001-03-05 04:59:27 +0000538
Ben Lindstromd0fca422001-03-26 13:44:06 +0000539Key *
540key_load_private_type(int type, const char *filename, const char *passphrase,
541 char **commentp)
542{
543 int fd;
544
545 fd = open(filename, O_RDONLY);
546 if (fd < 0)
547 return NULL;
548 if (!key_perm_ok(fd, filename)) {
Ben Lindstrom15f33862001-04-16 02:00:02 +0000549 error("bad permissions: ignore key: %s", filename);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000550 close(fd);
551 return NULL;
552 }
553 switch (type) {
554 case KEY_RSA1:
555 return key_load_private_rsa1(fd, filename, passphrase,
556 commentp);
557 /* closes fd */
Damien Millereba71ba2000-04-29 23:57:08 +1000558 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 */
Damien Millereba71ba2000-04-29 23:57:08 +1000563 default:
Ben Lindstromb257cca2001-03-05 04:59:27 +0000564 close(fd);
Damien Millereba71ba2000-04-29 23:57:08 +1000565 break;
566 }
Ben Lindstromd0fca422001-03-26 13:44:06 +0000567 return NULL;
568}
569
570Key *
571key_load_private(const char *filename, const char *passphrase,
572 char **commentp)
573{
Ben Lindstrom322915d2001-06-05 20:46:32 +0000574 Key *pub, *prv;
Ben Lindstromd0fca422001-03-26 13:44:06 +0000575 int fd;
576
577 fd = open(filename, O_RDONLY);
578 if (fd < 0)
579 return NULL;
580 if (!key_perm_ok(fd, filename)) {
Ben Lindstrom15f33862001-04-16 02:00:02 +0000581 error("bad permissions: ignore key: %s", filename);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000582 close(fd);
583 return NULL;
584 }
585 pub = key_load_public_rsa1(fd, filename, commentp);
586 lseek(fd, (off_t) 0, SEEK_SET); /* rewind */
587 if (pub == NULL) {
588 /* closes fd */
Ben Lindstrom322915d2001-06-05 20:46:32 +0000589 prv = key_load_private_pem(fd, KEY_UNSPEC, passphrase, NULL);
590 /* use the filename as a comment for PEM */
591 if (commentp && prv)
Ben Lindstrom2d0356f2001-06-05 21:13:57 +0000592 *commentp = xstrdup(filename);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000593 } else {
594 /* it's a SSH v1 key if the public key part is readable */
595 key_free(pub);
596 /* closes fd */
Ben Lindstrom322915d2001-06-05 20:46:32 +0000597 prv = key_load_private_rsa1(fd, filename, passphrase, NULL);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000598 }
Ben Lindstrom322915d2001-06-05 20:46:32 +0000599 return prv;
Damien Millereba71ba2000-04-29 23:57:08 +1000600}
Damien Millere4340be2000-09-16 13:29:08 +1100601
Ben Lindstrombba81212001-06-25 05:01:22 +0000602static int
Ben Lindstromd0fca422001-03-26 13:44:06 +0000603key_try_load_public(Key *k, const char *filename, char **commentp)
Damien Millere4340be2000-09-16 13:29:08 +1100604{
605 FILE *f;
Darren Tucker22cc7412004-12-06 22:47:41 +1100606 char line[SSH_MAX_PUBKEY_BYTES];
Damien Millere4340be2000-09-16 13:29:08 +1100607 char *cp;
Darren Tuckerf0f90982004-12-11 13:39:50 +1100608 u_long linenum = 0;
Damien Millere4340be2000-09-16 13:29:08 +1100609
610 f = fopen(filename, "r");
611 if (f != NULL) {
Darren Tucker22cc7412004-12-06 22:47:41 +1100612 while (read_keyfile_line(f, filename, line, sizeof(line),
613 &linenum) != -1) {
Damien Millere4340be2000-09-16 13:29:08 +1100614 cp = line;
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000615 switch (*cp) {
Damien Millere4340be2000-09-16 13:29:08 +1100616 case '#':
617 case '\n':
618 case '\0':
619 continue;
620 }
621 /* Skip leading whitespace. */
622 for (; *cp && (*cp == ' ' || *cp == '\t'); cp++)
623 ;
624 if (*cp) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100625 if (key_read(k, &cp) == 1) {
Damien Millere4340be2000-09-16 13:29:08 +1100626 if (commentp)
627 *commentp=xstrdup(filename);
628 fclose(f);
629 return 1;
630 }
631 }
632 }
633 fclose(f);
634 }
635 return 0;
636}
637
Ben Lindstromd0fca422001-03-26 13:44:06 +0000638/* load public key from ssh v1 private or any pubkey file */
639Key *
640key_load_public(const char *filename, char **commentp)
Damien Millere4340be2000-09-16 13:29:08 +1100641{
Ben Lindstromd0fca422001-03-26 13:44:06 +0000642 Key *pub;
643 char file[MAXPATHLEN];
Damien Millere4340be2000-09-16 13:29:08 +1100644
Damien Millerdb274722003-05-14 13:45:22 +1000645 /* try rsa1 private key */
Ben Lindstromd0fca422001-03-26 13:44:06 +0000646 pub = key_load_public_type(KEY_RSA1, filename, commentp);
647 if (pub != NULL)
648 return pub;
Damien Millerdb274722003-05-14 13:45:22 +1000649
650 /* try rsa1 public key */
651 pub = key_new(KEY_RSA1);
652 if (key_try_load_public(pub, filename, commentp) == 1)
653 return pub;
654 key_free(pub);
655
656 /* try ssh2 public key */
Ben Lindstromd0fca422001-03-26 13:44:06 +0000657 pub = key_new(KEY_UNSPEC);
658 if (key_try_load_public(pub, filename, commentp) == 1)
659 return pub;
660 if ((strlcpy(file, filename, sizeof file) < sizeof(file)) &&
661 (strlcat(file, ".pub", sizeof file) < sizeof(file)) &&
662 (key_try_load_public(pub, file, commentp) == 1))
663 return pub;
664 key_free(pub);
665 return NULL;
Damien Millere4340be2000-09-16 13:29:08 +1100666}