blob: cf2957dde1494e6cda769f32d1918b09646a4b61 [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 Lindstrom7aff2612001-09-23 13:53:22 +000039RCSID("$OpenBSD: authfile.c,v 1.38 2001/09/23 11:09:13 markus 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"
Ben Lindstrom31ca54a2001-02-09 02:11:24 +000052#include "authfile.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100053
Ben Lindstromd0fca422001-03-26 13:44:06 +000054/* Version identification string for SSH v1 identity files. */
Ben Lindstrom1170d712001-01-29 07:51:26 +000055static const char authfile_id_string[] =
56 "SSH PRIVATE KEY FILE FORMAT 1.1\n";
Damien Millerd4a8b7e1999-10-27 13:42:43 +100057
Damien Miller5428f641999-11-25 11:54:57 +110058/*
59 * Saves the authentication (private) key in a file, encrypting it with
60 * passphrase. The identification of the file (lowest 64 bits of n) will
61 * precede the key to provide identification of the key without needing a
62 * passphrase.
63 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100064
Ben Lindstrombba81212001-06-25 05:01:22 +000065static int
Ben Lindstromd0fca422001-03-26 13:44:06 +000066key_save_private_rsa1(Key *key, const char *filename, const char *passphrase,
67 const char *comment)
Damien Millerd4a8b7e1999-10-27 13:42:43 +100068{
Damien Miller95def091999-11-25 00:26:21 +110069 Buffer buffer, encrypted;
70 char buf[100], *cp;
Damien Miller037a0dc1999-12-07 15:38:31 +110071 int fd, i;
Damien Miller874d77b2000-10-14 16:23:11 +110072 CipherContext ciphercontext;
73 Cipher *cipher;
Damien Miller95def091999-11-25 00:26:21 +110074 u_int32_t rand;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100075
Damien Miller5428f641999-11-25 11:54:57 +110076 /*
77 * If the passphrase is empty, use SSH_CIPHER_NONE to ease converting
78 * to another cipher; otherwise use SSH_AUTHFILE_CIPHER.
79 */
Damien Miller95def091999-11-25 00:26:21 +110080 if (strcmp(passphrase, "") == 0)
Damien Miller874d77b2000-10-14 16:23:11 +110081 cipher = cipher_by_number(SSH_CIPHER_NONE);
Damien Miller95def091999-11-25 00:26:21 +110082 else
Damien Miller874d77b2000-10-14 16:23:11 +110083 cipher = cipher_by_number(SSH_AUTHFILE_CIPHER);
84 if (cipher == NULL)
85 fatal("save_private_key_rsa: bad cipher");
Damien Millerd4a8b7e1999-10-27 13:42:43 +100086
Damien Miller95def091999-11-25 00:26:21 +110087 /* This buffer is used to built the secret part of the private key. */
88 buffer_init(&buffer);
Damien Millerd4a8b7e1999-10-27 13:42:43 +100089
Damien Miller95def091999-11-25 00:26:21 +110090 /* Put checkbytes for checking passphrase validity. */
91 rand = arc4random();
92 buf[0] = rand & 0xff;
93 buf[1] = (rand >> 8) & 0xff;
94 buf[2] = buf[0];
95 buf[3] = buf[1];
96 buffer_append(&buffer, buf, 4);
Damien Millerd4a8b7e1999-10-27 13:42:43 +100097
Damien Miller5428f641999-11-25 11:54:57 +110098 /*
99 * Store the private key (n and e will not be stored because they
100 * will be stored in plain text, and storing them also in encrypted
101 * format would just give known plaintext).
102 */
Ben Lindstromd0fca422001-03-26 13:44:06 +0000103 buffer_put_bignum(&buffer, key->rsa->d);
104 buffer_put_bignum(&buffer, key->rsa->iqmp);
105 buffer_put_bignum(&buffer, key->rsa->q); /* reverse from SSL p */
106 buffer_put_bignum(&buffer, key->rsa->p); /* reverse from SSL q */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000107
Damien Miller95def091999-11-25 00:26:21 +1100108 /* Pad the part to be encrypted until its size is a multiple of 8. */
109 while (buffer_len(&buffer) % 8 != 0)
110 buffer_put_char(&buffer, 0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000111
Damien Miller95def091999-11-25 00:26:21 +1100112 /* This buffer will be used to contain the data in the file. */
113 buffer_init(&encrypted);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000114
Damien Miller95def091999-11-25 00:26:21 +1100115 /* First store keyfile id string. */
Ben Lindstrom1170d712001-01-29 07:51:26 +0000116 for (i = 0; authfile_id_string[i]; i++)
117 buffer_put_char(&encrypted, authfile_id_string[i]);
Damien Miller95def091999-11-25 00:26:21 +1100118 buffer_put_char(&encrypted, 0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000119
Damien Miller95def091999-11-25 00:26:21 +1100120 /* Store cipher type. */
Damien Miller874d77b2000-10-14 16:23:11 +1100121 buffer_put_char(&encrypted, cipher->number);
Damien Miller95def091999-11-25 00:26:21 +1100122 buffer_put_int(&encrypted, 0); /* For future extension */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000123
Damien Miller95def091999-11-25 00:26:21 +1100124 /* Store public key. This will be in plain text. */
Ben Lindstromd0fca422001-03-26 13:44:06 +0000125 buffer_put_int(&encrypted, BN_num_bits(key->rsa->n));
126 buffer_put_bignum(&encrypted, key->rsa->n);
127 buffer_put_bignum(&encrypted, key->rsa->e);
Ben Lindstrom664408d2001-06-09 01:42:01 +0000128 buffer_put_cstring(&encrypted, comment);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000129
Damien Miller95def091999-11-25 00:26:21 +1100130 /* Allocate space for the private part of the key in the buffer. */
131 buffer_append_space(&encrypted, &cp, buffer_len(&buffer));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000132
Damien Miller874d77b2000-10-14 16:23:11 +1100133 cipher_set_key_string(&ciphercontext, cipher, passphrase);
Ben Lindstrom46c16222000-12-22 01:43:59 +0000134 cipher_encrypt(&ciphercontext, (u_char *) cp,
135 (u_char *) buffer_ptr(&buffer), buffer_len(&buffer));
Damien Miller874d77b2000-10-14 16:23:11 +1100136 memset(&ciphercontext, 0, sizeof(ciphercontext));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000137
Damien Miller95def091999-11-25 00:26:21 +1100138 /* Destroy temporary data. */
139 memset(buf, 0, sizeof(buf));
140 buffer_free(&buffer);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000141
Damien Miller037a0dc1999-12-07 15:38:31 +1100142 fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0600);
Ben Lindstrom15f33862001-04-16 02:00:02 +0000143 if (fd < 0) {
144 error("open %s failed: %s.", filename, strerror(errno));
Damien Miller95def091999-11-25 00:26:21 +1100145 return 0;
Ben Lindstrom15f33862001-04-16 02:00:02 +0000146 }
Damien Miller037a0dc1999-12-07 15:38:31 +1100147 if (write(fd, buffer_ptr(&encrypted), buffer_len(&encrypted)) !=
Damien Miller95def091999-11-25 00:26:21 +1100148 buffer_len(&encrypted)) {
Ben Lindstrom15f33862001-04-16 02:00:02 +0000149 error("write to key file %s failed: %s", filename,
Damien Miller95def091999-11-25 00:26:21 +1100150 strerror(errno));
151 buffer_free(&encrypted);
Damien Miller037a0dc1999-12-07 15:38:31 +1100152 close(fd);
Damien Miller78315eb2000-09-29 23:01:36 +1100153 unlink(filename);
Damien Miller95def091999-11-25 00:26:21 +1100154 return 0;
155 }
Damien Miller037a0dc1999-12-07 15:38:31 +1100156 close(fd);
Damien Miller95def091999-11-25 00:26:21 +1100157 buffer_free(&encrypted);
158 return 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000159}
160
Ben Lindstromd0fca422001-03-26 13:44:06 +0000161/* save SSH v2 key in OpenSSL PEM format */
Ben Lindstrombba81212001-06-25 05:01:22 +0000162static int
Ben Lindstromd0fca422001-03-26 13:44:06 +0000163key_save_private_pem(Key *key, const char *filename, const char *_passphrase,
164 const char *comment)
Damien Millereba71ba2000-04-29 23:57:08 +1000165{
166 FILE *fp;
167 int fd;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100168 int success = 0;
169 int len = strlen(_passphrase);
170 char *passphrase = (len > 0) ? (char *)_passphrase : NULL;
171 EVP_CIPHER *cipher = (len > 0) ? EVP_des_ede3_cbc() : NULL;
Damien Millereba71ba2000-04-29 23:57:08 +1000172
173 if (len > 0 && len <= 4) {
Ben Lindstrom15f33862001-04-16 02:00:02 +0000174 error("passphrase too short: have %d bytes, need > 4", len);
Damien Millereba71ba2000-04-29 23:57:08 +1000175 return 0;
176 }
177 fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0600);
178 if (fd < 0) {
Ben Lindstrom15f33862001-04-16 02:00:02 +0000179 error("open %s failed: %s.", filename, strerror(errno));
Damien Millereba71ba2000-04-29 23:57:08 +1000180 return 0;
181 }
182 fp = fdopen(fd, "w");
183 if (fp == NULL ) {
Ben Lindstrom15f33862001-04-16 02:00:02 +0000184 error("fdopen %s failed: %s.", filename, strerror(errno));
Damien Millereba71ba2000-04-29 23:57:08 +1000185 close(fd);
186 return 0;
187 }
Damien Miller0bc1bd82000-11-13 22:57:25 +1100188 switch (key->type) {
Ben Lindstromc1116602001-03-29 00:28:37 +0000189 case KEY_DSA:
190 success = PEM_write_DSAPrivateKey(fp, key->dsa,
191 cipher, passphrase, len, NULL, NULL);
192 break;
193 case KEY_RSA:
194 success = PEM_write_RSAPrivateKey(fp, key->rsa,
195 cipher, passphrase, len, NULL, NULL);
196 break;
Damien Millereba71ba2000-04-29 23:57:08 +1000197 }
198 fclose(fp);
199 return success;
200}
201
202int
Ben Lindstromd0fca422001-03-26 13:44:06 +0000203key_save_private(Key *key, const char *filename, const char *passphrase,
Damien Millereba71ba2000-04-29 23:57:08 +1000204 const char *comment)
205{
206 switch (key->type) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100207 case KEY_RSA1:
Ben Lindstromd0fca422001-03-26 13:44:06 +0000208 return key_save_private_rsa1(key, filename, passphrase,
209 comment);
Damien Millereba71ba2000-04-29 23:57:08 +1000210 break;
211 case KEY_DSA:
Damien Miller0bc1bd82000-11-13 22:57:25 +1100212 case KEY_RSA:
Ben Lindstromd0fca422001-03-26 13:44:06 +0000213 return key_save_private_pem(key, filename, passphrase,
214 comment);
Damien Millereba71ba2000-04-29 23:57:08 +1000215 break;
216 default:
217 break;
218 }
Ben Lindstrom15f33862001-04-16 02:00:02 +0000219 error("key_save_private: cannot save key type %d", key->type);
Damien Millereba71ba2000-04-29 23:57:08 +1000220 return 0;
221}
222
Damien Miller5428f641999-11-25 11:54:57 +1100223/*
Ben Lindstromd0fca422001-03-26 13:44:06 +0000224 * Loads the public part of the ssh v1 key file. Returns NULL if an error was
225 * encountered (the file does not exist or is not readable), and the key
Damien Miller5428f641999-11-25 11:54:57 +1100226 * otherwise.
227 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000228
Ben Lindstrombba81212001-06-25 05:01:22 +0000229static Key *
Ben Lindstromd0fca422001-03-26 13:44:06 +0000230key_load_public_rsa1(int fd, const char *filename, char **commentp)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000231{
Damien Miller95def091999-11-25 00:26:21 +1100232 Buffer buffer;
Ben Lindstromd0fca422001-03-26 13:44:06 +0000233 Key *pub;
Damien Miller95def091999-11-25 00:26:21 +1100234 char *cp;
Ben Lindstromd0fca422001-03-26 13:44:06 +0000235 int i;
236 off_t len;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000237
Damien Miller037a0dc1999-12-07 15:38:31 +1100238 len = lseek(fd, (off_t) 0, SEEK_END);
239 lseek(fd, (off_t) 0, SEEK_SET);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000240
Damien Miller95def091999-11-25 00:26:21 +1100241 buffer_init(&buffer);
242 buffer_append_space(&buffer, &cp, len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000243
Damien Miller037a0dc1999-12-07 15:38:31 +1100244 if (read(fd, cp, (size_t) len) != (size_t) len) {
Damien Miller95def091999-11-25 00:26:21 +1100245 debug("Read from key file %.200s failed: %.100s", filename,
Damien Millereba71ba2000-04-29 23:57:08 +1000246 strerror(errno));
Damien Miller95def091999-11-25 00:26:21 +1100247 buffer_free(&buffer);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000248 return NULL;
Damien Miller95def091999-11-25 00:26:21 +1100249 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000250
Ben Lindstrom1170d712001-01-29 07:51:26 +0000251 /* Check that it is at least big enough to contain the ID string. */
252 if (len < sizeof(authfile_id_string)) {
Ben Lindstrom15f33862001-04-16 02:00:02 +0000253 debug3("No RSA1 key file %.200s.", filename);
Damien Miller95def091999-11-25 00:26:21 +1100254 buffer_free(&buffer);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000255 return NULL;
Damien Miller95def091999-11-25 00:26:21 +1100256 }
Damien Miller5428f641999-11-25 11:54:57 +1100257 /*
258 * Make sure it begins with the id string. Consume the id string
259 * from the buffer.
260 */
Ben Lindstrom1170d712001-01-29 07:51:26 +0000261 for (i = 0; i < sizeof(authfile_id_string); i++)
262 if (buffer_get_char(&buffer) != authfile_id_string[i]) {
Ben Lindstrom15f33862001-04-16 02:00:02 +0000263 debug3("No RSA1 key file %.200s.", filename);
Damien Miller95def091999-11-25 00:26:21 +1100264 buffer_free(&buffer);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000265 return NULL;
Damien Miller95def091999-11-25 00:26:21 +1100266 }
267 /* Skip cipher type and reserved data. */
268 (void) buffer_get_char(&buffer); /* cipher type */
269 (void) buffer_get_int(&buffer); /* reserved */
270
271 /* Read the public key from the buffer. */
272 buffer_get_int(&buffer);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000273 pub = key_new(KEY_RSA1);
274 buffer_get_bignum(&buffer, pub->rsa->n);
275 buffer_get_bignum(&buffer, pub->rsa->e);
276 if (commentp)
277 *commentp = buffer_get_string(&buffer, NULL);
Damien Miller95def091999-11-25 00:26:21 +1100278 /* The encrypted private part is not parsed by this function. */
279
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000280 buffer_free(&buffer);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000281 return pub;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000282}
283
Ben Lindstromd0fca422001-03-26 13:44:06 +0000284/* load public key from private-key file, works only for SSH v1 */
285Key *
286key_load_public_type(int type, const char *filename, char **commentp)
Damien Millereba71ba2000-04-29 23:57:08 +1000287{
Ben Lindstromd0fca422001-03-26 13:44:06 +0000288 Key *pub;
289 int fd;
290
291 if (type == KEY_RSA1) {
292 fd = open(filename, O_RDONLY);
293 if (fd < 0)
294 return NULL;
295 pub = key_load_public_rsa1(fd, filename, commentp);
296 close(fd);
297 return pub;
Damien Millereba71ba2000-04-29 23:57:08 +1000298 }
Ben Lindstromd0fca422001-03-26 13:44:06 +0000299 return NULL;
Damien Millereba71ba2000-04-29 23:57:08 +1000300}
301
Damien Miller5428f641999-11-25 11:54:57 +1100302/*
303 * Loads the private key from the file. Returns 0 if an error is encountered
304 * (file does not exist or is not readable, or passphrase is bad). This
305 * initializes the private key.
306 * Assumes we are called under uid of the owner of the file.
307 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000308
Ben Lindstrombba81212001-06-25 05:01:22 +0000309static Key *
Ben Lindstromd0fca422001-03-26 13:44:06 +0000310key_load_private_rsa1(int fd, const char *filename, const char *passphrase,
311 char **commentp)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000312{
Damien Millereba71ba2000-04-29 23:57:08 +1000313 int i, check1, check2, cipher_type;
Damien Miller95def091999-11-25 00:26:21 +1100314 off_t len;
315 Buffer buffer, decrypted;
316 char *cp;
Damien Miller874d77b2000-10-14 16:23:11 +1100317 CipherContext ciphercontext;
318 Cipher *cipher;
Damien Miller95def091999-11-25 00:26:21 +1100319 BN_CTX *ctx;
320 BIGNUM *aux;
Ben Lindstromd0fca422001-03-26 13:44:06 +0000321 Key *prv = NULL;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000322
Damien Miller037a0dc1999-12-07 15:38:31 +1100323 len = lseek(fd, (off_t) 0, SEEK_END);
324 lseek(fd, (off_t) 0, SEEK_SET);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000325
Damien Miller95def091999-11-25 00:26:21 +1100326 buffer_init(&buffer);
327 buffer_append_space(&buffer, &cp, len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000328
Damien Miller037a0dc1999-12-07 15:38:31 +1100329 if (read(fd, cp, (size_t) len) != (size_t) len) {
Damien Miller95def091999-11-25 00:26:21 +1100330 debug("Read from key file %.200s failed: %.100s", filename,
Damien Miller0bc1bd82000-11-13 22:57:25 +1100331 strerror(errno));
Damien Miller95def091999-11-25 00:26:21 +1100332 buffer_free(&buffer);
Damien Miller037a0dc1999-12-07 15:38:31 +1100333 close(fd);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000334 return NULL;
Damien Miller95def091999-11-25 00:26:21 +1100335 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000336
Ben Lindstrom1170d712001-01-29 07:51:26 +0000337 /* Check that it is at least big enough to contain the ID string. */
338 if (len < sizeof(authfile_id_string)) {
Ben Lindstrom15f33862001-04-16 02:00:02 +0000339 debug3("No RSA1 key file %.200s.", filename);
Damien Miller95def091999-11-25 00:26:21 +1100340 buffer_free(&buffer);
Ben Lindstromb257cca2001-03-05 04:59:27 +0000341 close(fd);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000342 return NULL;
Damien Miller95def091999-11-25 00:26:21 +1100343 }
Damien Miller5428f641999-11-25 11:54:57 +1100344 /*
345 * Make sure it begins with the id string. Consume the id string
346 * from the buffer.
347 */
Ben Lindstrom1170d712001-01-29 07:51:26 +0000348 for (i = 0; i < sizeof(authfile_id_string); i++)
349 if (buffer_get_char(&buffer) != authfile_id_string[i]) {
Ben Lindstrom15f33862001-04-16 02:00:02 +0000350 debug3("No RSA1 key file %.200s.", filename);
Damien Miller95def091999-11-25 00:26:21 +1100351 buffer_free(&buffer);
Ben Lindstromb257cca2001-03-05 04:59:27 +0000352 close(fd);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000353 return NULL;
Damien Miller95def091999-11-25 00:26:21 +1100354 }
Ben Lindstromb257cca2001-03-05 04:59:27 +0000355
Damien Miller95def091999-11-25 00:26:21 +1100356 /* 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);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000362 prv = key_new_private(KEY_RSA1);
363
364 buffer_get_bignum(&buffer, prv->rsa->n);
365 buffer_get_bignum(&buffer, prv->rsa->e);
366 if (commentp)
367 *commentp = buffer_get_string(&buffer, NULL);
Damien Miller95def091999-11-25 00:26:21 +1100368 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)
Ben Lindstromd0fca422001-03-26 13:44:06 +0000395 debug("Bad passphrase supplied for key file %.200s.",
396 filename);
Damien Miller95def091999-11-25 00:26:21 +1100397 /* Bad passphrase. */
398 buffer_free(&decrypted);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000399 goto fail;
Damien Miller95def091999-11-25 00:26:21 +1100400 }
401 /* Read the rest of the private key. */
Ben Lindstromd0fca422001-03-26 13:44:06 +0000402 buffer_get_bignum(&decrypted, prv->rsa->d);
403 buffer_get_bignum(&decrypted, prv->rsa->iqmp); /* u */
404 /* in SSL and SSH v1 p and q are exchanged */
405 buffer_get_bignum(&decrypted, prv->rsa->q); /* p */
406 buffer_get_bignum(&decrypted, prv->rsa->p); /* q */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000407
Ben Lindstromd0fca422001-03-26 13:44:06 +0000408 /* calculate p-1 and q-1 */
Damien Miller95def091999-11-25 00:26:21 +1100409 ctx = BN_CTX_new();
410 aux = BN_new();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000411
Ben Lindstromd0fca422001-03-26 13:44:06 +0000412 BN_sub(aux, prv->rsa->q, BN_value_one());
413 BN_mod(prv->rsa->dmq1, prv->rsa->d, aux, ctx);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000414
Ben Lindstromd0fca422001-03-26 13:44:06 +0000415 BN_sub(aux, prv->rsa->p, BN_value_one());
416 BN_mod(prv->rsa->dmp1, prv->rsa->d, aux, ctx);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000417
Damien Miller95def091999-11-25 00:26:21 +1100418 BN_clear_free(aux);
419 BN_CTX_free(ctx);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000420
Damien Miller95def091999-11-25 00:26:21 +1100421 buffer_free(&decrypted);
Ben Lindstromb257cca2001-03-05 04:59:27 +0000422 close(fd);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000423 return prv;
424
425fail:
426 if (commentp)
427 xfree(*commentp);
428 close(fd);
429 key_free(prv);
430 return NULL;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000431}
Damien Millereba71ba2000-04-29 23:57:08 +1000432
Ben Lindstrombba81212001-06-25 05:01:22 +0000433static Key *
Ben Lindstromd0fca422001-03-26 13:44:06 +0000434key_load_private_pem(int fd, int type, const char *passphrase,
435 char **commentp)
Damien Millereba71ba2000-04-29 23:57:08 +1000436{
Damien Millereba71ba2000-04-29 23:57:08 +1000437 FILE *fp;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100438 EVP_PKEY *pk = NULL;
Ben Lindstromd0fca422001-03-26 13:44:06 +0000439 Key *prv = NULL;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100440 char *name = "<no key>";
Damien Millereba71ba2000-04-29 23:57:08 +1000441
Damien Millereba71ba2000-04-29 23:57:08 +1000442 fp = fdopen(fd, "r");
443 if (fp == NULL) {
Ben Lindstrom15f33862001-04-16 02:00:02 +0000444 error("fdopen failed: %s", strerror(errno));
Ben Lindstromb257cca2001-03-05 04:59:27 +0000445 close(fd);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000446 return NULL;
Damien Millereba71ba2000-04-29 23:57:08 +1000447 }
Damien Miller0bc1bd82000-11-13 22:57:25 +1100448 pk = PEM_read_PrivateKey(fp, NULL, NULL, (char *)passphrase);
449 if (pk == NULL) {
Ben Lindstrom648772f2001-04-19 20:47:10 +0000450 debug("PEM_read_PrivateKey failed");
Damien Miller0bc1bd82000-11-13 22:57:25 +1100451 (void)ERR_get_error();
Ben Lindstromd0fca422001-03-26 13:44:06 +0000452 } else if (pk->type == EVP_PKEY_RSA &&
453 (type == KEY_UNSPEC||type==KEY_RSA)) {
454 prv = key_new(KEY_UNSPEC);
455 prv->rsa = EVP_PKEY_get1_RSA(pk);
456 prv->type = KEY_RSA;
457 name = "rsa w/o comment";
Damien Miller0bc1bd82000-11-13 22:57:25 +1100458#ifdef DEBUG_PK
Ben Lindstromd0fca422001-03-26 13:44:06 +0000459 RSA_print_fp(stderr, prv->rsa, 8);
Damien Millereba71ba2000-04-29 23:57:08 +1000460#endif
Ben Lindstromd0fca422001-03-26 13:44:06 +0000461 } else if (pk->type == EVP_PKEY_DSA &&
462 (type == KEY_UNSPEC||type==KEY_DSA)) {
463 prv = key_new(KEY_UNSPEC);
464 prv->dsa = EVP_PKEY_get1_DSA(pk);
465 prv->type = KEY_DSA;
466 name = "dsa w/o comment";
Damien Miller0bc1bd82000-11-13 22:57:25 +1100467#ifdef DEBUG_PK
Ben Lindstromd0fca422001-03-26 13:44:06 +0000468 DSA_print_fp(stderr, prv->dsa, 8);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100469#endif
Damien Miller0bc1bd82000-11-13 22:57:25 +1100470 } else {
471 error("PEM_read_PrivateKey: mismatch or "
472 "unknown EVP_PKEY save_type %d", pk->save_type);
473 }
474 fclose(fp);
475 if (pk != NULL)
476 EVP_PKEY_free(pk);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000477 if (prv != NULL && commentp)
478 *commentp = xstrdup(name);
479 debug("read PEM private key done: type %s",
480 prv ? key_type(prv) : "<unknown>");
481 return prv;
Damien Millereba71ba2000-04-29 23:57:08 +1000482}
483
Ben Lindstrombba81212001-06-25 05:01:22 +0000484static int
Ben Lindstromd0fca422001-03-26 13:44:06 +0000485key_perm_ok(int fd, const char *filename)
Damien Millereba71ba2000-04-29 23:57:08 +1000486{
Damien Millereba71ba2000-04-29 23:57:08 +1000487 struct stat st;
488
Ben Lindstrom7aff2612001-09-23 13:53:22 +0000489 if (fstat(fd, &st) < 0)
490 return 0;
491 /*
492 * if a key owned by the user is accessed, then we check the
493 * permissions of the file. if the key owned by a different user,
494 * then we don't care.
495 */
Damien Millerb70b61f2000-09-16 16:25:12 +1100496#ifdef HAVE_CYGWIN
Damien Millercb5e44a2000-09-29 12:12:36 +1100497 if (check_ntsec(filename))
Damien Millerb70b61f2000-09-16 16:25:12 +1100498#endif
Ben Lindstrom7aff2612001-09-23 13:53:22 +0000499 if ((st.st_uid == getuid()) && (st.st_mode & 077) != 0) {
Damien Millereba71ba2000-04-29 23:57:08 +1000500 error("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
501 error("@ WARNING: UNPROTECTED PRIVATE KEY FILE! @");
502 error("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
Ben Lindstrom7aff2612001-09-23 13:53:22 +0000503 error("Permissions 0%3.3o for '%s' are too open.",
Ben Lindstromb257cca2001-03-05 04:59:27 +0000504 st.st_mode & 0777, filename);
Damien Millereba71ba2000-04-29 23:57:08 +1000505 error("It is recommended that your private key files are NOT accessible by others.");
Ben Lindstromd0fca422001-03-26 13:44:06 +0000506 error("This private key will be ignored.");
Damien Millereba71ba2000-04-29 23:57:08 +1000507 return 0;
508 }
Ben Lindstromd0fca422001-03-26 13:44:06 +0000509 return 1;
510}
Ben Lindstromb257cca2001-03-05 04:59:27 +0000511
Ben Lindstromd0fca422001-03-26 13:44:06 +0000512Key *
513key_load_private_type(int type, const char *filename, const char *passphrase,
514 char **commentp)
515{
516 int fd;
517
518 fd = open(filename, O_RDONLY);
519 if (fd < 0)
520 return NULL;
521 if (!key_perm_ok(fd, filename)) {
Ben Lindstrom15f33862001-04-16 02:00:02 +0000522 error("bad permissions: ignore key: %s", filename);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000523 close(fd);
524 return NULL;
525 }
526 switch (type) {
527 case KEY_RSA1:
528 return key_load_private_rsa1(fd, filename, passphrase,
529 commentp);
530 /* closes fd */
Damien Millereba71ba2000-04-29 23:57:08 +1000531 break;
532 case KEY_DSA:
Damien Miller0bc1bd82000-11-13 22:57:25 +1100533 case KEY_RSA:
534 case KEY_UNSPEC:
Ben Lindstromd0fca422001-03-26 13:44:06 +0000535 return key_load_private_pem(fd, type, passphrase, commentp);
536 /* closes fd */
Ben Lindstromb257cca2001-03-05 04:59:27 +0000537 break;
Damien Millereba71ba2000-04-29 23:57:08 +1000538 default:
Ben Lindstromb257cca2001-03-05 04:59:27 +0000539 close(fd);
Damien Millereba71ba2000-04-29 23:57:08 +1000540 break;
541 }
Ben Lindstromd0fca422001-03-26 13:44:06 +0000542 return NULL;
543}
544
545Key *
546key_load_private(const char *filename, const char *passphrase,
547 char **commentp)
548{
Ben Lindstrom322915d2001-06-05 20:46:32 +0000549 Key *pub, *prv;
Ben Lindstromd0fca422001-03-26 13:44:06 +0000550 int fd;
551
552 fd = open(filename, O_RDONLY);
553 if (fd < 0)
554 return NULL;
555 if (!key_perm_ok(fd, filename)) {
Ben Lindstrom15f33862001-04-16 02:00:02 +0000556 error("bad permissions: ignore key: %s", filename);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000557 close(fd);
558 return NULL;
559 }
560 pub = key_load_public_rsa1(fd, filename, commentp);
561 lseek(fd, (off_t) 0, SEEK_SET); /* rewind */
562 if (pub == NULL) {
563 /* closes fd */
Ben Lindstrom322915d2001-06-05 20:46:32 +0000564 prv = key_load_private_pem(fd, KEY_UNSPEC, passphrase, NULL);
565 /* use the filename as a comment for PEM */
566 if (commentp && prv)
Ben Lindstrom2d0356f2001-06-05 21:13:57 +0000567 *commentp = xstrdup(filename);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000568 } else {
569 /* it's a SSH v1 key if the public key part is readable */
570 key_free(pub);
571 /* closes fd */
Ben Lindstrom322915d2001-06-05 20:46:32 +0000572 prv = key_load_private_rsa1(fd, filename, passphrase, NULL);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000573 }
Ben Lindstrom322915d2001-06-05 20:46:32 +0000574 return prv;
Damien Millereba71ba2000-04-29 23:57:08 +1000575}
Damien Millere4340be2000-09-16 13:29:08 +1100576
Ben Lindstrombba81212001-06-25 05:01:22 +0000577static int
Ben Lindstromd0fca422001-03-26 13:44:06 +0000578key_try_load_public(Key *k, const char *filename, char **commentp)
Damien Millere4340be2000-09-16 13:29:08 +1100579{
580 FILE *f;
Ben Lindstromd0fca422001-03-26 13:44:06 +0000581 char line[4096];
Damien Millere4340be2000-09-16 13:29:08 +1100582 char *cp;
583
584 f = fopen(filename, "r");
585 if (f != NULL) {
586 while (fgets(line, sizeof(line), f)) {
587 line[sizeof(line)-1] = '\0';
588 cp = line;
589 switch(*cp){
590 case '#':
591 case '\n':
592 case '\0':
593 continue;
594 }
595 /* Skip leading whitespace. */
596 for (; *cp && (*cp == ' ' || *cp == '\t'); cp++)
597 ;
598 if (*cp) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100599 if (key_read(k, &cp) == 1) {
Damien Millere4340be2000-09-16 13:29:08 +1100600 if (commentp)
601 *commentp=xstrdup(filename);
602 fclose(f);
603 return 1;
604 }
605 }
606 }
607 fclose(f);
608 }
609 return 0;
610}
611
Ben Lindstromd0fca422001-03-26 13:44:06 +0000612/* load public key from ssh v1 private or any pubkey file */
613Key *
614key_load_public(const char *filename, char **commentp)
Damien Millere4340be2000-09-16 13:29:08 +1100615{
Ben Lindstromd0fca422001-03-26 13:44:06 +0000616 Key *pub;
617 char file[MAXPATHLEN];
Damien Millere4340be2000-09-16 13:29:08 +1100618
Ben Lindstromd0fca422001-03-26 13:44:06 +0000619 pub = key_load_public_type(KEY_RSA1, filename, commentp);
620 if (pub != NULL)
621 return pub;
622 pub = key_new(KEY_UNSPEC);
623 if (key_try_load_public(pub, filename, commentp) == 1)
624 return pub;
625 if ((strlcpy(file, filename, sizeof file) < sizeof(file)) &&
626 (strlcat(file, ".pub", sizeof file) < sizeof(file)) &&
627 (key_try_load_public(pub, file, commentp) == 1))
628 return pub;
629 key_free(pub);
630 return NULL;
Damien Millere4340be2000-09-16 13:29:08 +1100631}