001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: /**
019: * @author Boris Kuznetsov
020: * @version $Revision$
021: */package org.apache.harmony.xnet.provider.jsse;
022:
023: import java.util.Hashtable;
024:
025: /**
026: *
027: * Represents Protocol Version
028: */
029: public class ProtocolVersion {
030:
031: /**
032: * Protocol name
033: */
034: public final String name;
035:
036: /**
037: * Protocol version as byte array
038: */
039: public final byte[] version;
040:
041: /**
042: * Protocols supported by this provider implementaton
043: */
044: public static final String[] supportedProtocols = new String[] {
045: "TLSv1", "SSLv3" };
046:
047: private static Hashtable protocolsByName = new Hashtable(4);
048:
049: private ProtocolVersion(String name, byte[] version) {
050: this .name = name;
051: this .version = version;
052: }
053:
054: /**
055: * Compares this ProtocolVersion to the specified object.
056: */
057: public boolean equals(Object o) {
058: if (o instanceof ProtocolVersion
059: && this .version[0] == ((ProtocolVersion) o).version[0]
060: && this .version[1] == ((ProtocolVersion) o).version[1]) {
061: return true;
062: }
063: return false;
064: }
065:
066: /**
067: *
068: * Returns true if protocol version is supported
069: *
070: * @param version
071: */
072: public static boolean isSupported(byte[] version) {
073: if (version[0] != 3 || (version[1] != 0 && version[1] != 1)) {
074: return false;
075: }
076: return true;
077: }
078:
079: /**
080: * Returns ProtocolVersion
081: *
082: * @param version
083: * @return
084: */
085: public static ProtocolVersion getByVersion(byte[] version) {
086: if (version[0] == 3) {
087: if (version[1] == 1) {
088: return TLSv1;
089: }
090: if (version[1] == 0) {
091: return SSLv3;
092: }
093: }
094: return null;
095: }
096:
097: /**
098: * Returns true if provider supports protocol version
099: *
100: * @param name
101: * @return
102: */
103: public static boolean isSupported(String name) {
104: return protocolsByName.containsKey(name);
105: }
106:
107: /**
108: * Returns ProtocolVersion
109: *
110: * @param name
111: * @return
112: */
113: public static ProtocolVersion getByName(String name) {
114: return (ProtocolVersion) protocolsByName.get(name);
115: }
116:
117: /**
118: * Highest protocol version supported by provider implementation
119: *
120: * @param protocols
121: * @return
122: */
123: public static ProtocolVersion getLatestVersion(String[] protocols) {
124: if (protocols == null || protocols.length == 0) {
125: return null;
126: }
127: ProtocolVersion latest = getByName(protocols[0]);
128: ProtocolVersion current;
129: for (int i = 1; i < protocols.length; i++) {
130: current = getByName(protocols[i]);
131: if (current == null) {
132: continue;
133: }
134: if ((latest == null)
135: || (latest.version[0] < current.version[0])
136: || (latest.version[0] == current.version[0] && latest.version[1] < current.version[1])) {
137: latest = current;
138: }
139: }
140: return latest;
141:
142: }
143:
144: /**
145: * SSL 3.0 protocol version
146: */
147: public static ProtocolVersion SSLv3 = new ProtocolVersion("SSLv3",
148: new byte[] { 3, 0 });
149:
150: /**
151: * TLS 1.0 protocol version
152: */
153: public static ProtocolVersion TLSv1 = new ProtocolVersion("TLSv1",
154: new byte[] { 3, 1 });
155:
156: static {
157: protocolsByName.put(SSLv3.name, SSLv3);
158: protocolsByName.put(TLSv1.name, TLSv1);
159: protocolsByName.put("SSL", SSLv3);
160: protocolsByName.put("TLS", TLSv1);
161: }
162:
163: }
|