blob: cf3d3d9e1213a296944333cb46442d2061bdbadc [file] [log] [blame]
Darren Tuckerba724052006-07-12 22:24:22 +10001/* $OpenBSD: authfile.c,v 1.69 2006/07/11 20:27:56 stevesk 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
Darren Tuckerba724052006-07-12 22:24:22 +100048#include <errno.h>
Damien Miller57cf6382006-07-10 21:13:46 +100049#include <fcntl.h>
50
Ben Lindstrom226cfa02001-01-22 05:34:40 +000051#include "cipher.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100052#include "xmalloc.h"
53#include "buffer.h"
54#include "bufaux.h"
Damien Millereba71ba2000-04-29 23:57:08 +100055#include "key.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000056#include "ssh.h"
57#include "log.h"
Ben Lindstrom31ca54a2001-02-09 02:11:24 +000058#include "authfile.h"
Damien Miller040b64f2002-01-22 23:10:04 +110059#include "rsa.h"
Darren Tuckerf0f90982004-12-11 13:39:50 +110060#include "misc.h"
Damien Millereccb9de2005-06-17 12:59:34 +100061#include "atomicio.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100062
Ben Lindstromd0fca422001-03-26 13:44:06 +000063/* Version identification string for SSH v1 identity files. */
Ben Lindstrom1170d712001-01-29 07:51:26 +000064static const char authfile_id_string[] =
65 "SSH PRIVATE KEY FILE FORMAT 1.1\n";
Damien Millerd4a8b7e1999-10-27 13:42:43 +100066
Damien Miller5428f641999-11-25 11:54:57 +110067/*
68 * Saves the authentication (private) key in a file, encrypting it with
69 * passphrase. The identification of the file (lowest 64 bits of n) will
70 * precede the key to provide identification of the key without needing a
71 * passphrase.
72 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100073
Ben Lindstrombba81212001-06-25 05:01:22 +000074static int
Ben Lindstromd0fca422001-03-26 13:44:06 +000075key_save_private_rsa1(Key *key, const char *filename, const char *passphrase,
76 const char *comment)
Damien Millerd4a8b7e1999-10-27 13:42:43 +100077{
Damien Miller95def091999-11-25 00:26:21 +110078 Buffer buffer, encrypted;
Damien Miller708d21c2002-01-22 23:18:15 +110079 u_char buf[100], *cp;
Damien Miller963f6b22002-02-19 15:21:23 +110080 int fd, i, cipher_num;
Damien Miller874d77b2000-10-14 16:23:11 +110081 CipherContext ciphercontext;
82 Cipher *cipher;
Darren Tucker3f9fdc72004-06-22 12:56:01 +100083 u_int32_t rnd;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100084
Damien Miller5428f641999-11-25 11:54:57 +110085 /*
86 * If the passphrase is empty, use SSH_CIPHER_NONE to ease converting
87 * to another cipher; otherwise use SSH_AUTHFILE_CIPHER.
88 */
Damien Miller963f6b22002-02-19 15:21:23 +110089 cipher_num = (strcmp(passphrase, "") == 0) ?
90 SSH_CIPHER_NONE : SSH_AUTHFILE_CIPHER;
91 if ((cipher = cipher_by_number(cipher_num)) == NULL)
Damien Miller874d77b2000-10-14 16:23:11 +110092 fatal("save_private_key_rsa: bad cipher");
Damien Millerd4a8b7e1999-10-27 13:42:43 +100093
Damien Miller95def091999-11-25 00:26:21 +110094 /* This buffer is used to built the secret part of the private key. */
95 buffer_init(&buffer);
Damien Millerd4a8b7e1999-10-27 13:42:43 +100096
Damien Miller95def091999-11-25 00:26:21 +110097 /* Put checkbytes for checking passphrase validity. */
Darren Tucker3f9fdc72004-06-22 12:56:01 +100098 rnd = arc4random();
99 buf[0] = rnd & 0xff;
100 buf[1] = (rnd >> 8) & 0xff;
Damien Miller95def091999-11-25 00:26:21 +1100101 buf[2] = buf[0];
102 buf[3] = buf[1];
103 buffer_append(&buffer, buf, 4);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000104
Damien Miller5428f641999-11-25 11:54:57 +1100105 /*
106 * Store the private key (n and e will not be stored because they
107 * will be stored in plain text, and storing them also in encrypted
108 * format would just give known plaintext).
109 */
Ben Lindstromd0fca422001-03-26 13:44:06 +0000110 buffer_put_bignum(&buffer, key->rsa->d);
111 buffer_put_bignum(&buffer, key->rsa->iqmp);
112 buffer_put_bignum(&buffer, key->rsa->q); /* reverse from SSL p */
113 buffer_put_bignum(&buffer, key->rsa->p); /* reverse from SSL q */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000114
Damien Miller95def091999-11-25 00:26:21 +1100115 /* Pad the part to be encrypted until its size is a multiple of 8. */
116 while (buffer_len(&buffer) % 8 != 0)
117 buffer_put_char(&buffer, 0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000118
Damien Miller95def091999-11-25 00:26:21 +1100119 /* This buffer will be used to contain the data in the file. */
120 buffer_init(&encrypted);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000121
Damien Miller95def091999-11-25 00:26:21 +1100122 /* First store keyfile id string. */
Ben Lindstrom1170d712001-01-29 07:51:26 +0000123 for (i = 0; authfile_id_string[i]; i++)
124 buffer_put_char(&encrypted, authfile_id_string[i]);
Damien Miller95def091999-11-25 00:26:21 +1100125 buffer_put_char(&encrypted, 0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000126
Damien Miller95def091999-11-25 00:26:21 +1100127 /* Store cipher type. */
Damien Miller963f6b22002-02-19 15:21:23 +1100128 buffer_put_char(&encrypted, cipher_num);
Damien Miller95def091999-11-25 00:26:21 +1100129 buffer_put_int(&encrypted, 0); /* For future extension */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000130
Damien Miller95def091999-11-25 00:26:21 +1100131 /* Store public key. This will be in plain text. */
Ben Lindstromd0fca422001-03-26 13:44:06 +0000132 buffer_put_int(&encrypted, BN_num_bits(key->rsa->n));
133 buffer_put_bignum(&encrypted, key->rsa->n);
134 buffer_put_bignum(&encrypted, key->rsa->e);
Ben Lindstrom664408d2001-06-09 01:42:01 +0000135 buffer_put_cstring(&encrypted, comment);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000136
Damien Miller95def091999-11-25 00:26:21 +1100137 /* Allocate space for the private part of the key in the buffer. */
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100138 cp = buffer_append_space(&encrypted, buffer_len(&buffer));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000139
Damien Miller963f6b22002-02-19 15:21:23 +1100140 cipher_set_key_string(&ciphercontext, cipher, passphrase,
141 CIPHER_ENCRYPT);
142 cipher_crypt(&ciphercontext, cp,
Damien Miller708d21c2002-01-22 23:18:15 +1100143 buffer_ptr(&buffer), buffer_len(&buffer));
Damien Miller963f6b22002-02-19 15:21:23 +1100144 cipher_cleanup(&ciphercontext);
Damien Miller874d77b2000-10-14 16:23:11 +1100145 memset(&ciphercontext, 0, sizeof(ciphercontext));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000146
Damien Miller95def091999-11-25 00:26:21 +1100147 /* Destroy temporary data. */
148 memset(buf, 0, sizeof(buf));
149 buffer_free(&buffer);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000150
Damien Miller037a0dc1999-12-07 15:38:31 +1100151 fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0600);
Ben Lindstrom15f33862001-04-16 02:00:02 +0000152 if (fd < 0) {
153 error("open %s failed: %s.", filename, strerror(errno));
Darren Tuckerd1d41b32003-09-22 21:01:27 +1000154 buffer_free(&encrypted);
Damien Miller95def091999-11-25 00:26:21 +1100155 return 0;
Ben Lindstrom15f33862001-04-16 02:00:02 +0000156 }
Damien Millereccb9de2005-06-17 12:59:34 +1000157 if (atomicio(vwrite, fd, buffer_ptr(&encrypted),
158 buffer_len(&encrypted)) != buffer_len(&encrypted)) {
Ben Lindstrom15f33862001-04-16 02:00:02 +0000159 error("write to key file %s failed: %s", filename,
Damien Miller9f0f5c62001-12-21 14:45:46 +1100160 strerror(errno));
Damien Miller95def091999-11-25 00:26:21 +1100161 buffer_free(&encrypted);
Damien Miller037a0dc1999-12-07 15:38:31 +1100162 close(fd);
Damien Miller78315eb2000-09-29 23:01:36 +1100163 unlink(filename);
Damien Miller95def091999-11-25 00:26:21 +1100164 return 0;
165 }
Damien Miller037a0dc1999-12-07 15:38:31 +1100166 close(fd);
Damien Miller95def091999-11-25 00:26:21 +1100167 buffer_free(&encrypted);
168 return 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000169}
170
Ben Lindstromd0fca422001-03-26 13:44:06 +0000171/* save SSH v2 key in OpenSSL PEM format */
Ben Lindstrombba81212001-06-25 05:01:22 +0000172static int
Ben Lindstromd0fca422001-03-26 13:44:06 +0000173key_save_private_pem(Key *key, const char *filename, const char *_passphrase,
174 const char *comment)
Damien Millereba71ba2000-04-29 23:57:08 +1000175{
176 FILE *fp;
177 int fd;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100178 int success = 0;
179 int len = strlen(_passphrase);
Ben Lindstrom90fd8142002-02-26 18:09:42 +0000180 u_char *passphrase = (len > 0) ? (u_char *)_passphrase : NULL;
Ben Lindstrom80cb27d2002-03-05 01:33:36 +0000181 const EVP_CIPHER *cipher = (len > 0) ? EVP_des_ede3_cbc() : NULL;
Damien Millereba71ba2000-04-29 23:57:08 +1000182
183 if (len > 0 && len <= 4) {
Ben Lindstrom15f33862001-04-16 02:00:02 +0000184 error("passphrase too short: have %d bytes, need > 4", len);
Damien Millereba71ba2000-04-29 23:57:08 +1000185 return 0;
186 }
187 fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0600);
188 if (fd < 0) {
Ben Lindstrom15f33862001-04-16 02:00:02 +0000189 error("open %s failed: %s.", filename, strerror(errno));
Damien Millereba71ba2000-04-29 23:57:08 +1000190 return 0;
191 }
192 fp = fdopen(fd, "w");
193 if (fp == NULL ) {
Ben Lindstrom15f33862001-04-16 02:00:02 +0000194 error("fdopen %s failed: %s.", filename, strerror(errno));
Damien Millereba71ba2000-04-29 23:57:08 +1000195 close(fd);
196 return 0;
197 }
Damien Miller0bc1bd82000-11-13 22:57:25 +1100198 switch (key->type) {
Ben Lindstromc1116602001-03-29 00:28:37 +0000199 case KEY_DSA:
200 success = PEM_write_DSAPrivateKey(fp, key->dsa,
201 cipher, passphrase, len, NULL, NULL);
202 break;
203 case KEY_RSA:
204 success = PEM_write_RSAPrivateKey(fp, key->rsa,
205 cipher, passphrase, len, NULL, NULL);
206 break;
Damien Millereba71ba2000-04-29 23:57:08 +1000207 }
208 fclose(fp);
209 return success;
210}
211
212int
Ben Lindstromd0fca422001-03-26 13:44:06 +0000213key_save_private(Key *key, const char *filename, const char *passphrase,
Damien Millereba71ba2000-04-29 23:57:08 +1000214 const char *comment)
215{
216 switch (key->type) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100217 case KEY_RSA1:
Ben Lindstromd0fca422001-03-26 13:44:06 +0000218 return key_save_private_rsa1(key, filename, passphrase,
219 comment);
Damien Millereba71ba2000-04-29 23:57:08 +1000220 case KEY_DSA:
Damien Miller0bc1bd82000-11-13 22:57:25 +1100221 case KEY_RSA:
Ben Lindstromd0fca422001-03-26 13:44:06 +0000222 return key_save_private_pem(key, filename, passphrase,
223 comment);
Damien Millereba71ba2000-04-29 23:57:08 +1000224 default:
225 break;
226 }
Ben Lindstrom15f33862001-04-16 02:00:02 +0000227 error("key_save_private: cannot save key type %d", key->type);
Damien Millereba71ba2000-04-29 23:57:08 +1000228 return 0;
229}
230
Damien Miller5428f641999-11-25 11:54:57 +1100231/*
Ben Lindstromd0fca422001-03-26 13:44:06 +0000232 * Loads the public part of the ssh v1 key file. Returns NULL if an error was
233 * encountered (the file does not exist or is not readable), and the key
Damien Miller5428f641999-11-25 11:54:57 +1100234 * otherwise.
235 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000236
Ben Lindstrombba81212001-06-25 05:01:22 +0000237static Key *
Ben Lindstromd0fca422001-03-26 13:44:06 +0000238key_load_public_rsa1(int fd, const char *filename, char **commentp)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000239{
Damien Miller95def091999-11-25 00:26:21 +1100240 Buffer buffer;
Ben Lindstromd0fca422001-03-26 13:44:06 +0000241 Key *pub;
Ben Lindstrom44adb8f2002-12-23 02:00:23 +0000242 struct stat st;
Damien Miller95def091999-11-25 00:26:21 +1100243 char *cp;
Damien Millereccb9de2005-06-17 12:59:34 +1000244 u_int i;
Darren Tucker1f8311c2004-05-13 16:39:33 +1000245 size_t len;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000246
Ben Lindstrom44adb8f2002-12-23 02:00:23 +0000247 if (fstat(fd, &st) < 0) {
248 error("fstat for key file %.200s failed: %.100s",
249 filename, strerror(errno));
250 return NULL;
251 }
Darren Tuckerf4b43712004-08-29 16:28:39 +1000252 if (st.st_size > 1*1024*1024) {
253 error("key file %.200s too large", filename);
254 return NULL;
255 }
Darren Tucker1f8311c2004-05-13 16:39:33 +1000256 len = (size_t)st.st_size; /* truncated */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000257
Damien Miller95def091999-11-25 00:26:21 +1100258 buffer_init(&buffer);
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100259 cp = buffer_append_space(&buffer, len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000260
Damien Millereccb9de2005-06-17 12:59:34 +1000261 if (atomicio(read, fd, cp, len) != len) {
Damien Miller95def091999-11-25 00:26:21 +1100262 debug("Read from key file %.200s failed: %.100s", filename,
Damien Millereba71ba2000-04-29 23:57:08 +1000263 strerror(errno));
Damien Miller95def091999-11-25 00:26:21 +1100264 buffer_free(&buffer);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000265 return NULL;
Damien Miller95def091999-11-25 00:26:21 +1100266 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000267
Ben Lindstrom1170d712001-01-29 07:51:26 +0000268 /* Check that it is at least big enough to contain the ID string. */
269 if (len < sizeof(authfile_id_string)) {
Damien Miller058655c2001-10-10 15:03:36 +1000270 debug3("Not a RSA1 key file %.200s.", filename);
Damien Miller95def091999-11-25 00:26:21 +1100271 buffer_free(&buffer);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000272 return NULL;
Damien Miller95def091999-11-25 00:26:21 +1100273 }
Damien Miller5428f641999-11-25 11:54:57 +1100274 /*
275 * Make sure it begins with the id string. Consume the id string
276 * from the buffer.
277 */
Ben Lindstrom1170d712001-01-29 07:51:26 +0000278 for (i = 0; i < sizeof(authfile_id_string); i++)
279 if (buffer_get_char(&buffer) != authfile_id_string[i]) {
Damien Miller058655c2001-10-10 15:03:36 +1000280 debug3("Not a RSA1 key file %.200s.", filename);
Damien Miller95def091999-11-25 00:26:21 +1100281 buffer_free(&buffer);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000282 return NULL;
Damien Miller95def091999-11-25 00:26:21 +1100283 }
284 /* Skip cipher type and reserved data. */
285 (void) buffer_get_char(&buffer); /* cipher type */
286 (void) buffer_get_int(&buffer); /* reserved */
287
288 /* Read the public key from the buffer. */
Ben Lindstromc5a7f4f2002-06-25 23:19:13 +0000289 (void) buffer_get_int(&buffer);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000290 pub = key_new(KEY_RSA1);
291 buffer_get_bignum(&buffer, pub->rsa->n);
292 buffer_get_bignum(&buffer, pub->rsa->e);
293 if (commentp)
294 *commentp = buffer_get_string(&buffer, NULL);
Damien Miller95def091999-11-25 00:26:21 +1100295 /* The encrypted private part is not parsed by this function. */
296
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000297 buffer_free(&buffer);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000298 return pub;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000299}
300
Ben Lindstromd0fca422001-03-26 13:44:06 +0000301/* load public key from private-key file, works only for SSH v1 */
302Key *
303key_load_public_type(int type, const char *filename, char **commentp)
Damien Millereba71ba2000-04-29 23:57:08 +1000304{
Ben Lindstromd0fca422001-03-26 13:44:06 +0000305 Key *pub;
306 int fd;
307
308 if (type == KEY_RSA1) {
309 fd = open(filename, O_RDONLY);
310 if (fd < 0)
311 return NULL;
312 pub = key_load_public_rsa1(fd, filename, commentp);
313 close(fd);
314 return pub;
Damien Millereba71ba2000-04-29 23:57:08 +1000315 }
Ben Lindstromd0fca422001-03-26 13:44:06 +0000316 return NULL;
Damien Millereba71ba2000-04-29 23:57:08 +1000317}
318
Damien Miller5428f641999-11-25 11:54:57 +1100319/*
320 * Loads the private key from the file. Returns 0 if an error is encountered
321 * (file does not exist or is not readable, or passphrase is bad). This
322 * initializes the private key.
323 * Assumes we are called under uid of the owner of the file.
324 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000325
Ben Lindstrombba81212001-06-25 05:01:22 +0000326static Key *
Ben Lindstromd0fca422001-03-26 13:44:06 +0000327key_load_private_rsa1(int fd, const char *filename, const char *passphrase,
328 char **commentp)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000329{
Damien Millereccb9de2005-06-17 12:59:34 +1000330 u_int i;
331 int check1, check2, cipher_type;
Darren Tucker1f8311c2004-05-13 16:39:33 +1000332 size_t len;
Damien Miller95def091999-11-25 00:26:21 +1100333 Buffer buffer, decrypted;
Damien Miller708d21c2002-01-22 23:18:15 +1100334 u_char *cp;
Damien Miller874d77b2000-10-14 16:23:11 +1100335 CipherContext ciphercontext;
336 Cipher *cipher;
Ben Lindstromd0fca422001-03-26 13:44:06 +0000337 Key *prv = NULL;
Ben Lindstrom44adb8f2002-12-23 02:00:23 +0000338 struct stat st;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000339
Ben Lindstrom44adb8f2002-12-23 02:00:23 +0000340 if (fstat(fd, &st) < 0) {
341 error("fstat for key file %.200s failed: %.100s",
342 filename, strerror(errno));
343 close(fd);
344 return NULL;
345 }
Darren Tucker1f8311c2004-05-13 16:39:33 +1000346 if (st.st_size > 1*1024*1024) {
Darren Tuckerf4b43712004-08-29 16:28:39 +1000347 error("key file %.200s too large", filename);
Darren Tucker1f8311c2004-05-13 16:39:33 +1000348 close(fd);
349 return (NULL);
350 }
351 len = (size_t)st.st_size; /* truncated */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000352
Damien Miller95def091999-11-25 00:26:21 +1100353 buffer_init(&buffer);
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100354 cp = buffer_append_space(&buffer, len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000355
Damien Millereccb9de2005-06-17 12:59:34 +1000356 if (atomicio(read, fd, cp, len) != len) {
Damien Miller95def091999-11-25 00:26:21 +1100357 debug("Read from key file %.200s failed: %.100s", filename,
Damien Miller0bc1bd82000-11-13 22:57:25 +1100358 strerror(errno));
Damien Miller95def091999-11-25 00:26:21 +1100359 buffer_free(&buffer);
Damien Miller037a0dc1999-12-07 15:38:31 +1100360 close(fd);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000361 return NULL;
Damien Miller95def091999-11-25 00:26:21 +1100362 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000363
Ben Lindstrom1170d712001-01-29 07:51:26 +0000364 /* Check that it is at least big enough to contain the ID string. */
365 if (len < sizeof(authfile_id_string)) {
Damien Miller058655c2001-10-10 15:03:36 +1000366 debug3("Not a RSA1 key file %.200s.", filename);
Damien Miller95def091999-11-25 00:26:21 +1100367 buffer_free(&buffer);
Ben Lindstromb257cca2001-03-05 04:59:27 +0000368 close(fd);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000369 return NULL;
Damien Miller95def091999-11-25 00:26:21 +1100370 }
Damien Miller5428f641999-11-25 11:54:57 +1100371 /*
372 * Make sure it begins with the id string. Consume the id string
373 * from the buffer.
374 */
Ben Lindstrom1170d712001-01-29 07:51:26 +0000375 for (i = 0; i < sizeof(authfile_id_string); i++)
376 if (buffer_get_char(&buffer) != authfile_id_string[i]) {
Damien Miller058655c2001-10-10 15:03:36 +1000377 debug3("Not a RSA1 key file %.200s.", filename);
Damien Miller95def091999-11-25 00:26:21 +1100378 buffer_free(&buffer);
Ben Lindstromb257cca2001-03-05 04:59:27 +0000379 close(fd);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000380 return NULL;
Damien Miller95def091999-11-25 00:26:21 +1100381 }
Ben Lindstromb257cca2001-03-05 04:59:27 +0000382
Damien Miller95def091999-11-25 00:26:21 +1100383 /* Read cipher type. */
384 cipher_type = buffer_get_char(&buffer);
385 (void) buffer_get_int(&buffer); /* Reserved data. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000386
Damien Miller95def091999-11-25 00:26:21 +1100387 /* Read the public key from the buffer. */
Ben Lindstromc5a7f4f2002-06-25 23:19:13 +0000388 (void) buffer_get_int(&buffer);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000389 prv = key_new_private(KEY_RSA1);
390
391 buffer_get_bignum(&buffer, prv->rsa->n);
392 buffer_get_bignum(&buffer, prv->rsa->e);
393 if (commentp)
394 *commentp = buffer_get_string(&buffer, NULL);
Damien Miller95def091999-11-25 00:26:21 +1100395 else
396 xfree(buffer_get_string(&buffer, NULL));
397
398 /* Check that it is a supported cipher. */
Damien Miller874d77b2000-10-14 16:23:11 +1100399 cipher = cipher_by_number(cipher_type);
400 if (cipher == NULL) {
401 debug("Unsupported cipher %d used in key file %.200s.",
402 cipher_type, filename);
Damien Miller95def091999-11-25 00:26:21 +1100403 buffer_free(&buffer);
404 goto fail;
405 }
406 /* Initialize space for decrypted data. */
407 buffer_init(&decrypted);
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100408 cp = buffer_append_space(&decrypted, buffer_len(&buffer));
Damien Miller95def091999-11-25 00:26:21 +1100409
410 /* Rest of the buffer is encrypted. Decrypt it using the passphrase. */
Damien Miller963f6b22002-02-19 15:21:23 +1100411 cipher_set_key_string(&ciphercontext, cipher, passphrase,
412 CIPHER_DECRYPT);
413 cipher_crypt(&ciphercontext, cp,
Damien Miller708d21c2002-01-22 23:18:15 +1100414 buffer_ptr(&buffer), buffer_len(&buffer));
Damien Miller963f6b22002-02-19 15:21:23 +1100415 cipher_cleanup(&ciphercontext);
Damien Miller874d77b2000-10-14 16:23:11 +1100416 memset(&ciphercontext, 0, sizeof(ciphercontext));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000417 buffer_free(&buffer);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000418
Damien Miller95def091999-11-25 00:26:21 +1100419 check1 = buffer_get_char(&decrypted);
420 check2 = buffer_get_char(&decrypted);
421 if (check1 != buffer_get_char(&decrypted) ||
422 check2 != buffer_get_char(&decrypted)) {
423 if (strcmp(passphrase, "") != 0)
Ben Lindstromd0fca422001-03-26 13:44:06 +0000424 debug("Bad passphrase supplied for key file %.200s.",
425 filename);
Damien Miller95def091999-11-25 00:26:21 +1100426 /* Bad passphrase. */
427 buffer_free(&decrypted);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000428 goto fail;
Damien Miller95def091999-11-25 00:26:21 +1100429 }
430 /* Read the rest of the private key. */
Ben Lindstromd0fca422001-03-26 13:44:06 +0000431 buffer_get_bignum(&decrypted, prv->rsa->d);
432 buffer_get_bignum(&decrypted, prv->rsa->iqmp); /* u */
433 /* in SSL and SSH v1 p and q are exchanged */
434 buffer_get_bignum(&decrypted, prv->rsa->q); /* p */
435 buffer_get_bignum(&decrypted, prv->rsa->p); /* q */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000436
Ben Lindstromd0fca422001-03-26 13:44:06 +0000437 /* calculate p-1 and q-1 */
Damien Millerda755162002-01-22 23:09:22 +1100438 rsa_generate_additional_parameters(prv->rsa);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000439
Damien Miller95def091999-11-25 00:26:21 +1100440 buffer_free(&decrypted);
Damien Millered33d3b2003-03-15 11:36:18 +1100441
442 /* enable blinding */
443 if (RSA_blinding_on(prv->rsa, NULL) != 1) {
444 error("key_load_private_rsa1: RSA_blinding_on failed");
445 goto fail;
446 }
Ben Lindstromb257cca2001-03-05 04:59:27 +0000447 close(fd);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000448 return prv;
449
450fail:
451 if (commentp)
452 xfree(*commentp);
453 close(fd);
454 key_free(prv);
455 return NULL;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000456}
Damien Millereba71ba2000-04-29 23:57:08 +1000457
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000458Key *
Ben Lindstromd0fca422001-03-26 13:44:06 +0000459key_load_private_pem(int fd, int type, const char *passphrase,
460 char **commentp)
Damien Millereba71ba2000-04-29 23:57:08 +1000461{
Damien Millereba71ba2000-04-29 23:57:08 +1000462 FILE *fp;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100463 EVP_PKEY *pk = NULL;
Ben Lindstromd0fca422001-03-26 13:44:06 +0000464 Key *prv = NULL;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100465 char *name = "<no key>";
Damien Millereba71ba2000-04-29 23:57:08 +1000466
Damien Millereba71ba2000-04-29 23:57:08 +1000467 fp = fdopen(fd, "r");
468 if (fp == NULL) {
Ben Lindstrom15f33862001-04-16 02:00:02 +0000469 error("fdopen failed: %s", strerror(errno));
Ben Lindstromb257cca2001-03-05 04:59:27 +0000470 close(fd);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000471 return NULL;
Damien Millereba71ba2000-04-29 23:57:08 +1000472 }
Damien Miller0bc1bd82000-11-13 22:57:25 +1100473 pk = PEM_read_PrivateKey(fp, NULL, NULL, (char *)passphrase);
474 if (pk == NULL) {
Ben Lindstrom648772f2001-04-19 20:47:10 +0000475 debug("PEM_read_PrivateKey failed");
Damien Miller0bc1bd82000-11-13 22:57:25 +1100476 (void)ERR_get_error();
Ben Lindstromd0fca422001-03-26 13:44:06 +0000477 } else if (pk->type == EVP_PKEY_RSA &&
Damien Miller9f0f5c62001-12-21 14:45:46 +1100478 (type == KEY_UNSPEC||type==KEY_RSA)) {
Ben Lindstromd0fca422001-03-26 13:44:06 +0000479 prv = key_new(KEY_UNSPEC);
480 prv->rsa = EVP_PKEY_get1_RSA(pk);
481 prv->type = KEY_RSA;
482 name = "rsa w/o comment";
Damien Miller0bc1bd82000-11-13 22:57:25 +1100483#ifdef DEBUG_PK
Ben Lindstromd0fca422001-03-26 13:44:06 +0000484 RSA_print_fp(stderr, prv->rsa, 8);
Damien Millereba71ba2000-04-29 23:57:08 +1000485#endif
Damien Millered33d3b2003-03-15 11:36:18 +1100486 if (RSA_blinding_on(prv->rsa, NULL) != 1) {
487 error("key_load_private_pem: RSA_blinding_on failed");
488 key_free(prv);
489 prv = NULL;
490 }
Ben Lindstromd0fca422001-03-26 13:44:06 +0000491 } else if (pk->type == EVP_PKEY_DSA &&
Damien Miller9f0f5c62001-12-21 14:45:46 +1100492 (type == KEY_UNSPEC||type==KEY_DSA)) {
Ben Lindstromd0fca422001-03-26 13:44:06 +0000493 prv = key_new(KEY_UNSPEC);
494 prv->dsa = EVP_PKEY_get1_DSA(pk);
495 prv->type = KEY_DSA;
496 name = "dsa w/o comment";
Damien Miller0bc1bd82000-11-13 22:57:25 +1100497#ifdef DEBUG_PK
Ben Lindstromd0fca422001-03-26 13:44:06 +0000498 DSA_print_fp(stderr, prv->dsa, 8);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100499#endif
Damien Miller0bc1bd82000-11-13 22:57:25 +1100500 } else {
501 error("PEM_read_PrivateKey: mismatch or "
502 "unknown EVP_PKEY save_type %d", pk->save_type);
503 }
504 fclose(fp);
505 if (pk != NULL)
506 EVP_PKEY_free(pk);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000507 if (prv != NULL && commentp)
508 *commentp = xstrdup(name);
509 debug("read PEM private key done: type %s",
510 prv ? key_type(prv) : "<unknown>");
511 return prv;
Damien Millereba71ba2000-04-29 23:57:08 +1000512}
513
Damien Miller8275fad2006-03-15 12:06:23 +1100514int
Ben Lindstromd0fca422001-03-26 13:44:06 +0000515key_perm_ok(int fd, const char *filename)
Damien Millereba71ba2000-04-29 23:57:08 +1000516{
Damien Millereba71ba2000-04-29 23:57:08 +1000517 struct stat st;
518
Ben Lindstrom7aff2612001-09-23 13:53:22 +0000519 if (fstat(fd, &st) < 0)
520 return 0;
521 /*
522 * if a key owned by the user is accessed, then we check the
523 * permissions of the file. if the key owned by a different user,
524 * then we don't care.
525 */
Damien Millerb70b61f2000-09-16 16:25:12 +1100526#ifdef HAVE_CYGWIN
Damien Millercb5e44a2000-09-29 12:12:36 +1100527 if (check_ntsec(filename))
Damien Millerb70b61f2000-09-16 16:25:12 +1100528#endif
Ben Lindstrom7aff2612001-09-23 13:53:22 +0000529 if ((st.st_uid == getuid()) && (st.st_mode & 077) != 0) {
Damien Millereba71ba2000-04-29 23:57:08 +1000530 error("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
531 error("@ WARNING: UNPROTECTED PRIVATE KEY FILE! @");
532 error("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
Ben Lindstrom7aff2612001-09-23 13:53:22 +0000533 error("Permissions 0%3.3o for '%s' are too open.",
Damien Miller04bd8b02003-05-25 14:38:33 +1000534 (u_int)st.st_mode & 0777, filename);
Damien Millereba71ba2000-04-29 23:57:08 +1000535 error("It is recommended that your private key files are NOT accessible by others.");
Ben Lindstromd0fca422001-03-26 13:44:06 +0000536 error("This private key will be ignored.");
Damien Millereba71ba2000-04-29 23:57:08 +1000537 return 0;
538 }
Ben Lindstromd0fca422001-03-26 13:44:06 +0000539 return 1;
540}
Ben Lindstromb257cca2001-03-05 04:59:27 +0000541
Ben Lindstromd0fca422001-03-26 13:44:06 +0000542Key *
543key_load_private_type(int type, const char *filename, const char *passphrase,
Darren Tucker232b76f2006-05-06 17:41:51 +1000544 char **commentp, int *perm_ok)
Ben Lindstromd0fca422001-03-26 13:44:06 +0000545{
546 int fd;
547
548 fd = open(filename, O_RDONLY);
549 if (fd < 0)
550 return NULL;
551 if (!key_perm_ok(fd, filename)) {
Darren Tucker232b76f2006-05-06 17:41:51 +1000552 if (perm_ok != NULL)
553 *perm_ok = 0;
Ben Lindstrom15f33862001-04-16 02:00:02 +0000554 error("bad permissions: ignore key: %s", filename);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000555 close(fd);
556 return NULL;
557 }
Darren Tucker232b76f2006-05-06 17:41:51 +1000558 if (perm_ok != NULL)
559 *perm_ok = 1;
Ben Lindstromd0fca422001-03-26 13:44:06 +0000560 switch (type) {
561 case KEY_RSA1:
562 return key_load_private_rsa1(fd, filename, passphrase,
563 commentp);
564 /* closes fd */
Damien Millereba71ba2000-04-29 23:57:08 +1000565 case KEY_DSA:
Damien Miller0bc1bd82000-11-13 22:57:25 +1100566 case KEY_RSA:
567 case KEY_UNSPEC:
Ben Lindstromd0fca422001-03-26 13:44:06 +0000568 return key_load_private_pem(fd, type, passphrase, commentp);
569 /* closes fd */
Damien Millereba71ba2000-04-29 23:57:08 +1000570 default:
Ben Lindstromb257cca2001-03-05 04:59:27 +0000571 close(fd);
Damien Millereba71ba2000-04-29 23:57:08 +1000572 break;
573 }
Ben Lindstromd0fca422001-03-26 13:44:06 +0000574 return NULL;
575}
576
577Key *
578key_load_private(const char *filename, const char *passphrase,
579 char **commentp)
580{
Ben Lindstrom322915d2001-06-05 20:46:32 +0000581 Key *pub, *prv;
Ben Lindstromd0fca422001-03-26 13:44:06 +0000582 int fd;
583
584 fd = open(filename, O_RDONLY);
585 if (fd < 0)
586 return NULL;
587 if (!key_perm_ok(fd, filename)) {
Ben Lindstrom15f33862001-04-16 02:00:02 +0000588 error("bad permissions: ignore key: %s", filename);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000589 close(fd);
590 return NULL;
591 }
592 pub = key_load_public_rsa1(fd, filename, commentp);
593 lseek(fd, (off_t) 0, SEEK_SET); /* rewind */
594 if (pub == NULL) {
595 /* closes fd */
Ben Lindstrom322915d2001-06-05 20:46:32 +0000596 prv = key_load_private_pem(fd, KEY_UNSPEC, passphrase, NULL);
597 /* use the filename as a comment for PEM */
598 if (commentp && prv)
Ben Lindstrom2d0356f2001-06-05 21:13:57 +0000599 *commentp = xstrdup(filename);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000600 } else {
601 /* it's a SSH v1 key if the public key part is readable */
602 key_free(pub);
603 /* closes fd */
Ben Lindstrom322915d2001-06-05 20:46:32 +0000604 prv = key_load_private_rsa1(fd, filename, passphrase, NULL);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000605 }
Ben Lindstrom322915d2001-06-05 20:46:32 +0000606 return prv;
Damien Millereba71ba2000-04-29 23:57:08 +1000607}
Damien Millere4340be2000-09-16 13:29:08 +1100608
Ben Lindstrombba81212001-06-25 05:01:22 +0000609static int
Ben Lindstromd0fca422001-03-26 13:44:06 +0000610key_try_load_public(Key *k, const char *filename, char **commentp)
Damien Millere4340be2000-09-16 13:29:08 +1100611{
612 FILE *f;
Darren Tucker22cc7412004-12-06 22:47:41 +1100613 char line[SSH_MAX_PUBKEY_BYTES];
Damien Millere4340be2000-09-16 13:29:08 +1100614 char *cp;
Darren Tuckerf0f90982004-12-11 13:39:50 +1100615 u_long linenum = 0;
Damien Millere4340be2000-09-16 13:29:08 +1100616
617 f = fopen(filename, "r");
618 if (f != NULL) {
Darren Tucker22cc7412004-12-06 22:47:41 +1100619 while (read_keyfile_line(f, filename, line, sizeof(line),
620 &linenum) != -1) {
Damien Millere4340be2000-09-16 13:29:08 +1100621 cp = line;
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000622 switch (*cp) {
Damien Millere4340be2000-09-16 13:29:08 +1100623 case '#':
624 case '\n':
625 case '\0':
626 continue;
627 }
628 /* Skip leading whitespace. */
629 for (; *cp && (*cp == ' ' || *cp == '\t'); cp++)
630 ;
631 if (*cp) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100632 if (key_read(k, &cp) == 1) {
Damien Millere4340be2000-09-16 13:29:08 +1100633 if (commentp)
634 *commentp=xstrdup(filename);
635 fclose(f);
636 return 1;
637 }
638 }
639 }
640 fclose(f);
641 }
642 return 0;
643}
644
Ben Lindstromd0fca422001-03-26 13:44:06 +0000645/* load public key from ssh v1 private or any pubkey file */
646Key *
647key_load_public(const char *filename, char **commentp)
Damien Millere4340be2000-09-16 13:29:08 +1100648{
Ben Lindstromd0fca422001-03-26 13:44:06 +0000649 Key *pub;
650 char file[MAXPATHLEN];
Damien Millere4340be2000-09-16 13:29:08 +1100651
Damien Millerdb274722003-05-14 13:45:22 +1000652 /* try rsa1 private key */
Ben Lindstromd0fca422001-03-26 13:44:06 +0000653 pub = key_load_public_type(KEY_RSA1, filename, commentp);
654 if (pub != NULL)
655 return pub;
Damien Millerdb274722003-05-14 13:45:22 +1000656
657 /* try rsa1 public key */
658 pub = key_new(KEY_RSA1);
659 if (key_try_load_public(pub, filename, commentp) == 1)
660 return pub;
661 key_free(pub);
662
663 /* try ssh2 public key */
Ben Lindstromd0fca422001-03-26 13:44:06 +0000664 pub = key_new(KEY_UNSPEC);
665 if (key_try_load_public(pub, filename, commentp) == 1)
666 return pub;
667 if ((strlcpy(file, filename, sizeof file) < sizeof(file)) &&
668 (strlcat(file, ".pub", sizeof file) < sizeof(file)) &&
669 (key_try_load_public(pub, file, commentp) == 1))
670 return pub;
671 key_free(pub);
672 return NULL;
Damien Millere4340be2000-09-16 13:29:08 +1100673}