blob: 97d0a878375edb7815edafcc97c1552b821d2718 [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 Miller5428f641999-11-25 11:54:57 +110018RCSID("$Id: authfile.c,v 1.5 1999/11/25 00:54:58 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
Damien Miller5428f641999-11-25 11:54:57 +110036/*
37 * Saves the authentication (private) key in a file, encrypting it with
38 * passphrase. The identification of the file (lowest 64 bits of n) will
39 * precede the key to provide identification of the key without needing a
40 * passphrase.
41 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100042
43int
44save_private_key(const char *filename, const char *passphrase,
45 RSA *key, const char *comment)
46{
Damien Miller95def091999-11-25 00:26:21 +110047 Buffer buffer, encrypted;
48 char buf[100], *cp;
49 int f, i;
50 CipherContext cipher;
51 int cipher_type;
52 u_int32_t rand;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100053
Damien Miller5428f641999-11-25 11:54:57 +110054 /*
55 * If the passphrase is empty, use SSH_CIPHER_NONE to ease converting
56 * to another cipher; otherwise use SSH_AUTHFILE_CIPHER.
57 */
Damien Miller95def091999-11-25 00:26:21 +110058 if (strcmp(passphrase, "") == 0)
59 cipher_type = SSH_CIPHER_NONE;
60 else
61 cipher_type = SSH_AUTHFILE_CIPHER;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100062
Damien Miller95def091999-11-25 00:26:21 +110063 /* This buffer is used to built the secret part of the private key. */
64 buffer_init(&buffer);
Damien Millerd4a8b7e1999-10-27 13:42:43 +100065
Damien Miller95def091999-11-25 00:26:21 +110066 /* Put checkbytes for checking passphrase validity. */
67 rand = arc4random();
68 buf[0] = rand & 0xff;
69 buf[1] = (rand >> 8) & 0xff;
70 buf[2] = buf[0];
71 buf[3] = buf[1];
72 buffer_append(&buffer, buf, 4);
Damien Millerd4a8b7e1999-10-27 13:42:43 +100073
Damien Miller5428f641999-11-25 11:54:57 +110074 /*
75 * Store the private key (n and e will not be stored because they
76 * will be stored in plain text, and storing them also in encrypted
77 * format would just give known plaintext).
78 */
Damien Miller95def091999-11-25 00:26:21 +110079 buffer_put_bignum(&buffer, key->d);
80 buffer_put_bignum(&buffer, key->iqmp);
81 buffer_put_bignum(&buffer, key->q); /* reverse from SSL p */
82 buffer_put_bignum(&buffer, key->p); /* reverse from SSL q */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100083
Damien Miller95def091999-11-25 00:26:21 +110084 /* Pad the part to be encrypted until its size is a multiple of 8. */
85 while (buffer_len(&buffer) % 8 != 0)
86 buffer_put_char(&buffer, 0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +100087
Damien Miller95def091999-11-25 00:26:21 +110088 /* This buffer will be used to contain the data in the file. */
89 buffer_init(&encrypted);
Damien Millerd4a8b7e1999-10-27 13:42:43 +100090
Damien Miller95def091999-11-25 00:26:21 +110091 /* First store keyfile id string. */
92 cp = AUTHFILE_ID_STRING;
93 for (i = 0; cp[i]; i++)
94 buffer_put_char(&encrypted, cp[i]);
95 buffer_put_char(&encrypted, 0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +100096
Damien Miller95def091999-11-25 00:26:21 +110097 /* Store cipher type. */
98 buffer_put_char(&encrypted, cipher_type);
99 buffer_put_int(&encrypted, 0); /* For future extension */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000100
Damien Miller95def091999-11-25 00:26:21 +1100101 /* Store public key. This will be in plain text. */
102 buffer_put_int(&encrypted, BN_num_bits(key->n));
103 buffer_put_bignum(&encrypted, key->n);
104 buffer_put_bignum(&encrypted, key->e);
105 buffer_put_string(&encrypted, comment, strlen(comment));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000106
Damien Miller95def091999-11-25 00:26:21 +1100107 /* Allocate space for the private part of the key in the buffer. */
108 buffer_append_space(&encrypted, &cp, buffer_len(&buffer));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000109
Damien Miller95def091999-11-25 00:26:21 +1100110 cipher_set_key_string(&cipher, cipher_type, passphrase, 1);
111 cipher_encrypt(&cipher, (unsigned char *) cp,
112 (unsigned char *) buffer_ptr(&buffer),
113 buffer_len(&buffer));
114 memset(&cipher, 0, sizeof(cipher));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000115
Damien Miller95def091999-11-25 00:26:21 +1100116 /* Destroy temporary data. */
117 memset(buf, 0, sizeof(buf));
118 buffer_free(&buffer);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000119
Damien Miller95def091999-11-25 00:26:21 +1100120 f = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0600);
121 if (f < 0)
122 return 0;
Damien Miller95def091999-11-25 00:26:21 +1100123 if (write(f, buffer_ptr(&encrypted), buffer_len(&encrypted)) !=
124 buffer_len(&encrypted)) {
125 debug("Write to key file %.200s failed: %.100s", filename,
126 strerror(errno));
127 buffer_free(&encrypted);
128 close(f);
129 remove(filename);
130 return 0;
131 }
132 close(f);
133 buffer_free(&encrypted);
134 return 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000135}
136
Damien Miller5428f641999-11-25 11:54:57 +1100137/*
138 * Loads the public part of the key file. Returns 0 if an error was
139 * encountered (the file does not exist or is not readable), and non-zero
140 * otherwise.
141 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000142
143int
Damien Miller95def091999-11-25 00:26:21 +1100144load_public_key(const char *filename, RSA * pub,
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000145 char **comment_return)
146{
Damien Miller95def091999-11-25 00:26:21 +1100147 int f, i;
148 off_t len;
149 Buffer buffer;
150 char *cp;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000151
Damien Miller95def091999-11-25 00:26:21 +1100152 f = open(filename, O_RDONLY);
153 if (f < 0)
154 return 0;
Damien Miller95def091999-11-25 00:26:21 +1100155 len = lseek(f, (off_t) 0, SEEK_END);
156 lseek(f, (off_t) 0, SEEK_SET);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000157
Damien Miller95def091999-11-25 00:26:21 +1100158 buffer_init(&buffer);
159 buffer_append_space(&buffer, &cp, len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000160
Damien Miller95def091999-11-25 00:26:21 +1100161 if (read(f, cp, (size_t) len) != (size_t) len) {
162 debug("Read from key file %.200s failed: %.100s", filename,
163 strerror(errno));
164 buffer_free(&buffer);
165 close(f);
166 return 0;
167 }
168 close(f);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000169
Damien Miller95def091999-11-25 00:26:21 +1100170 /* Check that it is at least big enought to contain the ID string. */
171 if (len < strlen(AUTHFILE_ID_STRING) + 1) {
172 debug("Bad key file %.200s.", filename);
173 buffer_free(&buffer);
174 return 0;
175 }
Damien Miller5428f641999-11-25 11:54:57 +1100176 /*
177 * Make sure it begins with the id string. Consume the id string
178 * from the buffer.
179 */
Damien Miller95def091999-11-25 00:26:21 +1100180 for (i = 0; i < (unsigned int) strlen(AUTHFILE_ID_STRING) + 1; i++)
181 if (buffer_get_char(&buffer) != (unsigned char) AUTHFILE_ID_STRING[i]) {
182 debug("Bad key file %.200s.", filename);
183 buffer_free(&buffer);
184 return 0;
185 }
186 /* Skip cipher type and reserved data. */
187 (void) buffer_get_char(&buffer); /* cipher type */
188 (void) buffer_get_int(&buffer); /* reserved */
189
190 /* Read the public key from the buffer. */
191 buffer_get_int(&buffer);
192 pub->n = BN_new();
193 buffer_get_bignum(&buffer, pub->n);
194 pub->e = BN_new();
195 buffer_get_bignum(&buffer, pub->e);
196 if (comment_return)
197 *comment_return = buffer_get_string(&buffer, NULL);
198 /* The encrypted private part is not parsed by this function. */
199
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000200 buffer_free(&buffer);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000201
Damien Miller95def091999-11-25 00:26:21 +1100202 return 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000203}
204
Damien Miller5428f641999-11-25 11:54:57 +1100205/*
206 * Loads the private key from the file. Returns 0 if an error is encountered
207 * (file does not exist or is not readable, or passphrase is bad). This
208 * initializes the private key.
209 * Assumes we are called under uid of the owner of the file.
210 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000211
212int
213load_private_key(const char *filename, const char *passphrase,
Damien Miller95def091999-11-25 00:26:21 +1100214 RSA * prv, char **comment_return)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000215{
Damien Miller95def091999-11-25 00:26:21 +1100216 int f, i, check1, check2, cipher_type;
217 off_t len;
218 Buffer buffer, decrypted;
219 char *cp;
220 CipherContext cipher;
221 BN_CTX *ctx;
222 BIGNUM *aux;
223 struct stat st;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000224
Damien Miller95def091999-11-25 00:26:21 +1100225 f = open(filename, O_RDONLY);
226 if (f < 0)
227 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000228
Damien Miller5428f641999-11-25 11:54:57 +1100229 /* check owner and modes */
Damien Miller95def091999-11-25 00:26:21 +1100230 if (fstat(f, &st) < 0 ||
231 (st.st_uid != 0 && st.st_uid != getuid()) ||
232 (st.st_mode & 077) != 0) {
233 error("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
234 error("@ WARNING: UNPROTECTED PRIVATE KEY FILE! @");
235 error("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
236 error("Bad ownership or mode(0%3.3o) for '%s'.",
237 st.st_mode & 0777, filename);
238 error("It is recommended that your private key files are NOT accessible by others.");
239 return 0;
240 }
241 len = lseek(f, (off_t) 0, SEEK_END);
242 lseek(f, (off_t) 0, SEEK_SET);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000243
Damien Miller95def091999-11-25 00:26:21 +1100244 buffer_init(&buffer);
245 buffer_append_space(&buffer, &cp, len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000246
Damien Miller95def091999-11-25 00:26:21 +1100247 if (read(f, cp, (size_t) len) != (size_t) len) {
248 debug("Read from key file %.200s failed: %.100s", filename,
249 strerror(errno));
250 buffer_free(&buffer);
251 close(f);
252 return 0;
253 }
254 close(f);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000255
Damien Miller95def091999-11-25 00:26:21 +1100256 /* Check that it is at least big enought to contain the ID string. */
257 if (len < strlen(AUTHFILE_ID_STRING) + 1) {
258 debug("Bad key file %.200s.", filename);
259 buffer_free(&buffer);
260 return 0;
261 }
Damien Miller5428f641999-11-25 11:54:57 +1100262 /*
263 * Make sure it begins with the id string. Consume the id string
264 * from the buffer.
265 */
Damien Miller95def091999-11-25 00:26:21 +1100266 for (i = 0; i < (unsigned int) strlen(AUTHFILE_ID_STRING) + 1; i++)
267 if (buffer_get_char(&buffer) != (unsigned char) AUTHFILE_ID_STRING[i]) {
268 debug("Bad key file %.200s.", filename);
269 buffer_free(&buffer);
270 return 0;
271 }
272 /* Read cipher type. */
273 cipher_type = buffer_get_char(&buffer);
274 (void) buffer_get_int(&buffer); /* Reserved data. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000275
Damien Miller95def091999-11-25 00:26:21 +1100276 /* Read the public key from the buffer. */
277 buffer_get_int(&buffer);
278 prv->n = BN_new();
279 buffer_get_bignum(&buffer, prv->n);
280 prv->e = BN_new();
281 buffer_get_bignum(&buffer, prv->e);
282 if (comment_return)
283 *comment_return = buffer_get_string(&buffer, NULL);
284 else
285 xfree(buffer_get_string(&buffer, NULL));
286
287 /* Check that it is a supported cipher. */
288 if (((cipher_mask() | SSH_CIPHER_NONE | SSH_AUTHFILE_CIPHER) &
289 (1 << cipher_type)) == 0) {
290 debug("Unsupported cipher %.100s used in key file %.200s.",
291 cipher_name(cipher_type), filename);
292 buffer_free(&buffer);
293 goto fail;
294 }
295 /* Initialize space for decrypted data. */
296 buffer_init(&decrypted);
297 buffer_append_space(&decrypted, &cp, buffer_len(&buffer));
298
299 /* Rest of the buffer is encrypted. Decrypt it using the passphrase. */
300 cipher_set_key_string(&cipher, cipher_type, passphrase, 0);
301 cipher_decrypt(&cipher, (unsigned char *) cp,
302 (unsigned char *) buffer_ptr(&buffer),
303 buffer_len(&buffer));
304
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000305 buffer_free(&buffer);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000306
Damien Miller95def091999-11-25 00:26:21 +1100307 check1 = buffer_get_char(&decrypted);
308 check2 = buffer_get_char(&decrypted);
309 if (check1 != buffer_get_char(&decrypted) ||
310 check2 != buffer_get_char(&decrypted)) {
311 if (strcmp(passphrase, "") != 0)
312 debug("Bad passphrase supplied for key file %.200s.", filename);
313 /* Bad passphrase. */
314 buffer_free(&decrypted);
315fail:
316 BN_clear_free(prv->n);
317 BN_clear_free(prv->e);
318 if (comment_return)
319 xfree(*comment_return);
320 return 0;
321 }
322 /* Read the rest of the private key. */
323 prv->d = BN_new();
324 buffer_get_bignum(&decrypted, prv->d);
325 prv->iqmp = BN_new();
326 buffer_get_bignum(&decrypted, prv->iqmp); /* u */
327 /* in SSL and SSH p and q are exchanged */
328 prv->q = BN_new();
329 buffer_get_bignum(&decrypted, prv->q); /* p */
330 prv->p = BN_new();
331 buffer_get_bignum(&decrypted, prv->p); /* q */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000332
Damien Miller95def091999-11-25 00:26:21 +1100333 ctx = BN_CTX_new();
334 aux = BN_new();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000335
Damien Miller95def091999-11-25 00:26:21 +1100336 BN_sub(aux, prv->q, BN_value_one());
337 prv->dmq1 = BN_new();
338 BN_mod(prv->dmq1, prv->d, aux, ctx);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000339
Damien Miller95def091999-11-25 00:26:21 +1100340 BN_sub(aux, prv->p, BN_value_one());
341 prv->dmp1 = BN_new();
342 BN_mod(prv->dmp1, prv->d, aux, ctx);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000343
Damien Miller95def091999-11-25 00:26:21 +1100344 BN_clear_free(aux);
345 BN_CTX_free(ctx);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000346
Damien Miller95def091999-11-25 00:26:21 +1100347 buffer_free(&decrypted);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000348
Damien Miller95def091999-11-25 00:26:21 +1100349 return 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000350}