]> andersk Git - openssh.git/blame - authfile.c
- (dtucker) [contrib/cygwin/ssh-host-config] Add SeTcbPrivilege privilege
[openssh.git] / authfile.c
CommitLineData
31652869 1/* $OpenBSD: authfile.c,v 1.76 2006/08/03 03:34:41 deraadt 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>
536c14e8 43#include <sys/param.h>
31652869 44#include <sys/uio.h>
8efc0c15 45
fa08c86b 46#include <openssl/err.h>
a306f2dd 47#include <openssl/evp.h>
42f11eb2 48#include <openssl/pem.h>
a306f2dd 49
dd984467 50#include <errno.h>
d3221cca 51#include <fcntl.h>
cf851879 52#include <stdio.h>
ffa517a8 53#include <stdlib.h>
00146caa 54#include <string.h>
5188ba17 55#include <unistd.h>
d3221cca 56
8efc0c15 57#include "xmalloc.h"
31652869 58#include "cipher.h"
8efc0c15 59#include "buffer.h"
a306f2dd 60#include "key.h"
42f11eb2 61#include "ssh.h"
62#include "log.h"
5ca51e19 63#include "authfile.h"
cf184a44 64#include "rsa.h"
41feb690 65#include "misc.h"
2ceb8101 66#include "atomicio.h"
8efc0c15 67
aeaa3d9e 68/* Version identification string for SSH v1 identity files. */
7e2183c0 69static const char authfile_id_string[] =
70 "SSH PRIVATE KEY FILE FORMAT 1.1\n";
8efc0c15 71
aa3378df 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 */
8efc0c15 78
396c147e 79static int
aeaa3d9e 80key_save_private_rsa1(Key *key, const char *filename, const char *passphrase,
81 const char *comment)
8efc0c15 82{
5260325f 83 Buffer buffer, encrypted;
20905a8e 84 u_char buf[100], *cp;
3ee832e5 85 int fd, i, cipher_num;
94ec8c6b 86 CipherContext ciphercontext;
87 Cipher *cipher;
ca75d7de 88 u_int32_t rnd;
5260325f 89
aa3378df 90 /*
91 * If the passphrase is empty, use SSH_CIPHER_NONE to ease converting
92 * to another cipher; otherwise use SSH_AUTHFILE_CIPHER.
93 */
3ee832e5 94 cipher_num = (strcmp(passphrase, "") == 0) ?
95 SSH_CIPHER_NONE : SSH_AUTHFILE_CIPHER;
96 if ((cipher = cipher_by_number(cipher_num)) == NULL)
94ec8c6b 97 fatal("save_private_key_rsa: bad cipher");
5260325f 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. */
ca75d7de 103 rnd = arc4random();
104 buf[0] = rnd & 0xff;
105 buf[1] = (rnd >> 8) & 0xff;
5260325f 106 buf[2] = buf[0];
107 buf[3] = buf[1];
108 buffer_append(&buffer, buf, 4);
109
aa3378df 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 */
aeaa3d9e 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 */
5260325f 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. */
7e2183c0 128 for (i = 0; authfile_id_string[i]; i++)
129 buffer_put_char(&encrypted, authfile_id_string[i]);
5260325f 130 buffer_put_char(&encrypted, 0);
131
132 /* Store cipher type. */
3ee832e5 133 buffer_put_char(&encrypted, cipher_num);
5260325f 134 buffer_put_int(&encrypted, 0); /* For future extension */
135
136 /* Store public key. This will be in plain text. */
aeaa3d9e 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);
449c5ba5 140 buffer_put_cstring(&encrypted, comment);
5260325f 141
142 /* Allocate space for the private part of the key in the buffer. */
6c0fa2b1 143 cp = buffer_append_space(&encrypted, buffer_len(&buffer));
5260325f 144
3ee832e5 145 cipher_set_key_string(&ciphercontext, cipher, passphrase,
146 CIPHER_ENCRYPT);
147 cipher_crypt(&ciphercontext, cp,
20905a8e 148 buffer_ptr(&buffer), buffer_len(&buffer));
3ee832e5 149 cipher_cleanup(&ciphercontext);
94ec8c6b 150 memset(&ciphercontext, 0, sizeof(ciphercontext));
5260325f 151
152 /* Destroy temporary data. */
153 memset(buf, 0, sizeof(buf));
154 buffer_free(&buffer);
155
a408af76 156 fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0600);
58cfa257 157 if (fd < 0) {
158 error("open %s failed: %s.", filename, strerror(errno));
1bd71826 159 buffer_free(&encrypted);
5260325f 160 return 0;
58cfa257 161 }
2ceb8101 162 if (atomicio(vwrite, fd, buffer_ptr(&encrypted),
163 buffer_len(&encrypted)) != buffer_len(&encrypted)) {
58cfa257 164 error("write to key file %s failed: %s", filename,
184eed6a 165 strerror(errno));
5260325f 166 buffer_free(&encrypted);
a408af76 167 close(fd);
77bb0bca 168 unlink(filename);
5260325f 169 return 0;
170 }
a408af76 171 close(fd);
5260325f 172 buffer_free(&encrypted);
173 return 1;
8efc0c15 174}
175
aeaa3d9e 176/* save SSH v2 key in OpenSSL PEM format */
396c147e 177static int
aeaa3d9e 178key_save_private_pem(Key *key, const char *filename, const char *_passphrase,
179 const char *comment)
a306f2dd 180{
181 FILE *fp;
182 int fd;
fa08c86b 183 int success = 0;
184 int len = strlen(_passphrase);
c66f9d0e 185 u_char *passphrase = (len > 0) ? (u_char *)_passphrase : NULL;
714954dc 186 const EVP_CIPHER *cipher = (len > 0) ? EVP_des_ede3_cbc() : NULL;
a306f2dd 187
188 if (len > 0 && len <= 4) {
58cfa257 189 error("passphrase too short: have %d bytes, need > 4", len);
a306f2dd 190 return 0;
191 }
192 fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0600);
193 if (fd < 0) {
58cfa257 194 error("open %s failed: %s.", filename, strerror(errno));
a306f2dd 195 return 0;
196 }
197 fp = fdopen(fd, "w");
d4f40d92 198 if (fp == NULL) {
58cfa257 199 error("fdopen %s failed: %s.", filename, strerror(errno));
a306f2dd 200 close(fd);
201 return 0;
202 }
fa08c86b 203 switch (key->type) {
569807fb 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;
a306f2dd 212 }
213 fclose(fp);
214 return success;
215}
216
217int
aeaa3d9e 218key_save_private(Key *key, const char *filename, const char *passphrase,
a306f2dd 219 const char *comment)
220{
221 switch (key->type) {
fa08c86b 222 case KEY_RSA1:
aeaa3d9e 223 return key_save_private_rsa1(key, filename, passphrase,
224 comment);
a306f2dd 225 case KEY_DSA:
fa08c86b 226 case KEY_RSA:
aeaa3d9e 227 return key_save_private_pem(key, filename, passphrase,
228 comment);
a306f2dd 229 default:
230 break;
231 }
58cfa257 232 error("key_save_private: cannot save key type %d", key->type);
a306f2dd 233 return 0;
234}
235
aa3378df 236/*
aeaa3d9e 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
aa3378df 239 * otherwise.
240 */
8efc0c15 241
396c147e 242static Key *
aeaa3d9e 243key_load_public_rsa1(int fd, const char *filename, char **commentp)
8efc0c15 244{
5260325f 245 Buffer buffer;
aeaa3d9e 246 Key *pub;
378d9956 247 struct stat st;
5260325f 248 char *cp;
2ceb8101 249 u_int i;
f6be21a0 250 size_t len;
5260325f 251
378d9956 252 if (fstat(fd, &st) < 0) {
253 error("fstat for key file %.200s failed: %.100s",
254 filename, strerror(errno));
255 return NULL;
256 }
528afafa 257 if (st.st_size > 1*1024*1024) {
258 error("key file %.200s too large", filename);
259 return NULL;
260 }
f6be21a0 261 len = (size_t)st.st_size; /* truncated */
5260325f 262
263 buffer_init(&buffer);
6c0fa2b1 264 cp = buffer_append_space(&buffer, len);
5260325f 265
2ceb8101 266 if (atomicio(read, fd, cp, len) != len) {
5260325f 267 debug("Read from key file %.200s failed: %.100s", filename,
a306f2dd 268 strerror(errno));
5260325f 269 buffer_free(&buffer);
aeaa3d9e 270 return NULL;
5260325f 271 }
5260325f 272
7e2183c0 273 /* Check that it is at least big enough to contain the ID string. */
274 if (len < sizeof(authfile_id_string)) {
2cdccb44 275 debug3("Not a RSA1 key file %.200s.", filename);
5260325f 276 buffer_free(&buffer);
aeaa3d9e 277 return NULL;
5260325f 278 }
aa3378df 279 /*
280 * Make sure it begins with the id string. Consume the id string
281 * from the buffer.
282 */
7e2183c0 283 for (i = 0; i < sizeof(authfile_id_string); i++)
284 if (buffer_get_char(&buffer) != authfile_id_string[i]) {
2cdccb44 285 debug3("Not a RSA1 key file %.200s.", filename);
5260325f 286 buffer_free(&buffer);
aeaa3d9e 287 return NULL;
5260325f 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. */
1169c3df 294 (void) buffer_get_int(&buffer);
aeaa3d9e 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);
5260325f 300 /* The encrypted private part is not parsed by this function. */
301
8efc0c15 302 buffer_free(&buffer);
aeaa3d9e 303 return pub;
8efc0c15 304}
305
aeaa3d9e 306/* load public key from private-key file, works only for SSH v1 */
307Key *
308key_load_public_type(int type, const char *filename, char **commentp)
a306f2dd 309{
aeaa3d9e 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;
a306f2dd 320 }
aeaa3d9e 321 return NULL;
a306f2dd 322}
323
aa3378df 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 */
8efc0c15 330
396c147e 331static Key *
aeaa3d9e 332key_load_private_rsa1(int fd, const char *filename, const char *passphrase,
333 char **commentp)
8efc0c15 334{
2ceb8101 335 u_int i;
336 int check1, check2, cipher_type;
f6be21a0 337 size_t len;
5260325f 338 Buffer buffer, decrypted;
20905a8e 339 u_char *cp;
94ec8c6b 340 CipherContext ciphercontext;
341 Cipher *cipher;
aeaa3d9e 342 Key *prv = NULL;
378d9956 343 struct stat st;
5260325f 344
378d9956 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 }
f6be21a0 351 if (st.st_size > 1*1024*1024) {
528afafa 352 error("key file %.200s too large", filename);
f6be21a0 353 close(fd);
354 return (NULL);
355 }
356 len = (size_t)st.st_size; /* truncated */
5260325f 357
358 buffer_init(&buffer);
6c0fa2b1 359 cp = buffer_append_space(&buffer, len);
5260325f 360
2ceb8101 361 if (atomicio(read, fd, cp, len) != len) {
5260325f 362 debug("Read from key file %.200s failed: %.100s", filename,
fa08c86b 363 strerror(errno));
5260325f 364 buffer_free(&buffer);
a408af76 365 close(fd);
aeaa3d9e 366 return NULL;
5260325f 367 }
5260325f 368
7e2183c0 369 /* Check that it is at least big enough to contain the ID string. */
370 if (len < sizeof(authfile_id_string)) {
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 }
aa3378df 376 /*
377 * Make sure it begins with the id string. Consume the id string
378 * from the buffer.
379 */
7e2183c0 380 for (i = 0; i < sizeof(authfile_id_string); i++)
381 if (buffer_get_char(&buffer) != authfile_id_string[i]) {
2cdccb44 382 debug3("Not a RSA1 key file %.200s.", filename);
5260325f 383 buffer_free(&buffer);
81333640 384 close(fd);
aeaa3d9e 385 return NULL;
5260325f 386 }
81333640 387
5260325f 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. */
1169c3df 393 (void) buffer_get_int(&buffer);
aeaa3d9e 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);
5260325f 400 else
401 xfree(buffer_get_string(&buffer, NULL));
402
403 /* Check that it is a supported cipher. */
94ec8c6b 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);
5260325f 408 buffer_free(&buffer);
409 goto fail;
410 }
411 /* Initialize space for decrypted data. */
412 buffer_init(&decrypted);
6c0fa2b1 413 cp = buffer_append_space(&decrypted, buffer_len(&buffer));
5260325f 414
415 /* Rest of the buffer is encrypted. Decrypt it using the passphrase. */
3ee832e5 416 cipher_set_key_string(&ciphercontext, cipher, passphrase,
417 CIPHER_DECRYPT);
418 cipher_crypt(&ciphercontext, cp,
20905a8e 419 buffer_ptr(&buffer), buffer_len(&buffer));
3ee832e5 420 cipher_cleanup(&ciphercontext);
94ec8c6b 421 memset(&ciphercontext, 0, sizeof(ciphercontext));
8efc0c15 422 buffer_free(&buffer);
5260325f 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)
aeaa3d9e 429 debug("Bad passphrase supplied for key file %.200s.",
430 filename);
5260325f 431 /* Bad passphrase. */
432 buffer_free(&decrypted);
aeaa3d9e 433 goto fail;
5260325f 434 }
435 /* Read the rest of the private key. */
aeaa3d9e 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 */
5260325f 441
aeaa3d9e 442 /* calculate p-1 and q-1 */
b775c6f2 443 rsa_generate_additional_parameters(prv->rsa);
5260325f 444
445 buffer_free(&decrypted);
15e98f49 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 }
81333640 452 close(fd);
aeaa3d9e 453 return prv;
454
455fail:
456 if (commentp)
457 xfree(*commentp);
458 close(fd);
459 key_free(prv);
460 return NULL;
8efc0c15 461}
a306f2dd 462
39c00dc2 463Key *
aeaa3d9e 464key_load_private_pem(int fd, int type, const char *passphrase,
465 char **commentp)
a306f2dd 466{
a306f2dd 467 FILE *fp;
fa08c86b 468 EVP_PKEY *pk = NULL;
aeaa3d9e 469 Key *prv = NULL;
fa08c86b 470 char *name = "<no key>";
a306f2dd 471
a306f2dd 472 fp = fdopen(fd, "r");
473 if (fp == NULL) {
58cfa257 474 error("fdopen failed: %s", strerror(errno));
81333640 475 close(fd);
aeaa3d9e 476 return NULL;
a306f2dd 477 }
fa08c86b 478 pk = PEM_read_PrivateKey(fp, NULL, NULL, (char *)passphrase);
479 if (pk == NULL) {
ae88ea7e 480 debug("PEM_read_PrivateKey failed");
fa08c86b 481 (void)ERR_get_error();
aeaa3d9e 482 } else if (pk->type == EVP_PKEY_RSA &&
184eed6a 483 (type == KEY_UNSPEC||type==KEY_RSA)) {
aeaa3d9e 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";
fa08c86b 488#ifdef DEBUG_PK
aeaa3d9e 489 RSA_print_fp(stderr, prv->rsa, 8);
fa08c86b 490#endif
15e98f49 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 }
aeaa3d9e 496 } else if (pk->type == EVP_PKEY_DSA &&
184eed6a 497 (type == KEY_UNSPEC||type==KEY_DSA)) {
aeaa3d9e 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";
fa08c86b 502#ifdef DEBUG_PK
aeaa3d9e 503 DSA_print_fp(stderr, prv->dsa, 8);
fa08c86b 504#endif
fa08c86b 505 } else {
506 error("PEM_read_PrivateKey: mismatch or "
507 "unknown EVP_PKEY save_type %d", pk->save_type);
a306f2dd 508 }
a306f2dd 509 fclose(fp);
fa08c86b 510 if (pk != NULL)
511 EVP_PKEY_free(pk);
aeaa3d9e 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;
a306f2dd 517}
518
45660a22 519int
aeaa3d9e 520key_perm_ok(int fd, const char *filename)
a306f2dd 521{
a306f2dd 522 struct stat st;
523
d842a5eb 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 */
9cd45ea4 531#ifdef HAVE_CYGWIN
22d89d24 532 if (check_ntsec(filename))
9cd45ea4 533#endif
d842a5eb 534 if ((st.st_uid == getuid()) && (st.st_mode & 077) != 0) {
a306f2dd 535 error("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
536 error("@ WARNING: UNPROTECTED PRIVATE KEY FILE! @");
537 error("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
d842a5eb 538 error("Permissions 0%3.3o for '%s' are too open.",
b9ed513a 539 (u_int)st.st_mode & 0777, filename);
a306f2dd 540 error("It is recommended that your private key files are NOT accessible by others.");
aeaa3d9e 541 error("This private key will be ignored.");
a306f2dd 542 return 0;
543 }
aeaa3d9e 544 return 1;
545}
81333640 546
aeaa3d9e 547Key *
548key_load_private_type(int type, const char *filename, const char *passphrase,
fc231518 549 char **commentp, int *perm_ok)
aeaa3d9e 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)) {
fc231518 557 if (perm_ok != NULL)
558 *perm_ok = 0;
58cfa257 559 error("bad permissions: ignore key: %s", filename);
aeaa3d9e 560 close(fd);
561 return NULL;
562 }
fc231518 563 if (perm_ok != NULL)
564 *perm_ok = 1;
aeaa3d9e 565 switch (type) {
566 case KEY_RSA1:
567 return key_load_private_rsa1(fd, filename, passphrase,
568 commentp);
569 /* closes fd */
a306f2dd 570 case KEY_DSA:
fa08c86b 571 case KEY_RSA:
572 case KEY_UNSPEC:
aeaa3d9e 573 return key_load_private_pem(fd, type, passphrase, commentp);
574 /* closes fd */
a306f2dd 575 default:
81333640 576 close(fd);
a306f2dd 577 break;
578 }
aeaa3d9e 579 return NULL;
580}
581
582Key *
583key_load_private(const char *filename, const char *passphrase,
584 char **commentp)
585{
eb2e1595 586 Key *pub, *prv;
aeaa3d9e 587 int fd;
588
589 fd = open(filename, O_RDONLY);
590 if (fd < 0)
591 return NULL;
592 if (!key_perm_ok(fd, filename)) {
58cfa257 593 error("bad permissions: ignore key: %s", filename);
aeaa3d9e 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 */
eb2e1595 601 prv = key_load_private_pem(fd, KEY_UNSPEC, passphrase, NULL);
602 /* use the filename as a comment for PEM */
603 if (commentp && prv)
a10bdd7c 604 *commentp = xstrdup(filename);
aeaa3d9e 605 } else {
606 /* it's a SSH v1 key if the public key part is readable */
607 key_free(pub);
608 /* closes fd */
eb2e1595 609 prv = key_load_private_rsa1(fd, filename, passphrase, NULL);
aeaa3d9e 610 }
eb2e1595 611 return prv;
a306f2dd 612}
bcbf86ec 613
396c147e 614static int
aeaa3d9e 615key_try_load_public(Key *k, const char *filename, char **commentp)
bcbf86ec 616{
617 FILE *f;
ea067773 618 char line[SSH_MAX_PUBKEY_BYTES];
bcbf86ec 619 char *cp;
41feb690 620 u_long linenum = 0;
bcbf86ec 621
622 f = fopen(filename, "r");
623 if (f != NULL) {
ea067773 624 while (read_keyfile_line(f, filename, line, sizeof(line),
625 &linenum) != -1) {
bcbf86ec 626 cp = line;
6aacefa7 627 switch (*cp) {
bcbf86ec 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) {
fa08c86b 637 if (key_read(k, &cp) == 1) {
bcbf86ec 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
aeaa3d9e 650/* load public key from ssh v1 private or any pubkey file */
651Key *
652key_load_public(const char *filename, char **commentp)
bcbf86ec 653{
aeaa3d9e 654 Key *pub;
655 char file[MAXPATHLEN];
656
3566c73c 657 /* try rsa1 private key */
aeaa3d9e 658 pub = key_load_public_type(KEY_RSA1, filename, commentp);
659 if (pub != NULL)
660 return pub;
3566c73c 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 */
aeaa3d9e 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;
bcbf86ec 678}
This page took 0.697503 seconds and 5 git commands to generate.