Debian Codegraph @

tls/key_agreement.go

raw symbols SCIP
1 // Copyright 2010 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 package tlsscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/
6
7 import (
8 "cryptoscip-go gomod github.com/golang/go/src go1.16 crypto/"
9 "crypto/ecdsascip-go gomod github.com/golang/go/src go1.16 `crypto/ecdsa`/"
10 "crypto/ellipticscip-go gomod github.com/golang/go/src go1.16 `crypto/elliptic`/"
11 "crypto/md5scip-go gomod github.com/golang/go/src go1.16 `crypto/md5`/"
12 "crypto/randscip-go gomod github.com/golang/go/src go1.16 `crypto/rand`/"
13 "crypto/rsascip-go gomod github.com/golang/go/src go1.16 `crypto/rsa`/"
14 "crypto/sha1scip-go gomod github.com/golang/go/src go1.16 `crypto/sha1`/"
15 "crypto/sha256scip-go gomod github.com/golang/go/src go1.16 `crypto/sha256`/"
16 "crypto/sha512scip-go gomod github.com/golang/go/src go1.16 `crypto/sha512`/"
17 "encoding/asn1scip-go gomod github.com/golang/go/src go1.16 `encoding/asn1`/"
18 "errorsscip-go gomod github.com/golang/go/src go1.16 errors/"
19 "ioscip-go gomod github.com/golang/go/src go1.16 io/"
20 "math/bigscip-go gomod github.com/golang/go/src go1.16 `math/big`/"
21
22 "github.com/zmap/zcrypto/dsascip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/dsa`/"
23
24 "github.com/zmap/zcrypto/x509scip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/x509`/"
25 )
26
27 var errClientKeyExchangescip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/errClientKeyExchange. = errorsscip-go gomod github.com/golang/go/src go1.16 errors/.Newscip-go gomod github.com/golang/go/src go1.16 errors/New().("tls: invalid ClientKeyExchange message")
28 var errServerKeyExchangescip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/errServerKeyExchange. = errorsscip-go gomod github.com/golang/go/src go1.16 errors/.Newscip-go gomod github.com/golang/go/src go1.16 errors/New().("tls: invalid ServerKeyExchange message")
29 var errUnexpectedServerKeyExchangescip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/errUnexpectedServerKeyExchange. = errorsscip-go gomod github.com/golang/go/src go1.16 errors/.Newscip-go gomod github.com/golang/go/src go1.16 errors/New().("tls: unexpected ServerKeyExchange message")
30
31 // rsaKeyAgreement implements the standard TLS key agreement where the client
32 // encrypts the pre-master secret to the server's public key.
33 type rsaKeyAgreementscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/rsaKeyAgreement# struct {
34 authscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/rsaKeyAgreement#auth. keyAgreementAuthenticationscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/keyAgreementAuthentication#
35 versionscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/rsaKeyAgreement#version. uint16
36 clientVersionscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/rsaKeyAgreement#clientVersion. uint16
37 ephemeralscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/rsaKeyAgreement#ephemeral. bool
38 privateKeyscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/rsaKeyAgreement#privateKey. *rsascip-go gomod github.com/golang/go/src go1.16 `crypto/rsa`/.PrivateKeyscip-go gomod github.com/golang/go/src go1.16 `crypto/rsa`/PrivateKey#
39 publicKeyscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/rsaKeyAgreement#publicKey. *rsascip-go gomod github.com/golang/go/src go1.16 `crypto/rsa`/.PublicKeyscip-go gomod github.com/golang/go/src go1.16 `crypto/rsa`/PublicKey#
40 verifyErrorscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/rsaKeyAgreement#verifyError. error
41 }
42
43 func (ka *rsaKeyAgreementscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/rsaKeyAgreement#) generateServerKeyExchangescip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/rsaKeyAgreement#generateServerKeyExchange().(config *Configscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/Config#, cert *Certificatescip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/Certificate#, clientHello *clientHelloMsgscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/clientHelloMsg#, hello *serverHelloMsgscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/serverHelloMsg#) (*serverKeyExchangeMsgscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/serverKeyExchangeMsg#, error) {
44 // Only send a server key agreement when the cipher is an RSA export
45 // TODO: Make this a configuration parameter
46 ka.clientVersionscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/rsaKeyAgreement#clientVersion. = clientHello.versscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/clientHelloMsg#vers.
47 if !ka.ephemeralscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/rsaKeyAgreement#ephemeral. {
48 return nil, nil
49 }
50
51 // Generate an ephemeral RSA key or use the one in the config
52 if config.ExportRSAKeyscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/Config#ExportRSAKey. != nil {
53 ka.privateKeyscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/rsaKeyAgreement#privateKey. = config.ExportRSAKeyscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/Config#ExportRSAKey.
54 } else {
55 key, err := rsascip-go gomod github.com/golang/go/src go1.16 `crypto/rsa`/.GenerateKeyscip-go gomod github.com/golang/go/src go1.16 `crypto/rsa`/GenerateKey().(config.randscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/Config#rand().(), 512)
56 if err != nil {
57 return nil, err
58 }
59 ka.privateKeyscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/rsaKeyAgreement#privateKey. = key
60 }
61
62 // Serialize the key parameters to a nice byte array. The byte array can be
63 // positioned later.
64 modulus := ka.privateKeyscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/rsaKeyAgreement#privateKey..Nscip-go gomod github.com/golang/go/src go1.16 `crypto/rsa`/PublicKey#N..Bytesscip-go gomod github.com/golang/go/src go1.16 `math/big`/Int#Bytes().()
65 exponent := bigscip-go gomod github.com/golang/go/src go1.16 `math/big`/.NewIntscip-go gomod github.com/golang/go/src go1.16 `math/big`/NewInt().(int64(ka.privateKeyscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/rsaKeyAgreement#privateKey..Escip-go gomod github.com/golang/go/src go1.16 `crypto/rsa`/PublicKey#E.)).Bytesscip-go gomod github.com/golang/go/src go1.16 `math/big`/Int#Bytes().()
66 serverRSAParams := make([]byte, 0, 2+len(modulus)+2+len(exponent))
67 serverRSAParams = append(serverRSAParams, byte(len(modulus)>>8), byte(len(modulus)))
68 serverRSAParams = append(serverRSAParams, modulus...)
69 serverRSAParams = append(serverRSAParams, byte(len(exponent)>>8), byte(len(exponent)))
70 serverRSAParams = append(serverRSAParams, exponent...)
71
72 return ka.authscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/rsaKeyAgreement#auth..signParametersscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/keyAgreementAuthentication#signParameters.(config, cert, clientHello, hello, serverRSAParams)
73 }
74
75 func (ka *rsaKeyAgreementscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/rsaKeyAgreement#) processClientKeyExchangescip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/rsaKeyAgreement#processClientKeyExchange().(config *Configscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/Config#, cert *Certificatescip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/Certificate#, ckx *clientKeyExchangeMsgscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/clientKeyExchangeMsg#) ([]byte, error) {
76 preMasterSecret := make([]byte, 48)
77 _, err := ioscip-go gomod github.com/golang/go/src go1.16 io/.ReadFullscip-go gomod github.com/golang/go/src go1.16 io/ReadFull().(config.randscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/Config#rand().(), preMasterSecret[2:])
78 if err != nil {
79 return nil, err
80 }
81
82 if len(ckx.ciphertextscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/clientKeyExchangeMsg#ciphertext.) < 2 {
83 return nil, errClientKeyExchangescip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/errClientKeyExchange.
84 }
85
86 ciphertext := ckx.ciphertextscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/clientKeyExchangeMsg#ciphertext.
87 if ka.versionscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/rsaKeyAgreement#version. != VersionSSL30scip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/VersionSSL30. {
88 ciphertextLen := int(ckx.ciphertextscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/clientKeyExchangeMsg#ciphertext.[0])<<8 | int(ckx.ciphertextscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/clientKeyExchangeMsg#ciphertext.[1])
89 if ciphertextLen != len(ckx.ciphertextscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/clientKeyExchangeMsg#ciphertext.)-2 {
90 return nil, errClientKeyExchangescip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/errClientKeyExchange.
91 }
92 ciphertext = ckx.ciphertextscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/clientKeyExchangeMsg#ciphertext.[2:]
93 }
94
95 key := ka.privateKeyscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/rsaKeyAgreement#privateKey.
96 if key == nil {
97 key = cert.PrivateKeyscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/Certificate#PrivateKey..(*rsascip-go gomod github.com/golang/go/src go1.16 `crypto/rsa`/.PrivateKeyscip-go gomod github.com/golang/go/src go1.16 `crypto/rsa`/PrivateKey#)
98 }
99
100 err = rsascip-go gomod github.com/golang/go/src go1.16 `crypto/rsa`/.DecryptPKCS1v15SessionKeyscip-go gomod github.com/golang/go/src go1.16 `crypto/rsa`/DecryptPKCS1v15SessionKey().(config.randscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/Config#rand().(), key, ciphertext, preMasterSecret)
101 if err != nil {
102 return nil, err
103 }
104 // We don't check the version number in the premaster secret. For one,
105 // by checking it, we would leak information about the validity of the
106 // encrypted pre-master secret. Secondly, it provides only a small
107 // benefit against a downgrade attack and some implementations send the
108 // wrong version anyway. See the discussion at the end of section
109 // 7.4.7.1 of RFC 4346.
110 return preMasterSecret, nil
111 }
112
113 func (ka *rsaKeyAgreementscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/rsaKeyAgreement#) processServerKeyExchangescip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/rsaKeyAgreement#processServerKeyExchange().(config *Configscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/Config#, clientHello *clientHelloMsgscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/clientHelloMsg#, serverHello *serverHelloMsgscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/serverHelloMsg#, cert *x509scip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/x509`/.Certificatescip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/x509`/Certificate#, skx *serverKeyExchangeMsgscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/serverKeyExchangeMsg#) error {
114 if !ka.ephemeralscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/rsaKeyAgreement#ephemeral. {
115 return nil
116 }
117
118 k := skx.keyscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/serverKeyExchangeMsg#key.
119 // Read the modulus
120 if len(k) < 2 {
121 return errServerKeyExchangescip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/errServerKeyExchange.
122 }
123 modulusLen := (int(k[0]) << 8) | int(k[1])
124 k = k[2:]
125 if len(k) < modulusLen {
126 return errServerKeyExchangescip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/errServerKeyExchange.
127 }
128 modulus := new(bigscip-go gomod github.com/golang/go/src go1.16 `math/big`/.Intscip-go gomod github.com/golang/go/src go1.16 `math/big`/Int#).SetBytesscip-go gomod github.com/golang/go/src go1.16 `math/big`/Int#SetBytes().(k[:modulusLen])
129 k = k[modulusLen:]
130
131 // Read the exponent
132 if len(k) < 2 {
133 return errServerKeyExchangescip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/errServerKeyExchange.
134 }
135 exponentLength := (int(k[0]) << 8) | int(k[1])
136 k = k[2:]
137 if len(k) < exponentLength || exponentLength > 4 {
138 return errServerKeyExchangescip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/errServerKeyExchange.
139 }
140 rawExponent := k[0:exponentLength]
141 exponent := 0
142 for _, b := range rawExponent {
143 exponent <<= 8
144 exponent |= int(b)
145 }
146 ka.publicKeyscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/rsaKeyAgreement#publicKey. = new(rsascip-go gomod github.com/golang/go/src go1.16 `crypto/rsa`/.PublicKeyscip-go gomod github.com/golang/go/src go1.16 `crypto/rsa`/PublicKey#)
147 ka.publicKeyscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/rsaKeyAgreement#publicKey..Escip-go gomod github.com/golang/go/src go1.16 `crypto/rsa`/PublicKey#E. = exponent
148 ka.publicKeyscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/rsaKeyAgreement#publicKey..Nscip-go gomod github.com/golang/go/src go1.16 `crypto/rsa`/PublicKey#N. = modulus
149
150 paramsLen := 2 + exponentLength + 2 + modulusLen
151
152 serverRSAParams := skx.keyscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/serverKeyExchangeMsg#key.[:paramsLen]
153 sig := skx.keyscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/serverKeyExchangeMsg#key.[paramsLen:]
154
155 skx.digestscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/serverKeyExchangeMsg#digest., ka.verifyErrorscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/rsaKeyAgreement#verifyError. = ka.authscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/rsaKeyAgreement#auth..verifyParametersscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/keyAgreementAuthentication#verifyParameters.(config, clientHello, serverHello, cert, serverRSAParams, sig)
156 if config.InsecureSkipVerifyscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/Config#InsecureSkipVerify. {
157 return nil
158 }
159 return ka.verifyErrorscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/rsaKeyAgreement#verifyError.
160 }
161
162 func (ka *rsaKeyAgreementscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/rsaKeyAgreement#) generateClientKeyExchangescip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/rsaKeyAgreement#generateClientKeyExchange().(config *Configscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/Config#, clientHello *clientHelloMsgscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/clientHelloMsg#, cert *x509scip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/x509`/.Certificatescip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/x509`/Certificate#) ([]byte, *clientKeyExchangeMsgscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/clientKeyExchangeMsg#, error) {
163 preMasterSecret := make([]byte, 48)
164 preMasterSecret[0] = byte(clientHello.versscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/clientHelloMsg#vers. >> 8)
165 preMasterSecret[1] = byte(clientHello.versscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/clientHelloMsg#vers.)
166 _, err := ioscip-go gomod github.com/golang/go/src go1.16 io/.ReadFullscip-go gomod github.com/golang/go/src go1.16 io/ReadFull().(config.randscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/Config#rand().(), preMasterSecret[2:])
167 if err != nil {
168 return nil, nil, err
169 }
170 var publicKey *rsascip-go gomod github.com/golang/go/src go1.16 `crypto/rsa`/.PublicKeyscip-go gomod github.com/golang/go/src go1.16 `crypto/rsa`/PublicKey#
171 if ka.publicKeyscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/rsaKeyAgreement#publicKey. != nil {
172 publicKey = ka.publicKeyscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/rsaKeyAgreement#publicKey.
173 } else {
174 var ok bool
175 publicKey, ok = cert.PublicKeyscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/x509`/Certificate#PublicKey..(*rsascip-go gomod github.com/golang/go/src go1.16 `crypto/rsa`/.PublicKeyscip-go gomod github.com/golang/go/src go1.16 `crypto/rsa`/PublicKey#)
176 if !ok {
177 return nil, nil, errClientKeyExchangescip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/errClientKeyExchange.
178 }
179 }
180 encrypted, err := rsascip-go gomod github.com/golang/go/src go1.16 `crypto/rsa`/.EncryptPKCS1v15scip-go gomod github.com/golang/go/src go1.16 `crypto/rsa`/EncryptPKCS1v15().(config.randscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/Config#rand().(), publicKey, preMasterSecret)
181 if err != nil {
182 return nil, nil, err
183 }
184 ckx := new(clientKeyExchangeMsgscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/clientKeyExchangeMsg#)
185 var body []byte
186 if ka.versionscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/rsaKeyAgreement#version. != VersionSSL30scip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/VersionSSL30. {
187 ckx.ciphertextscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/clientKeyExchangeMsg#ciphertext. = make([]byte, len(encrypted)+2)
188 ckx.ciphertextscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/clientKeyExchangeMsg#ciphertext.[0] = byte(len(encrypted) >> 8)
189 ckx.ciphertextscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/clientKeyExchangeMsg#ciphertext.[1] = byte(len(encrypted))
190 body = ckx.ciphertextscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/clientKeyExchangeMsg#ciphertext.[2:]
191 } else {
192 ckx.ciphertextscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/clientKeyExchangeMsg#ciphertext. = make([]byte, len(encrypted))
193 body = ckx.ciphertextscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/clientKeyExchangeMsg#ciphertext.
194 }
195 copy(body, encrypted)
196 return preMasterSecret, ckx, nil
197 }
198
199 // sha1Hash calculates a SHA1 hash over the given byte slices.
200 func md5Hashscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/md5Hash().(slices [][]byte) []byte {
201 h := md5scip-go gomod github.com/golang/go/src go1.16 `crypto/md5`/.Newscip-go gomod github.com/golang/go/src go1.16 `crypto/md5`/New().()
202 for _, slice := range slices {
203 h.Writescip-go gomod github.com/golang/go/src go1.16 io/Writer#Write().(slice)
204 }
205 return h.Sumscip-go gomod github.com/golang/go/src go1.16 hash/Hash#Sum().(nil)
206 }
207
208 // sha1Hash calculates a SHA1 hash over the given byte slices.
209 func sha1Hashscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/sha1Hash().(slices [][]byte) []byte {
210 hsha1 := sha1scip-go gomod github.com/golang/go/src go1.16 `crypto/sha1`/.Newscip-go gomod github.com/golang/go/src go1.16 `crypto/sha1`/New().()
211 for _, slice := range slices {
212 hsha1.Writescip-go gomod github.com/golang/go/src go1.16 io/Writer#Write().(slice)
213 }
214 return hsha1.Sumscip-go gomod github.com/golang/go/src go1.16 hash/Hash#Sum().(nil)
215 }
216
217 // md5SHA1Hash implements TLS 1.0's hybrid hash function which consists of the
218 // concatenation of an MD5 and SHA1 hash.
219 func md5SHA1Hashscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/md5SHA1Hash().(slices [][]byte) []byte {
220 md5sha1 := make([]byte, md5scip-go gomod github.com/golang/go/src go1.16 `crypto/md5`/.Sizescip-go gomod github.com/golang/go/src go1.16 `crypto/md5`/Size.+sha1scip-go gomod github.com/golang/go/src go1.16 `crypto/sha1`/.Sizescip-go gomod github.com/golang/go/src go1.16 `crypto/sha1`/Size.)
221 hmd5 := md5scip-go gomod github.com/golang/go/src go1.16 `crypto/md5`/.Newscip-go gomod github.com/golang/go/src go1.16 `crypto/md5`/New().()
222 for _, slice := range slices {
223 hmd5.Writescip-go gomod github.com/golang/go/src go1.16 io/Writer#Write().(slice)
224 }
225 copy(md5sha1, hmd5.Sumscip-go gomod github.com/golang/go/src go1.16 hash/Hash#Sum().(nil))
226 copy(md5sha1[md5scip-go gomod github.com/golang/go/src go1.16 `crypto/md5`/.Sizescip-go gomod github.com/golang/go/src go1.16 `crypto/md5`/Size.:], sha1Hashscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/sha1Hash().(slices))
227 return md5sha1
228 }
229
230 // sha224Hash implements TLS 1.2's hash function.
231 func sha224Hashscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/sha224Hash().(slices [][]byte) []byte {
232 h := cryptoscip-go gomod github.com/golang/go/src go1.16 crypto/.SHA224scip-go gomod github.com/golang/go/src go1.16 crypto/SHA224..Newscip-go gomod github.com/golang/go/src go1.16 crypto/Hash#New().()
233 for _, slice := range slices {
234 h.Writescip-go gomod github.com/golang/go/src go1.16 io/Writer#Write().(slice)
235 }
236 return h.Sumscip-go gomod github.com/golang/go/src go1.16 hash/Hash#Sum().(nil)
237 }
238
239 // sha256Hash implements TLS 1.2's hash function.
240 func sha256Hashscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/sha256Hash().(slices [][]byte) []byte {
241 h := sha256scip-go gomod github.com/golang/go/src go1.16 `crypto/sha256`/.Newscip-go gomod github.com/golang/go/src go1.16 `crypto/sha256`/New().()
242 for _, slice := range slices {
243 h.Writescip-go gomod github.com/golang/go/src go1.16 io/Writer#Write().(slice)
244 }
245 return h.Sumscip-go gomod github.com/golang/go/src go1.16 hash/Hash#Sum().(nil)
246 }
247
248 // sha256Hash implements TLS 1.2's hash function.
249 func sha384Hashscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/sha384Hash().(slices [][]byte) []byte {
250 h := cryptoscip-go gomod github.com/golang/go/src go1.16 crypto/.SHA384scip-go gomod github.com/golang/go/src go1.16 crypto/SHA384..Newscip-go gomod github.com/golang/go/src go1.16 crypto/Hash#New().()
251 for _, slice := range slices {
252 h.Writescip-go gomod github.com/golang/go/src go1.16 io/Writer#Write().(slice)
253 }
254 return h.Sumscip-go gomod github.com/golang/go/src go1.16 hash/Hash#Sum().(nil)
255 }
256
257 // sha512Hash implements TLS 1.2's hash function.
258 func sha512Hashscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/sha512Hash().(slices [][]byte) []byte {
259 h := sha512scip-go gomod github.com/golang/go/src go1.16 `crypto/sha512`/.Newscip-go gomod github.com/golang/go/src go1.16 `crypto/sha512`/New().()
260 for _, slice := range slices {
261 h.Writescip-go gomod github.com/golang/go/src go1.16 io/Writer#Write().(slice)
262 }
263 return h.Sumscip-go gomod github.com/golang/go/src go1.16 hash/Hash#Sum().(nil)
264 }
265
266 // hashForServerKeyExchange hashes the given slices and returns their digest
267 // and the identifier of the hash function used. The hashFunc argument is only
268 // used for >= TLS 1.2 and precisely identifies the hash function to use.
269 func hashForServerKeyExchangescip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/hashForServerKeyExchange().(sigType, hashFunc uint8, version uint16, slices ...[]byte) ([]byte, cryptoscip-go gomod github.com/golang/go/src go1.16 crypto/.Hashscip-go gomod github.com/golang/go/src go1.16 crypto/Hash#, error) {
270 if version >= VersionTLS12scip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/VersionTLS12. {
271 switch hashFunc {
272 case hashSHA512scip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/hashSHA512.:
273 return sha512Hashscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/sha512Hash().(slices), cryptoscip-go gomod github.com/golang/go/src go1.16 crypto/.SHA512scip-go gomod github.com/golang/go/src go1.16 crypto/SHA512., nil
274 case hashSHA384scip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/hashSHA384.:
275 return sha384Hashscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/sha384Hash().(slices), cryptoscip-go gomod github.com/golang/go/src go1.16 crypto/.SHA384scip-go gomod github.com/golang/go/src go1.16 crypto/SHA384., nil
276 case hashSHA256scip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/hashSHA256.:
277 return sha256Hashscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/sha256Hash().(slices), cryptoscip-go gomod github.com/golang/go/src go1.16 crypto/.SHA256scip-go gomod github.com/golang/go/src go1.16 crypto/SHA256., nil
278 case hashSHA224scip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/hashSHA224.:
279 return sha224Hashscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/sha224Hash().(slices), cryptoscip-go gomod github.com/golang/go/src go1.16 crypto/.SHA224scip-go gomod github.com/golang/go/src go1.16 crypto/SHA224., nil
280 case hashSHA1scip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/hashSHA1.:
281 return sha1Hashscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/sha1Hash().(slices), cryptoscip-go gomod github.com/golang/go/src go1.16 crypto/.SHA1scip-go gomod github.com/golang/go/src go1.16 crypto/SHA1., nil
282 case hashMD5scip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/hashMD5.:
283 return md5Hashscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/md5Hash().(slices), cryptoscip-go gomod github.com/golang/go/src go1.16 crypto/.MD5scip-go gomod github.com/golang/go/src go1.16 crypto/MD5., nil
284 default:
285 return nil, cryptoscip-go gomod github.com/golang/go/src go1.16 crypto/.Hashscip-go gomod github.com/golang/go/src go1.16 crypto/Hash#(0), errorsscip-go gomod github.com/golang/go/src go1.16 errors/.Newscip-go gomod github.com/golang/go/src go1.16 errors/New().("tls: unknown hash function used by peer")
286 }
287 }
288 if sigType == signatureECDSAscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/signatureECDSA. || sigType == signatureDSAscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/signatureDSA. {
289 return sha1Hashscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/sha1Hash().(slices), cryptoscip-go gomod github.com/golang/go/src go1.16 crypto/.SHA1scip-go gomod github.com/golang/go/src go1.16 crypto/SHA1., nil
290 }
291 return md5SHA1Hashscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/md5SHA1Hash().(slices), cryptoscip-go gomod github.com/golang/go/src go1.16 crypto/.MD5SHA1scip-go gomod github.com/golang/go/src go1.16 crypto/MD5SHA1., nil
292 }
293
294 // pickTLS12HashForSignature returns a TLS 1.2 hash identifier for signing a
295 // ServerKeyExchange given the signature type being used and the client's
296 // advertised list of supported signature and hash combinations.
297 func pickTLS12HashForSignaturescip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/pickTLS12HashForSignature().(sigType uint8, clientList, serverList []SigAndHashscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/SigAndHash#) (uint8, error) {
298 if len(clientList) == 0 {
299 // If the client didn't specify any signature_algorithms
300 // extension then we can assume that it supports SHA1. See
301 // http://tools.ietf.org/html/rfc5246#section-7.4.1.4.1
302 return hashSHA1scip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/hashSHA1., nil
303 }
304
305 for _, sigAndHash := range clientList {
306 if sigAndHash.Signaturescip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/SigAndHash#Signature. != sigType {
307 continue
308 }
309 if isSupportedSignatureAndHashscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/isSupportedSignatureAndHash().(sigAndHash, serverList) {
310 return sigAndHash.Hashscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/SigAndHash#Hash., nil
311 }
312 }
313
314 return 0, errorsscip-go gomod github.com/golang/go/src go1.16 errors/.Newscip-go gomod github.com/golang/go/src go1.16 errors/New().("tls: client doesn't support any common hash functions")
315 }
316
317 func curveForCurveIDscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/curveForCurveID().(id CurveIDscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/CurveID#) (ellipticscip-go gomod github.com/golang/go/src go1.16 `crypto/elliptic`/.Curvescip-go gomod github.com/golang/go/src go1.16 `crypto/elliptic`/Curve#, bool) {
318 switch id {
319 case CurveP256scip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/CurveP256.:
320 return ellipticscip-go gomod github.com/golang/go/src go1.16 `crypto/elliptic`/.P256scip-go gomod github.com/golang/go/src go1.16 `crypto/elliptic`/P256().(), true
321 case CurveP384scip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/CurveP384.:
322 return ellipticscip-go gomod github.com/golang/go/src go1.16 `crypto/elliptic`/.P384scip-go gomod github.com/golang/go/src go1.16 `crypto/elliptic`/P384().(), true
323 case CurveP521scip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/CurveP521.:
324 return ellipticscip-go gomod github.com/golang/go/src go1.16 `crypto/elliptic`/.P521scip-go gomod github.com/golang/go/src go1.16 `crypto/elliptic`/P521().(), true
325 default:
326 return nil, false
327 }
328 }
329
330 // keyAgreementAuthentication is a helper interface that specifies how
331 // to authenticate the ServerKeyExchange parameters.
332 type keyAgreementAuthenticationscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/keyAgreementAuthentication# interface {
333 signParametersscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/keyAgreementAuthentication#signParameters.(config *Configscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/Config#, cert *Certificatescip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/Certificate#, clientHello *clientHelloMsgscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/clientHelloMsg#, hello *serverHelloMsgscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/serverHelloMsg#, params []byte) (*serverKeyExchangeMsgscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/serverKeyExchangeMsg#, error)
334 verifyParametersscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/keyAgreementAuthentication#verifyParameters.(config *Configscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/Config#, clientHello *clientHelloMsgscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/clientHelloMsg#, serverHello *serverHelloMsgscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/serverHelloMsg#, cert *x509scip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/x509`/.Certificatescip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/x509`/Certificate#, params []byte, sig []byte) ([]byte, error)
335 }
336
337 // nilKeyAgreementAuthentication does not authenticate the key
338 // agreement parameters.
339 type nilKeyAgreementAuthenticationscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/nilKeyAgreementAuthentication# struct{}
340
341 func (ka *nilKeyAgreementAuthenticationscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/nilKeyAgreementAuthentication#) signParametersscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/nilKeyAgreementAuthentication#signParameters().(config *Configscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/Config#, cert *Certificatescip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/Certificate#, clientHello *clientHelloMsgscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/clientHelloMsg#, hello *serverHelloMsgscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/serverHelloMsg#, params []byte) (*serverKeyExchangeMsgscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/serverKeyExchangeMsg#, error) {
342 skx := new(serverKeyExchangeMsgscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/serverKeyExchangeMsg#)
343 skx.keyscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/serverKeyExchangeMsg#key. = params
344 return skx, nil
345 }
346
347 func (ka *nilKeyAgreementAuthenticationscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/nilKeyAgreementAuthentication#) verifyParametersscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/nilKeyAgreementAuthentication#verifyParameters().(config *Configscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/Config#, clientHello *clientHelloMsgscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/clientHelloMsg#, serverHello *serverHelloMsgscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/serverHelloMsg#, cert *x509scip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/x509`/.Certificatescip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/x509`/Certificate#, params []byte, sig []byte) ([]byte, error) {
348 return nil, nil
349 }
350
351 // signedKeyAgreement signs the ServerKeyExchange parameters with the
352 // server's private key.
353 type signedKeyAgreementscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/signedKeyAgreement# struct {
354 versionscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/signedKeyAgreement#version. uint16
355 sigTypescip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/signedKeyAgreement#sigType. uint8
356 rawscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/signedKeyAgreement#raw. []byte
357 validscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/signedKeyAgreement#valid. bool
358 shscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/signedKeyAgreement#sh. SigAndHashscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/SigAndHash#
359 }
360
361 func (ka *signedKeyAgreementscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/signedKeyAgreement#) signParametersscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/signedKeyAgreement#signParameters().(config *Configscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/Config#, cert *Certificatescip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/Certificate#, clientHello *clientHelloMsgscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/clientHelloMsg#, hello *serverHelloMsgscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/serverHelloMsg#, params []byte) (*serverKeyExchangeMsgscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/serverKeyExchangeMsg#, error) {
362 var tls12HashId uint8
363 var err error
364 if ka.versionscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/signedKeyAgreement#version. >= VersionTLS12scip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/VersionTLS12. {
365 if tls12HashId, err = pickTLS12HashForSignaturescip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/pickTLS12HashForSignature().(ka.sigTypescip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/signedKeyAgreement#sigType., clientHello.signatureAndHashesscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/clientHelloMsg#signatureAndHashes., config.signatureAndHashesForServerscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/Config#signatureAndHashesForServer().()); err != nil {
366 return nil, err
367 }
368 ka.shscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/signedKeyAgreement#sh..Hashscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/SigAndHash#Hash. = tls12HashId
369 }
370 ka.shscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/signedKeyAgreement#sh..Signaturescip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/SigAndHash#Signature. = ka.sigTypescip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/signedKeyAgreement#sigType.
371 digest, hashFunc, err := hashForServerKeyExchangescip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/hashForServerKeyExchange().(ka.sigTypescip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/signedKeyAgreement#sigType., tls12HashId, ka.versionscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/signedKeyAgreement#version., clientHello.randomscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/clientHelloMsg#random., hello.randomscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/serverHelloMsg#random., params)
372 if err != nil {
373 return nil, err
374 }
375 var sig []byte
376 switch ka.sigTypescip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/signedKeyAgreement#sigType. {
377 case signatureECDSAscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/signatureECDSA.:
378 privKey, ok := cert.PrivateKeyscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/Certificate#PrivateKey..(*ecdsascip-go gomod github.com/golang/go/src go1.16 `crypto/ecdsa`/.PrivateKeyscip-go gomod github.com/golang/go/src go1.16 `crypto/ecdsa`/PrivateKey#)
379 if !ok {
380 return nil, errorsscip-go gomod github.com/golang/go/src go1.16 errors/.Newscip-go gomod github.com/golang/go/src go1.16 errors/New().("ECDHE ECDSA requires an ECDSA server private key")
381 }
382 r, s, err := ecdsascip-go gomod github.com/golang/go/src go1.16 `crypto/ecdsa`/.Signscip-go gomod github.com/golang/go/src go1.16 `crypto/ecdsa`/Sign().(config.randscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/Config#rand().(), privKey, digest)
383 if err != nil {
384 return nil, errorsscip-go gomod github.com/golang/go/src go1.16 errors/.Newscip-go gomod github.com/golang/go/src go1.16 errors/New().("failed to sign ECDHE parameters: " + err.Error())
385 }
386 sig, err = asn1scip-go gomod github.com/golang/go/src go1.16 `encoding/asn1`/.Marshalscip-go gomod github.com/golang/go/src go1.16 `encoding/asn1`/Marshal().(ecdsaSignaturescip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/ecdsaSignature#{r, s})
387 if err != nil {
388 return nil, errorsscip-go gomod github.com/golang/go/src go1.16 errors/.Newscip-go gomod github.com/golang/go/src go1.16 errors/New().("failed to marshal ECDSA signature: " + err.Error())
389 }
390 case signatureRSAscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/signatureRSA.:
391 privKey, ok := cert.PrivateKeyscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/Certificate#PrivateKey..(*rsascip-go gomod github.com/golang/go/src go1.16 `crypto/rsa`/.PrivateKeyscip-go gomod github.com/golang/go/src go1.16 `crypto/rsa`/PrivateKey#)
392 if !ok {
393 return nil, errorsscip-go gomod github.com/golang/go/src go1.16 errors/.Newscip-go gomod github.com/golang/go/src go1.16 errors/New().("ECDHE RSA requires a RSA server private key")
394 }
395 sig, err = rsascip-go gomod github.com/golang/go/src go1.16 `crypto/rsa`/.SignPKCS1v15scip-go gomod github.com/golang/go/src go1.16 `crypto/rsa`/SignPKCS1v15().(config.randscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/Config#rand().(), privKey, hashFunc, digest)
396 if err != nil {
397 return nil, errorsscip-go gomod github.com/golang/go/src go1.16 errors/.Newscip-go gomod github.com/golang/go/src go1.16 errors/New().("failed to sign ECDHE parameters: " + err.Error())
398 }
399 default:
400 return nil, errorsscip-go gomod github.com/golang/go/src go1.16 errors/.Newscip-go gomod github.com/golang/go/src go1.16 errors/New().("unknown ECDHE signature algorithm")
401 }
402
403 skx := new(serverKeyExchangeMsgscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/serverKeyExchangeMsg#)
404 skx.digestscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/serverKeyExchangeMsg#digest. = digest
405 sigAndHashLen := 0
406 if ka.versionscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/signedKeyAgreement#version. >= VersionTLS12scip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/VersionTLS12. {
407 sigAndHashLen = 2
408 }
409 skx.keyscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/serverKeyExchangeMsg#key. = make([]byte, len(params)+sigAndHashLen+2+len(sig))
410 copy(skx.keyscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/serverKeyExchangeMsg#key., params)
411 k := skx.keyscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/serverKeyExchangeMsg#key.[len(params):]
412 if ka.versionscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/signedKeyAgreement#version. >= VersionTLS12scip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/VersionTLS12. {
413 k[0] = tls12HashId
414 k[1] = ka.sigTypescip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/signedKeyAgreement#sigType.
415 k = k[2:]
416 }
417 k[0] = byte(len(sig) >> 8)
418 k[1] = byte(len(sig))
419 copy(k[2:], sig)
420 ka.rawscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/signedKeyAgreement#raw. = sig
421 ka.validscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/signedKeyAgreement#valid. = true // We (the server) signed
422 return skx, nil
423 }
424
425 func (ka *signedKeyAgreementscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/signedKeyAgreement#) verifyParametersscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/signedKeyAgreement#verifyParameters().(config *Configscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/Config#, clientHello *clientHelloMsgscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/clientHelloMsg#, serverHello *serverHelloMsgscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/serverHelloMsg#, cert *x509scip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/x509`/.Certificatescip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/x509`/Certificate#, params []byte, sig []byte) ([]byte, error) {
426 if len(sig) < 2 {
427 return nil, errServerKeyExchangescip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/errServerKeyExchange.
428 }
429
430 var tls12HashId uint8
431 if ka.versionscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/signedKeyAgreement#version. >= VersionTLS12scip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/VersionTLS12. {
432 // handle SignatureAndHashAlgorithm
433 var sigAndHash []uint8
434 sigAndHash, sig = sig[:2], sig[2:]
435 tls12HashId = sigAndHash[0]
436 ka.shscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/signedKeyAgreement#sh..Hashscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/SigAndHash#Hash. = tls12HashId
437 ka.shscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/signedKeyAgreement#sh..Signaturescip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/SigAndHash#Signature. = sigAndHash[1]
438 if sigAndHash[1] != ka.sigTypescip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/signedKeyAgreement#sigType. {
439 return nil, errServerKeyExchangescip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/errServerKeyExchange.
440 }
441 if len(sig) < 2 {
442 return nil, errServerKeyExchangescip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/errServerKeyExchange.
443 }
444
445 if !isSupportedSignatureAndHashscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/isSupportedSignatureAndHash().(SigAndHashscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/SigAndHash#{ka.sigTypescip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/signedKeyAgreement#sigType., tls12HashId}, config.signatureAndHashesForClientscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/Config#signatureAndHashesForClient().()) {
446 return nil, errorsscip-go gomod github.com/golang/go/src go1.16 errors/.Newscip-go gomod github.com/golang/go/src go1.16 errors/New().("tls: unsupported hash function for ServerKeyExchange")
447 }
448 }
449 sigLen := int(sig[0])<<8 | int(sig[1])
450 if sigLen+2 != len(sig) {
451 return nil, errServerKeyExchangescip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/errServerKeyExchange.
452 }
453 sig = sig[2:]
454 ka.rawscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/signedKeyAgreement#raw. = sig
455
456 digest, hashFunc, err := hashForServerKeyExchangescip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/hashForServerKeyExchange().(ka.sigTypescip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/signedKeyAgreement#sigType., tls12HashId, ka.versionscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/signedKeyAgreement#version., clientHello.randomscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/clientHelloMsg#random., serverHello.randomscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/serverHelloMsg#random., params)
457 if err != nil {
458 return nil, err
459 }
460 switch ka.sigTypescip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/signedKeyAgreement#sigType. {
461 case signatureECDSAscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/signatureECDSA.:
462 augECDSA, ok := cert.PublicKeyscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/x509`/Certificate#PublicKey..(*x509scip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/x509`/.AugmentedECDSAscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/x509`/AugmentedECDSA#)
463 if !ok {
464 return digest, errorsscip-go gomod github.com/golang/go/src go1.16 errors/.Newscip-go gomod github.com/golang/go/src go1.16 errors/New().("ECDHE ECDSA: could not covert cert.PublicKey to x509.AugmentedECDSA")
465 }
466 pubKey := augECDSA.Pubscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/x509`/AugmentedECDSA#Pub.
467 ecdsaSig := new(ecdsaSignaturescip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/ecdsaSignature#)
468 if _, err := asn1scip-go gomod github.com/golang/go/src go1.16 `encoding/asn1`/.Unmarshalscip-go gomod github.com/golang/go/src go1.16 `encoding/asn1`/Unmarshal().(sig, ecdsaSig); err != nil {
469 return digest, err
470 }
471 if ecdsaSig.Rscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/dsaSignature#R..Signscip-go gomod github.com/golang/go/src go1.16 `math/big`/Int#Sign().() <= 0 || ecdsaSig.Sscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/dsaSignature#S..Signscip-go gomod github.com/golang/go/src go1.16 `math/big`/Int#Sign().() <= 0 {
472 return digest, errorsscip-go gomod github.com/golang/go/src go1.16 errors/.Newscip-go gomod github.com/golang/go/src go1.16 errors/New().("ECDSA signature contained zero or negative values")
473 }
474 if !ecdsascip-go gomod github.com/golang/go/src go1.16 `crypto/ecdsa`/.Verifyscip-go gomod github.com/golang/go/src go1.16 `crypto/ecdsa`/Verify().(pubKey, digest, ecdsaSig.Rscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/dsaSignature#R., ecdsaSig.Sscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/dsaSignature#S.) {
475 return digest, errorsscip-go gomod github.com/golang/go/src go1.16 errors/.Newscip-go gomod github.com/golang/go/src go1.16 errors/New().("ECDSA verification failure")
476 }
477 case signatureRSAscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/signatureRSA.:
478 pubKey, ok := cert.PublicKeyscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/x509`/Certificate#PublicKey..(*rsascip-go gomod github.com/golang/go/src go1.16 `crypto/rsa`/.PublicKeyscip-go gomod github.com/golang/go/src go1.16 `crypto/rsa`/PublicKey#)
479 if !ok {
480 return digest, errorsscip-go gomod github.com/golang/go/src go1.16 errors/.Newscip-go gomod github.com/golang/go/src go1.16 errors/New().("ECDHE RSA requires a RSA server public key")
481 }
482 if err := rsascip-go gomod github.com/golang/go/src go1.16 `crypto/rsa`/.VerifyPKCS1v15scip-go gomod github.com/golang/go/src go1.16 `crypto/rsa`/VerifyPKCS1v15().(pubKey, hashFunc, digest, sig); err != nil {
483 return digest, err
484 }
485 case signatureDSAscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/signatureDSA.:
486 pubKey, ok := cert.PublicKeyscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/x509`/Certificate#PublicKey..(*dsascip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/dsa`/.PublicKeyscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/dsa`/PublicKey#)
487 if !ok {
488 return digest, errorsscip-go gomod github.com/golang/go/src go1.16 errors/.Newscip-go gomod github.com/golang/go/src go1.16 errors/New().("DSS ciphers require a DSA server public key")
489 }
490 dsaSig := new(dsaSignaturescip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/dsaSignature#)
491 if _, err := asn1scip-go gomod github.com/golang/go/src go1.16 `encoding/asn1`/.Unmarshalscip-go gomod github.com/golang/go/src go1.16 `encoding/asn1`/Unmarshal().(sig, dsaSig); err != nil {
492 return digest, err
493 }
494 if dsaSig.Rscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/dsaSignature#R..Signscip-go gomod github.com/golang/go/src go1.16 `math/big`/Int#Sign().() <= 0 || dsaSig.Sscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/dsaSignature#S..Signscip-go gomod github.com/golang/go/src go1.16 `math/big`/Int#Sign().() <= 0 {
495 return digest, errorsscip-go gomod github.com/golang/go/src go1.16 errors/.Newscip-go gomod github.com/golang/go/src go1.16 errors/New().("DSA signature contained zero or negative values")
496 }
497 if !dsascip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/dsa`/.Verifyscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/dsa`/Verify().(pubKey, digest, dsaSig.Rscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/dsaSignature#R., dsaSig.Sscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/dsaSignature#S.) {
498 return digest, errorsscip-go gomod github.com/golang/go/src go1.16 errors/.Newscip-go gomod github.com/golang/go/src go1.16 errors/New().("DSA verification failure")
499 }
500 default:
501 return digest, errorsscip-go gomod github.com/golang/go/src go1.16 errors/.Newscip-go gomod github.com/golang/go/src go1.16 errors/New().("unknown ECDHE signature algorithm")
502 }
503 ka.validscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/signedKeyAgreement#valid. = true
504 return digest, nil
505 }
506
507 // ecdheRSAKeyAgreement implements a TLS key agreement where the server
508 // generates a ephemeral EC public/private key pair and signs it. The
509 // pre-master secret is then calculated using ECDH. The signature may
510 // either be ECDSA or RSA.
511 type ecdheKeyAgreementscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/ecdheKeyAgreement# struct {
512 authscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/ecdheKeyAgreement#auth. keyAgreementAuthenticationscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/keyAgreementAuthentication#
513 privateKeyscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/ecdheKeyAgreement#privateKey. []byte
514 curvescip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/ecdheKeyAgreement#curve. ellipticscip-go gomod github.com/golang/go/src go1.16 `crypto/elliptic`/.Curvescip-go gomod github.com/golang/go/src go1.16 `crypto/elliptic`/Curve#
515 xscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/ecdheKeyAgreement#x., yscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/ecdheKeyAgreement#y. *bigscip-go gomod github.com/golang/go/src go1.16 `math/big`/.Intscip-go gomod github.com/golang/go/src go1.16 `math/big`/Int#
516 verifyErrorscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/ecdheKeyAgreement#verifyError. error
517 curveIDscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/ecdheKeyAgreement#curveID. uint16
518 clientPrivKeyscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/ecdheKeyAgreement#clientPrivKey. []byte
519 serverPrivKeyscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/ecdheKeyAgreement#serverPrivKey. []byte
520 clientXscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/ecdheKeyAgreement#clientX. *bigscip-go gomod github.com/golang/go/src go1.16 `math/big`/.Intscip-go gomod github.com/golang/go/src go1.16 `math/big`/Int#
521 clientYscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/ecdheKeyAgreement#clientY. *bigscip-go gomod github.com/golang/go/src go1.16 `math/big`/.Intscip-go gomod github.com/golang/go/src go1.16 `math/big`/Int#
522 }
523
524 func (ka *ecdheKeyAgreementscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/ecdheKeyAgreement#) generateServerKeyExchangescip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/ecdheKeyAgreement#generateServerKeyExchange().(config *Configscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/Config#, cert *Certificatescip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/Certificate#, clientHello *clientHelloMsgscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/clientHelloMsg#, hello *serverHelloMsgscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/serverHelloMsg#) (*serverKeyExchangeMsgscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/serverKeyExchangeMsg#, error) {
525 var curveid CurveIDscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/CurveID#
526 preferredCurves := config.curvePreferencesscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/Config#curvePreferences().()
527
528 NextCandidate:
529 for _, candidate := range preferredCurves {
530 for _, c := range clientHello.supportedCurvesscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/clientHelloMsg#supportedCurves. {
531 if candidate == c {
532 curveid = c
533 break NextCandidate
534 }
535 }
536 }
537
538 if curveid == 0 {
539 return nil, errorsscip-go gomod github.com/golang/go/src go1.16 errors/.Newscip-go gomod github.com/golang/go/src go1.16 errors/New().("tls: no supported elliptic curves offered")
540 }
541 ka.curveIDscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/ecdheKeyAgreement#curveID. = uint16(curveid)
542
543 var ok bool
544 if ka.curvescip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/ecdheKeyAgreement#curve., ok = curveForCurveIDscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/curveForCurveID().(curveid); !ok {
545 return nil, errorsscip-go gomod github.com/golang/go/src go1.16 errors/.Newscip-go gomod github.com/golang/go/src go1.16 errors/New().("tls: preferredCurves includes unsupported curve")
546 }
547
548 var err error
549 ka.privateKeyscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/ecdheKeyAgreement#privateKey., ka.xscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/ecdheKeyAgreement#x., ka.yscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/ecdheKeyAgreement#y., err = ellipticscip-go gomod github.com/golang/go/src go1.16 `crypto/elliptic`/.GenerateKeyscip-go gomod github.com/golang/go/src go1.16 `crypto/elliptic`/GenerateKey().(ka.curvescip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/ecdheKeyAgreement#curve., config.randscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/Config#rand().())
550 if err != nil {
551 return nil, err
552 }
553 ecdhePublic := ellipticscip-go gomod github.com/golang/go/src go1.16 `crypto/elliptic`/.Marshalscip-go gomod github.com/golang/go/src go1.16 `crypto/elliptic`/Marshal().(ka.curvescip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/ecdheKeyAgreement#curve., ka.xscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/ecdheKeyAgreement#x., ka.yscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/ecdheKeyAgreement#y.)
554
555 ka.serverPrivKeyscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/ecdheKeyAgreement#serverPrivKey. = make([]byte, len(ka.privateKeyscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/ecdheKeyAgreement#privateKey.))
556 copy(ka.serverPrivKeyscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/ecdheKeyAgreement#serverPrivKey., ka.privateKeyscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/ecdheKeyAgreement#privateKey.)
557
558 // http://tools.ietf.org/html/rfc4492#section-5.4
559 serverECDHParams := make([]byte, 1+2+1+len(ecdhePublic))
560 serverECDHParams[0] = 3 // named curve
561 serverECDHParams[1] = byte(curveid >> 8)
562 serverECDHParams[2] = byte(curveid)
563 serverECDHParams[3] = byte(len(ecdhePublic))
564 copy(serverECDHParams[4:], ecdhePublic)
565
566 return ka.authscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/ecdheKeyAgreement#auth..signParametersscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/keyAgreementAuthentication#signParameters.(config, cert, clientHello, hello, serverECDHParams)
567 }
568
569 func (ka *ecdheKeyAgreementscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/ecdheKeyAgreement#) processClientKeyExchangescip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/ecdheKeyAgreement#processClientKeyExchange().(config *Configscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/Config#, cert *Certificatescip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/Certificate#, ckx *clientKeyExchangeMsgscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/clientKeyExchangeMsg#) ([]byte, error) {
570 if len(ckx.ciphertextscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/clientKeyExchangeMsg#ciphertext.) == 0 || int(ckx.ciphertextscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/clientKeyExchangeMsg#ciphertext.[0]) != len(ckx.ciphertextscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/clientKeyExchangeMsg#ciphertext.)-1 {
571 return nil, errClientKeyExchangescip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/errClientKeyExchange.
572 }
573 ka.clientXscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/ecdheKeyAgreement#clientX., ka.clientYscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/ecdheKeyAgreement#clientY. = ellipticscip-go gomod github.com/golang/go/src go1.16 `crypto/elliptic`/.Unmarshalscip-go gomod github.com/golang/go/src go1.16 `crypto/elliptic`/Unmarshal().(ka.curvescip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/ecdheKeyAgreement#curve., ckx.ciphertextscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/clientKeyExchangeMsg#ciphertext.[1:])
574 if ka.clientXscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/ecdheKeyAgreement#clientX. == nil {
575 return nil, errClientKeyExchangescip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/errClientKeyExchange.
576 }
577
578 sharedX, _ := ka.curvescip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/ecdheKeyAgreement#curve..ScalarMultscip-go gomod github.com/golang/go/src go1.16 `crypto/elliptic`/Curve#ScalarMult().(ka.clientXscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/ecdheKeyAgreement#clientX., ka.clientYscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/ecdheKeyAgreement#clientY., ka.privateKeyscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/ecdheKeyAgreement#privateKey.)
579 preMasterSecret := make([]byte, (ka.curvescip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/ecdheKeyAgreement#curve..Paramsscip-go gomod github.com/golang/go/src go1.16 `crypto/elliptic`/Curve#Params().().BitSizescip-go gomod github.com/golang/go/src go1.16 `crypto/elliptic`/CurveParams#BitSize.+7)>>3)
580 xBytes := sharedX.Bytesscip-go gomod github.com/golang/go/src go1.16 `math/big`/Int#Bytes().()
581 copy(preMasterSecret[len(preMasterSecret)-len(xBytes):], xBytes)
582
583 return preMasterSecret, nil
584 }
585
586 func (ka *ecdheKeyAgreementscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/ecdheKeyAgreement#) processServerKeyExchangescip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/ecdheKeyAgreement#processServerKeyExchange().(config *Configscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/Config#, clientHello *clientHelloMsgscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/clientHelloMsg#, serverHello *serverHelloMsgscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/serverHelloMsg#, cert *x509scip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/x509`/.Certificatescip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/x509`/Certificate#, skx *serverKeyExchangeMsgscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/serverKeyExchangeMsg#) error {
587 if len(skx.keyscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/serverKeyExchangeMsg#key.) < 4 {
588 return errServerKeyExchangescip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/errServerKeyExchange.
589 }
590 if skx.keyscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/serverKeyExchangeMsg#key.[0] != 3 { // named curve
591 return errorsscip-go gomod github.com/golang/go/src go1.16 errors/.Newscip-go gomod github.com/golang/go/src go1.16 errors/New().("tls: server selected unsupported curve")
592 }
593 curveid := CurveIDscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/CurveID#(skx.keyscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/serverKeyExchangeMsg#key.[1])<<8 | CurveIDscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/CurveID#(skx.keyscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/serverKeyExchangeMsg#key.[2])
594 ka.curveIDscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/ecdheKeyAgreement#curveID. = uint16(curveid)
595
596 var ok bool
597 if ka.curvescip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/ecdheKeyAgreement#curve., ok = curveForCurveIDscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/curveForCurveID().(curveid); !ok {
598 return errorsscip-go gomod github.com/golang/go/src go1.16 errors/.Newscip-go gomod github.com/golang/go/src go1.16 errors/New().("tls: server selected unsupported curve")
599 }
600
601 publicLen := int(skx.keyscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/serverKeyExchangeMsg#key.[3])
602 if publicLen+4 > len(skx.keyscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/serverKeyExchangeMsg#key.) {
603 return errServerKeyExchangescip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/errServerKeyExchange.
604 }
605 ka.xscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/ecdheKeyAgreement#x., ka.yscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/ecdheKeyAgreement#y. = ellipticscip-go gomod github.com/golang/go/src go1.16 `crypto/elliptic`/.Unmarshalscip-go gomod github.com/golang/go/src go1.16 `crypto/elliptic`/Unmarshal().(ka.curvescip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/ecdheKeyAgreement#curve., skx.keyscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/serverKeyExchangeMsg#key.[4:4+publicLen])
606 if ka.xscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/ecdheKeyAgreement#x. == nil {
607 return errServerKeyExchangescip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/errServerKeyExchange.
608 }
609 serverECDHParams := skx.keyscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/serverKeyExchangeMsg#key.[:4+publicLen]
610
611 sig := skx.keyscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/serverKeyExchangeMsg#key.[4+publicLen:]
612 skx.digestscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/serverKeyExchangeMsg#digest., ka.verifyErrorscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/ecdheKeyAgreement#verifyError. = ka.authscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/ecdheKeyAgreement#auth..verifyParametersscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/keyAgreementAuthentication#verifyParameters.(config, clientHello, serverHello, cert, serverECDHParams, sig)
613 if config.InsecureSkipVerifyscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/Config#InsecureSkipVerify. {
614 return nil
615 }
616 return ka.verifyErrorscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/ecdheKeyAgreement#verifyError.
617 }
618
619 func (ka *ecdheKeyAgreementscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/ecdheKeyAgreement#) generateClientKeyExchangescip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/ecdheKeyAgreement#generateClientKeyExchange().(config *Configscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/Config#, clientHello *clientHelloMsgscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/clientHelloMsg#, cert *x509scip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/x509`/.Certificatescip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/x509`/Certificate#) ([]byte, *clientKeyExchangeMsgscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/clientKeyExchangeMsg#, error) {
620 if ka.curvescip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/ecdheKeyAgreement#curve. == nil {
621 return nil, nil, errorsscip-go gomod github.com/golang/go/src go1.16 errors/.Newscip-go gomod github.com/golang/go/src go1.16 errors/New().("missing ServerKeyExchange message")
622 }
623 priv, mx, my, err := ellipticscip-go gomod github.com/golang/go/src go1.16 `crypto/elliptic`/.GenerateKeyscip-go gomod github.com/golang/go/src go1.16 `crypto/elliptic`/GenerateKey().(ka.curvescip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/ecdheKeyAgreement#curve., config.randscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/Config#rand().())
624 if err != nil {
625 return nil, nil, err
626 }
627
628 ka.clientPrivKeyscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/ecdheKeyAgreement#clientPrivKey. = make([]byte, len(priv))
629 copy(ka.clientPrivKeyscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/ecdheKeyAgreement#clientPrivKey., priv)
630 ka.clientXscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/ecdheKeyAgreement#clientX. = mx
631 ka.clientYscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/ecdheKeyAgreement#clientY. = my
632
633 x, _ := ka.curvescip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/ecdheKeyAgreement#curve..ScalarMultscip-go gomod github.com/golang/go/src go1.16 `crypto/elliptic`/Curve#ScalarMult().(ka.xscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/ecdheKeyAgreement#x., ka.yscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/ecdheKeyAgreement#y., priv)
634 preMasterSecret := make([]byte, (ka.curvescip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/ecdheKeyAgreement#curve..Paramsscip-go gomod github.com/golang/go/src go1.16 `crypto/elliptic`/Curve#Params().().BitSizescip-go gomod github.com/golang/go/src go1.16 `crypto/elliptic`/CurveParams#BitSize.+7)>>3)
635 xBytes := x.Bytesscip-go gomod github.com/golang/go/src go1.16 `math/big`/Int#Bytes().()
636 copy(preMasterSecret[len(preMasterSecret)-len(xBytes):], xBytes)
637
638 serialized := ellipticscip-go gomod github.com/golang/go/src go1.16 `crypto/elliptic`/.Marshalscip-go gomod github.com/golang/go/src go1.16 `crypto/elliptic`/Marshal().(ka.curvescip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/ecdheKeyAgreement#curve., mx, my)
639
640 ckx := new(clientKeyExchangeMsgscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/clientKeyExchangeMsg#)
641 var body []byte
642 ckx.ciphertextscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/clientKeyExchangeMsg#ciphertext. = make([]byte, 1+len(serialized))
643 ckx.ciphertextscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/clientKeyExchangeMsg#ciphertext.[0] = byte(len(serialized))
644 body = ckx.ciphertextscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/clientKeyExchangeMsg#ciphertext.[1:]
645 copy(body, serialized)
646
647 return preMasterSecret, ckx, nil
648 }
649
650 // dheRSAKeyAgreement implements a TLS key agreement where the server generates
651 // an ephemeral Diffie-Hellman public/private key pair and signs it. The
652 // pre-master secret is then calculated using Diffie-Hellman.
653 type dheKeyAgreementscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/dheKeyAgreement# struct {
654 authscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/dheKeyAgreement#auth. keyAgreementAuthenticationscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/keyAgreementAuthentication#
655 pscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/dheKeyAgreement#p., gscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/dheKeyAgreement#g. *bigscip-go gomod github.com/golang/go/src go1.16 `math/big`/.Intscip-go gomod github.com/golang/go/src go1.16 `math/big`/Int#
656 yTheirsscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/dheKeyAgreement#yTheirs. *bigscip-go gomod github.com/golang/go/src go1.16 `math/big`/.Intscip-go gomod github.com/golang/go/src go1.16 `math/big`/Int#
657 yOursscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/dheKeyAgreement#yOurs. *bigscip-go gomod github.com/golang/go/src go1.16 `math/big`/.Intscip-go gomod github.com/golang/go/src go1.16 `math/big`/Int#
658 xOursscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/dheKeyAgreement#xOurs. *bigscip-go gomod github.com/golang/go/src go1.16 `math/big`/.Intscip-go gomod github.com/golang/go/src go1.16 `math/big`/Int#
659 yServerscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/dheKeyAgreement#yServer. *bigscip-go gomod github.com/golang/go/src go1.16 `math/big`/.Intscip-go gomod github.com/golang/go/src go1.16 `math/big`/Int#
660 yClientscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/dheKeyAgreement#yClient. *bigscip-go gomod github.com/golang/go/src go1.16 `math/big`/.Intscip-go gomod github.com/golang/go/src go1.16 `math/big`/Int#
661 verifyErrorscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/dheKeyAgreement#verifyError. error
662 }
663
664 func (ka *dheKeyAgreementscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/dheKeyAgreement#) generateServerKeyExchangescip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/dheKeyAgreement#generateServerKeyExchange().(config *Configscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/Config#, cert *Certificatescip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/Certificate#, clientHello *clientHelloMsgscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/clientHelloMsg#, hello *serverHelloMsgscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/serverHelloMsg#) (*serverKeyExchangeMsgscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/serverKeyExchangeMsg#, error) {
665 var q *bigscip-go gomod github.com/golang/go/src go1.16 `math/big`/.Intscip-go gomod github.com/golang/go/src go1.16 `math/big`/Int#
666 // 2048-bit MODP Group with 256-bit Prime Order Subgroup (RFC
667 // 5114, Section 2.3)
668 // TODO: Take a prime in the config
669 ka.pscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/dheKeyAgreement#p., _ = new(bigscip-go gomod github.com/golang/go/src go1.16 `math/big`/.Intscip-go gomod github.com/golang/go/src go1.16 `math/big`/Int#).SetStringscip-go gomod github.com/golang/go/src go1.16 `math/big`/Int#SetString().("87A8E61DB4B6663CFFBBD19C651959998CEEF608660DD0F25D2CEED4435E3B00E00DF8F1D61957D4FAF7DF4561B2AA3016C3D91134096FAA3BF4296D830E9A7C209E0C6497517ABD5A8A9D306BCF67ED91F9E6725B4758C022E0B1EF4275BF7B6C5BFC11D45F9088B941F54EB1E59BB8BC39A0BF12307F5C4FDB70C581B23F76B63ACAE1CAA6B7902D52526735488A0EF13C6D9A51BFA4AB3AD8347796524D8EF6A167B5A41825D967E144E5140564251CCACB83E6B486F6B3CA3F7971506026C0B857F689962856DED4010ABD0BE621C3A3960A54E710C375F26375D7014103A4B54330C198AF126116D2276E11715F693877FAD7EF09CADB094AE91E1A1597", 16)
670 ka.gscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/dheKeyAgreement#g., _ = new(bigscip-go gomod github.com/golang/go/src go1.16 `math/big`/.Intscip-go gomod github.com/golang/go/src go1.16 `math/big`/Int#).SetStringscip-go gomod github.com/golang/go/src go1.16 `math/big`/Int#SetString().("3FB32C9B73134D0B2E77506660EDBD484CA7B18F21EF205407F4793A1A0BA12510DBC15077BE463FFF4FED4AAC0BB555BE3A6C1B0C6B47B1BC3773BF7E8C6F62901228F8C28CBB18A55AE31341000A650196F931C77A57F2DDF463E5E9EC144B777DE62AAAB8A8628AC376D282D6ED3864E67982428EBC831D14348F6F2F9193B5045AF2767164E1DFC967C1FB3F2E55A4BD1BFFE83B9C80D052B985D182EA0ADB2A3B7313D3FE14C8484B1E052588B9B7D2BBD2DF016199ECD06E1557CD0915B3353BBB64E0EC377FD028370DF92B52C7891428CDC67EB6184B523D1DB246C32F63078490F00EF8D647D148D47954515E2327CFEF98C582664B4C0F6CC41659", 16)
671 q, _ = new(bigscip-go gomod github.com/golang/go/src go1.16 `math/big`/.Intscip-go gomod github.com/golang/go/src go1.16 `math/big`/Int#).SetStringscip-go gomod github.com/golang/go/src go1.16 `math/big`/Int#SetString().("8CF83642A709A097B447997640129DA299B1A47D1EB3750BA308B0FE64F5FBD3", 16)
672
673 var err error
674 ka.xOursscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/dheKeyAgreement#xOurs., err = randscip-go gomod github.com/golang/go/src go1.16 `crypto/rand`/.Intscip-go gomod github.com/golang/go/src go1.16 `crypto/rand`/Int().(config.randscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/Config#rand().(), q)
675 if err != nil {
676 return nil, err
677 }
678 yOurs := new(bigscip-go gomod github.com/golang/go/src go1.16 `math/big`/.Intscip-go gomod github.com/golang/go/src go1.16 `math/big`/Int#).Expscip-go gomod github.com/golang/go/src go1.16 `math/big`/Int#Exp().(ka.gscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/dheKeyAgreement#g., ka.xOursscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/dheKeyAgreement#xOurs., ka.pscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/dheKeyAgreement#p.)
679 ka.yOursscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/dheKeyAgreement#yOurs. = yOurs
680 ka.yServerscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/dheKeyAgreement#yServer. = new(bigscip-go gomod github.com/golang/go/src go1.16 `math/big`/.Intscip-go gomod github.com/golang/go/src go1.16 `math/big`/Int#).Setscip-go gomod github.com/golang/go/src go1.16 `math/big`/Int#Set().(yOurs)
681
682 // http://tools.ietf.org/html/rfc5246#section-7.4.3
683 pBytes := ka.pscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/dheKeyAgreement#p..Bytesscip-go gomod github.com/golang/go/src go1.16 `math/big`/Int#Bytes().()
684 gBytes := ka.gscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/dheKeyAgreement#g..Bytesscip-go gomod github.com/golang/go/src go1.16 `math/big`/Int#Bytes().()
685 yBytes := yOurs.Bytesscip-go gomod github.com/golang/go/src go1.16 `math/big`/Int#Bytes().()
686 serverDHParams := make([]byte, 0, 2+len(pBytes)+2+len(gBytes)+2+len(yBytes))
687 serverDHParams = append(serverDHParams, byte(len(pBytes)>>8), byte(len(pBytes)))
688 serverDHParams = append(serverDHParams, pBytes...)
689 serverDHParams = append(serverDHParams, byte(len(gBytes)>>8), byte(len(gBytes)))
690 serverDHParams = append(serverDHParams, gBytes...)
691 serverDHParams = append(serverDHParams, byte(len(yBytes)>>8), byte(len(yBytes)))
692 serverDHParams = append(serverDHParams, yBytes...)
693
694 return ka.authscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/dheKeyAgreement#auth..signParametersscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/keyAgreementAuthentication#signParameters.(config, cert, clientHello, hello, serverDHParams)
695 }
696
697 func (ka *dheKeyAgreementscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/dheKeyAgreement#) processClientKeyExchangescip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/dheKeyAgreement#processClientKeyExchange().(config *Configscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/Config#, cert *Certificatescip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/Certificate#, ckx *clientKeyExchangeMsgscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/clientKeyExchangeMsg#) ([]byte, error) {
698 if len(ckx.ciphertextscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/clientKeyExchangeMsg#ciphertext.) < 2 {
699 return nil, errClientKeyExchangescip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/errClientKeyExchange.
700 }
701 yLen := (int(ckx.ciphertextscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/clientKeyExchangeMsg#ciphertext.[0]) << 8) | int(ckx.ciphertextscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/clientKeyExchangeMsg#ciphertext.[1])
702 if yLen != len(ckx.ciphertextscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/clientKeyExchangeMsg#ciphertext.)-2 {
703 return nil, errClientKeyExchangescip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/errClientKeyExchange.
704 }
705 yTheirs := new(bigscip-go gomod github.com/golang/go/src go1.16 `math/big`/.Intscip-go gomod github.com/golang/go/src go1.16 `math/big`/Int#).SetBytesscip-go gomod github.com/golang/go/src go1.16 `math/big`/Int#SetBytes().(ckx.ciphertextscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/clientKeyExchangeMsg#ciphertext.[2:])
706 ka.yClientscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/dheKeyAgreement#yClient. = new(bigscip-go gomod github.com/golang/go/src go1.16 `math/big`/.Intscip-go gomod github.com/golang/go/src go1.16 `math/big`/Int#).Setscip-go gomod github.com/golang/go/src go1.16 `math/big`/Int#Set().(yTheirs)
707 if yTheirs.Signscip-go gomod github.com/golang/go/src go1.16 `math/big`/Int#Sign().() <= 0 || yTheirs.Cmpscip-go gomod github.com/golang/go/src go1.16 `math/big`/Int#Cmp().(ka.pscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/dheKeyAgreement#p.) >= 0 {
708 return nil, errClientKeyExchangescip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/errClientKeyExchange.
709 }
710 return new(bigscip-go gomod github.com/golang/go/src go1.16 `math/big`/.Intscip-go gomod github.com/golang/go/src go1.16 `math/big`/Int#).Expscip-go gomod github.com/golang/go/src go1.16 `math/big`/Int#Exp().(yTheirs, ka.xOursscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/dheKeyAgreement#xOurs., ka.pscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/dheKeyAgreement#p.).Bytesscip-go gomod github.com/golang/go/src go1.16 `math/big`/Int#Bytes().(), nil
711 }
712
713 func (ka *dheKeyAgreementscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/dheKeyAgreement#) processServerKeyExchangescip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/dheKeyAgreement#processServerKeyExchange().(config *Configscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/Config#, clientHello *clientHelloMsgscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/clientHelloMsg#, serverHello *serverHelloMsgscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/serverHelloMsg#, cert *x509scip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/x509`/.Certificatescip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/x509`/Certificate#, skx *serverKeyExchangeMsgscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/serverKeyExchangeMsg#) error {
714 // Read dh_p
715 k := skx.keyscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/serverKeyExchangeMsg#key.
716 if len(k) < 2 {
717 return errServerKeyExchangescip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/errServerKeyExchange.
718 }
719 pLen := (int(k[0]) << 8) | int(k[1])
720 k = k[2:]
721 if len(k) < pLen {
722 return errServerKeyExchangescip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/errServerKeyExchange.
723 }
724 ka.pscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/dheKeyAgreement#p. = new(bigscip-go gomod github.com/golang/go/src go1.16 `math/big`/.Intscip-go gomod github.com/golang/go/src go1.16 `math/big`/Int#).SetBytesscip-go gomod github.com/golang/go/src go1.16 `math/big`/Int#SetBytes().(k[:pLen])
725 k = k[pLen:]
726
727 // Read dh_g
728 if len(k) < 2 {
729 return errServerKeyExchangescip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/errServerKeyExchange.
730 }
731 gLen := (int(k[0]) << 8) | int(k[1])
732 k = k[2:]
733 if len(k) < gLen {
734 return errServerKeyExchangescip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/errServerKeyExchange.
735 }
736 ka.gscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/dheKeyAgreement#g. = new(bigscip-go gomod github.com/golang/go/src go1.16 `math/big`/.Intscip-go gomod github.com/golang/go/src go1.16 `math/big`/Int#).SetBytesscip-go gomod github.com/golang/go/src go1.16 `math/big`/Int#SetBytes().(k[:gLen])
737 k = k[gLen:]
738
739 // Read dh_Ys
740 if len(k) < 2 {
741 return errServerKeyExchangescip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/errServerKeyExchange.
742 }
743 yLen := (int(k[0]) << 8) | int(k[1])
744 k = k[2:]
745 if len(k) < yLen {
746 return errServerKeyExchangescip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/errServerKeyExchange.
747 }
748 ka.yTheirsscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/dheKeyAgreement#yTheirs. = new(bigscip-go gomod github.com/golang/go/src go1.16 `math/big`/.Intscip-go gomod github.com/golang/go/src go1.16 `math/big`/Int#).SetBytesscip-go gomod github.com/golang/go/src go1.16 `math/big`/Int#SetBytes().(k[:yLen])
749 ka.yServerscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/dheKeyAgreement#yServer. = new(bigscip-go gomod github.com/golang/go/src go1.16 `math/big`/.Intscip-go gomod github.com/golang/go/src go1.16 `math/big`/Int#).Setscip-go gomod github.com/golang/go/src go1.16 `math/big`/Int#Set().(ka.yTheirsscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/dheKeyAgreement#yTheirs.)
750 k = k[yLen:]
751 if ka.yTheirsscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/dheKeyAgreement#yTheirs..Signscip-go gomod github.com/golang/go/src go1.16 `math/big`/Int#Sign().() <= 0 || ka.yTheirsscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/dheKeyAgreement#yTheirs..Cmpscip-go gomod github.com/golang/go/src go1.16 `math/big`/Int#Cmp().(ka.pscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/dheKeyAgreement#p.) >= 0 {
752 return errServerKeyExchangescip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/errServerKeyExchange.
753 }
754
755 sig := k
756 serverDHParams := skx.keyscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/serverKeyExchangeMsg#key.[:len(skx.keyscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/serverKeyExchangeMsg#key.)-len(sig)]
757 skx.digestscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/serverKeyExchangeMsg#digest., ka.verifyErrorscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/dheKeyAgreement#verifyError. = ka.authscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/dheKeyAgreement#auth..verifyParametersscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/keyAgreementAuthentication#verifyParameters.(config, clientHello, serverHello, cert, serverDHParams, sig)
758 if config.InsecureSkipVerifyscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/Config#InsecureSkipVerify. {
759 return nil
760 }
761 return ka.verifyErrorscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/dheKeyAgreement#verifyError.
762 }
763
764 func (ka *dheKeyAgreementscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/dheKeyAgreement#) generateClientKeyExchangescip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/dheKeyAgreement#generateClientKeyExchange().(config *Configscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/Config#, clientHello *clientHelloMsgscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/clientHelloMsg#, cert *x509scip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/x509`/.Certificatescip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/x509`/Certificate#) ([]byte, *clientKeyExchangeMsgscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/clientKeyExchangeMsg#, error) {
765 if ka.pscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/dheKeyAgreement#p. == nil || ka.gscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/dheKeyAgreement#g. == nil || ka.yTheirsscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/dheKeyAgreement#yTheirs. == nil {
766 return nil, nil, errorsscip-go gomod github.com/golang/go/src go1.16 errors/.Newscip-go gomod github.com/golang/go/src go1.16 errors/New().("missing ServerKeyExchange message")
767 }
768
769 xOurs, err := randscip-go gomod github.com/golang/go/src go1.16 `crypto/rand`/.Intscip-go gomod github.com/golang/go/src go1.16 `crypto/rand`/Int().(config.randscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/Config#rand().(), ka.pscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/dheKeyAgreement#p.)
770 if err != nil {
771 return nil, nil, err
772 }
773 preMasterSecret := new(bigscip-go gomod github.com/golang/go/src go1.16 `math/big`/.Intscip-go gomod github.com/golang/go/src go1.16 `math/big`/Int#).Expscip-go gomod github.com/golang/go/src go1.16 `math/big`/Int#Exp().(ka.yTheirsscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/dheKeyAgreement#yTheirs., xOurs, ka.pscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/dheKeyAgreement#p.).Bytesscip-go gomod github.com/golang/go/src go1.16 `math/big`/Int#Bytes().()
774
775 yOurs := new(bigscip-go gomod github.com/golang/go/src go1.16 `math/big`/.Intscip-go gomod github.com/golang/go/src go1.16 `math/big`/Int#).Expscip-go gomod github.com/golang/go/src go1.16 `math/big`/Int#Exp().(ka.gscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/dheKeyAgreement#g., xOurs, ka.pscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/dheKeyAgreement#p.)
776 ka.yOursscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/dheKeyAgreement#yOurs. = yOurs
777 ka.xOursscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/dheKeyAgreement#xOurs. = xOurs
778 ka.yClientscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/dheKeyAgreement#yClient. = new(bigscip-go gomod github.com/golang/go/src go1.16 `math/big`/.Intscip-go gomod github.com/golang/go/src go1.16 `math/big`/Int#).Setscip-go gomod github.com/golang/go/src go1.16 `math/big`/Int#Set().(yOurs)
779 yBytes := yOurs.Bytesscip-go gomod github.com/golang/go/src go1.16 `math/big`/Int#Bytes().()
780 ckx := new(clientKeyExchangeMsgscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/clientKeyExchangeMsg#)
781 ckx.ciphertextscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/clientKeyExchangeMsg#ciphertext. = make([]byte, 2+len(yBytes))
782 ckx.ciphertextscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/clientKeyExchangeMsg#ciphertext.[0] = byte(len(yBytes) >> 8)
783 ckx.ciphertextscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/clientKeyExchangeMsg#ciphertext.[1] = byte(len(yBytes))
784 copy(ckx.ciphertextscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/tls`/clientKeyExchangeMsg#ciphertext.[2:], yBytes)
785
786 return preMasterSecret, ckx, nil
787 }