Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/sun/security/ssl/Authenticator.java
38830 views
/*1* Copyright (c) 2012, 2018, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package sun.security.ssl;2627import java.nio.ByteBuffer;28import java.security.InvalidKeyException;29import java.security.NoSuchAlgorithmException;30import java.util.Arrays;31import javax.crypto.Mac;32import javax.crypto.SecretKey;33import sun.security.ssl.CipherSuite.MacAlg;3435/**36* This class represents an SSL/TLS message authentication token,37* which encapsulates a sequence number and ensures that attempts to38* delete or reorder messages can be detected.39*/40abstract class Authenticator {41// byte array containing the additional authentication information for42// each record43protected final byte[] block; // at least 8 bytes for sequence number4445private Authenticator(byte[] block) {46this.block = block;47}4849/**50* Constructs the message authentication token for the specified51* SSL/TLS protocol.52*/53static Authenticator valueOf(ProtocolVersion protocolVersion) {54if (protocolVersion.useTLS13PlusSpec()) {55return new TLS13Authenticator(protocolVersion);56} else if (protocolVersion.useTLS10PlusSpec()) {57return new TLS10Authenticator(protocolVersion);58} else {59return new SSL30Authenticator();60}61}6263@SuppressWarnings({"unchecked"})64static <T extends Authenticator & MAC> T65valueOf(ProtocolVersion protocolVersion, MacAlg macAlg,66SecretKey key) throws NoSuchAlgorithmException,67InvalidKeyException {68if (protocolVersion.useTLS13PlusSpec()) {69throw new RuntimeException("No MacAlg used in TLS 1.3");70} else if (protocolVersion.useTLS10PlusSpec()) {71return (T)(new TLS10Mac(protocolVersion, macAlg, key));72} else {73return (T)(new SSL30Mac(protocolVersion, macAlg, key));74}75}7677static Authenticator nullTlsMac() {78return new SSLNullMac();79}8081/**82* Checks whether the sequence number is close to wrap.83*84* Sequence numbers are of type uint64 and may not exceed 2^64-1.85* Sequence numbers do not wrap. When the sequence number is near86* to wrap, we need to close the connection immediately.87*88* @return true if the sequence number is close to wrap89*/90abstract boolean seqNumOverflow();9192/**93* Checks whether the sequence number close to renew.94*95* Sequence numbers are of type uint64 and may not exceed 2^64-1.96* Sequence numbers do not wrap. If a TLS97* implementation would need to wrap a sequence number, it must98* renegotiate instead.99*100* @return true if the sequence number is huge enough to renew101*/102abstract boolean seqNumIsHuge();103104/**105* Gets the current sequence number.106*107* @return the byte array of the current sequence number108*/109final byte[] sequenceNumber() {110return Arrays.copyOf(block, 8);111}112113/**114* Increase the sequence number.115*/116final void increaseSequenceNumber() {117/*118* The sequence number in the block array is a 64-bit119* number stored in big-endian format.120*/121int k = 7;122while ((k >= 0) && (++block[k] == 0)) {123k--;124}125}126127/**128* Acquires the current message authentication information with the129* specified record type and fragment length, and then increases the130* sequence number if using implicit sequence number.131*132* @param type the record type133* @param length the fragment of the record134* @param sequence the explicit sequence number of the record135*136* @return the byte array of the current message authentication information137*/138byte[] acquireAuthenticationBytes(139byte type, int length, byte[] sequence) {140throw new UnsupportedOperationException("Used by AEAD algorithms only");141}142143private static class SSLAuthenticator extends Authenticator {144private SSLAuthenticator(byte[] block) {145super(block);146}147148@Override149boolean seqNumOverflow() {150/*151* Conservatively, we don't allow more records to be generated152* when there are only 2^8 sequence numbers left.153*/154return (block.length != 0 &&155block[0] == (byte)0xFF && block[1] == (byte)0xFF &&156block[2] == (byte)0xFF && block[3] == (byte)0xFF &&157block[4] == (byte)0xFF && block[5] == (byte)0xFF &&158block[6] == (byte)0xFF);159}160161@Override162boolean seqNumIsHuge() {163return (block.length != 0 &&164block[0] == (byte)0xFF && block[1] == (byte)0xFF &&165block[2] == (byte)0xFF && block[3] == (byte)0xFF);166}167}168169// For null MAC only.170private static class SSLNullAuthenticator extends SSLAuthenticator {171private SSLNullAuthenticator() {172super(new byte[8]);173}174}175176// For SSL 3.0177private static class SSL30Authenticator extends SSLAuthenticator {178// Block size of SSL v3.0:179// sequence number + record type + + record length180private static final int BLOCK_SIZE = 11; // 8 + 1 + 2181182private SSL30Authenticator() {183super(new byte[BLOCK_SIZE]);184}185186@Override187byte[] acquireAuthenticationBytes(188byte type, int length, byte[] sequence) {189byte[] ad = block.clone();190191// Increase the implicit sequence number in the block array.192increaseSequenceNumber();193194ad[8] = type;195ad[9] = (byte)(length >> 8);196ad[10] = (byte)(length);197198return ad;199}200}201202// For TLS 1.0 - 1.2203private static class TLS10Authenticator extends SSLAuthenticator {204// Block size of TLS v1.0/1.1/1.2.205// sequence number + record type + protocol version + record length206private static final int BLOCK_SIZE = 13; // 8 + 1 + 2 + 2207208private TLS10Authenticator(ProtocolVersion protocolVersion) {209super(new byte[BLOCK_SIZE]);210block[9] = protocolVersion.major;211block[10] = protocolVersion.minor;212}213214@Override215byte[] acquireAuthenticationBytes(216byte type, int length, byte[] sequence) {217byte[] ad = block.clone();218if (sequence != null) {219if (sequence.length != 8) {220throw new RuntimeException(221"Insufficient explicit sequence number bytes");222}223224System.arraycopy(sequence, 0, ad, 0, sequence.length);225} else { // Otherwise, use the implicit sequence number.226// Increase the implicit sequence number in the block array.227increaseSequenceNumber();228}229230ad[8] = type;231ad[11] = (byte)(length >> 8);232ad[12] = (byte)(length);233234return ad;235}236}237238// For TLS 1.3239private static final class TLS13Authenticator extends SSLAuthenticator {240// Block size of TLS v1.3:241// record type + protocol version + record length + sequence number242private static final int BLOCK_SIZE = 13; // 1 + 2 + 2 + 8243244private TLS13Authenticator(ProtocolVersion protocolVersion) {245super(new byte[BLOCK_SIZE]);246block[9] = ProtocolVersion.TLS12.major;247block[10] = ProtocolVersion.TLS12.minor;248}249250@Override251byte[] acquireAuthenticationBytes(252byte type, int length, byte[] sequence) {253byte[] ad = Arrays.copyOfRange(block, 8, 13);254255// Increase the implicit sequence number in the block array.256increaseSequenceNumber();257258ad[0] = type;259ad[3] = (byte)(length >> 8);260ad[4] = (byte)(length & 0xFF);261262return ad;263}264}265266interface MAC {267MacAlg macAlg();268269/**270* Compute and returns the MAC for the remaining data271* in this ByteBuffer.272*273* On return, the bb position == limit, and limit will274* have not changed.275*276* @param type record type277* @param bb a ByteBuffer in which the position and limit278* demarcate the data to be MAC'd.279* @param isSimulated if true, simulate the MAC computation280* @param sequence the explicit sequence number, or null if using281* the implicit sequence number for the computation282*283* @return the MAC result284*/285byte[] compute(byte type, ByteBuffer bb,286byte[] sequence, boolean isSimulated);287288289/**290* Compute and returns the MAC for the remaining data291* in this ByteBuffer.292*293* On return, the bb position == limit, and limit will294* have not changed.295*296* @param type record type297* @param bb a ByteBuffer in which the position and limit298* demarcate the data to be MAC'd.299* @param isSimulated if true, simulate the MAC computation300*301* @return the MAC result302*/303default byte[] compute(byte type, ByteBuffer bb, boolean isSimulated) {304return compute(type, bb, null, isSimulated);305}306}307308private class MacImpl implements MAC {309// internal identifier for the MAC algorithm310private final MacAlg macAlg;311312// JCE Mac object313private final Mac mac;314315private MacImpl() {316macAlg = MacAlg.M_NULL;317mac = null;318}319320private MacImpl(ProtocolVersion protocolVersion, MacAlg macAlg,321SecretKey key) throws NoSuchAlgorithmException,322InvalidKeyException {323if (macAlg == null) {324throw new RuntimeException("Null MacAlg");325}326327// using SSL MAC computation?328boolean useSSLMac = (protocolVersion.id < ProtocolVersion.TLS10.id);329String algorithm;330switch (macAlg) {331case M_MD5:332algorithm = useSSLMac ? "SslMacMD5" : "HmacMD5";333break;334case M_SHA:335algorithm = useSSLMac ? "SslMacSHA1" : "HmacSHA1";336break;337case M_SHA256:338algorithm = "HmacSHA256"; // TLS 1.2+339break;340case M_SHA384:341algorithm = "HmacSHA384"; // TLS 1.2+342break;343default:344throw new RuntimeException("Unknown MacAlg " + macAlg);345}346347Mac m = JsseJce.getMac(algorithm);348m.init(key);349this.macAlg = macAlg;350this.mac = m;351}352353@Override354public MacAlg macAlg() {355return macAlg;356}357358@Override359public byte[] compute(byte type, ByteBuffer bb,360byte[] sequence, boolean isSimulated) {361362if (macAlg.size == 0) {363return new byte[0];364}365366if (!isSimulated) {367// Uses the explicit sequence number for the computation.368byte[] additional =369acquireAuthenticationBytes(type, bb.remaining(), sequence);370mac.update(additional);371}372mac.update(bb);373374return mac.doFinal();375}376}377378// NULL SSL MAC379private static final380class SSLNullMac extends SSLNullAuthenticator implements MAC {381private final MacImpl macImpl;382public SSLNullMac() {383super();384this.macImpl = new MacImpl();385}386387@Override388public MacAlg macAlg() {389return macImpl.macAlg;390}391392@Override393public byte[] compute(byte type, ByteBuffer bb,394byte[] sequence, boolean isSimulated) {395return macImpl.compute(type, bb, sequence, isSimulated);396}397}398399// For SSL 3.0400private static final401class SSL30Mac extends SSL30Authenticator implements MAC {402private final MacImpl macImpl;403public SSL30Mac(ProtocolVersion protocolVersion,404MacAlg macAlg, SecretKey key) throws NoSuchAlgorithmException,405InvalidKeyException {406super();407this.macImpl = new MacImpl(protocolVersion, macAlg, key);408}409410@Override411public MacAlg macAlg() {412return macImpl.macAlg;413}414415@Override416public byte[] compute(byte type, ByteBuffer bb,417byte[] sequence, boolean isSimulated) {418return macImpl.compute(type, bb, sequence, isSimulated);419}420}421422// For TLS 1.0 - 1.2423private static final424class TLS10Mac extends TLS10Authenticator implements MAC {425private final MacImpl macImpl;426public TLS10Mac(ProtocolVersion protocolVersion,427MacAlg macAlg, SecretKey key) throws NoSuchAlgorithmException,428InvalidKeyException {429super(protocolVersion);430this.macImpl = new MacImpl(protocolVersion, macAlg, key);431}432433@Override434public MacAlg macAlg() {435return macImpl.macAlg;436}437438@Override439public byte[] compute(byte type, ByteBuffer bb,440byte[] sequence, boolean isSimulated) {441return macImpl.compute(type, bb, sequence, isSimulated);442}443}444445static final long toLong(byte[] recordEnS) {446if (recordEnS != null && recordEnS.length == 8) {447return ((recordEnS[0] & 0xFFL) << 56) |448((recordEnS[1] & 0xFFL) << 48) |449((recordEnS[2] & 0xFFL) << 40) |450((recordEnS[3] & 0xFFL) << 32) |451((recordEnS[4] & 0xFFL) << 24) |452((recordEnS[5] & 0xFFL) << 16) |453((recordEnS[6] & 0xFFL) << 8) |454(recordEnS[7] & 0xFFL);455}456457return -1L;458}459}460461462