Darren Tucker | 69c01b1 | 2010-01-12 19:42:29 +1100 | [diff] [blame] | 1 | /* $OpenBSD: authfile.c,v 1.79 2010/01/12 00:16:47 dtucker 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; |
| 216 | case KEY_RSA: |
| 217 | success = PEM_write_RSAPrivateKey(fp, key->rsa, |
| 218 | cipher, passphrase, len, NULL, NULL); |
| 219 | break; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 220 | } |
| 221 | fclose(fp); |
| 222 | return success; |
| 223 | } |
| 224 | |
| 225 | int |
Ben Lindstrom | d0fca42 | 2001-03-26 13:44:06 +0000 | [diff] [blame] | 226 | key_save_private(Key *key, const char *filename, const char *passphrase, |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 227 | const char *comment) |
| 228 | { |
| 229 | switch (key->type) { |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 230 | case KEY_RSA1: |
Ben Lindstrom | d0fca42 | 2001-03-26 13:44:06 +0000 | [diff] [blame] | 231 | return key_save_private_rsa1(key, filename, passphrase, |
| 232 | comment); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 233 | case KEY_DSA: |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 234 | case KEY_RSA: |
Ben Lindstrom | d0fca42 | 2001-03-26 13:44:06 +0000 | [diff] [blame] | 235 | return key_save_private_pem(key, filename, passphrase, |
| 236 | comment); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 237 | default: |
| 238 | break; |
| 239 | } |
Ben Lindstrom | 15f3386 | 2001-04-16 02:00:02 +0000 | [diff] [blame] | 240 | error("key_save_private: cannot save key type %d", key->type); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 241 | return 0; |
| 242 | } |
| 243 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 244 | /* |
Ben Lindstrom | d0fca42 | 2001-03-26 13:44:06 +0000 | [diff] [blame] | 245 | * Loads the public part of the ssh v1 key file. Returns NULL if an error was |
| 246 | * 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] | 247 | * otherwise. |
| 248 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 249 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 250 | static Key * |
Ben Lindstrom | d0fca42 | 2001-03-26 13:44:06 +0000 | [diff] [blame] | 251 | key_load_public_rsa1(int fd, const char *filename, char **commentp) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 252 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 253 | Buffer buffer; |
Ben Lindstrom | d0fca42 | 2001-03-26 13:44:06 +0000 | [diff] [blame] | 254 | Key *pub; |
Ben Lindstrom | 44adb8f | 2002-12-23 02:00:23 +0000 | [diff] [blame] | 255 | struct stat st; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 256 | char *cp; |
Damien Miller | eccb9de | 2005-06-17 12:59:34 +1000 | [diff] [blame] | 257 | u_int i; |
Darren Tucker | 1f8311c | 2004-05-13 16:39:33 +1000 | [diff] [blame] | 258 | size_t len; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 259 | |
Ben Lindstrom | 44adb8f | 2002-12-23 02:00:23 +0000 | [diff] [blame] | 260 | if (fstat(fd, &st) < 0) { |
| 261 | error("fstat for key file %.200s failed: %.100s", |
| 262 | filename, strerror(errno)); |
| 263 | return NULL; |
| 264 | } |
Darren Tucker | f4b4371 | 2004-08-29 16:28:39 +1000 | [diff] [blame] | 265 | if (st.st_size > 1*1024*1024) { |
| 266 | error("key file %.200s too large", filename); |
| 267 | return NULL; |
| 268 | } |
Darren Tucker | 1f8311c | 2004-05-13 16:39:33 +1000 | [diff] [blame] | 269 | len = (size_t)st.st_size; /* truncated */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 270 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 271 | buffer_init(&buffer); |
Damien Miller | 5a6b4fe | 2001-12-21 14:56:54 +1100 | [diff] [blame] | 272 | cp = buffer_append_space(&buffer, len); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 273 | |
Damien Miller | eccb9de | 2005-06-17 12:59:34 +1000 | [diff] [blame] | 274 | if (atomicio(read, fd, cp, len) != len) { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 275 | debug("Read from key file %.200s failed: %.100s", filename, |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 276 | strerror(errno)); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 277 | buffer_free(&buffer); |
Ben Lindstrom | d0fca42 | 2001-03-26 13:44:06 +0000 | [diff] [blame] | 278 | return NULL; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 279 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 280 | |
Ben Lindstrom | 1170d71 | 2001-01-29 07:51:26 +0000 | [diff] [blame] | 281 | /* Check that it is at least big enough to contain the ID string. */ |
| 282 | if (len < sizeof(authfile_id_string)) { |
Damien Miller | 058655c | 2001-10-10 15:03:36 +1000 | [diff] [blame] | 283 | debug3("Not a RSA1 key file %.200s.", filename); |
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 | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 287 | /* |
| 288 | * Make sure it begins with the id string. Consume the id string |
| 289 | * from the buffer. |
| 290 | */ |
Ben Lindstrom | 1170d71 | 2001-01-29 07:51:26 +0000 | [diff] [blame] | 291 | for (i = 0; i < sizeof(authfile_id_string); i++) |
| 292 | if (buffer_get_char(&buffer) != authfile_id_string[i]) { |
Damien Miller | 058655c | 2001-10-10 15:03:36 +1000 | [diff] [blame] | 293 | debug3("Not a RSA1 key file %.200s.", filename); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 294 | buffer_free(&buffer); |
Ben Lindstrom | d0fca42 | 2001-03-26 13:44:06 +0000 | [diff] [blame] | 295 | return NULL; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 296 | } |
| 297 | /* Skip cipher type and reserved data. */ |
| 298 | (void) buffer_get_char(&buffer); /* cipher type */ |
| 299 | (void) buffer_get_int(&buffer); /* reserved */ |
| 300 | |
| 301 | /* Read the public key from the buffer. */ |
Ben Lindstrom | c5a7f4f | 2002-06-25 23:19:13 +0000 | [diff] [blame] | 302 | (void) buffer_get_int(&buffer); |
Ben Lindstrom | d0fca42 | 2001-03-26 13:44:06 +0000 | [diff] [blame] | 303 | pub = key_new(KEY_RSA1); |
| 304 | buffer_get_bignum(&buffer, pub->rsa->n); |
| 305 | buffer_get_bignum(&buffer, pub->rsa->e); |
| 306 | if (commentp) |
| 307 | *commentp = buffer_get_string(&buffer, NULL); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 308 | /* The encrypted private part is not parsed by this function. */ |
| 309 | |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 310 | buffer_free(&buffer); |
Ben Lindstrom | d0fca42 | 2001-03-26 13:44:06 +0000 | [diff] [blame] | 311 | return pub; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 312 | } |
| 313 | |
Ben Lindstrom | d0fca42 | 2001-03-26 13:44:06 +0000 | [diff] [blame] | 314 | /* load public key from private-key file, works only for SSH v1 */ |
| 315 | Key * |
| 316 | key_load_public_type(int type, const char *filename, char **commentp) |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 317 | { |
Ben Lindstrom | d0fca42 | 2001-03-26 13:44:06 +0000 | [diff] [blame] | 318 | Key *pub; |
| 319 | int fd; |
| 320 | |
| 321 | if (type == KEY_RSA1) { |
| 322 | fd = open(filename, O_RDONLY); |
| 323 | if (fd < 0) |
| 324 | return NULL; |
| 325 | pub = key_load_public_rsa1(fd, filename, commentp); |
| 326 | close(fd); |
| 327 | return pub; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 328 | } |
Ben Lindstrom | d0fca42 | 2001-03-26 13:44:06 +0000 | [diff] [blame] | 329 | return NULL; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 330 | } |
| 331 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 332 | /* |
| 333 | * Loads the private key from the file. Returns 0 if an error is encountered |
| 334 | * (file does not exist or is not readable, or passphrase is bad). This |
| 335 | * initializes the private key. |
| 336 | * Assumes we are called under uid of the owner of the file. |
| 337 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 338 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 339 | static Key * |
Ben Lindstrom | d0fca42 | 2001-03-26 13:44:06 +0000 | [diff] [blame] | 340 | key_load_private_rsa1(int fd, const char *filename, const char *passphrase, |
| 341 | char **commentp) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 342 | { |
Damien Miller | eccb9de | 2005-06-17 12:59:34 +1000 | [diff] [blame] | 343 | u_int i; |
| 344 | int check1, check2, cipher_type; |
Darren Tucker | 1f8311c | 2004-05-13 16:39:33 +1000 | [diff] [blame] | 345 | size_t len; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 346 | Buffer buffer, decrypted; |
Damien Miller | 708d21c | 2002-01-22 23:18:15 +1100 | [diff] [blame] | 347 | u_char *cp; |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 348 | CipherContext ciphercontext; |
| 349 | Cipher *cipher; |
Ben Lindstrom | d0fca42 | 2001-03-26 13:44:06 +0000 | [diff] [blame] | 350 | Key *prv = NULL; |
Ben Lindstrom | 44adb8f | 2002-12-23 02:00:23 +0000 | [diff] [blame] | 351 | struct stat st; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 352 | |
Ben Lindstrom | 44adb8f | 2002-12-23 02:00:23 +0000 | [diff] [blame] | 353 | if (fstat(fd, &st) < 0) { |
| 354 | error("fstat for key file %.200s failed: %.100s", |
| 355 | filename, strerror(errno)); |
| 356 | close(fd); |
| 357 | return NULL; |
| 358 | } |
Darren Tucker | 1f8311c | 2004-05-13 16:39:33 +1000 | [diff] [blame] | 359 | if (st.st_size > 1*1024*1024) { |
Darren Tucker | f4b4371 | 2004-08-29 16:28:39 +1000 | [diff] [blame] | 360 | error("key file %.200s too large", filename); |
Darren Tucker | 1f8311c | 2004-05-13 16:39:33 +1000 | [diff] [blame] | 361 | close(fd); |
| 362 | return (NULL); |
| 363 | } |
| 364 | len = (size_t)st.st_size; /* truncated */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 365 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 366 | buffer_init(&buffer); |
Damien Miller | 5a6b4fe | 2001-12-21 14:56:54 +1100 | [diff] [blame] | 367 | cp = buffer_append_space(&buffer, len); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 368 | |
Damien Miller | eccb9de | 2005-06-17 12:59:34 +1000 | [diff] [blame] | 369 | if (atomicio(read, fd, cp, len) != len) { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 370 | debug("Read from key file %.200s failed: %.100s", filename, |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 371 | strerror(errno)); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 372 | buffer_free(&buffer); |
Damien Miller | 037a0dc | 1999-12-07 15:38:31 +1100 | [diff] [blame] | 373 | close(fd); |
Ben Lindstrom | d0fca42 | 2001-03-26 13:44:06 +0000 | [diff] [blame] | 374 | return NULL; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 375 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 376 | |
Ben Lindstrom | 1170d71 | 2001-01-29 07:51:26 +0000 | [diff] [blame] | 377 | /* Check that it is at least big enough to contain the ID string. */ |
| 378 | if (len < sizeof(authfile_id_string)) { |
Damien Miller | 058655c | 2001-10-10 15:03:36 +1000 | [diff] [blame] | 379 | debug3("Not a RSA1 key file %.200s.", filename); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 380 | buffer_free(&buffer); |
Ben Lindstrom | b257cca | 2001-03-05 04:59:27 +0000 | [diff] [blame] | 381 | close(fd); |
Ben Lindstrom | d0fca42 | 2001-03-26 13:44:06 +0000 | [diff] [blame] | 382 | return NULL; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 383 | } |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 384 | /* |
| 385 | * Make sure it begins with the id string. Consume the id string |
| 386 | * from the buffer. |
| 387 | */ |
Ben Lindstrom | 1170d71 | 2001-01-29 07:51:26 +0000 | [diff] [blame] | 388 | for (i = 0; i < sizeof(authfile_id_string); i++) |
| 389 | if (buffer_get_char(&buffer) != authfile_id_string[i]) { |
Damien Miller | 058655c | 2001-10-10 15:03:36 +1000 | [diff] [blame] | 390 | debug3("Not a RSA1 key file %.200s.", filename); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 391 | buffer_free(&buffer); |
Ben Lindstrom | b257cca | 2001-03-05 04:59:27 +0000 | [diff] [blame] | 392 | close(fd); |
Ben Lindstrom | d0fca42 | 2001-03-26 13:44:06 +0000 | [diff] [blame] | 393 | return NULL; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 394 | } |
Ben Lindstrom | b257cca | 2001-03-05 04:59:27 +0000 | [diff] [blame] | 395 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 396 | /* Read cipher type. */ |
| 397 | cipher_type = buffer_get_char(&buffer); |
| 398 | (void) buffer_get_int(&buffer); /* Reserved data. */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 399 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 400 | /* Read the public key from the buffer. */ |
Ben Lindstrom | c5a7f4f | 2002-06-25 23:19:13 +0000 | [diff] [blame] | 401 | (void) buffer_get_int(&buffer); |
Ben Lindstrom | d0fca42 | 2001-03-26 13:44:06 +0000 | [diff] [blame] | 402 | prv = key_new_private(KEY_RSA1); |
| 403 | |
| 404 | buffer_get_bignum(&buffer, prv->rsa->n); |
| 405 | buffer_get_bignum(&buffer, prv->rsa->e); |
| 406 | if (commentp) |
| 407 | *commentp = buffer_get_string(&buffer, NULL); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 408 | else |
| 409 | xfree(buffer_get_string(&buffer, NULL)); |
| 410 | |
| 411 | /* Check that it is a supported cipher. */ |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 412 | cipher = cipher_by_number(cipher_type); |
| 413 | if (cipher == NULL) { |
| 414 | debug("Unsupported cipher %d used in key file %.200s.", |
| 415 | cipher_type, filename); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 416 | buffer_free(&buffer); |
| 417 | goto fail; |
| 418 | } |
| 419 | /* Initialize space for decrypted data. */ |
| 420 | buffer_init(&decrypted); |
Damien Miller | 5a6b4fe | 2001-12-21 14:56:54 +1100 | [diff] [blame] | 421 | cp = buffer_append_space(&decrypted, buffer_len(&buffer)); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 422 | |
| 423 | /* Rest of the buffer is encrypted. Decrypt it using the passphrase. */ |
Damien Miller | 963f6b2 | 2002-02-19 15:21:23 +1100 | [diff] [blame] | 424 | cipher_set_key_string(&ciphercontext, cipher, passphrase, |
| 425 | CIPHER_DECRYPT); |
| 426 | cipher_crypt(&ciphercontext, cp, |
Damien Miller | 708d21c | 2002-01-22 23:18:15 +1100 | [diff] [blame] | 427 | buffer_ptr(&buffer), buffer_len(&buffer)); |
Damien Miller | 963f6b2 | 2002-02-19 15:21:23 +1100 | [diff] [blame] | 428 | cipher_cleanup(&ciphercontext); |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 429 | memset(&ciphercontext, 0, sizeof(ciphercontext)); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 430 | buffer_free(&buffer); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 431 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 432 | check1 = buffer_get_char(&decrypted); |
| 433 | check2 = buffer_get_char(&decrypted); |
| 434 | if (check1 != buffer_get_char(&decrypted) || |
| 435 | check2 != buffer_get_char(&decrypted)) { |
| 436 | if (strcmp(passphrase, "") != 0) |
Ben Lindstrom | d0fca42 | 2001-03-26 13:44:06 +0000 | [diff] [blame] | 437 | debug("Bad passphrase supplied for key file %.200s.", |
| 438 | filename); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 439 | /* Bad passphrase. */ |
| 440 | buffer_free(&decrypted); |
Ben Lindstrom | d0fca42 | 2001-03-26 13:44:06 +0000 | [diff] [blame] | 441 | goto fail; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 442 | } |
| 443 | /* Read the rest of the private key. */ |
Ben Lindstrom | d0fca42 | 2001-03-26 13:44:06 +0000 | [diff] [blame] | 444 | buffer_get_bignum(&decrypted, prv->rsa->d); |
| 445 | buffer_get_bignum(&decrypted, prv->rsa->iqmp); /* u */ |
| 446 | /* in SSL and SSH v1 p and q are exchanged */ |
| 447 | buffer_get_bignum(&decrypted, prv->rsa->q); /* p */ |
| 448 | buffer_get_bignum(&decrypted, prv->rsa->p); /* q */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 449 | |
Ben Lindstrom | d0fca42 | 2001-03-26 13:44:06 +0000 | [diff] [blame] | 450 | /* calculate p-1 and q-1 */ |
Damien Miller | da75516 | 2002-01-22 23:09:22 +1100 | [diff] [blame] | 451 | rsa_generate_additional_parameters(prv->rsa); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 452 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 453 | buffer_free(&decrypted); |
Damien Miller | ed33d3b | 2003-03-15 11:36:18 +1100 | [diff] [blame] | 454 | |
| 455 | /* enable blinding */ |
| 456 | if (RSA_blinding_on(prv->rsa, NULL) != 1) { |
| 457 | error("key_load_private_rsa1: RSA_blinding_on failed"); |
| 458 | goto fail; |
| 459 | } |
Ben Lindstrom | b257cca | 2001-03-05 04:59:27 +0000 | [diff] [blame] | 460 | close(fd); |
Ben Lindstrom | d0fca42 | 2001-03-26 13:44:06 +0000 | [diff] [blame] | 461 | return prv; |
| 462 | |
| 463 | fail: |
| 464 | if (commentp) |
| 465 | xfree(*commentp); |
| 466 | close(fd); |
| 467 | key_free(prv); |
| 468 | return NULL; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 469 | } |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 470 | |
Ben Lindstrom | 1bad256 | 2002-06-06 19:57:33 +0000 | [diff] [blame] | 471 | Key * |
Ben Lindstrom | d0fca42 | 2001-03-26 13:44:06 +0000 | [diff] [blame] | 472 | key_load_private_pem(int fd, int type, const char *passphrase, |
| 473 | char **commentp) |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 474 | { |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 475 | FILE *fp; |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 476 | EVP_PKEY *pk = NULL; |
Ben Lindstrom | d0fca42 | 2001-03-26 13:44:06 +0000 | [diff] [blame] | 477 | Key *prv = NULL; |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 478 | char *name = "<no key>"; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 479 | |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 480 | fp = fdopen(fd, "r"); |
| 481 | if (fp == NULL) { |
Ben Lindstrom | 15f3386 | 2001-04-16 02:00:02 +0000 | [diff] [blame] | 482 | error("fdopen failed: %s", strerror(errno)); |
Ben Lindstrom | b257cca | 2001-03-05 04:59:27 +0000 | [diff] [blame] | 483 | close(fd); |
Ben Lindstrom | d0fca42 | 2001-03-26 13:44:06 +0000 | [diff] [blame] | 484 | return NULL; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 485 | } |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 486 | pk = PEM_read_PrivateKey(fp, NULL, NULL, (char *)passphrase); |
| 487 | if (pk == NULL) { |
Ben Lindstrom | 648772f | 2001-04-19 20:47:10 +0000 | [diff] [blame] | 488 | debug("PEM_read_PrivateKey failed"); |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 489 | (void)ERR_get_error(); |
Ben Lindstrom | d0fca42 | 2001-03-26 13:44:06 +0000 | [diff] [blame] | 490 | } else if (pk->type == EVP_PKEY_RSA && |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 491 | (type == KEY_UNSPEC||type==KEY_RSA)) { |
Ben Lindstrom | d0fca42 | 2001-03-26 13:44:06 +0000 | [diff] [blame] | 492 | prv = key_new(KEY_UNSPEC); |
| 493 | prv->rsa = EVP_PKEY_get1_RSA(pk); |
| 494 | prv->type = KEY_RSA; |
| 495 | name = "rsa w/o comment"; |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 496 | #ifdef DEBUG_PK |
Ben Lindstrom | d0fca42 | 2001-03-26 13:44:06 +0000 | [diff] [blame] | 497 | RSA_print_fp(stderr, prv->rsa, 8); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 498 | #endif |
Damien Miller | ed33d3b | 2003-03-15 11:36:18 +1100 | [diff] [blame] | 499 | if (RSA_blinding_on(prv->rsa, NULL) != 1) { |
| 500 | error("key_load_private_pem: RSA_blinding_on failed"); |
| 501 | key_free(prv); |
| 502 | prv = NULL; |
| 503 | } |
Ben Lindstrom | d0fca42 | 2001-03-26 13:44:06 +0000 | [diff] [blame] | 504 | } else if (pk->type == EVP_PKEY_DSA && |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 505 | (type == KEY_UNSPEC||type==KEY_DSA)) { |
Ben Lindstrom | d0fca42 | 2001-03-26 13:44:06 +0000 | [diff] [blame] | 506 | prv = key_new(KEY_UNSPEC); |
| 507 | prv->dsa = EVP_PKEY_get1_DSA(pk); |
| 508 | prv->type = KEY_DSA; |
| 509 | name = "dsa w/o comment"; |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 510 | #ifdef DEBUG_PK |
Ben Lindstrom | d0fca42 | 2001-03-26 13:44:06 +0000 | [diff] [blame] | 511 | DSA_print_fp(stderr, prv->dsa, 8); |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 512 | #endif |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 513 | } else { |
| 514 | error("PEM_read_PrivateKey: mismatch or " |
| 515 | "unknown EVP_PKEY save_type %d", pk->save_type); |
| 516 | } |
| 517 | fclose(fp); |
| 518 | if (pk != NULL) |
| 519 | EVP_PKEY_free(pk); |
Ben Lindstrom | d0fca42 | 2001-03-26 13:44:06 +0000 | [diff] [blame] | 520 | if (prv != NULL && commentp) |
| 521 | *commentp = xstrdup(name); |
| 522 | debug("read PEM private key done: type %s", |
| 523 | prv ? key_type(prv) : "<unknown>"); |
| 524 | return prv; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 525 | } |
| 526 | |
Damien Miller | 8275fad | 2006-03-15 12:06:23 +1100 | [diff] [blame] | 527 | int |
Ben Lindstrom | d0fca42 | 2001-03-26 13:44:06 +0000 | [diff] [blame] | 528 | key_perm_ok(int fd, const char *filename) |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 529 | { |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 530 | struct stat st; |
| 531 | |
Ben Lindstrom | 7aff261 | 2001-09-23 13:53:22 +0000 | [diff] [blame] | 532 | if (fstat(fd, &st) < 0) |
| 533 | return 0; |
| 534 | /* |
| 535 | * if a key owned by the user is accessed, then we check the |
| 536 | * permissions of the file. if the key owned by a different user, |
| 537 | * then we don't care. |
| 538 | */ |
Damien Miller | b70b61f | 2000-09-16 16:25:12 +1100 | [diff] [blame] | 539 | #ifdef HAVE_CYGWIN |
Damien Miller | cb5e44a | 2000-09-29 12:12:36 +1100 | [diff] [blame] | 540 | if (check_ntsec(filename)) |
Damien Miller | b70b61f | 2000-09-16 16:25:12 +1100 | [diff] [blame] | 541 | #endif |
Ben Lindstrom | 7aff261 | 2001-09-23 13:53:22 +0000 | [diff] [blame] | 542 | if ((st.st_uid == getuid()) && (st.st_mode & 077) != 0) { |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 543 | error("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"); |
| 544 | error("@ WARNING: UNPROTECTED PRIVATE KEY FILE! @"); |
| 545 | error("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"); |
Ben Lindstrom | 7aff261 | 2001-09-23 13:53:22 +0000 | [diff] [blame] | 546 | error("Permissions 0%3.3o for '%s' are too open.", |
Damien Miller | 04bd8b0 | 2003-05-25 14:38:33 +1000 | [diff] [blame] | 547 | (u_int)st.st_mode & 0777, filename); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 548 | 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] | 549 | error("This private key will be ignored."); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 550 | return 0; |
| 551 | } |
Ben Lindstrom | d0fca42 | 2001-03-26 13:44:06 +0000 | [diff] [blame] | 552 | return 1; |
| 553 | } |
Ben Lindstrom | b257cca | 2001-03-05 04:59:27 +0000 | [diff] [blame] | 554 | |
Ben Lindstrom | d0fca42 | 2001-03-26 13:44:06 +0000 | [diff] [blame] | 555 | Key * |
| 556 | key_load_private_type(int type, const char *filename, const char *passphrase, |
Darren Tucker | 232b76f | 2006-05-06 17:41:51 +1000 | [diff] [blame] | 557 | char **commentp, int *perm_ok) |
Ben Lindstrom | d0fca42 | 2001-03-26 13:44:06 +0000 | [diff] [blame] | 558 | { |
| 559 | int fd; |
| 560 | |
| 561 | fd = open(filename, O_RDONLY); |
Darren Tucker | d4c86b1 | 2010-01-12 19:41:22 +1100 | [diff] [blame] | 562 | if (fd < 0) { |
| 563 | debug("could not open key file '%s': %s", filename, |
| 564 | strerror(errno)); |
| 565 | if (perm_ok != NULL) |
| 566 | *perm_ok = 0; |
Ben Lindstrom | d0fca42 | 2001-03-26 13:44:06 +0000 | [diff] [blame] | 567 | return NULL; |
Darren Tucker | 69c01b1 | 2010-01-12 19:42:29 +1100 | [diff] [blame] | 568 | } |
Ben Lindstrom | d0fca42 | 2001-03-26 13:44:06 +0000 | [diff] [blame] | 569 | if (!key_perm_ok(fd, filename)) { |
Darren Tucker | 232b76f | 2006-05-06 17:41:51 +1000 | [diff] [blame] | 570 | if (perm_ok != NULL) |
| 571 | *perm_ok = 0; |
Ben Lindstrom | 15f3386 | 2001-04-16 02:00:02 +0000 | [diff] [blame] | 572 | error("bad permissions: ignore key: %s", filename); |
Ben Lindstrom | d0fca42 | 2001-03-26 13:44:06 +0000 | [diff] [blame] | 573 | close(fd); |
| 574 | return NULL; |
| 575 | } |
Darren Tucker | 232b76f | 2006-05-06 17:41:51 +1000 | [diff] [blame] | 576 | if (perm_ok != NULL) |
| 577 | *perm_ok = 1; |
Ben Lindstrom | d0fca42 | 2001-03-26 13:44:06 +0000 | [diff] [blame] | 578 | switch (type) { |
| 579 | case KEY_RSA1: |
| 580 | return key_load_private_rsa1(fd, filename, passphrase, |
| 581 | commentp); |
| 582 | /* closes fd */ |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 583 | case KEY_DSA: |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 584 | case KEY_RSA: |
| 585 | case KEY_UNSPEC: |
Ben Lindstrom | d0fca42 | 2001-03-26 13:44:06 +0000 | [diff] [blame] | 586 | return key_load_private_pem(fd, type, passphrase, commentp); |
| 587 | /* closes fd */ |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 588 | default: |
Ben Lindstrom | b257cca | 2001-03-05 04:59:27 +0000 | [diff] [blame] | 589 | close(fd); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 590 | break; |
| 591 | } |
Ben Lindstrom | d0fca42 | 2001-03-26 13:44:06 +0000 | [diff] [blame] | 592 | return NULL; |
| 593 | } |
| 594 | |
| 595 | Key * |
| 596 | key_load_private(const char *filename, const char *passphrase, |
| 597 | char **commentp) |
| 598 | { |
Ben Lindstrom | 322915d | 2001-06-05 20:46:32 +0000 | [diff] [blame] | 599 | Key *pub, *prv; |
Ben Lindstrom | d0fca42 | 2001-03-26 13:44:06 +0000 | [diff] [blame] | 600 | int fd; |
| 601 | |
| 602 | fd = open(filename, O_RDONLY); |
Darren Tucker | d4c86b1 | 2010-01-12 19:41:22 +1100 | [diff] [blame] | 603 | if (fd < 0) { |
| 604 | debug("could not open key file '%s': %s", filename, |
| 605 | strerror(errno)); |
Ben Lindstrom | d0fca42 | 2001-03-26 13:44:06 +0000 | [diff] [blame] | 606 | return NULL; |
Darren Tucker | d4c86b1 | 2010-01-12 19:41:22 +1100 | [diff] [blame] | 607 | } |
Ben Lindstrom | d0fca42 | 2001-03-26 13:44:06 +0000 | [diff] [blame] | 608 | if (!key_perm_ok(fd, filename)) { |
Ben Lindstrom | 15f3386 | 2001-04-16 02:00:02 +0000 | [diff] [blame] | 609 | error("bad permissions: ignore key: %s", filename); |
Ben Lindstrom | d0fca42 | 2001-03-26 13:44:06 +0000 | [diff] [blame] | 610 | close(fd); |
| 611 | return NULL; |
| 612 | } |
| 613 | pub = key_load_public_rsa1(fd, filename, commentp); |
| 614 | lseek(fd, (off_t) 0, SEEK_SET); /* rewind */ |
| 615 | if (pub == NULL) { |
| 616 | /* closes fd */ |
Ben Lindstrom | 322915d | 2001-06-05 20:46:32 +0000 | [diff] [blame] | 617 | prv = key_load_private_pem(fd, KEY_UNSPEC, passphrase, NULL); |
| 618 | /* use the filename as a comment for PEM */ |
| 619 | if (commentp && prv) |
Ben Lindstrom | 2d0356f | 2001-06-05 21:13:57 +0000 | [diff] [blame] | 620 | *commentp = xstrdup(filename); |
Ben Lindstrom | d0fca42 | 2001-03-26 13:44:06 +0000 | [diff] [blame] | 621 | } else { |
| 622 | /* it's a SSH v1 key if the public key part is readable */ |
| 623 | key_free(pub); |
| 624 | /* closes fd */ |
Ben Lindstrom | 322915d | 2001-06-05 20:46:32 +0000 | [diff] [blame] | 625 | prv = key_load_private_rsa1(fd, filename, passphrase, NULL); |
Ben Lindstrom | d0fca42 | 2001-03-26 13:44:06 +0000 | [diff] [blame] | 626 | } |
Ben Lindstrom | 322915d | 2001-06-05 20:46:32 +0000 | [diff] [blame] | 627 | return prv; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 628 | } |
Damien Miller | e4340be | 2000-09-16 13:29:08 +1100 | [diff] [blame] | 629 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 630 | static int |
Ben Lindstrom | d0fca42 | 2001-03-26 13:44:06 +0000 | [diff] [blame] | 631 | key_try_load_public(Key *k, const char *filename, char **commentp) |
Damien Miller | e4340be | 2000-09-16 13:29:08 +1100 | [diff] [blame] | 632 | { |
| 633 | FILE *f; |
Darren Tucker | 22cc741 | 2004-12-06 22:47:41 +1100 | [diff] [blame] | 634 | char line[SSH_MAX_PUBKEY_BYTES]; |
Damien Miller | e4340be | 2000-09-16 13:29:08 +1100 | [diff] [blame] | 635 | char *cp; |
Darren Tucker | f0f9098 | 2004-12-11 13:39:50 +1100 | [diff] [blame] | 636 | u_long linenum = 0; |
Damien Miller | e4340be | 2000-09-16 13:29:08 +1100 | [diff] [blame] | 637 | |
| 638 | f = fopen(filename, "r"); |
| 639 | if (f != NULL) { |
Darren Tucker | 22cc741 | 2004-12-06 22:47:41 +1100 | [diff] [blame] | 640 | while (read_keyfile_line(f, filename, line, sizeof(line), |
| 641 | &linenum) != -1) { |
Damien Miller | e4340be | 2000-09-16 13:29:08 +1100 | [diff] [blame] | 642 | cp = line; |
Ben Lindstrom | 1c37c6a | 2001-12-06 18:00:18 +0000 | [diff] [blame] | 643 | switch (*cp) { |
Damien Miller | e4340be | 2000-09-16 13:29:08 +1100 | [diff] [blame] | 644 | case '#': |
| 645 | case '\n': |
| 646 | case '\0': |
| 647 | continue; |
| 648 | } |
| 649 | /* Skip leading whitespace. */ |
| 650 | for (; *cp && (*cp == ' ' || *cp == '\t'); cp++) |
| 651 | ; |
| 652 | if (*cp) { |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 653 | if (key_read(k, &cp) == 1) { |
Damien Miller | e4340be | 2000-09-16 13:29:08 +1100 | [diff] [blame] | 654 | if (commentp) |
| 655 | *commentp=xstrdup(filename); |
| 656 | fclose(f); |
| 657 | return 1; |
| 658 | } |
| 659 | } |
| 660 | } |
| 661 | fclose(f); |
| 662 | } |
| 663 | return 0; |
| 664 | } |
| 665 | |
Ben Lindstrom | d0fca42 | 2001-03-26 13:44:06 +0000 | [diff] [blame] | 666 | /* load public key from ssh v1 private or any pubkey file */ |
| 667 | Key * |
| 668 | key_load_public(const char *filename, char **commentp) |
Damien Miller | e4340be | 2000-09-16 13:29:08 +1100 | [diff] [blame] | 669 | { |
Ben Lindstrom | d0fca42 | 2001-03-26 13:44:06 +0000 | [diff] [blame] | 670 | Key *pub; |
| 671 | char file[MAXPATHLEN]; |
Damien Miller | e4340be | 2000-09-16 13:29:08 +1100 | [diff] [blame] | 672 | |
Damien Miller | db27472 | 2003-05-14 13:45:22 +1000 | [diff] [blame] | 673 | /* try rsa1 private key */ |
Ben Lindstrom | d0fca42 | 2001-03-26 13:44:06 +0000 | [diff] [blame] | 674 | pub = key_load_public_type(KEY_RSA1, filename, commentp); |
| 675 | if (pub != NULL) |
| 676 | return pub; |
Damien Miller | db27472 | 2003-05-14 13:45:22 +1000 | [diff] [blame] | 677 | |
| 678 | /* try rsa1 public key */ |
| 679 | pub = key_new(KEY_RSA1); |
| 680 | if (key_try_load_public(pub, filename, commentp) == 1) |
| 681 | return pub; |
| 682 | key_free(pub); |
| 683 | |
| 684 | /* try ssh2 public key */ |
Ben Lindstrom | d0fca42 | 2001-03-26 13:44:06 +0000 | [diff] [blame] | 685 | pub = key_new(KEY_UNSPEC); |
| 686 | if (key_try_load_public(pub, filename, commentp) == 1) |
| 687 | return pub; |
| 688 | if ((strlcpy(file, filename, sizeof file) < sizeof(file)) && |
| 689 | (strlcat(file, ".pub", sizeof file) < sizeof(file)) && |
| 690 | (key_try_load_public(pub, file, commentp) == 1)) |
| 691 | return pub; |
| 692 | key_free(pub); |
| 693 | return NULL; |
Damien Miller | e4340be | 2000-09-16 13:29:08 +1100 | [diff] [blame] | 694 | } |