blob: 35a05d3894cf78c4a1ef3f9cc4431c276c3ec300 [file] [log] [blame]
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001/*
Damien Miller95def091999-11-25 00:26:21 +11002 *
3 * authfile.c
4 *
5 * Author: Tatu Ylonen <ylo@cs.hut.fi>
6 *
7 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
8 * All rights reserved
9 *
10 * Created: Mon Mar 27 03:52:05 1995 ylo
11 *
12 * This file contains functions for reading and writing identity files, and
13 * for reading the passphrase from the user.
14 *
15 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100016
17#include "includes.h"
Damien Miller95def091999-11-25 00:26:21 +110018RCSID("$Id: authfile.c,v 1.4 1999/11/24 13:26:22 damien Exp $");
Damien Millerd4a8b7e1999-10-27 13:42:43 +100019
Damien Miller7f6ea021999-10-28 13:25:17 +100020#ifdef HAVE_OPENSSL
Damien Millerd4a8b7e1999-10-27 13:42:43 +100021#include <openssl/bn.h>
Damien Miller7f6ea021999-10-28 13:25:17 +100022#endif
23#ifdef HAVE_SSL
24#include <ssl/bn.h>
25#endif
26
Damien Millerd4a8b7e1999-10-27 13:42:43 +100027#include "xmalloc.h"
28#include "buffer.h"
29#include "bufaux.h"
30#include "cipher.h"
31#include "ssh.h"
32
33/* Version identification string for identity files. */
34#define AUTHFILE_ID_STRING "SSH PRIVATE KEY FILE FORMAT 1.1\n"
35
36/* Saves the authentication (private) key in a file, encrypting it with
37 passphrase. The identification of the file (lowest 64 bits of n)
38 will precede the key to provide identification of the key without
39 needing a passphrase. */
40
41int
42save_private_key(const char *filename, const char *passphrase,
43 RSA *key, const char *comment)
44{
Damien Miller95def091999-11-25 00:26:21 +110045 Buffer buffer, encrypted;
46 char buf[100], *cp;
47 int f, i;
48 CipherContext cipher;
49 int cipher_type;
50 u_int32_t rand;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100051
Damien Miller95def091999-11-25 00:26:21 +110052 /* If the passphrase is empty, use SSH_CIPHER_NONE to ease
53 converting to another cipher; otherwise use
54 SSH_AUTHFILE_CIPHER. */
55 if (strcmp(passphrase, "") == 0)
56 cipher_type = SSH_CIPHER_NONE;
57 else
58 cipher_type = SSH_AUTHFILE_CIPHER;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100059
Damien Miller95def091999-11-25 00:26:21 +110060 /* This buffer is used to built the secret part of the private key. */
61 buffer_init(&buffer);
Damien Millerd4a8b7e1999-10-27 13:42:43 +100062
Damien Miller95def091999-11-25 00:26:21 +110063 /* Put checkbytes for checking passphrase validity. */
64 rand = arc4random();
65 buf[0] = rand & 0xff;
66 buf[1] = (rand >> 8) & 0xff;
67 buf[2] = buf[0];
68 buf[3] = buf[1];
69 buffer_append(&buffer, buf, 4);
Damien Millerd4a8b7e1999-10-27 13:42:43 +100070
Damien Miller95def091999-11-25 00:26:21 +110071 /* Store the private key (n and e will not be stored because they
72 will be stored in plain text, and storing them also in
73 encrypted format would just give known plaintext). */
74 buffer_put_bignum(&buffer, key->d);
75 buffer_put_bignum(&buffer, key->iqmp);
76 buffer_put_bignum(&buffer, key->q); /* reverse from SSL p */
77 buffer_put_bignum(&buffer, key->p); /* reverse from SSL q */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100078
Damien Miller95def091999-11-25 00:26:21 +110079 /* Pad the part to be encrypted until its size is a multiple of 8. */
80 while (buffer_len(&buffer) % 8 != 0)
81 buffer_put_char(&buffer, 0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +100082
Damien Miller95def091999-11-25 00:26:21 +110083 /* This buffer will be used to contain the data in the file. */
84 buffer_init(&encrypted);
Damien Millerd4a8b7e1999-10-27 13:42:43 +100085
Damien Miller95def091999-11-25 00:26:21 +110086 /* First store keyfile id string. */
87 cp = AUTHFILE_ID_STRING;
88 for (i = 0; cp[i]; i++)
89 buffer_put_char(&encrypted, cp[i]);
90 buffer_put_char(&encrypted, 0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +100091
Damien Miller95def091999-11-25 00:26:21 +110092 /* Store cipher type. */
93 buffer_put_char(&encrypted, cipher_type);
94 buffer_put_int(&encrypted, 0); /* For future extension */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100095
Damien Miller95def091999-11-25 00:26:21 +110096 /* Store public key. This will be in plain text. */
97 buffer_put_int(&encrypted, BN_num_bits(key->n));
98 buffer_put_bignum(&encrypted, key->n);
99 buffer_put_bignum(&encrypted, key->e);
100 buffer_put_string(&encrypted, comment, strlen(comment));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000101
Damien Miller95def091999-11-25 00:26:21 +1100102 /* Allocate space for the private part of the key in the buffer. */
103 buffer_append_space(&encrypted, &cp, buffer_len(&buffer));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000104
Damien Miller95def091999-11-25 00:26:21 +1100105 cipher_set_key_string(&cipher, cipher_type, passphrase, 1);
106 cipher_encrypt(&cipher, (unsigned char *) cp,
107 (unsigned char *) buffer_ptr(&buffer),
108 buffer_len(&buffer));
109 memset(&cipher, 0, sizeof(cipher));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000110
Damien Miller95def091999-11-25 00:26:21 +1100111 /* Destroy temporary data. */
112 memset(buf, 0, sizeof(buf));
113 buffer_free(&buffer);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000114
Damien Miller95def091999-11-25 00:26:21 +1100115 /* Write to a file. */
116 f = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0600);
117 if (f < 0)
118 return 0;
119
120 if (write(f, buffer_ptr(&encrypted), buffer_len(&encrypted)) !=
121 buffer_len(&encrypted)) {
122 debug("Write to key file %.200s failed: %.100s", filename,
123 strerror(errno));
124 buffer_free(&encrypted);
125 close(f);
126 remove(filename);
127 return 0;
128 }
129 close(f);
130 buffer_free(&encrypted);
131 return 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000132}
133
134/* Loads the public part of the key file. Returns 0 if an error
135 was encountered (the file does not exist or is not readable), and
136 non-zero otherwise. */
137
138int
Damien Miller95def091999-11-25 00:26:21 +1100139load_public_key(const char *filename, RSA * pub,
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000140 char **comment_return)
141{
Damien Miller95def091999-11-25 00:26:21 +1100142 int f, i;
143 off_t len;
144 Buffer buffer;
145 char *cp;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000146
Damien Miller95def091999-11-25 00:26:21 +1100147 /* Read data from the file into the buffer. */
148 f = open(filename, O_RDONLY);
149 if (f < 0)
150 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000151
Damien Miller95def091999-11-25 00:26:21 +1100152 len = lseek(f, (off_t) 0, SEEK_END);
153 lseek(f, (off_t) 0, SEEK_SET);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000154
Damien Miller95def091999-11-25 00:26:21 +1100155 buffer_init(&buffer);
156 buffer_append_space(&buffer, &cp, len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000157
Damien Miller95def091999-11-25 00:26:21 +1100158 if (read(f, cp, (size_t) len) != (size_t) len) {
159 debug("Read from key file %.200s failed: %.100s", filename,
160 strerror(errno));
161 buffer_free(&buffer);
162 close(f);
163 return 0;
164 }
165 close(f);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000166
Damien Miller95def091999-11-25 00:26:21 +1100167 /* Check that it is at least big enought to contain the ID string. */
168 if (len < strlen(AUTHFILE_ID_STRING) + 1) {
169 debug("Bad key file %.200s.", filename);
170 buffer_free(&buffer);
171 return 0;
172 }
173 /* Make sure it begins with the id string. Consume the id string
174 from the buffer. */
175 for (i = 0; i < (unsigned int) strlen(AUTHFILE_ID_STRING) + 1; i++)
176 if (buffer_get_char(&buffer) != (unsigned char) AUTHFILE_ID_STRING[i]) {
177 debug("Bad key file %.200s.", filename);
178 buffer_free(&buffer);
179 return 0;
180 }
181 /* Skip cipher type and reserved data. */
182 (void) buffer_get_char(&buffer); /* cipher type */
183 (void) buffer_get_int(&buffer); /* reserved */
184
185 /* Read the public key from the buffer. */
186 buffer_get_int(&buffer);
187 pub->n = BN_new();
188 buffer_get_bignum(&buffer, pub->n);
189 pub->e = BN_new();
190 buffer_get_bignum(&buffer, pub->e);
191 if (comment_return)
192 *comment_return = buffer_get_string(&buffer, NULL);
193 /* The encrypted private part is not parsed by this function. */
194
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000195 buffer_free(&buffer);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000196
Damien Miller95def091999-11-25 00:26:21 +1100197 return 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000198}
199
200/* Loads the private key from the file. Returns 0 if an error is encountered
201 (file does not exist or is not readable, or passphrase is bad).
202 This initializes the private key. */
203
204int
205load_private_key(const char *filename, const char *passphrase,
Damien Miller95def091999-11-25 00:26:21 +1100206 RSA * prv, char **comment_return)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000207{
Damien Miller95def091999-11-25 00:26:21 +1100208 int f, i, check1, check2, cipher_type;
209 off_t len;
210 Buffer buffer, decrypted;
211 char *cp;
212 CipherContext cipher;
213 BN_CTX *ctx;
214 BIGNUM *aux;
215 struct stat st;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000216
Damien Miller95def091999-11-25 00:26:21 +1100217 /* Read the file into the buffer. */
218 f = open(filename, O_RDONLY);
219 if (f < 0)
220 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000221
Damien Miller95def091999-11-25 00:26:21 +1100222 /* We assume we are called under uid of the owner of the file */
223 if (fstat(f, &st) < 0 ||
224 (st.st_uid != 0 && st.st_uid != getuid()) ||
225 (st.st_mode & 077) != 0) {
226 error("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
227 error("@ WARNING: UNPROTECTED PRIVATE KEY FILE! @");
228 error("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
229 error("Bad ownership or mode(0%3.3o) for '%s'.",
230 st.st_mode & 0777, filename);
231 error("It is recommended that your private key files are NOT accessible by others.");
232 return 0;
233 }
234 len = lseek(f, (off_t) 0, SEEK_END);
235 lseek(f, (off_t) 0, SEEK_SET);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000236
Damien Miller95def091999-11-25 00:26:21 +1100237 buffer_init(&buffer);
238 buffer_append_space(&buffer, &cp, len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000239
Damien Miller95def091999-11-25 00:26:21 +1100240 if (read(f, cp, (size_t) len) != (size_t) len) {
241 debug("Read from key file %.200s failed: %.100s", filename,
242 strerror(errno));
243 buffer_free(&buffer);
244 close(f);
245 return 0;
246 }
247 close(f);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000248
Damien Miller95def091999-11-25 00:26:21 +1100249 /* Check that it is at least big enought to contain the ID string. */
250 if (len < strlen(AUTHFILE_ID_STRING) + 1) {
251 debug("Bad key file %.200s.", filename);
252 buffer_free(&buffer);
253 return 0;
254 }
255 /* Make sure it begins with the id string. Consume the id string
256 from the buffer. */
257 for (i = 0; i < (unsigned int) strlen(AUTHFILE_ID_STRING) + 1; i++)
258 if (buffer_get_char(&buffer) != (unsigned char) AUTHFILE_ID_STRING[i]) {
259 debug("Bad key file %.200s.", filename);
260 buffer_free(&buffer);
261 return 0;
262 }
263 /* Read cipher type. */
264 cipher_type = buffer_get_char(&buffer);
265 (void) buffer_get_int(&buffer); /* Reserved data. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000266
Damien Miller95def091999-11-25 00:26:21 +1100267 /* Read the public key from the buffer. */
268 buffer_get_int(&buffer);
269 prv->n = BN_new();
270 buffer_get_bignum(&buffer, prv->n);
271 prv->e = BN_new();
272 buffer_get_bignum(&buffer, prv->e);
273 if (comment_return)
274 *comment_return = buffer_get_string(&buffer, NULL);
275 else
276 xfree(buffer_get_string(&buffer, NULL));
277
278 /* Check that it is a supported cipher. */
279 if (((cipher_mask() | SSH_CIPHER_NONE | SSH_AUTHFILE_CIPHER) &
280 (1 << cipher_type)) == 0) {
281 debug("Unsupported cipher %.100s used in key file %.200s.",
282 cipher_name(cipher_type), filename);
283 buffer_free(&buffer);
284 goto fail;
285 }
286 /* Initialize space for decrypted data. */
287 buffer_init(&decrypted);
288 buffer_append_space(&decrypted, &cp, buffer_len(&buffer));
289
290 /* Rest of the buffer is encrypted. Decrypt it using the passphrase. */
291 cipher_set_key_string(&cipher, cipher_type, passphrase, 0);
292 cipher_decrypt(&cipher, (unsigned char *) cp,
293 (unsigned char *) buffer_ptr(&buffer),
294 buffer_len(&buffer));
295
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000296 buffer_free(&buffer);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000297
Damien Miller95def091999-11-25 00:26:21 +1100298 check1 = buffer_get_char(&decrypted);
299 check2 = buffer_get_char(&decrypted);
300 if (check1 != buffer_get_char(&decrypted) ||
301 check2 != buffer_get_char(&decrypted)) {
302 if (strcmp(passphrase, "") != 0)
303 debug("Bad passphrase supplied for key file %.200s.", filename);
304 /* Bad passphrase. */
305 buffer_free(&decrypted);
306fail:
307 BN_clear_free(prv->n);
308 BN_clear_free(prv->e);
309 if (comment_return)
310 xfree(*comment_return);
311 return 0;
312 }
313 /* Read the rest of the private key. */
314 prv->d = BN_new();
315 buffer_get_bignum(&decrypted, prv->d);
316 prv->iqmp = BN_new();
317 buffer_get_bignum(&decrypted, prv->iqmp); /* u */
318 /* in SSL and SSH p and q are exchanged */
319 prv->q = BN_new();
320 buffer_get_bignum(&decrypted, prv->q); /* p */
321 prv->p = BN_new();
322 buffer_get_bignum(&decrypted, prv->p); /* q */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000323
Damien Miller95def091999-11-25 00:26:21 +1100324 ctx = BN_CTX_new();
325 aux = BN_new();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000326
Damien Miller95def091999-11-25 00:26:21 +1100327 BN_sub(aux, prv->q, BN_value_one());
328 prv->dmq1 = BN_new();
329 BN_mod(prv->dmq1, prv->d, aux, ctx);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000330
Damien Miller95def091999-11-25 00:26:21 +1100331 BN_sub(aux, prv->p, BN_value_one());
332 prv->dmp1 = BN_new();
333 BN_mod(prv->dmp1, prv->d, aux, ctx);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000334
Damien Miller95def091999-11-25 00:26:21 +1100335 BN_clear_free(aux);
336 BN_CTX_free(ctx);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000337
Damien Miller95def091999-11-25 00:26:21 +1100338 buffer_free(&decrypted);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000339
Damien Miller95def091999-11-25 00:26:21 +1100340 return 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000341}