View Javadoc
1   /*
2    * Copyright 2018 The Netty Project
3    *
4    * The Netty Project licenses this file to you under the Apache License,
5    * version 2.0 (the "License"); you may not use this file except in compliance
6    * with the License. You may obtain a copy of the License at:
7    *
8    *   https://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, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations
14   * under the License.
15   */
16  package io.netty.handler.ssl;
17  
18  import io.netty.util.internal.EmptyArrays;
19  
20  import java.security.Principal;
21  import java.security.cert.Certificate;
22  import java.util.Collections;
23  import java.util.List;
24  import java.util.Map;
25  import javax.net.ssl.ExtendedSSLSession;
26  import javax.net.ssl.SNIServerName;
27  import javax.net.ssl.SSLException;
28  import javax.net.ssl.SSLPeerUnverifiedException;
29  import javax.net.ssl.SSLSessionBindingEvent;
30  import javax.net.ssl.SSLSessionBindingListener;
31  import javax.security.cert.X509Certificate;
32  
33  /**
34   * Delegates all operations to a wrapped {@link OpenSslSession} except the methods defined by {@link ExtendedSSLSession}
35   * itself.
36   */
37  abstract class ExtendedOpenSslSession extends ExtendedSSLSession implements OpenSslSession {
38  
39      // TODO: use OpenSSL API to actually fetch the real data but for now just do what Conscrypt does:
40      // https://github.com/google/conscrypt/blob/1.2.0/common/
41      // src/main/java/org/conscrypt/Java7ExtendedSSLSession.java#L32
42      private static final String[] LOCAL_SUPPORTED_SIGNATURE_ALGORITHMS = {
43              "SHA512withRSA", "SHA512withECDSA", "SHA384withRSA", "SHA384withECDSA", "SHA256withRSA",
44              "SHA256withECDSA", "SHA224withRSA", "SHA224withECDSA", "SHA1withRSA", "SHA1withECDSA",
45              "RSASSA-PSS",
46      };
47  
48      private final OpenSslSession wrapped;
49  
50      ExtendedOpenSslSession(OpenSslSession wrapped) {
51          this.wrapped = wrapped;
52      }
53  
54      @Override
55      public abstract List<SNIServerName> getRequestedServerNames();
56  
57      // Do not mark as override so we can compile on java8.
58      public List<byte[]> getStatusResponses() {
59          // Just return an empty list for now until we support it as otherwise we will fail in java9
60          // because of their sun.security.ssl.X509TrustManagerImpl class.
61          return Collections.emptyList();
62      }
63  
64      @Override
65      public void prepareHandshake() {
66          wrapped.prepareHandshake();
67      }
68  
69      @Override
70      public Map<String, Object> keyValueStorage() {
71          return wrapped.keyValueStorage();
72      }
73  
74      @Override
75      public OpenSslSessionId sessionId() {
76          return wrapped.sessionId();
77      }
78  
79      @Override
80      public void setSessionDetails(long creationTime, long lastAccessedTime, OpenSslSessionId id,
81                                    Map<String, Object> keyValueStorage) {
82          wrapped.setSessionDetails(creationTime, lastAccessedTime, id, keyValueStorage);
83      }
84  
85      @Override
86      public final void setLocalCertificate(Certificate[] localCertificate) {
87          wrapped.setLocalCertificate(localCertificate);
88      }
89  
90      @Override
91      public String[] getPeerSupportedSignatureAlgorithms() {
92          return EmptyArrays.EMPTY_STRINGS;
93      }
94  
95      @Override
96      public final void tryExpandApplicationBufferSize(int packetLengthDataOnly) {
97          wrapped.tryExpandApplicationBufferSize(packetLengthDataOnly);
98      }
99  
100     @Override
101     public final String[] getLocalSupportedSignatureAlgorithms() {
102         return LOCAL_SUPPORTED_SIGNATURE_ALGORITHMS.clone();
103     }
104 
105     @Override
106     public final byte[] getId() {
107         return wrapped.getId();
108     }
109 
110     @Override
111     public final OpenSslSessionContext getSessionContext() {
112         return wrapped.getSessionContext();
113     }
114 
115     @Override
116     public final long getCreationTime() {
117         return wrapped.getCreationTime();
118     }
119 
120     @Override
121     public final long getLastAccessedTime() {
122         return wrapped.getLastAccessedTime();
123     }
124 
125     @Override
126     public void setLastAccessedTime(long time) {
127         wrapped.setLastAccessedTime(time);
128     }
129 
130     @Override
131     public final void invalidate() {
132         wrapped.invalidate();
133     }
134 
135     @Override
136     public final boolean isValid() {
137         return wrapped.isValid();
138     }
139 
140     @Override
141     public final void putValue(String name, Object value) {
142         if (value instanceof SSLSessionBindingListener) {
143             // Decorate the value if needed so we submit the correct SSLSession instance
144             value = new SSLSessionBindingListenerDecorator((SSLSessionBindingListener) value);
145         }
146         wrapped.putValue(name, value);
147     }
148 
149     @Override
150     public final Object getValue(String s) {
151         Object value =  wrapped.getValue(s);
152         if (value instanceof SSLSessionBindingListenerDecorator) {
153             // Unwrap as needed so we return the original value
154             return ((SSLSessionBindingListenerDecorator) value).delegate;
155         }
156         return value;
157     }
158 
159     @Override
160     public final void removeValue(String s) {
161         wrapped.removeValue(s);
162     }
163 
164     @Override
165     public final String[] getValueNames() {
166         return wrapped.getValueNames();
167     }
168 
169     @Override
170     public final Certificate[] getPeerCertificates() throws SSLPeerUnverifiedException {
171         return wrapped.getPeerCertificates();
172     }
173 
174     @Override
175     public final Certificate[] getLocalCertificates() {
176         return wrapped.getLocalCertificates();
177     }
178 
179     @Override
180     public final X509Certificate[] getPeerCertificateChain() throws SSLPeerUnverifiedException {
181         return wrapped.getPeerCertificateChain();
182     }
183 
184     @Override
185     public final Principal getPeerPrincipal() throws SSLPeerUnverifiedException {
186         return wrapped.getPeerPrincipal();
187     }
188 
189     @Override
190     public final Principal getLocalPrincipal() {
191         return wrapped.getLocalPrincipal();
192     }
193 
194     @Override
195     public final String getCipherSuite() {
196         return wrapped.getCipherSuite();
197     }
198 
199     @Override
200     public String getProtocol() {
201         return wrapped.getProtocol();
202     }
203 
204     @Override
205     public final String getPeerHost() {
206         return wrapped.getPeerHost();
207     }
208 
209     @Override
210     public final int getPeerPort() {
211         return wrapped.getPeerPort();
212     }
213 
214     @Override
215     public final int getPacketBufferSize() {
216         return wrapped.getPacketBufferSize();
217     }
218 
219     @Override
220     public final int getApplicationBufferSize() {
221         return wrapped.getApplicationBufferSize();
222     }
223 
224     private final class SSLSessionBindingListenerDecorator implements SSLSessionBindingListener {
225 
226         final SSLSessionBindingListener delegate;
227 
228         SSLSessionBindingListenerDecorator(SSLSessionBindingListener delegate) {
229             this.delegate = delegate;
230         }
231 
232         @Override
233         public void valueBound(SSLSessionBindingEvent event) {
234             delegate.valueBound(new SSLSessionBindingEvent(ExtendedOpenSslSession.this, event.getName()));
235         }
236 
237         @Override
238         public void valueUnbound(SSLSessionBindingEvent event) {
239             delegate.valueUnbound(new SSLSessionBindingEvent(ExtendedOpenSslSession.this, event.getName()));
240         }
241     }
242 
243     @Override
244     public void handshakeFinished(byte[] id, String cipher, String protocol, byte[] peerCertificate,
245                                   byte[][] peerCertificateChain, long creationTime, long timeout) throws SSLException {
246         wrapped.handshakeFinished(id, cipher, protocol, peerCertificate, peerCertificateChain, creationTime, timeout);
247     }
248 
249     @Override
250     public boolean equals(Object o) {
251         return wrapped.equals(o);
252     }
253 
254     @Override
255     public int hashCode() {
256         return wrapped.hashCode();
257     }
258 
259     @Override
260     public String toString() {
261         return "ExtendedOpenSslSession{" +
262                 "wrapped=" + wrapped +
263                 '}';
264     }
265 }