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