1 https://github.com/openssh/openssh-portable/commit/29e0ecd9b4eb3b9f305e2240351f0c59cad9ef81
5 @@ -3209,6 +3209,10 @@ sshkey_private_deserialize(struct sshbuf *buf, struct sshkey **kp)
6 if ((r = sshkey_froms(buf, &k)) != 0 ||
7 (r = sshbuf_get_bignum2(buf, &dsa_priv_key)) != 0)
9 + if (k->type != type) {
10 + r = SSH_ERR_INVALID_FORMAT;
13 if (!DSA_set0_key(k->dsa, NULL, dsa_priv_key)) {
14 r = SSH_ERR_LIBCRYPTO_ERROR;
16 @@ -3252,6 +3256,11 @@ sshkey_private_deserialize(struct sshbuf *buf, struct sshkey **kp)
17 if ((r = sshkey_froms(buf, &k)) != 0 ||
18 (r = sshbuf_get_bignum2(buf, &exponent)) != 0)
20 + if (k->type != type ||
21 + k->ecdsa_nid != sshkey_ecdsa_nid_from_name(tname)) {
22 + r = SSH_ERR_INVALID_FORMAT;
25 if (EC_KEY_set_private_key(k->ecdsa, exponent) != 1) {
26 r = SSH_ERR_LIBCRYPTO_ERROR;
28 @@ -3296,6 +3305,10 @@ sshkey_private_deserialize(struct sshbuf *buf, struct sshkey **kp)
29 (r = sshbuf_get_bignum2(buf, &rsa_p)) != 0 ||
30 (r = sshbuf_get_bignum2(buf, &rsa_q)) != 0)
32 + if (k->type != type) {
33 + r = SSH_ERR_INVALID_FORMAT;
36 if (!RSA_set0_key(k->rsa, NULL, NULL, rsa_d)) {
37 r = SSH_ERR_LIBCRYPTO_ERROR;
39 @@ -3333,13 +3346,17 @@ sshkey_private_deserialize(struct sshbuf *buf, struct sshkey **kp)
40 (r = sshbuf_get_string(buf, &ed25519_pk, &pklen)) != 0 ||
41 (r = sshbuf_get_string(buf, &ed25519_sk, &sklen)) != 0)
43 + if (k->type != type) {
44 + r = SSH_ERR_INVALID_FORMAT;
47 if (pklen != ED25519_PK_SZ || sklen != ED25519_SK_SZ) {
48 r = SSH_ERR_INVALID_FORMAT;
51 k->ed25519_pk = ed25519_pk;
52 k->ed25519_sk = ed25519_sk;
53 - ed25519_pk = ed25519_sk = NULL;
54 + ed25519_pk = ed25519_sk = NULL; /* transferred */
58 @@ -3370,7 +3387,7 @@ sshkey_private_deserialize(struct sshbuf *buf, struct sshkey **kp)
59 (r = sshbuf_get_string(buf, &xmss_pk, &pklen)) != 0 ||
60 (r = sshbuf_get_string(buf, &xmss_sk, &sklen)) != 0)
62 - if (strcmp(xmss_name, k->xmss_name)) {
63 + if (k->type != type || strcmp(xmss_name, k->xmss_name) != 0) {
64 r = SSH_ERR_INVALID_FORMAT;
67 @@ -3877,7 +3894,8 @@ sshkey_parse_private2(struct sshbuf *blob, int type, const char *passphrase,
70 /* check that an appropriate amount of auth data is present */
71 - if (sshbuf_len(decoded) < encrypted_len + authlen) {
72 + if (sshbuf_len(decoded) < authlen ||
73 + sshbuf_len(decoded) - authlen < encrypted_len) {
74 r = SSH_ERR_INVALID_FORMAT;