diff --git a/libcfnet/client_protocol.c b/libcfnet/client_protocol.c index 110c91cd1b8..41f82ebf7a4 100644 --- a/libcfnet/client_protocol.c +++ b/libcfnet/client_protocol.c @@ -398,13 +398,34 @@ bool AuthenticateAgent(AgentConnection *conn, bool trust_key) return false; } - decrypted_cchall = xmalloc(encrypted_len); - - if (RSA_private_decrypt(encrypted_len, in, (unsigned char *) decrypted_cchall, PRIVKEY, RSA_PKCS1_PADDING) <= 0) + /* RSA_private_decrypt() can write up to RSA_size() bytes, however short + * the ciphertext the server sent us is. */ + decrypted_cchall = xmalloc(RSA_size(PRIVKEY)); + + const int decrypted_len = RSA_private_decrypt( + encrypted_len, + in, + (unsigned char *) decrypted_cchall, + PRIVKEY, + RSA_PKCS1_PADDING); + if (decrypted_len <= 0) { Log(LOG_LEVEL_ERR, "Private decrypt failed, abandoning. (RSA_private_decrypt: %s)", CryptoLastErrorString()); + free(decrypted_cchall); + RSA_free(server_pubkey); + return false; + } + + if (decrypted_len < nonce_len) + { + Log(LOG_LEVEL_ERR, + "Counter-challenge from server '%s' is too short (%d bytes, expected %d)", + conn->this_server, + decrypted_len, + nonce_len); + free(decrypted_cchall); RSA_free(server_pubkey); return false; }