1 /* 2 * Copyright (C) 2014 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package libcore.tlswire.handshake; 18 19 import libcore.tlswire.util.TlsProtocolVersion; 20 import libcore.tlswire.util.IoUtils; 21 import java.io.ByteArrayInputStream; 22 import java.io.DataInput; 23 import java.io.DataInputStream; 24 import java.io.EOFException; 25 import java.io.IOException; 26 import java.math.BigInteger; 27 import java.util.ArrayList; 28 import java.util.List; 29 30 /** 31 * {@link ClientHello} {@link HandshakeMessage} from TLS 1.2 RFC 5246. 32 */ 33 public class ClientHello extends HandshakeMessage { 34 public TlsProtocolVersion clientVersion; 35 public byte[] random; 36 public byte[] sessionId; 37 public List<CipherSuite> cipherSuites; 38 public List<CompressionMethod> compressionMethods; 39 40 /** Extensions or {@code null} for no extensions. */ 41 public List<HelloExtension> extensions; 42 43 @Override 44 protected void parseBody(DataInput in) throws IOException { 45 clientVersion = TlsProtocolVersion.read(in); 46 random = new byte[32]; 47 in.readFully(random); 48 sessionId = IoUtils.readTlsVariableLengthByteVector(in, 32); 49 int[] cipherSuiteCodes = IoUtils.readTlsVariableLengthUnsignedShortVector(in, 0xfffe); 50 cipherSuites = new ArrayList<CipherSuite>(cipherSuiteCodes.length); 51 for (int i = 0; i < cipherSuiteCodes.length; i++) { 52 cipherSuites.add(CipherSuite.valueOf(cipherSuiteCodes[i])); 53 } 54 byte[] compressionMethodCodes = IoUtils.readTlsVariableLengthByteVector(in, 0xff); 55 compressionMethods = new ArrayList<CompressionMethod>(compressionMethodCodes.length); 56 for (int i = 0; i < compressionMethodCodes.length; i++) { 57 int code = compressionMethodCodes[i] & 0xff; 58 compressionMethods.add(CompressionMethod.valueOf(code)); 59 } 60 61 int extensionsSectionSize; 62 try { 63 extensionsSectionSize = in.readUnsignedShort(); 64 } catch (EOFException e) { 65 // No extensions present 66 extensionsSectionSize = 0; 67 } 68 69 if (extensionsSectionSize > 0) { 70 extensions = new ArrayList<HelloExtension>(); 71 byte[] extensionsBytes = new byte[extensionsSectionSize]; 72 in.readFully(extensionsBytes); 73 ByteArrayInputStream extensionsIn = new ByteArrayInputStream(extensionsBytes); 74 DataInput extensionsDataIn = new DataInputStream(extensionsIn); 75 while (extensionsIn.available() > 0) { 76 try { 77 extensions.add(HelloExtension.read(extensionsDataIn)); 78 } catch (IOException e) { 79 throw new IOException( 80 "Failed to read HelloExtension #" + (extensions.size() + 1)); 81 } 82 } 83 } 84 } 85 86 public HelloExtension findExtensionByType(int extensionType) { 87 if (extensions == null) { 88 return null; 89 } 90 for (HelloExtension extension : extensions) { 91 if (extension.type == extensionType) { 92 return extension; 93 } 94 } 95 return null; 96 } 97 98 @Override 99 public String toString() { 100 return "ClientHello{client version: " + clientVersion 101 + ", random: " + new BigInteger(1, random).toString(16) 102 + ", sessionId: " + new BigInteger(1, sessionId).toString(16) 103 + ", cipher suites: " + cipherSuites 104 + ", compression methods: " + compressionMethods 105 + ((extensions != null) ? (", extensions: " + String.valueOf(extensions)) : "") 106 + "}"; 107 } 108 } 109