blob: 53397ea53f45ca9bd405782cb4d77387e55477e4 [file] [log] [blame]
Damien Millere6b3b612006-07-24 14:01:23 +10001/* $OpenBSD: authfile.c,v 1.70 2006/07/17 01:31:09 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 Millere6b3b612006-07-24 14:01:23 +100050#include <unistd.h>
Damien Miller57cf6382006-07-10 21:13:46 +100051
Ben Lindstrom226cfa02001-01-22 05:34:40 +000052#include "cipher.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100053#include "xmalloc.h"
54#include "buffer.h"
55#include "bufaux.h"
Damien Millereba71ba2000-04-29 23:57:08 +100056#include "key.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000057#include "ssh.h"
58#include "log.h"
Ben Lindstrom31ca54a2001-02-09 02:11:24 +000059#include "authfile.h"
Damien Miller040b64f2002-01-22 23:10:04 +110060#include "rsa.h"
Darren Tuckerf0f90982004-12-11 13:39:50 +110061#include "misc.h"
Damien Millereccb9de2005-06-17 12:59:34 +100062#include "atomicio.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100063
Ben Lindstromd0fca422001-03-26 13:44:06 +000064/* Version identification string for SSH v1 identity files. */
Ben Lindstrom1170d712001-01-29 07:51:26 +000065static const char authfile_id_string[] =
66 "SSH PRIVATE KEY FILE FORMAT 1.1\n";
Damien Millerd4a8b7e1999-10-27 13:42:43 +100067
Damien Miller5428f641999-11-25 11:54:57 +110068/*
69 * Saves the authentication (private) key in a file, encrypting it with
70 * passphrase. The identification of the file (lowest 64 bits of n) will
71 * precede the key to provide identification of the key without needing a
72 * passphrase.
73 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100074
Ben Lindstrombba81212001-06-25 05:01:22 +000075static int
Ben Lindstromd0fca422001-03-26 13:44:06 +000076key_save_private_rsa1(Key *key, const char *filename, const char *passphrase,
77 const char *comment)
Damien Millerd4a8b7e1999-10-27 13:42:43 +100078{
Damien Miller95def091999-11-25 00:26:21 +110079 Buffer buffer, encrypted;
Damien Miller708d21c2002-01-22 23:18:15 +110080 u_char buf[100], *cp;
Damien Miller963f6b22002-02-19 15:21:23 +110081 int fd, i, cipher_num;
Damien Miller874d77b2000-10-14 16:23:11 +110082 CipherContext ciphercontext;
83 Cipher *cipher;
Darren Tucker3f9fdc72004-06-22 12:56:01 +100084 u_int32_t rnd;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100085
Damien Miller5428f641999-11-25 11:54:57 +110086 /*
87 * If the passphrase is empty, use SSH_CIPHER_NONE to ease converting
88 * to another cipher; otherwise use SSH_AUTHFILE_CIPHER.
89 */
Damien Miller963f6b22002-02-19 15:21:23 +110090 cipher_num = (strcmp(passphrase, "") == 0) ?
91 SSH_CIPHER_NONE : SSH_AUTHFILE_CIPHER;
92 if ((cipher = cipher_by_number(cipher_num)) == NULL)
Damien Miller874d77b2000-10-14 16:23:11 +110093 fatal("save_private_key_rsa: bad cipher");
Damien Millerd4a8b7e1999-10-27 13:42:43 +100094
Damien Miller95def091999-11-25 00:26:21 +110095 /* This buffer is used to built the secret part of the private key. */
96 buffer_init(&buffer);
Damien Millerd4a8b7e1999-10-27 13:42:43 +100097
Damien Miller95def091999-11-25 00:26:21 +110098 /* Put checkbytes for checking passphrase validity. */
Darren Tucker3f9fdc72004-06-22 12:56:01 +100099 rnd = arc4random();
100 buf[0] = rnd & 0xff;
101 buf[1] = (rnd >> 8) & 0xff;
Damien Miller95def091999-11-25 00:26:21 +1100102 buf[2] = buf[0];
103 buf[3] = buf[1];
104 buffer_append(&buffer, buf, 4);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000105
Damien Miller5428f641999-11-25 11:54:57 +1100106 /*
107 * Store the private key (n and e will not be stored because they
108 * will be stored in plain text, and storing them also in encrypted
109 * format would just give known plaintext).
110 */
Ben Lindstromd0fca422001-03-26 13:44:06 +0000111 buffer_put_bignum(&buffer, key->rsa->d);
112 buffer_put_bignum(&buffer, key->rsa->iqmp);
113 buffer_put_bignum(&buffer, key->rsa->q); /* reverse from SSL p */
114 buffer_put_bignum(&buffer, key->rsa->p); /* reverse from SSL q */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000115
Damien Miller95def091999-11-25 00:26:21 +1100116 /* Pad the part to be encrypted until its size is a multiple of 8. */
117 while (buffer_len(&buffer) % 8 != 0)
118 buffer_put_char(&buffer, 0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000119
Damien Miller95def091999-11-25 00:26:21 +1100120 /* This buffer will be used to contain the data in the file. */
121 buffer_init(&encrypted);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000122
Damien Miller95def091999-11-25 00:26:21 +1100123 /* First store keyfile id string. */
Ben Lindstrom1170d712001-01-29 07:51:26 +0000124 for (i = 0; authfile_id_string[i]; i++)
125 buffer_put_char(&encrypted, authfile_id_string[i]);
Damien Miller95def091999-11-25 00:26:21 +1100126 buffer_put_char(&encrypted, 0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000127
Damien Miller95def091999-11-25 00:26:21 +1100128 /* Store cipher type. */
Damien Miller963f6b22002-02-19 15:21:23 +1100129 buffer_put_char(&encrypted, cipher_num);
Damien Miller95def091999-11-25 00:26:21 +1100130 buffer_put_int(&encrypted, 0); /* For future extension */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000131
Damien Miller95def091999-11-25 00:26:21 +1100132 /* Store public key. This will be in plain text. */
Ben Lindstromd0fca422001-03-26 13:44:06 +0000133 buffer_put_int(&encrypted, BN_num_bits(key->rsa->n));
134 buffer_put_bignum(&encrypted, key->rsa->n);
135 buffer_put_bignum(&encrypted, key->rsa->e);
Ben Lindstrom664408d2001-06-09 01:42:01 +0000136 buffer_put_cstring(&encrypted, comment);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000137
Damien Miller95def091999-11-25 00:26:21 +1100138 /* Allocate space for the private part of the key in the buffer. */
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100139 cp = buffer_append_space(&encrypted, buffer_len(&buffer));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000140
Damien Miller963f6b22002-02-19 15:21:23 +1100141 cipher_set_key_string(&ciphercontext, cipher, passphrase,
142 CIPHER_ENCRYPT);
143 cipher_crypt(&ciphercontext, cp,
Damien Miller708d21c2002-01-22 23:18:15 +1100144 buffer_ptr(&buffer), buffer_len(&buffer));
Damien Miller963f6b22002-02-19 15:21:23 +1100145 cipher_cleanup(&ciphercontext);
Damien Miller874d77b2000-10-14 16:23:11 +1100146 memset(&ciphercontext, 0, sizeof(ciphercontext));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000147
Damien Miller95def091999-11-25 00:26:21 +1100148 /* Destroy temporary data. */
149 memset(buf, 0, sizeof(buf));
150 buffer_free(&buffer);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000151
Damien Miller037a0dc1999-12-07 15:38:31 +1100152 fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0600);
Ben Lindstrom15f33862001-04-16 02:00:02 +0000153 if (fd < 0) {
154 error("open %s failed: %s.", filename, strerror(errno));
Darren Tuckerd1d41b32003-09-22 21:01:27 +1000155 buffer_free(&encrypted);
Damien Miller95def091999-11-25 00:26:21 +1100156 return 0;
Ben Lindstrom15f33862001-04-16 02:00:02 +0000157 }
Damien Millereccb9de2005-06-17 12:59:34 +1000158 if (atomicio(vwrite, fd, buffer_ptr(&encrypted),
159 buffer_len(&encrypted)) != buffer_len(&encrypted)) {
Ben Lindstrom15f33862001-04-16 02:00:02 +0000160 error("write to key file %s failed: %s", filename,
Damien Miller9f0f5c62001-12-21 14:45:46 +1100161 strerror(errno));
Damien Miller95def091999-11-25 00:26:21 +1100162 buffer_free(&encrypted);
Damien Miller037a0dc1999-12-07 15:38:31 +1100163 close(fd);
Damien Miller78315eb2000-09-29 23:01:36 +1100164 unlink(filename);
Damien Miller95def091999-11-25 00:26:21 +1100165 return 0;
166 }
Damien Miller037a0dc1999-12-07 15:38:31 +1100167 close(fd);
Damien Miller95def091999-11-25 00:26:21 +1100168 buffer_free(&encrypted);
169 return 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000170}
171
Ben Lindstromd0fca422001-03-26 13:44:06 +0000172/* save SSH v2 key in OpenSSL PEM format */
Ben Lindstrombba81212001-06-25 05:01:22 +0000173static int
Ben Lindstromd0fca422001-03-26 13:44:06 +0000174key_save_private_pem(Key *key, const char *filename, const char *_passphrase,
175 const char *comment)
Damien Millereba71ba2000-04-29 23:57:08 +1000176{
177 FILE *fp;
178 int fd;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100179 int success = 0;
180 int len = strlen(_passphrase);
Ben Lindstrom90fd8142002-02-26 18:09:42 +0000181 u_char *passphrase = (len > 0) ? (u_char *)_passphrase : NULL;
Ben Lindstrom80cb27d2002-03-05 01:33:36 +0000182 const EVP_CIPHER *cipher = (len > 0) ? EVP_des_ede3_cbc() : NULL;
Damien Millereba71ba2000-04-29 23:57:08 +1000183
184 if (len > 0 && len <= 4) {
Ben Lindstrom15f33862001-04-16 02:00:02 +0000185 error("passphrase too short: have %d bytes, need > 4", len);
Damien Millereba71ba2000-04-29 23:57:08 +1000186 return 0;
187 }
188 fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0600);
189 if (fd < 0) {
Ben Lindstrom15f33862001-04-16 02:00:02 +0000190 error("open %s failed: %s.", filename, strerror(errno));
Damien Millereba71ba2000-04-29 23:57:08 +1000191 return 0;
192 }
193 fp = fdopen(fd, "w");
194 if (fp == NULL ) {
Ben Lindstrom15f33862001-04-16 02:00:02 +0000195 error("fdopen %s failed: %s.", filename, strerror(errno));
Damien Millereba71ba2000-04-29 23:57:08 +1000196 close(fd);
197 return 0;
198 }
Damien Miller0bc1bd82000-11-13 22:57:25 +1100199 switch (key->type) {
Ben Lindstromc1116602001-03-29 00:28:37 +0000200 case KEY_DSA:
201 success = PEM_write_DSAPrivateKey(fp, key->dsa,
202 cipher, passphrase, len, NULL, NULL);
203 break;
204 case KEY_RSA:
205 success = PEM_write_RSAPrivateKey(fp, key->rsa,
206 cipher, passphrase, len, NULL, NULL);
207 break;
Damien Millereba71ba2000-04-29 23:57:08 +1000208 }
209 fclose(fp);
210 return success;
211}
212
213int
Ben Lindstromd0fca422001-03-26 13:44:06 +0000214key_save_private(Key *key, const char *filename, const char *passphrase,
Damien Millereba71ba2000-04-29 23:57:08 +1000215 const char *comment)
216{
217 switch (key->type) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100218 case KEY_RSA1:
Ben Lindstromd0fca422001-03-26 13:44:06 +0000219 return key_save_private_rsa1(key, filename, passphrase,
220 comment);
Damien Millereba71ba2000-04-29 23:57:08 +1000221 case KEY_DSA:
Damien Miller0bc1bd82000-11-13 22:57:25 +1100222 case KEY_RSA:
Ben Lindstromd0fca422001-03-26 13:44:06 +0000223 return key_save_private_pem(key, filename, passphrase,
224 comment);
Damien Millereba71ba2000-04-29 23:57:08 +1000225 default:
226 break;
227 }
Ben Lindstrom15f33862001-04-16 02:00:02 +0000228 error("key_save_private: cannot save key type %d", key->type);
Damien Millereba71ba2000-04-29 23:57:08 +1000229 return 0;
230}
231
Damien Miller5428f641999-11-25 11:54:57 +1100232/*
Ben Lindstromd0fca422001-03-26 13:44:06 +0000233 * Loads the public part of the ssh v1 key file. Returns NULL if an error was
234 * encountered (the file does not exist or is not readable), and the key
Damien Miller5428f641999-11-25 11:54:57 +1100235 * otherwise.
236 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000237
Ben Lindstrombba81212001-06-25 05:01:22 +0000238static Key *
Ben Lindstromd0fca422001-03-26 13:44:06 +0000239key_load_public_rsa1(int fd, const char *filename, char **commentp)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000240{
Damien Miller95def091999-11-25 00:26:21 +1100241 Buffer buffer;
Ben Lindstromd0fca422001-03-26 13:44:06 +0000242 Key *pub;
Ben Lindstrom44adb8f2002-12-23 02:00:23 +0000243 struct stat st;
Damien Miller95def091999-11-25 00:26:21 +1100244 char *cp;
Damien Millereccb9de2005-06-17 12:59:34 +1000245 u_int i;
Darren Tucker1f8311c2004-05-13 16:39:33 +1000246 size_t len;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000247
Ben Lindstrom44adb8f2002-12-23 02:00:23 +0000248 if (fstat(fd, &st) < 0) {
249 error("fstat for key file %.200s failed: %.100s",
250 filename, strerror(errno));
251 return NULL;
252 }
Darren Tuckerf4b43712004-08-29 16:28:39 +1000253 if (st.st_size > 1*1024*1024) {
254 error("key file %.200s too large", filename);
255 return NULL;
256 }
Darren Tucker1f8311c2004-05-13 16:39:33 +1000257 len = (size_t)st.st_size; /* truncated */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000258
Damien Miller95def091999-11-25 00:26:21 +1100259 buffer_init(&buffer);
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100260 cp = buffer_append_space(&buffer, len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000261
Damien Millereccb9de2005-06-17 12:59:34 +1000262 if (atomicio(read, fd, cp, len) != len) {
Damien Miller95def091999-11-25 00:26:21 +1100263 debug("Read from key file %.200s failed: %.100s", filename,
Damien Millereba71ba2000-04-29 23:57:08 +1000264 strerror(errno));
Damien Miller95def091999-11-25 00:26:21 +1100265 buffer_free(&buffer);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000266 return NULL;
Damien Miller95def091999-11-25 00:26:21 +1100267 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000268
Ben Lindstrom1170d712001-01-29 07:51:26 +0000269 /* Check that it is at least big enough to contain the ID string. */
270 if (len < sizeof(authfile_id_string)) {
Damien Miller058655c2001-10-10 15:03:36 +1000271 debug3("Not a RSA1 key file %.200s.", filename);
Damien Miller95def091999-11-25 00:26:21 +1100272 buffer_free(&buffer);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000273 return NULL;
Damien Miller95def091999-11-25 00:26:21 +1100274 }
Damien Miller5428f641999-11-25 11:54:57 +1100275 /*
276 * Make sure it begins with the id string. Consume the id string
277 * from the buffer.
278 */
Ben Lindstrom1170d712001-01-29 07:51:26 +0000279 for (i = 0; i < sizeof(authfile_id_string); i++)
280 if (buffer_get_char(&buffer) != authfile_id_string[i]) {
Damien Miller058655c2001-10-10 15:03:36 +1000281 debug3("Not a RSA1 key file %.200s.", filename);
Damien Miller95def091999-11-25 00:26:21 +1100282 buffer_free(&buffer);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000283 return NULL;
Damien Miller95def091999-11-25 00:26:21 +1100284 }
285 /* Skip cipher type and reserved data. */
286 (void) buffer_get_char(&buffer); /* cipher type */
287 (void) buffer_get_int(&buffer); /* reserved */
288
289 /* Read the public key from the buffer. */
Ben Lindstromc5a7f4f2002-06-25 23:19:13 +0000290 (void) buffer_get_int(&buffer);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000291 pub = key_new(KEY_RSA1);
292 buffer_get_bignum(&buffer, pub->rsa->n);
293 buffer_get_bignum(&buffer, pub->rsa->e);
294 if (commentp)
295 *commentp = buffer_get_string(&buffer, NULL);
Damien Miller95def091999-11-25 00:26:21 +1100296 /* The encrypted private part is not parsed by this function. */
297
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000298 buffer_free(&buffer);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000299 return pub;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000300}
301
Ben Lindstromd0fca422001-03-26 13:44:06 +0000302/* load public key from private-key file, works only for SSH v1 */
303Key *
304key_load_public_type(int type, const char *filename, char **commentp)
Damien Millereba71ba2000-04-29 23:57:08 +1000305{
Ben Lindstromd0fca422001-03-26 13:44:06 +0000306 Key *pub;
307 int fd;
308
309 if (type == KEY_RSA1) {
310 fd = open(filename, O_RDONLY);
311 if (fd < 0)
312 return NULL;
313 pub = key_load_public_rsa1(fd, filename, commentp);
314 close(fd);
315 return pub;
Damien Millereba71ba2000-04-29 23:57:08 +1000316 }
Ben Lindstromd0fca422001-03-26 13:44:06 +0000317 return NULL;
Damien Millereba71ba2000-04-29 23:57:08 +1000318}
319
Damien Miller5428f641999-11-25 11:54:57 +1100320/*
321 * Loads the private key from the file. Returns 0 if an error is encountered
322 * (file does not exist or is not readable, or passphrase is bad). This
323 * initializes the private key.
324 * Assumes we are called under uid of the owner of the file.
325 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000326
Ben Lindstrombba81212001-06-25 05:01:22 +0000327static Key *
Ben Lindstromd0fca422001-03-26 13:44:06 +0000328key_load_private_rsa1(int fd, const char *filename, const char *passphrase,
329 char **commentp)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000330{
Damien Millereccb9de2005-06-17 12:59:34 +1000331 u_int i;
332 int check1, check2, cipher_type;
Darren Tucker1f8311c2004-05-13 16:39:33 +1000333 size_t len;
Damien Miller95def091999-11-25 00:26:21 +1100334 Buffer buffer, decrypted;
Damien Miller708d21c2002-01-22 23:18:15 +1100335 u_char *cp;
Damien Miller874d77b2000-10-14 16:23:11 +1100336 CipherContext ciphercontext;
337 Cipher *cipher;
Ben Lindstromd0fca422001-03-26 13:44:06 +0000338 Key *prv = NULL;
Ben Lindstrom44adb8f2002-12-23 02:00:23 +0000339 struct stat st;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000340
Ben Lindstrom44adb8f2002-12-23 02:00:23 +0000341 if (fstat(fd, &st) < 0) {
342 error("fstat for key file %.200s failed: %.100s",
343 filename, strerror(errno));
344 close(fd);
345 return NULL;
346 }
Darren Tucker1f8311c2004-05-13 16:39:33 +1000347 if (st.st_size > 1*1024*1024) {
Darren Tuckerf4b43712004-08-29 16:28:39 +1000348 error("key file %.200s too large", filename);
Darren Tucker1f8311c2004-05-13 16:39:33 +1000349 close(fd);
350 return (NULL);
351 }
352 len = (size_t)st.st_size; /* truncated */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000353
Damien Miller95def091999-11-25 00:26:21 +1100354 buffer_init(&buffer);
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100355 cp = buffer_append_space(&buffer, len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000356
Damien Millereccb9de2005-06-17 12:59:34 +1000357 if (atomicio(read, fd, cp, len) != len) {
Damien Miller95def091999-11-25 00:26:21 +1100358 debug("Read from key file %.200s failed: %.100s", filename,
Damien Miller0bc1bd82000-11-13 22:57:25 +1100359 strerror(errno));
Damien Miller95def091999-11-25 00:26:21 +1100360 buffer_free(&buffer);
Damien Miller037a0dc1999-12-07 15:38:31 +1100361 close(fd);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000362 return NULL;
Damien Miller95def091999-11-25 00:26:21 +1100363 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000364
Ben Lindstrom1170d712001-01-29 07:51:26 +0000365 /* Check that it is at least big enough to contain the ID string. */
366 if (len < sizeof(authfile_id_string)) {
Damien Miller058655c2001-10-10 15:03:36 +1000367 debug3("Not a RSA1 key file %.200s.", filename);
Damien Miller95def091999-11-25 00:26:21 +1100368 buffer_free(&buffer);
Ben Lindstromb257cca2001-03-05 04:59:27 +0000369 close(fd);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000370 return NULL;
Damien Miller95def091999-11-25 00:26:21 +1100371 }
Damien Miller5428f641999-11-25 11:54:57 +1100372 /*
373 * Make sure it begins with the id string. Consume the id string
374 * from the buffer.
375 */
Ben Lindstrom1170d712001-01-29 07:51:26 +0000376 for (i = 0; i < sizeof(authfile_id_string); i++)
377 if (buffer_get_char(&buffer) != authfile_id_string[i]) {
Damien Miller058655c2001-10-10 15:03:36 +1000378 debug3("Not a RSA1 key file %.200s.", filename);
Damien Miller95def091999-11-25 00:26:21 +1100379 buffer_free(&buffer);
Ben Lindstromb257cca2001-03-05 04:59:27 +0000380 close(fd);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000381 return NULL;
Damien Miller95def091999-11-25 00:26:21 +1100382 }
Ben Lindstromb257cca2001-03-05 04:59:27 +0000383
Damien Miller95def091999-11-25 00:26:21 +1100384 /* Read cipher type. */
385 cipher_type = buffer_get_char(&buffer);
386 (void) buffer_get_int(&buffer); /* Reserved data. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000387
Damien Miller95def091999-11-25 00:26:21 +1100388 /* Read the public key from the buffer. */
Ben Lindstromc5a7f4f2002-06-25 23:19:13 +0000389 (void) buffer_get_int(&buffer);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000390 prv = key_new_private(KEY_RSA1);
391
392 buffer_get_bignum(&buffer, prv->rsa->n);
393 buffer_get_bignum(&buffer, prv->rsa->e);
394 if (commentp)
395 *commentp = buffer_get_string(&buffer, NULL);
Damien Miller95def091999-11-25 00:26:21 +1100396 else
397 xfree(buffer_get_string(&buffer, NULL));
398
399 /* Check that it is a supported cipher. */
Damien Miller874d77b2000-10-14 16:23:11 +1100400 cipher = cipher_by_number(cipher_type);
401 if (cipher == NULL) {
402 debug("Unsupported cipher %d used in key file %.200s.",
403 cipher_type, filename);
Damien Miller95def091999-11-25 00:26:21 +1100404 buffer_free(&buffer);
405 goto fail;
406 }
407 /* Initialize space for decrypted data. */
408 buffer_init(&decrypted);
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100409 cp = buffer_append_space(&decrypted, buffer_len(&buffer));
Damien Miller95def091999-11-25 00:26:21 +1100410
411 /* Rest of the buffer is encrypted. Decrypt it using the passphrase. */
Damien Miller963f6b22002-02-19 15:21:23 +1100412 cipher_set_key_string(&ciphercontext, cipher, passphrase,
413 CIPHER_DECRYPT);
414 cipher_crypt(&ciphercontext, cp,
Damien Miller708d21c2002-01-22 23:18:15 +1100415 buffer_ptr(&buffer), buffer_len(&buffer));
Damien Miller963f6b22002-02-19 15:21:23 +1100416 cipher_cleanup(&ciphercontext);
Damien Miller874d77b2000-10-14 16:23:11 +1100417 memset(&ciphercontext, 0, sizeof(ciphercontext));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000418 buffer_free(&buffer);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000419
Damien Miller95def091999-11-25 00:26:21 +1100420 check1 = buffer_get_char(&decrypted);
421 check2 = buffer_get_char(&decrypted);
422 if (check1 != buffer_get_char(&decrypted) ||
423 check2 != buffer_get_char(&decrypted)) {
424 if (strcmp(passphrase, "") != 0)
Ben Lindstromd0fca422001-03-26 13:44:06 +0000425 debug("Bad passphrase supplied for key file %.200s.",
426 filename);
Damien Miller95def091999-11-25 00:26:21 +1100427 /* Bad passphrase. */
428 buffer_free(&decrypted);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000429 goto fail;
Damien Miller95def091999-11-25 00:26:21 +1100430 }
431 /* Read the rest of the private key. */
Ben Lindstromd0fca422001-03-26 13:44:06 +0000432 buffer_get_bignum(&decrypted, prv->rsa->d);
433 buffer_get_bignum(&decrypted, prv->rsa->iqmp); /* u */
434 /* in SSL and SSH v1 p and q are exchanged */
435 buffer_get_bignum(&decrypted, prv->rsa->q); /* p */
436 buffer_get_bignum(&decrypted, prv->rsa->p); /* q */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000437
Ben Lindstromd0fca422001-03-26 13:44:06 +0000438 /* calculate p-1 and q-1 */
Damien Millerda755162002-01-22 23:09:22 +1100439 rsa_generate_additional_parameters(prv->rsa);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000440
Damien Miller95def091999-11-25 00:26:21 +1100441 buffer_free(&decrypted);
Damien Millered33d3b2003-03-15 11:36:18 +1100442
443 /* enable blinding */
444 if (RSA_blinding_on(prv->rsa, NULL) != 1) {
445 error("key_load_private_rsa1: RSA_blinding_on failed");
446 goto fail;
447 }
Ben Lindstromb257cca2001-03-05 04:59:27 +0000448 close(fd);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000449 return prv;
450
451fail:
452 if (commentp)
453 xfree(*commentp);
454 close(fd);
455 key_free(prv);
456 return NULL;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000457}
Damien Millereba71ba2000-04-29 23:57:08 +1000458
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000459Key *
Ben Lindstromd0fca422001-03-26 13:44:06 +0000460key_load_private_pem(int fd, int type, const char *passphrase,
461 char **commentp)
Damien Millereba71ba2000-04-29 23:57:08 +1000462{
Damien Millereba71ba2000-04-29 23:57:08 +1000463 FILE *fp;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100464 EVP_PKEY *pk = NULL;
Ben Lindstromd0fca422001-03-26 13:44:06 +0000465 Key *prv = NULL;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100466 char *name = "<no key>";
Damien Millereba71ba2000-04-29 23:57:08 +1000467
Damien Millereba71ba2000-04-29 23:57:08 +1000468 fp = fdopen(fd, "r");
469 if (fp == NULL) {
Ben Lindstrom15f33862001-04-16 02:00:02 +0000470 error("fdopen failed: %s", strerror(errno));
Ben Lindstromb257cca2001-03-05 04:59:27 +0000471 close(fd);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000472 return NULL;
Damien Millereba71ba2000-04-29 23:57:08 +1000473 }
Damien Miller0bc1bd82000-11-13 22:57:25 +1100474 pk = PEM_read_PrivateKey(fp, NULL, NULL, (char *)passphrase);
475 if (pk == NULL) {
Ben Lindstrom648772f2001-04-19 20:47:10 +0000476 debug("PEM_read_PrivateKey failed");
Damien Miller0bc1bd82000-11-13 22:57:25 +1100477 (void)ERR_get_error();
Ben Lindstromd0fca422001-03-26 13:44:06 +0000478 } else if (pk->type == EVP_PKEY_RSA &&
Damien Miller9f0f5c62001-12-21 14:45:46 +1100479 (type == KEY_UNSPEC||type==KEY_RSA)) {
Ben Lindstromd0fca422001-03-26 13:44:06 +0000480 prv = key_new(KEY_UNSPEC);
481 prv->rsa = EVP_PKEY_get1_RSA(pk);
482 prv->type = KEY_RSA;
483 name = "rsa w/o comment";
Damien Miller0bc1bd82000-11-13 22:57:25 +1100484#ifdef DEBUG_PK
Ben Lindstromd0fca422001-03-26 13:44:06 +0000485 RSA_print_fp(stderr, prv->rsa, 8);
Damien Millereba71ba2000-04-29 23:57:08 +1000486#endif
Damien Millered33d3b2003-03-15 11:36:18 +1100487 if (RSA_blinding_on(prv->rsa, NULL) != 1) {
488 error("key_load_private_pem: RSA_blinding_on failed");
489 key_free(prv);
490 prv = NULL;
491 }
Ben Lindstromd0fca422001-03-26 13:44:06 +0000492 } else if (pk->type == EVP_PKEY_DSA &&
Damien Miller9f0f5c62001-12-21 14:45:46 +1100493 (type == KEY_UNSPEC||type==KEY_DSA)) {
Ben Lindstromd0fca422001-03-26 13:44:06 +0000494 prv = key_new(KEY_UNSPEC);
495 prv->dsa = EVP_PKEY_get1_DSA(pk);
496 prv->type = KEY_DSA;
497 name = "dsa w/o comment";
Damien Miller0bc1bd82000-11-13 22:57:25 +1100498#ifdef DEBUG_PK
Ben Lindstromd0fca422001-03-26 13:44:06 +0000499 DSA_print_fp(stderr, prv->dsa, 8);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100500#endif
Damien Miller0bc1bd82000-11-13 22:57:25 +1100501 } else {
502 error("PEM_read_PrivateKey: mismatch or "
503 "unknown EVP_PKEY save_type %d", pk->save_type);
504 }
505 fclose(fp);
506 if (pk != NULL)
507 EVP_PKEY_free(pk);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000508 if (prv != NULL && commentp)
509 *commentp = xstrdup(name);
510 debug("read PEM private key done: type %s",
511 prv ? key_type(prv) : "<unknown>");
512 return prv;
Damien Millereba71ba2000-04-29 23:57:08 +1000513}
514
Damien Miller8275fad2006-03-15 12:06:23 +1100515int
Ben Lindstromd0fca422001-03-26 13:44:06 +0000516key_perm_ok(int fd, const char *filename)
Damien Millereba71ba2000-04-29 23:57:08 +1000517{
Damien Millereba71ba2000-04-29 23:57:08 +1000518 struct stat st;
519
Ben Lindstrom7aff2612001-09-23 13:53:22 +0000520 if (fstat(fd, &st) < 0)
521 return 0;
522 /*
523 * if a key owned by the user is accessed, then we check the
524 * permissions of the file. if the key owned by a different user,
525 * then we don't care.
526 */
Damien Millerb70b61f2000-09-16 16:25:12 +1100527#ifdef HAVE_CYGWIN
Damien Millercb5e44a2000-09-29 12:12:36 +1100528 if (check_ntsec(filename))
Damien Millerb70b61f2000-09-16 16:25:12 +1100529#endif
Ben Lindstrom7aff2612001-09-23 13:53:22 +0000530 if ((st.st_uid == getuid()) && (st.st_mode & 077) != 0) {
Damien Millereba71ba2000-04-29 23:57:08 +1000531 error("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
532 error("@ WARNING: UNPROTECTED PRIVATE KEY FILE! @");
533 error("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
Ben Lindstrom7aff2612001-09-23 13:53:22 +0000534 error("Permissions 0%3.3o for '%s' are too open.",
Damien Miller04bd8b02003-05-25 14:38:33 +1000535 (u_int)st.st_mode & 0777, filename);
Damien Millereba71ba2000-04-29 23:57:08 +1000536 error("It is recommended that your private key files are NOT accessible by others.");
Ben Lindstromd0fca422001-03-26 13:44:06 +0000537 error("This private key will be ignored.");
Damien Millereba71ba2000-04-29 23:57:08 +1000538 return 0;
539 }
Ben Lindstromd0fca422001-03-26 13:44:06 +0000540 return 1;
541}
Ben Lindstromb257cca2001-03-05 04:59:27 +0000542
Ben Lindstromd0fca422001-03-26 13:44:06 +0000543Key *
544key_load_private_type(int type, const char *filename, const char *passphrase,
Darren Tucker232b76f2006-05-06 17:41:51 +1000545 char **commentp, int *perm_ok)
Ben Lindstromd0fca422001-03-26 13:44:06 +0000546{
547 int fd;
548
549 fd = open(filename, O_RDONLY);
550 if (fd < 0)
551 return NULL;
552 if (!key_perm_ok(fd, filename)) {
Darren Tucker232b76f2006-05-06 17:41:51 +1000553 if (perm_ok != NULL)
554 *perm_ok = 0;
Ben Lindstrom15f33862001-04-16 02:00:02 +0000555 error("bad permissions: ignore key: %s", filename);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000556 close(fd);
557 return NULL;
558 }
Darren Tucker232b76f2006-05-06 17:41:51 +1000559 if (perm_ok != NULL)
560 *perm_ok = 1;
Ben Lindstromd0fca422001-03-26 13:44:06 +0000561 switch (type) {
562 case KEY_RSA1:
563 return key_load_private_rsa1(fd, filename, passphrase,
564 commentp);
565 /* closes fd */
Damien Millereba71ba2000-04-29 23:57:08 +1000566 case KEY_DSA:
Damien Miller0bc1bd82000-11-13 22:57:25 +1100567 case KEY_RSA:
568 case KEY_UNSPEC:
Ben Lindstromd0fca422001-03-26 13:44:06 +0000569 return key_load_private_pem(fd, type, passphrase, commentp);
570 /* closes fd */
Damien Millereba71ba2000-04-29 23:57:08 +1000571 default:
Ben Lindstromb257cca2001-03-05 04:59:27 +0000572 close(fd);
Damien Millereba71ba2000-04-29 23:57:08 +1000573 break;
574 }
Ben Lindstromd0fca422001-03-26 13:44:06 +0000575 return NULL;
576}
577
578Key *
579key_load_private(const char *filename, const char *passphrase,
580 char **commentp)
581{
Ben Lindstrom322915d2001-06-05 20:46:32 +0000582 Key *pub, *prv;
Ben Lindstromd0fca422001-03-26 13:44:06 +0000583 int fd;
584
585 fd = open(filename, O_RDONLY);
586 if (fd < 0)
587 return NULL;
588 if (!key_perm_ok(fd, filename)) {
Ben Lindstrom15f33862001-04-16 02:00:02 +0000589 error("bad permissions: ignore key: %s", filename);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000590 close(fd);
591 return NULL;
592 }
593 pub = key_load_public_rsa1(fd, filename, commentp);
594 lseek(fd, (off_t) 0, SEEK_SET); /* rewind */
595 if (pub == NULL) {
596 /* closes fd */
Ben Lindstrom322915d2001-06-05 20:46:32 +0000597 prv = key_load_private_pem(fd, KEY_UNSPEC, passphrase, NULL);
598 /* use the filename as a comment for PEM */
599 if (commentp && prv)
Ben Lindstrom2d0356f2001-06-05 21:13:57 +0000600 *commentp = xstrdup(filename);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000601 } else {
602 /* it's a SSH v1 key if the public key part is readable */
603 key_free(pub);
604 /* closes fd */
Ben Lindstrom322915d2001-06-05 20:46:32 +0000605 prv = key_load_private_rsa1(fd, filename, passphrase, NULL);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000606 }
Ben Lindstrom322915d2001-06-05 20:46:32 +0000607 return prv;
Damien Millereba71ba2000-04-29 23:57:08 +1000608}
Damien Millere4340be2000-09-16 13:29:08 +1100609
Ben Lindstrombba81212001-06-25 05:01:22 +0000610static int
Ben Lindstromd0fca422001-03-26 13:44:06 +0000611key_try_load_public(Key *k, const char *filename, char **commentp)
Damien Millere4340be2000-09-16 13:29:08 +1100612{
613 FILE *f;
Darren Tucker22cc7412004-12-06 22:47:41 +1100614 char line[SSH_MAX_PUBKEY_BYTES];
Damien Millere4340be2000-09-16 13:29:08 +1100615 char *cp;
Darren Tuckerf0f90982004-12-11 13:39:50 +1100616 u_long linenum = 0;
Damien Millere4340be2000-09-16 13:29:08 +1100617
618 f = fopen(filename, "r");
619 if (f != NULL) {
Darren Tucker22cc7412004-12-06 22:47:41 +1100620 while (read_keyfile_line(f, filename, line, sizeof(line),
621 &linenum) != -1) {
Damien Millere4340be2000-09-16 13:29:08 +1100622 cp = line;
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000623 switch (*cp) {
Damien Millere4340be2000-09-16 13:29:08 +1100624 case '#':
625 case '\n':
626 case '\0':
627 continue;
628 }
629 /* Skip leading whitespace. */
630 for (; *cp && (*cp == ' ' || *cp == '\t'); cp++)
631 ;
632 if (*cp) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100633 if (key_read(k, &cp) == 1) {
Damien Millere4340be2000-09-16 13:29:08 +1100634 if (commentp)
635 *commentp=xstrdup(filename);
636 fclose(f);
637 return 1;
638 }
639 }
640 }
641 fclose(f);
642 }
643 return 0;
644}
645
Ben Lindstromd0fca422001-03-26 13:44:06 +0000646/* load public key from ssh v1 private or any pubkey file */
647Key *
648key_load_public(const char *filename, char **commentp)
Damien Millere4340be2000-09-16 13:29:08 +1100649{
Ben Lindstromd0fca422001-03-26 13:44:06 +0000650 Key *pub;
651 char file[MAXPATHLEN];
Damien Millere4340be2000-09-16 13:29:08 +1100652
Damien Millerdb274722003-05-14 13:45:22 +1000653 /* try rsa1 private key */
Ben Lindstromd0fca422001-03-26 13:44:06 +0000654 pub = key_load_public_type(KEY_RSA1, filename, commentp);
655 if (pub != NULL)
656 return pub;
Damien Millerdb274722003-05-14 13:45:22 +1000657
658 /* try rsa1 public key */
659 pub = key_new(KEY_RSA1);
660 if (key_try_load_public(pub, filename, commentp) == 1)
661 return pub;
662 key_free(pub);
663
664 /* try ssh2 public key */
Ben Lindstromd0fca422001-03-26 13:44:06 +0000665 pub = key_new(KEY_UNSPEC);
666 if (key_try_load_public(pub, filename, commentp) == 1)
667 return pub;
668 if ((strlcpy(file, filename, sizeof file) < sizeof(file)) &&
669 (strlcat(file, ".pub", sizeof file) < sizeof(file)) &&
670 (key_try_load_public(pub, file, commentp) == 1))
671 return pub;
672 key_free(pub);
673 return NULL;
Damien Millere4340be2000-09-16 13:29:08 +1100674}