1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
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
40
41
42
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
63
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
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
111
112 public abstract void setSessionCacheEnabled(boolean enabled);
113
114
115
116
117 public abstract boolean isSessionCacheEnabled();
118
119
120
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 }