1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 package org.conscrypt; 18 19 import java.util.Hashtable; 20 21 /** 22 * 23 * Represents Protocol Version 24 */ 25 public class ProtocolVersion { 26 /** 27 * Protocols supported by this provider implementation 28 */ 29 public static final String[] supportedProtocols = new String[] { "TLSv1", 30 "SSLv3" }; 31 32 private static Hashtable<String, ProtocolVersion> protocolsByName = new Hashtable<String, ProtocolVersion>(4); 33 34 /** 35 * 36 * Returns true if protocol version is supported 37 * 38 * @param version 39 */ 40 public static boolean isSupported(byte[] version) { 41 if (version[0] != 3 || (version[1] != 0 && version[1] != 1)) { 42 return false; 43 } 44 return true; 45 } 46 47 /** 48 * Returns ProtocolVersion 49 * 50 * @param version 51 * @return 52 */ 53 public static ProtocolVersion getByVersion(byte[] version) { 54 if (version[0] == 3) { 55 if (version[1] == 1) { 56 return TLSv1; 57 } 58 if (version[1] == 0) { 59 return SSLv3; 60 } 61 } 62 return null; 63 } 64 65 /** 66 * Returns true if provider supports protocol version 67 * 68 * @param name 69 * @return 70 */ 71 public static boolean isSupported(String name) { 72 return protocolsByName.containsKey(name); 73 } 74 75 /** 76 * Returns ProtocolVersion 77 * 78 * @param name 79 * @return 80 */ 81 public static ProtocolVersion getByName(String name) { 82 return protocolsByName.get(name); 83 } 84 85 /** 86 * Highest protocol version supported by provider implementation 87 * 88 * @param protocols 89 * @return 90 */ 91 public static ProtocolVersion getLatestVersion(String[] protocols) { 92 if (protocols == null || protocols.length == 0) { 93 return null; 94 } 95 ProtocolVersion latest = getByName(protocols[0]); 96 ProtocolVersion current; 97 for (int i = 1; i < protocols.length; i++) { 98 current = getByName(protocols[i]); 99 if (current == null) { 100 continue; 101 } 102 if ((latest == null) 103 || (latest.version[0] < current.version[0]) 104 || (latest.version[0] == current.version[0] && latest.version[1] < current.version[1])) { 105 latest = current; 106 } 107 } 108 return latest; 109 110 } 111 112 /** 113 * SSL 3.0 protocol version 114 */ 115 public static final ProtocolVersion SSLv3 = new ProtocolVersion("SSLv3", 116 new byte[] { 3, 0 }); 117 118 /** 119 * TLS 1.0 protocol version 120 */ 121 public static final ProtocolVersion TLSv1 = new ProtocolVersion("TLSv1", 122 new byte[] { 3, 1 }); 123 124 static { 125 protocolsByName.put(SSLv3.name, SSLv3); 126 protocolsByName.put(TLSv1.name, TLSv1); 127 protocolsByName.put("SSL", SSLv3); 128 protocolsByName.put("TLS", TLSv1); 129 } 130 131 /** 132 * Protocol name 133 */ 134 public final String name; 135 136 /** 137 * Protocol version as byte array 138 */ 139 public final byte[] version; 140 141 private ProtocolVersion(String name, byte[] version) { 142 this.name = name; 143 this.version = version; 144 } 145 }