View Javadoc

1   /*
2    * Copyright 2014 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    *   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, 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.ObjectUtil;
19  import io.netty.internal.tcnative.SSL;
20  import io.netty.internal.tcnative.SSLContext;
21  import io.netty.internal.tcnative.SessionTicketKey;
22  
23  import javax.net.ssl.SSLSession;
24  import javax.net.ssl.SSLSessionContext;
25  import java.util.Arrays;
26  import java.util.Enumeration;
27  import java.util.NoSuchElementException;
28  import java.util.concurrent.locks.Lock;
29  
30  /**
31   * OpenSSL specific {@link SSLSessionContext} implementation.
32   */
33  public abstract class OpenSslSessionContext implements SSLSessionContext {
34      private static final Enumeration<byte[]> EMPTY = new EmptyEnumeration();
35  
36      private final OpenSslSessionStats stats;
37      final ReferenceCountedOpenSslContext context;
38  
39      // IMPORTANT: We take the OpenSslContext and not just the long (which points the native instance) to prevent
40      //            the GC to collect OpenSslContext as this would also free the pointer and so could result in a
41      //            segfault when the user calls any of the methods here that try to pass the pointer down to the native
42      //            level.
43      OpenSslSessionContext(ReferenceCountedOpenSslContext context) {
44          this.context = context;
45          stats = new OpenSslSessionStats(context);
46      }
47  
48      @Override
49      public SSLSession getSession(byte[] bytes) {
50          if (bytes == null) {
51              throw new NullPointerException("bytes");
52          }
53          return null;
54      }
55  
56      @Override
57      public Enumeration<byte[]> getIds() {
58          return EMPTY;
59      }
60  
61      /**
62       * Sets the SSL session ticket keys of this context.
63       * @deprecated use {@link #setTicketKeys(OpenSslSessionTicketKey...)}.
64       */
65      @Deprecated
66      public void setTicketKeys(byte[] keys) {
67          if (keys.length % SessionTicketKey.TICKET_KEY_SIZE != 0) {
68              throw new IllegalArgumentException("keys.length % " + SessionTicketKey.TICKET_KEY_SIZE  + " != 0");
69          }
70          SessionTicketKey[] tickets = new SessionTicketKey[keys.length / SessionTicketKey.TICKET_KEY_SIZE];
71          for (int i = 0, a = 0; i < tickets.length; i++) {
72              byte[] name = Arrays.copyOfRange(keys, a, SessionTicketKey.NAME_SIZE);
73              a += SessionTicketKey.NAME_SIZE;
74              byte[] hmacKey = Arrays.copyOfRange(keys, a, SessionTicketKey.HMAC_KEY_SIZE);
75              i += SessionTicketKey.HMAC_KEY_SIZE;
76              byte[] aesKey = Arrays.copyOfRange(keys, a, SessionTicketKey.AES_KEY_SIZE);
77              a += SessionTicketKey.AES_KEY_SIZE;
78              tickets[i] = new SessionTicketKey(name, hmacKey, aesKey);
79          }
80          Lock writerLock = context.ctxLock.writeLock();
81          writerLock.lock();
82          try {
83              SSLContext.clearOptions(context.ctx, SSL.SSL_OP_NO_TICKET);
84              SSLContext.setSessionTicketKeys(context.ctx, tickets);
85          } finally {
86              writerLock.unlock();
87          }
88      }
89  
90      /**
91       * Sets the SSL session ticket keys of this context.
92       */
93      public void setTicketKeys(OpenSslSessionTicketKey... keys) {
94          ObjectUtil.checkNotNull(keys, "keys");
95          SessionTicketKey[] ticketKeys = new SessionTicketKey[keys.length];
96          for (int i = 0; i < ticketKeys.length; i++) {
97              ticketKeys[i] = keys[i].key;
98          }
99          Lock writerLock = context.ctxLock.writeLock();
100         writerLock.lock();
101         try {
102             SSLContext.clearOptions(context.ctx, SSL.SSL_OP_NO_TICKET);
103             SSLContext.setSessionTicketKeys(context.ctx, ticketKeys);
104         } finally {
105             writerLock.unlock();
106         }
107     }
108 
109     /**
110      * Enable or disable caching of SSL sessions.
111      */
112     public abstract void setSessionCacheEnabled(boolean enabled);
113 
114     /**
115      * Return {@code true} if caching of SSL sessions is enabled, {@code false} otherwise.
116      */
117     public abstract boolean isSessionCacheEnabled();
118 
119     /**
120      * Returns the stats of this context.
121      */
122     public OpenSslSessionStats stats() {
123         return stats;
124     }
125 
126     private static final class EmptyEnumeration implements Enumeration<byte[]> {
127         @Override
128         public boolean hasMoreElements() {
129             return false;
130         }
131 
132         @Override
133         public byte[] nextElement() {
134             throw new NoSuchElementException();
135         }
136     }
137 }