blob: db27b0ba0597d50233e5442fb8740800bc2b400d [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 2003-2007 Sun Microsystems, Inc. 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. Sun designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Sun 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
22 * CA 95054 USA or visit www.sun.com if you need additional information or
23 * have any questions.
24 */
25
26package sun.security.ssl;
27
28import java.io.*;
29import java.security.*;
30import java.security.interfaces.*;
31
32import javax.net.ssl.*;
33
34import sun.security.krb5.EncryptionKey;
35import sun.security.krb5.EncryptedData;
36import sun.security.krb5.KrbException;
37import sun.security.krb5.internal.crypto.KeyUsage;
38
39/**
40 * This is the Kerberos premaster secret in the Kerberos client key
41 * exchange message (CLIENT --> SERVER); it holds the
42 * Kerberos-encrypted pre-master secret. The secret is encrypted using the
43 * Kerberos session key. The padding and size of the resulting message
44 * depends on the session key type, but the pre-master secret is
45 * always exactly 48 bytes.
46 *
47 */
48final class KerberosPreMasterSecret {
49
50 private ProtocolVersion protocolVersion; // preMaster [0,1]
51 private byte preMaster[]; // 48 bytes
52 private byte encrypted[];
53
54 /**
55 * Constructor used by client to generate premaster secret.
56 *
57 * Client randomly creates a pre-master secret and encrypts it
58 * using the Kerberos session key; only the server can decrypt
59 * it, using the session key available in the service ticket.
60 *
61 * @param protocolVersion used to set preMaster[0,1]
62 * @param generator random number generator for generating premaster secret
63 * @param sessionKey Kerberos session key for encrypting premaster secret
64 */
65 KerberosPreMasterSecret(ProtocolVersion protocolVersion,
66 SecureRandom generator, EncryptionKey sessionKey) throws IOException {
67
68 if (sessionKey.getEType() ==
69 EncryptedData.ETYPE_DES3_CBC_HMAC_SHA1_KD) {
70 throw new IOException(
71 "session keys with des3-cbc-hmac-sha1-kd encryption type " +
72 "are not supported for TLS Kerberos cipher suites");
73 }
74
75 this.protocolVersion = protocolVersion;
76 preMaster = generatePreMaster(generator, protocolVersion);
77
78 // Encrypt premaster secret
79 try {
80 EncryptedData eData = new EncryptedData(sessionKey, preMaster,
81 KeyUsage.KU_UNKNOWN);
82 encrypted = eData.getBytes(); // not ASN.1 encoded.
83
84 } catch (KrbException e) {
85 throw (SSLKeyException)new SSLKeyException
86 ("Kerberos premaster secret error").initCause(e);
87 }
88 }
89
90 /*
91 * Constructor used by server to decrypt encrypted premaster secret.
92 * The protocol version in preMaster[0,1] must match either currentVersion
93 * or clientVersion, otherwise, the premaster secret is set to
94 * a random one to foil possible attack.
95 *
96 * @param currentVersion version of protocol being used
97 * @param clientVersion version requested by client
98 * @param generator random number generator used to generate
99 * bogus premaster secret if premaster secret verification fails
100 * @param input input stream from which to read the encrypted
101 * premaster secret
102 * @param sessionKey Kerberos session key to be used for decryption
103 */
104 KerberosPreMasterSecret(ProtocolVersion currentVersion,
105 ProtocolVersion clientVersion,
106 SecureRandom generator, HandshakeInStream input,
107 EncryptionKey sessionKey) throws IOException {
108
109 // Extract encrypted premaster secret from message
110 encrypted = input.getBytes16();
111
112 if (HandshakeMessage.debug != null && Debug.isOn("handshake")) {
113 if (encrypted != null) {
114 Debug.println(System.out,
115 "encrypted premaster secret", encrypted);
116 }
117 }
118
119 if (sessionKey.getEType() ==
120 EncryptedData.ETYPE_DES3_CBC_HMAC_SHA1_KD) {
121 throw new IOException(
122 "session keys with des3-cbc-hmac-sha1-kd encryption type " +
123 "are not supported for TLS Kerberos cipher suites");
124 }
125
126 // Decrypt premaster secret
127 try {
128 EncryptedData data = new EncryptedData(sessionKey.getEType(),
129 null /* optional kvno */, encrypted);
130
131 byte[] temp = data.decrypt(sessionKey, KeyUsage.KU_UNKNOWN);
132 if (HandshakeMessage.debug != null && Debug.isOn("handshake")) {
133 if (encrypted != null) {
134 Debug.println(System.out,
135 "decrypted premaster secret", temp);
136 }
137 }
138
139 // Reset data stream after decryption, remove redundant bytes
140 preMaster = data.reset(temp, false);
141
142 protocolVersion = ProtocolVersion.valueOf(preMaster[0],
143 preMaster[1]);
144 if (HandshakeMessage.debug != null && Debug.isOn("handshake")) {
145 System.out.println("Kerberos PreMasterSecret version: "
146 + protocolVersion);
147 }
148 } catch (Exception e) {
149 // catch exception & process below
150 preMaster = null;
151 protocolVersion = currentVersion;
152 }
153
154 // check if the premaster secret version is ok
155 // the specification says that it must be the maximum version supported
156 // by the client from its ClientHello message. However, many
157 // implementations send the negotiated version, so accept both
158 // NOTE that we may be comparing two unsupported version numbers in
159 // the second case, which is why we cannot use object references
160 // equality in this special case
161 boolean versionMismatch = (protocolVersion != currentVersion) &&
162 (protocolVersion.v != clientVersion.v);
163
164
165 /*
166 * Bogus decrypted ClientKeyExchange? If so, conjure a
167 * a random preMaster secret that will fail later during
168 * Finished message processing. This is a countermeasure against
169 * the "interactive RSA PKCS#1 encryption envelop attack" reported
170 * in June 1998. Preserving the executation path will
171 * mitigate timing attacks and force consistent error handling
172 * that will prevent an attacking client from differentiating
173 * different kinds of decrypted ClientKeyExchange bogosities.
174 */
175 if ((preMaster == null) || (preMaster.length != 48)
176 || versionMismatch) {
177 if (HandshakeMessage.debug != null && Debug.isOn("handshake")) {
178 System.out.println("Kerberos PreMasterSecret error, "
179 + "generating random secret");
180 if (preMaster != null) {
181 Debug.println(System.out, "Invalid secret", preMaster);
182 }
183 }
184 preMaster = generatePreMaster(generator, currentVersion);
185 protocolVersion = currentVersion;
186 }
187 }
188
189 /*
190 * Used by server to generate premaster secret in case of
191 * problem decoding ticket.
192 *
193 * @param protocolVersion used for preMaster[0,1]
194 * @param generator random number generator to use for generating secret.
195 */
196 KerberosPreMasterSecret(ProtocolVersion protocolVersion,
197 SecureRandom generator) {
198
199 this.protocolVersion = protocolVersion;
200 preMaster = generatePreMaster(generator, protocolVersion);
201 }
202
203 private static byte[] generatePreMaster(SecureRandom rand,
204 ProtocolVersion ver) {
205
206 byte[] pm = new byte[48];
207 rand.nextBytes(pm);
208 pm[0] = ver.major;
209 pm[1] = ver.minor;
210
211 return pm;
212 }
213
214 // Clone not needed; package internal use only
215 byte[] getUnencrypted() {
216 return preMaster;
217 }
218
219 // Clone not needed; package internal use only
220 byte[] getEncrypted() {
221 return encrypted;
222 }
223}