diff options
-rw-r--r-- | sshkey.c | 26 |
1 files changed, 22 insertions, 4 deletions
@@ -1,4 +1,4 @@ | |||
1 | /* $OpenBSD: sshkey.c,v 1.83 2019/09/06 05:23:55 djm Exp $ */ | 1 | /* $OpenBSD: sshkey.c,v 1.84 2019/10/09 00:04:42 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. |
@@ -3209,6 +3209,10 @@ sshkey_private_deserialize(struct sshbuf *buf, struct sshkey **kp) | |||
3209 | if ((r = sshkey_froms(buf, &k)) != 0 || | 3209 | if ((r = sshkey_froms(buf, &k)) != 0 || |
3210 | (r = sshbuf_get_bignum2(buf, &dsa_priv_key)) != 0) | 3210 | (r = sshbuf_get_bignum2(buf, &dsa_priv_key)) != 0) |
3211 | goto out; | 3211 | goto out; |
3212 | if (k->type != type) { | ||
3213 | r = SSH_ERR_INVALID_FORMAT; | ||
3214 | goto out; | ||
3215 | } | ||
3212 | if (!DSA_set0_key(k->dsa, NULL, dsa_priv_key)) { | 3216 | if (!DSA_set0_key(k->dsa, NULL, dsa_priv_key)) { |
3213 | r = SSH_ERR_LIBCRYPTO_ERROR; | 3217 | r = SSH_ERR_LIBCRYPTO_ERROR; |
3214 | goto out; | 3218 | goto out; |
@@ -3252,6 +3256,11 @@ sshkey_private_deserialize(struct sshbuf *buf, struct sshkey **kp) | |||
3252 | if ((r = sshkey_froms(buf, &k)) != 0 || | 3256 | if ((r = sshkey_froms(buf, &k)) != 0 || |
3253 | (r = sshbuf_get_bignum2(buf, &exponent)) != 0) | 3257 | (r = sshbuf_get_bignum2(buf, &exponent)) != 0) |
3254 | goto out; | 3258 | goto out; |
3259 | if (k->type != type || | ||
3260 | k->ecdsa_nid != sshkey_ecdsa_nid_from_name(tname)) { | ||
3261 | r = SSH_ERR_INVALID_FORMAT; | ||
3262 | goto out; | ||
3263 | } | ||
3255 | if (EC_KEY_set_private_key(k->ecdsa, exponent) != 1) { | 3264 | if (EC_KEY_set_private_key(k->ecdsa, exponent) != 1) { |
3256 | r = SSH_ERR_LIBCRYPTO_ERROR; | 3265 | r = SSH_ERR_LIBCRYPTO_ERROR; |
3257 | goto out; | 3266 | goto out; |
@@ -3296,6 +3305,10 @@ sshkey_private_deserialize(struct sshbuf *buf, struct sshkey **kp) | |||
3296 | (r = sshbuf_get_bignum2(buf, &rsa_p)) != 0 || | 3305 | (r = sshbuf_get_bignum2(buf, &rsa_p)) != 0 || |
3297 | (r = sshbuf_get_bignum2(buf, &rsa_q)) != 0) | 3306 | (r = sshbuf_get_bignum2(buf, &rsa_q)) != 0) |
3298 | goto out; | 3307 | goto out; |
3308 | if (k->type != type) { | ||
3309 | r = SSH_ERR_INVALID_FORMAT; | ||
3310 | goto out; | ||
3311 | } | ||
3299 | if (!RSA_set0_key(k->rsa, NULL, NULL, rsa_d)) { | 3312 | if (!RSA_set0_key(k->rsa, NULL, NULL, rsa_d)) { |
3300 | r = SSH_ERR_LIBCRYPTO_ERROR; | 3313 | r = SSH_ERR_LIBCRYPTO_ERROR; |
3301 | goto out; | 3314 | goto out; |
@@ -3333,13 +3346,17 @@ sshkey_private_deserialize(struct sshbuf *buf, struct sshkey **kp) | |||
3333 | (r = sshbuf_get_string(buf, &ed25519_pk, &pklen)) != 0 || | 3346 | (r = sshbuf_get_string(buf, &ed25519_pk, &pklen)) != 0 || |
3334 | (r = sshbuf_get_string(buf, &ed25519_sk, &sklen)) != 0) | 3347 | (r = sshbuf_get_string(buf, &ed25519_sk, &sklen)) != 0) |
3335 | goto out; | 3348 | goto out; |
3349 | if (k->type != type) { | ||
3350 | r = SSH_ERR_INVALID_FORMAT; | ||
3351 | goto out; | ||
3352 | } | ||
3336 | if (pklen != ED25519_PK_SZ || sklen != ED25519_SK_SZ) { | 3353 | if (pklen != ED25519_PK_SZ || sklen != ED25519_SK_SZ) { |
3337 | r = SSH_ERR_INVALID_FORMAT; | 3354 | r = SSH_ERR_INVALID_FORMAT; |
3338 | goto out; | 3355 | goto out; |
3339 | } | 3356 | } |
3340 | k->ed25519_pk = ed25519_pk; | 3357 | k->ed25519_pk = ed25519_pk; |
3341 | k->ed25519_sk = ed25519_sk; | 3358 | k->ed25519_sk = ed25519_sk; |
3342 | ed25519_pk = ed25519_sk = NULL; | 3359 | ed25519_pk = ed25519_sk = NULL; /* transferred */ |
3343 | break; | 3360 | break; |
3344 | #ifdef WITH_XMSS | 3361 | #ifdef WITH_XMSS |
3345 | case KEY_XMSS: | 3362 | case KEY_XMSS: |
@@ -3370,7 +3387,7 @@ sshkey_private_deserialize(struct sshbuf *buf, struct sshkey **kp) | |||
3370 | (r = sshbuf_get_string(buf, &xmss_pk, &pklen)) != 0 || | 3387 | (r = sshbuf_get_string(buf, &xmss_pk, &pklen)) != 0 || |
3371 | (r = sshbuf_get_string(buf, &xmss_sk, &sklen)) != 0) | 3388 | (r = sshbuf_get_string(buf, &xmss_sk, &sklen)) != 0) |
3372 | goto out; | 3389 | goto out; |
3373 | if (strcmp(xmss_name, k->xmss_name)) { | 3390 | if (k->type != type || strcmp(xmss_name, k->xmss_name) != 0) { |
3374 | r = SSH_ERR_INVALID_FORMAT; | 3391 | r = SSH_ERR_INVALID_FORMAT; |
3375 | goto out; | 3392 | goto out; |
3376 | } | 3393 | } |
@@ -3877,7 +3894,8 @@ sshkey_parse_private2(struct sshbuf *blob, int type, const char *passphrase, | |||
3877 | } | 3894 | } |
3878 | 3895 | ||
3879 | /* check that an appropriate amount of auth data is present */ | 3896 | /* check that an appropriate amount of auth data is present */ |
3880 | if (sshbuf_len(decoded) < encrypted_len + authlen) { | 3897 | if (sshbuf_len(decoded) < authlen || |
3898 | sshbuf_len(decoded) - authlen < encrypted_len) { | ||
3881 | r = SSH_ERR_INVALID_FORMAT; | 3899 | r = SSH_ERR_INVALID_FORMAT; |
3882 | goto out; | 3900 | goto out; |
3883 | } | 3901 | } |