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"
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
44 save_private_key(const char *filename, const char *passphrase,
45 RSA *key, const char *comment)
47 Buffer buffer, encrypted;
55 * If the passphrase is empty, use SSH_CIPHER_NONE to ease converting
56 * to another cipher; otherwise use SSH_AUTHFILE_CIPHER.
58 if (strcmp(passphrase, "") == 0)
59 cipher_type = SSH_CIPHER_NONE;
61 cipher_type = SSH_AUTHFILE_CIPHER;
63 /* This buffer is used to built the secret part of the private key. */
66 /* Put checkbytes for checking passphrase validity. */
69 buf[1] = (rand >> 8) & 0xff;
72 buffer_append(&buffer, buf, 4);
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).
79 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 */
84 /* 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);
88 /* This buffer will be used to contain the data in the file. */
89 buffer_init(&encrypted);
91 /* 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);
97 /* Store cipher type. */
98 buffer_put_char(&encrypted, cipher_type);
99 buffer_put_int(&encrypted, 0); /* For future extension */
101 /* 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));
107 /* Allocate space for the private part of the key in the buffer. */
108 buffer_append_space(&encrypted, &cp, buffer_len(&buffer));
110 cipher_set_key_string(&cipher, cipher_type, passphrase);
111 cipher_encrypt(&cipher, (unsigned char *) cp,
112 (unsigned char *) buffer_ptr(&buffer),
113 buffer_len(&buffer));
114 memset(&cipher, 0, sizeof(cipher));
116 /* Destroy temporary data. */
117 memset(buf, 0, sizeof(buf));
118 buffer_free(&buffer);
120 fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0600);
123 if (write(fd, buffer_ptr(&encrypted), buffer_len(&encrypted)) !=
124 buffer_len(&encrypted)) {
125 debug("Write to key file %.200s failed: %.100s", filename,
127 buffer_free(&encrypted);
133 buffer_free(&encrypted);
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
144 load_public_key(const char *filename, RSA * pub,
145 char **comment_return)
152 fd = open(filename, O_RDONLY);
155 len = lseek(fd, (off_t) 0, SEEK_END);
156 lseek(fd, (off_t) 0, SEEK_SET);
158 buffer_init(&buffer);
159 buffer_append_space(&buffer, &cp, len);
161 if (read(fd, cp, (size_t) len) != (size_t) len) {
162 debug("Read from key file %.200s failed: %.100s", filename,
164 buffer_free(&buffer);
170 /* 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);
177 * Make sure it begins with the id string. Consume the id string
180 for (i = 0; i < (unsigned int) strlen(AUTHFILE_ID_STRING) + 1; i++)
181 if (buffer_get_char(&buffer) != (u_char) AUTHFILE_ID_STRING[i]) {
182 debug("Bad key file %.200s.", filename);
183 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);
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.
213 load_private_key(const char *filename, const char *passphrase,
214 RSA * prv, char **comment_return)
216 int fd, i, check1, check2, cipher_type;
218 Buffer buffer, decrypted;
220 CipherContext cipher;
225 fd = open(filename, O_RDONLY);
229 /* check owner and modes */
230 if (fstat(fd, &st) < 0 ||
231 (st.st_uid != 0 && getuid() != 0 && st.st_uid != getuid()) ||
232 (st.st_mode & 077) != 0) {
234 error("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
235 error("@ WARNING: UNPROTECTED PRIVATE KEY FILE! @");
236 error("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
237 error("Bad ownership or mode(0%3.3o) for '%s'.",
238 st.st_mode & 0777, filename);
239 error("It is recommended that your private key files are NOT accessible by others.");
242 len = lseek(fd, (off_t) 0, SEEK_END);
243 lseek(fd, (off_t) 0, SEEK_SET);
245 buffer_init(&buffer);
246 buffer_append_space(&buffer, &cp, len);
248 if (read(fd, cp, (size_t) len) != (size_t) len) {
249 debug("Read from key file %.200s failed: %.100s", filename,
251 buffer_free(&buffer);
257 /* Check that it is at least big enought to contain the ID string. */
258 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
267 for (i = 0; i < (unsigned int) strlen(AUTHFILE_ID_STRING) + 1; i++)
268 if (buffer_get_char(&buffer) != (unsigned char) AUTHFILE_ID_STRING[i]) {
269 debug("Bad key file %.200s.", filename);
270 buffer_free(&buffer);
273 /* Read cipher type. */
274 cipher_type = buffer_get_char(&buffer);
275 (void) buffer_get_int(&buffer); /* Reserved data. */
277 /* Read the public key from the buffer. */
278 buffer_get_int(&buffer);
280 buffer_get_bignum(&buffer, prv->n);
282 buffer_get_bignum(&buffer, prv->e);
284 *comment_return = buffer_get_string(&buffer, NULL);
286 xfree(buffer_get_string(&buffer, NULL));
288 /* Check that it is a supported cipher. */
289 if (((cipher_mask1() | SSH_CIPHER_NONE | SSH_AUTHFILE_CIPHER) &
290 (1 << cipher_type)) == 0) {
291 debug("Unsupported cipher %.100s used in key file %.200s.",
292 cipher_name(cipher_type), filename);
293 buffer_free(&buffer);
296 /* Initialize space for decrypted data. */
297 buffer_init(&decrypted);
298 buffer_append_space(&decrypted, &cp, buffer_len(&buffer));
300 /* Rest of the buffer is encrypted. Decrypt it using the passphrase. */
301 cipher_set_key_string(&cipher, cipher_type, passphrase);
302 cipher_decrypt(&cipher, (unsigned char *) cp,
303 (unsigned char *) buffer_ptr(&buffer),
304 buffer_len(&buffer));
306 buffer_free(&buffer);
308 check1 = buffer_get_char(&decrypted);
309 check2 = buffer_get_char(&decrypted);
310 if (check1 != buffer_get_char(&decrypted) ||
311 check2 != buffer_get_char(&decrypted)) {
312 if (strcmp(passphrase, "") != 0)
313 debug("Bad passphrase supplied for key file %.200s.", filename);
314 /* Bad passphrase. */
315 buffer_free(&decrypted);
317 BN_clear_free(prv->n);
318 BN_clear_free(prv->e);
320 xfree(*comment_return);
323 /* Read the rest of the private key. */
325 buffer_get_bignum(&decrypted, prv->d);
326 prv->iqmp = BN_new();
327 buffer_get_bignum(&decrypted, prv->iqmp); /* u */
328 /* in SSL and SSH p and q are exchanged */
330 buffer_get_bignum(&decrypted, prv->q); /* p */
332 buffer_get_bignum(&decrypted, prv->p); /* q */
337 BN_sub(aux, prv->q, BN_value_one());
338 prv->dmq1 = BN_new();
339 BN_mod(prv->dmq1, prv->d, aux, ctx);
341 BN_sub(aux, prv->p, BN_value_one());
342 prv->dmp1 = BN_new();
343 BN_mod(prv->dmp1, prv->d, aux, ctx);
348 buffer_free(&decrypted);