]> andersk Git - openssh.git/blame - authfile.c
- djm@cvs.openbsd.org 2006/03/25 13:17:03
[openssh.git] / authfile.c
CommitLineData
c1cb7bae 1/* $OpenBSD: authfile.c,v 1.66 2006/03/25 13:17:01 djm 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
42f11eb2 48#include "cipher.h"
8efc0c15 49#include "xmalloc.h"
50#include "buffer.h"
51#include "bufaux.h"
a306f2dd 52#include "key.h"
42f11eb2 53#include "ssh.h"
54#include "log.h"
5ca51e19 55#include "authfile.h"
cf184a44 56#include "rsa.h"
41feb690 57#include "misc.h"
2ceb8101 58#include "atomicio.h"
8efc0c15 59
aeaa3d9e 60/* Version identification string for SSH v1 identity files. */
7e2183c0 61static const char authfile_id_string[] =
62 "SSH PRIVATE KEY FILE FORMAT 1.1\n";
8efc0c15 63
aa3378df 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 */
8efc0c15 70
396c147e 71static int
aeaa3d9e 72key_save_private_rsa1(Key *key, const char *filename, const char *passphrase,
73 const char *comment)
8efc0c15 74{
5260325f 75 Buffer buffer, encrypted;
20905a8e 76 u_char buf[100], *cp;
3ee832e5 77 int fd, i, cipher_num;
94ec8c6b 78 CipherContext ciphercontext;
79 Cipher *cipher;
ca75d7de 80 u_int32_t rnd;
5260325f 81
aa3378df 82 /*
83 * If the passphrase is empty, use SSH_CIPHER_NONE to ease converting
84 * to another cipher; otherwise use SSH_AUTHFILE_CIPHER.
85 */
3ee832e5 86 cipher_num = (strcmp(passphrase, "") == 0) ?
87 SSH_CIPHER_NONE : SSH_AUTHFILE_CIPHER;
88 if ((cipher = cipher_by_number(cipher_num)) == NULL)
94ec8c6b 89 fatal("save_private_key_rsa: bad cipher");
5260325f 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. */
ca75d7de 95 rnd = arc4random();
96 buf[0] = rnd & 0xff;
97 buf[1] = (rnd >> 8) & 0xff;
5260325f 98 buf[2] = buf[0];
99 buf[3] = buf[1];
100 buffer_append(&buffer, buf, 4);
101
aa3378df 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 */
aeaa3d9e 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 */
5260325f 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. */
7e2183c0 120 for (i = 0; authfile_id_string[i]; i++)
121 buffer_put_char(&encrypted, authfile_id_string[i]);
5260325f 122 buffer_put_char(&encrypted, 0);
123
124 /* Store cipher type. */
3ee832e5 125 buffer_put_char(&encrypted, cipher_num);
5260325f 126 buffer_put_int(&encrypted, 0); /* For future extension */
127
128 /* Store public key. This will be in plain text. */
aeaa3d9e 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);
449c5ba5 132 buffer_put_cstring(&encrypted, comment);
5260325f 133
134 /* Allocate space for the private part of the key in the buffer. */
6c0fa2b1 135 cp = buffer_append_space(&encrypted, buffer_len(&buffer));
5260325f 136
3ee832e5 137 cipher_set_key_string(&ciphercontext, cipher, passphrase,
138 CIPHER_ENCRYPT);
139 cipher_crypt(&ciphercontext, cp,
20905a8e 140 buffer_ptr(&buffer), buffer_len(&buffer));
3ee832e5 141 cipher_cleanup(&ciphercontext);
94ec8c6b 142 memset(&ciphercontext, 0, sizeof(ciphercontext));
5260325f 143
144 /* Destroy temporary data. */
145 memset(buf, 0, sizeof(buf));
146 buffer_free(&buffer);
147
a408af76 148 fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0600);
58cfa257 149 if (fd < 0) {
150 error("open %s failed: %s.", filename, strerror(errno));
1bd71826 151 buffer_free(&encrypted);
5260325f 152 return 0;
58cfa257 153 }
2ceb8101 154 if (atomicio(vwrite, fd, buffer_ptr(&encrypted),
155 buffer_len(&encrypted)) != buffer_len(&encrypted)) {
58cfa257 156 error("write to key file %s failed: %s", filename,
184eed6a 157 strerror(errno));
5260325f 158 buffer_free(&encrypted);
a408af76 159 close(fd);
77bb0bca 160 unlink(filename);
5260325f 161 return 0;
162 }
a408af76 163 close(fd);
5260325f 164 buffer_free(&encrypted);
165 return 1;
8efc0c15 166}
167
aeaa3d9e 168/* save SSH v2 key in OpenSSL PEM format */
396c147e 169static int
aeaa3d9e 170key_save_private_pem(Key *key, const char *filename, const char *_passphrase,
171 const char *comment)
a306f2dd 172{
173 FILE *fp;
174 int fd;
fa08c86b 175 int success = 0;
176 int len = strlen(_passphrase);
c66f9d0e 177 u_char *passphrase = (len > 0) ? (u_char *)_passphrase : NULL;
714954dc 178 const EVP_CIPHER *cipher = (len > 0) ? EVP_des_ede3_cbc() : NULL;
a306f2dd 179
180 if (len > 0 && len <= 4) {
58cfa257 181 error("passphrase too short: have %d bytes, need > 4", len);
a306f2dd 182 return 0;
183 }
184 fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0600);
185 if (fd < 0) {
58cfa257 186 error("open %s failed: %s.", filename, strerror(errno));
a306f2dd 187 return 0;
188 }
189 fp = fdopen(fd, "w");
190 if (fp == NULL ) {
58cfa257 191 error("fdopen %s failed: %s.", filename, strerror(errno));
a306f2dd 192 close(fd);
193 return 0;
194 }
fa08c86b 195 switch (key->type) {
569807fb 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;
a306f2dd 204 }
205 fclose(fp);
206 return success;
207}
208
209int
aeaa3d9e 210key_save_private(Key *key, const char *filename, const char *passphrase,
a306f2dd 211 const char *comment)
212{
213 switch (key->type) {
fa08c86b 214 case KEY_RSA1:
aeaa3d9e 215 return key_save_private_rsa1(key, filename, passphrase,
216 comment);
a306f2dd 217 case KEY_DSA:
fa08c86b 218 case KEY_RSA:
aeaa3d9e 219 return key_save_private_pem(key, filename, passphrase,
220 comment);
a306f2dd 221 default:
222 break;
223 }
58cfa257 224 error("key_save_private: cannot save key type %d", key->type);
a306f2dd 225 return 0;
226}
227
aa3378df 228/*
aeaa3d9e 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
aa3378df 231 * otherwise.
232 */
8efc0c15 233
396c147e 234static Key *
aeaa3d9e 235key_load_public_rsa1(int fd, const char *filename, char **commentp)
8efc0c15 236{
5260325f 237 Buffer buffer;
aeaa3d9e 238 Key *pub;
378d9956 239 struct stat st;
5260325f 240 char *cp;
2ceb8101 241 u_int i;
f6be21a0 242 size_t len;
5260325f 243
378d9956 244 if (fstat(fd, &st) < 0) {
245 error("fstat for key file %.200s failed: %.100s",
246 filename, strerror(errno));
247 return NULL;
248 }
528afafa 249 if (st.st_size > 1*1024*1024) {
250 error("key file %.200s too large", filename);
251 return NULL;
252 }
f6be21a0 253 len = (size_t)st.st_size; /* truncated */
5260325f 254
255 buffer_init(&buffer);
6c0fa2b1 256 cp = buffer_append_space(&buffer, len);
5260325f 257
2ceb8101 258 if (atomicio(read, fd, cp, len) != len) {
5260325f 259 debug("Read from key file %.200s failed: %.100s", filename,
a306f2dd 260 strerror(errno));
5260325f 261 buffer_free(&buffer);
aeaa3d9e 262 return NULL;
5260325f 263 }
5260325f 264
7e2183c0 265 /* Check that it is at least big enough to contain the ID string. */
266 if (len < sizeof(authfile_id_string)) {
2cdccb44 267 debug3("Not a RSA1 key file %.200s.", filename);
5260325f 268 buffer_free(&buffer);
aeaa3d9e 269 return NULL;
5260325f 270 }
aa3378df 271 /*
272 * Make sure it begins with the id string. Consume the id string
273 * from the buffer.
274 */
7e2183c0 275 for (i = 0; i < sizeof(authfile_id_string); i++)
276 if (buffer_get_char(&buffer) != authfile_id_string[i]) {
2cdccb44 277 debug3("Not a RSA1 key file %.200s.", filename);
5260325f 278 buffer_free(&buffer);
aeaa3d9e 279 return NULL;
5260325f 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. */
1169c3df 286 (void) buffer_get_int(&buffer);
aeaa3d9e 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);
5260325f 292 /* The encrypted private part is not parsed by this function. */
293
8efc0c15 294 buffer_free(&buffer);
aeaa3d9e 295 return pub;
8efc0c15 296}
297
aeaa3d9e 298/* load public key from private-key file, works only for SSH v1 */
299Key *
300key_load_public_type(int type, const char *filename, char **commentp)
a306f2dd 301{
aeaa3d9e 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;
a306f2dd 312 }
aeaa3d9e 313 return NULL;
a306f2dd 314}
315
aa3378df 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 */
8efc0c15 322
396c147e 323static Key *
aeaa3d9e 324key_load_private_rsa1(int fd, const char *filename, const char *passphrase,
325 char **commentp)
8efc0c15 326{
2ceb8101 327 u_int i;
328 int check1, check2, cipher_type;
f6be21a0 329 size_t len;
5260325f 330 Buffer buffer, decrypted;
20905a8e 331 u_char *cp;
94ec8c6b 332 CipherContext ciphercontext;
333 Cipher *cipher;
aeaa3d9e 334 Key *prv = NULL;
378d9956 335 struct stat st;
5260325f 336
378d9956 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 }
f6be21a0 343 if (st.st_size > 1*1024*1024) {
528afafa 344 error("key file %.200s too large", filename);
f6be21a0 345 close(fd);
346 return (NULL);
347 }
348 len = (size_t)st.st_size; /* truncated */
5260325f 349
350 buffer_init(&buffer);
6c0fa2b1 351 cp = buffer_append_space(&buffer, len);
5260325f 352
2ceb8101 353 if (atomicio(read, fd, cp, len) != len) {
5260325f 354 debug("Read from key file %.200s failed: %.100s", filename,
fa08c86b 355 strerror(errno));
5260325f 356 buffer_free(&buffer);
a408af76 357 close(fd);
aeaa3d9e 358 return NULL;
5260325f 359 }
5260325f 360
7e2183c0 361 /* Check that it is at least big enough to contain the ID string. */
362 if (len < sizeof(authfile_id_string)) {
2cdccb44 363 debug3("Not a RSA1 key file %.200s.", filename);
5260325f 364 buffer_free(&buffer);
81333640 365 close(fd);
aeaa3d9e 366 return NULL;
5260325f 367 }
aa3378df 368 /*
369 * Make sure it begins with the id string. Consume the id string
370 * from the buffer.
371 */
7e2183c0 372 for (i = 0; i < sizeof(authfile_id_string); i++)
373 if (buffer_get_char(&buffer) != authfile_id_string[i]) {
2cdccb44 374 debug3("Not a RSA1 key file %.200s.", filename);
5260325f 375 buffer_free(&buffer);
81333640 376 close(fd);
aeaa3d9e 377 return NULL;
5260325f 378 }
81333640 379
5260325f 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. */
1169c3df 385 (void) buffer_get_int(&buffer);
aeaa3d9e 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);
5260325f 392 else
393 xfree(buffer_get_string(&buffer, NULL));
394
395 /* Check that it is a supported cipher. */
94ec8c6b 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);
5260325f 400 buffer_free(&buffer);
401 goto fail;
402 }
403 /* Initialize space for decrypted data. */
404 buffer_init(&decrypted);
6c0fa2b1 405 cp = buffer_append_space(&decrypted, buffer_len(&buffer));
5260325f 406
407 /* Rest of the buffer is encrypted. Decrypt it using the passphrase. */
3ee832e5 408 cipher_set_key_string(&ciphercontext, cipher, passphrase,
409 CIPHER_DECRYPT);
410 cipher_crypt(&ciphercontext, cp,
20905a8e 411 buffer_ptr(&buffer), buffer_len(&buffer));
3ee832e5 412 cipher_cleanup(&ciphercontext);
94ec8c6b 413 memset(&ciphercontext, 0, sizeof(ciphercontext));
8efc0c15 414 buffer_free(&buffer);
5260325f 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)
aeaa3d9e 421 debug("Bad passphrase supplied for key file %.200s.",
422 filename);
5260325f 423 /* Bad passphrase. */
424 buffer_free(&decrypted);
aeaa3d9e 425 goto fail;
5260325f 426 }
427 /* Read the rest of the private key. */
aeaa3d9e 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 */
5260325f 433
aeaa3d9e 434 /* calculate p-1 and q-1 */
b775c6f2 435 rsa_generate_additional_parameters(prv->rsa);
5260325f 436
437 buffer_free(&decrypted);
15e98f49 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 }
81333640 444 close(fd);
aeaa3d9e 445 return prv;
446
447fail:
448 if (commentp)
449 xfree(*commentp);
450 close(fd);
451 key_free(prv);
452 return NULL;
8efc0c15 453}
a306f2dd 454
39c00dc2 455Key *
aeaa3d9e 456key_load_private_pem(int fd, int type, const char *passphrase,
457 char **commentp)
a306f2dd 458{
a306f2dd 459 FILE *fp;
fa08c86b 460 EVP_PKEY *pk = NULL;
aeaa3d9e 461 Key *prv = NULL;
fa08c86b 462 char *name = "<no key>";
a306f2dd 463
a306f2dd 464 fp = fdopen(fd, "r");
465 if (fp == NULL) {
58cfa257 466 error("fdopen failed: %s", strerror(errno));
81333640 467 close(fd);
aeaa3d9e 468 return NULL;
a306f2dd 469 }
fa08c86b 470 pk = PEM_read_PrivateKey(fp, NULL, NULL, (char *)passphrase);
471 if (pk == NULL) {
ae88ea7e 472 debug("PEM_read_PrivateKey failed");
fa08c86b 473 (void)ERR_get_error();
aeaa3d9e 474 } else if (pk->type == EVP_PKEY_RSA &&
184eed6a 475 (type == KEY_UNSPEC||type==KEY_RSA)) {
aeaa3d9e 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";
fa08c86b 480#ifdef DEBUG_PK
aeaa3d9e 481 RSA_print_fp(stderr, prv->rsa, 8);
fa08c86b 482#endif
15e98f49 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 }
aeaa3d9e 488 } else if (pk->type == EVP_PKEY_DSA &&
184eed6a 489 (type == KEY_UNSPEC||type==KEY_DSA)) {
aeaa3d9e 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";
fa08c86b 494#ifdef DEBUG_PK
aeaa3d9e 495 DSA_print_fp(stderr, prv->dsa, 8);
fa08c86b 496#endif
fa08c86b 497 } else {
498 error("PEM_read_PrivateKey: mismatch or "
499 "unknown EVP_PKEY save_type %d", pk->save_type);
a306f2dd 500 }
a306f2dd 501 fclose(fp);
fa08c86b 502 if (pk != NULL)
503 EVP_PKEY_free(pk);
aeaa3d9e 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;
a306f2dd 509}
510
45660a22 511int
aeaa3d9e 512key_perm_ok(int fd, const char *filename)
a306f2dd 513{
a306f2dd 514 struct stat st;
515
d842a5eb 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 */
9cd45ea4 523#ifdef HAVE_CYGWIN
22d89d24 524 if (check_ntsec(filename))
9cd45ea4 525#endif
d842a5eb 526 if ((st.st_uid == getuid()) && (st.st_mode & 077) != 0) {
a306f2dd 527 error("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
528 error("@ WARNING: UNPROTECTED PRIVATE KEY FILE! @");
529 error("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
d842a5eb 530 error("Permissions 0%3.3o for '%s' are too open.",
b9ed513a 531 (u_int)st.st_mode & 0777, filename);
a306f2dd 532 error("It is recommended that your private key files are NOT accessible by others.");
aeaa3d9e 533 error("This private key will be ignored.");
a306f2dd 534 return 0;
535 }
aeaa3d9e 536 return 1;
537}
81333640 538
aeaa3d9e 539Key *
540key_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)) {
58cfa257 549 error("bad permissions: ignore key: %s", filename);
aeaa3d9e 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 */
a306f2dd 558 case KEY_DSA:
fa08c86b 559 case KEY_RSA:
560 case KEY_UNSPEC:
aeaa3d9e 561 return key_load_private_pem(fd, type, passphrase, commentp);
562 /* closes fd */
a306f2dd 563 default:
81333640 564 close(fd);
a306f2dd 565 break;
566 }
aeaa3d9e 567 return NULL;
568}
569
570Key *
571key_load_private(const char *filename, const char *passphrase,
572 char **commentp)
573{
eb2e1595 574 Key *pub, *prv;
aeaa3d9e 575 int fd;
576
577 fd = open(filename, O_RDONLY);
578 if (fd < 0)
579 return NULL;
580 if (!key_perm_ok(fd, filename)) {
58cfa257 581 error("bad permissions: ignore key: %s", filename);
aeaa3d9e 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 */
eb2e1595 589 prv = key_load_private_pem(fd, KEY_UNSPEC, passphrase, NULL);
590 /* use the filename as a comment for PEM */
591 if (commentp && prv)
a10bdd7c 592 *commentp = xstrdup(filename);
aeaa3d9e 593 } else {
594 /* it's a SSH v1 key if the public key part is readable */
595 key_free(pub);
596 /* closes fd */
eb2e1595 597 prv = key_load_private_rsa1(fd, filename, passphrase, NULL);
aeaa3d9e 598 }
eb2e1595 599 return prv;
a306f2dd 600}
bcbf86ec 601
396c147e 602static int
aeaa3d9e 603key_try_load_public(Key *k, const char *filename, char **commentp)
bcbf86ec 604{
605 FILE *f;
ea067773 606 char line[SSH_MAX_PUBKEY_BYTES];
bcbf86ec 607 char *cp;
41feb690 608 u_long linenum = 0;
bcbf86ec 609
610 f = fopen(filename, "r");
611 if (f != NULL) {
ea067773 612 while (read_keyfile_line(f, filename, line, sizeof(line),
613 &linenum) != -1) {
bcbf86ec 614 cp = line;
6aacefa7 615 switch (*cp) {
bcbf86ec 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) {
fa08c86b 625 if (key_read(k, &cp) == 1) {
bcbf86ec 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
aeaa3d9e 638/* load public key from ssh v1 private or any pubkey file */
639Key *
640key_load_public(const char *filename, char **commentp)
bcbf86ec 641{
aeaa3d9e 642 Key *pub;
643 char file[MAXPATHLEN];
644
3566c73c 645 /* try rsa1 private key */
aeaa3d9e 646 pub = key_load_public_type(KEY_RSA1, filename, commentp);
647 if (pub != NULL)
648 return pub;
3566c73c 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 */
aeaa3d9e 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;
bcbf86ec 666}
This page took 0.370912 seconds and 5 git commands to generate.