Annotation of src/usr.bin/ssh/authfile.c, Revision 1.88
1.88 ! djm 1: /* $OpenBSD: authfile.c,v 1.87 2010/11/29 18:57:04 markus 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:
1.62 stevesk 39:
40: #include <sys/types.h>
41: #include <sys/stat.h>
1.72 stevesk 42: #include <sys/param.h>
1.76 deraadt 43: #include <sys/uio.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.74 stevesk 51: #include <stdio.h>
1.73 stevesk 52: #include <stdlib.h>
1.71 stevesk 53: #include <string.h>
1.70 stevesk 54: #include <unistd.h>
1.15 markus 55:
1.76 deraadt 56: #include "xmalloc.h"
1.25 markus 57: #include "cipher.h"
1.1 deraadt 58: #include "buffer.h"
1.25 markus 59: #include "key.h"
1.1 deraadt 60: #include "ssh.h"
1.25 markus 61: #include "log.h"
1.27 itojun 62: #include "authfile.h"
1.44 markus 63: #include "rsa.h"
1.60 dtucker 64: #include "misc.h"
1.61 djm 65: #include "atomicio.h"
1.1 deraadt 66:
1.88 ! djm 67: #define MAX_KEY_FILE_SIZE (1024 * 1024)
! 68:
1.29 markus 69: /* Version identification string for SSH v1 identity files. */
1.26 stevesk 70: static const char authfile_id_string[] =
71: "SSH PRIVATE KEY FILE FORMAT 1.1\n";
1.1 deraadt 72:
1.10 markus 73: /*
1.86 djm 74: * Serialises the authentication (private) key to a blob, encrypting it with
75: * passphrase. The identification of the blob (lowest 64 bits of n) will
1.10 markus 76: * precede the key to provide identification of the key without needing a
77: * passphrase.
78: */
1.37 itojun 79: static int
1.86 djm 80: key_private_rsa1_to_blob(Key *key, Buffer *blob, const char *passphrase,
1.29 markus 81: const char *comment)
1.1 deraadt 82: {
1.8 markus 83: Buffer buffer, encrypted;
1.45 stevesk 84: u_char buf[100], *cp;
1.86 djm 85: int i, cipher_num;
1.20 markus 86: CipherContext ciphercontext;
87: Cipher *cipher;
1.57 avsm 88: u_int32_t rnd;
1.8 markus 89:
1.10 markus 90: /*
91: * If the passphrase is empty, use SSH_CIPHER_NONE to ease converting
92: * to another cipher; otherwise use SSH_AUTHFILE_CIPHER.
93: */
1.46 markus 94: cipher_num = (strcmp(passphrase, "") == 0) ?
95: SSH_CIPHER_NONE : SSH_AUTHFILE_CIPHER;
96: if ((cipher = cipher_by_number(cipher_num)) == NULL)
1.20 markus 97: fatal("save_private_key_rsa: bad cipher");
1.8 markus 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. */
1.57 avsm 103: rnd = arc4random();
104: buf[0] = rnd & 0xff;
105: buf[1] = (rnd >> 8) & 0xff;
1.8 markus 106: buf[2] = buf[0];
107: buf[3] = buf[1];
108: buffer_append(&buffer, buf, 4);
109:
1.10 markus 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: */
1.29 markus 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 */
1.8 markus 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. */
1.26 stevesk 128: for (i = 0; authfile_id_string[i]; i++)
129: buffer_put_char(&encrypted, authfile_id_string[i]);
1.8 markus 130: buffer_put_char(&encrypted, 0);
131:
132: /* Store cipher type. */
1.46 markus 133: buffer_put_char(&encrypted, cipher_num);
1.8 markus 134: buffer_put_int(&encrypted, 0); /* For future extension */
135:
136: /* Store public key. This will be in plain text. */
1.29 markus 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);
1.36 markus 140: buffer_put_cstring(&encrypted, comment);
1.8 markus 141:
142: /* Allocate space for the private part of the key in the buffer. */
1.42 stevesk 143: cp = buffer_append_space(&encrypted, buffer_len(&buffer));
1.8 markus 144:
1.46 markus 145: cipher_set_key_string(&ciphercontext, cipher, passphrase,
146: CIPHER_ENCRYPT);
147: cipher_crypt(&ciphercontext, cp,
1.45 stevesk 148: buffer_ptr(&buffer), buffer_len(&buffer));
1.46 markus 149: cipher_cleanup(&ciphercontext);
1.20 markus 150: memset(&ciphercontext, 0, sizeof(ciphercontext));
1.8 markus 151:
152: /* Destroy temporary data. */
153: memset(buf, 0, sizeof(buf));
154: buffer_free(&buffer);
155:
1.86 djm 156: buffer_append(blob, buffer_ptr(&encrypted), buffer_len(&encrypted));
1.8 markus 157: buffer_free(&encrypted);
1.86 djm 158:
1.8 markus 159: return 1;
1.1 deraadt 160: }
161:
1.86 djm 162: /* convert SSH v2 key in OpenSSL PEM format */
1.37 itojun 163: static int
1.86 djm 164: key_private_pem_to_blob(Key *key, Buffer *blob, const char *_passphrase,
1.29 markus 165: const char *comment)
1.15 markus 166: {
1.21 markus 167: int success = 0;
1.86 djm 168: int blen, len = strlen(_passphrase);
1.47 markus 169: u_char *passphrase = (len > 0) ? (u_char *)_passphrase : NULL;
1.77 djm 170: const EVP_CIPHER *cipher = (len > 0) ? EVP_aes_128_cbc() : NULL;
1.86 djm 171: const u_char *bptr;
172: BIO *bio;
1.15 markus 173:
174: if (len > 0 && len <= 4) {
1.31 markus 175: error("passphrase too short: have %d bytes, need > 4", len);
1.15 markus 176: return 0;
177: }
1.86 djm 178: if ((bio = BIO_new(BIO_s_mem())) == NULL) {
179: error("%s: BIO_new failed", __func__);
1.15 markus 180: return 0;
181: }
1.21 markus 182: switch (key->type) {
1.30 markus 183: case KEY_DSA:
1.86 djm 184: success = PEM_write_bio_DSAPrivateKey(bio, key->dsa,
1.30 markus 185: cipher, passphrase, len, NULL, NULL);
186: break;
1.83 djm 187: case KEY_ECDSA:
1.86 djm 188: success = PEM_write_bio_ECPrivateKey(bio, key->ecdsa,
1.83 djm 189: cipher, passphrase, len, NULL, NULL);
190: break;
1.30 markus 191: case KEY_RSA:
1.86 djm 192: success = PEM_write_bio_RSAPrivateKey(bio, key->rsa,
1.30 markus 193: cipher, passphrase, len, NULL, NULL);
194: break;
1.15 markus 195: }
1.86 djm 196: if (success) {
197: if ((blen = BIO_get_mem_data(bio, &bptr)) <= 0)
198: success = 0;
199: else
200: buffer_append(blob, bptr, blen);
201: }
202: BIO_free(bio);
1.15 markus 203: return success;
204: }
205:
1.86 djm 206: /* Save a key blob to a file */
207: static int
208: key_save_private_blob(Buffer *keybuf, const char *filename)
209: {
210: int fd;
211:
212: if ((fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0600)) < 0) {
213: error("open %s failed: %s.", filename, strerror(errno));
214: return 0;
215: }
216: if (atomicio(vwrite, fd, buffer_ptr(keybuf),
217: buffer_len(keybuf)) != buffer_len(keybuf)) {
218: error("write to key file %s failed: %s", filename,
219: strerror(errno));
220: close(fd);
221: unlink(filename);
222: return 0;
223: }
224: close(fd);
225: return 1;
226: }
227:
228: /* Serialise "key" to buffer "blob" */
229: static int
230: key_private_to_blob(Key *key, Buffer *blob, const char *passphrase,
1.15 markus 231: const char *comment)
232: {
233: switch (key->type) {
1.21 markus 234: case KEY_RSA1:
1.86 djm 235: return key_private_rsa1_to_blob(key, blob, passphrase, comment);
1.15 markus 236: case KEY_DSA:
1.83 djm 237: case KEY_ECDSA:
1.21 markus 238: case KEY_RSA:
1.86 djm 239: return key_private_pem_to_blob(key, blob, passphrase, comment);
1.15 markus 240: default:
1.86 djm 241: error("%s: cannot save key type %d", __func__, key->type);
242: return 0;
1.15 markus 243: }
1.86 djm 244: }
245:
246: int
247: key_save_private(Key *key, const char *filename, const char *passphrase,
248: const char *comment)
249: {
250: Buffer keyblob;
251: int success = 0;
252:
253: buffer_init(&keyblob);
254: if (!key_private_to_blob(key, &keyblob, passphrase, comment))
255: goto out;
256: if (!key_save_private_blob(&keyblob, filename))
257: goto out;
258: success = 1;
259: out:
260: buffer_free(&keyblob);
261: return success;
1.15 markus 262: }
263:
1.10 markus 264: /*
1.86 djm 265: * Parse the public, unencrypted portion of a RSA1 key.
1.10 markus 266: */
1.37 itojun 267: static Key *
1.86 djm 268: key_parse_public_rsa1(Buffer *blob, char **commentp)
1.1 deraadt 269: {
1.29 markus 270: Key *pub;
1.86 djm 271:
272: /* Check that it is at least big enough to contain the ID string. */
273: if (buffer_len(blob) < sizeof(authfile_id_string)) {
274: debug3("Truncated RSA1 identifier");
275: return NULL;
276: }
277:
278: /*
279: * Make sure it begins with the id string. Consume the id string
280: * from the buffer.
281: */
282: if (memcmp(buffer_ptr(blob), authfile_id_string,
283: sizeof(authfile_id_string)) != 0) {
284: debug3("Incorrect RSA1 identifier");
285: return NULL;
286: }
287: buffer_consume(blob, sizeof(authfile_id_string));
288:
289: /* Skip cipher type and reserved data. */
290: (void) buffer_get_char(blob); /* cipher type */
291: (void) buffer_get_int(blob); /* reserved */
292:
293: /* Read the public key from the buffer. */
294: (void) buffer_get_int(blob);
295: pub = key_new(KEY_RSA1);
296: buffer_get_bignum(blob, pub->rsa->n);
297: buffer_get_bignum(blob, pub->rsa->e);
298: if (commentp)
299: *commentp = buffer_get_string(blob, NULL);
300: /* The encrypted private part is not parsed by this function. */
301: buffer_clear(blob);
302:
303: return pub;
304: }
305:
1.88 ! djm 306: /* Load a key from a fd into a buffer */
! 307: int
1.86 djm 308: key_load_file(int fd, const char *filename, Buffer *blob)
309: {
1.88 ! djm 310: u_char buf[1024];
1.86 djm 311: size_t len;
1.51 fgsch 312: struct stat st;
1.8 markus 313:
1.51 fgsch 314: if (fstat(fd, &st) < 0) {
1.86 djm 315: error("%s: fstat of key file %.200s%sfailed: %.100s", __func__,
316: filename == NULL ? "" : filename,
317: filename == NULL ? "" : " ",
318: strerror(errno));
319: return 0;
1.51 fgsch 320: }
1.88 ! djm 321: if ((st.st_mode & (S_IFSOCK|S_IFCHR|S_IFIFO)) == 0 &&
! 322: st.st_size > MAX_KEY_FILE_SIZE) {
! 323: toobig:
1.86 djm 324: error("%s: key file %.200s%stoo large", __func__,
325: filename == NULL ? "" : filename,
326: filename == NULL ? "" : " ");
327: return 0;
1.58 djm 328: }
1.86 djm 329: buffer_init(blob);
1.88 ! djm 330: for (;;) {
! 331: if ((len = atomicio(read, fd, buf, sizeof(buf))) == 0) {
! 332: if (errno == EPIPE)
! 333: break;
! 334: debug("%s: read from key file %.200s%sfailed: %.100s",
! 335: __func__, filename == NULL ? "" : filename,
! 336: filename == NULL ? "" : " ", strerror(errno));
! 337: buffer_clear(blob);
! 338: bzero(buf, sizeof(buf));
! 339: return 0;
! 340: }
! 341: buffer_append(blob, buf, len);
! 342: if (buffer_len(blob) > MAX_KEY_FILE_SIZE) {
! 343: buffer_clear(blob);
! 344: bzero(buf, sizeof(buf));
! 345: goto toobig;
! 346: }
! 347: }
! 348: bzero(buf, sizeof(buf));
! 349: if ((st.st_mode & (S_IFSOCK|S_IFCHR|S_IFIFO)) == 0 &&
! 350: st.st_size != buffer_len(blob)) {
! 351: debug("%s: key file %.200s%schanged size while reading",
! 352: __func__, filename == NULL ? "" : filename,
! 353: filename == NULL ? "" : " ");
1.86 djm 354: buffer_clear(blob);
355: return 0;
1.8 markus 356: }
1.88 ! djm 357:
1.86 djm 358: return 1;
359: }
1.8 markus 360:
1.86 djm 361: /*
362: * Loads the public part of the ssh v1 key file. Returns NULL if an error was
363: * encountered (the file does not exist or is not readable), and the key
364: * otherwise.
365: */
366: static Key *
367: key_load_public_rsa1(int fd, const char *filename, char **commentp)
368: {
369: Buffer buffer;
370: Key *pub;
371:
372: buffer_init(&buffer);
373: if (!key_load_file(fd, filename, &buffer)) {
1.8 markus 374: buffer_free(&buffer);
1.29 markus 375: return NULL;
1.8 markus 376: }
377:
1.86 djm 378: pub = key_parse_public_rsa1(&buffer, commentp);
379: if (pub == NULL)
380: debug3("Could not load \"%s\" as a RSA1 public key", filename);
1.1 deraadt 381: buffer_free(&buffer);
1.29 markus 382: return pub;
1.1 deraadt 383: }
384:
1.29 markus 385: /* load public key from private-key file, works only for SSH v1 */
386: Key *
387: key_load_public_type(int type, const char *filename, char **commentp)
1.15 markus 388: {
1.29 markus 389: Key *pub;
390: int fd;
391:
392: if (type == KEY_RSA1) {
393: fd = open(filename, O_RDONLY);
394: if (fd < 0)
395: return NULL;
396: pub = key_load_public_rsa1(fd, filename, commentp);
397: close(fd);
398: return pub;
1.15 markus 399: }
1.29 markus 400: return NULL;
1.15 markus 401: }
402:
1.37 itojun 403: static Key *
1.86 djm 404: key_parse_private_rsa1(Buffer *blob, const char *passphrase, char **commentp)
1.1 deraadt 405: {
1.61 djm 406: int check1, check2, cipher_type;
1.86 djm 407: Buffer decrypted;
1.45 stevesk 408: u_char *cp;
1.20 markus 409: CipherContext ciphercontext;
410: Cipher *cipher;
1.29 markus 411: Key *prv = NULL;
1.8 markus 412:
1.86 djm 413: /* Check that it is at least big enough to contain the ID string. */
414: if (buffer_len(blob) < sizeof(authfile_id_string)) {
415: debug3("Truncated RSA1 identifier");
1.51 fgsch 416: return NULL;
417: }
1.8 markus 418:
1.10 markus 419: /*
420: * Make sure it begins with the id string. Consume the id string
421: * from the buffer.
422: */
1.86 djm 423: if (memcmp(buffer_ptr(blob), authfile_id_string,
424: sizeof(authfile_id_string)) != 0) {
425: debug3("Incorrect RSA1 identifier");
426: return NULL;
427: }
428: buffer_consume(blob, sizeof(authfile_id_string));
1.28 deraadt 429:
1.8 markus 430: /* Read cipher type. */
1.86 djm 431: cipher_type = buffer_get_char(blob);
432: (void) buffer_get_int(blob); /* Reserved data. */
1.8 markus 433:
434: /* Read the public key from the buffer. */
1.86 djm 435: (void) buffer_get_int(blob);
1.29 markus 436: prv = key_new_private(KEY_RSA1);
437:
1.86 djm 438: buffer_get_bignum(blob, prv->rsa->n);
439: buffer_get_bignum(blob, prv->rsa->e);
1.29 markus 440: if (commentp)
1.86 djm 441: *commentp = buffer_get_string(blob, NULL);
1.8 markus 442: else
1.86 djm 443: (void)buffer_get_string_ptr(blob, NULL);
1.8 markus 444:
445: /* Check that it is a supported cipher. */
1.20 markus 446: cipher = cipher_by_number(cipher_type);
447: if (cipher == NULL) {
1.86 djm 448: debug("Unsupported RSA1 cipher %d", cipher_type);
1.8 markus 449: goto fail;
450: }
451: /* Initialize space for decrypted data. */
452: buffer_init(&decrypted);
1.86 djm 453: cp = buffer_append_space(&decrypted, buffer_len(blob));
1.8 markus 454:
455: /* Rest of the buffer is encrypted. Decrypt it using the passphrase. */
1.46 markus 456: cipher_set_key_string(&ciphercontext, cipher, passphrase,
457: CIPHER_DECRYPT);
458: cipher_crypt(&ciphercontext, cp,
1.86 djm 459: buffer_ptr(blob), buffer_len(blob));
1.46 markus 460: cipher_cleanup(&ciphercontext);
1.20 markus 461: memset(&ciphercontext, 0, sizeof(ciphercontext));
1.86 djm 462: buffer_clear(blob);
1.1 deraadt 463:
1.8 markus 464: check1 = buffer_get_char(&decrypted);
465: check2 = buffer_get_char(&decrypted);
466: if (check1 != buffer_get_char(&decrypted) ||
467: check2 != buffer_get_char(&decrypted)) {
468: if (strcmp(passphrase, "") != 0)
1.86 djm 469: debug("Bad passphrase supplied for RSA1 key");
1.8 markus 470: /* Bad passphrase. */
471: buffer_free(&decrypted);
1.29 markus 472: goto fail;
1.8 markus 473: }
474: /* Read the rest of the private key. */
1.29 markus 475: buffer_get_bignum(&decrypted, prv->rsa->d);
476: buffer_get_bignum(&decrypted, prv->rsa->iqmp); /* u */
477: /* in SSL and SSH v1 p and q are exchanged */
478: buffer_get_bignum(&decrypted, prv->rsa->q); /* p */
479: buffer_get_bignum(&decrypted, prv->rsa->p); /* q */
1.8 markus 480:
1.29 markus 481: /* calculate p-1 and q-1 */
1.43 markus 482: rsa_generate_additional_parameters(prv->rsa);
1.8 markus 483:
484: buffer_free(&decrypted);
1.52 markus 485:
486: /* enable blinding */
487: if (RSA_blinding_on(prv->rsa, NULL) != 1) {
1.86 djm 488: error("%s: RSA_blinding_on failed", __func__);
1.52 markus 489: goto fail;
490: }
1.29 markus 491: return prv;
492:
493: fail:
494: if (commentp)
495: xfree(*commentp);
496: key_free(prv);
497: return NULL;
1.15 markus 498: }
499:
1.86 djm 500: static Key *
501: key_parse_private_pem(Buffer *blob, int type, const char *passphrase,
1.29 markus 502: char **commentp)
1.15 markus 503: {
1.21 markus 504: EVP_PKEY *pk = NULL;
1.29 markus 505: Key *prv = NULL;
1.21 markus 506: char *name = "<no key>";
1.86 djm 507: BIO *bio;
1.15 markus 508:
1.86 djm 509: if ((bio = BIO_new_mem_buf(buffer_ptr(blob),
510: buffer_len(blob))) == NULL) {
511: error("%s: BIO_new_mem_buf failed", __func__);
1.29 markus 512: return NULL;
1.15 markus 513: }
1.86 djm 514:
515: pk = PEM_read_bio_PrivateKey(bio, NULL, NULL, (char *)passphrase);
516: BIO_free(bio);
1.21 markus 517: if (pk == NULL) {
1.86 djm 518: debug("%s: PEM_read_PrivateKey failed", __func__);
1.21 markus 519: (void)ERR_get_error();
1.29 markus 520: } else if (pk->type == EVP_PKEY_RSA &&
1.41 deraadt 521: (type == KEY_UNSPEC||type==KEY_RSA)) {
1.29 markus 522: prv = key_new(KEY_UNSPEC);
523: prv->rsa = EVP_PKEY_get1_RSA(pk);
524: prv->type = KEY_RSA;
525: name = "rsa w/o comment";
1.21 markus 526: #ifdef DEBUG_PK
1.29 markus 527: RSA_print_fp(stderr, prv->rsa, 8);
1.21 markus 528: #endif
1.52 markus 529: if (RSA_blinding_on(prv->rsa, NULL) != 1) {
1.86 djm 530: error("%s: RSA_blinding_on failed", __func__);
1.52 markus 531: key_free(prv);
532: prv = NULL;
533: }
1.29 markus 534: } else if (pk->type == EVP_PKEY_DSA &&
1.41 deraadt 535: (type == KEY_UNSPEC||type==KEY_DSA)) {
1.29 markus 536: prv = key_new(KEY_UNSPEC);
537: prv->dsa = EVP_PKEY_get1_DSA(pk);
538: prv->type = KEY_DSA;
539: name = "dsa w/o comment";
1.21 markus 540: #ifdef DEBUG_PK
1.29 markus 541: DSA_print_fp(stderr, prv->dsa, 8);
1.21 markus 542: #endif
1.83 djm 543: } else if (pk->type == EVP_PKEY_EC &&
544: (type == KEY_UNSPEC||type==KEY_ECDSA)) {
545: prv = key_new(KEY_UNSPEC);
546: prv->ecdsa = EVP_PKEY_get1_EC_KEY(pk);
547: prv->type = KEY_ECDSA;
1.85 djm 548: if ((prv->ecdsa_nid = key_ecdsa_key_to_nid(prv->ecdsa)) == -1 ||
549: key_curve_nid_to_name(prv->ecdsa_nid) == NULL ||
550: key_ec_validate_public(EC_KEY_get0_group(prv->ecdsa),
1.83 djm 551: EC_KEY_get0_public_key(prv->ecdsa)) != 0 ||
552: key_ec_validate_private(prv->ecdsa) != 0) {
553: error("%s: bad ECDSA key", __func__);
554: key_free(prv);
555: prv = NULL;
556: }
1.84 djm 557: name = "ecdsa w/o comment";
1.83 djm 558: #ifdef DEBUG_PK
1.85 djm 559: if (prv != NULL && prv->ecdsa != NULL)
1.83 djm 560: key_dump_ec_key(prv->ecdsa);
561: #endif
1.15 markus 562: } else {
1.86 djm 563: error("%s: PEM_read_PrivateKey: mismatch or "
564: "unknown EVP_PKEY save_type %d", __func__, pk->save_type);
1.15 markus 565: }
1.21 markus 566: if (pk != NULL)
567: EVP_PKEY_free(pk);
1.29 markus 568: if (prv != NULL && commentp)
569: *commentp = xstrdup(name);
570: debug("read PEM private key done: type %s",
571: prv ? key_type(prv) : "<unknown>");
572: return prv;
1.15 markus 573: }
574:
1.86 djm 575: Key *
576: key_load_private_pem(int fd, int type, const char *passphrase,
577: char **commentp)
578: {
579: Buffer buffer;
580: Key *prv;
581:
582: buffer_init(&buffer);
583: if (!key_load_file(fd, NULL, &buffer)) {
584: buffer_free(&buffer);
585: return NULL;
586: }
587: prv = key_parse_private_pem(&buffer, type, passphrase, commentp);
588: buffer_free(&buffer);
589: return prv;
590: }
591:
1.63 dtucker 592: int
1.29 markus 593: key_perm_ok(int fd, const char *filename)
1.15 markus 594: {
595: struct stat st;
596:
1.38 markus 597: if (fstat(fd, &st) < 0)
598: return 0;
599: /*
600: * if a key owned by the user is accessed, then we check the
601: * permissions of the file. if the key owned by a different user,
602: * then we don't care.
603: */
604: if ((st.st_uid == getuid()) && (st.st_mode & 077) != 0) {
1.15 markus 605: error("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
606: error("@ WARNING: UNPROTECTED PRIVATE KEY FILE! @");
607: error("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
1.38 markus 608: error("Permissions 0%3.3o for '%s' are too open.",
1.54 djm 609: (u_int)st.st_mode & 0777, filename);
1.15 markus 610: error("It is recommended that your private key files are NOT accessible by others.");
1.29 markus 611: error("This private key will be ignored.");
1.15 markus 612: return 0;
613: }
1.29 markus 614: return 1;
615: }
616:
1.86 djm 617: static Key *
618: key_parse_private_type(Buffer *blob, int type, const char *passphrase,
619: char **commentp)
620: {
621: switch (type) {
622: case KEY_RSA1:
623: return key_parse_private_rsa1(blob, passphrase, commentp);
624: case KEY_DSA:
625: case KEY_ECDSA:
626: case KEY_RSA:
627: case KEY_UNSPEC:
628: return key_parse_private_pem(blob, type, passphrase, commentp);
629: default:
630: break;
631: }
632: return NULL;
633: }
634:
1.29 markus 635: Key *
636: key_load_private_type(int type, const char *filename, const char *passphrase,
1.67 dtucker 637: char **commentp, int *perm_ok)
1.29 markus 638: {
639: int fd;
1.86 djm 640: Key *ret;
641: Buffer buffer;
1.29 markus 642:
643: fd = open(filename, O_RDONLY);
1.78 dtucker 644: if (fd < 0) {
645: debug("could not open key file '%s': %s", filename,
646: strerror(errno));
647: if (perm_ok != NULL)
648: *perm_ok = 0;
1.79 dtucker 649: return NULL;
1.78 dtucker 650: }
1.29 markus 651: if (!key_perm_ok(fd, filename)) {
1.67 dtucker 652: if (perm_ok != NULL)
653: *perm_ok = 0;
1.31 markus 654: error("bad permissions: ignore key: %s", filename);
1.29 markus 655: close(fd);
656: return NULL;
657: }
1.67 dtucker 658: if (perm_ok != NULL)
659: *perm_ok = 1;
1.86 djm 660:
661: buffer_init(&buffer);
662: if (!key_load_file(fd, filename, &buffer)) {
663: buffer_free(&buffer);
1.28 deraadt 664: close(fd);
1.86 djm 665: return NULL;
1.15 markus 666: }
1.86 djm 667: close(fd);
668: ret = key_parse_private_type(&buffer, type, passphrase, commentp);
669: buffer_free(&buffer);
670: return ret;
1.29 markus 671: }
672:
673: Key *
1.88 ! djm 674: key_parse_private(Buffer *buffer, const char *filename,
! 675: const char *passphrase, char **commentp)
! 676: {
! 677: Key *pub, *prv;
! 678: Buffer pubcopy;
! 679:
! 680: buffer_init(&pubcopy);
! 681: buffer_append(&pubcopy, buffer_ptr(buffer), buffer_len(buffer));
! 682: /* it's a SSH v1 key if the public key part is readable */
! 683: pub = key_parse_public_rsa1(&pubcopy, commentp);
! 684: buffer_free(&pubcopy);
! 685: if (pub == NULL) {
! 686: prv = key_parse_private_type(buffer, KEY_UNSPEC,
! 687: passphrase, NULL);
! 688: /* use the filename as a comment for PEM */
! 689: if (commentp && prv)
! 690: *commentp = xstrdup(filename);
! 691: } else {
! 692: key_free(pub);
! 693: /* key_parse_public_rsa1() has already loaded the comment */
! 694: prv = key_parse_private_type(buffer, KEY_RSA1, passphrase,
! 695: NULL);
! 696: }
! 697: return prv;
! 698: }
! 699:
! 700: Key *
1.29 markus 701: key_load_private(const char *filename, const char *passphrase,
702: char **commentp)
703: {
1.88 ! djm 704: Key *prv;
! 705: Buffer buffer;
1.29 markus 706: int fd;
707:
708: fd = open(filename, O_RDONLY);
1.78 dtucker 709: if (fd < 0) {
710: debug("could not open key file '%s': %s", filename,
711: strerror(errno));
1.29 markus 712: return NULL;
1.78 dtucker 713: }
1.29 markus 714: if (!key_perm_ok(fd, filename)) {
1.31 markus 715: error("bad permissions: ignore key: %s", filename);
1.29 markus 716: close(fd);
717: return NULL;
718: }
1.86 djm 719:
720: buffer_init(&buffer);
721: if (!key_load_file(fd, filename, &buffer)) {
722: buffer_free(&buffer);
723: close(fd);
724: return NULL;
725: }
726: close(fd);
727:
1.88 ! djm 728: prv = key_parse_private(&buffer, filename, passphrase, commentp);
1.86 djm 729: buffer_free(&buffer);
1.34 markus 730: return prv;
1.18 markus 731: }
732:
1.37 itojun 733: static int
1.29 markus 734: key_try_load_public(Key *k, const char *filename, char **commentp)
1.18 markus 735: {
736: FILE *f;
1.59 dtucker 737: char line[SSH_MAX_PUBKEY_BYTES];
1.18 markus 738: char *cp;
1.60 dtucker 739: u_long linenum = 0;
1.18 markus 740:
741: f = fopen(filename, "r");
742: if (f != NULL) {
1.59 dtucker 743: while (read_keyfile_line(f, filename, line, sizeof(line),
744: &linenum) != -1) {
1.18 markus 745: cp = line;
1.40 deraadt 746: switch (*cp) {
1.18 markus 747: case '#':
748: case '\n':
749: case '\0':
750: continue;
751: }
752: /* Skip leading whitespace. */
753: for (; *cp && (*cp == ' ' || *cp == '\t'); cp++)
754: ;
755: if (*cp) {
1.21 markus 756: if (key_read(k, &cp) == 1) {
1.18 markus 757: if (commentp)
758: *commentp=xstrdup(filename);
759: fclose(f);
760: return 1;
761: }
762: }
763: }
764: fclose(f);
765: }
766: return 0;
767: }
768:
1.29 markus 769: /* load public key from ssh v1 private or any pubkey file */
770: Key *
771: key_load_public(const char *filename, char **commentp)
1.18 markus 772: {
1.29 markus 773: Key *pub;
774: char file[MAXPATHLEN];
1.18 markus 775:
1.53 markus 776: /* try rsa1 private key */
1.29 markus 777: pub = key_load_public_type(KEY_RSA1, filename, commentp);
778: if (pub != NULL)
779: return pub;
1.53 markus 780:
781: /* try rsa1 public key */
782: pub = key_new(KEY_RSA1);
783: if (key_try_load_public(pub, filename, commentp) == 1)
784: return pub;
785: key_free(pub);
786:
787: /* try ssh2 public key */
1.29 markus 788: pub = key_new(KEY_UNSPEC);
789: if (key_try_load_public(pub, filename, commentp) == 1)
790: return pub;
791: if ((strlcpy(file, filename, sizeof file) < sizeof(file)) &&
792: (strlcat(file, ".pub", sizeof file) < sizeof(file)) &&
793: (key_try_load_public(pub, file, commentp) == 1))
794: return pub;
1.81 djm 795: key_free(pub);
796: return NULL;
797: }
798:
799: /* Load the certificate associated with the named private key */
800: Key *
801: key_load_cert(const char *filename)
802: {
803: Key *pub;
1.82 djm 804: char *file;
1.81 djm 805:
806: pub = key_new(KEY_UNSPEC);
1.82 djm 807: xasprintf(&file, "%s-cert.pub", filename);
808: if (key_try_load_public(pub, file, NULL) == 1) {
809: xfree(file);
1.81 djm 810: return pub;
1.82 djm 811: }
812: xfree(file);
1.81 djm 813: key_free(pub);
814: return NULL;
815: }
816:
817: /* Load private key and certificate */
818: Key *
819: key_load_private_cert(int type, const char *filename, const char *passphrase,
820: int *perm_ok)
821: {
822: Key *key, *pub;
823:
824: switch (type) {
825: case KEY_RSA:
826: case KEY_DSA:
1.83 djm 827: case KEY_ECDSA:
1.81 djm 828: break;
829: default:
830: error("%s: unsupported key type", __func__);
831: return NULL;
832: }
833:
834: if ((key = key_load_private_type(type, filename,
835: passphrase, NULL, perm_ok)) == NULL)
836: return NULL;
837:
838: if ((pub = key_load_cert(filename)) == NULL) {
839: key_free(key);
840: return NULL;
841: }
842:
843: /* Make sure the private key matches the certificate */
844: if (key_equal_public(key, pub) == 0) {
845: error("%s: certificate does not match private key %s",
846: __func__, filename);
847: } else if (key_to_certified(key, key_cert_is_legacy(pub)) != 0) {
848: error("%s: key_to_certified failed", __func__);
849: } else {
850: key_cert_copy(pub, key);
851: key_free(pub);
852: return key;
853: }
854:
855: key_free(key);
1.29 markus 856: key_free(pub);
857: return NULL;
1.1 deraadt 858: }
1.80 djm 859:
860: /*
861: * Returns 1 if the specified "key" is listed in the file "filename",
862: * 0 if the key is not listed or -1 on error.
863: * If strict_type is set then the key type must match exactly,
864: * otherwise a comparison that ignores certficiate data is performed.
865: */
866: int
867: key_in_file(Key *key, const char *filename, int strict_type)
868: {
869: FILE *f;
870: char line[SSH_MAX_PUBKEY_BYTES];
871: char *cp;
872: u_long linenum = 0;
873: int ret = 0;
874: Key *pub;
875: int (*key_compare)(const Key *, const Key *) = strict_type ?
876: key_equal : key_equal_public;
877:
878: if ((f = fopen(filename, "r")) == NULL) {
879: if (errno == ENOENT) {
880: debug("%s: keyfile \"%s\" missing", __func__, filename);
881: return 0;
882: } else {
883: error("%s: could not open keyfile \"%s\": %s", __func__,
884: filename, strerror(errno));
885: return -1;
886: }
887: }
888:
889: while (read_keyfile_line(f, filename, line, sizeof(line),
890: &linenum) != -1) {
891: cp = line;
892:
893: /* Skip leading whitespace. */
894: for (; *cp && (*cp == ' ' || *cp == '\t'); cp++)
895: ;
896:
897: /* Skip comments and empty lines */
898: switch (*cp) {
899: case '#':
900: case '\n':
901: case '\0':
902: continue;
903: }
904:
905: pub = key_new(KEY_UNSPEC);
906: if (key_read(pub, &cp) != 1) {
907: key_free(pub);
908: continue;
909: }
910: if (key_compare(key, pub)) {
911: ret = 1;
912: key_free(pub);
913: break;
914: }
915: key_free(pub);
916: }
917: fclose(f);
918: return ret;
919: }
920: