]>
Commit | Line | Data |
---|---|---|
536c14e8 | 1 | /* $OpenBSD: authfile.c,v 1.72 2006/07/26 02:35:17 stevesk Exp $ */ |
8efc0c15 | 2 | /* |
5260325f | 3 | * Author: Tatu Ylonen <ylo@cs.hut.fi> |
5260325f | 4 | * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland |
5 | * All rights reserved | |
5260325f | 6 | * This file contains functions for reading and writing identity files, and |
7 | * for reading the passphrase from the user. | |
6ae2364d | 8 | * |
bcbf86ec | 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. | |
5260325f | 37 | */ |
8efc0c15 | 38 | |
39 | #include "includes.h" | |
4095f623 | 40 | |
41 | #include <sys/types.h> | |
42 | #include <sys/stat.h> | |
536c14e8 | 43 | #include <sys/param.h> |
8efc0c15 | 44 | |
fa08c86b | 45 | #include <openssl/err.h> |
a306f2dd | 46 | #include <openssl/evp.h> |
42f11eb2 | 47 | #include <openssl/pem.h> |
a306f2dd | 48 | |
dd984467 | 49 | #include <errno.h> |
d3221cca | 50 | #include <fcntl.h> |
00146caa | 51 | #include <string.h> |
5188ba17 | 52 | #include <unistd.h> |
d3221cca | 53 | |
42f11eb2 | 54 | #include "cipher.h" |
8efc0c15 | 55 | #include "xmalloc.h" |
56 | #include "buffer.h" | |
57 | #include "bufaux.h" | |
a306f2dd | 58 | #include "key.h" |
42f11eb2 | 59 | #include "ssh.h" |
60 | #include "log.h" | |
5ca51e19 | 61 | #include "authfile.h" |
cf184a44 | 62 | #include "rsa.h" |
41feb690 | 63 | #include "misc.h" |
2ceb8101 | 64 | #include "atomicio.h" |
8efc0c15 | 65 | |
aeaa3d9e | 66 | /* Version identification string for SSH v1 identity files. */ |
7e2183c0 | 67 | static const char authfile_id_string[] = |
68 | "SSH PRIVATE KEY FILE FORMAT 1.1\n"; | |
8efc0c15 | 69 | |
aa3378df | 70 | /* |
71 | * Saves the authentication (private) key in a file, encrypting it with | |
72 | * passphrase. The identification of the file (lowest 64 bits of n) will | |
73 | * precede the key to provide identification of the key without needing a | |
74 | * passphrase. | |
75 | */ | |
8efc0c15 | 76 | |
396c147e | 77 | static int |
aeaa3d9e | 78 | key_save_private_rsa1(Key *key, const char *filename, const char *passphrase, |
79 | const char *comment) | |
8efc0c15 | 80 | { |
5260325f | 81 | Buffer buffer, encrypted; |
20905a8e | 82 | u_char buf[100], *cp; |
3ee832e5 | 83 | int fd, i, cipher_num; |
94ec8c6b | 84 | CipherContext ciphercontext; |
85 | Cipher *cipher; | |
ca75d7de | 86 | u_int32_t rnd; |
5260325f | 87 | |
aa3378df | 88 | /* |
89 | * If the passphrase is empty, use SSH_CIPHER_NONE to ease converting | |
90 | * to another cipher; otherwise use SSH_AUTHFILE_CIPHER. | |
91 | */ | |
3ee832e5 | 92 | cipher_num = (strcmp(passphrase, "") == 0) ? |
93 | SSH_CIPHER_NONE : SSH_AUTHFILE_CIPHER; | |
94 | if ((cipher = cipher_by_number(cipher_num)) == NULL) | |
94ec8c6b | 95 | fatal("save_private_key_rsa: bad cipher"); |
5260325f | 96 | |
97 | /* This buffer is used to built the secret part of the private key. */ | |
98 | buffer_init(&buffer); | |
99 | ||
100 | /* Put checkbytes for checking passphrase validity. */ | |
ca75d7de | 101 | rnd = arc4random(); |
102 | buf[0] = rnd & 0xff; | |
103 | buf[1] = (rnd >> 8) & 0xff; | |
5260325f | 104 | buf[2] = buf[0]; |
105 | buf[3] = buf[1]; | |
106 | buffer_append(&buffer, buf, 4); | |
107 | ||
aa3378df | 108 | /* |
109 | * Store the private key (n and e will not be stored because they | |
110 | * will be stored in plain text, and storing them also in encrypted | |
111 | * format would just give known plaintext). | |
112 | */ | |
aeaa3d9e | 113 | buffer_put_bignum(&buffer, key->rsa->d); |
114 | buffer_put_bignum(&buffer, key->rsa->iqmp); | |
115 | buffer_put_bignum(&buffer, key->rsa->q); /* reverse from SSL p */ | |
116 | buffer_put_bignum(&buffer, key->rsa->p); /* reverse from SSL q */ | |
5260325f | 117 | |
118 | /* Pad the part to be encrypted until its size is a multiple of 8. */ | |
119 | while (buffer_len(&buffer) % 8 != 0) | |
120 | buffer_put_char(&buffer, 0); | |
121 | ||
122 | /* This buffer will be used to contain the data in the file. */ | |
123 | buffer_init(&encrypted); | |
124 | ||
125 | /* First store keyfile id string. */ | |
7e2183c0 | 126 | for (i = 0; authfile_id_string[i]; i++) |
127 | buffer_put_char(&encrypted, authfile_id_string[i]); | |
5260325f | 128 | buffer_put_char(&encrypted, 0); |
129 | ||
130 | /* Store cipher type. */ | |
3ee832e5 | 131 | buffer_put_char(&encrypted, cipher_num); |
5260325f | 132 | buffer_put_int(&encrypted, 0); /* For future extension */ |
133 | ||
134 | /* Store public key. This will be in plain text. */ | |
aeaa3d9e | 135 | buffer_put_int(&encrypted, BN_num_bits(key->rsa->n)); |
136 | buffer_put_bignum(&encrypted, key->rsa->n); | |
137 | buffer_put_bignum(&encrypted, key->rsa->e); | |
449c5ba5 | 138 | buffer_put_cstring(&encrypted, comment); |
5260325f | 139 | |
140 | /* Allocate space for the private part of the key in the buffer. */ | |
6c0fa2b1 | 141 | cp = buffer_append_space(&encrypted, buffer_len(&buffer)); |
5260325f | 142 | |
3ee832e5 | 143 | cipher_set_key_string(&ciphercontext, cipher, passphrase, |
144 | CIPHER_ENCRYPT); | |
145 | cipher_crypt(&ciphercontext, cp, | |
20905a8e | 146 | buffer_ptr(&buffer), buffer_len(&buffer)); |
3ee832e5 | 147 | cipher_cleanup(&ciphercontext); |
94ec8c6b | 148 | memset(&ciphercontext, 0, sizeof(ciphercontext)); |
5260325f | 149 | |
150 | /* Destroy temporary data. */ | |
151 | memset(buf, 0, sizeof(buf)); | |
152 | buffer_free(&buffer); | |
153 | ||
a408af76 | 154 | fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0600); |
58cfa257 | 155 | if (fd < 0) { |
156 | error("open %s failed: %s.", filename, strerror(errno)); | |
1bd71826 | 157 | buffer_free(&encrypted); |
5260325f | 158 | return 0; |
58cfa257 | 159 | } |
2ceb8101 | 160 | if (atomicio(vwrite, fd, buffer_ptr(&encrypted), |
161 | buffer_len(&encrypted)) != buffer_len(&encrypted)) { | |
58cfa257 | 162 | error("write to key file %s failed: %s", filename, |
184eed6a | 163 | strerror(errno)); |
5260325f | 164 | buffer_free(&encrypted); |
a408af76 | 165 | close(fd); |
77bb0bca | 166 | unlink(filename); |
5260325f | 167 | return 0; |
168 | } | |
a408af76 | 169 | close(fd); |
5260325f | 170 | buffer_free(&encrypted); |
171 | return 1; | |
8efc0c15 | 172 | } |
173 | ||
aeaa3d9e | 174 | /* save SSH v2 key in OpenSSL PEM format */ |
396c147e | 175 | static int |
aeaa3d9e | 176 | key_save_private_pem(Key *key, const char *filename, const char *_passphrase, |
177 | const char *comment) | |
a306f2dd | 178 | { |
179 | FILE *fp; | |
180 | int fd; | |
fa08c86b | 181 | int success = 0; |
182 | int len = strlen(_passphrase); | |
c66f9d0e | 183 | u_char *passphrase = (len > 0) ? (u_char *)_passphrase : NULL; |
714954dc | 184 | const EVP_CIPHER *cipher = (len > 0) ? EVP_des_ede3_cbc() : NULL; |
a306f2dd | 185 | |
186 | if (len > 0 && len <= 4) { | |
58cfa257 | 187 | error("passphrase too short: have %d bytes, need > 4", len); |
a306f2dd | 188 | return 0; |
189 | } | |
190 | fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0600); | |
191 | if (fd < 0) { | |
58cfa257 | 192 | error("open %s failed: %s.", filename, strerror(errno)); |
a306f2dd | 193 | return 0; |
194 | } | |
195 | fp = fdopen(fd, "w"); | |
196 | if (fp == NULL ) { | |
58cfa257 | 197 | error("fdopen %s failed: %s.", filename, strerror(errno)); |
a306f2dd | 198 | close(fd); |
199 | return 0; | |
200 | } | |
fa08c86b | 201 | switch (key->type) { |
569807fb | 202 | case KEY_DSA: |
203 | success = PEM_write_DSAPrivateKey(fp, key->dsa, | |
204 | cipher, passphrase, len, NULL, NULL); | |
205 | break; | |
206 | case KEY_RSA: | |
207 | success = PEM_write_RSAPrivateKey(fp, key->rsa, | |
208 | cipher, passphrase, len, NULL, NULL); | |
209 | break; | |
a306f2dd | 210 | } |
211 | fclose(fp); | |
212 | return success; | |
213 | } | |
214 | ||
215 | int | |
aeaa3d9e | 216 | key_save_private(Key *key, const char *filename, const char *passphrase, |
a306f2dd | 217 | const char *comment) |
218 | { | |
219 | switch (key->type) { | |
fa08c86b | 220 | case KEY_RSA1: |
aeaa3d9e | 221 | return key_save_private_rsa1(key, filename, passphrase, |
222 | comment); | |
a306f2dd | 223 | case KEY_DSA: |
fa08c86b | 224 | case KEY_RSA: |
aeaa3d9e | 225 | return key_save_private_pem(key, filename, passphrase, |
226 | comment); | |
a306f2dd | 227 | default: |
228 | break; | |
229 | } | |
58cfa257 | 230 | error("key_save_private: cannot save key type %d", key->type); |
a306f2dd | 231 | return 0; |
232 | } | |
233 | ||
aa3378df | 234 | /* |
aeaa3d9e | 235 | * Loads the public part of the ssh v1 key file. Returns NULL if an error was |
236 | * encountered (the file does not exist or is not readable), and the key | |
aa3378df | 237 | * otherwise. |
238 | */ | |
8efc0c15 | 239 | |
396c147e | 240 | static Key * |
aeaa3d9e | 241 | key_load_public_rsa1(int fd, const char *filename, char **commentp) |
8efc0c15 | 242 | { |
5260325f | 243 | Buffer buffer; |
aeaa3d9e | 244 | Key *pub; |
378d9956 | 245 | struct stat st; |
5260325f | 246 | char *cp; |
2ceb8101 | 247 | u_int i; |
f6be21a0 | 248 | size_t len; |
5260325f | 249 | |
378d9956 | 250 | if (fstat(fd, &st) < 0) { |
251 | error("fstat for key file %.200s failed: %.100s", | |
252 | filename, strerror(errno)); | |
253 | return NULL; | |
254 | } | |
528afafa | 255 | if (st.st_size > 1*1024*1024) { |
256 | error("key file %.200s too large", filename); | |
257 | return NULL; | |
258 | } | |
f6be21a0 | 259 | len = (size_t)st.st_size; /* truncated */ |
5260325f | 260 | |
261 | buffer_init(&buffer); | |
6c0fa2b1 | 262 | cp = buffer_append_space(&buffer, len); |
5260325f | 263 | |
2ceb8101 | 264 | if (atomicio(read, fd, cp, len) != len) { |
5260325f | 265 | debug("Read from key file %.200s failed: %.100s", filename, |
a306f2dd | 266 | strerror(errno)); |
5260325f | 267 | buffer_free(&buffer); |
aeaa3d9e | 268 | return NULL; |
5260325f | 269 | } |
5260325f | 270 | |
7e2183c0 | 271 | /* Check that it is at least big enough to contain the ID string. */ |
272 | if (len < sizeof(authfile_id_string)) { | |
2cdccb44 | 273 | debug3("Not a RSA1 key file %.200s.", filename); |
5260325f | 274 | buffer_free(&buffer); |
aeaa3d9e | 275 | return NULL; |
5260325f | 276 | } |
aa3378df | 277 | /* |
278 | * Make sure it begins with the id string. Consume the id string | |
279 | * from the buffer. | |
280 | */ | |
7e2183c0 | 281 | for (i = 0; i < sizeof(authfile_id_string); i++) |
282 | if (buffer_get_char(&buffer) != authfile_id_string[i]) { | |
2cdccb44 | 283 | debug3("Not a RSA1 key file %.200s.", filename); |
5260325f | 284 | buffer_free(&buffer); |
aeaa3d9e | 285 | return NULL; |
5260325f | 286 | } |
287 | /* Skip cipher type and reserved data. */ | |
288 | (void) buffer_get_char(&buffer); /* cipher type */ | |
289 | (void) buffer_get_int(&buffer); /* reserved */ | |
290 | ||
291 | /* Read the public key from the buffer. */ | |
1169c3df | 292 | (void) buffer_get_int(&buffer); |
aeaa3d9e | 293 | pub = key_new(KEY_RSA1); |
294 | buffer_get_bignum(&buffer, pub->rsa->n); | |
295 | buffer_get_bignum(&buffer, pub->rsa->e); | |
296 | if (commentp) | |
297 | *commentp = buffer_get_string(&buffer, NULL); | |
5260325f | 298 | /* The encrypted private part is not parsed by this function. */ |
299 | ||
8efc0c15 | 300 | buffer_free(&buffer); |
aeaa3d9e | 301 | return pub; |
8efc0c15 | 302 | } |
303 | ||
aeaa3d9e | 304 | /* load public key from private-key file, works only for SSH v1 */ |
305 | Key * | |
306 | key_load_public_type(int type, const char *filename, char **commentp) | |
a306f2dd | 307 | { |
aeaa3d9e | 308 | Key *pub; |
309 | int fd; | |
310 | ||
311 | if (type == KEY_RSA1) { | |
312 | fd = open(filename, O_RDONLY); | |
313 | if (fd < 0) | |
314 | return NULL; | |
315 | pub = key_load_public_rsa1(fd, filename, commentp); | |
316 | close(fd); | |
317 | return pub; | |
a306f2dd | 318 | } |
aeaa3d9e | 319 | return NULL; |
a306f2dd | 320 | } |
321 | ||
aa3378df | 322 | /* |
323 | * Loads the private key from the file. Returns 0 if an error is encountered | |
324 | * (file does not exist or is not readable, or passphrase is bad). This | |
325 | * initializes the private key. | |
326 | * Assumes we are called under uid of the owner of the file. | |
327 | */ | |
8efc0c15 | 328 | |
396c147e | 329 | static Key * |
aeaa3d9e | 330 | key_load_private_rsa1(int fd, const char *filename, const char *passphrase, |
331 | char **commentp) | |
8efc0c15 | 332 | { |
2ceb8101 | 333 | u_int i; |
334 | int check1, check2, cipher_type; | |
f6be21a0 | 335 | size_t len; |
5260325f | 336 | Buffer buffer, decrypted; |
20905a8e | 337 | u_char *cp; |
94ec8c6b | 338 | CipherContext ciphercontext; |
339 | Cipher *cipher; | |
aeaa3d9e | 340 | Key *prv = NULL; |
378d9956 | 341 | struct stat st; |
5260325f | 342 | |
378d9956 | 343 | if (fstat(fd, &st) < 0) { |
344 | error("fstat for key file %.200s failed: %.100s", | |
345 | filename, strerror(errno)); | |
346 | close(fd); | |
347 | return NULL; | |
348 | } | |
f6be21a0 | 349 | if (st.st_size > 1*1024*1024) { |
528afafa | 350 | error("key file %.200s too large", filename); |
f6be21a0 | 351 | close(fd); |
352 | return (NULL); | |
353 | } | |
354 | len = (size_t)st.st_size; /* truncated */ | |
5260325f | 355 | |
356 | buffer_init(&buffer); | |
6c0fa2b1 | 357 | cp = buffer_append_space(&buffer, len); |
5260325f | 358 | |
2ceb8101 | 359 | if (atomicio(read, fd, cp, len) != len) { |
5260325f | 360 | debug("Read from key file %.200s failed: %.100s", filename, |
fa08c86b | 361 | strerror(errno)); |
5260325f | 362 | buffer_free(&buffer); |
a408af76 | 363 | close(fd); |
aeaa3d9e | 364 | return NULL; |
5260325f | 365 | } |
5260325f | 366 | |
7e2183c0 | 367 | /* Check that it is at least big enough to contain the ID string. */ |
368 | if (len < sizeof(authfile_id_string)) { | |
2cdccb44 | 369 | debug3("Not a RSA1 key file %.200s.", filename); |
5260325f | 370 | buffer_free(&buffer); |
81333640 | 371 | close(fd); |
aeaa3d9e | 372 | return NULL; |
5260325f | 373 | } |
aa3378df | 374 | /* |
375 | * Make sure it begins with the id string. Consume the id string | |
376 | * from the buffer. | |
377 | */ | |
7e2183c0 | 378 | for (i = 0; i < sizeof(authfile_id_string); i++) |
379 | if (buffer_get_char(&buffer) != authfile_id_string[i]) { | |
2cdccb44 | 380 | debug3("Not a RSA1 key file %.200s.", filename); |
5260325f | 381 | buffer_free(&buffer); |
81333640 | 382 | close(fd); |
aeaa3d9e | 383 | return NULL; |
5260325f | 384 | } |
81333640 | 385 | |
5260325f | 386 | /* Read cipher type. */ |
387 | cipher_type = buffer_get_char(&buffer); | |
388 | (void) buffer_get_int(&buffer); /* Reserved data. */ | |
389 | ||
390 | /* Read the public key from the buffer. */ | |
1169c3df | 391 | (void) buffer_get_int(&buffer); |
aeaa3d9e | 392 | prv = key_new_private(KEY_RSA1); |
393 | ||
394 | buffer_get_bignum(&buffer, prv->rsa->n); | |
395 | buffer_get_bignum(&buffer, prv->rsa->e); | |
396 | if (commentp) | |
397 | *commentp = buffer_get_string(&buffer, NULL); | |
5260325f | 398 | else |
399 | xfree(buffer_get_string(&buffer, NULL)); | |
400 | ||
401 | /* Check that it is a supported cipher. */ | |
94ec8c6b | 402 | cipher = cipher_by_number(cipher_type); |
403 | if (cipher == NULL) { | |
404 | debug("Unsupported cipher %d used in key file %.200s.", | |
405 | cipher_type, filename); | |
5260325f | 406 | buffer_free(&buffer); |
407 | goto fail; | |
408 | } | |
409 | /* Initialize space for decrypted data. */ | |
410 | buffer_init(&decrypted); | |
6c0fa2b1 | 411 | cp = buffer_append_space(&decrypted, buffer_len(&buffer)); |
5260325f | 412 | |
413 | /* Rest of the buffer is encrypted. Decrypt it using the passphrase. */ | |
3ee832e5 | 414 | cipher_set_key_string(&ciphercontext, cipher, passphrase, |
415 | CIPHER_DECRYPT); | |
416 | cipher_crypt(&ciphercontext, cp, | |
20905a8e | 417 | buffer_ptr(&buffer), buffer_len(&buffer)); |
3ee832e5 | 418 | cipher_cleanup(&ciphercontext); |
94ec8c6b | 419 | memset(&ciphercontext, 0, sizeof(ciphercontext)); |
8efc0c15 | 420 | buffer_free(&buffer); |
5260325f | 421 | |
422 | check1 = buffer_get_char(&decrypted); | |
423 | check2 = buffer_get_char(&decrypted); | |
424 | if (check1 != buffer_get_char(&decrypted) || | |
425 | check2 != buffer_get_char(&decrypted)) { | |
426 | if (strcmp(passphrase, "") != 0) | |
aeaa3d9e | 427 | debug("Bad passphrase supplied for key file %.200s.", |
428 | filename); | |
5260325f | 429 | /* Bad passphrase. */ |
430 | buffer_free(&decrypted); | |
aeaa3d9e | 431 | goto fail; |
5260325f | 432 | } |
433 | /* Read the rest of the private key. */ | |
aeaa3d9e | 434 | buffer_get_bignum(&decrypted, prv->rsa->d); |
435 | buffer_get_bignum(&decrypted, prv->rsa->iqmp); /* u */ | |
436 | /* in SSL and SSH v1 p and q are exchanged */ | |
437 | buffer_get_bignum(&decrypted, prv->rsa->q); /* p */ | |
438 | buffer_get_bignum(&decrypted, prv->rsa->p); /* q */ | |
5260325f | 439 | |
aeaa3d9e | 440 | /* calculate p-1 and q-1 */ |
b775c6f2 | 441 | rsa_generate_additional_parameters(prv->rsa); |
5260325f | 442 | |
443 | buffer_free(&decrypted); | |
15e98f49 | 444 | |
445 | /* enable blinding */ | |
446 | if (RSA_blinding_on(prv->rsa, NULL) != 1) { | |
447 | error("key_load_private_rsa1: RSA_blinding_on failed"); | |
448 | goto fail; | |
449 | } | |
81333640 | 450 | close(fd); |
aeaa3d9e | 451 | return prv; |
452 | ||
453 | fail: | |
454 | if (commentp) | |
455 | xfree(*commentp); | |
456 | close(fd); | |
457 | key_free(prv); | |
458 | return NULL; | |
8efc0c15 | 459 | } |
a306f2dd | 460 | |
39c00dc2 | 461 | Key * |
aeaa3d9e | 462 | key_load_private_pem(int fd, int type, const char *passphrase, |
463 | char **commentp) | |
a306f2dd | 464 | { |
a306f2dd | 465 | FILE *fp; |
fa08c86b | 466 | EVP_PKEY *pk = NULL; |
aeaa3d9e | 467 | Key *prv = NULL; |
fa08c86b | 468 | char *name = "<no key>"; |
a306f2dd | 469 | |
a306f2dd | 470 | fp = fdopen(fd, "r"); |
471 | if (fp == NULL) { | |
58cfa257 | 472 | error("fdopen failed: %s", strerror(errno)); |
81333640 | 473 | close(fd); |
aeaa3d9e | 474 | return NULL; |
a306f2dd | 475 | } |
fa08c86b | 476 | pk = PEM_read_PrivateKey(fp, NULL, NULL, (char *)passphrase); |
477 | if (pk == NULL) { | |
ae88ea7e | 478 | debug("PEM_read_PrivateKey failed"); |
fa08c86b | 479 | (void)ERR_get_error(); |
aeaa3d9e | 480 | } else if (pk->type == EVP_PKEY_RSA && |
184eed6a | 481 | (type == KEY_UNSPEC||type==KEY_RSA)) { |
aeaa3d9e | 482 | prv = key_new(KEY_UNSPEC); |
483 | prv->rsa = EVP_PKEY_get1_RSA(pk); | |
484 | prv->type = KEY_RSA; | |
485 | name = "rsa w/o comment"; | |
fa08c86b | 486 | #ifdef DEBUG_PK |
aeaa3d9e | 487 | RSA_print_fp(stderr, prv->rsa, 8); |
fa08c86b | 488 | #endif |
15e98f49 | 489 | if (RSA_blinding_on(prv->rsa, NULL) != 1) { |
490 | error("key_load_private_pem: RSA_blinding_on failed"); | |
491 | key_free(prv); | |
492 | prv = NULL; | |
493 | } | |
aeaa3d9e | 494 | } else if (pk->type == EVP_PKEY_DSA && |
184eed6a | 495 | (type == KEY_UNSPEC||type==KEY_DSA)) { |
aeaa3d9e | 496 | prv = key_new(KEY_UNSPEC); |
497 | prv->dsa = EVP_PKEY_get1_DSA(pk); | |
498 | prv->type = KEY_DSA; | |
499 | name = "dsa w/o comment"; | |
fa08c86b | 500 | #ifdef DEBUG_PK |
aeaa3d9e | 501 | DSA_print_fp(stderr, prv->dsa, 8); |
fa08c86b | 502 | #endif |
fa08c86b | 503 | } else { |
504 | error("PEM_read_PrivateKey: mismatch or " | |
505 | "unknown EVP_PKEY save_type %d", pk->save_type); | |
a306f2dd | 506 | } |
a306f2dd | 507 | fclose(fp); |
fa08c86b | 508 | if (pk != NULL) |
509 | EVP_PKEY_free(pk); | |
aeaa3d9e | 510 | if (prv != NULL && commentp) |
511 | *commentp = xstrdup(name); | |
512 | debug("read PEM private key done: type %s", | |
513 | prv ? key_type(prv) : "<unknown>"); | |
514 | return prv; | |
a306f2dd | 515 | } |
516 | ||
45660a22 | 517 | int |
aeaa3d9e | 518 | key_perm_ok(int fd, const char *filename) |
a306f2dd | 519 | { |
a306f2dd | 520 | struct stat st; |
521 | ||
d842a5eb | 522 | if (fstat(fd, &st) < 0) |
523 | return 0; | |
524 | /* | |
525 | * if a key owned by the user is accessed, then we check the | |
526 | * permissions of the file. if the key owned by a different user, | |
527 | * then we don't care. | |
528 | */ | |
9cd45ea4 | 529 | #ifdef HAVE_CYGWIN |
22d89d24 | 530 | if (check_ntsec(filename)) |
9cd45ea4 | 531 | #endif |
d842a5eb | 532 | if ((st.st_uid == getuid()) && (st.st_mode & 077) != 0) { |
a306f2dd | 533 | error("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"); |
534 | error("@ WARNING: UNPROTECTED PRIVATE KEY FILE! @"); | |
535 | error("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"); | |
d842a5eb | 536 | error("Permissions 0%3.3o for '%s' are too open.", |
b9ed513a | 537 | (u_int)st.st_mode & 0777, filename); |
a306f2dd | 538 | error("It is recommended that your private key files are NOT accessible by others."); |
aeaa3d9e | 539 | error("This private key will be ignored."); |
a306f2dd | 540 | return 0; |
541 | } | |
aeaa3d9e | 542 | return 1; |
543 | } | |
81333640 | 544 | |
aeaa3d9e | 545 | Key * |
546 | key_load_private_type(int type, const char *filename, const char *passphrase, | |
fc231518 | 547 | char **commentp, int *perm_ok) |
aeaa3d9e | 548 | { |
549 | int fd; | |
550 | ||
551 | fd = open(filename, O_RDONLY); | |
552 | if (fd < 0) | |
553 | return NULL; | |
554 | if (!key_perm_ok(fd, filename)) { | |
fc231518 | 555 | if (perm_ok != NULL) |
556 | *perm_ok = 0; | |
58cfa257 | 557 | error("bad permissions: ignore key: %s", filename); |
aeaa3d9e | 558 | close(fd); |
559 | return NULL; | |
560 | } | |
fc231518 | 561 | if (perm_ok != NULL) |
562 | *perm_ok = 1; | |
aeaa3d9e | 563 | switch (type) { |
564 | case KEY_RSA1: | |
565 | return key_load_private_rsa1(fd, filename, passphrase, | |
566 | commentp); | |
567 | /* closes fd */ | |
a306f2dd | 568 | case KEY_DSA: |
fa08c86b | 569 | case KEY_RSA: |
570 | case KEY_UNSPEC: | |
aeaa3d9e | 571 | return key_load_private_pem(fd, type, passphrase, commentp); |
572 | /* closes fd */ | |
a306f2dd | 573 | default: |
81333640 | 574 | close(fd); |
a306f2dd | 575 | break; |
576 | } | |
aeaa3d9e | 577 | return NULL; |
578 | } | |
579 | ||
580 | Key * | |
581 | key_load_private(const char *filename, const char *passphrase, | |
582 | char **commentp) | |
583 | { | |
eb2e1595 | 584 | Key *pub, *prv; |
aeaa3d9e | 585 | int fd; |
586 | ||
587 | fd = open(filename, O_RDONLY); | |
588 | if (fd < 0) | |
589 | return NULL; | |
590 | if (!key_perm_ok(fd, filename)) { | |
58cfa257 | 591 | error("bad permissions: ignore key: %s", filename); |
aeaa3d9e | 592 | close(fd); |
593 | return NULL; | |
594 | } | |
595 | pub = key_load_public_rsa1(fd, filename, commentp); | |
596 | lseek(fd, (off_t) 0, SEEK_SET); /* rewind */ | |
597 | if (pub == NULL) { | |
598 | /* closes fd */ | |
eb2e1595 | 599 | prv = key_load_private_pem(fd, KEY_UNSPEC, passphrase, NULL); |
600 | /* use the filename as a comment for PEM */ | |
601 | if (commentp && prv) | |
a10bdd7c | 602 | *commentp = xstrdup(filename); |
aeaa3d9e | 603 | } else { |
604 | /* it's a SSH v1 key if the public key part is readable */ | |
605 | key_free(pub); | |
606 | /* closes fd */ | |
eb2e1595 | 607 | prv = key_load_private_rsa1(fd, filename, passphrase, NULL); |
aeaa3d9e | 608 | } |
eb2e1595 | 609 | return prv; |
a306f2dd | 610 | } |
bcbf86ec | 611 | |
396c147e | 612 | static int |
aeaa3d9e | 613 | key_try_load_public(Key *k, const char *filename, char **commentp) |
bcbf86ec | 614 | { |
615 | FILE *f; | |
ea067773 | 616 | char line[SSH_MAX_PUBKEY_BYTES]; |
bcbf86ec | 617 | char *cp; |
41feb690 | 618 | u_long linenum = 0; |
bcbf86ec | 619 | |
620 | f = fopen(filename, "r"); | |
621 | if (f != NULL) { | |
ea067773 | 622 | while (read_keyfile_line(f, filename, line, sizeof(line), |
623 | &linenum) != -1) { | |
bcbf86ec | 624 | cp = line; |
6aacefa7 | 625 | switch (*cp) { |
bcbf86ec | 626 | case '#': |
627 | case '\n': | |
628 | case '\0': | |
629 | continue; | |
630 | } | |
631 | /* Skip leading whitespace. */ | |
632 | for (; *cp && (*cp == ' ' || *cp == '\t'); cp++) | |
633 | ; | |
634 | if (*cp) { | |
fa08c86b | 635 | if (key_read(k, &cp) == 1) { |
bcbf86ec | 636 | if (commentp) |
637 | *commentp=xstrdup(filename); | |
638 | fclose(f); | |
639 | return 1; | |
640 | } | |
641 | } | |
642 | } | |
643 | fclose(f); | |
644 | } | |
645 | return 0; | |
646 | } | |
647 | ||
aeaa3d9e | 648 | /* load public key from ssh v1 private or any pubkey file */ |
649 | Key * | |
650 | key_load_public(const char *filename, char **commentp) | |
bcbf86ec | 651 | { |
aeaa3d9e | 652 | Key *pub; |
653 | char file[MAXPATHLEN]; | |
654 | ||
3566c73c | 655 | /* try rsa1 private key */ |
aeaa3d9e | 656 | pub = key_load_public_type(KEY_RSA1, filename, commentp); |
657 | if (pub != NULL) | |
658 | return pub; | |
3566c73c | 659 | |
660 | /* try rsa1 public key */ | |
661 | pub = key_new(KEY_RSA1); | |
662 | if (key_try_load_public(pub, filename, commentp) == 1) | |
663 | return pub; | |
664 | key_free(pub); | |
665 | ||
666 | /* try ssh2 public key */ | |
aeaa3d9e | 667 | pub = key_new(KEY_UNSPEC); |
668 | if (key_try_load_public(pub, filename, commentp) == 1) | |
669 | return pub; | |
670 | if ((strlcpy(file, filename, sizeof file) < sizeof(file)) && | |
671 | (strlcat(file, ".pub", sizeof file) < sizeof(file)) && | |
672 | (key_try_load_public(pub, file, commentp) == 1)) | |
673 | return pub; | |
674 | key_free(pub); | |
675 | return NULL; | |
bcbf86ec | 676 | } |