author | xuelei |
Wed, 23 Mar 2016 12:25:08 +0000 | |
changeset 36661 | 044bf6a5474a |
parent 31712 | e4d5230193da |
child 39563 | 1449ed425710 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
31712 | 2 |
* Copyright (c) 2006, 2015, Oracle and/or its affiliates. All rights reserved. |
2 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 10 |
* |
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
5506 | 21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
2 | 24 |
*/ |
25 |
||
26 |
package sun.security.ssl; |
|
27 |
||
28 |
import java.security.*; |
|
29 |
import java.security.interfaces.ECPublicKey; |
|
30 |
import java.security.spec.*; |
|
31 |
||
31712 | 32 |
import java.util.EnumSet; |
2 | 33 |
import javax.crypto.SecretKey; |
34 |
import javax.crypto.KeyAgreement; |
|
25807
6c325960c9ee
8042982: Unexpected RuntimeExceptions being thrown by SSLEngine
robm
parents:
23010
diff
changeset
|
35 |
import javax.net.ssl.SSLHandshakeException; |
2 | 36 |
|
37 |
/** |
|
38 |
* Helper class for the ECDH key exchange. It generates the appropriate |
|
39 |
* ephemeral keys as necessary and performs the actual shared secret derivation. |
|
40 |
* |
|
41 |
* @since 1.6 |
|
42 |
* @author Andreas Sterbenz |
|
43 |
*/ |
|
44 |
final class ECDHCrypt { |
|
45 |
||
46 |
// our private key |
|
47 |
private PrivateKey privateKey; |
|
48 |
||
49 |
// our public key |
|
50 |
private ECPublicKey publicKey; |
|
51 |
||
52 |
// Called by ServerHandshaker for static ECDH |
|
53 |
ECDHCrypt(PrivateKey privateKey, PublicKey publicKey) { |
|
54 |
this.privateKey = privateKey; |
|
55 |
this.publicKey = (ECPublicKey)publicKey; |
|
56 |
} |
|
57 |
||
58 |
// Called by ServerHandshaker for ephemeral ECDH |
|
59 |
ECDHCrypt(String curveName, SecureRandom random) { |
|
60 |
try { |
|
61 |
KeyPairGenerator kpg = JsseJce.getKeyPairGenerator("EC"); |
|
62 |
ECGenParameterSpec params = new ECGenParameterSpec(curveName); |
|
63 |
kpg.initialize(params, random); |
|
64 |
KeyPair kp = kpg.generateKeyPair(); |
|
65 |
privateKey = kp.getPrivate(); |
|
66 |
publicKey = (ECPublicKey)kp.getPublic(); |
|
67 |
} catch (GeneralSecurityException e) { |
|
68 |
throw new RuntimeException("Could not generate DH keypair", e); |
|
69 |
} |
|
70 |
} |
|
71 |
||
72 |
// Called by ClientHandshaker with params it received from the server |
|
73 |
ECDHCrypt(ECParameterSpec params, SecureRandom random) { |
|
74 |
try { |
|
75 |
KeyPairGenerator kpg = JsseJce.getKeyPairGenerator("EC"); |
|
76 |
kpg.initialize(params, random); |
|
77 |
KeyPair kp = kpg.generateKeyPair(); |
|
78 |
privateKey = kp.getPrivate(); |
|
79 |
publicKey = (ECPublicKey)kp.getPublic(); |
|
80 |
} catch (GeneralSecurityException e) { |
|
81 |
throw new RuntimeException("Could not generate DH keypair", e); |
|
82 |
} |
|
83 |
} |
|
84 |
||
85 |
/** |
|
86 |
* Gets the public key of this end of the key exchange. |
|
87 |
*/ |
|
88 |
PublicKey getPublicKey() { |
|
89 |
return publicKey; |
|
90 |
} |
|
91 |
||
31712 | 92 |
// called by ClientHandshaker with either the server's static or |
93 |
// ephemeral public key |
|
94 |
SecretKey getAgreedSecret( |
|
95 |
PublicKey peerPublicKey) throws SSLHandshakeException { |
|
96 |
||
2 | 97 |
try { |
98 |
KeyAgreement ka = JsseJce.getKeyAgreement("ECDH"); |
|
99 |
ka.init(privateKey); |
|
100 |
ka.doPhase(peerPublicKey, true); |
|
101 |
return ka.generateSecret("TlsPremasterSecret"); |
|
102 |
} catch (GeneralSecurityException e) { |
|
25807
6c325960c9ee
8042982: Unexpected RuntimeExceptions being thrown by SSLEngine
robm
parents:
23010
diff
changeset
|
103 |
throw (SSLHandshakeException) new SSLHandshakeException( |
6c325960c9ee
8042982: Unexpected RuntimeExceptions being thrown by SSLEngine
robm
parents:
23010
diff
changeset
|
104 |
"Could not generate secret").initCause(e); |
2 | 105 |
} |
106 |
} |
|
107 |
||
108 |
// called by ServerHandshaker |
|
31712 | 109 |
SecretKey getAgreedSecret( |
110 |
byte[] encodedPoint) throws SSLHandshakeException { |
|
111 |
||
2 | 112 |
try { |
113 |
ECParameterSpec params = publicKey.getParams(); |
|
31712 | 114 |
ECPoint point = |
115 |
JsseJce.decodePoint(encodedPoint, params.getCurve()); |
|
2 | 116 |
KeyFactory kf = JsseJce.getKeyFactory("EC"); |
117 |
ECPublicKeySpec spec = new ECPublicKeySpec(point, params); |
|
118 |
PublicKey peerPublicKey = kf.generatePublic(spec); |
|
119 |
return getAgreedSecret(peerPublicKey); |
|
25807
6c325960c9ee
8042982: Unexpected RuntimeExceptions being thrown by SSLEngine
robm
parents:
23010
diff
changeset
|
120 |
} catch (GeneralSecurityException | java.io.IOException e) { |
6c325960c9ee
8042982: Unexpected RuntimeExceptions being thrown by SSLEngine
robm
parents:
23010
diff
changeset
|
121 |
throw (SSLHandshakeException) new SSLHandshakeException( |
6c325960c9ee
8042982: Unexpected RuntimeExceptions being thrown by SSLEngine
robm
parents:
23010
diff
changeset
|
122 |
"Could not generate secret").initCause(e); |
2 | 123 |
} |
124 |
} |
|
125 |
||
31712 | 126 |
// Check constraints of the specified EC public key. |
127 |
void checkConstraints(AlgorithmConstraints constraints, |
|
128 |
byte[] encodedPoint) throws SSLHandshakeException { |
|
129 |
||
130 |
try { |
|
131 |
||
132 |
ECParameterSpec params = publicKey.getParams(); |
|
133 |
ECPoint point = |
|
134 |
JsseJce.decodePoint(encodedPoint, params.getCurve()); |
|
135 |
ECPublicKeySpec spec = new ECPublicKeySpec(point, params); |
|
136 |
||
137 |
KeyFactory kf = JsseJce.getKeyFactory("EC"); |
|
138 |
ECPublicKey publicKey = (ECPublicKey)kf.generatePublic(spec); |
|
139 |
||
140 |
// check constraints of ECPublicKey |
|
141 |
if (!constraints.permits( |
|
142 |
EnumSet.of(CryptoPrimitive.KEY_AGREEMENT), publicKey)) { |
|
143 |
throw new SSLHandshakeException( |
|
144 |
"ECPublicKey does not comply to algorithm constraints"); |
|
145 |
} |
|
146 |
} catch (GeneralSecurityException | java.io.IOException e) { |
|
147 |
throw (SSLHandshakeException) new SSLHandshakeException( |
|
148 |
"Could not generate ECPublicKey").initCause(e); |
|
149 |
} |
|
150 |
} |
|
151 |
||
2 | 152 |
} |