@@ -519,7 +519,8 @@ func (hs *clientHandshakeState) doFullHandshake() error {
519
519
certRequested = true
520
520
hs .finishedHash .Write (certReq .marshal ())
521
521
522
- if chainToSend , err = hs .getCertificate (certReq ); err != nil {
522
+ cri := certificateRequestInfoFromMsg (certReq )
523
+ if chainToSend , err = c .getClientCertificate (cri ); err != nil {
523
524
c .sendAlert (alertInternalError )
524
525
return err
525
526
}
@@ -863,20 +864,15 @@ func (c *Conn) verifyServerCertificate(certificates [][]byte) error {
863
864
864
865
// tls11SignatureSchemes contains the signature schemes that we synthesise for
865
866
// a TLS <= 1.1 connection, based on the supported certificate types.
866
- var tls11SignatureSchemes = []SignatureScheme {ECDSAWithP256AndSHA256 , ECDSAWithP384AndSHA384 , ECDSAWithP521AndSHA512 , PKCS1WithSHA256 , PKCS1WithSHA384 , PKCS1WithSHA512 , PKCS1WithSHA1 }
867
-
868
- const (
869
- // tls11SignatureSchemesNumECDSA is the number of initial elements of
870
- // tls11SignatureSchemes that use ECDSA.
871
- tls11SignatureSchemesNumECDSA = 3
872
- // tls11SignatureSchemesNumRSA is the number of trailing elements of
873
- // tls11SignatureSchemes that use RSA.
874
- tls11SignatureSchemesNumRSA = 4
867
+ var (
868
+ tls11SignatureSchemes = []SignatureScheme {ECDSAWithP256AndSHA256 , ECDSAWithP384AndSHA384 , ECDSAWithP521AndSHA512 , PKCS1WithSHA256 , PKCS1WithSHA384 , PKCS1WithSHA512 , PKCS1WithSHA1 }
869
+ tls11SignatureSchemesECDSA = tls11SignatureSchemes [:3 ]
870
+ tls11SignatureSchemesRSA = tls11SignatureSchemes [3 :]
875
871
)
876
872
877
- func ( hs * clientHandshakeState ) getCertificate ( certReq * certificateRequestMsg ) ( * Certificate , error ) {
878
- c := hs . c
879
-
873
+ // certificateRequestInfoFromMsg generates a CertificateRequestInfo from a TLS
874
+ // <= 1.2 CertificateRequest, making an effort to fill in missing information.
875
+ func certificateRequestInfoFromMsg ( certReq * certificateRequestMsg ) * CertificateRequestInfo {
880
876
var rsaAvail , ecdsaAvail bool
881
877
for _ , certType := range certReq .certificateTypes {
882
878
switch certType {
@@ -887,77 +883,84 @@ func (hs *clientHandshakeState) getCertificate(certReq *certificateRequestMsg) (
887
883
}
888
884
}
889
885
890
- if c .config .GetClientCertificate != nil {
891
- var signatureSchemes []SignatureScheme
892
-
893
- if ! certReq .hasSignatureAlgorithm {
894
- // Prior to TLS 1.2, the signature schemes were not
895
- // included in the certificate request message. In this
896
- // case we use a plausible list based on the acceptable
897
- // certificate types.
898
- signatureSchemes = tls11SignatureSchemes
899
- if ! ecdsaAvail {
900
- signatureSchemes = signatureSchemes [tls11SignatureSchemesNumECDSA :]
886
+ cri := & CertificateRequestInfo {
887
+ AcceptableCAs : certReq .certificateAuthorities ,
888
+ }
889
+
890
+ if ! certReq .hasSignatureAlgorithm {
891
+ // Prior to TLS 1.2, the signature schemes were not
892
+ // included in the certificate request message. In this
893
+ // case we use a plausible list based on the acceptable
894
+ // certificate types.
895
+ switch {
896
+ case rsaAvail && ecdsaAvail :
897
+ cri .SignatureSchemes = tls11SignatureSchemes
898
+ case rsaAvail :
899
+ cri .SignatureSchemes = tls11SignatureSchemesRSA
900
+ case ecdsaAvail :
901
+ cri .SignatureSchemes = tls11SignatureSchemesECDSA
902
+ }
903
+ return cri
904
+ }
905
+
906
+ // In TLS 1.2, the signature schemes apply to both the certificate chain and
907
+ // the leaf key, while the certificate types only apply to the leaf key.
908
+ // See RFC 5246, Section 7.4.4 (where it calls this "somewhat complicated").
909
+ // Filter the signature schemes based on the certificate type.
910
+ cri .SignatureSchemes = make ([]SignatureScheme , 0 , len (certReq .supportedSignatureAlgorithms ))
911
+ for _ , sigScheme := range certReq .supportedSignatureAlgorithms {
912
+ switch signatureFromSignatureScheme (sigScheme ) {
913
+ case signatureECDSA :
914
+ if ecdsaAvail {
915
+ cri .SignatureSchemes = append (cri .SignatureSchemes , sigScheme )
901
916
}
902
- if ! rsaAvail {
903
- signatureSchemes = signatureSchemes [:len (signatureSchemes )- tls11SignatureSchemesNumRSA ]
917
+ case signatureRSAPSS , signaturePKCS1v15 :
918
+ if rsaAvail {
919
+ cri .SignatureSchemes = append (cri .SignatureSchemes , sigScheme )
904
920
}
905
- } else {
906
- signatureSchemes = certReq .supportedSignatureAlgorithms
907
921
}
908
-
909
- return c .config .GetClientCertificate (& CertificateRequestInfo {
910
- AcceptableCAs : certReq .certificateAuthorities ,
911
- SignatureSchemes : signatureSchemes ,
912
- })
913
922
}
914
923
915
- // RFC 4346 on the certificateAuthorities field: A list of the
916
- // distinguished names of acceptable certificate authorities.
917
- // These distinguished names may specify a desired
918
- // distinguished name for a root CA or for a subordinate CA;
919
- // thus, this message can be used to describe both known roots
920
- // and a desired authorization space. If the
921
- // certificate_authorities list is empty then the client MAY
922
- // send any certificate of the appropriate
923
- // ClientCertificateType, unless there is some external
924
- // arrangement to the contrary.
924
+ return cri
925
+ }
926
+
927
+ func (c * Conn ) getClientCertificate (cri * CertificateRequestInfo ) (* Certificate , error ) {
928
+ if c .config .GetClientCertificate != nil {
929
+ return c .config .GetClientCertificate (cri )
930
+ }
925
931
926
932
// We need to search our list of client certs for one
927
933
// where SignatureAlgorithm is acceptable to the server and the
928
- // Issuer is in certReq.certificateAuthorities
929
- findCert:
934
+ // Issuer is in AcceptableCAs.
930
935
for i , chain := range c .config .Certificates {
931
- if ! rsaAvail && ! ecdsaAvail {
936
+ sigOK := false
937
+ for _ , alg := range signatureSchemesForCertificate (& chain ) {
938
+ if isSupportedSignatureAlgorithm (alg , cri .SignatureSchemes ) {
939
+ sigOK = true
940
+ break
941
+ }
942
+ }
943
+ if ! sigOK {
932
944
continue
933
945
}
934
946
947
+ if len (cri .AcceptableCAs ) == 0 {
948
+ return & chain , nil
949
+ }
950
+
935
951
for j , cert := range chain .Certificate {
936
952
x509Cert := chain .Leaf
937
- // parse the certificate if this isn't the leaf
938
- // node, or if chain.Leaf was nil
953
+ // Parse the certificate if this isn't the leaf node, or if
954
+ // chain.Leaf was nil.
939
955
if j != 0 || x509Cert == nil {
940
956
var err error
941
957
if x509Cert , err = x509 .ParseCertificate (cert ); err != nil {
942
958
c .sendAlert (alertInternalError )
943
- return nil , errors .New ("tls: failed to parse client certificate #" + strconv .Itoa (i ) + ": " + err .Error ())
959
+ return nil , errors .New ("tls: failed to parse configured certificate chain #" + strconv .Itoa (i ) + ": " + err .Error ())
944
960
}
945
961
}
946
962
947
- switch {
948
- case rsaAvail && x509Cert .PublicKeyAlgorithm == x509 .RSA :
949
- case ecdsaAvail && x509Cert .PublicKeyAlgorithm == x509 .ECDSA :
950
- default :
951
- continue findCert
952
- }
953
-
954
- if len (certReq .certificateAuthorities ) == 0 {
955
- // they gave us an empty list, so just take the
956
- // first cert from c.config.Certificates
957
- return & chain , nil
958
- }
959
-
960
- for _ , ca := range certReq .certificateAuthorities {
963
+ for _ , ca := range cri .AcceptableCAs {
961
964
if bytes .Equal (x509Cert .RawIssuer , ca ) {
962
965
return & chain , nil
963
966
}
0 commit comments