blob: 675085646479ab091e212670d94d839cc4d3ff0f [file] [log] [blame]
Damien Millerd7834352006-08-05 12:39:39 +10001/* $OpenBSD: authfile.c,v 1.76 2006/08/03 03:34:41 deraadt 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 Millerd7834352006-08-05 12:39:39 +100044#include <sys/uio.h>
Damien Millerd4a8b7e1999-10-27 13:42:43 +100045
Damien Miller0bc1bd82000-11-13 22:57:25 +110046#include <openssl/err.h>
Damien Millereba71ba2000-04-29 23:57:08 +100047#include <openssl/evp.h>
Ben Lindstrom226cfa02001-01-22 05:34:40 +000048#include <openssl/pem.h>
Damien Millereba71ba2000-04-29 23:57:08 +100049
Darren Tuckerba724052006-07-12 22:24:22 +100050#include <errno.h>
Damien Miller57cf6382006-07-10 21:13:46 +100051#include <fcntl.h>
Damien Millera7a73ee2006-08-05 11:37:59 +100052#include <stdio.h>
Damien Millere7a1e5c2006-08-05 11:34:19 +100053#include <stdlib.h>
Damien Millere3476ed2006-07-24 14:13:33 +100054#include <string.h>
Damien Millere6b3b612006-07-24 14:01:23 +100055#include <unistd.h>
Damien Miller57cf6382006-07-10 21:13:46 +100056
Damien Millerd4a8b7e1999-10-27 13:42:43 +100057#include "xmalloc.h"
Damien Millerd7834352006-08-05 12:39:39 +100058#include "cipher.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100059#include "buffer.h"
Damien Millereba71ba2000-04-29 23:57:08 +100060#include "key.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000061#include "ssh.h"
62#include "log.h"
Ben Lindstrom31ca54a2001-02-09 02:11:24 +000063#include "authfile.h"
Damien Miller040b64f2002-01-22 23:10:04 +110064#include "rsa.h"
Darren Tuckerf0f90982004-12-11 13:39:50 +110065#include "misc.h"
Damien Millereccb9de2005-06-17 12:59:34 +100066#include "atomicio.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100067
Ben Lindstromd0fca422001-03-26 13:44:06 +000068/* Version identification string for SSH v1 identity files. */
Ben Lindstrom1170d712001-01-29 07:51:26 +000069static const char authfile_id_string[] =
70 "SSH PRIVATE KEY FILE FORMAT 1.1\n";
Damien Millerd4a8b7e1999-10-27 13:42:43 +100071
Damien Miller5428f641999-11-25 11:54:57 +110072/*
73 * Saves the authentication (private) key in a file, encrypting it with
74 * passphrase. The identification of the file (lowest 64 bits of n) will
75 * precede the key to provide identification of the key without needing a
76 * passphrase.
77 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100078
Ben Lindstrombba81212001-06-25 05:01:22 +000079static int
Ben Lindstromd0fca422001-03-26 13:44:06 +000080key_save_private_rsa1(Key *key, const char *filename, const char *passphrase,
81 const char *comment)
Damien Millerd4a8b7e1999-10-27 13:42:43 +100082{
Damien Miller95def091999-11-25 00:26:21 +110083 Buffer buffer, encrypted;
Damien Miller708d21c2002-01-22 23:18:15 +110084 u_char buf[100], *cp;
Damien Miller963f6b22002-02-19 15:21:23 +110085 int fd, i, cipher_num;
Damien Miller874d77b2000-10-14 16:23:11 +110086 CipherContext ciphercontext;
87 Cipher *cipher;
Darren Tucker3f9fdc72004-06-22 12:56:01 +100088 u_int32_t rnd;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100089
Damien Miller5428f641999-11-25 11:54:57 +110090 /*
91 * If the passphrase is empty, use SSH_CIPHER_NONE to ease converting
92 * to another cipher; otherwise use SSH_AUTHFILE_CIPHER.
93 */
Damien Miller963f6b22002-02-19 15:21:23 +110094 cipher_num = (strcmp(passphrase, "") == 0) ?
95 SSH_CIPHER_NONE : SSH_AUTHFILE_CIPHER;
96 if ((cipher = cipher_by_number(cipher_num)) == NULL)
Damien Miller874d77b2000-10-14 16:23:11 +110097 fatal("save_private_key_rsa: bad cipher");
Damien Millerd4a8b7e1999-10-27 13:42:43 +100098
Damien Miller95def091999-11-25 00:26:21 +110099 /* This buffer is used to built the secret part of the private key. */
100 buffer_init(&buffer);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000101
Damien Miller95def091999-11-25 00:26:21 +1100102 /* Put checkbytes for checking passphrase validity. */
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000103 rnd = arc4random();
104 buf[0] = rnd & 0xff;
105 buf[1] = (rnd >> 8) & 0xff;
Damien Miller95def091999-11-25 00:26:21 +1100106 buf[2] = buf[0];
107 buf[3] = buf[1];
108 buffer_append(&buffer, buf, 4);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000109
Damien Miller5428f641999-11-25 11:54:57 +1100110 /*
111 * Store the private key (n and e will not be stored because they
112 * will be stored in plain text, and storing them also in encrypted
113 * format would just give known plaintext).
114 */
Ben Lindstromd0fca422001-03-26 13:44:06 +0000115 buffer_put_bignum(&buffer, key->rsa->d);
116 buffer_put_bignum(&buffer, key->rsa->iqmp);
117 buffer_put_bignum(&buffer, key->rsa->q); /* reverse from SSL p */
118 buffer_put_bignum(&buffer, key->rsa->p); /* reverse from SSL q */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000119
Damien Miller95def091999-11-25 00:26:21 +1100120 /* Pad the part to be encrypted until its size is a multiple of 8. */
121 while (buffer_len(&buffer) % 8 != 0)
122 buffer_put_char(&buffer, 0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000123
Damien Miller95def091999-11-25 00:26:21 +1100124 /* This buffer will be used to contain the data in the file. */
125 buffer_init(&encrypted);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000126
Damien Miller95def091999-11-25 00:26:21 +1100127 /* First store keyfile id string. */
Ben Lindstrom1170d712001-01-29 07:51:26 +0000128 for (i = 0; authfile_id_string[i]; i++)
129 buffer_put_char(&encrypted, authfile_id_string[i]);
Damien Miller95def091999-11-25 00:26:21 +1100130 buffer_put_char(&encrypted, 0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000131
Damien Miller95def091999-11-25 00:26:21 +1100132 /* Store cipher type. */
Damien Miller963f6b22002-02-19 15:21:23 +1100133 buffer_put_char(&encrypted, cipher_num);
Damien Miller95def091999-11-25 00:26:21 +1100134 buffer_put_int(&encrypted, 0); /* For future extension */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000135
Damien Miller95def091999-11-25 00:26:21 +1100136 /* Store public key. This will be in plain text. */
Ben Lindstromd0fca422001-03-26 13:44:06 +0000137 buffer_put_int(&encrypted, BN_num_bits(key->rsa->n));
138 buffer_put_bignum(&encrypted, key->rsa->n);
139 buffer_put_bignum(&encrypted, key->rsa->e);
Ben Lindstrom664408d2001-06-09 01:42:01 +0000140 buffer_put_cstring(&encrypted, comment);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000141
Damien Miller95def091999-11-25 00:26:21 +1100142 /* Allocate space for the private part of the key in the buffer. */
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100143 cp = buffer_append_space(&encrypted, buffer_len(&buffer));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000144
Damien Miller963f6b22002-02-19 15:21:23 +1100145 cipher_set_key_string(&ciphercontext, cipher, passphrase,
146 CIPHER_ENCRYPT);
147 cipher_crypt(&ciphercontext, cp,
Damien Miller708d21c2002-01-22 23:18:15 +1100148 buffer_ptr(&buffer), buffer_len(&buffer));
Damien Miller963f6b22002-02-19 15:21:23 +1100149 cipher_cleanup(&ciphercontext);
Damien Miller874d77b2000-10-14 16:23:11 +1100150 memset(&ciphercontext, 0, sizeof(ciphercontext));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000151
Damien Miller95def091999-11-25 00:26:21 +1100152 /* Destroy temporary data. */
153 memset(buf, 0, sizeof(buf));
154 buffer_free(&buffer);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000155
Damien Miller037a0dc1999-12-07 15:38:31 +1100156 fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0600);
Ben Lindstrom15f33862001-04-16 02:00:02 +0000157 if (fd < 0) {
158 error("open %s failed: %s.", filename, strerror(errno));
Darren Tuckerd1d41b32003-09-22 21:01:27 +1000159 buffer_free(&encrypted);
Damien Miller95def091999-11-25 00:26:21 +1100160 return 0;
Ben Lindstrom15f33862001-04-16 02:00:02 +0000161 }
Damien Millereccb9de2005-06-17 12:59:34 +1000162 if (atomicio(vwrite, fd, buffer_ptr(&encrypted),
163 buffer_len(&encrypted)) != buffer_len(&encrypted)) {
Ben Lindstrom15f33862001-04-16 02:00:02 +0000164 error("write to key file %s failed: %s", filename,
Damien Miller9f0f5c62001-12-21 14:45:46 +1100165 strerror(errno));
Damien Miller95def091999-11-25 00:26:21 +1100166 buffer_free(&encrypted);
Damien Miller037a0dc1999-12-07 15:38:31 +1100167 close(fd);
Damien Miller78315eb2000-09-29 23:01:36 +1100168 unlink(filename);
Damien Miller95def091999-11-25 00:26:21 +1100169 return 0;
170 }
Damien Miller037a0dc1999-12-07 15:38:31 +1100171 close(fd);
Damien Miller95def091999-11-25 00:26:21 +1100172 buffer_free(&encrypted);
173 return 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000174}
175
Ben Lindstromd0fca422001-03-26 13:44:06 +0000176/* save SSH v2 key in OpenSSL PEM format */
Ben Lindstrombba81212001-06-25 05:01:22 +0000177static int
Ben Lindstromd0fca422001-03-26 13:44:06 +0000178key_save_private_pem(Key *key, const char *filename, const char *_passphrase,
179 const char *comment)
Damien Millereba71ba2000-04-29 23:57:08 +1000180{
181 FILE *fp;
182 int fd;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100183 int success = 0;
184 int len = strlen(_passphrase);
Ben Lindstrom90fd8142002-02-26 18:09:42 +0000185 u_char *passphrase = (len > 0) ? (u_char *)_passphrase : NULL;
Ben Lindstrom80cb27d2002-03-05 01:33:36 +0000186 const EVP_CIPHER *cipher = (len > 0) ? EVP_des_ede3_cbc() : NULL;
Damien Millereba71ba2000-04-29 23:57:08 +1000187
188 if (len > 0 && len <= 4) {
Ben Lindstrom15f33862001-04-16 02:00:02 +0000189 error("passphrase too short: have %d bytes, need > 4", len);
Damien Millereba71ba2000-04-29 23:57:08 +1000190 return 0;
191 }
192 fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0600);
193 if (fd < 0) {
Ben Lindstrom15f33862001-04-16 02:00:02 +0000194 error("open %s failed: %s.", filename, strerror(errno));
Damien Millereba71ba2000-04-29 23:57:08 +1000195 return 0;
196 }
197 fp = fdopen(fd, "w");
Damien Miller4dec5d72006-08-05 11:38:40 +1000198 if (fp == NULL) {
Ben Lindstrom15f33862001-04-16 02:00:02 +0000199 error("fdopen %s failed: %s.", filename, strerror(errno));
Damien Millereba71ba2000-04-29 23:57:08 +1000200 close(fd);
201 return 0;
202 }
Damien Miller0bc1bd82000-11-13 22:57:25 +1100203 switch (key->type) {
Ben Lindstromc1116602001-03-29 00:28:37 +0000204 case KEY_DSA:
205 success = PEM_write_DSAPrivateKey(fp, key->dsa,
206 cipher, passphrase, len, NULL, NULL);
207 break;
208 case KEY_RSA:
209 success = PEM_write_RSAPrivateKey(fp, key->rsa,
210 cipher, passphrase, len, NULL, NULL);
211 break;
Damien Millereba71ba2000-04-29 23:57:08 +1000212 }
213 fclose(fp);
214 return success;
215}
216
217int
Ben Lindstromd0fca422001-03-26 13:44:06 +0000218key_save_private(Key *key, const char *filename, const char *passphrase,
Damien Millereba71ba2000-04-29 23:57:08 +1000219 const char *comment)
220{
221 switch (key->type) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100222 case KEY_RSA1:
Ben Lindstromd0fca422001-03-26 13:44:06 +0000223 return key_save_private_rsa1(key, filename, passphrase,
224 comment);
Damien Millereba71ba2000-04-29 23:57:08 +1000225 case KEY_DSA:
Damien Miller0bc1bd82000-11-13 22:57:25 +1100226 case KEY_RSA:
Ben Lindstromd0fca422001-03-26 13:44:06 +0000227 return key_save_private_pem(key, filename, passphrase,
228 comment);
Damien Millereba71ba2000-04-29 23:57:08 +1000229 default:
230 break;
231 }
Ben Lindstrom15f33862001-04-16 02:00:02 +0000232 error("key_save_private: cannot save key type %d", key->type);
Damien Millereba71ba2000-04-29 23:57:08 +1000233 return 0;
234}
235
Damien Miller5428f641999-11-25 11:54:57 +1100236/*
Ben Lindstromd0fca422001-03-26 13:44:06 +0000237 * Loads the public part of the ssh v1 key file. Returns NULL if an error was
238 * encountered (the file does not exist or is not readable), and the key
Damien Miller5428f641999-11-25 11:54:57 +1100239 * otherwise.
240 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000241
Ben Lindstrombba81212001-06-25 05:01:22 +0000242static Key *
Ben Lindstromd0fca422001-03-26 13:44:06 +0000243key_load_public_rsa1(int fd, const char *filename, char **commentp)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000244{
Damien Miller95def091999-11-25 00:26:21 +1100245 Buffer buffer;
Ben Lindstromd0fca422001-03-26 13:44:06 +0000246 Key *pub;
Ben Lindstrom44adb8f2002-12-23 02:00:23 +0000247 struct stat st;
Damien Miller95def091999-11-25 00:26:21 +1100248 char *cp;
Damien Millereccb9de2005-06-17 12:59:34 +1000249 u_int i;
Darren Tucker1f8311c2004-05-13 16:39:33 +1000250 size_t len;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000251
Ben Lindstrom44adb8f2002-12-23 02:00:23 +0000252 if (fstat(fd, &st) < 0) {
253 error("fstat for key file %.200s failed: %.100s",
254 filename, strerror(errno));
255 return NULL;
256 }
Darren Tuckerf4b43712004-08-29 16:28:39 +1000257 if (st.st_size > 1*1024*1024) {
258 error("key file %.200s too large", filename);
259 return NULL;
260 }
Darren Tucker1f8311c2004-05-13 16:39:33 +1000261 len = (size_t)st.st_size; /* truncated */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000262
Damien Miller95def091999-11-25 00:26:21 +1100263 buffer_init(&buffer);
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100264 cp = buffer_append_space(&buffer, len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000265
Damien Millereccb9de2005-06-17 12:59:34 +1000266 if (atomicio(read, fd, cp, len) != len) {
Damien Miller95def091999-11-25 00:26:21 +1100267 debug("Read from key file %.200s failed: %.100s", filename,
Damien Millereba71ba2000-04-29 23:57:08 +1000268 strerror(errno));
Damien Miller95def091999-11-25 00:26:21 +1100269 buffer_free(&buffer);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000270 return NULL;
Damien Miller95def091999-11-25 00:26:21 +1100271 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000272
Ben Lindstrom1170d712001-01-29 07:51:26 +0000273 /* Check that it is at least big enough to contain the ID string. */
274 if (len < sizeof(authfile_id_string)) {
Damien Miller058655c2001-10-10 15:03:36 +1000275 debug3("Not a RSA1 key file %.200s.", filename);
Damien Miller95def091999-11-25 00:26:21 +1100276 buffer_free(&buffer);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000277 return NULL;
Damien Miller95def091999-11-25 00:26:21 +1100278 }
Damien Miller5428f641999-11-25 11:54:57 +1100279 /*
280 * Make sure it begins with the id string. Consume the id string
281 * from the buffer.
282 */
Ben Lindstrom1170d712001-01-29 07:51:26 +0000283 for (i = 0; i < sizeof(authfile_id_string); i++)
284 if (buffer_get_char(&buffer) != authfile_id_string[i]) {
Damien Miller058655c2001-10-10 15:03:36 +1000285 debug3("Not a RSA1 key file %.200s.", filename);
Damien Miller95def091999-11-25 00:26:21 +1100286 buffer_free(&buffer);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000287 return NULL;
Damien Miller95def091999-11-25 00:26:21 +1100288 }
289 /* Skip cipher type and reserved data. */
290 (void) buffer_get_char(&buffer); /* cipher type */
291 (void) buffer_get_int(&buffer); /* reserved */
292
293 /* Read the public key from the buffer. */
Ben Lindstromc5a7f4f2002-06-25 23:19:13 +0000294 (void) buffer_get_int(&buffer);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000295 pub = key_new(KEY_RSA1);
296 buffer_get_bignum(&buffer, pub->rsa->n);
297 buffer_get_bignum(&buffer, pub->rsa->e);
298 if (commentp)
299 *commentp = buffer_get_string(&buffer, NULL);
Damien Miller95def091999-11-25 00:26:21 +1100300 /* The encrypted private part is not parsed by this function. */
301
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000302 buffer_free(&buffer);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000303 return pub;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000304}
305
Ben Lindstromd0fca422001-03-26 13:44:06 +0000306/* load public key from private-key file, works only for SSH v1 */
307Key *
308key_load_public_type(int type, const char *filename, char **commentp)
Damien Millereba71ba2000-04-29 23:57:08 +1000309{
Ben Lindstromd0fca422001-03-26 13:44:06 +0000310 Key *pub;
311 int fd;
312
313 if (type == KEY_RSA1) {
314 fd = open(filename, O_RDONLY);
315 if (fd < 0)
316 return NULL;
317 pub = key_load_public_rsa1(fd, filename, commentp);
318 close(fd);
319 return pub;
Damien Millereba71ba2000-04-29 23:57:08 +1000320 }
Ben Lindstromd0fca422001-03-26 13:44:06 +0000321 return NULL;
Damien Millereba71ba2000-04-29 23:57:08 +1000322}
323
Damien Miller5428f641999-11-25 11:54:57 +1100324/*
325 * Loads the private key from the file. Returns 0 if an error is encountered
326 * (file does not exist or is not readable, or passphrase is bad). This
327 * initializes the private key.
328 * Assumes we are called under uid of the owner of the file.
329 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000330
Ben Lindstrombba81212001-06-25 05:01:22 +0000331static Key *
Ben Lindstromd0fca422001-03-26 13:44:06 +0000332key_load_private_rsa1(int fd, const char *filename, const char *passphrase,
333 char **commentp)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000334{
Damien Millereccb9de2005-06-17 12:59:34 +1000335 u_int i;
336 int check1, check2, cipher_type;
Darren Tucker1f8311c2004-05-13 16:39:33 +1000337 size_t len;
Damien Miller95def091999-11-25 00:26:21 +1100338 Buffer buffer, decrypted;
Damien Miller708d21c2002-01-22 23:18:15 +1100339 u_char *cp;
Damien Miller874d77b2000-10-14 16:23:11 +1100340 CipherContext ciphercontext;
341 Cipher *cipher;
Ben Lindstromd0fca422001-03-26 13:44:06 +0000342 Key *prv = NULL;
Ben Lindstrom44adb8f2002-12-23 02:00:23 +0000343 struct stat st;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000344
Ben Lindstrom44adb8f2002-12-23 02:00:23 +0000345 if (fstat(fd, &st) < 0) {
346 error("fstat for key file %.200s failed: %.100s",
347 filename, strerror(errno));
348 close(fd);
349 return NULL;
350 }
Darren Tucker1f8311c2004-05-13 16:39:33 +1000351 if (st.st_size > 1*1024*1024) {
Darren Tuckerf4b43712004-08-29 16:28:39 +1000352 error("key file %.200s too large", filename);
Darren Tucker1f8311c2004-05-13 16:39:33 +1000353 close(fd);
354 return (NULL);
355 }
356 len = (size_t)st.st_size; /* truncated */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000357
Damien Miller95def091999-11-25 00:26:21 +1100358 buffer_init(&buffer);
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100359 cp = buffer_append_space(&buffer, len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000360
Damien Millereccb9de2005-06-17 12:59:34 +1000361 if (atomicio(read, fd, cp, len) != len) {
Damien Miller95def091999-11-25 00:26:21 +1100362 debug("Read from key file %.200s failed: %.100s", filename,
Damien Miller0bc1bd82000-11-13 22:57:25 +1100363 strerror(errno));
Damien Miller95def091999-11-25 00:26:21 +1100364 buffer_free(&buffer);
Damien Miller037a0dc1999-12-07 15:38:31 +1100365 close(fd);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000366 return NULL;
Damien Miller95def091999-11-25 00:26:21 +1100367 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000368
Ben Lindstrom1170d712001-01-29 07:51:26 +0000369 /* Check that it is at least big enough to contain the ID string. */
370 if (len < sizeof(authfile_id_string)) {
Damien Miller058655c2001-10-10 15:03:36 +1000371 debug3("Not a RSA1 key file %.200s.", filename);
Damien Miller95def091999-11-25 00:26:21 +1100372 buffer_free(&buffer);
Ben Lindstromb257cca2001-03-05 04:59:27 +0000373 close(fd);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000374 return NULL;
Damien Miller95def091999-11-25 00:26:21 +1100375 }
Damien Miller5428f641999-11-25 11:54:57 +1100376 /*
377 * Make sure it begins with the id string. Consume the id string
378 * from the buffer.
379 */
Ben Lindstrom1170d712001-01-29 07:51:26 +0000380 for (i = 0; i < sizeof(authfile_id_string); i++)
381 if (buffer_get_char(&buffer) != authfile_id_string[i]) {
Damien Miller058655c2001-10-10 15:03:36 +1000382 debug3("Not a RSA1 key file %.200s.", filename);
Damien Miller95def091999-11-25 00:26:21 +1100383 buffer_free(&buffer);
Ben Lindstromb257cca2001-03-05 04:59:27 +0000384 close(fd);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000385 return NULL;
Damien Miller95def091999-11-25 00:26:21 +1100386 }
Ben Lindstromb257cca2001-03-05 04:59:27 +0000387
Damien Miller95def091999-11-25 00:26:21 +1100388 /* Read cipher type. */
389 cipher_type = buffer_get_char(&buffer);
390 (void) buffer_get_int(&buffer); /* Reserved data. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000391
Damien Miller95def091999-11-25 00:26:21 +1100392 /* Read the public key from the buffer. */
Ben Lindstromc5a7f4f2002-06-25 23:19:13 +0000393 (void) buffer_get_int(&buffer);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000394 prv = key_new_private(KEY_RSA1);
395
396 buffer_get_bignum(&buffer, prv->rsa->n);
397 buffer_get_bignum(&buffer, prv->rsa->e);
398 if (commentp)
399 *commentp = buffer_get_string(&buffer, NULL);
Damien Miller95def091999-11-25 00:26:21 +1100400 else
401 xfree(buffer_get_string(&buffer, NULL));
402
403 /* Check that it is a supported cipher. */
Damien Miller874d77b2000-10-14 16:23:11 +1100404 cipher = cipher_by_number(cipher_type);
405 if (cipher == NULL) {
406 debug("Unsupported cipher %d used in key file %.200s.",
407 cipher_type, filename);
Damien Miller95def091999-11-25 00:26:21 +1100408 buffer_free(&buffer);
409 goto fail;
410 }
411 /* Initialize space for decrypted data. */
412 buffer_init(&decrypted);
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100413 cp = buffer_append_space(&decrypted, buffer_len(&buffer));
Damien Miller95def091999-11-25 00:26:21 +1100414
415 /* Rest of the buffer is encrypted. Decrypt it using the passphrase. */
Damien Miller963f6b22002-02-19 15:21:23 +1100416 cipher_set_key_string(&ciphercontext, cipher, passphrase,
417 CIPHER_DECRYPT);
418 cipher_crypt(&ciphercontext, cp,
Damien Miller708d21c2002-01-22 23:18:15 +1100419 buffer_ptr(&buffer), buffer_len(&buffer));
Damien Miller963f6b22002-02-19 15:21:23 +1100420 cipher_cleanup(&ciphercontext);
Damien Miller874d77b2000-10-14 16:23:11 +1100421 memset(&ciphercontext, 0, sizeof(ciphercontext));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000422 buffer_free(&buffer);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000423
Damien Miller95def091999-11-25 00:26:21 +1100424 check1 = buffer_get_char(&decrypted);
425 check2 = buffer_get_char(&decrypted);
426 if (check1 != buffer_get_char(&decrypted) ||
427 check2 != buffer_get_char(&decrypted)) {
428 if (strcmp(passphrase, "") != 0)
Ben Lindstromd0fca422001-03-26 13:44:06 +0000429 debug("Bad passphrase supplied for key file %.200s.",
430 filename);
Damien Miller95def091999-11-25 00:26:21 +1100431 /* Bad passphrase. */
432 buffer_free(&decrypted);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000433 goto fail;
Damien Miller95def091999-11-25 00:26:21 +1100434 }
435 /* Read the rest of the private key. */
Ben Lindstromd0fca422001-03-26 13:44:06 +0000436 buffer_get_bignum(&decrypted, prv->rsa->d);
437 buffer_get_bignum(&decrypted, prv->rsa->iqmp); /* u */
438 /* in SSL and SSH v1 p and q are exchanged */
439 buffer_get_bignum(&decrypted, prv->rsa->q); /* p */
440 buffer_get_bignum(&decrypted, prv->rsa->p); /* q */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000441
Ben Lindstromd0fca422001-03-26 13:44:06 +0000442 /* calculate p-1 and q-1 */
Damien Millerda755162002-01-22 23:09:22 +1100443 rsa_generate_additional_parameters(prv->rsa);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000444
Damien Miller95def091999-11-25 00:26:21 +1100445 buffer_free(&decrypted);
Damien Millered33d3b2003-03-15 11:36:18 +1100446
447 /* enable blinding */
448 if (RSA_blinding_on(prv->rsa, NULL) != 1) {
449 error("key_load_private_rsa1: RSA_blinding_on failed");
450 goto fail;
451 }
Ben Lindstromb257cca2001-03-05 04:59:27 +0000452 close(fd);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000453 return prv;
454
455fail:
456 if (commentp)
457 xfree(*commentp);
458 close(fd);
459 key_free(prv);
460 return NULL;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000461}
Damien Millereba71ba2000-04-29 23:57:08 +1000462
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000463Key *
Ben Lindstromd0fca422001-03-26 13:44:06 +0000464key_load_private_pem(int fd, int type, const char *passphrase,
465 char **commentp)
Damien Millereba71ba2000-04-29 23:57:08 +1000466{
Damien Millereba71ba2000-04-29 23:57:08 +1000467 FILE *fp;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100468 EVP_PKEY *pk = NULL;
Ben Lindstromd0fca422001-03-26 13:44:06 +0000469 Key *prv = NULL;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100470 char *name = "<no key>";
Damien Millereba71ba2000-04-29 23:57:08 +1000471
Damien Millereba71ba2000-04-29 23:57:08 +1000472 fp = fdopen(fd, "r");
473 if (fp == NULL) {
Ben Lindstrom15f33862001-04-16 02:00:02 +0000474 error("fdopen failed: %s", strerror(errno));
Ben Lindstromb257cca2001-03-05 04:59:27 +0000475 close(fd);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000476 return NULL;
Damien Millereba71ba2000-04-29 23:57:08 +1000477 }
Damien Miller0bc1bd82000-11-13 22:57:25 +1100478 pk = PEM_read_PrivateKey(fp, NULL, NULL, (char *)passphrase);
479 if (pk == NULL) {
Ben Lindstrom648772f2001-04-19 20:47:10 +0000480 debug("PEM_read_PrivateKey failed");
Damien Miller0bc1bd82000-11-13 22:57:25 +1100481 (void)ERR_get_error();
Ben Lindstromd0fca422001-03-26 13:44:06 +0000482 } else if (pk->type == EVP_PKEY_RSA &&
Damien Miller9f0f5c62001-12-21 14:45:46 +1100483 (type == KEY_UNSPEC||type==KEY_RSA)) {
Ben Lindstromd0fca422001-03-26 13:44:06 +0000484 prv = key_new(KEY_UNSPEC);
485 prv->rsa = EVP_PKEY_get1_RSA(pk);
486 prv->type = KEY_RSA;
487 name = "rsa w/o comment";
Damien Miller0bc1bd82000-11-13 22:57:25 +1100488#ifdef DEBUG_PK
Ben Lindstromd0fca422001-03-26 13:44:06 +0000489 RSA_print_fp(stderr, prv->rsa, 8);
Damien Millereba71ba2000-04-29 23:57:08 +1000490#endif
Damien Millered33d3b2003-03-15 11:36:18 +1100491 if (RSA_blinding_on(prv->rsa, NULL) != 1) {
492 error("key_load_private_pem: RSA_blinding_on failed");
493 key_free(prv);
494 prv = NULL;
495 }
Ben Lindstromd0fca422001-03-26 13:44:06 +0000496 } else if (pk->type == EVP_PKEY_DSA &&
Damien Miller9f0f5c62001-12-21 14:45:46 +1100497 (type == KEY_UNSPEC||type==KEY_DSA)) {
Ben Lindstromd0fca422001-03-26 13:44:06 +0000498 prv = key_new(KEY_UNSPEC);
499 prv->dsa = EVP_PKEY_get1_DSA(pk);
500 prv->type = KEY_DSA;
501 name = "dsa w/o comment";
Damien Miller0bc1bd82000-11-13 22:57:25 +1100502#ifdef DEBUG_PK
Ben Lindstromd0fca422001-03-26 13:44:06 +0000503 DSA_print_fp(stderr, prv->dsa, 8);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100504#endif
Damien Miller0bc1bd82000-11-13 22:57:25 +1100505 } else {
506 error("PEM_read_PrivateKey: mismatch or "
507 "unknown EVP_PKEY save_type %d", pk->save_type);
508 }
509 fclose(fp);
510 if (pk != NULL)
511 EVP_PKEY_free(pk);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000512 if (prv != NULL && commentp)
513 *commentp = xstrdup(name);
514 debug("read PEM private key done: type %s",
515 prv ? key_type(prv) : "<unknown>");
516 return prv;
Damien Millereba71ba2000-04-29 23:57:08 +1000517}
518
Damien Miller8275fad2006-03-15 12:06:23 +1100519int
Ben Lindstromd0fca422001-03-26 13:44:06 +0000520key_perm_ok(int fd, const char *filename)
Damien Millereba71ba2000-04-29 23:57:08 +1000521{
Damien Millereba71ba2000-04-29 23:57:08 +1000522 struct stat st;
523
Ben Lindstrom7aff2612001-09-23 13:53:22 +0000524 if (fstat(fd, &st) < 0)
525 return 0;
526 /*
527 * if a key owned by the user is accessed, then we check the
528 * permissions of the file. if the key owned by a different user,
529 * then we don't care.
530 */
Damien Millerb70b61f2000-09-16 16:25:12 +1100531#ifdef HAVE_CYGWIN
Damien Millercb5e44a2000-09-29 12:12:36 +1100532 if (check_ntsec(filename))
Damien Millerb70b61f2000-09-16 16:25:12 +1100533#endif
Ben Lindstrom7aff2612001-09-23 13:53:22 +0000534 if ((st.st_uid == getuid()) && (st.st_mode & 077) != 0) {
Damien Millereba71ba2000-04-29 23:57:08 +1000535 error("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
536 error("@ WARNING: UNPROTECTED PRIVATE KEY FILE! @");
537 error("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
Ben Lindstrom7aff2612001-09-23 13:53:22 +0000538 error("Permissions 0%3.3o for '%s' are too open.",
Damien Miller04bd8b02003-05-25 14:38:33 +1000539 (u_int)st.st_mode & 0777, filename);
Damien Millereba71ba2000-04-29 23:57:08 +1000540 error("It is recommended that your private key files are NOT accessible by others.");
Ben Lindstromd0fca422001-03-26 13:44:06 +0000541 error("This private key will be ignored.");
Damien Millereba71ba2000-04-29 23:57:08 +1000542 return 0;
543 }
Ben Lindstromd0fca422001-03-26 13:44:06 +0000544 return 1;
545}
Ben Lindstromb257cca2001-03-05 04:59:27 +0000546
Ben Lindstromd0fca422001-03-26 13:44:06 +0000547Key *
548key_load_private_type(int type, const char *filename, const char *passphrase,
Darren Tucker232b76f2006-05-06 17:41:51 +1000549 char **commentp, int *perm_ok)
Ben Lindstromd0fca422001-03-26 13:44:06 +0000550{
551 int fd;
552
553 fd = open(filename, O_RDONLY);
554 if (fd < 0)
555 return NULL;
556 if (!key_perm_ok(fd, filename)) {
Darren Tucker232b76f2006-05-06 17:41:51 +1000557 if (perm_ok != NULL)
558 *perm_ok = 0;
Ben Lindstrom15f33862001-04-16 02:00:02 +0000559 error("bad permissions: ignore key: %s", filename);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000560 close(fd);
561 return NULL;
562 }
Darren Tucker232b76f2006-05-06 17:41:51 +1000563 if (perm_ok != NULL)
564 *perm_ok = 1;
Ben Lindstromd0fca422001-03-26 13:44:06 +0000565 switch (type) {
566 case KEY_RSA1:
567 return key_load_private_rsa1(fd, filename, passphrase,
568 commentp);
569 /* closes fd */
Damien Millereba71ba2000-04-29 23:57:08 +1000570 case KEY_DSA:
Damien Miller0bc1bd82000-11-13 22:57:25 +1100571 case KEY_RSA:
572 case KEY_UNSPEC:
Ben Lindstromd0fca422001-03-26 13:44:06 +0000573 return key_load_private_pem(fd, type, passphrase, commentp);
574 /* closes fd */
Damien Millereba71ba2000-04-29 23:57:08 +1000575 default:
Ben Lindstromb257cca2001-03-05 04:59:27 +0000576 close(fd);
Damien Millereba71ba2000-04-29 23:57:08 +1000577 break;
578 }
Ben Lindstromd0fca422001-03-26 13:44:06 +0000579 return NULL;
580}
581
582Key *
583key_load_private(const char *filename, const char *passphrase,
584 char **commentp)
585{
Ben Lindstrom322915d2001-06-05 20:46:32 +0000586 Key *pub, *prv;
Ben Lindstromd0fca422001-03-26 13:44:06 +0000587 int fd;
588
589 fd = open(filename, O_RDONLY);
590 if (fd < 0)
591 return NULL;
592 if (!key_perm_ok(fd, filename)) {
Ben Lindstrom15f33862001-04-16 02:00:02 +0000593 error("bad permissions: ignore key: %s", filename);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000594 close(fd);
595 return NULL;
596 }
597 pub = key_load_public_rsa1(fd, filename, commentp);
598 lseek(fd, (off_t) 0, SEEK_SET); /* rewind */
599 if (pub == NULL) {
600 /* closes fd */
Ben Lindstrom322915d2001-06-05 20:46:32 +0000601 prv = key_load_private_pem(fd, KEY_UNSPEC, passphrase, NULL);
602 /* use the filename as a comment for PEM */
603 if (commentp && prv)
Ben Lindstrom2d0356f2001-06-05 21:13:57 +0000604 *commentp = xstrdup(filename);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000605 } else {
606 /* it's a SSH v1 key if the public key part is readable */
607 key_free(pub);
608 /* closes fd */
Ben Lindstrom322915d2001-06-05 20:46:32 +0000609 prv = key_load_private_rsa1(fd, filename, passphrase, NULL);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000610 }
Ben Lindstrom322915d2001-06-05 20:46:32 +0000611 return prv;
Damien Millereba71ba2000-04-29 23:57:08 +1000612}
Damien Millere4340be2000-09-16 13:29:08 +1100613
Ben Lindstrombba81212001-06-25 05:01:22 +0000614static int
Ben Lindstromd0fca422001-03-26 13:44:06 +0000615key_try_load_public(Key *k, const char *filename, char **commentp)
Damien Millere4340be2000-09-16 13:29:08 +1100616{
617 FILE *f;
Darren Tucker22cc7412004-12-06 22:47:41 +1100618 char line[SSH_MAX_PUBKEY_BYTES];
Damien Millere4340be2000-09-16 13:29:08 +1100619 char *cp;
Darren Tuckerf0f90982004-12-11 13:39:50 +1100620 u_long linenum = 0;
Damien Millere4340be2000-09-16 13:29:08 +1100621
622 f = fopen(filename, "r");
623 if (f != NULL) {
Darren Tucker22cc7412004-12-06 22:47:41 +1100624 while (read_keyfile_line(f, filename, line, sizeof(line),
625 &linenum) != -1) {
Damien Millere4340be2000-09-16 13:29:08 +1100626 cp = line;
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000627 switch (*cp) {
Damien Millere4340be2000-09-16 13:29:08 +1100628 case '#':
629 case '\n':
630 case '\0':
631 continue;
632 }
633 /* Skip leading whitespace. */
634 for (; *cp && (*cp == ' ' || *cp == '\t'); cp++)
635 ;
636 if (*cp) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100637 if (key_read(k, &cp) == 1) {
Damien Millere4340be2000-09-16 13:29:08 +1100638 if (commentp)
639 *commentp=xstrdup(filename);
640 fclose(f);
641 return 1;
642 }
643 }
644 }
645 fclose(f);
646 }
647 return 0;
648}
649
Ben Lindstromd0fca422001-03-26 13:44:06 +0000650/* load public key from ssh v1 private or any pubkey file */
651Key *
652key_load_public(const char *filename, char **commentp)
Damien Millere4340be2000-09-16 13:29:08 +1100653{
Ben Lindstromd0fca422001-03-26 13:44:06 +0000654 Key *pub;
655 char file[MAXPATHLEN];
Damien Millere4340be2000-09-16 13:29:08 +1100656
Damien Millerdb274722003-05-14 13:45:22 +1000657 /* try rsa1 private key */
Ben Lindstromd0fca422001-03-26 13:44:06 +0000658 pub = key_load_public_type(KEY_RSA1, filename, commentp);
659 if (pub != NULL)
660 return pub;
Damien Millerdb274722003-05-14 13:45:22 +1000661
662 /* try rsa1 public key */
663 pub = key_new(KEY_RSA1);
664 if (key_try_load_public(pub, filename, commentp) == 1)
665 return pub;
666 key_free(pub);
667
668 /* try ssh2 public key */
Ben Lindstromd0fca422001-03-26 13:44:06 +0000669 pub = key_new(KEY_UNSPEC);
670 if (key_try_load_public(pub, filename, commentp) == 1)
671 return pub;
672 if ((strlcpy(file, filename, sizeof file) < sizeof(file)) &&
673 (strlcat(file, ".pub", sizeof file) < sizeof(file)) &&
674 (key_try_load_public(pub, file, commentp) == 1))
675 return pub;
676 key_free(pub);
677 return NULL;
Damien Millere4340be2000-09-16 13:29:08 +1100678}