]>
Commit | Line | Data |
---|---|---|
1 | /* $OpenBSD: authfile.c,v 1.66 2006/03/25 13:17:01 djm Exp $ */ | |
2 | /* | |
3 | * Author: Tatu Ylonen <ylo@cs.hut.fi> | |
4 | * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland | |
5 | * All rights reserved | |
6 | * This file contains functions for reading and writing identity files, and | |
7 | * for reading the passphrase from the user. | |
8 | * | |
9 | * As far as I am concerned, the code I have written for this software | |
10 | * can be used freely for any purpose. Any derived versions of this | |
11 | * software must be clearly marked as such, and if the derived work is | |
12 | * incompatible with the protocol description in the RFC file, it must be | |
13 | * called by a name other than "ssh" or "Secure Shell". | |
14 | * | |
15 | * | |
16 | * Copyright (c) 2000 Markus Friedl. All rights reserved. | |
17 | * | |
18 | * Redistribution and use in source and binary forms, with or without | |
19 | * modification, are permitted provided that the following conditions | |
20 | * are met: | |
21 | * 1. Redistributions of source code must retain the above copyright | |
22 | * notice, this list of conditions and the following disclaimer. | |
23 | * 2. Redistributions in binary form must reproduce the above copyright | |
24 | * notice, this list of conditions and the following disclaimer in the | |
25 | * documentation and/or other materials provided with the distribution. | |
26 | * | |
27 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR | |
28 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES | |
29 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. | |
30 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, | |
31 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT | |
32 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |
33 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | |
34 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
35 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF | |
36 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
37 | */ | |
38 | ||
39 | #include "includes.h" | |
40 | ||
41 | #include <sys/types.h> | |
42 | #include <sys/stat.h> | |
43 | ||
44 | #include <openssl/err.h> | |
45 | #include <openssl/evp.h> | |
46 | #include <openssl/pem.h> | |
47 | ||
48 | #include "cipher.h" | |
49 | #include "xmalloc.h" | |
50 | #include "buffer.h" | |
51 | #include "bufaux.h" | |
52 | #include "key.h" | |
53 | #include "ssh.h" | |
54 | #include "log.h" | |
55 | #include "authfile.h" | |
56 | #include "rsa.h" | |
57 | #include "misc.h" | |
58 | #include "atomicio.h" | |
59 | ||
60 | /* Version identification string for SSH v1 identity files. */ | |
61 | static const char authfile_id_string[] = | |
62 | "SSH PRIVATE KEY FILE FORMAT 1.1\n"; | |
63 | ||
64 | /* | |
65 | * Saves the authentication (private) key in a file, encrypting it with | |
66 | * passphrase. The identification of the file (lowest 64 bits of n) will | |
67 | * precede the key to provide identification of the key without needing a | |
68 | * passphrase. | |
69 | */ | |
70 | ||
71 | static int | |
72 | key_save_private_rsa1(Key *key, const char *filename, const char *passphrase, | |
73 | const char *comment) | |
74 | { | |
75 | Buffer buffer, encrypted; | |
76 | u_char buf[100], *cp; | |
77 | int fd, i, cipher_num; | |
78 | CipherContext ciphercontext; | |
79 | Cipher *cipher; | |
80 | u_int32_t rnd; | |
81 | ||
82 | /* | |
83 | * If the passphrase is empty, use SSH_CIPHER_NONE to ease converting | |
84 | * to another cipher; otherwise use SSH_AUTHFILE_CIPHER. | |
85 | */ | |
86 | cipher_num = (strcmp(passphrase, "") == 0) ? | |
87 | SSH_CIPHER_NONE : SSH_AUTHFILE_CIPHER; | |
88 | if ((cipher = cipher_by_number(cipher_num)) == NULL) | |
89 | fatal("save_private_key_rsa: bad cipher"); | |
90 | ||
91 | /* This buffer is used to built the secret part of the private key. */ | |
92 | buffer_init(&buffer); | |
93 | ||
94 | /* Put checkbytes for checking passphrase validity. */ | |
95 | rnd = arc4random(); | |
96 | buf[0] = rnd & 0xff; | |
97 | buf[1] = (rnd >> 8) & 0xff; | |
98 | buf[2] = buf[0]; | |
99 | buf[3] = buf[1]; | |
100 | buffer_append(&buffer, buf, 4); | |
101 | ||
102 | /* | |
103 | * Store the private key (n and e will not be stored because they | |
104 | * will be stored in plain text, and storing them also in encrypted | |
105 | * format would just give known plaintext). | |
106 | */ | |
107 | buffer_put_bignum(&buffer, key->rsa->d); | |
108 | buffer_put_bignum(&buffer, key->rsa->iqmp); | |
109 | buffer_put_bignum(&buffer, key->rsa->q); /* reverse from SSL p */ | |
110 | buffer_put_bignum(&buffer, key->rsa->p); /* reverse from SSL q */ | |
111 | ||
112 | /* Pad the part to be encrypted until its size is a multiple of 8. */ | |
113 | while (buffer_len(&buffer) % 8 != 0) | |
114 | buffer_put_char(&buffer, 0); | |
115 | ||
116 | /* This buffer will be used to contain the data in the file. */ | |
117 | buffer_init(&encrypted); | |
118 | ||
119 | /* First store keyfile id string. */ | |
120 | for (i = 0; authfile_id_string[i]; i++) | |
121 | buffer_put_char(&encrypted, authfile_id_string[i]); | |
122 | buffer_put_char(&encrypted, 0); | |
123 | ||
124 | /* Store cipher type. */ | |
125 | buffer_put_char(&encrypted, cipher_num); | |
126 | buffer_put_int(&encrypted, 0); /* For future extension */ | |
127 | ||
128 | /* Store public key. This will be in plain text. */ | |
129 | buffer_put_int(&encrypted, BN_num_bits(key->rsa->n)); | |
130 | buffer_put_bignum(&encrypted, key->rsa->n); | |
131 | buffer_put_bignum(&encrypted, key->rsa->e); | |
132 | buffer_put_cstring(&encrypted, comment); | |
133 | ||
134 | /* Allocate space for the private part of the key in the buffer. */ | |
135 | cp = buffer_append_space(&encrypted, buffer_len(&buffer)); | |
136 | ||
137 | cipher_set_key_string(&ciphercontext, cipher, passphrase, | |
138 | CIPHER_ENCRYPT); | |
139 | cipher_crypt(&ciphercontext, cp, | |
140 | buffer_ptr(&buffer), buffer_len(&buffer)); | |
141 | cipher_cleanup(&ciphercontext); | |
142 | memset(&ciphercontext, 0, sizeof(ciphercontext)); | |
143 | ||
144 | /* Destroy temporary data. */ | |
145 | memset(buf, 0, sizeof(buf)); | |
146 | buffer_free(&buffer); | |
147 | ||
148 | fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0600); | |
149 | if (fd < 0) { | |
150 | error("open %s failed: %s.", filename, strerror(errno)); | |
151 | buffer_free(&encrypted); | |
152 | return 0; | |
153 | } | |
154 | if (atomicio(vwrite, fd, buffer_ptr(&encrypted), | |
155 | buffer_len(&encrypted)) != buffer_len(&encrypted)) { | |
156 | error("write to key file %s failed: %s", filename, | |
157 | strerror(errno)); | |
158 | buffer_free(&encrypted); | |
159 | close(fd); | |
160 | unlink(filename); | |
161 | return 0; | |
162 | } | |
163 | close(fd); | |
164 | buffer_free(&encrypted); | |
165 | return 1; | |
166 | } | |
167 | ||
168 | /* save SSH v2 key in OpenSSL PEM format */ | |
169 | static int | |
170 | key_save_private_pem(Key *key, const char *filename, const char *_passphrase, | |
171 | const char *comment) | |
172 | { | |
173 | FILE *fp; | |
174 | int fd; | |
175 | int success = 0; | |
176 | int len = strlen(_passphrase); | |
177 | u_char *passphrase = (len > 0) ? (u_char *)_passphrase : NULL; | |
178 | const EVP_CIPHER *cipher = (len > 0) ? EVP_des_ede3_cbc() : NULL; | |
179 | ||
180 | if (len > 0 && len <= 4) { | |
181 | error("passphrase too short: have %d bytes, need > 4", len); | |
182 | return 0; | |
183 | } | |
184 | fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0600); | |
185 | if (fd < 0) { | |
186 | error("open %s failed: %s.", filename, strerror(errno)); | |
187 | return 0; | |
188 | } | |
189 | fp = fdopen(fd, "w"); | |
190 | if (fp == NULL ) { | |
191 | error("fdopen %s failed: %s.", filename, strerror(errno)); | |
192 | close(fd); | |
193 | return 0; | |
194 | } | |
195 | switch (key->type) { | |
196 | case KEY_DSA: | |
197 | success = PEM_write_DSAPrivateKey(fp, key->dsa, | |
198 | cipher, passphrase, len, NULL, NULL); | |
199 | break; | |
200 | case KEY_RSA: | |
201 | success = PEM_write_RSAPrivateKey(fp, key->rsa, | |
202 | cipher, passphrase, len, NULL, NULL); | |
203 | break; | |
204 | } | |
205 | fclose(fp); | |
206 | return success; | |
207 | } | |
208 | ||
209 | int | |
210 | key_save_private(Key *key, const char *filename, const char *passphrase, | |
211 | const char *comment) | |
212 | { | |
213 | switch (key->type) { | |
214 | case KEY_RSA1: | |
215 | return key_save_private_rsa1(key, filename, passphrase, | |
216 | comment); | |
217 | case KEY_DSA: | |
218 | case KEY_RSA: | |
219 | return key_save_private_pem(key, filename, passphrase, | |
220 | comment); | |
221 | default: | |
222 | break; | |
223 | } | |
224 | error("key_save_private: cannot save key type %d", key->type); | |
225 | return 0; | |
226 | } | |
227 | ||
228 | /* | |
229 | * Loads the public part of the ssh v1 key file. Returns NULL if an error was | |
230 | * encountered (the file does not exist or is not readable), and the key | |
231 | * otherwise. | |
232 | */ | |
233 | ||
234 | static Key * | |
235 | key_load_public_rsa1(int fd, const char *filename, char **commentp) | |
236 | { | |
237 | Buffer buffer; | |
238 | Key *pub; | |
239 | struct stat st; | |
240 | char *cp; | |
241 | u_int i; | |
242 | size_t len; | |
243 | ||
244 | if (fstat(fd, &st) < 0) { | |
245 | error("fstat for key file %.200s failed: %.100s", | |
246 | filename, strerror(errno)); | |
247 | return NULL; | |
248 | } | |
249 | if (st.st_size > 1*1024*1024) { | |
250 | error("key file %.200s too large", filename); | |
251 | return NULL; | |
252 | } | |
253 | len = (size_t)st.st_size; /* truncated */ | |
254 | ||
255 | buffer_init(&buffer); | |
256 | cp = buffer_append_space(&buffer, len); | |
257 | ||
258 | if (atomicio(read, fd, cp, len) != len) { | |
259 | debug("Read from key file %.200s failed: %.100s", filename, | |
260 | strerror(errno)); | |
261 | buffer_free(&buffer); | |
262 | return NULL; | |
263 | } | |
264 | ||
265 | /* Check that it is at least big enough to contain the ID string. */ | |
266 | if (len < sizeof(authfile_id_string)) { | |
267 | debug3("Not a RSA1 key file %.200s.", filename); | |
268 | buffer_free(&buffer); | |
269 | return NULL; | |
270 | } | |
271 | /* | |
272 | * Make sure it begins with the id string. Consume the id string | |
273 | * from the buffer. | |
274 | */ | |
275 | for (i = 0; i < sizeof(authfile_id_string); i++) | |
276 | if (buffer_get_char(&buffer) != authfile_id_string[i]) { | |
277 | debug3("Not a RSA1 key file %.200s.", filename); | |
278 | buffer_free(&buffer); | |
279 | return NULL; | |
280 | } | |
281 | /* Skip cipher type and reserved data. */ | |
282 | (void) buffer_get_char(&buffer); /* cipher type */ | |
283 | (void) buffer_get_int(&buffer); /* reserved */ | |
284 | ||
285 | /* Read the public key from the buffer. */ | |
286 | (void) buffer_get_int(&buffer); | |
287 | pub = key_new(KEY_RSA1); | |
288 | buffer_get_bignum(&buffer, pub->rsa->n); | |
289 | buffer_get_bignum(&buffer, pub->rsa->e); | |
290 | if (commentp) | |
291 | *commentp = buffer_get_string(&buffer, NULL); | |
292 | /* The encrypted private part is not parsed by this function. */ | |
293 | ||
294 | buffer_free(&buffer); | |
295 | return pub; | |
296 | } | |
297 | ||
298 | /* load public key from private-key file, works only for SSH v1 */ | |
299 | Key * | |
300 | key_load_public_type(int type, const char *filename, char **commentp) | |
301 | { | |
302 | Key *pub; | |
303 | int fd; | |
304 | ||
305 | if (type == KEY_RSA1) { | |
306 | fd = open(filename, O_RDONLY); | |
307 | if (fd < 0) | |
308 | return NULL; | |
309 | pub = key_load_public_rsa1(fd, filename, commentp); | |
310 | close(fd); | |
311 | return pub; | |
312 | } | |
313 | return NULL; | |
314 | } | |
315 | ||
316 | /* | |
317 | * Loads the private key from the file. Returns 0 if an error is encountered | |
318 | * (file does not exist or is not readable, or passphrase is bad). This | |
319 | * initializes the private key. | |
320 | * Assumes we are called under uid of the owner of the file. | |
321 | */ | |
322 | ||
323 | static Key * | |
324 | key_load_private_rsa1(int fd, const char *filename, const char *passphrase, | |
325 | char **commentp) | |
326 | { | |
327 | u_int i; | |
328 | int check1, check2, cipher_type; | |
329 | size_t len; | |
330 | Buffer buffer, decrypted; | |
331 | u_char *cp; | |
332 | CipherContext ciphercontext; | |
333 | Cipher *cipher; | |
334 | Key *prv = NULL; | |
335 | struct stat st; | |
336 | ||
337 | if (fstat(fd, &st) < 0) { | |
338 | error("fstat for key file %.200s failed: %.100s", | |
339 | filename, strerror(errno)); | |
340 | close(fd); | |
341 | return NULL; | |
342 | } | |
343 | if (st.st_size > 1*1024*1024) { | |
344 | error("key file %.200s too large", filename); | |
345 | close(fd); | |
346 | return (NULL); | |
347 | } | |
348 | len = (size_t)st.st_size; /* truncated */ | |
349 | ||
350 | buffer_init(&buffer); | |
351 | cp = buffer_append_space(&buffer, len); | |
352 | ||
353 | if (atomicio(read, fd, cp, len) != len) { | |
354 | debug("Read from key file %.200s failed: %.100s", filename, | |
355 | strerror(errno)); | |
356 | buffer_free(&buffer); | |
357 | close(fd); | |
358 | return NULL; | |
359 | } | |
360 | ||
361 | /* Check that it is at least big enough to contain the ID string. */ | |
362 | if (len < sizeof(authfile_id_string)) { | |
363 | debug3("Not a RSA1 key file %.200s.", filename); | |
364 | buffer_free(&buffer); | |
365 | close(fd); | |
366 | return NULL; | |
367 | } | |
368 | /* | |
369 | * Make sure it begins with the id string. Consume the id string | |
370 | * from the buffer. | |
371 | */ | |
372 | for (i = 0; i < sizeof(authfile_id_string); i++) | |
373 | if (buffer_get_char(&buffer) != authfile_id_string[i]) { | |
374 | debug3("Not a RSA1 key file %.200s.", filename); | |
375 | buffer_free(&buffer); | |
376 | close(fd); | |
377 | return NULL; | |
378 | } | |
379 | ||
380 | /* Read cipher type. */ | |
381 | cipher_type = buffer_get_char(&buffer); | |
382 | (void) buffer_get_int(&buffer); /* Reserved data. */ | |
383 | ||
384 | /* Read the public key from the buffer. */ | |
385 | (void) buffer_get_int(&buffer); | |
386 | prv = key_new_private(KEY_RSA1); | |
387 | ||
388 | buffer_get_bignum(&buffer, prv->rsa->n); | |
389 | buffer_get_bignum(&buffer, prv->rsa->e); | |
390 | if (commentp) | |
391 | *commentp = buffer_get_string(&buffer, NULL); | |
392 | else | |
393 | xfree(buffer_get_string(&buffer, NULL)); | |
394 | ||
395 | /* Check that it is a supported cipher. */ | |
396 | cipher = cipher_by_number(cipher_type); | |
397 | if (cipher == NULL) { | |
398 | debug("Unsupported cipher %d used in key file %.200s.", | |
399 | cipher_type, filename); | |
400 | buffer_free(&buffer); | |
401 | goto fail; | |
402 | } | |
403 | /* Initialize space for decrypted data. */ | |
404 | buffer_init(&decrypted); | |
405 | cp = buffer_append_space(&decrypted, buffer_len(&buffer)); | |
406 | ||
407 | /* Rest of the buffer is encrypted. Decrypt it using the passphrase. */ | |
408 | cipher_set_key_string(&ciphercontext, cipher, passphrase, | |
409 | CIPHER_DECRYPT); | |
410 | cipher_crypt(&ciphercontext, cp, | |
411 | buffer_ptr(&buffer), buffer_len(&buffer)); | |
412 | cipher_cleanup(&ciphercontext); | |
413 | memset(&ciphercontext, 0, sizeof(ciphercontext)); | |
414 | buffer_free(&buffer); | |
415 | ||
416 | check1 = buffer_get_char(&decrypted); | |
417 | check2 = buffer_get_char(&decrypted); | |
418 | if (check1 != buffer_get_char(&decrypted) || | |
419 | check2 != buffer_get_char(&decrypted)) { | |
420 | if (strcmp(passphrase, "") != 0) | |
421 | debug("Bad passphrase supplied for key file %.200s.", | |
422 | filename); | |
423 | /* Bad passphrase. */ | |
424 | buffer_free(&decrypted); | |
425 | goto fail; | |
426 | } | |
427 | /* Read the rest of the private key. */ | |
428 | buffer_get_bignum(&decrypted, prv->rsa->d); | |
429 | buffer_get_bignum(&decrypted, prv->rsa->iqmp); /* u */ | |
430 | /* in SSL and SSH v1 p and q are exchanged */ | |
431 | buffer_get_bignum(&decrypted, prv->rsa->q); /* p */ | |
432 | buffer_get_bignum(&decrypted, prv->rsa->p); /* q */ | |
433 | ||
434 | /* calculate p-1 and q-1 */ | |
435 | rsa_generate_additional_parameters(prv->rsa); | |
436 | ||
437 | buffer_free(&decrypted); | |
438 | ||
439 | /* enable blinding */ | |
440 | if (RSA_blinding_on(prv->rsa, NULL) != 1) { | |
441 | error("key_load_private_rsa1: RSA_blinding_on failed"); | |
442 | goto fail; | |
443 | } | |
444 | close(fd); | |
445 | return prv; | |
446 | ||
447 | fail: | |
448 | if (commentp) | |
449 | xfree(*commentp); | |
450 | close(fd); | |
451 | key_free(prv); | |
452 | return NULL; | |
453 | } | |
454 | ||
455 | Key * | |
456 | key_load_private_pem(int fd, int type, const char *passphrase, | |
457 | char **commentp) | |
458 | { | |
459 | FILE *fp; | |
460 | EVP_PKEY *pk = NULL; | |
461 | Key *prv = NULL; | |
462 | char *name = "<no key>"; | |
463 | ||
464 | fp = fdopen(fd, "r"); | |
465 | if (fp == NULL) { | |
466 | error("fdopen failed: %s", strerror(errno)); | |
467 | close(fd); | |
468 | return NULL; | |
469 | } | |
470 | pk = PEM_read_PrivateKey(fp, NULL, NULL, (char *)passphrase); | |
471 | if (pk == NULL) { | |
472 | debug("PEM_read_PrivateKey failed"); | |
473 | (void)ERR_get_error(); | |
474 | } else if (pk->type == EVP_PKEY_RSA && | |
475 | (type == KEY_UNSPEC||type==KEY_RSA)) { | |
476 | prv = key_new(KEY_UNSPEC); | |
477 | prv->rsa = EVP_PKEY_get1_RSA(pk); | |
478 | prv->type = KEY_RSA; | |
479 | name = "rsa w/o comment"; | |
480 | #ifdef DEBUG_PK | |
481 | RSA_print_fp(stderr, prv->rsa, 8); | |
482 | #endif | |
483 | if (RSA_blinding_on(prv->rsa, NULL) != 1) { | |
484 | error("key_load_private_pem: RSA_blinding_on failed"); | |
485 | key_free(prv); | |
486 | prv = NULL; | |
487 | } | |
488 | } else if (pk->type == EVP_PKEY_DSA && | |
489 | (type == KEY_UNSPEC||type==KEY_DSA)) { | |
490 | prv = key_new(KEY_UNSPEC); | |
491 | prv->dsa = EVP_PKEY_get1_DSA(pk); | |
492 | prv->type = KEY_DSA; | |
493 | name = "dsa w/o comment"; | |
494 | #ifdef DEBUG_PK | |
495 | DSA_print_fp(stderr, prv->dsa, 8); | |
496 | #endif | |
497 | } else { | |
498 | error("PEM_read_PrivateKey: mismatch or " | |
499 | "unknown EVP_PKEY save_type %d", pk->save_type); | |
500 | } | |
501 | fclose(fp); | |
502 | if (pk != NULL) | |
503 | EVP_PKEY_free(pk); | |
504 | if (prv != NULL && commentp) | |
505 | *commentp = xstrdup(name); | |
506 | debug("read PEM private key done: type %s", | |
507 | prv ? key_type(prv) : "<unknown>"); | |
508 | return prv; | |
509 | } | |
510 | ||
511 | int | |
512 | key_perm_ok(int fd, const char *filename) | |
513 | { | |
514 | struct stat st; | |
515 | ||
516 | if (fstat(fd, &st) < 0) | |
517 | return 0; | |
518 | /* | |
519 | * if a key owned by the user is accessed, then we check the | |
520 | * permissions of the file. if the key owned by a different user, | |
521 | * then we don't care. | |
522 | */ | |
523 | #ifdef HAVE_CYGWIN | |
524 | if (check_ntsec(filename)) | |
525 | #endif | |
526 | if ((st.st_uid == getuid()) && (st.st_mode & 077) != 0) { | |
527 | error("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"); | |
528 | error("@ WARNING: UNPROTECTED PRIVATE KEY FILE! @"); | |
529 | error("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"); | |
530 | error("Permissions 0%3.3o for '%s' are too open.", | |
531 | (u_int)st.st_mode & 0777, filename); | |
532 | error("It is recommended that your private key files are NOT accessible by others."); | |
533 | error("This private key will be ignored."); | |
534 | return 0; | |
535 | } | |
536 | return 1; | |
537 | } | |
538 | ||
539 | Key * | |
540 | key_load_private_type(int type, const char *filename, const char *passphrase, | |
541 | char **commentp) | |
542 | { | |
543 | int fd; | |
544 | ||
545 | fd = open(filename, O_RDONLY); | |
546 | if (fd < 0) | |
547 | return NULL; | |
548 | if (!key_perm_ok(fd, filename)) { | |
549 | error("bad permissions: ignore key: %s", filename); | |
550 | close(fd); | |
551 | return NULL; | |
552 | } | |
553 | switch (type) { | |
554 | case KEY_RSA1: | |
555 | return key_load_private_rsa1(fd, filename, passphrase, | |
556 | commentp); | |
557 | /* closes fd */ | |
558 | case KEY_DSA: | |
559 | case KEY_RSA: | |
560 | case KEY_UNSPEC: | |
561 | return key_load_private_pem(fd, type, passphrase, commentp); | |
562 | /* closes fd */ | |
563 | default: | |
564 | close(fd); | |
565 | break; | |
566 | } | |
567 | return NULL; | |
568 | } | |
569 | ||
570 | Key * | |
571 | key_load_private(const char *filename, const char *passphrase, | |
572 | char **commentp) | |
573 | { | |
574 | Key *pub, *prv; | |
575 | int fd; | |
576 | ||
577 | fd = open(filename, O_RDONLY); | |
578 | if (fd < 0) | |
579 | return NULL; | |
580 | if (!key_perm_ok(fd, filename)) { | |
581 | error("bad permissions: ignore key: %s", filename); | |
582 | close(fd); | |
583 | return NULL; | |
584 | } | |
585 | pub = key_load_public_rsa1(fd, filename, commentp); | |
586 | lseek(fd, (off_t) 0, SEEK_SET); /* rewind */ | |
587 | if (pub == NULL) { | |
588 | /* closes fd */ | |
589 | prv = key_load_private_pem(fd, KEY_UNSPEC, passphrase, NULL); | |
590 | /* use the filename as a comment for PEM */ | |
591 | if (commentp && prv) | |
592 | *commentp = xstrdup(filename); | |
593 | } else { | |
594 | /* it's a SSH v1 key if the public key part is readable */ | |
595 | key_free(pub); | |
596 | /* closes fd */ | |
597 | prv = key_load_private_rsa1(fd, filename, passphrase, NULL); | |
598 | } | |
599 | return prv; | |
600 | } | |
601 | ||
602 | static int | |
603 | key_try_load_public(Key *k, const char *filename, char **commentp) | |
604 | { | |
605 | FILE *f; | |
606 | char line[SSH_MAX_PUBKEY_BYTES]; | |
607 | char *cp; | |
608 | u_long linenum = 0; | |
609 | ||
610 | f = fopen(filename, "r"); | |
611 | if (f != NULL) { | |
612 | while (read_keyfile_line(f, filename, line, sizeof(line), | |
613 | &linenum) != -1) { | |
614 | cp = line; | |
615 | switch (*cp) { | |
616 | case '#': | |
617 | case '\n': | |
618 | case '\0': | |
619 | continue; | |
620 | } | |
621 | /* Skip leading whitespace. */ | |
622 | for (; *cp && (*cp == ' ' || *cp == '\t'); cp++) | |
623 | ; | |
624 | if (*cp) { | |
625 | if (key_read(k, &cp) == 1) { | |
626 | if (commentp) | |
627 | *commentp=xstrdup(filename); | |
628 | fclose(f); | |
629 | return 1; | |
630 | } | |
631 | } | |
632 | } | |
633 | fclose(f); | |
634 | } | |
635 | return 0; | |
636 | } | |
637 | ||
638 | /* load public key from ssh v1 private or any pubkey file */ | |
639 | Key * | |
640 | key_load_public(const char *filename, char **commentp) | |
641 | { | |
642 | Key *pub; | |
643 | char file[MAXPATHLEN]; | |
644 | ||
645 | /* try rsa1 private key */ | |
646 | pub = key_load_public_type(KEY_RSA1, filename, commentp); | |
647 | if (pub != NULL) | |
648 | return pub; | |
649 | ||
650 | /* try rsa1 public key */ | |
651 | pub = key_new(KEY_RSA1); | |
652 | if (key_try_load_public(pub, filename, commentp) == 1) | |
653 | return pub; | |
654 | key_free(pub); | |
655 | ||
656 | /* try ssh2 public key */ | |
657 | pub = key_new(KEY_UNSPEC); | |
658 | if (key_try_load_public(pub, filename, commentp) == 1) | |
659 | return pub; | |
660 | if ((strlcpy(file, filename, sizeof file) < sizeof(file)) && | |
661 | (strlcat(file, ".pub", sizeof file) < sizeof(file)) && | |
662 | (key_try_load_public(pub, file, commentp) == 1)) | |
663 | return pub; | |
664 | key_free(pub); | |
665 | return NULL; | |
666 | } |