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