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