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