Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1 | /* |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2 | * Author: Tatu Ylonen <ylo@cs.hut.fi> |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 3 | * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland |
| 4 | * All rights reserved |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 5 | * This file contains functions for reading and writing identity files, and |
| 6 | * for reading the passphrase from the user. |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 7 | * |
Damien Miller | e4340be | 2000-09-16 13:29:08 +1100 | [diff] [blame] | 8 | * As far as I am concerned, the code I have written for this software |
| 9 | * can be used freely for any purpose. Any derived versions of this |
| 10 | * software must be clearly marked as such, and if the derived work is |
| 11 | * incompatible with the protocol description in the RFC file, it must be |
| 12 | * called by a name other than "ssh" or "Secure Shell". |
| 13 | * |
| 14 | * |
| 15 | * Copyright (c) 2000 Markus Friedl. All rights reserved. |
| 16 | * |
| 17 | * Redistribution and use in source and binary forms, with or without |
| 18 | * modification, are permitted provided that the following conditions |
| 19 | * are met: |
| 20 | * 1. Redistributions of source code must retain the above copyright |
| 21 | * notice, this list of conditions and the following disclaimer. |
| 22 | * 2. Redistributions in binary form must reproduce the above copyright |
| 23 | * notice, this list of conditions and the following disclaimer in the |
| 24 | * documentation and/or other materials provided with the distribution. |
| 25 | * |
| 26 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
| 27 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
| 28 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
| 29 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 30 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 31 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 32 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 33 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 34 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
| 35 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 36 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 37 | |
| 38 | #include "includes.h" |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 39 | RCSID("$OpenBSD: authfile.c,v 1.20 2000/10/11 20:27:23 markus Exp $"); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 40 | |
| 41 | #include <openssl/bn.h> |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 42 | #include <openssl/dsa.h> |
| 43 | #include <openssl/rsa.h> |
| 44 | #include <openssl/pem.h> |
| 45 | #include <openssl/evp.h> |
| 46 | |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 47 | #include "xmalloc.h" |
| 48 | #include "buffer.h" |
| 49 | #include "bufaux.h" |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 50 | #include "ssh.h" |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 51 | #include "key.h" |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 52 | |
| 53 | /* Version identification string for identity files. */ |
| 54 | #define AUTHFILE_ID_STRING "SSH PRIVATE KEY FILE FORMAT 1.1\n" |
| 55 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 56 | /* |
| 57 | * Saves the authentication (private) key in a file, encrypting it with |
| 58 | * passphrase. The identification of the file (lowest 64 bits of n) will |
| 59 | * precede the key to provide identification of the key without needing a |
| 60 | * passphrase. |
| 61 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 62 | |
| 63 | int |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 64 | save_private_key_rsa(const char *filename, const char *passphrase, |
| 65 | RSA *key, const char *comment) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 66 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 67 | Buffer buffer, encrypted; |
| 68 | char buf[100], *cp; |
Damien Miller | 037a0dc | 1999-12-07 15:38:31 +1100 | [diff] [blame] | 69 | int fd, i; |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 70 | CipherContext ciphercontext; |
| 71 | Cipher *cipher; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 72 | u_int32_t rand; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 73 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 74 | /* |
| 75 | * If the passphrase is empty, use SSH_CIPHER_NONE to ease converting |
| 76 | * to another cipher; otherwise use SSH_AUTHFILE_CIPHER. |
| 77 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 78 | if (strcmp(passphrase, "") == 0) |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 79 | cipher = cipher_by_number(SSH_CIPHER_NONE); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 80 | else |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 81 | cipher = cipher_by_number(SSH_AUTHFILE_CIPHER); |
| 82 | if (cipher == NULL) |
| 83 | fatal("save_private_key_rsa: bad cipher"); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 84 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 85 | /* This buffer is used to built the secret part of the private key. */ |
| 86 | buffer_init(&buffer); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 87 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 88 | /* Put checkbytes for checking passphrase validity. */ |
| 89 | rand = arc4random(); |
| 90 | buf[0] = rand & 0xff; |
| 91 | buf[1] = (rand >> 8) & 0xff; |
| 92 | buf[2] = buf[0]; |
| 93 | buf[3] = buf[1]; |
| 94 | buffer_append(&buffer, buf, 4); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 95 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 96 | /* |
| 97 | * Store the private key (n and e will not be stored because they |
| 98 | * will be stored in plain text, and storing them also in encrypted |
| 99 | * format would just give known plaintext). |
| 100 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 101 | buffer_put_bignum(&buffer, key->d); |
| 102 | buffer_put_bignum(&buffer, key->iqmp); |
| 103 | buffer_put_bignum(&buffer, key->q); /* reverse from SSL p */ |
| 104 | buffer_put_bignum(&buffer, key->p); /* reverse from SSL q */ |
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 | /* Pad the part to be encrypted until its size is a multiple of 8. */ |
| 107 | while (buffer_len(&buffer) % 8 != 0) |
| 108 | buffer_put_char(&buffer, 0); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 109 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 110 | /* This buffer will be used to contain the data in the file. */ |
| 111 | buffer_init(&encrypted); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 112 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 113 | /* First store keyfile id string. */ |
| 114 | cp = AUTHFILE_ID_STRING; |
| 115 | for (i = 0; cp[i]; i++) |
| 116 | buffer_put_char(&encrypted, cp[i]); |
| 117 | buffer_put_char(&encrypted, 0); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 118 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 119 | /* Store cipher type. */ |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 120 | buffer_put_char(&encrypted, cipher->number); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 121 | buffer_put_int(&encrypted, 0); /* For future extension */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 122 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 123 | /* Store public key. This will be in plain text. */ |
| 124 | buffer_put_int(&encrypted, BN_num_bits(key->n)); |
| 125 | buffer_put_bignum(&encrypted, key->n); |
| 126 | buffer_put_bignum(&encrypted, key->e); |
| 127 | buffer_put_string(&encrypted, comment, strlen(comment)); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 128 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 129 | /* Allocate space for the private part of the key in the buffer. */ |
| 130 | buffer_append_space(&encrypted, &cp, buffer_len(&buffer)); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 131 | |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 132 | cipher_set_key_string(&ciphercontext, cipher, passphrase); |
| 133 | cipher_encrypt(&ciphercontext, (unsigned char *) cp, |
| 134 | (unsigned char *) buffer_ptr(&buffer), buffer_len(&buffer)); |
| 135 | memset(&ciphercontext, 0, sizeof(ciphercontext)); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 136 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 137 | /* Destroy temporary data. */ |
| 138 | memset(buf, 0, sizeof(buf)); |
| 139 | buffer_free(&buffer); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 140 | |
Damien Miller | 037a0dc | 1999-12-07 15:38:31 +1100 | [diff] [blame] | 141 | fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0600); |
| 142 | if (fd < 0) |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 143 | return 0; |
Damien Miller | 037a0dc | 1999-12-07 15:38:31 +1100 | [diff] [blame] | 144 | if (write(fd, buffer_ptr(&encrypted), buffer_len(&encrypted)) != |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 145 | buffer_len(&encrypted)) { |
| 146 | debug("Write to key file %.200s failed: %.100s", filename, |
| 147 | strerror(errno)); |
| 148 | buffer_free(&encrypted); |
Damien Miller | 037a0dc | 1999-12-07 15:38:31 +1100 | [diff] [blame] | 149 | close(fd); |
Damien Miller | 78315eb | 2000-09-29 23:01:36 +1100 | [diff] [blame] | 150 | unlink(filename); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 151 | return 0; |
| 152 | } |
Damien Miller | 037a0dc | 1999-12-07 15:38:31 +1100 | [diff] [blame] | 153 | close(fd); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 154 | buffer_free(&encrypted); |
| 155 | return 1; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 156 | } |
| 157 | |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 158 | /* save DSA key in OpenSSL PEM format */ |
| 159 | |
| 160 | int |
| 161 | save_private_key_dsa(const char *filename, const char *passphrase, |
| 162 | DSA *dsa, const char *comment) |
| 163 | { |
| 164 | FILE *fp; |
| 165 | int fd; |
| 166 | int success = 1; |
| 167 | int len = strlen(passphrase); |
| 168 | |
| 169 | if (len > 0 && len <= 4) { |
| 170 | error("passphrase too short: %d bytes", len); |
| 171 | errno = 0; |
| 172 | return 0; |
| 173 | } |
| 174 | fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0600); |
| 175 | if (fd < 0) { |
| 176 | debug("open %s failed", filename); |
| 177 | return 0; |
| 178 | } |
| 179 | fp = fdopen(fd, "w"); |
| 180 | if (fp == NULL ) { |
| 181 | debug("fdopen %s failed", filename); |
| 182 | close(fd); |
| 183 | return 0; |
| 184 | } |
| 185 | if (len > 0) { |
| 186 | if (!PEM_write_DSAPrivateKey(fp, dsa, EVP_des_ede3_cbc(), |
| 187 | (char *)passphrase, strlen(passphrase), NULL, NULL)) |
| 188 | success = 0; |
| 189 | } else { |
| 190 | if (!PEM_write_DSAPrivateKey(fp, dsa, NULL, |
| 191 | NULL, 0, NULL, NULL)) |
| 192 | success = 0; |
| 193 | } |
| 194 | fclose(fp); |
| 195 | return success; |
| 196 | } |
| 197 | |
| 198 | int |
| 199 | save_private_key(const char *filename, const char *passphrase, Key *key, |
| 200 | const char *comment) |
| 201 | { |
| 202 | switch (key->type) { |
| 203 | case KEY_RSA: |
| 204 | return save_private_key_rsa(filename, passphrase, key->rsa, comment); |
| 205 | break; |
| 206 | case KEY_DSA: |
| 207 | return save_private_key_dsa(filename, passphrase, key->dsa, comment); |
| 208 | break; |
| 209 | default: |
| 210 | break; |
| 211 | } |
| 212 | return 0; |
| 213 | } |
| 214 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 215 | /* |
| 216 | * Loads the public part of the key file. Returns 0 if an error was |
| 217 | * encountered (the file does not exist or is not readable), and non-zero |
| 218 | * otherwise. |
| 219 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 220 | |
| 221 | int |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 222 | load_public_key_rsa(const char *filename, RSA * pub, char **comment_return) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 223 | { |
Damien Miller | 037a0dc | 1999-12-07 15:38:31 +1100 | [diff] [blame] | 224 | int fd, i; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 225 | off_t len; |
| 226 | Buffer buffer; |
| 227 | char *cp; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 228 | |
Damien Miller | 037a0dc | 1999-12-07 15:38:31 +1100 | [diff] [blame] | 229 | fd = open(filename, O_RDONLY); |
| 230 | if (fd < 0) |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 231 | return 0; |
Damien Miller | 037a0dc | 1999-12-07 15:38:31 +1100 | [diff] [blame] | 232 | len = lseek(fd, (off_t) 0, SEEK_END); |
| 233 | lseek(fd, (off_t) 0, SEEK_SET); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 234 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 235 | buffer_init(&buffer); |
| 236 | buffer_append_space(&buffer, &cp, len); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 237 | |
Damien Miller | 037a0dc | 1999-12-07 15:38:31 +1100 | [diff] [blame] | 238 | if (read(fd, cp, (size_t) len) != (size_t) len) { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 239 | debug("Read from key file %.200s failed: %.100s", filename, |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 240 | strerror(errno)); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 241 | buffer_free(&buffer); |
Damien Miller | 037a0dc | 1999-12-07 15:38:31 +1100 | [diff] [blame] | 242 | close(fd); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 243 | return 0; |
| 244 | } |
Damien Miller | 037a0dc | 1999-12-07 15:38:31 +1100 | [diff] [blame] | 245 | close(fd); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 246 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 247 | /* Check that it is at least big enought to contain the ID string. */ |
| 248 | if (len < strlen(AUTHFILE_ID_STRING) + 1) { |
| 249 | debug("Bad key file %.200s.", filename); |
| 250 | buffer_free(&buffer); |
| 251 | return 0; |
| 252 | } |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 253 | /* |
| 254 | * Make sure it begins with the id string. Consume the id string |
| 255 | * from the buffer. |
| 256 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 257 | for (i = 0; i < (unsigned int) strlen(AUTHFILE_ID_STRING) + 1; i++) |
Damien Miller | 037a0dc | 1999-12-07 15:38:31 +1100 | [diff] [blame] | 258 | if (buffer_get_char(&buffer) != (u_char) AUTHFILE_ID_STRING[i]) { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 259 | debug("Bad key file %.200s.", filename); |
| 260 | buffer_free(&buffer); |
| 261 | return 0; |
| 262 | } |
| 263 | /* Skip cipher type and reserved data. */ |
| 264 | (void) buffer_get_char(&buffer); /* cipher type */ |
| 265 | (void) buffer_get_int(&buffer); /* reserved */ |
| 266 | |
| 267 | /* Read the public key from the buffer. */ |
| 268 | buffer_get_int(&buffer); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 269 | /* XXX alloc */ |
| 270 | if (pub->n == NULL) |
| 271 | pub->n = BN_new(); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 272 | buffer_get_bignum(&buffer, pub->n); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 273 | /* XXX alloc */ |
| 274 | if (pub->e == NULL) |
| 275 | pub->e = BN_new(); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 276 | buffer_get_bignum(&buffer, pub->e); |
| 277 | if (comment_return) |
| 278 | *comment_return = buffer_get_string(&buffer, NULL); |
| 279 | /* The encrypted private part is not parsed by this function. */ |
| 280 | |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 281 | buffer_free(&buffer); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 282 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 283 | return 1; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 284 | } |
| 285 | |
Damien Miller | e4340be | 2000-09-16 13:29:08 +1100 | [diff] [blame] | 286 | /* load public key from private-key file */ |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 287 | int |
| 288 | load_public_key(const char *filename, Key * key, char **comment_return) |
| 289 | { |
| 290 | switch (key->type) { |
| 291 | case KEY_RSA: |
| 292 | return load_public_key_rsa(filename, key->rsa, comment_return); |
| 293 | break; |
| 294 | case KEY_DSA: |
| 295 | default: |
| 296 | break; |
| 297 | } |
| 298 | return 0; |
| 299 | } |
| 300 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 301 | /* |
| 302 | * Loads the private key from the file. Returns 0 if an error is encountered |
| 303 | * (file does not exist or is not readable, or passphrase is bad). This |
| 304 | * initializes the private key. |
| 305 | * Assumes we are called under uid of the owner of the file. |
| 306 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 307 | |
| 308 | int |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 309 | load_private_key_rsa(int fd, const char *filename, |
| 310 | const char *passphrase, RSA * prv, char **comment_return) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 311 | { |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 312 | int i, check1, check2, cipher_type; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 313 | off_t len; |
| 314 | Buffer buffer, decrypted; |
| 315 | char *cp; |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 316 | CipherContext ciphercontext; |
| 317 | Cipher *cipher; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 318 | BN_CTX *ctx; |
| 319 | BIGNUM *aux; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 320 | |
Damien Miller | 037a0dc | 1999-12-07 15:38:31 +1100 | [diff] [blame] | 321 | len = lseek(fd, (off_t) 0, SEEK_END); |
| 322 | lseek(fd, (off_t) 0, SEEK_SET); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 323 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 324 | buffer_init(&buffer); |
| 325 | buffer_append_space(&buffer, &cp, len); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 326 | |
Damien Miller | 037a0dc | 1999-12-07 15:38:31 +1100 | [diff] [blame] | 327 | if (read(fd, cp, (size_t) len) != (size_t) len) { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 328 | debug("Read from key file %.200s failed: %.100s", filename, |
| 329 | strerror(errno)); |
| 330 | buffer_free(&buffer); |
Damien Miller | 037a0dc | 1999-12-07 15:38:31 +1100 | [diff] [blame] | 331 | close(fd); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 332 | return 0; |
| 333 | } |
Damien Miller | 037a0dc | 1999-12-07 15:38:31 +1100 | [diff] [blame] | 334 | close(fd); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 335 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 336 | /* Check that it is at least big enought to contain the ID string. */ |
| 337 | if (len < strlen(AUTHFILE_ID_STRING) + 1) { |
| 338 | debug("Bad key file %.200s.", filename); |
| 339 | buffer_free(&buffer); |
| 340 | return 0; |
| 341 | } |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 342 | /* |
| 343 | * Make sure it begins with the id string. Consume the id string |
| 344 | * from the buffer. |
| 345 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 346 | for (i = 0; i < (unsigned int) strlen(AUTHFILE_ID_STRING) + 1; i++) |
| 347 | if (buffer_get_char(&buffer) != (unsigned char) AUTHFILE_ID_STRING[i]) { |
| 348 | debug("Bad key file %.200s.", filename); |
| 349 | buffer_free(&buffer); |
| 350 | return 0; |
| 351 | } |
| 352 | /* Read cipher type. */ |
| 353 | cipher_type = buffer_get_char(&buffer); |
| 354 | (void) buffer_get_int(&buffer); /* Reserved data. */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 355 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 356 | /* Read the public key from the buffer. */ |
| 357 | buffer_get_int(&buffer); |
| 358 | prv->n = BN_new(); |
| 359 | buffer_get_bignum(&buffer, prv->n); |
| 360 | prv->e = BN_new(); |
| 361 | buffer_get_bignum(&buffer, prv->e); |
| 362 | if (comment_return) |
| 363 | *comment_return = buffer_get_string(&buffer, NULL); |
| 364 | else |
| 365 | xfree(buffer_get_string(&buffer, NULL)); |
| 366 | |
| 367 | /* Check that it is a supported cipher. */ |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 368 | cipher = cipher_by_number(cipher_type); |
| 369 | if (cipher == NULL) { |
| 370 | debug("Unsupported cipher %d used in key file %.200s.", |
| 371 | cipher_type, filename); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 372 | buffer_free(&buffer); |
| 373 | goto fail; |
| 374 | } |
| 375 | /* Initialize space for decrypted data. */ |
| 376 | buffer_init(&decrypted); |
| 377 | buffer_append_space(&decrypted, &cp, buffer_len(&buffer)); |
| 378 | |
| 379 | /* Rest of the buffer is encrypted. Decrypt it using the passphrase. */ |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 380 | cipher_set_key_string(&ciphercontext, cipher, passphrase); |
| 381 | cipher_decrypt(&ciphercontext, (unsigned char *) cp, |
| 382 | (unsigned char *) buffer_ptr(&buffer), buffer_len(&buffer)); |
| 383 | memset(&ciphercontext, 0, sizeof(ciphercontext)); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 384 | buffer_free(&buffer); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 385 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 386 | check1 = buffer_get_char(&decrypted); |
| 387 | check2 = buffer_get_char(&decrypted); |
| 388 | if (check1 != buffer_get_char(&decrypted) || |
| 389 | check2 != buffer_get_char(&decrypted)) { |
| 390 | if (strcmp(passphrase, "") != 0) |
| 391 | debug("Bad passphrase supplied for key file %.200s.", filename); |
| 392 | /* Bad passphrase. */ |
| 393 | buffer_free(&decrypted); |
| 394 | fail: |
| 395 | BN_clear_free(prv->n); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 396 | prv->n = NULL; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 397 | BN_clear_free(prv->e); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 398 | prv->e = NULL; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 399 | if (comment_return) |
| 400 | xfree(*comment_return); |
| 401 | return 0; |
| 402 | } |
| 403 | /* Read the rest of the private key. */ |
| 404 | prv->d = BN_new(); |
| 405 | buffer_get_bignum(&decrypted, prv->d); |
| 406 | prv->iqmp = BN_new(); |
| 407 | buffer_get_bignum(&decrypted, prv->iqmp); /* u */ |
| 408 | /* in SSL and SSH p and q are exchanged */ |
| 409 | prv->q = BN_new(); |
| 410 | buffer_get_bignum(&decrypted, prv->q); /* p */ |
| 411 | prv->p = BN_new(); |
| 412 | buffer_get_bignum(&decrypted, prv->p); /* q */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 413 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 414 | ctx = BN_CTX_new(); |
| 415 | aux = BN_new(); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 416 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 417 | BN_sub(aux, prv->q, BN_value_one()); |
| 418 | prv->dmq1 = BN_new(); |
| 419 | BN_mod(prv->dmq1, prv->d, aux, ctx); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 420 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 421 | BN_sub(aux, prv->p, BN_value_one()); |
| 422 | prv->dmp1 = BN_new(); |
| 423 | BN_mod(prv->dmp1, prv->d, aux, ctx); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 424 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 425 | BN_clear_free(aux); |
| 426 | BN_CTX_free(ctx); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 427 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 428 | buffer_free(&decrypted); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 429 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 430 | return 1; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 431 | } |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 432 | |
| 433 | int |
| 434 | load_private_key_dsa(int fd, const char *passphrase, Key *k, char **comment_return) |
| 435 | { |
| 436 | DSA *dsa; |
| 437 | BIO *in; |
| 438 | FILE *fp; |
| 439 | |
| 440 | in = BIO_new(BIO_s_file()); |
| 441 | if (in == NULL) { |
| 442 | error("BIO_new failed"); |
| 443 | return 0; |
| 444 | } |
| 445 | fp = fdopen(fd, "r"); |
| 446 | if (fp == NULL) { |
| 447 | error("fdopen failed"); |
| 448 | return 0; |
| 449 | } |
| 450 | BIO_set_fp(in, fp, BIO_NOCLOSE); |
| 451 | dsa = PEM_read_bio_DSAPrivateKey(in, NULL, NULL, (char *)passphrase); |
| 452 | if (dsa == NULL) { |
| 453 | debug("PEM_read_bio_DSAPrivateKey failed"); |
| 454 | } else { |
| 455 | /* replace k->dsa with loaded key */ |
| 456 | DSA_free(k->dsa); |
| 457 | k->dsa = dsa; |
| 458 | } |
| 459 | BIO_free(in); |
| 460 | fclose(fp); |
| 461 | if (comment_return) |
| 462 | *comment_return = xstrdup("dsa w/o comment"); |
| 463 | debug("read DSA private key done"); |
| 464 | #ifdef DEBUG_DSS |
| 465 | DSA_print_fp(stderr, dsa, 8); |
| 466 | #endif |
| 467 | return dsa != NULL ? 1 : 0; |
| 468 | } |
| 469 | |
| 470 | int |
| 471 | load_private_key(const char *filename, const char *passphrase, Key *key, |
| 472 | char **comment_return) |
| 473 | { |
| 474 | int fd; |
| 475 | int ret = 0; |
| 476 | struct stat st; |
| 477 | |
| 478 | fd = open(filename, O_RDONLY); |
| 479 | if (fd < 0) |
| 480 | return 0; |
| 481 | |
Damien Miller | cb5e44a | 2000-09-29 12:12:36 +1100 | [diff] [blame] | 482 | /* check owner and modes */ |
Damien Miller | b70b61f | 2000-09-16 16:25:12 +1100 | [diff] [blame] | 483 | #ifdef HAVE_CYGWIN |
Damien Miller | cb5e44a | 2000-09-29 12:12:36 +1100 | [diff] [blame] | 484 | if (check_ntsec(filename)) |
Damien Miller | b70b61f | 2000-09-16 16:25:12 +1100 | [diff] [blame] | 485 | #endif |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 486 | if (fstat(fd, &st) < 0 || |
| 487 | (st.st_uid != 0 && st.st_uid != getuid()) || |
| 488 | (st.st_mode & 077) != 0) { |
| 489 | close(fd); |
| 490 | error("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"); |
| 491 | error("@ WARNING: UNPROTECTED PRIVATE KEY FILE! @"); |
| 492 | error("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"); |
| 493 | error("Bad ownership or mode(0%3.3o) for '%s'.", |
| 494 | st.st_mode & 0777, filename); |
| 495 | error("It is recommended that your private key files are NOT accessible by others."); |
| 496 | return 0; |
| 497 | } |
| 498 | switch (key->type) { |
| 499 | case KEY_RSA: |
| 500 | if (key->rsa->e != NULL) { |
| 501 | BN_clear_free(key->rsa->e); |
| 502 | key->rsa->e = NULL; |
| 503 | } |
| 504 | if (key->rsa->n != NULL) { |
| 505 | BN_clear_free(key->rsa->n); |
| 506 | key->rsa->n = NULL; |
| 507 | } |
| 508 | ret = load_private_key_rsa(fd, filename, passphrase, |
| 509 | key->rsa, comment_return); |
| 510 | break; |
| 511 | case KEY_DSA: |
| 512 | ret = load_private_key_dsa(fd, passphrase, key, comment_return); |
| 513 | default: |
| 514 | break; |
| 515 | } |
| 516 | close(fd); |
| 517 | return ret; |
| 518 | } |
Damien Miller | e4340be | 2000-09-16 13:29:08 +1100 | [diff] [blame] | 519 | |
| 520 | int |
| 521 | do_load_public_key(const char *filename, Key *k, char **commentp) |
| 522 | { |
| 523 | FILE *f; |
| 524 | unsigned int bits; |
| 525 | char line[1024]; |
| 526 | char *cp; |
| 527 | |
| 528 | f = fopen(filename, "r"); |
| 529 | if (f != NULL) { |
| 530 | while (fgets(line, sizeof(line), f)) { |
| 531 | line[sizeof(line)-1] = '\0'; |
| 532 | cp = line; |
| 533 | switch(*cp){ |
| 534 | case '#': |
| 535 | case '\n': |
| 536 | case '\0': |
| 537 | continue; |
| 538 | } |
| 539 | /* Skip leading whitespace. */ |
| 540 | for (; *cp && (*cp == ' ' || *cp == '\t'); cp++) |
| 541 | ; |
| 542 | if (*cp) { |
| 543 | bits = key_read(k, &cp); |
| 544 | if (bits != 0) { |
| 545 | if (commentp) |
| 546 | *commentp=xstrdup(filename); |
| 547 | fclose(f); |
| 548 | return 1; |
| 549 | } |
| 550 | } |
| 551 | } |
| 552 | fclose(f); |
| 553 | } |
| 554 | return 0; |
| 555 | } |
| 556 | |
| 557 | /* load public key from pubkey file */ |
| 558 | int |
| 559 | try_load_public_key(const char *filename, Key *k, char **commentp) |
| 560 | { |
| 561 | char pub[MAXPATHLEN]; |
| 562 | |
| 563 | if (do_load_public_key(filename, k, commentp) == 1) |
| 564 | return 1; |
| 565 | if (strlcpy(pub, filename, sizeof pub) >= MAXPATHLEN) |
| 566 | return 0; |
| 567 | if (strlcat(pub, ".pub", sizeof pub) >= MAXPATHLEN) |
| 568 | return 0; |
| 569 | if (do_load_public_key(pub, k, commentp) == 1) |
| 570 | return 1; |
| 571 | return 0; |
| 572 | } |