5 Author: Tatu Ylonen <ylo@cs.hut.fi>
7 Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
10 Created: Mon Mar 27 03:52:05 1995 ylo
12 This file contains functions for reading and writing identity files, and
13 for reading the passphrase from the user.
21 #include <openssl/bn.h>
33 /* Version identification string for identity files. */
34 #define AUTHFILE_ID_STRING "SSH PRIVATE KEY FILE FORMAT 1.1\n"
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. */
42 save_private_key(const char *filename, const char *passphrase,
43 RSA *key, const char *comment)
45 Buffer buffer, encrypted;
52 /* If the passphrase is empty, use SSH_CIPHER_NONE to ease converting to
53 another cipher; otherwise use SSH_AUTHFILE_CIPHER. */
54 if (strcmp(passphrase, "") == 0)
55 cipher_type = SSH_CIPHER_NONE;
57 cipher_type = SSH_AUTHFILE_CIPHER;
59 /* This buffer is used to built the secret part of the private key. */
62 /* Put checkbytes for checking passphrase validity. */
65 buf[1] = (rand >> 8) & 0xff;
68 buffer_append(&buffer, buf, 4);
70 /* Store the private key (n and e will not be stored because they will
71 be stored in plain text, and storing them also in encrypted format
72 would just give known plaintext). */
73 buffer_put_bignum(&buffer, key->d);
74 buffer_put_bignum(&buffer, key->iqmp);
75 buffer_put_bignum(&buffer, key->q); /* reverse from SSL p */
76 buffer_put_bignum(&buffer, key->p); /* reverse from SSL q */
78 /* Pad the part to be encrypted until its size is a multiple of 8. */
79 while (buffer_len(&buffer) % 8 != 0)
80 buffer_put_char(&buffer, 0);
82 /* This buffer will be used to contain the data in the file. */
83 buffer_init(&encrypted);
85 /* First store keyfile id string. */
86 cp = AUTHFILE_ID_STRING;
87 for (i = 0; cp[i]; i++)
88 buffer_put_char(&encrypted, cp[i]);
89 buffer_put_char(&encrypted, 0);
91 /* Store cipher type. */
92 buffer_put_char(&encrypted, cipher_type);
93 buffer_put_int(&encrypted, 0); /* For future extension */
95 /* Store public key. This will be in plain text. */
96 buffer_put_int(&encrypted, BN_num_bits(key->n));
97 buffer_put_bignum(&encrypted, key->n);
98 buffer_put_bignum(&encrypted, key->e);
99 buffer_put_string(&encrypted, comment, strlen(comment));
101 /* Allocate space for the private part of the key in the buffer. */
102 buffer_append_space(&encrypted, &cp, buffer_len(&buffer));
104 cipher_set_key_string(&cipher, cipher_type, passphrase, 1);
105 cipher_encrypt(&cipher, (unsigned char *)cp,
106 (unsigned char *)buffer_ptr(&buffer),
107 buffer_len(&buffer));
108 memset(&cipher, 0, sizeof(cipher));
110 /* Destroy temporary data. */
111 memset(buf, 0, sizeof(buf));
112 buffer_free(&buffer);
114 /* Write to a file. */
115 f = open(filename, O_WRONLY|O_CREAT|O_TRUNC, 0600);
119 if (write(f, buffer_ptr(&encrypted), buffer_len(&encrypted)) !=
120 buffer_len(&encrypted))
122 debug("Write to key file %.200s failed: %.100s", filename,
124 buffer_free(&encrypted);
130 buffer_free(&encrypted);
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. */
139 load_public_key(const char *filename, RSA *pub,
140 char **comment_return)
147 /* Read data from the file into the buffer. */
148 f = open(filename, O_RDONLY);
152 len = lseek(f, (off_t)0, SEEK_END);
153 lseek(f, (off_t)0, SEEK_SET);
155 buffer_init(&buffer);
156 buffer_append_space(&buffer, &cp, len);
158 if (read(f, cp, (size_t)len) != (size_t)len)
160 debug("Read from key file %.200s failed: %.100s", filename,
162 buffer_free(&buffer);
168 /* Check that it is at least big enought to contain the ID string. */
169 if (len < strlen(AUTHFILE_ID_STRING) + 1)
171 debug("Bad key file %.200s.", filename);
172 buffer_free(&buffer);
176 /* Make sure it begins with the id string. Consume the id string from
178 for (i = 0; i < (unsigned int)strlen(AUTHFILE_ID_STRING) + 1; i++)
179 if (buffer_get_char(&buffer) != (unsigned char)AUTHFILE_ID_STRING[i])
181 debug("Bad key file %.200s.", filename);
182 buffer_free(&buffer);
186 /* Skip cipher type and reserved data. */
187 (void)buffer_get_char(&buffer); /* cipher type */
188 (void)buffer_get_int(&buffer); /* reserved */
190 /* Read the public key from the buffer. */
191 buffer_get_int(&buffer);
193 buffer_get_bignum(&buffer, pub->n);
195 buffer_get_bignum(&buffer, pub->e);
197 *comment_return = buffer_get_string(&buffer, NULL);
198 /* The encrypted private part is not parsed by this function. */
200 buffer_free(&buffer);
205 /* Loads the private key from the file. Returns 0 if an error is encountered
206 (file does not exist or is not readable, or passphrase is bad).
207 This initializes the private key. */
210 load_private_key(const char *filename, const char *passphrase,
211 RSA *prv, char **comment_return)
213 int f, i, check1, check2, cipher_type;
215 Buffer buffer, decrypted;
217 CipherContext cipher;
222 /* Read the file into the buffer. */
223 f = open(filename, O_RDONLY);
227 /* We assume we are called under uid of the owner of the file */
228 if (fstat(f, &st) < 0 ||
229 (st.st_uid != 0 && st.st_uid != getuid()) ||
230 (st.st_mode & 077) != 0) {
231 error("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
232 error("@ WARNING: UNPROTECTED PRIVATE KEY FILE! @");
233 error("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
234 error("Bad ownership or mode(0%3.3o) for '%s'.",
235 st.st_mode & 0777, filename);
236 error("It is recommended that your private key files are NOT accessible by others.");
240 len = lseek(f, (off_t)0, SEEK_END);
241 lseek(f, (off_t)0, SEEK_SET);
243 buffer_init(&buffer);
244 buffer_append_space(&buffer, &cp, len);
246 if (read(f, cp, (size_t)len) != (size_t)len)
248 debug("Read from key file %.200s failed: %.100s", filename,
250 buffer_free(&buffer);
256 /* Check that it is at least big enought to contain the ID string. */
257 if (len < strlen(AUTHFILE_ID_STRING) + 1)
259 debug("Bad key file %.200s.", filename);
260 buffer_free(&buffer);
264 /* Make sure it begins with the id string. Consume the id string from
266 for (i = 0; i < (unsigned int)strlen(AUTHFILE_ID_STRING) + 1; i++)
267 if (buffer_get_char(&buffer) != (unsigned char)AUTHFILE_ID_STRING[i])
269 debug("Bad key file %.200s.", filename);
270 buffer_free(&buffer);
274 /* Read cipher type. */
275 cipher_type = buffer_get_char(&buffer);
276 (void)buffer_get_int(&buffer); /* Reserved data. */
278 /* Read the public key from the buffer. */
279 buffer_get_int(&buffer);
281 buffer_get_bignum(&buffer, prv->n);
283 buffer_get_bignum(&buffer, prv->e);
285 *comment_return = buffer_get_string(&buffer, NULL);
287 xfree(buffer_get_string(&buffer, NULL));
289 /* Check that it is a supported cipher. */
290 if (((cipher_mask() | SSH_CIPHER_NONE | SSH_AUTHFILE_CIPHER) &
291 (1 << cipher_type)) == 0)
293 debug("Unsupported cipher %.100s used in key file %.200s.",
294 cipher_name(cipher_type), filename);
295 buffer_free(&buffer);
299 /* Initialize space for decrypted data. */
300 buffer_init(&decrypted);
301 buffer_append_space(&decrypted, &cp, buffer_len(&buffer));
303 /* Rest of the buffer is encrypted. Decrypt it using the passphrase. */
304 cipher_set_key_string(&cipher, cipher_type, passphrase, 0);
305 cipher_decrypt(&cipher, (unsigned char *)cp,
306 (unsigned char *)buffer_ptr(&buffer),
307 buffer_len(&buffer));
309 buffer_free(&buffer);
311 check1 = buffer_get_char(&decrypted);
312 check2 = buffer_get_char(&decrypted);
313 if (check1 != buffer_get_char(&decrypted) ||
314 check2 != buffer_get_char(&decrypted))
316 if (strcmp(passphrase, "") != 0)
317 debug("Bad passphrase supplied for key file %.200s.", filename);
318 /* Bad passphrase. */
319 buffer_free(&decrypted);
321 BN_clear_free(prv->n);
322 BN_clear_free(prv->e);
324 xfree(*comment_return);
328 /* Read the rest of the private key. */
330 buffer_get_bignum(&decrypted, prv->d);
331 prv->iqmp = BN_new();
332 buffer_get_bignum(&decrypted, prv->iqmp); /* u */
333 /* in SSL and SSH p and q are exchanged */
335 buffer_get_bignum(&decrypted, prv->q); /* p */
337 buffer_get_bignum(&decrypted, prv->p); /* q */
342 BN_sub(aux, prv->q, BN_value_one());
343 prv->dmq1 = BN_new();
344 BN_mod(prv->dmq1, prv->d, aux, ctx);
346 BN_sub(aux, prv->p, BN_value_one());
347 prv->dmp1 = BN_new();
348 BN_mod(prv->dmp1, prv->d, aux, ctx);
353 buffer_free(&decrypted);