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