Annotation of src/usr.bin/ssh/authfile.c, Revision 1.12
1.1 deraadt 1: /*
1.9 deraadt 2: *
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: */
1.1 deraadt 16:
17: #include "includes.h"
1.12 ! markus 18: RCSID("$Id: authfile.c,v 1.11 1999/12/06 19:11:15 deraadt Exp $");
1.1 deraadt 19:
1.3 provos 20: #include <ssl/bn.h>
1.1 deraadt 21: #include "xmalloc.h"
22: #include "buffer.h"
23: #include "bufaux.h"
24: #include "cipher.h"
25: #include "ssh.h"
26:
27: /* Version identification string for identity files. */
28: #define AUTHFILE_ID_STRING "SSH PRIVATE KEY FILE FORMAT 1.1\n"
29:
1.10 markus 30: /*
31: * Saves the authentication (private) key in a file, encrypting it with
32: * passphrase. The identification of the file (lowest 64 bits of n) will
33: * precede the key to provide identification of the key without needing a
34: * passphrase.
35: */
1.1 deraadt 36:
1.3 provos 37: int
38: save_private_key(const char *filename, const char *passphrase,
39: RSA *key, const char *comment)
1.1 deraadt 40: {
1.8 markus 41: Buffer buffer, encrypted;
42: char buf[100], *cp;
1.11 deraadt 43: int fd, i;
1.8 markus 44: CipherContext cipher;
45: int cipher_type;
46: u_int32_t rand;
47:
1.10 markus 48: /*
49: * If the passphrase is empty, use SSH_CIPHER_NONE to ease converting
50: * to another cipher; otherwise use SSH_AUTHFILE_CIPHER.
51: */
1.8 markus 52: if (strcmp(passphrase, "") == 0)
53: cipher_type = SSH_CIPHER_NONE;
54: else
55: cipher_type = SSH_AUTHFILE_CIPHER;
56:
57: /* This buffer is used to built the secret part of the private key. */
58: buffer_init(&buffer);
59:
60: /* Put checkbytes for checking passphrase validity. */
61: rand = arc4random();
62: buf[0] = rand & 0xff;
63: buf[1] = (rand >> 8) & 0xff;
64: buf[2] = buf[0];
65: buf[3] = buf[1];
66: buffer_append(&buffer, buf, 4);
67:
1.10 markus 68: /*
69: * Store the private key (n and e will not be stored because they
70: * will be stored in plain text, and storing them also in encrypted
71: * format would just give known plaintext).
72: */
1.8 markus 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 */
77:
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);
81:
82: /* This buffer will be used to contain the data in the file. */
83: buffer_init(&encrypted);
84:
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);
90:
91: /* Store cipher type. */
92: buffer_put_char(&encrypted, cipher_type);
93: buffer_put_int(&encrypted, 0); /* For future extension */
94:
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));
100:
101: /* Allocate space for the private part of the key in the buffer. */
102: buffer_append_space(&encrypted, &cp, buffer_len(&buffer));
103:
1.12 ! markus 104: cipher_set_key_string(&cipher, cipher_type, passphrase);
1.8 markus 105: cipher_encrypt(&cipher, (unsigned char *) cp,
106: (unsigned char *) buffer_ptr(&buffer),
107: buffer_len(&buffer));
108: memset(&cipher, 0, sizeof(cipher));
109:
110: /* Destroy temporary data. */
111: memset(buf, 0, sizeof(buf));
112: buffer_free(&buffer);
113:
1.11 deraadt 114: fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0600);
115: if (fd < 0)
1.8 markus 116: return 0;
1.11 deraadt 117: if (write(fd, buffer_ptr(&encrypted), buffer_len(&encrypted)) !=
1.8 markus 118: buffer_len(&encrypted)) {
119: debug("Write to key file %.200s failed: %.100s", filename,
120: strerror(errno));
121: buffer_free(&encrypted);
1.11 deraadt 122: close(fd);
1.8 markus 123: remove(filename);
124: return 0;
125: }
1.11 deraadt 126: close(fd);
1.8 markus 127: buffer_free(&encrypted);
128: return 1;
1.1 deraadt 129: }
130:
1.10 markus 131: /*
132: * Loads the public part of the key file. Returns 0 if an error was
133: * encountered (the file does not exist or is not readable), and non-zero
134: * otherwise.
135: */
1.1 deraadt 136:
1.3 provos 137: int
1.8 markus 138: load_public_key(const char *filename, RSA * pub,
1.3 provos 139: char **comment_return)
1.1 deraadt 140: {
1.11 deraadt 141: int fd, i;
1.8 markus 142: off_t len;
143: Buffer buffer;
144: char *cp;
145:
1.11 deraadt 146: fd = open(filename, O_RDONLY);
147: if (fd < 0)
1.8 markus 148: return 0;
1.11 deraadt 149: len = lseek(fd, (off_t) 0, SEEK_END);
150: lseek(fd, (off_t) 0, SEEK_SET);
1.8 markus 151:
152: buffer_init(&buffer);
153: buffer_append_space(&buffer, &cp, len);
154:
1.11 deraadt 155: if (read(fd, cp, (size_t) len) != (size_t) len) {
1.8 markus 156: debug("Read from key file %.200s failed: %.100s", filename,
157: strerror(errno));
158: buffer_free(&buffer);
1.11 deraadt 159: close(fd);
1.8 markus 160: return 0;
161: }
1.11 deraadt 162: close(fd);
1.8 markus 163:
164: /* Check that it is at least big enought to contain the ID string. */
165: if (len < strlen(AUTHFILE_ID_STRING) + 1) {
166: debug("Bad key file %.200s.", filename);
167: buffer_free(&buffer);
168: return 0;
169: }
1.10 markus 170: /*
171: * Make sure it begins with the id string. Consume the id string
172: * from the buffer.
173: */
1.8 markus 174: for (i = 0; i < (unsigned int) strlen(AUTHFILE_ID_STRING) + 1; i++)
1.11 deraadt 175: if (buffer_get_char(&buffer) != (u_char) AUTHFILE_ID_STRING[i]) {
1.8 markus 176: debug("Bad key file %.200s.", filename);
177: buffer_free(&buffer);
178: return 0;
179: }
180: /* Skip cipher type and reserved data. */
181: (void) buffer_get_char(&buffer); /* cipher type */
182: (void) buffer_get_int(&buffer); /* reserved */
183:
184: /* Read the public key from the buffer. */
185: buffer_get_int(&buffer);
186: pub->n = BN_new();
187: buffer_get_bignum(&buffer, pub->n);
188: pub->e = BN_new();
189: buffer_get_bignum(&buffer, pub->e);
190: if (comment_return)
191: *comment_return = buffer_get_string(&buffer, NULL);
192: /* The encrypted private part is not parsed by this function. */
193:
1.1 deraadt 194: buffer_free(&buffer);
195:
1.8 markus 196: return 1;
1.1 deraadt 197: }
198:
1.10 markus 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). This
202: * initializes the private key.
203: * Assumes we are called under uid of the owner of the file.
204: */
1.1 deraadt 205:
1.3 provos 206: int
207: load_private_key(const char *filename, const char *passphrase,
1.8 markus 208: RSA * prv, char **comment_return)
1.1 deraadt 209: {
1.11 deraadt 210: int fd, i, check1, check2, cipher_type;
1.8 markus 211: off_t len;
212: Buffer buffer, decrypted;
213: char *cp;
214: CipherContext cipher;
215: BN_CTX *ctx;
216: BIGNUM *aux;
217: struct stat st;
218:
1.11 deraadt 219: fd = open(filename, O_RDONLY);
220: if (fd < 0)
1.8 markus 221: return 0;
222:
1.10 markus 223: /* check owner and modes */
1.11 deraadt 224: if (fstat(fd, &st) < 0 ||
1.8 markus 225: (st.st_uid != 0 && st.st_uid != getuid()) ||
226: (st.st_mode & 077) != 0) {
1.11 deraadt 227: close(fd);
1.8 markus 228: error("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
229: error("@ WARNING: UNPROTECTED PRIVATE KEY FILE! @");
230: error("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
231: error("Bad ownership or mode(0%3.3o) for '%s'.",
232: st.st_mode & 0777, filename);
233: error("It is recommended that your private key files are NOT accessible by others.");
234: return 0;
235: }
1.11 deraadt 236: len = lseek(fd, (off_t) 0, SEEK_END);
237: lseek(fd, (off_t) 0, SEEK_SET);
1.8 markus 238:
239: buffer_init(&buffer);
240: buffer_append_space(&buffer, &cp, len);
241:
1.11 deraadt 242: if (read(fd, cp, (size_t) len) != (size_t) len) {
1.8 markus 243: debug("Read from key file %.200s failed: %.100s", filename,
244: strerror(errno));
245: buffer_free(&buffer);
1.11 deraadt 246: close(fd);
1.8 markus 247: return 0;
248: }
1.11 deraadt 249: close(fd);
1.8 markus 250:
251: /* Check that it is at least big enought to contain the ID string. */
252: if (len < strlen(AUTHFILE_ID_STRING) + 1) {
253: debug("Bad key file %.200s.", filename);
254: buffer_free(&buffer);
255: return 0;
256: }
1.10 markus 257: /*
258: * Make sure it begins with the id string. Consume the id string
259: * from the buffer.
260: */
1.8 markus 261: for (i = 0; i < (unsigned int) strlen(AUTHFILE_ID_STRING) + 1; i++)
262: if (buffer_get_char(&buffer) != (unsigned char) AUTHFILE_ID_STRING[i]) {
263: debug("Bad key file %.200s.", filename);
264: buffer_free(&buffer);
265: return 0;
266: }
267: /* Read cipher type. */
268: cipher_type = buffer_get_char(&buffer);
269: (void) buffer_get_int(&buffer); /* Reserved data. */
270:
271: /* Read the public key from the buffer. */
272: buffer_get_int(&buffer);
273: prv->n = BN_new();
274: buffer_get_bignum(&buffer, prv->n);
275: prv->e = BN_new();
276: buffer_get_bignum(&buffer, prv->e);
277: if (comment_return)
278: *comment_return = buffer_get_string(&buffer, NULL);
279: else
280: xfree(buffer_get_string(&buffer, NULL));
281:
282: /* Check that it is a supported cipher. */
1.12 ! markus 283: if (((cipher_mask1() | SSH_CIPHER_NONE | SSH_AUTHFILE_CIPHER) &
1.8 markus 284: (1 << cipher_type)) == 0) {
285: debug("Unsupported cipher %.100s used in key file %.200s.",
286: cipher_name(cipher_type), filename);
287: buffer_free(&buffer);
288: goto fail;
289: }
290: /* Initialize space for decrypted data. */
291: buffer_init(&decrypted);
292: buffer_append_space(&decrypted, &cp, buffer_len(&buffer));
293:
294: /* Rest of the buffer is encrypted. Decrypt it using the passphrase. */
1.12 ! markus 295: cipher_set_key_string(&cipher, cipher_type, passphrase);
1.8 markus 296: cipher_decrypt(&cipher, (unsigned char *) cp,
297: (unsigned char *) buffer_ptr(&buffer),
298: buffer_len(&buffer));
299:
1.1 deraadt 300: buffer_free(&buffer);
301:
1.8 markus 302: check1 = buffer_get_char(&decrypted);
303: check2 = buffer_get_char(&decrypted);
304: if (check1 != buffer_get_char(&decrypted) ||
305: check2 != buffer_get_char(&decrypted)) {
306: if (strcmp(passphrase, "") != 0)
307: debug("Bad passphrase supplied for key file %.200s.", filename);
308: /* Bad passphrase. */
309: buffer_free(&decrypted);
310: fail:
311: BN_clear_free(prv->n);
312: BN_clear_free(prv->e);
313: if (comment_return)
314: xfree(*comment_return);
315: return 0;
316: }
317: /* Read the rest of the private key. */
318: prv->d = BN_new();
319: buffer_get_bignum(&decrypted, prv->d);
320: prv->iqmp = BN_new();
321: buffer_get_bignum(&decrypted, prv->iqmp); /* u */
322: /* in SSL and SSH p and q are exchanged */
323: prv->q = BN_new();
324: buffer_get_bignum(&decrypted, prv->q); /* p */
325: prv->p = BN_new();
326: buffer_get_bignum(&decrypted, prv->p); /* q */
327:
328: ctx = BN_CTX_new();
329: aux = BN_new();
330:
331: BN_sub(aux, prv->q, BN_value_one());
332: prv->dmq1 = BN_new();
333: BN_mod(prv->dmq1, prv->d, aux, ctx);
334:
335: BN_sub(aux, prv->p, BN_value_one());
336: prv->dmp1 = BN_new();
337: BN_mod(prv->dmp1, prv->d, aux, ctx);
338:
339: BN_clear_free(aux);
340: BN_CTX_free(ctx);
341:
342: buffer_free(&decrypted);
1.1 deraadt 343:
1.8 markus 344: return 1;
1.1 deraadt 345: }