From: Roland Shoemaker Date: Wed, 9 Jun 2021 18:31:27 +0000 (-0700) Subject: [release-branch.go1.16] crypto/tls: test key type when casting X-Git-Tag: go1.16.6~1 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=58bc454a11d4b3dbc03f44dfcabb9068a9c076f4;p=gostls13.git [release-branch.go1.16] crypto/tls: test key type when casting When casting the certificate public key in generateClientKeyExchange, check the type is appropriate. This prevents a panic when a server agrees to a RSA based key exchange, but then sends an ECDSA (or other) certificate. Updates #47143 Fixes #47145 Fixes CVE-2021-34558 Thanks to Imre Rad for reporting this issue. Change-Id: Iabccacca6052769a605cccefa1216a9f7b7f6aea Reviewed-on: https://team-review.git.corp.google.com/c/golang/go-private/+/1116723 Reviewed-by: Filippo Valsorda Reviewed-by: Katie Hockman Reviewed-on: https://go-review.googlesource.com/c/go/+/334029 Trust: Filippo Valsorda Run-TryBot: Filippo Valsorda Reviewed-by: Dmitri Shuralyov TryBot-Result: Go Bot --- diff --git a/src/crypto/tls/key_agreement.go b/src/crypto/tls/key_agreement.go index 7e6534bd46..22f1b2e1f2 100644 --- a/src/crypto/tls/key_agreement.go +++ b/src/crypto/tls/key_agreement.go @@ -67,7 +67,11 @@ func (ka rsaKeyAgreement) generateClientKeyExchange(config *Config, clientHello return nil, nil, err } - encrypted, err := rsa.EncryptPKCS1v15(config.rand(), cert.PublicKey.(*rsa.PublicKey), preMasterSecret) + rsaKey, ok := cert.PublicKey.(*rsa.PublicKey) + if !ok { + return nil, nil, errors.New("tls: server certificate contains incorrect key type for selected ciphersuite") + } + encrypted, err := rsa.EncryptPKCS1v15(config.rand(), rsaKey, preMasterSecret) if err != nil { return nil, nil, err }