Annotation of src/usr.bin/ssh/authfile.c, Revision 1.91
1.91 ! djm 1: /* $OpenBSD: authfile.c,v 1.90 2011/05/13 00:05:36 djm 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:
1.90 djm 630: error("%s: cannot parse key type %d", __func__, type);
1.86 djm 631: break;
632: }
633: return NULL;
634: }
635:
1.29 markus 636: Key *
637: key_load_private_type(int type, const char *filename, const char *passphrase,
1.67 dtucker 638: char **commentp, int *perm_ok)
1.29 markus 639: {
640: int fd;
1.86 djm 641: Key *ret;
642: Buffer buffer;
1.29 markus 643:
644: fd = open(filename, O_RDONLY);
1.78 dtucker 645: if (fd < 0) {
646: debug("could not open key file '%s': %s", filename,
647: strerror(errno));
648: if (perm_ok != NULL)
649: *perm_ok = 0;
1.79 dtucker 650: return NULL;
1.78 dtucker 651: }
1.29 markus 652: if (!key_perm_ok(fd, filename)) {
1.67 dtucker 653: if (perm_ok != NULL)
654: *perm_ok = 0;
1.31 markus 655: error("bad permissions: ignore key: %s", filename);
1.29 markus 656: close(fd);
657: return NULL;
658: }
1.67 dtucker 659: if (perm_ok != NULL)
660: *perm_ok = 1;
1.86 djm 661:
662: buffer_init(&buffer);
663: if (!key_load_file(fd, filename, &buffer)) {
664: buffer_free(&buffer);
1.28 deraadt 665: close(fd);
1.86 djm 666: return NULL;
1.15 markus 667: }
1.86 djm 668: close(fd);
669: ret = key_parse_private_type(&buffer, type, passphrase, commentp);
670: buffer_free(&buffer);
671: return ret;
1.29 markus 672: }
673:
674: Key *
1.88 djm 675: key_parse_private(Buffer *buffer, const char *filename,
676: const char *passphrase, char **commentp)
677: {
678: Key *pub, *prv;
679: Buffer pubcopy;
680:
681: buffer_init(&pubcopy);
682: buffer_append(&pubcopy, buffer_ptr(buffer), buffer_len(buffer));
683: /* it's a SSH v1 key if the public key part is readable */
684: pub = key_parse_public_rsa1(&pubcopy, commentp);
685: buffer_free(&pubcopy);
686: if (pub == NULL) {
687: prv = key_parse_private_type(buffer, KEY_UNSPEC,
688: passphrase, NULL);
689: /* use the filename as a comment for PEM */
690: if (commentp && prv)
691: *commentp = xstrdup(filename);
692: } else {
693: key_free(pub);
694: /* key_parse_public_rsa1() has already loaded the comment */
695: prv = key_parse_private_type(buffer, KEY_RSA1, passphrase,
696: NULL);
697: }
698: return prv;
699: }
700:
701: Key *
1.29 markus 702: key_load_private(const char *filename, const char *passphrase,
703: char **commentp)
704: {
1.88 djm 705: Key *prv;
706: Buffer buffer;
1.29 markus 707: int fd;
708:
709: fd = open(filename, O_RDONLY);
1.78 dtucker 710: if (fd < 0) {
711: debug("could not open key file '%s': %s", filename,
712: strerror(errno));
1.29 markus 713: return NULL;
1.78 dtucker 714: }
1.29 markus 715: if (!key_perm_ok(fd, filename)) {
1.31 markus 716: error("bad permissions: ignore key: %s", filename);
1.29 markus 717: close(fd);
718: return NULL;
719: }
1.86 djm 720:
721: buffer_init(&buffer);
722: if (!key_load_file(fd, filename, &buffer)) {
723: buffer_free(&buffer);
724: close(fd);
725: return NULL;
726: }
727: close(fd);
728:
1.88 djm 729: prv = key_parse_private(&buffer, filename, passphrase, commentp);
1.86 djm 730: buffer_free(&buffer);
1.34 markus 731: return prv;
1.18 markus 732: }
733:
1.37 itojun 734: static int
1.29 markus 735: key_try_load_public(Key *k, const char *filename, char **commentp)
1.18 markus 736: {
737: FILE *f;
1.59 dtucker 738: char line[SSH_MAX_PUBKEY_BYTES];
1.18 markus 739: char *cp;
1.60 dtucker 740: u_long linenum = 0;
1.18 markus 741:
742: f = fopen(filename, "r");
743: if (f != NULL) {
1.59 dtucker 744: while (read_keyfile_line(f, filename, line, sizeof(line),
745: &linenum) != -1) {
1.18 markus 746: cp = line;
1.40 deraadt 747: switch (*cp) {
1.18 markus 748: case '#':
749: case '\n':
750: case '\0':
751: continue;
752: }
1.89 djm 753: /* Abort loading if this looks like a private key */
754: if (strncmp(cp, "-----BEGIN", 10) == 0)
755: break;
1.18 markus 756: /* Skip leading whitespace. */
757: for (; *cp && (*cp == ' ' || *cp == '\t'); cp++)
758: ;
759: if (*cp) {
1.21 markus 760: if (key_read(k, &cp) == 1) {
1.91 ! djm 761: cp[strcspn(cp, "\r\n")] = '\0';
! 762: if (commentp) {
! 763: *commentp = xstrdup(*cp ?
! 764: cp : filename);
! 765: }
1.18 markus 766: fclose(f);
767: return 1;
768: }
769: }
770: }
771: fclose(f);
772: }
773: return 0;
774: }
775:
1.29 markus 776: /* load public key from ssh v1 private or any pubkey file */
777: Key *
778: key_load_public(const char *filename, char **commentp)
1.18 markus 779: {
1.29 markus 780: Key *pub;
781: char file[MAXPATHLEN];
1.18 markus 782:
1.53 markus 783: /* try rsa1 private key */
1.29 markus 784: pub = key_load_public_type(KEY_RSA1, filename, commentp);
785: if (pub != NULL)
786: return pub;
1.53 markus 787:
788: /* try rsa1 public key */
789: pub = key_new(KEY_RSA1);
790: if (key_try_load_public(pub, filename, commentp) == 1)
791: return pub;
792: key_free(pub);
793:
794: /* try ssh2 public key */
1.29 markus 795: pub = key_new(KEY_UNSPEC);
796: if (key_try_load_public(pub, filename, commentp) == 1)
797: return pub;
798: if ((strlcpy(file, filename, sizeof file) < sizeof(file)) &&
799: (strlcat(file, ".pub", sizeof file) < sizeof(file)) &&
800: (key_try_load_public(pub, file, commentp) == 1))
801: return pub;
1.81 djm 802: key_free(pub);
803: return NULL;
804: }
805:
806: /* Load the certificate associated with the named private key */
807: Key *
808: key_load_cert(const char *filename)
809: {
810: Key *pub;
1.82 djm 811: char *file;
1.81 djm 812:
813: pub = key_new(KEY_UNSPEC);
1.82 djm 814: xasprintf(&file, "%s-cert.pub", filename);
815: if (key_try_load_public(pub, file, NULL) == 1) {
816: xfree(file);
1.81 djm 817: return pub;
1.82 djm 818: }
819: xfree(file);
1.81 djm 820: key_free(pub);
821: return NULL;
822: }
823:
824: /* Load private key and certificate */
825: Key *
826: key_load_private_cert(int type, const char *filename, const char *passphrase,
827: int *perm_ok)
828: {
829: Key *key, *pub;
830:
831: switch (type) {
832: case KEY_RSA:
833: case KEY_DSA:
1.83 djm 834: case KEY_ECDSA:
1.81 djm 835: break;
836: default:
837: error("%s: unsupported key type", __func__);
838: return NULL;
839: }
840:
841: if ((key = key_load_private_type(type, filename,
842: passphrase, NULL, perm_ok)) == NULL)
843: return NULL;
844:
845: if ((pub = key_load_cert(filename)) == NULL) {
846: key_free(key);
847: return NULL;
848: }
849:
850: /* Make sure the private key matches the certificate */
851: if (key_equal_public(key, pub) == 0) {
852: error("%s: certificate does not match private key %s",
853: __func__, filename);
854: } else if (key_to_certified(key, key_cert_is_legacy(pub)) != 0) {
855: error("%s: key_to_certified failed", __func__);
856: } else {
857: key_cert_copy(pub, key);
858: key_free(pub);
859: return key;
860: }
861:
862: key_free(key);
1.29 markus 863: key_free(pub);
864: return NULL;
1.1 deraadt 865: }
1.80 djm 866:
867: /*
868: * Returns 1 if the specified "key" is listed in the file "filename",
869: * 0 if the key is not listed or -1 on error.
870: * If strict_type is set then the key type must match exactly,
871: * otherwise a comparison that ignores certficiate data is performed.
872: */
873: int
874: key_in_file(Key *key, const char *filename, int strict_type)
875: {
876: FILE *f;
877: char line[SSH_MAX_PUBKEY_BYTES];
878: char *cp;
879: u_long linenum = 0;
880: int ret = 0;
881: Key *pub;
882: int (*key_compare)(const Key *, const Key *) = strict_type ?
883: key_equal : key_equal_public;
884:
885: if ((f = fopen(filename, "r")) == NULL) {
886: if (errno == ENOENT) {
887: debug("%s: keyfile \"%s\" missing", __func__, filename);
888: return 0;
889: } else {
890: error("%s: could not open keyfile \"%s\": %s", __func__,
891: filename, strerror(errno));
892: return -1;
893: }
894: }
895:
896: while (read_keyfile_line(f, filename, line, sizeof(line),
897: &linenum) != -1) {
898: cp = line;
899:
900: /* Skip leading whitespace. */
901: for (; *cp && (*cp == ' ' || *cp == '\t'); cp++)
902: ;
903:
904: /* Skip comments and empty lines */
905: switch (*cp) {
906: case '#':
907: case '\n':
908: case '\0':
909: continue;
910: }
911:
912: pub = key_new(KEY_UNSPEC);
913: if (key_read(pub, &cp) != 1) {
914: key_free(pub);
915: continue;
916: }
917: if (key_compare(key, pub)) {
918: ret = 1;
919: key_free(pub);
920: break;
921: }
922: key_free(pub);
923: }
924: fclose(f);
925: return ret;
926: }
927: