blob: 8d39e32e43dafd6f9f0c4ccea00f371d4a10115b [file] [log] [blame]
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001/*
Damien Miller95def091999-11-25 00:26:21 +11002 * Author: Tatu Ylonen <ylo@cs.hut.fi>
Damien Miller95def091999-11-25 00:26:21 +11003 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
4 * All rights reserved
Damien Miller95def091999-11-25 00:26:21 +11005 * This file contains functions for reading and writing identity files, and
6 * for reading the passphrase from the user.
Damien Miller4af51302000-04-16 11:18:38 +10007 *
Damien Millere4340be2000-09-16 13:29:08 +11008 * As far as I am concerned, the code I have written for this software
9 * can be used freely for any purpose. Any derived versions of this
10 * software must be clearly marked as such, and if the derived work is
11 * incompatible with the protocol description in the RFC file, it must be
12 * called by a name other than "ssh" or "Secure Shell".
13 *
14 *
15 * Copyright (c) 2000 Markus Friedl. All rights reserved.
16 *
17 * Redistribution and use in source and binary forms, with or without
18 * modification, are permitted provided that the following conditions
19 * are met:
20 * 1. Redistributions of source code must retain the above copyright
21 * notice, this list of conditions and the following disclaimer.
22 * 2. Redistributions in binary form must reproduce the above copyright
23 * notice, this list of conditions and the following disclaimer in the
24 * documentation and/or other materials provided with the distribution.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
27 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
28 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
29 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
30 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
31 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
35 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Damien Miller95def091999-11-25 00:26:21 +110036 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100037
38#include "includes.h"
Ben Lindstrom1170d712001-01-29 07:51:26 +000039RCSID("$OpenBSD: authfile.c,v 1.26 2001/01/28 22:27:05 stevesk Exp $");
Damien Millerd4a8b7e1999-10-27 13:42:43 +100040
Damien Miller0bc1bd82000-11-13 22:57:25 +110041#include <openssl/err.h>
Damien Millereba71ba2000-04-29 23:57:08 +100042#include <openssl/evp.h>
Ben Lindstrom226cfa02001-01-22 05:34:40 +000043#include <openssl/pem.h>
Damien Millereba71ba2000-04-29 23:57:08 +100044
Ben Lindstrom226cfa02001-01-22 05:34:40 +000045#include "cipher.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100046#include "xmalloc.h"
47#include "buffer.h"
48#include "bufaux.h"
Damien Millereba71ba2000-04-29 23:57:08 +100049#include "key.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000050#include "ssh.h"
51#include "log.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100052
53/* Version identification string for identity files. */
Ben Lindstrom1170d712001-01-29 07:51:26 +000054static const char authfile_id_string[] =
55 "SSH PRIVATE KEY FILE FORMAT 1.1\n";
Damien Millerd4a8b7e1999-10-27 13:42:43 +100056
Damien Miller5428f641999-11-25 11:54:57 +110057/*
58 * Saves the authentication (private) key in a file, encrypting it with
59 * passphrase. The identification of the file (lowest 64 bits of n) will
60 * precede the key to provide identification of the key without needing a
61 * passphrase.
62 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100063
64int
Damien Miller0bc1bd82000-11-13 22:57:25 +110065save_private_key_rsa1(const char *filename, const char *passphrase,
Damien Millereba71ba2000-04-29 23:57:08 +100066 RSA *key, const char *comment)
Damien Millerd4a8b7e1999-10-27 13:42:43 +100067{
Damien Miller95def091999-11-25 00:26:21 +110068 Buffer buffer, encrypted;
69 char buf[100], *cp;
Damien Miller037a0dc1999-12-07 15:38:31 +110070 int fd, i;
Damien Miller874d77b2000-10-14 16:23:11 +110071 CipherContext ciphercontext;
72 Cipher *cipher;
Damien Miller95def091999-11-25 00:26:21 +110073 u_int32_t rand;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100074
Damien Miller5428f641999-11-25 11:54:57 +110075 /*
76 * If the passphrase is empty, use SSH_CIPHER_NONE to ease converting
77 * to another cipher; otherwise use SSH_AUTHFILE_CIPHER.
78 */
Damien Miller95def091999-11-25 00:26:21 +110079 if (strcmp(passphrase, "") == 0)
Damien Miller874d77b2000-10-14 16:23:11 +110080 cipher = cipher_by_number(SSH_CIPHER_NONE);
Damien Miller95def091999-11-25 00:26:21 +110081 else
Damien Miller874d77b2000-10-14 16:23:11 +110082 cipher = cipher_by_number(SSH_AUTHFILE_CIPHER);
83 if (cipher == NULL)
84 fatal("save_private_key_rsa: bad cipher");
Damien Millerd4a8b7e1999-10-27 13:42:43 +100085
Damien Miller95def091999-11-25 00:26:21 +110086 /* This buffer is used to built the secret part of the private key. */
87 buffer_init(&buffer);
Damien Millerd4a8b7e1999-10-27 13:42:43 +100088
Damien Miller95def091999-11-25 00:26:21 +110089 /* Put checkbytes for checking passphrase validity. */
90 rand = arc4random();
91 buf[0] = rand & 0xff;
92 buf[1] = (rand >> 8) & 0xff;
93 buf[2] = buf[0];
94 buf[3] = buf[1];
95 buffer_append(&buffer, buf, 4);
Damien Millerd4a8b7e1999-10-27 13:42:43 +100096
Damien Miller5428f641999-11-25 11:54:57 +110097 /*
98 * Store the private key (n and e will not be stored because they
99 * will be stored in plain text, and storing them also in encrypted
100 * format would just give known plaintext).
101 */
Damien Miller95def091999-11-25 00:26:21 +1100102 buffer_put_bignum(&buffer, key->d);
103 buffer_put_bignum(&buffer, key->iqmp);
104 buffer_put_bignum(&buffer, key->q); /* reverse from SSL p */
105 buffer_put_bignum(&buffer, key->p); /* reverse from SSL q */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000106
Damien Miller95def091999-11-25 00:26:21 +1100107 /* Pad the part to be encrypted until its size is a multiple of 8. */
108 while (buffer_len(&buffer) % 8 != 0)
109 buffer_put_char(&buffer, 0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000110
Damien Miller95def091999-11-25 00:26:21 +1100111 /* This buffer will be used to contain the data in the file. */
112 buffer_init(&encrypted);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000113
Damien Miller95def091999-11-25 00:26:21 +1100114 /* First store keyfile id string. */
Ben Lindstrom1170d712001-01-29 07:51:26 +0000115 for (i = 0; authfile_id_string[i]; i++)
116 buffer_put_char(&encrypted, authfile_id_string[i]);
Damien Miller95def091999-11-25 00:26:21 +1100117 buffer_put_char(&encrypted, 0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000118
Damien Miller95def091999-11-25 00:26:21 +1100119 /* Store cipher type. */
Damien Miller874d77b2000-10-14 16:23:11 +1100120 buffer_put_char(&encrypted, cipher->number);
Damien Miller95def091999-11-25 00:26:21 +1100121 buffer_put_int(&encrypted, 0); /* For future extension */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000122
Damien Miller95def091999-11-25 00:26:21 +1100123 /* Store public key. This will be in plain text. */
124 buffer_put_int(&encrypted, BN_num_bits(key->n));
125 buffer_put_bignum(&encrypted, key->n);
126 buffer_put_bignum(&encrypted, key->e);
127 buffer_put_string(&encrypted, comment, strlen(comment));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000128
Damien Miller95def091999-11-25 00:26:21 +1100129 /* Allocate space for the private part of the key in the buffer. */
130 buffer_append_space(&encrypted, &cp, buffer_len(&buffer));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000131
Damien Miller874d77b2000-10-14 16:23:11 +1100132 cipher_set_key_string(&ciphercontext, cipher, passphrase);
Ben Lindstrom46c16222000-12-22 01:43:59 +0000133 cipher_encrypt(&ciphercontext, (u_char *) cp,
134 (u_char *) buffer_ptr(&buffer), buffer_len(&buffer));
Damien Miller874d77b2000-10-14 16:23:11 +1100135 memset(&ciphercontext, 0, sizeof(ciphercontext));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000136
Damien Miller95def091999-11-25 00:26:21 +1100137 /* Destroy temporary data. */
138 memset(buf, 0, sizeof(buf));
139 buffer_free(&buffer);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000140
Damien Miller037a0dc1999-12-07 15:38:31 +1100141 fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0600);
142 if (fd < 0)
Damien Miller95def091999-11-25 00:26:21 +1100143 return 0;
Damien Miller037a0dc1999-12-07 15:38:31 +1100144 if (write(fd, buffer_ptr(&encrypted), buffer_len(&encrypted)) !=
Damien Miller95def091999-11-25 00:26:21 +1100145 buffer_len(&encrypted)) {
146 debug("Write to key file %.200s failed: %.100s", filename,
147 strerror(errno));
148 buffer_free(&encrypted);
Damien Miller037a0dc1999-12-07 15:38:31 +1100149 close(fd);
Damien Miller78315eb2000-09-29 23:01:36 +1100150 unlink(filename);
Damien Miller95def091999-11-25 00:26:21 +1100151 return 0;
152 }
Damien Miller037a0dc1999-12-07 15:38:31 +1100153 close(fd);
Damien Miller95def091999-11-25 00:26:21 +1100154 buffer_free(&encrypted);
155 return 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000156}
157
Damien Miller0bc1bd82000-11-13 22:57:25 +1100158/* save SSH2 key in OpenSSL PEM format */
Damien Millereba71ba2000-04-29 23:57:08 +1000159int
Damien Miller0bc1bd82000-11-13 22:57:25 +1100160save_private_key_ssh2(const char *filename, const char *_passphrase,
161 Key *key, const char *comment)
Damien Millereba71ba2000-04-29 23:57:08 +1000162{
163 FILE *fp;
164 int fd;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100165 int success = 0;
166 int len = strlen(_passphrase);
167 char *passphrase = (len > 0) ? (char *)_passphrase : NULL;
168 EVP_CIPHER *cipher = (len > 0) ? EVP_des_ede3_cbc() : NULL;
Damien Millereba71ba2000-04-29 23:57:08 +1000169
170 if (len > 0 && len <= 4) {
171 error("passphrase too short: %d bytes", len);
172 errno = 0;
173 return 0;
174 }
175 fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0600);
176 if (fd < 0) {
177 debug("open %s failed", filename);
178 return 0;
179 }
180 fp = fdopen(fd, "w");
181 if (fp == NULL ) {
182 debug("fdopen %s failed", filename);
183 close(fd);
184 return 0;
185 }
Damien Miller0bc1bd82000-11-13 22:57:25 +1100186 switch (key->type) {
187 case KEY_DSA:
188 success = PEM_write_DSAPrivateKey(fp, key->dsa,
189 cipher, passphrase, len, NULL, NULL);
190 break;
191 case KEY_RSA:
192 success = PEM_write_RSAPrivateKey(fp, key->rsa,
193 cipher, passphrase, len, NULL, NULL);
194 break;
Damien Millereba71ba2000-04-29 23:57:08 +1000195 }
196 fclose(fp);
197 return success;
198}
199
200int
201save_private_key(const char *filename, const char *passphrase, Key *key,
202 const char *comment)
203{
204 switch (key->type) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100205 case KEY_RSA1:
206 return save_private_key_rsa1(filename, passphrase, key->rsa, comment);
Damien Millereba71ba2000-04-29 23:57:08 +1000207 break;
208 case KEY_DSA:
Damien Miller0bc1bd82000-11-13 22:57:25 +1100209 case KEY_RSA:
210 return save_private_key_ssh2(filename, passphrase, key, comment);
Damien Millereba71ba2000-04-29 23:57:08 +1000211 break;
212 default:
213 break;
214 }
215 return 0;
216}
217
Damien Miller5428f641999-11-25 11:54:57 +1100218/*
219 * Loads the public part of the key file. Returns 0 if an error was
220 * encountered (the file does not exist or is not readable), and non-zero
221 * otherwise.
222 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000223
224int
Damien Millereba71ba2000-04-29 23:57:08 +1000225load_public_key_rsa(const char *filename, RSA * pub, char **comment_return)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000226{
Damien Miller037a0dc1999-12-07 15:38:31 +1100227 int fd, i;
Damien Miller95def091999-11-25 00:26:21 +1100228 off_t len;
229 Buffer buffer;
230 char *cp;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000231
Damien Miller037a0dc1999-12-07 15:38:31 +1100232 fd = open(filename, O_RDONLY);
233 if (fd < 0)
Damien Miller95def091999-11-25 00:26:21 +1100234 return 0;
Damien Miller037a0dc1999-12-07 15:38:31 +1100235 len = lseek(fd, (off_t) 0, SEEK_END);
236 lseek(fd, (off_t) 0, SEEK_SET);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000237
Damien Miller95def091999-11-25 00:26:21 +1100238 buffer_init(&buffer);
239 buffer_append_space(&buffer, &cp, len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000240
Damien Miller037a0dc1999-12-07 15:38:31 +1100241 if (read(fd, cp, (size_t) len) != (size_t) len) {
Damien Miller95def091999-11-25 00:26:21 +1100242 debug("Read from key file %.200s failed: %.100s", filename,
Damien Millereba71ba2000-04-29 23:57:08 +1000243 strerror(errno));
Damien Miller95def091999-11-25 00:26:21 +1100244 buffer_free(&buffer);
Damien Miller037a0dc1999-12-07 15:38:31 +1100245 close(fd);
Damien Miller95def091999-11-25 00:26:21 +1100246 return 0;
247 }
Damien Miller037a0dc1999-12-07 15:38:31 +1100248 close(fd);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000249
Ben Lindstrom1170d712001-01-29 07:51:26 +0000250 /* Check that it is at least big enough to contain the ID string. */
251 if (len < sizeof(authfile_id_string)) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100252 debug3("Bad RSA1 key file %.200s.", filename);
Damien Miller95def091999-11-25 00:26:21 +1100253 buffer_free(&buffer);
254 return 0;
255 }
Damien Miller5428f641999-11-25 11:54:57 +1100256 /*
257 * Make sure it begins with the id string. Consume the id string
258 * from the buffer.
259 */
Ben Lindstrom1170d712001-01-29 07:51:26 +0000260 for (i = 0; i < sizeof(authfile_id_string); i++)
261 if (buffer_get_char(&buffer) != authfile_id_string[i]) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100262 debug3("Bad RSA1 key file %.200s.", filename);
Damien Miller95def091999-11-25 00:26:21 +1100263 buffer_free(&buffer);
264 return 0;
265 }
266 /* Skip cipher type and reserved data. */
267 (void) buffer_get_char(&buffer); /* cipher type */
268 (void) buffer_get_int(&buffer); /* reserved */
269
270 /* Read the public key from the buffer. */
271 buffer_get_int(&buffer);
Damien Millereba71ba2000-04-29 23:57:08 +1000272 /* XXX alloc */
273 if (pub->n == NULL)
274 pub->n = BN_new();
Damien Miller95def091999-11-25 00:26:21 +1100275 buffer_get_bignum(&buffer, pub->n);
Damien Millereba71ba2000-04-29 23:57:08 +1000276 /* XXX alloc */
277 if (pub->e == NULL)
278 pub->e = BN_new();
Damien Miller95def091999-11-25 00:26:21 +1100279 buffer_get_bignum(&buffer, pub->e);
280 if (comment_return)
281 *comment_return = buffer_get_string(&buffer, NULL);
282 /* The encrypted private part is not parsed by this function. */
283
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000284 buffer_free(&buffer);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000285
Damien Miller95def091999-11-25 00:26:21 +1100286 return 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000287}
288
Damien Millere4340be2000-09-16 13:29:08 +1100289/* load public key from private-key file */
Damien Millereba71ba2000-04-29 23:57:08 +1000290int
291load_public_key(const char *filename, Key * key, char **comment_return)
292{
293 switch (key->type) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100294 case KEY_RSA1:
Damien Millereba71ba2000-04-29 23:57:08 +1000295 return load_public_key_rsa(filename, key->rsa, comment_return);
296 break;
297 case KEY_DSA:
Damien Miller0bc1bd82000-11-13 22:57:25 +1100298 case KEY_RSA:
Damien Millereba71ba2000-04-29 23:57:08 +1000299 default:
300 break;
301 }
302 return 0;
303}
304
Damien Miller5428f641999-11-25 11:54:57 +1100305/*
306 * Loads the private key from the file. Returns 0 if an error is encountered
307 * (file does not exist or is not readable, or passphrase is bad). This
308 * initializes the private key.
309 * Assumes we are called under uid of the owner of the file.
310 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000311
312int
Damien Miller0bc1bd82000-11-13 22:57:25 +1100313load_private_key_rsa1(int fd, const char *filename,
Damien Millereba71ba2000-04-29 23:57:08 +1000314 const char *passphrase, RSA * prv, char **comment_return)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000315{
Damien Millereba71ba2000-04-29 23:57:08 +1000316 int i, check1, check2, cipher_type;
Damien Miller95def091999-11-25 00:26:21 +1100317 off_t len;
318 Buffer buffer, decrypted;
319 char *cp;
Damien Miller874d77b2000-10-14 16:23:11 +1100320 CipherContext ciphercontext;
321 Cipher *cipher;
Damien Miller95def091999-11-25 00:26:21 +1100322 BN_CTX *ctx;
323 BIGNUM *aux;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000324
Damien Miller037a0dc1999-12-07 15:38:31 +1100325 len = lseek(fd, (off_t) 0, SEEK_END);
326 lseek(fd, (off_t) 0, SEEK_SET);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000327
Damien Miller95def091999-11-25 00:26:21 +1100328 buffer_init(&buffer);
329 buffer_append_space(&buffer, &cp, len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000330
Damien Miller037a0dc1999-12-07 15:38:31 +1100331 if (read(fd, cp, (size_t) len) != (size_t) len) {
Damien Miller95def091999-11-25 00:26:21 +1100332 debug("Read from key file %.200s failed: %.100s", filename,
Damien Miller0bc1bd82000-11-13 22:57:25 +1100333 strerror(errno));
Damien Miller95def091999-11-25 00:26:21 +1100334 buffer_free(&buffer);
Damien Miller037a0dc1999-12-07 15:38:31 +1100335 close(fd);
Damien Miller95def091999-11-25 00:26:21 +1100336 return 0;
337 }
Damien Miller037a0dc1999-12-07 15:38:31 +1100338 close(fd);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000339
Ben Lindstrom1170d712001-01-29 07:51:26 +0000340 /* Check that it is at least big enough to contain the ID string. */
341 if (len < sizeof(authfile_id_string)) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100342 debug3("Bad RSA1 key file %.200s.", filename);
Damien Miller95def091999-11-25 00:26:21 +1100343 buffer_free(&buffer);
344 return 0;
345 }
Damien Miller5428f641999-11-25 11:54:57 +1100346 /*
347 * Make sure it begins with the id string. Consume the id string
348 * from the buffer.
349 */
Ben Lindstrom1170d712001-01-29 07:51:26 +0000350 for (i = 0; i < sizeof(authfile_id_string); i++)
351 if (buffer_get_char(&buffer) != authfile_id_string[i]) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100352 debug3("Bad RSA1 key file %.200s.", filename);
Damien Miller95def091999-11-25 00:26:21 +1100353 buffer_free(&buffer);
354 return 0;
355 }
356 /* Read cipher type. */
357 cipher_type = buffer_get_char(&buffer);
358 (void) buffer_get_int(&buffer); /* Reserved data. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000359
Damien Miller95def091999-11-25 00:26:21 +1100360 /* Read the public key from the buffer. */
361 buffer_get_int(&buffer);
362 prv->n = BN_new();
363 buffer_get_bignum(&buffer, prv->n);
364 prv->e = BN_new();
365 buffer_get_bignum(&buffer, prv->e);
366 if (comment_return)
367 *comment_return = buffer_get_string(&buffer, NULL);
368 else
369 xfree(buffer_get_string(&buffer, NULL));
370
371 /* Check that it is a supported cipher. */
Damien Miller874d77b2000-10-14 16:23:11 +1100372 cipher = cipher_by_number(cipher_type);
373 if (cipher == NULL) {
374 debug("Unsupported cipher %d used in key file %.200s.",
375 cipher_type, filename);
Damien Miller95def091999-11-25 00:26:21 +1100376 buffer_free(&buffer);
377 goto fail;
378 }
379 /* Initialize space for decrypted data. */
380 buffer_init(&decrypted);
381 buffer_append_space(&decrypted, &cp, buffer_len(&buffer));
382
383 /* Rest of the buffer is encrypted. Decrypt it using the passphrase. */
Damien Miller874d77b2000-10-14 16:23:11 +1100384 cipher_set_key_string(&ciphercontext, cipher, passphrase);
Ben Lindstrom46c16222000-12-22 01:43:59 +0000385 cipher_decrypt(&ciphercontext, (u_char *) cp,
386 (u_char *) buffer_ptr(&buffer), buffer_len(&buffer));
Damien Miller874d77b2000-10-14 16:23:11 +1100387 memset(&ciphercontext, 0, sizeof(ciphercontext));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000388 buffer_free(&buffer);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000389
Damien Miller95def091999-11-25 00:26:21 +1100390 check1 = buffer_get_char(&decrypted);
391 check2 = buffer_get_char(&decrypted);
392 if (check1 != buffer_get_char(&decrypted) ||
393 check2 != buffer_get_char(&decrypted)) {
394 if (strcmp(passphrase, "") != 0)
395 debug("Bad passphrase supplied for key file %.200s.", filename);
396 /* Bad passphrase. */
397 buffer_free(&decrypted);
398fail:
399 BN_clear_free(prv->n);
Damien Millereba71ba2000-04-29 23:57:08 +1000400 prv->n = NULL;
Damien Miller95def091999-11-25 00:26:21 +1100401 BN_clear_free(prv->e);
Damien Millereba71ba2000-04-29 23:57:08 +1000402 prv->e = NULL;
Damien Miller95def091999-11-25 00:26:21 +1100403 if (comment_return)
404 xfree(*comment_return);
405 return 0;
406 }
407 /* Read the rest of the private key. */
408 prv->d = BN_new();
409 buffer_get_bignum(&decrypted, prv->d);
410 prv->iqmp = BN_new();
411 buffer_get_bignum(&decrypted, prv->iqmp); /* u */
412 /* in SSL and SSH p and q are exchanged */
413 prv->q = BN_new();
414 buffer_get_bignum(&decrypted, prv->q); /* p */
415 prv->p = BN_new();
416 buffer_get_bignum(&decrypted, prv->p); /* q */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000417
Damien Miller95def091999-11-25 00:26:21 +1100418 ctx = BN_CTX_new();
419 aux = BN_new();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000420
Damien Miller95def091999-11-25 00:26:21 +1100421 BN_sub(aux, prv->q, BN_value_one());
422 prv->dmq1 = BN_new();
423 BN_mod(prv->dmq1, prv->d, aux, ctx);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000424
Damien Miller95def091999-11-25 00:26:21 +1100425 BN_sub(aux, prv->p, BN_value_one());
426 prv->dmp1 = BN_new();
427 BN_mod(prv->dmp1, prv->d, aux, ctx);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000428
Damien Miller95def091999-11-25 00:26:21 +1100429 BN_clear_free(aux);
430 BN_CTX_free(ctx);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000431
Damien Miller95def091999-11-25 00:26:21 +1100432 buffer_free(&decrypted);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000433
Damien Miller95def091999-11-25 00:26:21 +1100434 return 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000435}
Damien Millereba71ba2000-04-29 23:57:08 +1000436
437int
Damien Miller0bc1bd82000-11-13 22:57:25 +1100438load_private_key_ssh2(int fd, const char *passphrase, Key *k, char **comment_return)
Damien Millereba71ba2000-04-29 23:57:08 +1000439{
Damien Millereba71ba2000-04-29 23:57:08 +1000440 FILE *fp;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100441 int success = 0;
442 EVP_PKEY *pk = NULL;
443 char *name = "<no key>";
Damien Millereba71ba2000-04-29 23:57:08 +1000444
Damien Millereba71ba2000-04-29 23:57:08 +1000445 fp = fdopen(fd, "r");
446 if (fp == NULL) {
447 error("fdopen failed");
448 return 0;
449 }
Damien Miller0bc1bd82000-11-13 22:57:25 +1100450 pk = PEM_read_PrivateKey(fp, NULL, NULL, (char *)passphrase);
451 if (pk == NULL) {
452 debug("PEM_read_PrivateKey failed");
453 (void)ERR_get_error();
454 } else if (pk->type == EVP_PKEY_RSA) {
455 /* replace k->rsa with loaded key */
456 if (k->type == KEY_RSA || k->type == KEY_UNSPEC) {
457 if (k->rsa != NULL)
458 RSA_free(k->rsa);
459 k->rsa = EVP_PKEY_get1_RSA(pk);
460 k->type = KEY_RSA;
461 name = "rsa w/o comment";
462 success = 1;
463#ifdef DEBUG_PK
464 RSA_print_fp(stderr, k->rsa, 8);
Damien Millereba71ba2000-04-29 23:57:08 +1000465#endif
Damien Miller0bc1bd82000-11-13 22:57:25 +1100466 }
467 } else if (pk->type == EVP_PKEY_DSA) {
468 /* replace k->dsa with loaded key */
469 if (k->type == KEY_DSA || k->type == KEY_UNSPEC) {
470 if (k->dsa != NULL)
471 DSA_free(k->dsa);
472 k->dsa = EVP_PKEY_get1_DSA(pk);
473 k->type = KEY_DSA;
474 name = "dsa w/o comment";
475#ifdef DEBUG_PK
476 DSA_print_fp(stderr, k->dsa, 8);
477#endif
478 success = 1;
479 }
480 } else {
481 error("PEM_read_PrivateKey: mismatch or "
482 "unknown EVP_PKEY save_type %d", pk->save_type);
483 }
484 fclose(fp);
485 if (pk != NULL)
486 EVP_PKEY_free(pk);
487 if (success && comment_return)
488 *comment_return = xstrdup(name);
489 debug("read SSH2 private key done: name %s success %d", name, success);
490 return success;
Damien Millereba71ba2000-04-29 23:57:08 +1000491}
492
493int
494load_private_key(const char *filename, const char *passphrase, Key *key,
495 char **comment_return)
496{
497 int fd;
498 int ret = 0;
499 struct stat st;
500
501 fd = open(filename, O_RDONLY);
502 if (fd < 0)
503 return 0;
504
Damien Millercb5e44a2000-09-29 12:12:36 +1100505 /* check owner and modes */
Damien Millerb70b61f2000-09-16 16:25:12 +1100506#ifdef HAVE_CYGWIN
Damien Millercb5e44a2000-09-29 12:12:36 +1100507 if (check_ntsec(filename))
Damien Millerb70b61f2000-09-16 16:25:12 +1100508#endif
Damien Millereba71ba2000-04-29 23:57:08 +1000509 if (fstat(fd, &st) < 0 ||
Ben Lindstrom46c16222000-12-22 01:43:59 +0000510 (st.st_uid != 0 && getuid() != 0 && st.st_uid != getuid()) ||
Damien Millereba71ba2000-04-29 23:57:08 +1000511 (st.st_mode & 077) != 0) {
512 close(fd);
513 error("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
514 error("@ WARNING: UNPROTECTED PRIVATE KEY FILE! @");
515 error("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
516 error("Bad ownership or mode(0%3.3o) for '%s'.",
517 st.st_mode & 0777, filename);
518 error("It is recommended that your private key files are NOT accessible by others.");
519 return 0;
520 }
521 switch (key->type) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100522 case KEY_RSA1:
Damien Millereba71ba2000-04-29 23:57:08 +1000523 if (key->rsa->e != NULL) {
524 BN_clear_free(key->rsa->e);
525 key->rsa->e = NULL;
526 }
527 if (key->rsa->n != NULL) {
528 BN_clear_free(key->rsa->n);
529 key->rsa->n = NULL;
530 }
Damien Miller0bc1bd82000-11-13 22:57:25 +1100531 ret = load_private_key_rsa1(fd, filename, passphrase,
Damien Millereba71ba2000-04-29 23:57:08 +1000532 key->rsa, comment_return);
533 break;
534 case KEY_DSA:
Damien Miller0bc1bd82000-11-13 22:57:25 +1100535 case KEY_RSA:
536 case KEY_UNSPEC:
537 ret = load_private_key_ssh2(fd, passphrase, key, comment_return);
Damien Millereba71ba2000-04-29 23:57:08 +1000538 default:
539 break;
540 }
541 close(fd);
542 return ret;
543}
Damien Millere4340be2000-09-16 13:29:08 +1100544
545int
546do_load_public_key(const char *filename, Key *k, char **commentp)
547{
548 FILE *f;
Damien Millere4340be2000-09-16 13:29:08 +1100549 char line[1024];
550 char *cp;
551
552 f = fopen(filename, "r");
553 if (f != NULL) {
554 while (fgets(line, sizeof(line), f)) {
555 line[sizeof(line)-1] = '\0';
556 cp = line;
557 switch(*cp){
558 case '#':
559 case '\n':
560 case '\0':
561 continue;
562 }
563 /* Skip leading whitespace. */
564 for (; *cp && (*cp == ' ' || *cp == '\t'); cp++)
565 ;
566 if (*cp) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100567 if (key_read(k, &cp) == 1) {
Damien Millere4340be2000-09-16 13:29:08 +1100568 if (commentp)
569 *commentp=xstrdup(filename);
570 fclose(f);
571 return 1;
572 }
573 }
574 }
575 fclose(f);
576 }
577 return 0;
578}
579
580/* load public key from pubkey file */
581int
582try_load_public_key(const char *filename, Key *k, char **commentp)
583{
584 char pub[MAXPATHLEN];
585
586 if (do_load_public_key(filename, k, commentp) == 1)
587 return 1;
588 if (strlcpy(pub, filename, sizeof pub) >= MAXPATHLEN)
589 return 0;
590 if (strlcat(pub, ".pub", sizeof pub) >= MAXPATHLEN)
591 return 0;
592 if (do_load_public_key(pub, k, commentp) == 1)
593 return 1;
594 return 0;
595}