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