blob: 8421665b88786617d65e14c843564e9800f13731 [file] [log] [blame]
Damien Millere3476ed2006-07-24 14:13:33 +10001/* $OpenBSD: authfile.c,v 1.71 2006/07/22 20:48:22 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>
Damien Millere3476ed2006-07-24 14:13:33 +100050#include <string.h>
Damien Millere6b3b612006-07-24 14:01:23 +100051#include <unistd.h>
Damien Miller57cf6382006-07-10 21:13:46 +100052
Ben Lindstrom226cfa02001-01-22 05:34:40 +000053#include "cipher.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100054#include "xmalloc.h"
55#include "buffer.h"
56#include "bufaux.h"
Damien Millereba71ba2000-04-29 23:57:08 +100057#include "key.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000058#include "ssh.h"
59#include "log.h"
Ben Lindstrom31ca54a2001-02-09 02:11:24 +000060#include "authfile.h"
Damien Miller040b64f2002-01-22 23:10:04 +110061#include "rsa.h"
Darren Tuckerf0f90982004-12-11 13:39:50 +110062#include "misc.h"
Damien Millereccb9de2005-06-17 12:59:34 +100063#include "atomicio.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100064
Ben Lindstromd0fca422001-03-26 13:44:06 +000065/* Version identification string for SSH v1 identity files. */
Ben Lindstrom1170d712001-01-29 07:51:26 +000066static const char authfile_id_string[] =
67 "SSH PRIVATE KEY FILE FORMAT 1.1\n";
Damien Millerd4a8b7e1999-10-27 13:42:43 +100068
Damien Miller5428f641999-11-25 11:54:57 +110069/*
70 * Saves the authentication (private) key in a file, encrypting it with
71 * passphrase. The identification of the file (lowest 64 bits of n) will
72 * precede the key to provide identification of the key without needing a
73 * passphrase.
74 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100075
Ben Lindstrombba81212001-06-25 05:01:22 +000076static int
Ben Lindstromd0fca422001-03-26 13:44:06 +000077key_save_private_rsa1(Key *key, const char *filename, const char *passphrase,
78 const char *comment)
Damien Millerd4a8b7e1999-10-27 13:42:43 +100079{
Damien Miller95def091999-11-25 00:26:21 +110080 Buffer buffer, encrypted;
Damien Miller708d21c2002-01-22 23:18:15 +110081 u_char buf[100], *cp;
Damien Miller963f6b22002-02-19 15:21:23 +110082 int fd, i, cipher_num;
Damien Miller874d77b2000-10-14 16:23:11 +110083 CipherContext ciphercontext;
84 Cipher *cipher;
Darren Tucker3f9fdc72004-06-22 12:56:01 +100085 u_int32_t rnd;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100086
Damien Miller5428f641999-11-25 11:54:57 +110087 /*
88 * If the passphrase is empty, use SSH_CIPHER_NONE to ease converting
89 * to another cipher; otherwise use SSH_AUTHFILE_CIPHER.
90 */
Damien Miller963f6b22002-02-19 15:21:23 +110091 cipher_num = (strcmp(passphrase, "") == 0) ?
92 SSH_CIPHER_NONE : SSH_AUTHFILE_CIPHER;
93 if ((cipher = cipher_by_number(cipher_num)) == NULL)
Damien Miller874d77b2000-10-14 16:23:11 +110094 fatal("save_private_key_rsa: bad cipher");
Damien Millerd4a8b7e1999-10-27 13:42:43 +100095
Damien Miller95def091999-11-25 00:26:21 +110096 /* This buffer is used to built the secret part of the private key. */
97 buffer_init(&buffer);
Damien Millerd4a8b7e1999-10-27 13:42:43 +100098
Damien Miller95def091999-11-25 00:26:21 +110099 /* Put checkbytes for checking passphrase validity. */
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000100 rnd = arc4random();
101 buf[0] = rnd & 0xff;
102 buf[1] = (rnd >> 8) & 0xff;
Damien Miller95def091999-11-25 00:26:21 +1100103 buf[2] = buf[0];
104 buf[3] = buf[1];
105 buffer_append(&buffer, buf, 4);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000106
Damien Miller5428f641999-11-25 11:54:57 +1100107 /*
108 * Store the private key (n and e will not be stored because they
109 * will be stored in plain text, and storing them also in encrypted
110 * format would just give known plaintext).
111 */
Ben Lindstromd0fca422001-03-26 13:44:06 +0000112 buffer_put_bignum(&buffer, key->rsa->d);
113 buffer_put_bignum(&buffer, key->rsa->iqmp);
114 buffer_put_bignum(&buffer, key->rsa->q); /* reverse from SSL p */
115 buffer_put_bignum(&buffer, key->rsa->p); /* reverse from SSL q */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000116
Damien Miller95def091999-11-25 00:26:21 +1100117 /* Pad the part to be encrypted until its size is a multiple of 8. */
118 while (buffer_len(&buffer) % 8 != 0)
119 buffer_put_char(&buffer, 0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000120
Damien Miller95def091999-11-25 00:26:21 +1100121 /* This buffer will be used to contain the data in the file. */
122 buffer_init(&encrypted);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000123
Damien Miller95def091999-11-25 00:26:21 +1100124 /* First store keyfile id string. */
Ben Lindstrom1170d712001-01-29 07:51:26 +0000125 for (i = 0; authfile_id_string[i]; i++)
126 buffer_put_char(&encrypted, authfile_id_string[i]);
Damien Miller95def091999-11-25 00:26:21 +1100127 buffer_put_char(&encrypted, 0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000128
Damien Miller95def091999-11-25 00:26:21 +1100129 /* Store cipher type. */
Damien Miller963f6b22002-02-19 15:21:23 +1100130 buffer_put_char(&encrypted, cipher_num);
Damien Miller95def091999-11-25 00:26:21 +1100131 buffer_put_int(&encrypted, 0); /* For future extension */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000132
Damien Miller95def091999-11-25 00:26:21 +1100133 /* Store public key. This will be in plain text. */
Ben Lindstromd0fca422001-03-26 13:44:06 +0000134 buffer_put_int(&encrypted, BN_num_bits(key->rsa->n));
135 buffer_put_bignum(&encrypted, key->rsa->n);
136 buffer_put_bignum(&encrypted, key->rsa->e);
Ben Lindstrom664408d2001-06-09 01:42:01 +0000137 buffer_put_cstring(&encrypted, comment);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000138
Damien Miller95def091999-11-25 00:26:21 +1100139 /* Allocate space for the private part of the key in the buffer. */
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100140 cp = buffer_append_space(&encrypted, buffer_len(&buffer));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000141
Damien Miller963f6b22002-02-19 15:21:23 +1100142 cipher_set_key_string(&ciphercontext, cipher, passphrase,
143 CIPHER_ENCRYPT);
144 cipher_crypt(&ciphercontext, cp,
Damien Miller708d21c2002-01-22 23:18:15 +1100145 buffer_ptr(&buffer), buffer_len(&buffer));
Damien Miller963f6b22002-02-19 15:21:23 +1100146 cipher_cleanup(&ciphercontext);
Damien Miller874d77b2000-10-14 16:23:11 +1100147 memset(&ciphercontext, 0, sizeof(ciphercontext));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000148
Damien Miller95def091999-11-25 00:26:21 +1100149 /* Destroy temporary data. */
150 memset(buf, 0, sizeof(buf));
151 buffer_free(&buffer);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000152
Damien Miller037a0dc1999-12-07 15:38:31 +1100153 fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0600);
Ben Lindstrom15f33862001-04-16 02:00:02 +0000154 if (fd < 0) {
155 error("open %s failed: %s.", filename, strerror(errno));
Darren Tuckerd1d41b32003-09-22 21:01:27 +1000156 buffer_free(&encrypted);
Damien Miller95def091999-11-25 00:26:21 +1100157 return 0;
Ben Lindstrom15f33862001-04-16 02:00:02 +0000158 }
Damien Millereccb9de2005-06-17 12:59:34 +1000159 if (atomicio(vwrite, fd, buffer_ptr(&encrypted),
160 buffer_len(&encrypted)) != buffer_len(&encrypted)) {
Ben Lindstrom15f33862001-04-16 02:00:02 +0000161 error("write to key file %s failed: %s", filename,
Damien Miller9f0f5c62001-12-21 14:45:46 +1100162 strerror(errno));
Damien Miller95def091999-11-25 00:26:21 +1100163 buffer_free(&encrypted);
Damien Miller037a0dc1999-12-07 15:38:31 +1100164 close(fd);
Damien Miller78315eb2000-09-29 23:01:36 +1100165 unlink(filename);
Damien Miller95def091999-11-25 00:26:21 +1100166 return 0;
167 }
Damien Miller037a0dc1999-12-07 15:38:31 +1100168 close(fd);
Damien Miller95def091999-11-25 00:26:21 +1100169 buffer_free(&encrypted);
170 return 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000171}
172
Ben Lindstromd0fca422001-03-26 13:44:06 +0000173/* save SSH v2 key in OpenSSL PEM format */
Ben Lindstrombba81212001-06-25 05:01:22 +0000174static int
Ben Lindstromd0fca422001-03-26 13:44:06 +0000175key_save_private_pem(Key *key, const char *filename, const char *_passphrase,
176 const char *comment)
Damien Millereba71ba2000-04-29 23:57:08 +1000177{
178 FILE *fp;
179 int fd;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100180 int success = 0;
181 int len = strlen(_passphrase);
Ben Lindstrom90fd8142002-02-26 18:09:42 +0000182 u_char *passphrase = (len > 0) ? (u_char *)_passphrase : NULL;
Ben Lindstrom80cb27d2002-03-05 01:33:36 +0000183 const EVP_CIPHER *cipher = (len > 0) ? EVP_des_ede3_cbc() : NULL;
Damien Millereba71ba2000-04-29 23:57:08 +1000184
185 if (len > 0 && len <= 4) {
Ben Lindstrom15f33862001-04-16 02:00:02 +0000186 error("passphrase too short: have %d bytes, need > 4", len);
Damien Millereba71ba2000-04-29 23:57:08 +1000187 return 0;
188 }
189 fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0600);
190 if (fd < 0) {
Ben Lindstrom15f33862001-04-16 02:00:02 +0000191 error("open %s failed: %s.", filename, strerror(errno));
Damien Millereba71ba2000-04-29 23:57:08 +1000192 return 0;
193 }
194 fp = fdopen(fd, "w");
195 if (fp == NULL ) {
Ben Lindstrom15f33862001-04-16 02:00:02 +0000196 error("fdopen %s failed: %s.", filename, strerror(errno));
Damien Millereba71ba2000-04-29 23:57:08 +1000197 close(fd);
198 return 0;
199 }
Damien Miller0bc1bd82000-11-13 22:57:25 +1100200 switch (key->type) {
Ben Lindstromc1116602001-03-29 00:28:37 +0000201 case KEY_DSA:
202 success = PEM_write_DSAPrivateKey(fp, key->dsa,
203 cipher, passphrase, len, NULL, NULL);
204 break;
205 case KEY_RSA:
206 success = PEM_write_RSAPrivateKey(fp, key->rsa,
207 cipher, passphrase, len, NULL, NULL);
208 break;
Damien Millereba71ba2000-04-29 23:57:08 +1000209 }
210 fclose(fp);
211 return success;
212}
213
214int
Ben Lindstromd0fca422001-03-26 13:44:06 +0000215key_save_private(Key *key, const char *filename, const char *passphrase,
Damien Millereba71ba2000-04-29 23:57:08 +1000216 const char *comment)
217{
218 switch (key->type) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100219 case KEY_RSA1:
Ben Lindstromd0fca422001-03-26 13:44:06 +0000220 return key_save_private_rsa1(key, filename, passphrase,
221 comment);
Damien Millereba71ba2000-04-29 23:57:08 +1000222 case KEY_DSA:
Damien Miller0bc1bd82000-11-13 22:57:25 +1100223 case KEY_RSA:
Ben Lindstromd0fca422001-03-26 13:44:06 +0000224 return key_save_private_pem(key, filename, passphrase,
225 comment);
Damien Millereba71ba2000-04-29 23:57:08 +1000226 default:
227 break;
228 }
Ben Lindstrom15f33862001-04-16 02:00:02 +0000229 error("key_save_private: cannot save key type %d", key->type);
Damien Millereba71ba2000-04-29 23:57:08 +1000230 return 0;
231}
232
Damien Miller5428f641999-11-25 11:54:57 +1100233/*
Ben Lindstromd0fca422001-03-26 13:44:06 +0000234 * Loads the public part of the ssh v1 key file. Returns NULL if an error was
235 * encountered (the file does not exist or is not readable), and the key
Damien Miller5428f641999-11-25 11:54:57 +1100236 * otherwise.
237 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000238
Ben Lindstrombba81212001-06-25 05:01:22 +0000239static Key *
Ben Lindstromd0fca422001-03-26 13:44:06 +0000240key_load_public_rsa1(int fd, const char *filename, char **commentp)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000241{
Damien Miller95def091999-11-25 00:26:21 +1100242 Buffer buffer;
Ben Lindstromd0fca422001-03-26 13:44:06 +0000243 Key *pub;
Ben Lindstrom44adb8f2002-12-23 02:00:23 +0000244 struct stat st;
Damien Miller95def091999-11-25 00:26:21 +1100245 char *cp;
Damien Millereccb9de2005-06-17 12:59:34 +1000246 u_int i;
Darren Tucker1f8311c2004-05-13 16:39:33 +1000247 size_t len;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000248
Ben Lindstrom44adb8f2002-12-23 02:00:23 +0000249 if (fstat(fd, &st) < 0) {
250 error("fstat for key file %.200s failed: %.100s",
251 filename, strerror(errno));
252 return NULL;
253 }
Darren Tuckerf4b43712004-08-29 16:28:39 +1000254 if (st.st_size > 1*1024*1024) {
255 error("key file %.200s too large", filename);
256 return NULL;
257 }
Darren Tucker1f8311c2004-05-13 16:39:33 +1000258 len = (size_t)st.st_size; /* truncated */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000259
Damien Miller95def091999-11-25 00:26:21 +1100260 buffer_init(&buffer);
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100261 cp = buffer_append_space(&buffer, len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000262
Damien Millereccb9de2005-06-17 12:59:34 +1000263 if (atomicio(read, fd, cp, len) != len) {
Damien Miller95def091999-11-25 00:26:21 +1100264 debug("Read from key file %.200s failed: %.100s", filename,
Damien Millereba71ba2000-04-29 23:57:08 +1000265 strerror(errno));
Damien Miller95def091999-11-25 00:26:21 +1100266 buffer_free(&buffer);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000267 return NULL;
Damien Miller95def091999-11-25 00:26:21 +1100268 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000269
Ben Lindstrom1170d712001-01-29 07:51:26 +0000270 /* Check that it is at least big enough to contain the ID string. */
271 if (len < sizeof(authfile_id_string)) {
Damien Miller058655c2001-10-10 15:03:36 +1000272 debug3("Not a RSA1 key file %.200s.", filename);
Damien Miller95def091999-11-25 00:26:21 +1100273 buffer_free(&buffer);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000274 return NULL;
Damien Miller95def091999-11-25 00:26:21 +1100275 }
Damien Miller5428f641999-11-25 11:54:57 +1100276 /*
277 * Make sure it begins with the id string. Consume the id string
278 * from the buffer.
279 */
Ben Lindstrom1170d712001-01-29 07:51:26 +0000280 for (i = 0; i < sizeof(authfile_id_string); i++)
281 if (buffer_get_char(&buffer) != authfile_id_string[i]) {
Damien Miller058655c2001-10-10 15:03:36 +1000282 debug3("Not a RSA1 key file %.200s.", filename);
Damien Miller95def091999-11-25 00:26:21 +1100283 buffer_free(&buffer);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000284 return NULL;
Damien Miller95def091999-11-25 00:26:21 +1100285 }
286 /* Skip cipher type and reserved data. */
287 (void) buffer_get_char(&buffer); /* cipher type */
288 (void) buffer_get_int(&buffer); /* reserved */
289
290 /* Read the public key from the buffer. */
Ben Lindstromc5a7f4f2002-06-25 23:19:13 +0000291 (void) buffer_get_int(&buffer);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000292 pub = key_new(KEY_RSA1);
293 buffer_get_bignum(&buffer, pub->rsa->n);
294 buffer_get_bignum(&buffer, pub->rsa->e);
295 if (commentp)
296 *commentp = buffer_get_string(&buffer, NULL);
Damien Miller95def091999-11-25 00:26:21 +1100297 /* The encrypted private part is not parsed by this function. */
298
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000299 buffer_free(&buffer);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000300 return pub;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000301}
302
Ben Lindstromd0fca422001-03-26 13:44:06 +0000303/* load public key from private-key file, works only for SSH v1 */
304Key *
305key_load_public_type(int type, const char *filename, char **commentp)
Damien Millereba71ba2000-04-29 23:57:08 +1000306{
Ben Lindstromd0fca422001-03-26 13:44:06 +0000307 Key *pub;
308 int fd;
309
310 if (type == KEY_RSA1) {
311 fd = open(filename, O_RDONLY);
312 if (fd < 0)
313 return NULL;
314 pub = key_load_public_rsa1(fd, filename, commentp);
315 close(fd);
316 return pub;
Damien Millereba71ba2000-04-29 23:57:08 +1000317 }
Ben Lindstromd0fca422001-03-26 13:44:06 +0000318 return NULL;
Damien Millereba71ba2000-04-29 23:57:08 +1000319}
320
Damien Miller5428f641999-11-25 11:54:57 +1100321/*
322 * Loads the private key from the file. Returns 0 if an error is encountered
323 * (file does not exist or is not readable, or passphrase is bad). This
324 * initializes the private key.
325 * Assumes we are called under uid of the owner of the file.
326 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000327
Ben Lindstrombba81212001-06-25 05:01:22 +0000328static Key *
Ben Lindstromd0fca422001-03-26 13:44:06 +0000329key_load_private_rsa1(int fd, const char *filename, const char *passphrase,
330 char **commentp)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000331{
Damien Millereccb9de2005-06-17 12:59:34 +1000332 u_int i;
333 int check1, check2, cipher_type;
Darren Tucker1f8311c2004-05-13 16:39:33 +1000334 size_t len;
Damien Miller95def091999-11-25 00:26:21 +1100335 Buffer buffer, decrypted;
Damien Miller708d21c2002-01-22 23:18:15 +1100336 u_char *cp;
Damien Miller874d77b2000-10-14 16:23:11 +1100337 CipherContext ciphercontext;
338 Cipher *cipher;
Ben Lindstromd0fca422001-03-26 13:44:06 +0000339 Key *prv = NULL;
Ben Lindstrom44adb8f2002-12-23 02:00:23 +0000340 struct stat st;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000341
Ben Lindstrom44adb8f2002-12-23 02:00:23 +0000342 if (fstat(fd, &st) < 0) {
343 error("fstat for key file %.200s failed: %.100s",
344 filename, strerror(errno));
345 close(fd);
346 return NULL;
347 }
Darren Tucker1f8311c2004-05-13 16:39:33 +1000348 if (st.st_size > 1*1024*1024) {
Darren Tuckerf4b43712004-08-29 16:28:39 +1000349 error("key file %.200s too large", filename);
Darren Tucker1f8311c2004-05-13 16:39:33 +1000350 close(fd);
351 return (NULL);
352 }
353 len = (size_t)st.st_size; /* truncated */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000354
Damien Miller95def091999-11-25 00:26:21 +1100355 buffer_init(&buffer);
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100356 cp = buffer_append_space(&buffer, len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000357
Damien Millereccb9de2005-06-17 12:59:34 +1000358 if (atomicio(read, fd, cp, len) != len) {
Damien Miller95def091999-11-25 00:26:21 +1100359 debug("Read from key file %.200s failed: %.100s", filename,
Damien Miller0bc1bd82000-11-13 22:57:25 +1100360 strerror(errno));
Damien Miller95def091999-11-25 00:26:21 +1100361 buffer_free(&buffer);
Damien Miller037a0dc1999-12-07 15:38:31 +1100362 close(fd);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000363 return NULL;
Damien Miller95def091999-11-25 00:26:21 +1100364 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000365
Ben Lindstrom1170d712001-01-29 07:51:26 +0000366 /* Check that it is at least big enough to contain the ID string. */
367 if (len < sizeof(authfile_id_string)) {
Damien Miller058655c2001-10-10 15:03:36 +1000368 debug3("Not a RSA1 key file %.200s.", filename);
Damien Miller95def091999-11-25 00:26:21 +1100369 buffer_free(&buffer);
Ben Lindstromb257cca2001-03-05 04:59:27 +0000370 close(fd);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000371 return NULL;
Damien Miller95def091999-11-25 00:26:21 +1100372 }
Damien Miller5428f641999-11-25 11:54:57 +1100373 /*
374 * Make sure it begins with the id string. Consume the id string
375 * from the buffer.
376 */
Ben Lindstrom1170d712001-01-29 07:51:26 +0000377 for (i = 0; i < sizeof(authfile_id_string); i++)
378 if (buffer_get_char(&buffer) != authfile_id_string[i]) {
Damien Miller058655c2001-10-10 15:03:36 +1000379 debug3("Not a RSA1 key file %.200s.", filename);
Damien Miller95def091999-11-25 00:26:21 +1100380 buffer_free(&buffer);
Ben Lindstromb257cca2001-03-05 04:59:27 +0000381 close(fd);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000382 return NULL;
Damien Miller95def091999-11-25 00:26:21 +1100383 }
Ben Lindstromb257cca2001-03-05 04:59:27 +0000384
Damien Miller95def091999-11-25 00:26:21 +1100385 /* Read cipher type. */
386 cipher_type = buffer_get_char(&buffer);
387 (void) buffer_get_int(&buffer); /* Reserved data. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000388
Damien Miller95def091999-11-25 00:26:21 +1100389 /* Read the public key from the buffer. */
Ben Lindstromc5a7f4f2002-06-25 23:19:13 +0000390 (void) buffer_get_int(&buffer);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000391 prv = key_new_private(KEY_RSA1);
392
393 buffer_get_bignum(&buffer, prv->rsa->n);
394 buffer_get_bignum(&buffer, prv->rsa->e);
395 if (commentp)
396 *commentp = buffer_get_string(&buffer, NULL);
Damien Miller95def091999-11-25 00:26:21 +1100397 else
398 xfree(buffer_get_string(&buffer, NULL));
399
400 /* Check that it is a supported cipher. */
Damien Miller874d77b2000-10-14 16:23:11 +1100401 cipher = cipher_by_number(cipher_type);
402 if (cipher == NULL) {
403 debug("Unsupported cipher %d used in key file %.200s.",
404 cipher_type, filename);
Damien Miller95def091999-11-25 00:26:21 +1100405 buffer_free(&buffer);
406 goto fail;
407 }
408 /* Initialize space for decrypted data. */
409 buffer_init(&decrypted);
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100410 cp = buffer_append_space(&decrypted, buffer_len(&buffer));
Damien Miller95def091999-11-25 00:26:21 +1100411
412 /* Rest of the buffer is encrypted. Decrypt it using the passphrase. */
Damien Miller963f6b22002-02-19 15:21:23 +1100413 cipher_set_key_string(&ciphercontext, cipher, passphrase,
414 CIPHER_DECRYPT);
415 cipher_crypt(&ciphercontext, cp,
Damien Miller708d21c2002-01-22 23:18:15 +1100416 buffer_ptr(&buffer), buffer_len(&buffer));
Damien Miller963f6b22002-02-19 15:21:23 +1100417 cipher_cleanup(&ciphercontext);
Damien Miller874d77b2000-10-14 16:23:11 +1100418 memset(&ciphercontext, 0, sizeof(ciphercontext));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000419 buffer_free(&buffer);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000420
Damien Miller95def091999-11-25 00:26:21 +1100421 check1 = buffer_get_char(&decrypted);
422 check2 = buffer_get_char(&decrypted);
423 if (check1 != buffer_get_char(&decrypted) ||
424 check2 != buffer_get_char(&decrypted)) {
425 if (strcmp(passphrase, "") != 0)
Ben Lindstromd0fca422001-03-26 13:44:06 +0000426 debug("Bad passphrase supplied for key file %.200s.",
427 filename);
Damien Miller95def091999-11-25 00:26:21 +1100428 /* Bad passphrase. */
429 buffer_free(&decrypted);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000430 goto fail;
Damien Miller95def091999-11-25 00:26:21 +1100431 }
432 /* Read the rest of the private key. */
Ben Lindstromd0fca422001-03-26 13:44:06 +0000433 buffer_get_bignum(&decrypted, prv->rsa->d);
434 buffer_get_bignum(&decrypted, prv->rsa->iqmp); /* u */
435 /* in SSL and SSH v1 p and q are exchanged */
436 buffer_get_bignum(&decrypted, prv->rsa->q); /* p */
437 buffer_get_bignum(&decrypted, prv->rsa->p); /* q */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000438
Ben Lindstromd0fca422001-03-26 13:44:06 +0000439 /* calculate p-1 and q-1 */
Damien Millerda755162002-01-22 23:09:22 +1100440 rsa_generate_additional_parameters(prv->rsa);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000441
Damien Miller95def091999-11-25 00:26:21 +1100442 buffer_free(&decrypted);
Damien Millered33d3b2003-03-15 11:36:18 +1100443
444 /* enable blinding */
445 if (RSA_blinding_on(prv->rsa, NULL) != 1) {
446 error("key_load_private_rsa1: RSA_blinding_on failed");
447 goto fail;
448 }
Ben Lindstromb257cca2001-03-05 04:59:27 +0000449 close(fd);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000450 return prv;
451
452fail:
453 if (commentp)
454 xfree(*commentp);
455 close(fd);
456 key_free(prv);
457 return NULL;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000458}
Damien Millereba71ba2000-04-29 23:57:08 +1000459
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000460Key *
Ben Lindstromd0fca422001-03-26 13:44:06 +0000461key_load_private_pem(int fd, int type, const char *passphrase,
462 char **commentp)
Damien Millereba71ba2000-04-29 23:57:08 +1000463{
Damien Millereba71ba2000-04-29 23:57:08 +1000464 FILE *fp;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100465 EVP_PKEY *pk = NULL;
Ben Lindstromd0fca422001-03-26 13:44:06 +0000466 Key *prv = NULL;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100467 char *name = "<no key>";
Damien Millereba71ba2000-04-29 23:57:08 +1000468
Damien Millereba71ba2000-04-29 23:57:08 +1000469 fp = fdopen(fd, "r");
470 if (fp == NULL) {
Ben Lindstrom15f33862001-04-16 02:00:02 +0000471 error("fdopen failed: %s", strerror(errno));
Ben Lindstromb257cca2001-03-05 04:59:27 +0000472 close(fd);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000473 return NULL;
Damien Millereba71ba2000-04-29 23:57:08 +1000474 }
Damien Miller0bc1bd82000-11-13 22:57:25 +1100475 pk = PEM_read_PrivateKey(fp, NULL, NULL, (char *)passphrase);
476 if (pk == NULL) {
Ben Lindstrom648772f2001-04-19 20:47:10 +0000477 debug("PEM_read_PrivateKey failed");
Damien Miller0bc1bd82000-11-13 22:57:25 +1100478 (void)ERR_get_error();
Ben Lindstromd0fca422001-03-26 13:44:06 +0000479 } else if (pk->type == EVP_PKEY_RSA &&
Damien Miller9f0f5c62001-12-21 14:45:46 +1100480 (type == KEY_UNSPEC||type==KEY_RSA)) {
Ben Lindstromd0fca422001-03-26 13:44:06 +0000481 prv = key_new(KEY_UNSPEC);
482 prv->rsa = EVP_PKEY_get1_RSA(pk);
483 prv->type = KEY_RSA;
484 name = "rsa w/o comment";
Damien Miller0bc1bd82000-11-13 22:57:25 +1100485#ifdef DEBUG_PK
Ben Lindstromd0fca422001-03-26 13:44:06 +0000486 RSA_print_fp(stderr, prv->rsa, 8);
Damien Millereba71ba2000-04-29 23:57:08 +1000487#endif
Damien Millered33d3b2003-03-15 11:36:18 +1100488 if (RSA_blinding_on(prv->rsa, NULL) != 1) {
489 error("key_load_private_pem: RSA_blinding_on failed");
490 key_free(prv);
491 prv = NULL;
492 }
Ben Lindstromd0fca422001-03-26 13:44:06 +0000493 } else if (pk->type == EVP_PKEY_DSA &&
Damien Miller9f0f5c62001-12-21 14:45:46 +1100494 (type == KEY_UNSPEC||type==KEY_DSA)) {
Ben Lindstromd0fca422001-03-26 13:44:06 +0000495 prv = key_new(KEY_UNSPEC);
496 prv->dsa = EVP_PKEY_get1_DSA(pk);
497 prv->type = KEY_DSA;
498 name = "dsa w/o comment";
Damien Miller0bc1bd82000-11-13 22:57:25 +1100499#ifdef DEBUG_PK
Ben Lindstromd0fca422001-03-26 13:44:06 +0000500 DSA_print_fp(stderr, prv->dsa, 8);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100501#endif
Damien Miller0bc1bd82000-11-13 22:57:25 +1100502 } else {
503 error("PEM_read_PrivateKey: mismatch or "
504 "unknown EVP_PKEY save_type %d", pk->save_type);
505 }
506 fclose(fp);
507 if (pk != NULL)
508 EVP_PKEY_free(pk);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000509 if (prv != NULL && commentp)
510 *commentp = xstrdup(name);
511 debug("read PEM private key done: type %s",
512 prv ? key_type(prv) : "<unknown>");
513 return prv;
Damien Millereba71ba2000-04-29 23:57:08 +1000514}
515
Damien Miller8275fad2006-03-15 12:06:23 +1100516int
Ben Lindstromd0fca422001-03-26 13:44:06 +0000517key_perm_ok(int fd, const char *filename)
Damien Millereba71ba2000-04-29 23:57:08 +1000518{
Damien Millereba71ba2000-04-29 23:57:08 +1000519 struct stat st;
520
Ben Lindstrom7aff2612001-09-23 13:53:22 +0000521 if (fstat(fd, &st) < 0)
522 return 0;
523 /*
524 * if a key owned by the user is accessed, then we check the
525 * permissions of the file. if the key owned by a different user,
526 * then we don't care.
527 */
Damien Millerb70b61f2000-09-16 16:25:12 +1100528#ifdef HAVE_CYGWIN
Damien Millercb5e44a2000-09-29 12:12:36 +1100529 if (check_ntsec(filename))
Damien Millerb70b61f2000-09-16 16:25:12 +1100530#endif
Ben Lindstrom7aff2612001-09-23 13:53:22 +0000531 if ((st.st_uid == getuid()) && (st.st_mode & 077) != 0) {
Damien Millereba71ba2000-04-29 23:57:08 +1000532 error("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
533 error("@ WARNING: UNPROTECTED PRIVATE KEY FILE! @");
534 error("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
Ben Lindstrom7aff2612001-09-23 13:53:22 +0000535 error("Permissions 0%3.3o for '%s' are too open.",
Damien Miller04bd8b02003-05-25 14:38:33 +1000536 (u_int)st.st_mode & 0777, filename);
Damien Millereba71ba2000-04-29 23:57:08 +1000537 error("It is recommended that your private key files are NOT accessible by others.");
Ben Lindstromd0fca422001-03-26 13:44:06 +0000538 error("This private key will be ignored.");
Damien Millereba71ba2000-04-29 23:57:08 +1000539 return 0;
540 }
Ben Lindstromd0fca422001-03-26 13:44:06 +0000541 return 1;
542}
Ben Lindstromb257cca2001-03-05 04:59:27 +0000543
Ben Lindstromd0fca422001-03-26 13:44:06 +0000544Key *
545key_load_private_type(int type, const char *filename, const char *passphrase,
Darren Tucker232b76f2006-05-06 17:41:51 +1000546 char **commentp, int *perm_ok)
Ben Lindstromd0fca422001-03-26 13:44:06 +0000547{
548 int fd;
549
550 fd = open(filename, O_RDONLY);
551 if (fd < 0)
552 return NULL;
553 if (!key_perm_ok(fd, filename)) {
Darren Tucker232b76f2006-05-06 17:41:51 +1000554 if (perm_ok != NULL)
555 *perm_ok = 0;
Ben Lindstrom15f33862001-04-16 02:00:02 +0000556 error("bad permissions: ignore key: %s", filename);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000557 close(fd);
558 return NULL;
559 }
Darren Tucker232b76f2006-05-06 17:41:51 +1000560 if (perm_ok != NULL)
561 *perm_ok = 1;
Ben Lindstromd0fca422001-03-26 13:44:06 +0000562 switch (type) {
563 case KEY_RSA1:
564 return key_load_private_rsa1(fd, filename, passphrase,
565 commentp);
566 /* closes fd */
Damien Millereba71ba2000-04-29 23:57:08 +1000567 case KEY_DSA:
Damien Miller0bc1bd82000-11-13 22:57:25 +1100568 case KEY_RSA:
569 case KEY_UNSPEC:
Ben Lindstromd0fca422001-03-26 13:44:06 +0000570 return key_load_private_pem(fd, type, passphrase, commentp);
571 /* closes fd */
Damien Millereba71ba2000-04-29 23:57:08 +1000572 default:
Ben Lindstromb257cca2001-03-05 04:59:27 +0000573 close(fd);
Damien Millereba71ba2000-04-29 23:57:08 +1000574 break;
575 }
Ben Lindstromd0fca422001-03-26 13:44:06 +0000576 return NULL;
577}
578
579Key *
580key_load_private(const char *filename, const char *passphrase,
581 char **commentp)
582{
Ben Lindstrom322915d2001-06-05 20:46:32 +0000583 Key *pub, *prv;
Ben Lindstromd0fca422001-03-26 13:44:06 +0000584 int fd;
585
586 fd = open(filename, O_RDONLY);
587 if (fd < 0)
588 return NULL;
589 if (!key_perm_ok(fd, filename)) {
Ben Lindstrom15f33862001-04-16 02:00:02 +0000590 error("bad permissions: ignore key: %s", filename);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000591 close(fd);
592 return NULL;
593 }
594 pub = key_load_public_rsa1(fd, filename, commentp);
595 lseek(fd, (off_t) 0, SEEK_SET); /* rewind */
596 if (pub == NULL) {
597 /* closes fd */
Ben Lindstrom322915d2001-06-05 20:46:32 +0000598 prv = key_load_private_pem(fd, KEY_UNSPEC, passphrase, NULL);
599 /* use the filename as a comment for PEM */
600 if (commentp && prv)
Ben Lindstrom2d0356f2001-06-05 21:13:57 +0000601 *commentp = xstrdup(filename);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000602 } else {
603 /* it's a SSH v1 key if the public key part is readable */
604 key_free(pub);
605 /* closes fd */
Ben Lindstrom322915d2001-06-05 20:46:32 +0000606 prv = key_load_private_rsa1(fd, filename, passphrase, NULL);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000607 }
Ben Lindstrom322915d2001-06-05 20:46:32 +0000608 return prv;
Damien Millereba71ba2000-04-29 23:57:08 +1000609}
Damien Millere4340be2000-09-16 13:29:08 +1100610
Ben Lindstrombba81212001-06-25 05:01:22 +0000611static int
Ben Lindstromd0fca422001-03-26 13:44:06 +0000612key_try_load_public(Key *k, const char *filename, char **commentp)
Damien Millere4340be2000-09-16 13:29:08 +1100613{
614 FILE *f;
Darren Tucker22cc7412004-12-06 22:47:41 +1100615 char line[SSH_MAX_PUBKEY_BYTES];
Damien Millere4340be2000-09-16 13:29:08 +1100616 char *cp;
Darren Tuckerf0f90982004-12-11 13:39:50 +1100617 u_long linenum = 0;
Damien Millere4340be2000-09-16 13:29:08 +1100618
619 f = fopen(filename, "r");
620 if (f != NULL) {
Darren Tucker22cc7412004-12-06 22:47:41 +1100621 while (read_keyfile_line(f, filename, line, sizeof(line),
622 &linenum) != -1) {
Damien Millere4340be2000-09-16 13:29:08 +1100623 cp = line;
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000624 switch (*cp) {
Damien Millere4340be2000-09-16 13:29:08 +1100625 case '#':
626 case '\n':
627 case '\0':
628 continue;
629 }
630 /* Skip leading whitespace. */
631 for (; *cp && (*cp == ' ' || *cp == '\t'); cp++)
632 ;
633 if (*cp) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100634 if (key_read(k, &cp) == 1) {
Damien Millere4340be2000-09-16 13:29:08 +1100635 if (commentp)
636 *commentp=xstrdup(filename);
637 fclose(f);
638 return 1;
639 }
640 }
641 }
642 fclose(f);
643 }
644 return 0;
645}
646
Ben Lindstromd0fca422001-03-26 13:44:06 +0000647/* load public key from ssh v1 private or any pubkey file */
648Key *
649key_load_public(const char *filename, char **commentp)
Damien Millere4340be2000-09-16 13:29:08 +1100650{
Ben Lindstromd0fca422001-03-26 13:44:06 +0000651 Key *pub;
652 char file[MAXPATHLEN];
Damien Millere4340be2000-09-16 13:29:08 +1100653
Damien Millerdb274722003-05-14 13:45:22 +1000654 /* try rsa1 private key */
Ben Lindstromd0fca422001-03-26 13:44:06 +0000655 pub = key_load_public_type(KEY_RSA1, filename, commentp);
656 if (pub != NULL)
657 return pub;
Damien Millerdb274722003-05-14 13:45:22 +1000658
659 /* try rsa1 public key */
660 pub = key_new(KEY_RSA1);
661 if (key_try_load_public(pub, filename, commentp) == 1)
662 return pub;
663 key_free(pub);
664
665 /* try ssh2 public key */
Ben Lindstromd0fca422001-03-26 13:44:06 +0000666 pub = key_new(KEY_UNSPEC);
667 if (key_try_load_public(pub, filename, commentp) == 1)
668 return pub;
669 if ((strlcpy(file, filename, sizeof file) < sizeof(file)) &&
670 (strlcat(file, ".pub", sizeof file) < sizeof(file)) &&
671 (key_try_load_public(pub, file, commentp) == 1))
672 return pub;
673 key_free(pub);
674 return NULL;
Damien Millere4340be2000-09-16 13:29:08 +1100675}