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