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" |
Ben Lindstrom | b257cca | 2001-03-05 04:59:27 +0000 | [diff] [blame] | 39 | RCSID("$OpenBSD: authfile.c,v 1.28 2001/02/21 09:05:54 deraadt Exp $"); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 40 | |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 41 | #include <openssl/err.h> |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 42 | #include <openssl/evp.h> |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 43 | #include <openssl/pem.h> |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 44 | |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 45 | #include "cipher.h" |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 46 | #include "xmalloc.h" |
| 47 | #include "buffer.h" |
| 48 | #include "bufaux.h" |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 49 | #include "key.h" |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 50 | #include "ssh.h" |
| 51 | #include "log.h" |
Ben Lindstrom | 31ca54a | 2001-02-09 02:11:24 +0000 | [diff] [blame] | 52 | #include "authfile.h" |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 53 | |
| 54 | /* Version identification string for identity files. */ |
Ben Lindstrom | 1170d71 | 2001-01-29 07:51:26 +0000 | [diff] [blame] | 55 | static const char authfile_id_string[] = |
| 56 | "SSH PRIVATE KEY FILE FORMAT 1.1\n"; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 57 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 58 | /* |
| 59 | * Saves the authentication (private) key in a file, encrypting it with |
| 60 | * passphrase. The identification of the file (lowest 64 bits of n) will |
| 61 | * precede the key to provide identification of the key without needing a |
| 62 | * passphrase. |
| 63 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 64 | |
| 65 | int |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 66 | save_private_key_rsa1(const char *filename, const char *passphrase, |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 67 | RSA *key, const char *comment) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 68 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 69 | Buffer buffer, encrypted; |
| 70 | char buf[100], *cp; |
Damien Miller | 037a0dc | 1999-12-07 15:38:31 +1100 | [diff] [blame] | 71 | int fd, i; |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 72 | CipherContext ciphercontext; |
| 73 | Cipher *cipher; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 74 | u_int32_t rand; |
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 | * If the passphrase is empty, use SSH_CIPHER_NONE to ease converting |
| 78 | * to another cipher; otherwise use SSH_AUTHFILE_CIPHER. |
| 79 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 80 | if (strcmp(passphrase, "") == 0) |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 81 | cipher = cipher_by_number(SSH_CIPHER_NONE); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 82 | else |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 83 | cipher = cipher_by_number(SSH_AUTHFILE_CIPHER); |
| 84 | if (cipher == NULL) |
| 85 | fatal("save_private_key_rsa: bad cipher"); |
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 | /* This buffer is used to built the secret part of the private key. */ |
| 88 | buffer_init(&buffer); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 89 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 90 | /* Put checkbytes for checking passphrase validity. */ |
| 91 | rand = arc4random(); |
| 92 | buf[0] = rand & 0xff; |
| 93 | buf[1] = (rand >> 8) & 0xff; |
| 94 | buf[2] = buf[0]; |
| 95 | buf[3] = buf[1]; |
| 96 | buffer_append(&buffer, buf, 4); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 97 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 98 | /* |
| 99 | * Store the private key (n and e will not be stored because they |
| 100 | * will be stored in plain text, and storing them also in encrypted |
| 101 | * format would just give known plaintext). |
| 102 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 103 | buffer_put_bignum(&buffer, key->d); |
| 104 | buffer_put_bignum(&buffer, key->iqmp); |
| 105 | buffer_put_bignum(&buffer, key->q); /* reverse from SSL p */ |
| 106 | buffer_put_bignum(&buffer, key->p); /* reverse from SSL q */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 107 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 108 | /* Pad the part to be encrypted until its size is a multiple of 8. */ |
| 109 | while (buffer_len(&buffer) % 8 != 0) |
| 110 | buffer_put_char(&buffer, 0); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 111 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 112 | /* This buffer will be used to contain the data in the file. */ |
| 113 | buffer_init(&encrypted); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 114 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 115 | /* First store keyfile id string. */ |
Ben Lindstrom | 1170d71 | 2001-01-29 07:51:26 +0000 | [diff] [blame] | 116 | for (i = 0; authfile_id_string[i]; i++) |
| 117 | buffer_put_char(&encrypted, authfile_id_string[i]); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 118 | buffer_put_char(&encrypted, 0); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 119 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 120 | /* Store cipher type. */ |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 121 | buffer_put_char(&encrypted, cipher->number); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 122 | buffer_put_int(&encrypted, 0); /* For future extension */ |
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 | /* Store public key. This will be in plain text. */ |
| 125 | buffer_put_int(&encrypted, BN_num_bits(key->n)); |
| 126 | buffer_put_bignum(&encrypted, key->n); |
| 127 | buffer_put_bignum(&encrypted, key->e); |
| 128 | buffer_put_string(&encrypted, comment, strlen(comment)); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 129 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 130 | /* Allocate space for the private part of the key in the buffer. */ |
| 131 | buffer_append_space(&encrypted, &cp, buffer_len(&buffer)); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 132 | |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 133 | cipher_set_key_string(&ciphercontext, cipher, passphrase); |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 134 | cipher_encrypt(&ciphercontext, (u_char *) cp, |
| 135 | (u_char *) buffer_ptr(&buffer), buffer_len(&buffer)); |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 136 | memset(&ciphercontext, 0, sizeof(ciphercontext)); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 137 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 138 | /* Destroy temporary data. */ |
| 139 | memset(buf, 0, sizeof(buf)); |
| 140 | buffer_free(&buffer); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 141 | |
Damien Miller | 037a0dc | 1999-12-07 15:38:31 +1100 | [diff] [blame] | 142 | fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0600); |
| 143 | if (fd < 0) |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 144 | return 0; |
Damien Miller | 037a0dc | 1999-12-07 15:38:31 +1100 | [diff] [blame] | 145 | if (write(fd, buffer_ptr(&encrypted), buffer_len(&encrypted)) != |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 146 | buffer_len(&encrypted)) { |
| 147 | debug("Write to key file %.200s failed: %.100s", filename, |
| 148 | strerror(errno)); |
| 149 | buffer_free(&encrypted); |
Damien Miller | 037a0dc | 1999-12-07 15:38:31 +1100 | [diff] [blame] | 150 | close(fd); |
Damien Miller | 78315eb | 2000-09-29 23:01:36 +1100 | [diff] [blame] | 151 | unlink(filename); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 152 | return 0; |
| 153 | } |
Damien Miller | 037a0dc | 1999-12-07 15:38:31 +1100 | [diff] [blame] | 154 | close(fd); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 155 | buffer_free(&encrypted); |
| 156 | return 1; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 157 | } |
| 158 | |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 159 | /* save SSH2 key in OpenSSL PEM format */ |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 160 | int |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 161 | save_private_key_ssh2(const char *filename, const char *_passphrase, |
| 162 | Key *key, const char *comment) |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 163 | { |
| 164 | FILE *fp; |
| 165 | int fd; |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 166 | int success = 0; |
| 167 | int len = strlen(_passphrase); |
| 168 | char *passphrase = (len > 0) ? (char *)_passphrase : NULL; |
| 169 | EVP_CIPHER *cipher = (len > 0) ? EVP_des_ede3_cbc() : NULL; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 170 | |
| 171 | if (len > 0 && len <= 4) { |
| 172 | error("passphrase too short: %d bytes", len); |
| 173 | errno = 0; |
| 174 | return 0; |
| 175 | } |
| 176 | fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0600); |
| 177 | if (fd < 0) { |
| 178 | debug("open %s failed", filename); |
| 179 | return 0; |
| 180 | } |
| 181 | fp = fdopen(fd, "w"); |
| 182 | if (fp == NULL ) { |
| 183 | debug("fdopen %s failed", filename); |
| 184 | close(fd); |
| 185 | return 0; |
| 186 | } |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 187 | switch (key->type) { |
| 188 | case KEY_DSA: |
| 189 | success = PEM_write_DSAPrivateKey(fp, key->dsa, |
| 190 | cipher, passphrase, len, NULL, NULL); |
| 191 | break; |
| 192 | case KEY_RSA: |
| 193 | success = PEM_write_RSAPrivateKey(fp, key->rsa, |
| 194 | cipher, passphrase, len, NULL, NULL); |
| 195 | break; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 196 | } |
| 197 | fclose(fp); |
| 198 | return success; |
| 199 | } |
| 200 | |
| 201 | int |
| 202 | save_private_key(const char *filename, const char *passphrase, Key *key, |
| 203 | const char *comment) |
| 204 | { |
| 205 | switch (key->type) { |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 206 | case KEY_RSA1: |
| 207 | return save_private_key_rsa1(filename, passphrase, key->rsa, comment); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 208 | break; |
| 209 | case KEY_DSA: |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 210 | case KEY_RSA: |
| 211 | return save_private_key_ssh2(filename, passphrase, key, comment); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 212 | break; |
| 213 | default: |
| 214 | break; |
| 215 | } |
| 216 | return 0; |
| 217 | } |
| 218 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 219 | /* |
| 220 | * Loads the public part of the key file. Returns 0 if an error was |
| 221 | * encountered (the file does not exist or is not readable), and non-zero |
| 222 | * otherwise. |
| 223 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 224 | |
| 225 | int |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 226 | load_public_key_rsa(const char *filename, RSA * pub, char **comment_return) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 227 | { |
Damien Miller | 037a0dc | 1999-12-07 15:38:31 +1100 | [diff] [blame] | 228 | int fd, i; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 229 | off_t len; |
| 230 | Buffer buffer; |
| 231 | char *cp; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 232 | |
Damien Miller | 037a0dc | 1999-12-07 15:38:31 +1100 | [diff] [blame] | 233 | fd = open(filename, O_RDONLY); |
| 234 | if (fd < 0) |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 235 | return 0; |
Damien Miller | 037a0dc | 1999-12-07 15:38:31 +1100 | [diff] [blame] | 236 | len = lseek(fd, (off_t) 0, SEEK_END); |
| 237 | lseek(fd, (off_t) 0, SEEK_SET); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 238 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 239 | buffer_init(&buffer); |
| 240 | buffer_append_space(&buffer, &cp, len); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 241 | |
Damien Miller | 037a0dc | 1999-12-07 15:38:31 +1100 | [diff] [blame] | 242 | if (read(fd, cp, (size_t) len) != (size_t) len) { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 243 | debug("Read from key file %.200s failed: %.100s", filename, |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 244 | strerror(errno)); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 245 | buffer_free(&buffer); |
Damien Miller | 037a0dc | 1999-12-07 15:38:31 +1100 | [diff] [blame] | 246 | close(fd); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 247 | return 0; |
| 248 | } |
Damien Miller | 037a0dc | 1999-12-07 15:38:31 +1100 | [diff] [blame] | 249 | close(fd); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 250 | |
Ben Lindstrom | 1170d71 | 2001-01-29 07:51:26 +0000 | [diff] [blame] | 251 | /* Check that it is at least big enough to contain the ID string. */ |
| 252 | if (len < sizeof(authfile_id_string)) { |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 253 | debug3("Bad RSA1 key file %.200s.", filename); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 254 | buffer_free(&buffer); |
| 255 | return 0; |
| 256 | } |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 257 | /* |
| 258 | * Make sure it begins with the id string. Consume the id string |
| 259 | * from the buffer. |
| 260 | */ |
Ben Lindstrom | 1170d71 | 2001-01-29 07:51:26 +0000 | [diff] [blame] | 261 | for (i = 0; i < sizeof(authfile_id_string); i++) |
| 262 | if (buffer_get_char(&buffer) != authfile_id_string[i]) { |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 263 | debug3("Bad RSA1 key file %.200s.", filename); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 264 | buffer_free(&buffer); |
| 265 | return 0; |
| 266 | } |
| 267 | /* Skip cipher type and reserved data. */ |
| 268 | (void) buffer_get_char(&buffer); /* cipher type */ |
| 269 | (void) buffer_get_int(&buffer); /* reserved */ |
| 270 | |
| 271 | /* Read the public key from the buffer. */ |
| 272 | buffer_get_int(&buffer); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 273 | /* XXX alloc */ |
| 274 | if (pub->n == NULL) |
| 275 | pub->n = BN_new(); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 276 | buffer_get_bignum(&buffer, pub->n); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 277 | /* XXX alloc */ |
| 278 | if (pub->e == NULL) |
| 279 | pub->e = BN_new(); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 280 | buffer_get_bignum(&buffer, pub->e); |
| 281 | if (comment_return) |
| 282 | *comment_return = buffer_get_string(&buffer, NULL); |
| 283 | /* The encrypted private part is not parsed by this function. */ |
| 284 | |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 285 | buffer_free(&buffer); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 286 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 287 | return 1; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 288 | } |
| 289 | |
Damien Miller | e4340be | 2000-09-16 13:29:08 +1100 | [diff] [blame] | 290 | /* load public key from private-key file */ |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 291 | int |
| 292 | load_public_key(const char *filename, Key * key, char **comment_return) |
| 293 | { |
| 294 | switch (key->type) { |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 295 | case KEY_RSA1: |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 296 | return load_public_key_rsa(filename, key->rsa, comment_return); |
| 297 | break; |
| 298 | case KEY_DSA: |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 299 | case KEY_RSA: |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 300 | default: |
| 301 | break; |
| 302 | } |
| 303 | return 0; |
| 304 | } |
| 305 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 306 | /* |
| 307 | * Loads the private key from the file. Returns 0 if an error is encountered |
| 308 | * (file does not exist or is not readable, or passphrase is bad). This |
| 309 | * initializes the private key. |
| 310 | * Assumes we are called under uid of the owner of the file. |
| 311 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 312 | |
| 313 | int |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 314 | load_private_key_rsa1(int fd, const char *filename, |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 315 | const char *passphrase, RSA * prv, char **comment_return) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 316 | { |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 317 | int i, check1, check2, cipher_type; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 318 | off_t len; |
| 319 | Buffer buffer, decrypted; |
| 320 | char *cp; |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 321 | CipherContext ciphercontext; |
| 322 | Cipher *cipher; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 323 | BN_CTX *ctx; |
| 324 | BIGNUM *aux; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 325 | |
Damien Miller | 037a0dc | 1999-12-07 15:38:31 +1100 | [diff] [blame] | 326 | len = lseek(fd, (off_t) 0, SEEK_END); |
| 327 | lseek(fd, (off_t) 0, SEEK_SET); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 328 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 329 | buffer_init(&buffer); |
| 330 | buffer_append_space(&buffer, &cp, len); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 331 | |
Damien Miller | 037a0dc | 1999-12-07 15:38:31 +1100 | [diff] [blame] | 332 | if (read(fd, cp, (size_t) len) != (size_t) len) { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 333 | debug("Read from key file %.200s failed: %.100s", filename, |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 334 | strerror(errno)); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 335 | buffer_free(&buffer); |
Damien Miller | 037a0dc | 1999-12-07 15:38:31 +1100 | [diff] [blame] | 336 | close(fd); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 337 | return 0; |
| 338 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 339 | |
Ben Lindstrom | 1170d71 | 2001-01-29 07:51:26 +0000 | [diff] [blame] | 340 | /* Check that it is at least big enough to contain the ID string. */ |
| 341 | if (len < sizeof(authfile_id_string)) { |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 342 | debug3("Bad RSA1 key file %.200s.", filename); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 343 | buffer_free(&buffer); |
Ben Lindstrom | b257cca | 2001-03-05 04:59:27 +0000 | [diff] [blame] | 344 | close(fd); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 345 | return 0; |
| 346 | } |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 347 | /* |
| 348 | * Make sure it begins with the id string. Consume the id string |
| 349 | * from the buffer. |
| 350 | */ |
Ben Lindstrom | 1170d71 | 2001-01-29 07:51:26 +0000 | [diff] [blame] | 351 | for (i = 0; i < sizeof(authfile_id_string); i++) |
| 352 | if (buffer_get_char(&buffer) != authfile_id_string[i]) { |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 353 | debug3("Bad RSA1 key file %.200s.", filename); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 354 | buffer_free(&buffer); |
Ben Lindstrom | b257cca | 2001-03-05 04:59:27 +0000 | [diff] [blame] | 355 | close(fd); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 356 | return 0; |
| 357 | } |
Ben Lindstrom | b257cca | 2001-03-05 04:59:27 +0000 | [diff] [blame] | 358 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 359 | /* Read cipher type. */ |
| 360 | cipher_type = buffer_get_char(&buffer); |
| 361 | (void) buffer_get_int(&buffer); /* Reserved data. */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 362 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 363 | /* Read the public key from the buffer. */ |
| 364 | buffer_get_int(&buffer); |
| 365 | prv->n = BN_new(); |
| 366 | buffer_get_bignum(&buffer, prv->n); |
| 367 | prv->e = BN_new(); |
| 368 | buffer_get_bignum(&buffer, prv->e); |
| 369 | if (comment_return) |
| 370 | *comment_return = buffer_get_string(&buffer, NULL); |
| 371 | else |
| 372 | xfree(buffer_get_string(&buffer, NULL)); |
| 373 | |
| 374 | /* Check that it is a supported cipher. */ |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 375 | cipher = cipher_by_number(cipher_type); |
| 376 | if (cipher == NULL) { |
| 377 | debug("Unsupported cipher %d used in key file %.200s.", |
| 378 | cipher_type, filename); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 379 | buffer_free(&buffer); |
| 380 | goto fail; |
| 381 | } |
| 382 | /* Initialize space for decrypted data. */ |
| 383 | buffer_init(&decrypted); |
| 384 | buffer_append_space(&decrypted, &cp, buffer_len(&buffer)); |
| 385 | |
| 386 | /* Rest of the buffer is encrypted. Decrypt it using the passphrase. */ |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 387 | cipher_set_key_string(&ciphercontext, cipher, passphrase); |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 388 | cipher_decrypt(&ciphercontext, (u_char *) cp, |
| 389 | (u_char *) buffer_ptr(&buffer), buffer_len(&buffer)); |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 390 | memset(&ciphercontext, 0, sizeof(ciphercontext)); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 391 | buffer_free(&buffer); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 392 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 393 | check1 = buffer_get_char(&decrypted); |
| 394 | check2 = buffer_get_char(&decrypted); |
| 395 | if (check1 != buffer_get_char(&decrypted) || |
| 396 | check2 != buffer_get_char(&decrypted)) { |
| 397 | if (strcmp(passphrase, "") != 0) |
| 398 | debug("Bad passphrase supplied for key file %.200s.", filename); |
| 399 | /* Bad passphrase. */ |
| 400 | buffer_free(&decrypted); |
| 401 | fail: |
| 402 | BN_clear_free(prv->n); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 403 | prv->n = NULL; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 404 | BN_clear_free(prv->e); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 405 | prv->e = NULL; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 406 | if (comment_return) |
| 407 | xfree(*comment_return); |
Ben Lindstrom | b257cca | 2001-03-05 04:59:27 +0000 | [diff] [blame] | 408 | close(fd); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 409 | return 0; |
| 410 | } |
| 411 | /* Read the rest of the private key. */ |
| 412 | prv->d = BN_new(); |
| 413 | buffer_get_bignum(&decrypted, prv->d); |
| 414 | prv->iqmp = BN_new(); |
| 415 | buffer_get_bignum(&decrypted, prv->iqmp); /* u */ |
| 416 | /* in SSL and SSH p and q are exchanged */ |
| 417 | prv->q = BN_new(); |
| 418 | buffer_get_bignum(&decrypted, prv->q); /* p */ |
| 419 | prv->p = BN_new(); |
| 420 | buffer_get_bignum(&decrypted, prv->p); /* q */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 421 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 422 | ctx = BN_CTX_new(); |
| 423 | aux = BN_new(); |
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_sub(aux, prv->q, BN_value_one()); |
| 426 | prv->dmq1 = BN_new(); |
| 427 | BN_mod(prv->dmq1, prv->d, aux, ctx); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 428 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 429 | BN_sub(aux, prv->p, BN_value_one()); |
| 430 | prv->dmp1 = BN_new(); |
| 431 | BN_mod(prv->dmp1, prv->d, aux, ctx); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 432 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 433 | BN_clear_free(aux); |
| 434 | BN_CTX_free(ctx); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 435 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 436 | buffer_free(&decrypted); |
Ben Lindstrom | b257cca | 2001-03-05 04:59:27 +0000 | [diff] [blame] | 437 | close(fd); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 438 | return 1; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 439 | } |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 440 | |
| 441 | int |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 442 | load_private_key_ssh2(int fd, const char *passphrase, Key *k, char **comment_return) |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 443 | { |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 444 | FILE *fp; |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 445 | int success = 0; |
| 446 | EVP_PKEY *pk = NULL; |
| 447 | char *name = "<no key>"; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 448 | |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 449 | fp = fdopen(fd, "r"); |
| 450 | if (fp == NULL) { |
| 451 | error("fdopen failed"); |
Ben Lindstrom | b257cca | 2001-03-05 04:59:27 +0000 | [diff] [blame] | 452 | close(fd); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 453 | return 0; |
| 454 | } |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 455 | pk = PEM_read_PrivateKey(fp, NULL, NULL, (char *)passphrase); |
| 456 | if (pk == NULL) { |
| 457 | debug("PEM_read_PrivateKey failed"); |
| 458 | (void)ERR_get_error(); |
| 459 | } else if (pk->type == EVP_PKEY_RSA) { |
| 460 | /* replace k->rsa with loaded key */ |
| 461 | if (k->type == KEY_RSA || k->type == KEY_UNSPEC) { |
| 462 | if (k->rsa != NULL) |
| 463 | RSA_free(k->rsa); |
| 464 | k->rsa = EVP_PKEY_get1_RSA(pk); |
| 465 | k->type = KEY_RSA; |
| 466 | name = "rsa w/o comment"; |
| 467 | success = 1; |
| 468 | #ifdef DEBUG_PK |
| 469 | RSA_print_fp(stderr, k->rsa, 8); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 470 | #endif |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 471 | } |
| 472 | } else if (pk->type == EVP_PKEY_DSA) { |
| 473 | /* replace k->dsa with loaded key */ |
| 474 | if (k->type == KEY_DSA || k->type == KEY_UNSPEC) { |
| 475 | if (k->dsa != NULL) |
| 476 | DSA_free(k->dsa); |
| 477 | k->dsa = EVP_PKEY_get1_DSA(pk); |
| 478 | k->type = KEY_DSA; |
| 479 | name = "dsa w/o comment"; |
| 480 | #ifdef DEBUG_PK |
| 481 | DSA_print_fp(stderr, k->dsa, 8); |
| 482 | #endif |
| 483 | success = 1; |
| 484 | } |
| 485 | } else { |
| 486 | error("PEM_read_PrivateKey: mismatch or " |
| 487 | "unknown EVP_PKEY save_type %d", pk->save_type); |
| 488 | } |
| 489 | fclose(fp); |
| 490 | if (pk != NULL) |
| 491 | EVP_PKEY_free(pk); |
| 492 | if (success && comment_return) |
| 493 | *comment_return = xstrdup(name); |
| 494 | debug("read SSH2 private key done: name %s success %d", name, success); |
| 495 | return success; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 496 | } |
| 497 | |
| 498 | int |
| 499 | load_private_key(const char *filename, const char *passphrase, Key *key, |
| 500 | char **comment_return) |
| 501 | { |
| 502 | int fd; |
| 503 | int ret = 0; |
| 504 | struct stat st; |
| 505 | |
| 506 | fd = open(filename, O_RDONLY); |
| 507 | if (fd < 0) |
| 508 | return 0; |
| 509 | |
Damien Miller | cb5e44a | 2000-09-29 12:12:36 +1100 | [diff] [blame] | 510 | /* check owner and modes */ |
Damien Miller | b70b61f | 2000-09-16 16:25:12 +1100 | [diff] [blame] | 511 | #ifdef HAVE_CYGWIN |
Damien Miller | cb5e44a | 2000-09-29 12:12:36 +1100 | [diff] [blame] | 512 | if (check_ntsec(filename)) |
Damien Miller | b70b61f | 2000-09-16 16:25:12 +1100 | [diff] [blame] | 513 | #endif |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 514 | if (fstat(fd, &st) < 0 || |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 515 | (st.st_uid != 0 && getuid() != 0 && st.st_uid != getuid()) || |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 516 | (st.st_mode & 077) != 0) { |
| 517 | close(fd); |
| 518 | error("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"); |
| 519 | error("@ WARNING: UNPROTECTED PRIVATE KEY FILE! @"); |
| 520 | error("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"); |
| 521 | error("Bad ownership or mode(0%3.3o) for '%s'.", |
Ben Lindstrom | b257cca | 2001-03-05 04:59:27 +0000 | [diff] [blame] | 522 | st.st_mode & 0777, filename); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 523 | error("It is recommended that your private key files are NOT accessible by others."); |
| 524 | return 0; |
| 525 | } |
| 526 | switch (key->type) { |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 527 | case KEY_RSA1: |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 528 | if (key->rsa->e != NULL) { |
| 529 | BN_clear_free(key->rsa->e); |
| 530 | key->rsa->e = NULL; |
| 531 | } |
| 532 | if (key->rsa->n != NULL) { |
| 533 | BN_clear_free(key->rsa->n); |
| 534 | key->rsa->n = NULL; |
| 535 | } |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 536 | ret = load_private_key_rsa1(fd, filename, passphrase, |
Ben Lindstrom | b257cca | 2001-03-05 04:59:27 +0000 | [diff] [blame] | 537 | key->rsa, comment_return); /* closes fd */ |
| 538 | |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 539 | break; |
| 540 | case KEY_DSA: |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 541 | case KEY_RSA: |
| 542 | case KEY_UNSPEC: |
Ben Lindstrom | b257cca | 2001-03-05 04:59:27 +0000 | [diff] [blame] | 543 | ret = load_private_key_ssh2(fd, passphrase, key, |
| 544 | comment_return); /* closes fd */ |
| 545 | break; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 546 | default: |
Ben Lindstrom | b257cca | 2001-03-05 04:59:27 +0000 | [diff] [blame] | 547 | close(fd); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 548 | break; |
| 549 | } |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 550 | return ret; |
| 551 | } |
Damien Miller | e4340be | 2000-09-16 13:29:08 +1100 | [diff] [blame] | 552 | |
| 553 | int |
| 554 | do_load_public_key(const char *filename, Key *k, char **commentp) |
| 555 | { |
| 556 | FILE *f; |
Damien Miller | e4340be | 2000-09-16 13:29:08 +1100 | [diff] [blame] | 557 | char line[1024]; |
| 558 | char *cp; |
| 559 | |
| 560 | f = fopen(filename, "r"); |
| 561 | if (f != NULL) { |
| 562 | while (fgets(line, sizeof(line), f)) { |
| 563 | line[sizeof(line)-1] = '\0'; |
| 564 | cp = line; |
| 565 | switch(*cp){ |
| 566 | case '#': |
| 567 | case '\n': |
| 568 | case '\0': |
| 569 | continue; |
| 570 | } |
| 571 | /* Skip leading whitespace. */ |
| 572 | for (; *cp && (*cp == ' ' || *cp == '\t'); cp++) |
| 573 | ; |
| 574 | if (*cp) { |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 575 | if (key_read(k, &cp) == 1) { |
Damien Miller | e4340be | 2000-09-16 13:29:08 +1100 | [diff] [blame] | 576 | if (commentp) |
| 577 | *commentp=xstrdup(filename); |
| 578 | fclose(f); |
| 579 | return 1; |
| 580 | } |
| 581 | } |
| 582 | } |
| 583 | fclose(f); |
| 584 | } |
| 585 | return 0; |
| 586 | } |
| 587 | |
| 588 | /* load public key from pubkey file */ |
| 589 | int |
| 590 | try_load_public_key(const char *filename, Key *k, char **commentp) |
| 591 | { |
| 592 | char pub[MAXPATHLEN]; |
| 593 | |
| 594 | if (do_load_public_key(filename, k, commentp) == 1) |
| 595 | return 1; |
| 596 | if (strlcpy(pub, filename, sizeof pub) >= MAXPATHLEN) |
| 597 | return 0; |
| 598 | if (strlcat(pub, ".pub", sizeof pub) >= MAXPATHLEN) |
| 599 | return 0; |
| 600 | if (do_load_public_key(pub, k, commentp) == 1) |
| 601 | return 1; |
| 602 | return 0; |
| 603 | } |