diff options
Diffstat (limited to 'sshkey.c')
-rw-r--r-- | sshkey.c | 37 |
1 files changed, 14 insertions, 23 deletions
@@ -1,4 +1,4 @@ | |||
1 | /* $OpenBSD: sshkey.c,v 1.35 2016/06/19 07:48:02 djm Exp $ */ | 1 | /* $OpenBSD: sshkey.c,v 1.36 2016/08/03 05:41:57 djm Exp $ */ |
2 | /* | 2 | /* |
3 | * Copyright (c) 2000, 2001 Markus Friedl. All rights reserved. | 3 | * Copyright (c) 2000, 2001 Markus Friedl. All rights reserved. |
4 | * Copyright (c) 2008 Alexander von Gernler. All rights reserved. | 4 | * Copyright (c) 2008 Alexander von Gernler. All rights reserved. |
@@ -3029,13 +3029,11 @@ sshkey_private_to_blob2(const struct sshkey *prv, struct sshbuf *blob, | |||
3029 | size_t i, pubkeylen, keylen, ivlen, blocksize, authlen; | 3029 | size_t i, pubkeylen, keylen, ivlen, blocksize, authlen; |
3030 | u_int check; | 3030 | u_int check; |
3031 | int r = SSH_ERR_INTERNAL_ERROR; | 3031 | int r = SSH_ERR_INTERNAL_ERROR; |
3032 | struct sshcipher_ctx ciphercontext; | 3032 | struct sshcipher_ctx *ciphercontext = NULL; |
3033 | const struct sshcipher *cipher; | 3033 | const struct sshcipher *cipher; |
3034 | const char *kdfname = KDFNAME; | 3034 | const char *kdfname = KDFNAME; |
3035 | struct sshbuf *encoded = NULL, *encrypted = NULL, *kdf = NULL; | 3035 | struct sshbuf *encoded = NULL, *encrypted = NULL, *kdf = NULL; |
3036 | 3036 | ||
3037 | memset(&ciphercontext, 0, sizeof(ciphercontext)); | ||
3038 | |||
3039 | if (rounds <= 0) | 3037 | if (rounds <= 0) |
3040 | rounds = DEFAULT_ROUNDS; | 3038 | rounds = DEFAULT_ROUNDS; |
3041 | if (passphrase == NULL || !strlen(passphrase)) { | 3039 | if (passphrase == NULL || !strlen(passphrase)) { |
@@ -3122,7 +3120,7 @@ sshkey_private_to_blob2(const struct sshkey *prv, struct sshbuf *blob, | |||
3122 | if ((r = sshbuf_reserve(encoded, | 3120 | if ((r = sshbuf_reserve(encoded, |
3123 | sshbuf_len(encrypted) + authlen, &cp)) != 0) | 3121 | sshbuf_len(encrypted) + authlen, &cp)) != 0) |
3124 | goto out; | 3122 | goto out; |
3125 | if ((r = cipher_crypt(&ciphercontext, 0, cp, | 3123 | if ((r = cipher_crypt(ciphercontext, 0, cp, |
3126 | sshbuf_ptr(encrypted), sshbuf_len(encrypted), 0, authlen)) != 0) | 3124 | sshbuf_ptr(encrypted), sshbuf_len(encrypted), 0, authlen)) != 0) |
3127 | goto out; | 3125 | goto out; |
3128 | 3126 | ||
@@ -3154,7 +3152,7 @@ sshkey_private_to_blob2(const struct sshkey *prv, struct sshbuf *blob, | |||
3154 | sshbuf_free(kdf); | 3152 | sshbuf_free(kdf); |
3155 | sshbuf_free(encoded); | 3153 | sshbuf_free(encoded); |
3156 | sshbuf_free(encrypted); | 3154 | sshbuf_free(encrypted); |
3157 | cipher_cleanup(&ciphercontext); | 3155 | cipher_free(ciphercontext); |
3158 | explicit_bzero(salt, sizeof(salt)); | 3156 | explicit_bzero(salt, sizeof(salt)); |
3159 | if (key != NULL) { | 3157 | if (key != NULL) { |
3160 | explicit_bzero(key, keylen + ivlen); | 3158 | explicit_bzero(key, keylen + ivlen); |
@@ -3183,12 +3181,11 @@ sshkey_parse_private2(struct sshbuf *blob, int type, const char *passphrase, | |||
3183 | size_t i, keylen = 0, ivlen = 0, authlen = 0, slen = 0; | 3181 | size_t i, keylen = 0, ivlen = 0, authlen = 0, slen = 0; |
3184 | struct sshbuf *encoded = NULL, *decoded = NULL; | 3182 | struct sshbuf *encoded = NULL, *decoded = NULL; |
3185 | struct sshbuf *kdf = NULL, *decrypted = NULL; | 3183 | struct sshbuf *kdf = NULL, *decrypted = NULL; |
3186 | struct sshcipher_ctx ciphercontext; | 3184 | struct sshcipher_ctx *ciphercontext = NULL; |
3187 | struct sshkey *k = NULL; | 3185 | struct sshkey *k = NULL; |
3188 | u_char *key = NULL, *salt = NULL, *dp, pad, last; | 3186 | u_char *key = NULL, *salt = NULL, *dp, pad, last; |
3189 | u_int blocksize, rounds, nkeys, encrypted_len, check1, check2; | 3187 | u_int blocksize, rounds, nkeys, encrypted_len, check1, check2; |
3190 | 3188 | ||
3191 | memset(&ciphercontext, 0, sizeof(ciphercontext)); | ||
3192 | if (keyp != NULL) | 3189 | if (keyp != NULL) |
3193 | *keyp = NULL; | 3190 | *keyp = NULL; |
3194 | if (commentp != NULL) | 3191 | if (commentp != NULL) |
@@ -3317,7 +3314,7 @@ sshkey_parse_private2(struct sshbuf *blob, int type, const char *passphrase, | |||
3317 | (r = cipher_init(&ciphercontext, cipher, key, keylen, | 3314 | (r = cipher_init(&ciphercontext, cipher, key, keylen, |
3318 | key + keylen, ivlen, 0)) != 0) | 3315 | key + keylen, ivlen, 0)) != 0) |
3319 | goto out; | 3316 | goto out; |
3320 | if ((r = cipher_crypt(&ciphercontext, 0, dp, sshbuf_ptr(decoded), | 3317 | if ((r = cipher_crypt(ciphercontext, 0, dp, sshbuf_ptr(decoded), |
3321 | encrypted_len, 0, authlen)) != 0) { | 3318 | encrypted_len, 0, authlen)) != 0) { |
3322 | /* an integrity error here indicates an incorrect passphrase */ | 3319 | /* an integrity error here indicates an incorrect passphrase */ |
3323 | if (r == SSH_ERR_MAC_INVALID) | 3320 | if (r == SSH_ERR_MAC_INVALID) |
@@ -3371,7 +3368,7 @@ sshkey_parse_private2(struct sshbuf *blob, int type, const char *passphrase, | |||
3371 | } | 3368 | } |
3372 | out: | 3369 | out: |
3373 | pad = 0; | 3370 | pad = 0; |
3374 | cipher_cleanup(&ciphercontext); | 3371 | cipher_free(ciphercontext); |
3375 | free(ciphername); | 3372 | free(ciphername); |
3376 | free(kdfname); | 3373 | free(kdfname); |
3377 | free(comment); | 3374 | free(comment); |
@@ -3405,7 +3402,7 @@ sshkey_private_rsa1_to_blob(struct sshkey *key, struct sshbuf *blob, | |||
3405 | struct sshbuf *buffer = NULL, *encrypted = NULL; | 3402 | struct sshbuf *buffer = NULL, *encrypted = NULL; |
3406 | u_char buf[8]; | 3403 | u_char buf[8]; |
3407 | int r, cipher_num; | 3404 | int r, cipher_num; |
3408 | struct sshcipher_ctx ciphercontext; | 3405 | struct sshcipher_ctx *ciphercontext = NULL; |
3409 | const struct sshcipher *cipher; | 3406 | const struct sshcipher *cipher; |
3410 | u_char *cp; | 3407 | u_char *cp; |
3411 | 3408 | ||
@@ -3475,16 +3472,14 @@ sshkey_private_rsa1_to_blob(struct sshkey *key, struct sshbuf *blob, | |||
3475 | if ((r = cipher_set_key_string(&ciphercontext, cipher, passphrase, | 3472 | if ((r = cipher_set_key_string(&ciphercontext, cipher, passphrase, |
3476 | CIPHER_ENCRYPT)) != 0) | 3473 | CIPHER_ENCRYPT)) != 0) |
3477 | goto out; | 3474 | goto out; |
3478 | if ((r = cipher_crypt(&ciphercontext, 0, cp, | 3475 | if ((r = cipher_crypt(ciphercontext, 0, cp, |
3479 | sshbuf_ptr(buffer), sshbuf_len(buffer), 0, 0)) != 0) | 3476 | sshbuf_ptr(buffer), sshbuf_len(buffer), 0, 0)) != 0) |
3480 | goto out; | 3477 | goto out; |
3481 | if ((r = cipher_cleanup(&ciphercontext)) != 0) | ||
3482 | goto out; | ||
3483 | 3478 | ||
3484 | r = sshbuf_putb(blob, encrypted); | 3479 | r = sshbuf_putb(blob, encrypted); |
3485 | 3480 | ||
3486 | out: | 3481 | out: |
3487 | explicit_bzero(&ciphercontext, sizeof(ciphercontext)); | 3482 | cipher_free(ciphercontext); |
3488 | explicit_bzero(buf, sizeof(buf)); | 3483 | explicit_bzero(buf, sizeof(buf)); |
3489 | sshbuf_free(buffer); | 3484 | sshbuf_free(buffer); |
3490 | sshbuf_free(encrypted); | 3485 | sshbuf_free(encrypted); |
@@ -3654,7 +3649,7 @@ sshkey_parse_private_rsa1(struct sshbuf *blob, const char *passphrase, | |||
3654 | struct sshbuf *decrypted = NULL, *copy = NULL; | 3649 | struct sshbuf *decrypted = NULL, *copy = NULL; |
3655 | u_char *cp; | 3650 | u_char *cp; |
3656 | char *comment = NULL; | 3651 | char *comment = NULL; |
3657 | struct sshcipher_ctx ciphercontext; | 3652 | struct sshcipher_ctx *ciphercontext = NULL; |
3658 | const struct sshcipher *cipher; | 3653 | const struct sshcipher *cipher; |
3659 | struct sshkey *prv = NULL; | 3654 | struct sshkey *prv = NULL; |
3660 | 3655 | ||
@@ -3712,12 +3707,8 @@ sshkey_parse_private_rsa1(struct sshbuf *blob, const char *passphrase, | |||
3712 | if ((r = cipher_set_key_string(&ciphercontext, cipher, passphrase, | 3707 | if ((r = cipher_set_key_string(&ciphercontext, cipher, passphrase, |
3713 | CIPHER_DECRYPT)) != 0) | 3708 | CIPHER_DECRYPT)) != 0) |
3714 | goto out; | 3709 | goto out; |
3715 | if ((r = cipher_crypt(&ciphercontext, 0, cp, | 3710 | if ((r = cipher_crypt(ciphercontext, 0, cp, |
3716 | sshbuf_ptr(copy), sshbuf_len(copy), 0, 0)) != 0) { | 3711 | sshbuf_ptr(copy), sshbuf_len(copy), 0, 0)) != 0) |
3717 | cipher_cleanup(&ciphercontext); | ||
3718 | goto out; | ||
3719 | } | ||
3720 | if ((r = cipher_cleanup(&ciphercontext)) != 0) | ||
3721 | goto out; | 3712 | goto out; |
3722 | 3713 | ||
3723 | if ((r = sshbuf_get_u16(decrypted, &check1)) != 0 || | 3714 | if ((r = sshbuf_get_u16(decrypted, &check1)) != 0 || |
@@ -3754,7 +3745,7 @@ sshkey_parse_private_rsa1(struct sshbuf *blob, const char *passphrase, | |||
3754 | comment = NULL; | 3745 | comment = NULL; |
3755 | } | 3746 | } |
3756 | out: | 3747 | out: |
3757 | explicit_bzero(&ciphercontext, sizeof(ciphercontext)); | 3748 | cipher_free(ciphercontext); |
3758 | free(comment); | 3749 | free(comment); |
3759 | sshkey_free(prv); | 3750 | sshkey_free(prv); |
3760 | sshbuf_free(copy); | 3751 | sshbuf_free(copy); |