blob: f97cf1820df6b7ca6695b31e4e3fe2b6d175cf3c [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 +110039RCSID("$OpenBSD: authfile.c,v 1.62 2006/02/20 17:19:54 stevesk Exp $");
40
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 break;
218 case KEY_DSA:
Damien Miller0bc1bd82000-11-13 22:57:25 +1100219 case KEY_RSA:
Ben Lindstromd0fca422001-03-26 13:44:06 +0000220 return key_save_private_pem(key, filename, passphrase,
221 comment);
Damien Millereba71ba2000-04-29 23:57:08 +1000222 break;
223 default:
224 break;
225 }
Ben Lindstrom15f33862001-04-16 02:00:02 +0000226 error("key_save_private: cannot save key type %d", key->type);
Damien Millereba71ba2000-04-29 23:57:08 +1000227 return 0;
228}
229
Damien Miller5428f641999-11-25 11:54:57 +1100230/*
Ben Lindstromd0fca422001-03-26 13:44:06 +0000231 * Loads the public part of the ssh v1 key file. Returns NULL if an error was
232 * encountered (the file does not exist or is not readable), and the key
Damien Miller5428f641999-11-25 11:54:57 +1100233 * otherwise.
234 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000235
Ben Lindstrombba81212001-06-25 05:01:22 +0000236static Key *
Ben Lindstromd0fca422001-03-26 13:44:06 +0000237key_load_public_rsa1(int fd, const char *filename, char **commentp)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000238{
Damien Miller95def091999-11-25 00:26:21 +1100239 Buffer buffer;
Ben Lindstromd0fca422001-03-26 13:44:06 +0000240 Key *pub;
Ben Lindstrom44adb8f2002-12-23 02:00:23 +0000241 struct stat st;
Damien Miller95def091999-11-25 00:26:21 +1100242 char *cp;
Damien Millereccb9de2005-06-17 12:59:34 +1000243 u_int i;
Darren Tucker1f8311c2004-05-13 16:39:33 +1000244 size_t len;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000245
Ben Lindstrom44adb8f2002-12-23 02:00:23 +0000246 if (fstat(fd, &st) < 0) {
247 error("fstat for key file %.200s failed: %.100s",
248 filename, strerror(errno));
249 return NULL;
250 }
Darren Tuckerf4b43712004-08-29 16:28:39 +1000251 if (st.st_size > 1*1024*1024) {
252 error("key file %.200s too large", filename);
253 return NULL;
254 }
Darren Tucker1f8311c2004-05-13 16:39:33 +1000255 len = (size_t)st.st_size; /* truncated */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000256
Damien Miller95def091999-11-25 00:26:21 +1100257 buffer_init(&buffer);
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100258 cp = buffer_append_space(&buffer, len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000259
Damien Millereccb9de2005-06-17 12:59:34 +1000260 if (atomicio(read, fd, cp, len) != len) {
Damien Miller95def091999-11-25 00:26:21 +1100261 debug("Read from key file %.200s failed: %.100s", filename,
Damien Millereba71ba2000-04-29 23:57:08 +1000262 strerror(errno));
Damien Miller95def091999-11-25 00:26:21 +1100263 buffer_free(&buffer);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000264 return NULL;
Damien Miller95def091999-11-25 00:26:21 +1100265 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000266
Ben Lindstrom1170d712001-01-29 07:51:26 +0000267 /* Check that it is at least big enough to contain the ID string. */
268 if (len < sizeof(authfile_id_string)) {
Damien Miller058655c2001-10-10 15:03:36 +1000269 debug3("Not a RSA1 key file %.200s.", filename);
Damien Miller95def091999-11-25 00:26:21 +1100270 buffer_free(&buffer);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000271 return NULL;
Damien Miller95def091999-11-25 00:26:21 +1100272 }
Damien Miller5428f641999-11-25 11:54:57 +1100273 /*
274 * Make sure it begins with the id string. Consume the id string
275 * from the buffer.
276 */
Ben Lindstrom1170d712001-01-29 07:51:26 +0000277 for (i = 0; i < sizeof(authfile_id_string); i++)
278 if (buffer_get_char(&buffer) != authfile_id_string[i]) {
Damien Miller058655c2001-10-10 15:03:36 +1000279 debug3("Not a RSA1 key file %.200s.", filename);
Damien Miller95def091999-11-25 00:26:21 +1100280 buffer_free(&buffer);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000281 return NULL;
Damien Miller95def091999-11-25 00:26:21 +1100282 }
283 /* Skip cipher type and reserved data. */
284 (void) buffer_get_char(&buffer); /* cipher type */
285 (void) buffer_get_int(&buffer); /* reserved */
286
287 /* Read the public key from the buffer. */
Ben Lindstromc5a7f4f2002-06-25 23:19:13 +0000288 (void) buffer_get_int(&buffer);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000289 pub = key_new(KEY_RSA1);
290 buffer_get_bignum(&buffer, pub->rsa->n);
291 buffer_get_bignum(&buffer, pub->rsa->e);
292 if (commentp)
293 *commentp = buffer_get_string(&buffer, NULL);
Damien Miller95def091999-11-25 00:26:21 +1100294 /* The encrypted private part is not parsed by this function. */
295
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000296 buffer_free(&buffer);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000297 return pub;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000298}
299
Ben Lindstromd0fca422001-03-26 13:44:06 +0000300/* load public key from private-key file, works only for SSH v1 */
301Key *
302key_load_public_type(int type, const char *filename, char **commentp)
Damien Millereba71ba2000-04-29 23:57:08 +1000303{
Ben Lindstromd0fca422001-03-26 13:44:06 +0000304 Key *pub;
305 int fd;
306
307 if (type == KEY_RSA1) {
308 fd = open(filename, O_RDONLY);
309 if (fd < 0)
310 return NULL;
311 pub = key_load_public_rsa1(fd, filename, commentp);
312 close(fd);
313 return pub;
Damien Millereba71ba2000-04-29 23:57:08 +1000314 }
Ben Lindstromd0fca422001-03-26 13:44:06 +0000315 return NULL;
Damien Millereba71ba2000-04-29 23:57:08 +1000316}
317
Damien Miller5428f641999-11-25 11:54:57 +1100318/*
319 * Loads the private key from the file. Returns 0 if an error is encountered
320 * (file does not exist or is not readable, or passphrase is bad). This
321 * initializes the private key.
322 * Assumes we are called under uid of the owner of the file.
323 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000324
Ben Lindstrombba81212001-06-25 05:01:22 +0000325static Key *
Ben Lindstromd0fca422001-03-26 13:44:06 +0000326key_load_private_rsa1(int fd, const char *filename, const char *passphrase,
327 char **commentp)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000328{
Damien Millereccb9de2005-06-17 12:59:34 +1000329 u_int i;
330 int check1, check2, cipher_type;
Darren Tucker1f8311c2004-05-13 16:39:33 +1000331 size_t len;
Damien Miller95def091999-11-25 00:26:21 +1100332 Buffer buffer, decrypted;
Damien Miller708d21c2002-01-22 23:18:15 +1100333 u_char *cp;
Damien Miller874d77b2000-10-14 16:23:11 +1100334 CipherContext ciphercontext;
335 Cipher *cipher;
Ben Lindstromd0fca422001-03-26 13:44:06 +0000336 Key *prv = NULL;
Ben Lindstrom44adb8f2002-12-23 02:00:23 +0000337 struct stat st;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000338
Ben Lindstrom44adb8f2002-12-23 02:00:23 +0000339 if (fstat(fd, &st) < 0) {
340 error("fstat for key file %.200s failed: %.100s",
341 filename, strerror(errno));
342 close(fd);
343 return NULL;
344 }
Darren Tucker1f8311c2004-05-13 16:39:33 +1000345 if (st.st_size > 1*1024*1024) {
Darren Tuckerf4b43712004-08-29 16:28:39 +1000346 error("key file %.200s too large", filename);
Darren Tucker1f8311c2004-05-13 16:39:33 +1000347 close(fd);
348 return (NULL);
349 }
350 len = (size_t)st.st_size; /* truncated */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000351
Damien Miller95def091999-11-25 00:26:21 +1100352 buffer_init(&buffer);
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100353 cp = buffer_append_space(&buffer, len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000354
Damien Millereccb9de2005-06-17 12:59:34 +1000355 if (atomicio(read, fd, cp, len) != len) {
Damien Miller95def091999-11-25 00:26:21 +1100356 debug("Read from key file %.200s failed: %.100s", filename,
Damien Miller0bc1bd82000-11-13 22:57:25 +1100357 strerror(errno));
Damien Miller95def091999-11-25 00:26:21 +1100358 buffer_free(&buffer);
Damien Miller037a0dc1999-12-07 15:38:31 +1100359 close(fd);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000360 return NULL;
Damien Miller95def091999-11-25 00:26:21 +1100361 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000362
Ben Lindstrom1170d712001-01-29 07:51:26 +0000363 /* Check that it is at least big enough to contain the ID string. */
364 if (len < sizeof(authfile_id_string)) {
Damien Miller058655c2001-10-10 15:03:36 +1000365 debug3("Not a RSA1 key file %.200s.", filename);
Damien Miller95def091999-11-25 00:26:21 +1100366 buffer_free(&buffer);
Ben Lindstromb257cca2001-03-05 04:59:27 +0000367 close(fd);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000368 return NULL;
Damien Miller95def091999-11-25 00:26:21 +1100369 }
Damien Miller5428f641999-11-25 11:54:57 +1100370 /*
371 * Make sure it begins with the id string. Consume the id string
372 * from the buffer.
373 */
Ben Lindstrom1170d712001-01-29 07:51:26 +0000374 for (i = 0; i < sizeof(authfile_id_string); i++)
375 if (buffer_get_char(&buffer) != authfile_id_string[i]) {
Damien Miller058655c2001-10-10 15:03:36 +1000376 debug3("Not a RSA1 key file %.200s.", filename);
Damien Miller95def091999-11-25 00:26:21 +1100377 buffer_free(&buffer);
Ben Lindstromb257cca2001-03-05 04:59:27 +0000378 close(fd);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000379 return NULL;
Damien Miller95def091999-11-25 00:26:21 +1100380 }
Ben Lindstromb257cca2001-03-05 04:59:27 +0000381
Damien Miller95def091999-11-25 00:26:21 +1100382 /* Read cipher type. */
383 cipher_type = buffer_get_char(&buffer);
384 (void) buffer_get_int(&buffer); /* Reserved data. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000385
Damien Miller95def091999-11-25 00:26:21 +1100386 /* Read the public key from the buffer. */
Ben Lindstromc5a7f4f2002-06-25 23:19:13 +0000387 (void) buffer_get_int(&buffer);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000388 prv = key_new_private(KEY_RSA1);
389
390 buffer_get_bignum(&buffer, prv->rsa->n);
391 buffer_get_bignum(&buffer, prv->rsa->e);
392 if (commentp)
393 *commentp = buffer_get_string(&buffer, NULL);
Damien Miller95def091999-11-25 00:26:21 +1100394 else
395 xfree(buffer_get_string(&buffer, NULL));
396
397 /* Check that it is a supported cipher. */
Damien Miller874d77b2000-10-14 16:23:11 +1100398 cipher = cipher_by_number(cipher_type);
399 if (cipher == NULL) {
400 debug("Unsupported cipher %d used in key file %.200s.",
401 cipher_type, filename);
Damien Miller95def091999-11-25 00:26:21 +1100402 buffer_free(&buffer);
403 goto fail;
404 }
405 /* Initialize space for decrypted data. */
406 buffer_init(&decrypted);
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100407 cp = buffer_append_space(&decrypted, buffer_len(&buffer));
Damien Miller95def091999-11-25 00:26:21 +1100408
409 /* Rest of the buffer is encrypted. Decrypt it using the passphrase. */
Damien Miller963f6b22002-02-19 15:21:23 +1100410 cipher_set_key_string(&ciphercontext, cipher, passphrase,
411 CIPHER_DECRYPT);
412 cipher_crypt(&ciphercontext, cp,
Damien Miller708d21c2002-01-22 23:18:15 +1100413 buffer_ptr(&buffer), buffer_len(&buffer));
Damien Miller963f6b22002-02-19 15:21:23 +1100414 cipher_cleanup(&ciphercontext);
Damien Miller874d77b2000-10-14 16:23:11 +1100415 memset(&ciphercontext, 0, sizeof(ciphercontext));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000416 buffer_free(&buffer);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000417
Damien Miller95def091999-11-25 00:26:21 +1100418 check1 = buffer_get_char(&decrypted);
419 check2 = buffer_get_char(&decrypted);
420 if (check1 != buffer_get_char(&decrypted) ||
421 check2 != buffer_get_char(&decrypted)) {
422 if (strcmp(passphrase, "") != 0)
Ben Lindstromd0fca422001-03-26 13:44:06 +0000423 debug("Bad passphrase supplied for key file %.200s.",
424 filename);
Damien Miller95def091999-11-25 00:26:21 +1100425 /* Bad passphrase. */
426 buffer_free(&decrypted);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000427 goto fail;
Damien Miller95def091999-11-25 00:26:21 +1100428 }
429 /* Read the rest of the private key. */
Ben Lindstromd0fca422001-03-26 13:44:06 +0000430 buffer_get_bignum(&decrypted, prv->rsa->d);
431 buffer_get_bignum(&decrypted, prv->rsa->iqmp); /* u */
432 /* in SSL and SSH v1 p and q are exchanged */
433 buffer_get_bignum(&decrypted, prv->rsa->q); /* p */
434 buffer_get_bignum(&decrypted, prv->rsa->p); /* q */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000435
Ben Lindstromd0fca422001-03-26 13:44:06 +0000436 /* calculate p-1 and q-1 */
Damien Millerda755162002-01-22 23:09:22 +1100437 rsa_generate_additional_parameters(prv->rsa);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000438
Damien Miller95def091999-11-25 00:26:21 +1100439 buffer_free(&decrypted);
Damien Millered33d3b2003-03-15 11:36:18 +1100440
441 /* enable blinding */
442 if (RSA_blinding_on(prv->rsa, NULL) != 1) {
443 error("key_load_private_rsa1: RSA_blinding_on failed");
444 goto fail;
445 }
Ben Lindstromb257cca2001-03-05 04:59:27 +0000446 close(fd);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000447 return prv;
448
449fail:
450 if (commentp)
451 xfree(*commentp);
452 close(fd);
453 key_free(prv);
454 return NULL;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000455}
Damien Millereba71ba2000-04-29 23:57:08 +1000456
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000457Key *
Ben Lindstromd0fca422001-03-26 13:44:06 +0000458key_load_private_pem(int fd, int type, const char *passphrase,
459 char **commentp)
Damien Millereba71ba2000-04-29 23:57:08 +1000460{
Damien Millereba71ba2000-04-29 23:57:08 +1000461 FILE *fp;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100462 EVP_PKEY *pk = NULL;
Ben Lindstromd0fca422001-03-26 13:44:06 +0000463 Key *prv = NULL;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100464 char *name = "<no key>";
Damien Millereba71ba2000-04-29 23:57:08 +1000465
Damien Millereba71ba2000-04-29 23:57:08 +1000466 fp = fdopen(fd, "r");
467 if (fp == NULL) {
Ben Lindstrom15f33862001-04-16 02:00:02 +0000468 error("fdopen failed: %s", strerror(errno));
Ben Lindstromb257cca2001-03-05 04:59:27 +0000469 close(fd);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000470 return NULL;
Damien Millereba71ba2000-04-29 23:57:08 +1000471 }
Damien Miller0bc1bd82000-11-13 22:57:25 +1100472 pk = PEM_read_PrivateKey(fp, NULL, NULL, (char *)passphrase);
473 if (pk == NULL) {
Ben Lindstrom648772f2001-04-19 20:47:10 +0000474 debug("PEM_read_PrivateKey failed");
Damien Miller0bc1bd82000-11-13 22:57:25 +1100475 (void)ERR_get_error();
Ben Lindstromd0fca422001-03-26 13:44:06 +0000476 } else if (pk->type == EVP_PKEY_RSA &&
Damien Miller9f0f5c62001-12-21 14:45:46 +1100477 (type == KEY_UNSPEC||type==KEY_RSA)) {
Ben Lindstromd0fca422001-03-26 13:44:06 +0000478 prv = key_new(KEY_UNSPEC);
479 prv->rsa = EVP_PKEY_get1_RSA(pk);
480 prv->type = KEY_RSA;
481 name = "rsa w/o comment";
Damien Miller0bc1bd82000-11-13 22:57:25 +1100482#ifdef DEBUG_PK
Ben Lindstromd0fca422001-03-26 13:44:06 +0000483 RSA_print_fp(stderr, prv->rsa, 8);
Damien Millereba71ba2000-04-29 23:57:08 +1000484#endif
Damien Millered33d3b2003-03-15 11:36:18 +1100485 if (RSA_blinding_on(prv->rsa, NULL) != 1) {
486 error("key_load_private_pem: RSA_blinding_on failed");
487 key_free(prv);
488 prv = NULL;
489 }
Ben Lindstromd0fca422001-03-26 13:44:06 +0000490 } else if (pk->type == EVP_PKEY_DSA &&
Damien Miller9f0f5c62001-12-21 14:45:46 +1100491 (type == KEY_UNSPEC||type==KEY_DSA)) {
Ben Lindstromd0fca422001-03-26 13:44:06 +0000492 prv = key_new(KEY_UNSPEC);
493 prv->dsa = EVP_PKEY_get1_DSA(pk);
494 prv->type = KEY_DSA;
495 name = "dsa w/o comment";
Damien Miller0bc1bd82000-11-13 22:57:25 +1100496#ifdef DEBUG_PK
Ben Lindstromd0fca422001-03-26 13:44:06 +0000497 DSA_print_fp(stderr, prv->dsa, 8);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100498#endif
Damien Miller0bc1bd82000-11-13 22:57:25 +1100499 } else {
500 error("PEM_read_PrivateKey: mismatch or "
501 "unknown EVP_PKEY save_type %d", pk->save_type);
502 }
503 fclose(fp);
504 if (pk != NULL)
505 EVP_PKEY_free(pk);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000506 if (prv != NULL && commentp)
507 *commentp = xstrdup(name);
508 debug("read PEM private key done: type %s",
509 prv ? key_type(prv) : "<unknown>");
510 return prv;
Damien Millereba71ba2000-04-29 23:57:08 +1000511}
512
Ben Lindstrombba81212001-06-25 05:01:22 +0000513static int
Ben Lindstromd0fca422001-03-26 13:44:06 +0000514key_perm_ok(int fd, const char *filename)
Damien Millereba71ba2000-04-29 23:57:08 +1000515{
Damien Millereba71ba2000-04-29 23:57:08 +1000516 struct stat st;
517
Ben Lindstrom7aff2612001-09-23 13:53:22 +0000518 if (fstat(fd, &st) < 0)
519 return 0;
520 /*
521 * if a key owned by the user is accessed, then we check the
522 * permissions of the file. if the key owned by a different user,
523 * then we don't care.
524 */
Damien Millerb70b61f2000-09-16 16:25:12 +1100525#ifdef HAVE_CYGWIN
Damien Millercb5e44a2000-09-29 12:12:36 +1100526 if (check_ntsec(filename))
Damien Millerb70b61f2000-09-16 16:25:12 +1100527#endif
Ben Lindstrom7aff2612001-09-23 13:53:22 +0000528 if ((st.st_uid == getuid()) && (st.st_mode & 077) != 0) {
Damien Millereba71ba2000-04-29 23:57:08 +1000529 error("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
530 error("@ WARNING: UNPROTECTED PRIVATE KEY FILE! @");
531 error("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
Ben Lindstrom7aff2612001-09-23 13:53:22 +0000532 error("Permissions 0%3.3o for '%s' are too open.",
Damien Miller04bd8b02003-05-25 14:38:33 +1000533 (u_int)st.st_mode & 0777, filename);
Damien Millereba71ba2000-04-29 23:57:08 +1000534 error("It is recommended that your private key files are NOT accessible by others.");
Ben Lindstromd0fca422001-03-26 13:44:06 +0000535 error("This private key will be ignored.");
Damien Millereba71ba2000-04-29 23:57:08 +1000536 return 0;
537 }
Ben Lindstromd0fca422001-03-26 13:44:06 +0000538 return 1;
539}
Ben Lindstromb257cca2001-03-05 04:59:27 +0000540
Ben Lindstromd0fca422001-03-26 13:44:06 +0000541Key *
542key_load_private_type(int type, const char *filename, const char *passphrase,
543 char **commentp)
544{
545 int fd;
546
547 fd = open(filename, O_RDONLY);
548 if (fd < 0)
549 return NULL;
550 if (!key_perm_ok(fd, filename)) {
Ben Lindstrom15f33862001-04-16 02:00:02 +0000551 error("bad permissions: ignore key: %s", filename);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000552 close(fd);
553 return NULL;
554 }
555 switch (type) {
556 case KEY_RSA1:
557 return key_load_private_rsa1(fd, filename, passphrase,
558 commentp);
559 /* closes fd */
Damien Millereba71ba2000-04-29 23:57:08 +1000560 break;
561 case KEY_DSA:
Damien Miller0bc1bd82000-11-13 22:57:25 +1100562 case KEY_RSA:
563 case KEY_UNSPEC:
Ben Lindstromd0fca422001-03-26 13:44:06 +0000564 return key_load_private_pem(fd, type, passphrase, commentp);
565 /* closes fd */
Ben Lindstromb257cca2001-03-05 04:59:27 +0000566 break;
Damien Millereba71ba2000-04-29 23:57:08 +1000567 default:
Ben Lindstromb257cca2001-03-05 04:59:27 +0000568 close(fd);
Damien Millereba71ba2000-04-29 23:57:08 +1000569 break;
570 }
Ben Lindstromd0fca422001-03-26 13:44:06 +0000571 return NULL;
572}
573
574Key *
575key_load_private(const char *filename, const char *passphrase,
576 char **commentp)
577{
Ben Lindstrom322915d2001-06-05 20:46:32 +0000578 Key *pub, *prv;
Ben Lindstromd0fca422001-03-26 13:44:06 +0000579 int fd;
580
581 fd = open(filename, O_RDONLY);
582 if (fd < 0)
583 return NULL;
584 if (!key_perm_ok(fd, filename)) {
Ben Lindstrom15f33862001-04-16 02:00:02 +0000585 error("bad permissions: ignore key: %s", filename);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000586 close(fd);
587 return NULL;
588 }
589 pub = key_load_public_rsa1(fd, filename, commentp);
590 lseek(fd, (off_t) 0, SEEK_SET); /* rewind */
591 if (pub == NULL) {
592 /* closes fd */
Ben Lindstrom322915d2001-06-05 20:46:32 +0000593 prv = key_load_private_pem(fd, KEY_UNSPEC, passphrase, NULL);
594 /* use the filename as a comment for PEM */
595 if (commentp && prv)
Ben Lindstrom2d0356f2001-06-05 21:13:57 +0000596 *commentp = xstrdup(filename);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000597 } else {
598 /* it's a SSH v1 key if the public key part is readable */
599 key_free(pub);
600 /* closes fd */
Ben Lindstrom322915d2001-06-05 20:46:32 +0000601 prv = key_load_private_rsa1(fd, filename, passphrase, NULL);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000602 }
Ben Lindstrom322915d2001-06-05 20:46:32 +0000603 return prv;
Damien Millereba71ba2000-04-29 23:57:08 +1000604}
Damien Millere4340be2000-09-16 13:29:08 +1100605
Ben Lindstrombba81212001-06-25 05:01:22 +0000606static int
Ben Lindstromd0fca422001-03-26 13:44:06 +0000607key_try_load_public(Key *k, const char *filename, char **commentp)
Damien Millere4340be2000-09-16 13:29:08 +1100608{
609 FILE *f;
Darren Tucker22cc7412004-12-06 22:47:41 +1100610 char line[SSH_MAX_PUBKEY_BYTES];
Damien Millere4340be2000-09-16 13:29:08 +1100611 char *cp;
Darren Tuckerf0f90982004-12-11 13:39:50 +1100612 u_long linenum = 0;
Damien Millere4340be2000-09-16 13:29:08 +1100613
614 f = fopen(filename, "r");
615 if (f != NULL) {
Darren Tucker22cc7412004-12-06 22:47:41 +1100616 while (read_keyfile_line(f, filename, line, sizeof(line),
617 &linenum) != -1) {
Damien Millere4340be2000-09-16 13:29:08 +1100618 cp = line;
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000619 switch (*cp) {
Damien Millere4340be2000-09-16 13:29:08 +1100620 case '#':
621 case '\n':
622 case '\0':
623 continue;
624 }
625 /* Skip leading whitespace. */
626 for (; *cp && (*cp == ' ' || *cp == '\t'); cp++)
627 ;
628 if (*cp) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100629 if (key_read(k, &cp) == 1) {
Damien Millere4340be2000-09-16 13:29:08 +1100630 if (commentp)
631 *commentp=xstrdup(filename);
632 fclose(f);
633 return 1;
634 }
635 }
636 }
637 fclose(f);
638 }
639 return 0;
640}
641
Ben Lindstromd0fca422001-03-26 13:44:06 +0000642/* load public key from ssh v1 private or any pubkey file */
643Key *
644key_load_public(const char *filename, char **commentp)
Damien Millere4340be2000-09-16 13:29:08 +1100645{
Ben Lindstromd0fca422001-03-26 13:44:06 +0000646 Key *pub;
647 char file[MAXPATHLEN];
Damien Millere4340be2000-09-16 13:29:08 +1100648
Damien Millerdb274722003-05-14 13:45:22 +1000649 /* try rsa1 private key */
Ben Lindstromd0fca422001-03-26 13:44:06 +0000650 pub = key_load_public_type(KEY_RSA1, filename, commentp);
651 if (pub != NULL)
652 return pub;
Damien Millerdb274722003-05-14 13:45:22 +1000653
654 /* try rsa1 public key */
655 pub = key_new(KEY_RSA1);
656 if (key_try_load_public(pub, filename, commentp) == 1)
657 return pub;
658 key_free(pub);
659
660 /* try ssh2 public key */
Ben Lindstromd0fca422001-03-26 13:44:06 +0000661 pub = key_new(KEY_UNSPEC);
662 if (key_try_load_public(pub, filename, commentp) == 1)
663 return pub;
664 if ((strlcpy(file, filename, sizeof file) < sizeof(file)) &&
665 (strlcat(file, ".pub", sizeof file) < sizeof(file)) &&
666 (key_try_load_public(pub, file, commentp) == 1))
667 return pub;
668 key_free(pub);
669 return NULL;
Damien Millere4340be2000-09-16 13:29:08 +1100670}