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.IoUtils; 20 import java.io.ByteArrayInputStream; 21 import java.io.DataInputStream; 22 import java.io.IOException; 23 import java.util.ArrayList; 24 import java.util.List; 25 26 /** 27 * {@code server_name} (SNI) {@link HelloExtension} from TLS 1.2 RFC 5246. 28 */ 29 public class ServerNameHelloExtension extends HelloExtension { 30 private static final int TYPE_HOST_NAME = 0; 31 32 public List<String> hostnames; 33 34 @Override 35 protected void parseData() throws IOException { 36 byte[] serverNameListBytes = IoUtils.readTlsVariableLengthByteVector( 37 new DataInputStream(new ByteArrayInputStream(data)), 0xffff); 38 ByteArrayInputStream serverNameListIn = new ByteArrayInputStream(serverNameListBytes); 39 DataInputStream in = new DataInputStream(serverNameListIn); 40 hostnames = new ArrayList<String>(); 41 while (serverNameListIn.available() > 0) { 42 int type = in.readUnsignedByte(); 43 if (type != TYPE_HOST_NAME) { 44 throw new IOException("Unsupported ServerName type: " + type); 45 } 46 byte[] hostnameBytes = IoUtils.readTlsVariableLengthByteVector(in, 0xffff); 47 String hostname = new String(hostnameBytes, "US-ASCII"); 48 hostnames.add(hostname); 49 } 50 } 51 52 @Override 53 public String toString() { 54 return "HelloExtension{type: server_name, hostnames: " + hostnames + "}"; 55 } 56 } 57