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.
19 // rsaKeyAgreement implements the standard TLS key agreement where the client
20 // encrypts the pre-master secret to the server's public key.
21 type rsaKeyAgreement struct{}
23 func (ka rsaKeyAgreement) generateServerKeyExchange(config *Config, clientHello *clientHelloMsg, hello *serverHelloMsg) (*serverKeyExchangeMsg, os.Error) {
27 func (ka rsaKeyAgreement) processClientKeyExchange(config *Config, ckx *clientKeyExchangeMsg) ([]byte, os.Error) {
28 preMasterSecret := make([]byte, 48)
29 _, err := io.ReadFull(config.rand(), preMasterSecret[2:])
34 if len(ckx.ciphertext) < 2 {
35 return nil, os.ErrorString("bad ClientKeyExchange")
37 ciphertextLen := int(ckx.ciphertext[0])<<8 | int(ckx.ciphertext[1])
38 if ciphertextLen != len(ckx.ciphertext)-2 {
39 return nil, os.ErrorString("bad ClientKeyExchange")
41 ciphertext := ckx.ciphertext[2:]
43 err = rsa.DecryptPKCS1v15SessionKey(config.rand(), config.Certificates[0].PrivateKey, ciphertext, preMasterSecret)
47 // We don't check the version number in the premaster secret. For one,
48 // by checking it, we would leak information about the validity of the
49 // encrypted pre-master secret. Secondly, it provides only a small
50 // benefit against a downgrade attack and some implementations send the
51 // wrong version anyway. See the discussion at the end of section
52 // 7.4.7.1 of RFC 4346.
53 return preMasterSecret, nil
56 func (ka rsaKeyAgreement) processServerKeyExchange(config *Config, clientHello *clientHelloMsg, serverHello *serverHelloMsg, cert *x509.Certificate, skx *serverKeyExchangeMsg) os.Error {
57 return os.ErrorString("unexpected ServerKeyExchange")
60 func (ka rsaKeyAgreement) generateClientKeyExchange(config *Config, clientHello *clientHelloMsg, cert *x509.Certificate) ([]byte, *clientKeyExchangeMsg, os.Error) {
61 preMasterSecret := make([]byte, 48)
62 preMasterSecret[0] = byte(clientHello.vers >> 8)
63 preMasterSecret[1] = byte(clientHello.vers)
64 _, err := io.ReadFull(config.rand(), preMasterSecret[2:])
69 encrypted, err := rsa.EncryptPKCS1v15(config.rand(), cert.PublicKey.(*rsa.PublicKey), preMasterSecret)
73 ckx := new(clientKeyExchangeMsg)
74 ckx.ciphertext = make([]byte, len(encrypted)+2)
75 ckx.ciphertext[0] = byte(len(encrypted) >> 8)
76 ckx.ciphertext[1] = byte(len(encrypted))
77 copy(ckx.ciphertext[2:], encrypted)
78 return preMasterSecret, ckx, nil
82 // md5SHA1Hash implements TLS 1.0's hybrid hash function which consists of the
83 // concatenation of an MD5 and SHA1 hash.
84 func md5SHA1Hash(slices ...[]byte) []byte {
85 md5sha1 := make([]byte, md5.Size+sha1.Size)
87 for _, slice := range slices {
90 copy(md5sha1, hmd5.Sum())
93 for _, slice := range slices {
96 copy(md5sha1[md5.Size:], hsha1.Sum())
100 // ecdheRSAKeyAgreement implements a TLS key agreement where the server
101 // generates a ephemeral EC public/private key pair and signs it. The
102 // pre-master secret is then calculated using ECDH.
103 type ecdheRSAKeyAgreement struct {
105 curve *elliptic.Curve
109 func (ka *ecdheRSAKeyAgreement) generateServerKeyExchange(config *Config, clientHello *clientHelloMsg, hello *serverHelloMsg) (*serverKeyExchangeMsg, os.Error) {
113 for _, c := range clientHello.supportedCurves {
116 ka.curve = elliptic.P256()
120 ka.curve = elliptic.P384()
124 ka.curve = elliptic.P521()
132 ka.privateKey, x, y, err = ka.curve.GenerateKey(config.rand())
136 ecdhePublic := ka.curve.Marshal(x, y)
138 // http://tools.ietf.org/html/rfc4492#section-5.4
139 serverECDHParams := make([]byte, 1+2+1+len(ecdhePublic))
140 serverECDHParams[0] = 3 // named curve
141 serverECDHParams[1] = byte(curveid >> 8)
142 serverECDHParams[2] = byte(curveid)
143 serverECDHParams[3] = byte(len(ecdhePublic))
144 copy(serverECDHParams[4:], ecdhePublic)
146 md5sha1 := md5SHA1Hash(clientHello.random, hello.random, serverECDHParams)
147 sig, err := rsa.SignPKCS1v15(config.rand(), config.Certificates[0].PrivateKey, crypto.MD5SHA1, md5sha1)
149 return nil, os.ErrorString("failed to sign ECDHE parameters: " + err.String())
152 skx := new(serverKeyExchangeMsg)
153 skx.key = make([]byte, len(serverECDHParams)+2+len(sig))
154 copy(skx.key, serverECDHParams)
155 k := skx.key[len(serverECDHParams):]
156 k[0] = byte(len(sig) >> 8)
157 k[1] = byte(len(sig))
163 func (ka *ecdheRSAKeyAgreement) processClientKeyExchange(config *Config, ckx *clientKeyExchangeMsg) ([]byte, os.Error) {
164 if len(ckx.ciphertext) == 0 || int(ckx.ciphertext[0]) != len(ckx.ciphertext)-1 {
165 return nil, os.ErrorString("bad ClientKeyExchange")
167 x, y := ka.curve.Unmarshal(ckx.ciphertext[1:])
169 return nil, os.ErrorString("bad ClientKeyExchange")
171 x, _ = ka.curve.ScalarMult(x, y, ka.privateKey)
172 preMasterSecret := make([]byte, (ka.curve.BitSize+7)>>3)
174 copy(preMasterSecret[len(preMasterSecret)-len(xBytes):], xBytes)
176 return preMasterSecret, nil
179 func (ka *ecdheRSAKeyAgreement) processServerKeyExchange(config *Config, clientHello *clientHelloMsg, serverHello *serverHelloMsg, cert *x509.Certificate, skx *serverKeyExchangeMsg) os.Error {
180 if len(skx.key) < 4 {
183 if skx.key[0] != 3 { // named curve
184 return os.ErrorString("server selected unsupported curve")
186 curveid := uint16(skx.key[1])<<8 | uint16(skx.key[2])
190 ka.curve = elliptic.P256()
192 ka.curve = elliptic.P384()
194 ka.curve = elliptic.P521()
196 return os.ErrorString("server selected unsupported curve")
199 publicLen := int(skx.key[3])
200 if publicLen+4 > len(skx.key) {
203 ka.x, ka.y = ka.curve.Unmarshal(skx.key[4 : 4+publicLen])
207 serverECDHParams := skx.key[:4+publicLen]
209 sig := skx.key[4+publicLen:]
213 sigLen := int(sig[0])<<8 | int(sig[1])
214 if sigLen+2 != len(sig) {
219 md5sha1 := md5SHA1Hash(clientHello.random, serverHello.random, serverECDHParams)
220 return rsa.VerifyPKCS1v15(cert.PublicKey.(*rsa.PublicKey), crypto.MD5SHA1, md5sha1, sig)
223 return os.ErrorString("invalid ServerKeyExchange")
226 func (ka *ecdheRSAKeyAgreement) generateClientKeyExchange(config *Config, clientHello *clientHelloMsg, cert *x509.Certificate) ([]byte, *clientKeyExchangeMsg, os.Error) {
228 return nil, nil, os.ErrorString("missing ServerKeyExchange message")
230 priv, mx, my, err := ka.curve.GenerateKey(config.rand())
234 x, _ := ka.curve.ScalarMult(ka.x, ka.y, priv)
235 preMasterSecret := make([]byte, (ka.curve.BitSize+7)>>3)
237 copy(preMasterSecret[len(preMasterSecret)-len(xBytes):], xBytes)
239 serialised := ka.curve.Marshal(mx, my)
241 ckx := new(clientKeyExchangeMsg)
242 ckx.ciphertext = make([]byte, 1+len(serialised))
243 ckx.ciphertext[0] = byte(len(serialised))
244 copy(ckx.ciphertext[1:], serialised)
246 return preMasterSecret, ckx, nil