< prev index next >
src/share/classes/sun/security/ssl/Authenticator.java
Print this page
rev 13968 : 8245467: Remove 8u TLSv1.2 implementation files
rev 13969 : 8245468: Add TLSv1.3 implementation classes from 11.0.7
rev 13970 : 8245469: Remove DTLS protocol implementation
*** 32,42 ****
import javax.crypto.Mac;
import javax.crypto.SecretKey;
import sun.security.ssl.CipherSuite.MacAlg;
/**
! * This class represents an SSL/TLS/DTLS message authentication token,
* which encapsulates a sequence number and ensures that attempts to
* delete or reorder messages can be detected.
*/
abstract class Authenticator {
// byte array containing the additional authentication information for
--- 32,42 ----
import javax.crypto.Mac;
import javax.crypto.SecretKey;
import sun.security.ssl.CipherSuite.MacAlg;
/**
! * This class represents an SSL/TLS message authentication token,
* which encapsulates a sequence number and ensures that attempts to
* delete or reorder messages can be detected.
*/
abstract class Authenticator {
// byte array containing the additional authentication information for
*** 50,106 ****
/**
* Constructs the message authentication token for the specified
* SSL/TLS protocol.
*/
static Authenticator valueOf(ProtocolVersion protocolVersion) {
- if (protocolVersion.isDTLS) {
- if (protocolVersion.useTLS13PlusSpec()) {
- return new DTLS13Authenticator(protocolVersion);
- } else {
- return new DTLS10Authenticator(protocolVersion);
- }
- } else {
if (protocolVersion.useTLS13PlusSpec()) {
return new TLS13Authenticator(protocolVersion);
} else if (protocolVersion.useTLS10PlusSpec()) {
return new TLS10Authenticator(protocolVersion);
} else {
return new SSL30Authenticator();
}
}
- }
@SuppressWarnings({"unchecked"})
static <T extends Authenticator & MAC> T
valueOf(ProtocolVersion protocolVersion, MacAlg macAlg,
SecretKey key) throws NoSuchAlgorithmException,
InvalidKeyException {
- if (protocolVersion.isDTLS) {
- if (protocolVersion.useTLS13PlusSpec()) {
- throw new RuntimeException("No MacAlg used in DTLS 1.3");
- } else {
- return (T)(new DTLS10Mac(protocolVersion, macAlg, key));
- }
- } else {
if (protocolVersion.useTLS13PlusSpec()) {
throw new RuntimeException("No MacAlg used in TLS 1.3");
} else if (protocolVersion.useTLS10PlusSpec()) {
return (T)(new TLS10Mac(protocolVersion, macAlg, key));
} else {
return (T)(new SSL30Mac(protocolVersion, macAlg, key));
}
}
- }
static Authenticator nullTlsMac() {
return new SSLNullMac();
}
- static Authenticator nullDtlsMac() {
- return new DTLSNullMac();
- }
-
/**
* Checks whether the sequence number is close to wrap.
*
* Sequence numbers are of type uint64 and may not exceed 2^64-1.
* Sequence numbers do not wrap. When the sequence number is near
--- 50,86 ----
*** 121,148 ****
* @return true if the sequence number is huge enough to renew
*/
abstract boolean seqNumIsHuge();
/**
! * Gets the current sequence number, including the epoch number for
! * DTLS protocols.
*
* @return the byte array of the current sequence number
*/
final byte[] sequenceNumber() {
return Arrays.copyOf(block, 8);
}
/**
- * Sets the epoch number (only apply to DTLS protocols).
- */
- void setEpochNumber(int epoch) {
- throw new UnsupportedOperationException(
- "Epoch numbers apply to DTLS protocols only");
- }
-
- /**
* Increase the sequence number.
*/
final void increaseSequenceNumber() {
/*
* The sequence number in the block array is a 64-bit
--- 101,119 ----
* @return true if the sequence number is huge enough to renew
*/
abstract boolean seqNumIsHuge();
/**
! * Gets the current sequence number.
*
* @return the byte array of the current sequence number
*/
final byte[] sequenceNumber() {
return Arrays.copyOf(block, 8);
}
/**
* Increase the sequence number.
*/
final void increaseSequenceNumber() {
/*
* The sequence number in the block array is a 64-bit
*** 291,410 ****
return ad;
}
}
- private static class DTLSAuthenticator extends Authenticator {
- private DTLSAuthenticator(byte[] block) {
- super(block);
- }
-
- @Override
- boolean seqNumOverflow() {
- /*
- * Conservatively, we don't allow more records to be generated
- * when there are only 2^8 sequence numbers left.
- */
- return (block.length != 0 &&
- // no epoch bytes, block[0] and block[1]
- block[2] == (byte)0xFF && block[3] == (byte)0xFF &&
- block[4] == (byte)0xFF && block[5] == (byte)0xFF &&
- block[6] == (byte)0xFF);
- }
-
- @Override
- boolean seqNumIsHuge() {
- return (block.length != 0 &&
- // no epoch bytes, block[0] and block[1]
- block[2] == (byte)0xFF && block[3] == (byte)0xFF);
- }
-
- @Override
- void setEpochNumber(int epoch) {
- block[0] = (byte)((epoch >> 8) & 0xFF);
- block[1] = (byte)(epoch & 0xFF);
- }
- }
-
- // For null MAC only.
- private static class DTLSNullAuthenticator extends DTLSAuthenticator {
- private DTLSNullAuthenticator() {
- // For DTLS protocols, plaintexts use explicit epoch and
- // sequence number in each record. The first 8 byte of
- // the block is initialized for null MAC so that the
- // epoch and sequence number can be acquired to generate
- // plaintext records.
- super(new byte[8]);
- }
- }
-
- // DTLS 1.0/1.2
- private static class DTLS10Authenticator extends DTLSAuthenticator {
- // Block size of DTLS v1.0 and later:
- // epoch + sequence number +
- // record type + protocol version + record length
- private static final int BLOCK_SIZE = 13; // 2 + 6 + 1 + 2 + 2;
-
- private DTLS10Authenticator(ProtocolVersion protocolVersion) {
- super(new byte[BLOCK_SIZE]);
- block[9] = protocolVersion.major;
- block[10] = protocolVersion.minor;
- }
-
- @Override
- byte[] acquireAuthenticationBytes(
- byte type, int length, byte[] sequence) {
- byte[] ad = block.clone();
- if (sequence != null) {
- if (sequence.length != 8) {
- throw new RuntimeException(
- "Insufficient explicit sequence number bytes");
- }
-
- System.arraycopy(sequence, 0, ad, 0, sequence.length);
- } else { // Otherwise, use the implicit sequence number.
- // Increase the implicit sequence number in the block array.
- increaseSequenceNumber();
- }
-
- ad[8] = type;
- ad[11] = (byte)(length >> 8);
- ad[12] = (byte)(length);
-
- return ad;
- }
- }
-
- // DTLS 1.3
- private static final class DTLS13Authenticator extends DTLSAuthenticator {
- // Block size of DTLS v1.0 and later:
- // epoch + sequence number +
- // record type + protocol version + record length
- private static final int BLOCK_SIZE = 13; // 2 + 6 + 1 + 2 + 2;
-
- private DTLS13Authenticator(ProtocolVersion protocolVersion) {
- super(new byte[BLOCK_SIZE]);
- block[9] = ProtocolVersion.TLS12.major;
- block[10] = ProtocolVersion.TLS12.minor;
- }
-
- @Override
- byte[] acquireAuthenticationBytes(
- byte type, int length, byte[] sequence) {
- byte[] ad = Arrays.copyOfRange(block, 8, 13);
-
- // Increase the implicit sequence number in the block array.
- increaseSequenceNumber();
-
- ad[0] = type;
- ad[3] = (byte)(length >> 8);
- ad[4] = (byte)(length & 0xFF);
-
- return ad;
- }
- }
-
interface MAC {
MacAlg macAlg();
/**
* Compute and returns the MAC for the remaining data
--- 262,271 ----
*** 580,633 ****
byte[] sequence, boolean isSimulated) {
return macImpl.compute(type, bb, sequence, isSimulated);
}
}
- // NULL DTLS MAC
- private static final
- class DTLSNullMac extends DTLSNullAuthenticator implements MAC {
- private final MacImpl macImpl;
- public DTLSNullMac() {
- super();
- this.macImpl = new MacImpl();
- }
-
- @Override
- public MacAlg macAlg() {
- return macImpl.macAlg;
- }
-
- @Override
- public byte[] compute(byte type, ByteBuffer bb,
- byte[] sequence, boolean isSimulated) {
- return macImpl.compute(type, bb, sequence, isSimulated);
- }
- }
-
- // DTLS 1.0/1.2
- private static final class DTLS10Mac
- extends DTLS10Authenticator implements MAC {
- private final MacImpl macImpl;
- public DTLS10Mac(ProtocolVersion protocolVersion,
- MacAlg macAlg, SecretKey key) throws NoSuchAlgorithmException,
- InvalidKeyException {
- super(protocolVersion);
- this.macImpl = new MacImpl(protocolVersion, macAlg, key);
- }
-
- @Override
- public MacAlg macAlg() {
- return macImpl.macAlg;
- }
-
- @Override
- public byte[] compute(byte type, ByteBuffer bb,
- byte[] sequence, boolean isSimulated) {
- return macImpl.compute(type, bb, sequence, isSimulated);
- }
- }
-
static final long toLong(byte[] recordEnS) {
if (recordEnS != null && recordEnS.length == 8) {
return ((recordEnS[0] & 0xFFL) << 56) |
((recordEnS[1] & 0xFFL) << 48) |
((recordEnS[2] & 0xFFL) << 40) |
--- 441,450 ----
< prev index next >