blob: 7231be55f32f61bee1c2dfe20e3d7d904abde7cd [file] [log] [blame]
Damien Millere7a1e5c2006-08-05 11:34:19 +10001/* $OpenBSD: authfile.c,v 1.73 2006/07/26 13:57:17 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 Miller8dbffe72006-08-05 11:02:17 +100043#include <sys/param.h>
Damien Millerd4a8b7e1999-10-27 13:42:43 +100044
Damien Miller0bc1bd82000-11-13 22:57:25 +110045#include <openssl/err.h>
Damien Millereba71ba2000-04-29 23:57:08 +100046#include <openssl/evp.h>
Ben Lindstrom226cfa02001-01-22 05:34:40 +000047#include <openssl/pem.h>
Damien Millereba71ba2000-04-29 23:57:08 +100048
Darren Tuckerba724052006-07-12 22:24:22 +100049#include <errno.h>
Damien Miller57cf6382006-07-10 21:13:46 +100050#include <fcntl.h>
Damien Millere7a1e5c2006-08-05 11:34:19 +100051#include <stdlib.h>
Damien Millere3476ed2006-07-24 14:13:33 +100052#include <string.h>
Damien Millere6b3b612006-07-24 14:01:23 +100053#include <unistd.h>
Damien Miller57cf6382006-07-10 21:13:46 +100054
Ben Lindstrom226cfa02001-01-22 05:34:40 +000055#include "cipher.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100056#include "xmalloc.h"
57#include "buffer.h"
58#include "bufaux.h"
Damien Millereba71ba2000-04-29 23:57:08 +100059#include "key.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000060#include "ssh.h"
61#include "log.h"
Ben Lindstrom31ca54a2001-02-09 02:11:24 +000062#include "authfile.h"
Damien Miller040b64f2002-01-22 23:10:04 +110063#include "rsa.h"
Darren Tuckerf0f90982004-12-11 13:39:50 +110064#include "misc.h"
Damien Millereccb9de2005-06-17 12:59:34 +100065#include "atomicio.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100066
Ben Lindstromd0fca422001-03-26 13:44:06 +000067/* Version identification string for SSH v1 identity files. */
Ben Lindstrom1170d712001-01-29 07:51:26 +000068static const char authfile_id_string[] =
69 "SSH PRIVATE KEY FILE FORMAT 1.1\n";
Damien Millerd4a8b7e1999-10-27 13:42:43 +100070
Damien Miller5428f641999-11-25 11:54:57 +110071/*
72 * Saves the authentication (private) key in a file, encrypting it with
73 * passphrase. The identification of the file (lowest 64 bits of n) will
74 * precede the key to provide identification of the key without needing a
75 * passphrase.
76 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100077
Ben Lindstrombba81212001-06-25 05:01:22 +000078static int
Ben Lindstromd0fca422001-03-26 13:44:06 +000079key_save_private_rsa1(Key *key, const char *filename, const char *passphrase,
80 const char *comment)
Damien Millerd4a8b7e1999-10-27 13:42:43 +100081{
Damien Miller95def091999-11-25 00:26:21 +110082 Buffer buffer, encrypted;
Damien Miller708d21c2002-01-22 23:18:15 +110083 u_char buf[100], *cp;
Damien Miller963f6b22002-02-19 15:21:23 +110084 int fd, i, cipher_num;
Damien Miller874d77b2000-10-14 16:23:11 +110085 CipherContext ciphercontext;
86 Cipher *cipher;
Darren Tucker3f9fdc72004-06-22 12:56:01 +100087 u_int32_t rnd;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100088
Damien Miller5428f641999-11-25 11:54:57 +110089 /*
90 * If the passphrase is empty, use SSH_CIPHER_NONE to ease converting
91 * to another cipher; otherwise use SSH_AUTHFILE_CIPHER.
92 */
Damien Miller963f6b22002-02-19 15:21:23 +110093 cipher_num = (strcmp(passphrase, "") == 0) ?
94 SSH_CIPHER_NONE : SSH_AUTHFILE_CIPHER;
95 if ((cipher = cipher_by_number(cipher_num)) == NULL)
Damien Miller874d77b2000-10-14 16:23:11 +110096 fatal("save_private_key_rsa: bad cipher");
Damien Millerd4a8b7e1999-10-27 13:42:43 +100097
Damien Miller95def091999-11-25 00:26:21 +110098 /* This buffer is used to built the secret part of the private key. */
99 buffer_init(&buffer);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000100
Damien Miller95def091999-11-25 00:26:21 +1100101 /* Put checkbytes for checking passphrase validity. */
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000102 rnd = arc4random();
103 buf[0] = rnd & 0xff;
104 buf[1] = (rnd >> 8) & 0xff;
Damien Miller95def091999-11-25 00:26:21 +1100105 buf[2] = buf[0];
106 buf[3] = buf[1];
107 buffer_append(&buffer, buf, 4);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000108
Damien Miller5428f641999-11-25 11:54:57 +1100109 /*
110 * Store the private key (n and e will not be stored because they
111 * will be stored in plain text, and storing them also in encrypted
112 * format would just give known plaintext).
113 */
Ben Lindstromd0fca422001-03-26 13:44:06 +0000114 buffer_put_bignum(&buffer, key->rsa->d);
115 buffer_put_bignum(&buffer, key->rsa->iqmp);
116 buffer_put_bignum(&buffer, key->rsa->q); /* reverse from SSL p */
117 buffer_put_bignum(&buffer, key->rsa->p); /* reverse from SSL q */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000118
Damien Miller95def091999-11-25 00:26:21 +1100119 /* Pad the part to be encrypted until its size is a multiple of 8. */
120 while (buffer_len(&buffer) % 8 != 0)
121 buffer_put_char(&buffer, 0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000122
Damien Miller95def091999-11-25 00:26:21 +1100123 /* This buffer will be used to contain the data in the file. */
124 buffer_init(&encrypted);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000125
Damien Miller95def091999-11-25 00:26:21 +1100126 /* First store keyfile id string. */
Ben Lindstrom1170d712001-01-29 07:51:26 +0000127 for (i = 0; authfile_id_string[i]; i++)
128 buffer_put_char(&encrypted, authfile_id_string[i]);
Damien Miller95def091999-11-25 00:26:21 +1100129 buffer_put_char(&encrypted, 0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000130
Damien Miller95def091999-11-25 00:26:21 +1100131 /* Store cipher type. */
Damien Miller963f6b22002-02-19 15:21:23 +1100132 buffer_put_char(&encrypted, cipher_num);
Damien Miller95def091999-11-25 00:26:21 +1100133 buffer_put_int(&encrypted, 0); /* For future extension */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000134
Damien Miller95def091999-11-25 00:26:21 +1100135 /* Store public key. This will be in plain text. */
Ben Lindstromd0fca422001-03-26 13:44:06 +0000136 buffer_put_int(&encrypted, BN_num_bits(key->rsa->n));
137 buffer_put_bignum(&encrypted, key->rsa->n);
138 buffer_put_bignum(&encrypted, key->rsa->e);
Ben Lindstrom664408d2001-06-09 01:42:01 +0000139 buffer_put_cstring(&encrypted, comment);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000140
Damien Miller95def091999-11-25 00:26:21 +1100141 /* Allocate space for the private part of the key in the buffer. */
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100142 cp = buffer_append_space(&encrypted, buffer_len(&buffer));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000143
Damien Miller963f6b22002-02-19 15:21:23 +1100144 cipher_set_key_string(&ciphercontext, cipher, passphrase,
145 CIPHER_ENCRYPT);
146 cipher_crypt(&ciphercontext, cp,
Damien Miller708d21c2002-01-22 23:18:15 +1100147 buffer_ptr(&buffer), buffer_len(&buffer));
Damien Miller963f6b22002-02-19 15:21:23 +1100148 cipher_cleanup(&ciphercontext);
Damien Miller874d77b2000-10-14 16:23:11 +1100149 memset(&ciphercontext, 0, sizeof(ciphercontext));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000150
Damien Miller95def091999-11-25 00:26:21 +1100151 /* Destroy temporary data. */
152 memset(buf, 0, sizeof(buf));
153 buffer_free(&buffer);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000154
Damien Miller037a0dc1999-12-07 15:38:31 +1100155 fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0600);
Ben Lindstrom15f33862001-04-16 02:00:02 +0000156 if (fd < 0) {
157 error("open %s failed: %s.", filename, strerror(errno));
Darren Tuckerd1d41b32003-09-22 21:01:27 +1000158 buffer_free(&encrypted);
Damien Miller95def091999-11-25 00:26:21 +1100159 return 0;
Ben Lindstrom15f33862001-04-16 02:00:02 +0000160 }
Damien Millereccb9de2005-06-17 12:59:34 +1000161 if (atomicio(vwrite, fd, buffer_ptr(&encrypted),
162 buffer_len(&encrypted)) != buffer_len(&encrypted)) {
Ben Lindstrom15f33862001-04-16 02:00:02 +0000163 error("write to key file %s failed: %s", filename,
Damien Miller9f0f5c62001-12-21 14:45:46 +1100164 strerror(errno));
Damien Miller95def091999-11-25 00:26:21 +1100165 buffer_free(&encrypted);
Damien Miller037a0dc1999-12-07 15:38:31 +1100166 close(fd);
Damien Miller78315eb2000-09-29 23:01:36 +1100167 unlink(filename);
Damien Miller95def091999-11-25 00:26:21 +1100168 return 0;
169 }
Damien Miller037a0dc1999-12-07 15:38:31 +1100170 close(fd);
Damien Miller95def091999-11-25 00:26:21 +1100171 buffer_free(&encrypted);
172 return 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000173}
174
Ben Lindstromd0fca422001-03-26 13:44:06 +0000175/* save SSH v2 key in OpenSSL PEM format */
Ben Lindstrombba81212001-06-25 05:01:22 +0000176static int
Ben Lindstromd0fca422001-03-26 13:44:06 +0000177key_save_private_pem(Key *key, const char *filename, const char *_passphrase,
178 const char *comment)
Damien Millereba71ba2000-04-29 23:57:08 +1000179{
180 FILE *fp;
181 int fd;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100182 int success = 0;
183 int len = strlen(_passphrase);
Ben Lindstrom90fd8142002-02-26 18:09:42 +0000184 u_char *passphrase = (len > 0) ? (u_char *)_passphrase : NULL;
Ben Lindstrom80cb27d2002-03-05 01:33:36 +0000185 const EVP_CIPHER *cipher = (len > 0) ? EVP_des_ede3_cbc() : NULL;
Damien Millereba71ba2000-04-29 23:57:08 +1000186
187 if (len > 0 && len <= 4) {
Ben Lindstrom15f33862001-04-16 02:00:02 +0000188 error("passphrase too short: have %d bytes, need > 4", len);
Damien Millereba71ba2000-04-29 23:57:08 +1000189 return 0;
190 }
191 fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0600);
192 if (fd < 0) {
Ben Lindstrom15f33862001-04-16 02:00:02 +0000193 error("open %s failed: %s.", filename, strerror(errno));
Damien Millereba71ba2000-04-29 23:57:08 +1000194 return 0;
195 }
196 fp = fdopen(fd, "w");
197 if (fp == NULL ) {
Ben Lindstrom15f33862001-04-16 02:00:02 +0000198 error("fdopen %s failed: %s.", filename, strerror(errno));
Damien Millereba71ba2000-04-29 23:57:08 +1000199 close(fd);
200 return 0;
201 }
Damien Miller0bc1bd82000-11-13 22:57:25 +1100202 switch (key->type) {
Ben Lindstromc1116602001-03-29 00:28:37 +0000203 case KEY_DSA:
204 success = PEM_write_DSAPrivateKey(fp, key->dsa,
205 cipher, passphrase, len, NULL, NULL);
206 break;
207 case KEY_RSA:
208 success = PEM_write_RSAPrivateKey(fp, key->rsa,
209 cipher, passphrase, len, NULL, NULL);
210 break;
Damien Millereba71ba2000-04-29 23:57:08 +1000211 }
212 fclose(fp);
213 return success;
214}
215
216int
Ben Lindstromd0fca422001-03-26 13:44:06 +0000217key_save_private(Key *key, const char *filename, const char *passphrase,
Damien Millereba71ba2000-04-29 23:57:08 +1000218 const char *comment)
219{
220 switch (key->type) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100221 case KEY_RSA1:
Ben Lindstromd0fca422001-03-26 13:44:06 +0000222 return key_save_private_rsa1(key, filename, passphrase,
223 comment);
Damien Millereba71ba2000-04-29 23:57:08 +1000224 case KEY_DSA:
Damien Miller0bc1bd82000-11-13 22:57:25 +1100225 case KEY_RSA:
Ben Lindstromd0fca422001-03-26 13:44:06 +0000226 return key_save_private_pem(key, filename, passphrase,
227 comment);
Damien Millereba71ba2000-04-29 23:57:08 +1000228 default:
229 break;
230 }
Ben Lindstrom15f33862001-04-16 02:00:02 +0000231 error("key_save_private: cannot save key type %d", key->type);
Damien Millereba71ba2000-04-29 23:57:08 +1000232 return 0;
233}
234
Damien Miller5428f641999-11-25 11:54:57 +1100235/*
Ben Lindstromd0fca422001-03-26 13:44:06 +0000236 * Loads the public part of the ssh v1 key file. Returns NULL if an error was
237 * encountered (the file does not exist or is not readable), and the key
Damien Miller5428f641999-11-25 11:54:57 +1100238 * otherwise.
239 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000240
Ben Lindstrombba81212001-06-25 05:01:22 +0000241static Key *
Ben Lindstromd0fca422001-03-26 13:44:06 +0000242key_load_public_rsa1(int fd, const char *filename, char **commentp)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000243{
Damien Miller95def091999-11-25 00:26:21 +1100244 Buffer buffer;
Ben Lindstromd0fca422001-03-26 13:44:06 +0000245 Key *pub;
Ben Lindstrom44adb8f2002-12-23 02:00:23 +0000246 struct stat st;
Damien Miller95def091999-11-25 00:26:21 +1100247 char *cp;
Damien Millereccb9de2005-06-17 12:59:34 +1000248 u_int i;
Darren Tucker1f8311c2004-05-13 16:39:33 +1000249 size_t len;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000250
Ben Lindstrom44adb8f2002-12-23 02:00:23 +0000251 if (fstat(fd, &st) < 0) {
252 error("fstat for key file %.200s failed: %.100s",
253 filename, strerror(errno));
254 return NULL;
255 }
Darren Tuckerf4b43712004-08-29 16:28:39 +1000256 if (st.st_size > 1*1024*1024) {
257 error("key file %.200s too large", filename);
258 return NULL;
259 }
Darren Tucker1f8311c2004-05-13 16:39:33 +1000260 len = (size_t)st.st_size; /* truncated */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000261
Damien Miller95def091999-11-25 00:26:21 +1100262 buffer_init(&buffer);
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100263 cp = buffer_append_space(&buffer, len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000264
Damien Millereccb9de2005-06-17 12:59:34 +1000265 if (atomicio(read, fd, cp, len) != len) {
Damien Miller95def091999-11-25 00:26:21 +1100266 debug("Read from key file %.200s failed: %.100s", filename,
Damien Millereba71ba2000-04-29 23:57:08 +1000267 strerror(errno));
Damien Miller95def091999-11-25 00:26:21 +1100268 buffer_free(&buffer);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000269 return NULL;
Damien Miller95def091999-11-25 00:26:21 +1100270 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000271
Ben Lindstrom1170d712001-01-29 07:51:26 +0000272 /* Check that it is at least big enough to contain the ID string. */
273 if (len < sizeof(authfile_id_string)) {
Damien Miller058655c2001-10-10 15:03:36 +1000274 debug3("Not a RSA1 key file %.200s.", filename);
Damien Miller95def091999-11-25 00:26:21 +1100275 buffer_free(&buffer);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000276 return NULL;
Damien Miller95def091999-11-25 00:26:21 +1100277 }
Damien Miller5428f641999-11-25 11:54:57 +1100278 /*
279 * Make sure it begins with the id string. Consume the id string
280 * from the buffer.
281 */
Ben Lindstrom1170d712001-01-29 07:51:26 +0000282 for (i = 0; i < sizeof(authfile_id_string); i++)
283 if (buffer_get_char(&buffer) != authfile_id_string[i]) {
Damien Miller058655c2001-10-10 15:03:36 +1000284 debug3("Not a RSA1 key file %.200s.", filename);
Damien Miller95def091999-11-25 00:26:21 +1100285 buffer_free(&buffer);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000286 return NULL;
Damien Miller95def091999-11-25 00:26:21 +1100287 }
288 /* Skip cipher type and reserved data. */
289 (void) buffer_get_char(&buffer); /* cipher type */
290 (void) buffer_get_int(&buffer); /* reserved */
291
292 /* Read the public key from the buffer. */
Ben Lindstromc5a7f4f2002-06-25 23:19:13 +0000293 (void) buffer_get_int(&buffer);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000294 pub = key_new(KEY_RSA1);
295 buffer_get_bignum(&buffer, pub->rsa->n);
296 buffer_get_bignum(&buffer, pub->rsa->e);
297 if (commentp)
298 *commentp = buffer_get_string(&buffer, NULL);
Damien Miller95def091999-11-25 00:26:21 +1100299 /* The encrypted private part is not parsed by this function. */
300
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000301 buffer_free(&buffer);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000302 return pub;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000303}
304
Ben Lindstromd0fca422001-03-26 13:44:06 +0000305/* load public key from private-key file, works only for SSH v1 */
306Key *
307key_load_public_type(int type, const char *filename, char **commentp)
Damien Millereba71ba2000-04-29 23:57:08 +1000308{
Ben Lindstromd0fca422001-03-26 13:44:06 +0000309 Key *pub;
310 int fd;
311
312 if (type == KEY_RSA1) {
313 fd = open(filename, O_RDONLY);
314 if (fd < 0)
315 return NULL;
316 pub = key_load_public_rsa1(fd, filename, commentp);
317 close(fd);
318 return pub;
Damien Millereba71ba2000-04-29 23:57:08 +1000319 }
Ben Lindstromd0fca422001-03-26 13:44:06 +0000320 return NULL;
Damien Millereba71ba2000-04-29 23:57:08 +1000321}
322
Damien Miller5428f641999-11-25 11:54:57 +1100323/*
324 * Loads the private key from the file. Returns 0 if an error is encountered
325 * (file does not exist or is not readable, or passphrase is bad). This
326 * initializes the private key.
327 * Assumes we are called under uid of the owner of the file.
328 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000329
Ben Lindstrombba81212001-06-25 05:01:22 +0000330static Key *
Ben Lindstromd0fca422001-03-26 13:44:06 +0000331key_load_private_rsa1(int fd, const char *filename, const char *passphrase,
332 char **commentp)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000333{
Damien Millereccb9de2005-06-17 12:59:34 +1000334 u_int i;
335 int check1, check2, cipher_type;
Darren Tucker1f8311c2004-05-13 16:39:33 +1000336 size_t len;
Damien Miller95def091999-11-25 00:26:21 +1100337 Buffer buffer, decrypted;
Damien Miller708d21c2002-01-22 23:18:15 +1100338 u_char *cp;
Damien Miller874d77b2000-10-14 16:23:11 +1100339 CipherContext ciphercontext;
340 Cipher *cipher;
Ben Lindstromd0fca422001-03-26 13:44:06 +0000341 Key *prv = NULL;
Ben Lindstrom44adb8f2002-12-23 02:00:23 +0000342 struct stat st;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000343
Ben Lindstrom44adb8f2002-12-23 02:00:23 +0000344 if (fstat(fd, &st) < 0) {
345 error("fstat for key file %.200s failed: %.100s",
346 filename, strerror(errno));
347 close(fd);
348 return NULL;
349 }
Darren Tucker1f8311c2004-05-13 16:39:33 +1000350 if (st.st_size > 1*1024*1024) {
Darren Tuckerf4b43712004-08-29 16:28:39 +1000351 error("key file %.200s too large", filename);
Darren Tucker1f8311c2004-05-13 16:39:33 +1000352 close(fd);
353 return (NULL);
354 }
355 len = (size_t)st.st_size; /* truncated */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000356
Damien Miller95def091999-11-25 00:26:21 +1100357 buffer_init(&buffer);
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100358 cp = buffer_append_space(&buffer, len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000359
Damien Millereccb9de2005-06-17 12:59:34 +1000360 if (atomicio(read, fd, cp, len) != len) {
Damien Miller95def091999-11-25 00:26:21 +1100361 debug("Read from key file %.200s failed: %.100s", filename,
Damien Miller0bc1bd82000-11-13 22:57:25 +1100362 strerror(errno));
Damien Miller95def091999-11-25 00:26:21 +1100363 buffer_free(&buffer);
Damien Miller037a0dc1999-12-07 15:38:31 +1100364 close(fd);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000365 return NULL;
Damien Miller95def091999-11-25 00:26:21 +1100366 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000367
Ben Lindstrom1170d712001-01-29 07:51:26 +0000368 /* Check that it is at least big enough to contain the ID string. */
369 if (len < sizeof(authfile_id_string)) {
Damien Miller058655c2001-10-10 15:03:36 +1000370 debug3("Not a RSA1 key file %.200s.", filename);
Damien Miller95def091999-11-25 00:26:21 +1100371 buffer_free(&buffer);
Ben Lindstromb257cca2001-03-05 04:59:27 +0000372 close(fd);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000373 return NULL;
Damien Miller95def091999-11-25 00:26:21 +1100374 }
Damien Miller5428f641999-11-25 11:54:57 +1100375 /*
376 * Make sure it begins with the id string. Consume the id string
377 * from the buffer.
378 */
Ben Lindstrom1170d712001-01-29 07:51:26 +0000379 for (i = 0; i < sizeof(authfile_id_string); i++)
380 if (buffer_get_char(&buffer) != authfile_id_string[i]) {
Damien Miller058655c2001-10-10 15:03:36 +1000381 debug3("Not a RSA1 key file %.200s.", filename);
Damien Miller95def091999-11-25 00:26:21 +1100382 buffer_free(&buffer);
Ben Lindstromb257cca2001-03-05 04:59:27 +0000383 close(fd);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000384 return NULL;
Damien Miller95def091999-11-25 00:26:21 +1100385 }
Ben Lindstromb257cca2001-03-05 04:59:27 +0000386
Damien Miller95def091999-11-25 00:26:21 +1100387 /* Read cipher type. */
388 cipher_type = buffer_get_char(&buffer);
389 (void) buffer_get_int(&buffer); /* Reserved data. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000390
Damien Miller95def091999-11-25 00:26:21 +1100391 /* Read the public key from the buffer. */
Ben Lindstromc5a7f4f2002-06-25 23:19:13 +0000392 (void) buffer_get_int(&buffer);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000393 prv = key_new_private(KEY_RSA1);
394
395 buffer_get_bignum(&buffer, prv->rsa->n);
396 buffer_get_bignum(&buffer, prv->rsa->e);
397 if (commentp)
398 *commentp = buffer_get_string(&buffer, NULL);
Damien Miller95def091999-11-25 00:26:21 +1100399 else
400 xfree(buffer_get_string(&buffer, NULL));
401
402 /* Check that it is a supported cipher. */
Damien Miller874d77b2000-10-14 16:23:11 +1100403 cipher = cipher_by_number(cipher_type);
404 if (cipher == NULL) {
405 debug("Unsupported cipher %d used in key file %.200s.",
406 cipher_type, filename);
Damien Miller95def091999-11-25 00:26:21 +1100407 buffer_free(&buffer);
408 goto fail;
409 }
410 /* Initialize space for decrypted data. */
411 buffer_init(&decrypted);
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100412 cp = buffer_append_space(&decrypted, buffer_len(&buffer));
Damien Miller95def091999-11-25 00:26:21 +1100413
414 /* Rest of the buffer is encrypted. Decrypt it using the passphrase. */
Damien Miller963f6b22002-02-19 15:21:23 +1100415 cipher_set_key_string(&ciphercontext, cipher, passphrase,
416 CIPHER_DECRYPT);
417 cipher_crypt(&ciphercontext, cp,
Damien Miller708d21c2002-01-22 23:18:15 +1100418 buffer_ptr(&buffer), buffer_len(&buffer));
Damien Miller963f6b22002-02-19 15:21:23 +1100419 cipher_cleanup(&ciphercontext);
Damien Miller874d77b2000-10-14 16:23:11 +1100420 memset(&ciphercontext, 0, sizeof(ciphercontext));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000421 buffer_free(&buffer);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000422
Damien Miller95def091999-11-25 00:26:21 +1100423 check1 = buffer_get_char(&decrypted);
424 check2 = buffer_get_char(&decrypted);
425 if (check1 != buffer_get_char(&decrypted) ||
426 check2 != buffer_get_char(&decrypted)) {
427 if (strcmp(passphrase, "") != 0)
Ben Lindstromd0fca422001-03-26 13:44:06 +0000428 debug("Bad passphrase supplied for key file %.200s.",
429 filename);
Damien Miller95def091999-11-25 00:26:21 +1100430 /* Bad passphrase. */
431 buffer_free(&decrypted);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000432 goto fail;
Damien Miller95def091999-11-25 00:26:21 +1100433 }
434 /* Read the rest of the private key. */
Ben Lindstromd0fca422001-03-26 13:44:06 +0000435 buffer_get_bignum(&decrypted, prv->rsa->d);
436 buffer_get_bignum(&decrypted, prv->rsa->iqmp); /* u */
437 /* in SSL and SSH v1 p and q are exchanged */
438 buffer_get_bignum(&decrypted, prv->rsa->q); /* p */
439 buffer_get_bignum(&decrypted, prv->rsa->p); /* q */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000440
Ben Lindstromd0fca422001-03-26 13:44:06 +0000441 /* calculate p-1 and q-1 */
Damien Millerda755162002-01-22 23:09:22 +1100442 rsa_generate_additional_parameters(prv->rsa);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000443
Damien Miller95def091999-11-25 00:26:21 +1100444 buffer_free(&decrypted);
Damien Millered33d3b2003-03-15 11:36:18 +1100445
446 /* enable blinding */
447 if (RSA_blinding_on(prv->rsa, NULL) != 1) {
448 error("key_load_private_rsa1: RSA_blinding_on failed");
449 goto fail;
450 }
Ben Lindstromb257cca2001-03-05 04:59:27 +0000451 close(fd);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000452 return prv;
453
454fail:
455 if (commentp)
456 xfree(*commentp);
457 close(fd);
458 key_free(prv);
459 return NULL;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000460}
Damien Millereba71ba2000-04-29 23:57:08 +1000461
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000462Key *
Ben Lindstromd0fca422001-03-26 13:44:06 +0000463key_load_private_pem(int fd, int type, const char *passphrase,
464 char **commentp)
Damien Millereba71ba2000-04-29 23:57:08 +1000465{
Damien Millereba71ba2000-04-29 23:57:08 +1000466 FILE *fp;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100467 EVP_PKEY *pk = NULL;
Ben Lindstromd0fca422001-03-26 13:44:06 +0000468 Key *prv = NULL;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100469 char *name = "<no key>";
Damien Millereba71ba2000-04-29 23:57:08 +1000470
Damien Millereba71ba2000-04-29 23:57:08 +1000471 fp = fdopen(fd, "r");
472 if (fp == NULL) {
Ben Lindstrom15f33862001-04-16 02:00:02 +0000473 error("fdopen failed: %s", strerror(errno));
Ben Lindstromb257cca2001-03-05 04:59:27 +0000474 close(fd);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000475 return NULL;
Damien Millereba71ba2000-04-29 23:57:08 +1000476 }
Damien Miller0bc1bd82000-11-13 22:57:25 +1100477 pk = PEM_read_PrivateKey(fp, NULL, NULL, (char *)passphrase);
478 if (pk == NULL) {
Ben Lindstrom648772f2001-04-19 20:47:10 +0000479 debug("PEM_read_PrivateKey failed");
Damien Miller0bc1bd82000-11-13 22:57:25 +1100480 (void)ERR_get_error();
Ben Lindstromd0fca422001-03-26 13:44:06 +0000481 } else if (pk->type == EVP_PKEY_RSA &&
Damien Miller9f0f5c62001-12-21 14:45:46 +1100482 (type == KEY_UNSPEC||type==KEY_RSA)) {
Ben Lindstromd0fca422001-03-26 13:44:06 +0000483 prv = key_new(KEY_UNSPEC);
484 prv->rsa = EVP_PKEY_get1_RSA(pk);
485 prv->type = KEY_RSA;
486 name = "rsa w/o comment";
Damien Miller0bc1bd82000-11-13 22:57:25 +1100487#ifdef DEBUG_PK
Ben Lindstromd0fca422001-03-26 13:44:06 +0000488 RSA_print_fp(stderr, prv->rsa, 8);
Damien Millereba71ba2000-04-29 23:57:08 +1000489#endif
Damien Millered33d3b2003-03-15 11:36:18 +1100490 if (RSA_blinding_on(prv->rsa, NULL) != 1) {
491 error("key_load_private_pem: RSA_blinding_on failed");
492 key_free(prv);
493 prv = NULL;
494 }
Ben Lindstromd0fca422001-03-26 13:44:06 +0000495 } else if (pk->type == EVP_PKEY_DSA &&
Damien Miller9f0f5c62001-12-21 14:45:46 +1100496 (type == KEY_UNSPEC||type==KEY_DSA)) {
Ben Lindstromd0fca422001-03-26 13:44:06 +0000497 prv = key_new(KEY_UNSPEC);
498 prv->dsa = EVP_PKEY_get1_DSA(pk);
499 prv->type = KEY_DSA;
500 name = "dsa w/o comment";
Damien Miller0bc1bd82000-11-13 22:57:25 +1100501#ifdef DEBUG_PK
Ben Lindstromd0fca422001-03-26 13:44:06 +0000502 DSA_print_fp(stderr, prv->dsa, 8);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100503#endif
Damien Miller0bc1bd82000-11-13 22:57:25 +1100504 } else {
505 error("PEM_read_PrivateKey: mismatch or "
506 "unknown EVP_PKEY save_type %d", pk->save_type);
507 }
508 fclose(fp);
509 if (pk != NULL)
510 EVP_PKEY_free(pk);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000511 if (prv != NULL && commentp)
512 *commentp = xstrdup(name);
513 debug("read PEM private key done: type %s",
514 prv ? key_type(prv) : "<unknown>");
515 return prv;
Damien Millereba71ba2000-04-29 23:57:08 +1000516}
517
Damien Miller8275fad2006-03-15 12:06:23 +1100518int
Ben Lindstromd0fca422001-03-26 13:44:06 +0000519key_perm_ok(int fd, const char *filename)
Damien Millereba71ba2000-04-29 23:57:08 +1000520{
Damien Millereba71ba2000-04-29 23:57:08 +1000521 struct stat st;
522
Ben Lindstrom7aff2612001-09-23 13:53:22 +0000523 if (fstat(fd, &st) < 0)
524 return 0;
525 /*
526 * if a key owned by the user is accessed, then we check the
527 * permissions of the file. if the key owned by a different user,
528 * then we don't care.
529 */
Damien Millerb70b61f2000-09-16 16:25:12 +1100530#ifdef HAVE_CYGWIN
Damien Millercb5e44a2000-09-29 12:12:36 +1100531 if (check_ntsec(filename))
Damien Millerb70b61f2000-09-16 16:25:12 +1100532#endif
Ben Lindstrom7aff2612001-09-23 13:53:22 +0000533 if ((st.st_uid == getuid()) && (st.st_mode & 077) != 0) {
Damien Millereba71ba2000-04-29 23:57:08 +1000534 error("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
535 error("@ WARNING: UNPROTECTED PRIVATE KEY FILE! @");
536 error("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
Ben Lindstrom7aff2612001-09-23 13:53:22 +0000537 error("Permissions 0%3.3o for '%s' are too open.",
Damien Miller04bd8b02003-05-25 14:38:33 +1000538 (u_int)st.st_mode & 0777, filename);
Damien Millereba71ba2000-04-29 23:57:08 +1000539 error("It is recommended that your private key files are NOT accessible by others.");
Ben Lindstromd0fca422001-03-26 13:44:06 +0000540 error("This private key will be ignored.");
Damien Millereba71ba2000-04-29 23:57:08 +1000541 return 0;
542 }
Ben Lindstromd0fca422001-03-26 13:44:06 +0000543 return 1;
544}
Ben Lindstromb257cca2001-03-05 04:59:27 +0000545
Ben Lindstromd0fca422001-03-26 13:44:06 +0000546Key *
547key_load_private_type(int type, const char *filename, const char *passphrase,
Darren Tucker232b76f2006-05-06 17:41:51 +1000548 char **commentp, int *perm_ok)
Ben Lindstromd0fca422001-03-26 13:44:06 +0000549{
550 int fd;
551
552 fd = open(filename, O_RDONLY);
553 if (fd < 0)
554 return NULL;
555 if (!key_perm_ok(fd, filename)) {
Darren Tucker232b76f2006-05-06 17:41:51 +1000556 if (perm_ok != NULL)
557 *perm_ok = 0;
Ben Lindstrom15f33862001-04-16 02:00:02 +0000558 error("bad permissions: ignore key: %s", filename);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000559 close(fd);
560 return NULL;
561 }
Darren Tucker232b76f2006-05-06 17:41:51 +1000562 if (perm_ok != NULL)
563 *perm_ok = 1;
Ben Lindstromd0fca422001-03-26 13:44:06 +0000564 switch (type) {
565 case KEY_RSA1:
566 return key_load_private_rsa1(fd, filename, passphrase,
567 commentp);
568 /* closes fd */
Damien Millereba71ba2000-04-29 23:57:08 +1000569 case KEY_DSA:
Damien Miller0bc1bd82000-11-13 22:57:25 +1100570 case KEY_RSA:
571 case KEY_UNSPEC:
Ben Lindstromd0fca422001-03-26 13:44:06 +0000572 return key_load_private_pem(fd, type, passphrase, commentp);
573 /* closes fd */
Damien Millereba71ba2000-04-29 23:57:08 +1000574 default:
Ben Lindstromb257cca2001-03-05 04:59:27 +0000575 close(fd);
Damien Millereba71ba2000-04-29 23:57:08 +1000576 break;
577 }
Ben Lindstromd0fca422001-03-26 13:44:06 +0000578 return NULL;
579}
580
581Key *
582key_load_private(const char *filename, const char *passphrase,
583 char **commentp)
584{
Ben Lindstrom322915d2001-06-05 20:46:32 +0000585 Key *pub, *prv;
Ben Lindstromd0fca422001-03-26 13:44:06 +0000586 int fd;
587
588 fd = open(filename, O_RDONLY);
589 if (fd < 0)
590 return NULL;
591 if (!key_perm_ok(fd, filename)) {
Ben Lindstrom15f33862001-04-16 02:00:02 +0000592 error("bad permissions: ignore key: %s", filename);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000593 close(fd);
594 return NULL;
595 }
596 pub = key_load_public_rsa1(fd, filename, commentp);
597 lseek(fd, (off_t) 0, SEEK_SET); /* rewind */
598 if (pub == NULL) {
599 /* closes fd */
Ben Lindstrom322915d2001-06-05 20:46:32 +0000600 prv = key_load_private_pem(fd, KEY_UNSPEC, passphrase, NULL);
601 /* use the filename as a comment for PEM */
602 if (commentp && prv)
Ben Lindstrom2d0356f2001-06-05 21:13:57 +0000603 *commentp = xstrdup(filename);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000604 } else {
605 /* it's a SSH v1 key if the public key part is readable */
606 key_free(pub);
607 /* closes fd */
Ben Lindstrom322915d2001-06-05 20:46:32 +0000608 prv = key_load_private_rsa1(fd, filename, passphrase, NULL);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000609 }
Ben Lindstrom322915d2001-06-05 20:46:32 +0000610 return prv;
Damien Millereba71ba2000-04-29 23:57:08 +1000611}
Damien Millere4340be2000-09-16 13:29:08 +1100612
Ben Lindstrombba81212001-06-25 05:01:22 +0000613static int
Ben Lindstromd0fca422001-03-26 13:44:06 +0000614key_try_load_public(Key *k, const char *filename, char **commentp)
Damien Millere4340be2000-09-16 13:29:08 +1100615{
616 FILE *f;
Darren Tucker22cc7412004-12-06 22:47:41 +1100617 char line[SSH_MAX_PUBKEY_BYTES];
Damien Millere4340be2000-09-16 13:29:08 +1100618 char *cp;
Darren Tuckerf0f90982004-12-11 13:39:50 +1100619 u_long linenum = 0;
Damien Millere4340be2000-09-16 13:29:08 +1100620
621 f = fopen(filename, "r");
622 if (f != NULL) {
Darren Tucker22cc7412004-12-06 22:47:41 +1100623 while (read_keyfile_line(f, filename, line, sizeof(line),
624 &linenum) != -1) {
Damien Millere4340be2000-09-16 13:29:08 +1100625 cp = line;
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000626 switch (*cp) {
Damien Millere4340be2000-09-16 13:29:08 +1100627 case '#':
628 case '\n':
629 case '\0':
630 continue;
631 }
632 /* Skip leading whitespace. */
633 for (; *cp && (*cp == ' ' || *cp == '\t'); cp++)
634 ;
635 if (*cp) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100636 if (key_read(k, &cp) == 1) {
Damien Millere4340be2000-09-16 13:29:08 +1100637 if (commentp)
638 *commentp=xstrdup(filename);
639 fclose(f);
640 return 1;
641 }
642 }
643 }
644 fclose(f);
645 }
646 return 0;
647}
648
Ben Lindstromd0fca422001-03-26 13:44:06 +0000649/* load public key from ssh v1 private or any pubkey file */
650Key *
651key_load_public(const char *filename, char **commentp)
Damien Millere4340be2000-09-16 13:29:08 +1100652{
Ben Lindstromd0fca422001-03-26 13:44:06 +0000653 Key *pub;
654 char file[MAXPATHLEN];
Damien Millere4340be2000-09-16 13:29:08 +1100655
Damien Millerdb274722003-05-14 13:45:22 +1000656 /* try rsa1 private key */
Ben Lindstromd0fca422001-03-26 13:44:06 +0000657 pub = key_load_public_type(KEY_RSA1, filename, commentp);
658 if (pub != NULL)
659 return pub;
Damien Millerdb274722003-05-14 13:45:22 +1000660
661 /* try rsa1 public key */
662 pub = key_new(KEY_RSA1);
663 if (key_try_load_public(pub, filename, commentp) == 1)
664 return pub;
665 key_free(pub);
666
667 /* try ssh2 public key */
Ben Lindstromd0fca422001-03-26 13:44:06 +0000668 pub = key_new(KEY_UNSPEC);
669 if (key_try_load_public(pub, filename, commentp) == 1)
670 return pub;
671 if ((strlcpy(file, filename, sizeof file) < sizeof(file)) &&
672 (strlcat(file, ".pub", sizeof file) < sizeof(file)) &&
673 (key_try_load_public(pub, file, commentp) == 1))
674 return pub;
675 key_free(pub);
676 return NULL;
Damien Millere4340be2000-09-16 13:29:08 +1100677}