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