Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/jdk.crypto.cryptoki/share/classes/sun/security/pkcs11/P11TlsMasterSecretGenerator.java
41154 views
1
/*
2
* Copyright (c) 2005, 2018, Oracle and/or its affiliates. All rights reserved.
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
7
* published by the Free Software Foundation. Oracle designates this
8
* particular file as subject to the "Classpath" exception as provided
9
* by Oracle in the LICENSE file that accompanied this code.
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
*
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.
24
*/
25
26
package sun.security.pkcs11;
27
28
import java.security.*;
29
import java.security.spec.AlgorithmParameterSpec;
30
31
import javax.crypto.*;
32
import sun.security.internal.spec.TlsMasterSecretParameterSpec;
33
34
import static sun.security.pkcs11.TemplateManager.*;
35
import sun.security.pkcs11.wrapper.*;
36
37
import static sun.security.pkcs11.wrapper.PKCS11Constants.*;
38
39
/**
40
* KeyGenerator for the SSL/TLS master secret.
41
*
42
* @author Andreas Sterbenz
43
* @since 1.6
44
*/
45
public final class P11TlsMasterSecretGenerator extends KeyGeneratorSpi {
46
47
private static final String MSG = "TlsMasterSecretGenerator must be "
48
+ "initialized using a TlsMasterSecretParameterSpec";
49
50
// token instance
51
private final Token token;
52
53
// algorithm name
54
private final String algorithm;
55
56
// mechanism id
57
private long mechanism;
58
59
private int tlsVersion;
60
61
@SuppressWarnings("deprecation")
62
private TlsMasterSecretParameterSpec spec;
63
private P11Key p11Key;
64
65
CK_VERSION ckVersion;
66
67
// whether SSLv3 is supported
68
private final boolean supportSSLv3;
69
70
P11TlsMasterSecretGenerator(Token token, String algorithm, long mechanism)
71
throws PKCS11Exception {
72
super();
73
this.token = token;
74
this.algorithm = algorithm;
75
this.mechanism = mechanism;
76
77
// Given the current lookup order specified in SunPKCS11.java, if
78
// CKM_SSL3_MASTER_KEY_DERIVE is not used to construct this object,
79
// it means that this mech is disabled or unsupported.
80
supportSSLv3 = (mechanism == CKM_SSL3_MASTER_KEY_DERIVE);
81
}
82
83
protected void engineInit(SecureRandom random) {
84
throw new InvalidParameterException(MSG);
85
}
86
87
@SuppressWarnings("deprecation")
88
protected void engineInit(AlgorithmParameterSpec params,
89
SecureRandom random) throws InvalidAlgorithmParameterException {
90
if (params instanceof TlsMasterSecretParameterSpec == false) {
91
throw new InvalidAlgorithmParameterException(MSG);
92
}
93
94
TlsMasterSecretParameterSpec spec = (TlsMasterSecretParameterSpec)params;
95
tlsVersion = (spec.getMajorVersion() << 8) | spec.getMinorVersion();
96
if ((tlsVersion == 0x0300 && !supportSSLv3) ||
97
(tlsVersion < 0x0300) || (tlsVersion > 0x0303)) {
98
throw new InvalidAlgorithmParameterException
99
("Only" + (supportSSLv3? " SSL 3.0,": "") +
100
" TLS 1.0, TLS 1.1 and TLS 1.2 are supported (" +
101
tlsVersion + ")");
102
}
103
104
SecretKey key = spec.getPremasterSecret();
105
// algorithm should be either TlsRsaPremasterSecret or TlsPremasterSecret,
106
// but we omit the check
107
try {
108
p11Key = P11SecretKeyFactory.convertKey(token, key, null);
109
} catch (InvalidKeyException e) {
110
throw new InvalidAlgorithmParameterException("init() failed", e);
111
}
112
this.spec = spec;
113
final boolean isTlsRsaPremasterSecret =
114
p11Key.getAlgorithm().equals("TlsRsaPremasterSecret");
115
if (tlsVersion == 0x0300) {
116
mechanism = isTlsRsaPremasterSecret ?
117
CKM_SSL3_MASTER_KEY_DERIVE : CKM_SSL3_MASTER_KEY_DERIVE_DH;
118
} else if (tlsVersion == 0x0301 || tlsVersion == 0x0302) {
119
mechanism = isTlsRsaPremasterSecret ?
120
CKM_TLS_MASTER_KEY_DERIVE : CKM_TLS_MASTER_KEY_DERIVE_DH;
121
} else if (tlsVersion == 0x0303) {
122
mechanism = isTlsRsaPremasterSecret ?
123
CKM_TLS12_MASTER_KEY_DERIVE : CKM_TLS12_MASTER_KEY_DERIVE_DH;
124
}
125
if (isTlsRsaPremasterSecret) {
126
ckVersion = new CK_VERSION(0, 0);
127
} else {
128
// Note: we use DH for all non-RSA premaster secrets. That includes
129
// Kerberos. That should not be a problem because master secret
130
// calculation is always a straightforward application of the
131
// TLS PRF (or the SSL equivalent).
132
// The only thing special about RSA master secret calculation is
133
// that it extracts the version numbers from the premaster secret.
134
ckVersion = null;
135
}
136
}
137
138
protected void engineInit(int keysize, SecureRandom random) {
139
throw new InvalidParameterException(MSG);
140
}
141
142
protected SecretKey engineGenerateKey() {
143
if (spec == null) {
144
throw new IllegalStateException
145
("TlsMasterSecretGenerator must be initialized");
146
}
147
byte[] clientRandom = spec.getClientRandom();
148
byte[] serverRandom = spec.getServerRandom();
149
CK_SSL3_RANDOM_DATA random =
150
new CK_SSL3_RANDOM_DATA(clientRandom, serverRandom);
151
CK_MECHANISM ckMechanism = null;
152
if (tlsVersion < 0x0303) {
153
CK_SSL3_MASTER_KEY_DERIVE_PARAMS params =
154
new CK_SSL3_MASTER_KEY_DERIVE_PARAMS(random, ckVersion);
155
ckMechanism = new CK_MECHANISM(mechanism, params);
156
} else if (tlsVersion == 0x0303) {
157
CK_TLS12_MASTER_KEY_DERIVE_PARAMS params =
158
new CK_TLS12_MASTER_KEY_DERIVE_PARAMS(random, ckVersion,
159
Functions.getHashMechId(spec.getPRFHashAlg()));
160
ckMechanism = new CK_MECHANISM(mechanism, params);
161
}
162
Session session = null;
163
long p11KeyID = p11Key.getKeyID();
164
try {
165
session = token.getObjSession();
166
CK_ATTRIBUTE[] attributes = token.getAttributes(O_GENERATE,
167
CKO_SECRET_KEY, CKK_GENERIC_SECRET, new CK_ATTRIBUTE[0]);
168
long keyID = token.p11.C_DeriveKey(session.id(),
169
ckMechanism, p11KeyID, attributes);
170
int major, minor;
171
if (ckVersion == null) {
172
major = -1;
173
minor = -1;
174
} else {
175
major = ckVersion.major;
176
minor = ckVersion.minor;
177
}
178
return P11Key.masterSecretKey(session, keyID,
179
"TlsMasterSecret", 48 << 3, attributes, major, minor);
180
} catch (Exception e) {
181
throw new ProviderException("Could not generate key", e);
182
} finally {
183
p11Key.releaseKeyID();
184
token.releaseSession(session);
185
}
186
}
187
}
188
189