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