File: ALPN.patch

package info (click to toggle)
undertow 2.3.20-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 13,008 kB
  • sloc: java: 155,572; xml: 3,179; makefile: 3; sh: 1
file content (270 lines) | stat: -rw-r--r-- 8,689 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
From: Markus Koschany <apo@debian.org>
Date: Sun, 1 Nov 2015 17:54:39 +0100
Subject: ALPN

Provide the ALPN API with the undertow source package.

Forwarded: not-needed
---
 .../src/main/java/org/eclipse/jetty/alpn/ALPN.java | 252 +++++++++++++++++++++
 1 file changed, 252 insertions(+)
 create mode 100644 core/src/main/java/org/eclipse/jetty/alpn/ALPN.java

diff --git a/core/src/main/java/org/eclipse/jetty/alpn/ALPN.java b/core/src/main/java/org/eclipse/jetty/alpn/ALPN.java
new file mode 100644
index 0000000..6a70617
--- /dev/null
+++ b/core/src/main/java/org/eclipse/jetty/alpn/ALPN.java
@@ -0,0 +1,252 @@
+//
+//  ========================================================================
+//  Copyright (c) 1995-2014 Mort Bay Consulting Pty. Ltd.
+//  ------------------------------------------------------------------------
+//  All rights reserved. This program and the accompanying materials
+//  are made available under the terms of the Eclipse Public License v1.0
+//  and Apache License v2.0 which accompanies this distribution.
+//
+//      The Eclipse Public License is available at
+//      http://www.eclipse.org/legal/epl-v10.html
+//
+//      The Apache License v2.0 is available at
+//      http://www.opensource.org/licenses/apache2.0.php
+//
+//  You may elect to redistribute this code under either of these licenses.
+//  ========================================================================
+//
+
+package org.eclipse.jetty.alpn;
+
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import javax.net.ssl.SSLEngine;
+import javax.net.ssl.SSLSocket;
+
+/**
+ * {@link ALPN} provides an API to applications that want to make use of the
+ * <a href="http://tools.ietf.org/html/draft-ietf-tls-applayerprotoneg">Application Layer Protocol Negotiation</a>.
+ * <p/>
+ * The ALPN extension is only available when using the TLS protocol, therefore applications must
+ * ensure that the TLS protocol is used:
+ * <pre>
+ * SSLContext context = SSLContext.getInstance("TLSv1");
+ * </pre>
+ * Refer to the
+ * <a href="http://docs.oracle.com/javase/7/docs/technotes/guides/security/StandardNames.html#SSLContext">list
+ * of standard SSLContext protocol names</a> for further information on TLS protocol versions supported.
+ * <p/>
+ * Applications must register instances of either {@link SSLSocket} or {@link SSLEngine} with a
+ * {@link ClientProvider} or with a {@link ServerProvider}, depending whether they are on client or
+ * server side.
+ * <p/>
+ * The ALPN implementation will invoke the provider callbacks to allow applications to interact
+ * with the negotiation of the protocol.
+ * <p/>
+ * Client side typical usage:
+ * <pre>
+ * SSLSocket sslSocket = ...;
+ * ALPN.put(sslSocket, new ALPN.ClientProvider()
+ * {
+ *     &#64;Override
+ *     public boolean supports()
+ *     {
+ *         return true;
+ *     }
+ *
+ *     &#64;Override
+ *     public List&lt;String&gt; protocols()
+ *     {
+ *         return Arrays.asList("spdy/3", "http/1.1");
+ *     }
+ *
+ *     &#64;Override
+ *     public void unsupported()
+ *     {
+ *     }
+ *
+ *     &#64;Override
+ *     public void selected(String protocol)
+ *     {
+ *         System.out.println("Selected protocol: " + protocol);
+ *     }
+ *  });
+ * </pre>
+ * Server side typical usage:
+ * <pre>
+ * SSLSocket sslSocket = ...;
+ * ALPN.put(sslSocket, new ALPN.ServerProvider()
+ * {
+ *     &#64;Override
+ *     public void unsupported()
+ *     {
+ *     }
+ *
+ *     &#64;Override
+ *     public String select(List&lt;String&gt; protocols)
+ *     {
+ *         return protocols.get(0);
+ *     }
+ *  });
+ * </pre>
+ * Applications must ensure to deregister {@link SSLSocket} or {@link SSLEngine} instances,
+ * because they are kept in a global map.
+ * Deregistration should typically happen when the application detects the end of the protocol
+ * negotiation, and/or when the associated socket connection is closed.
+ * <p/>
+ * In order to help application development, you can set the {@link ALPN#debug} field
+ * to {@code true} to have debug code printed to {@link System#err}.
+ */
+public class ALPN
+{
+    /**
+     * Flag that enables printing of debug statements to {@link System#err}.
+     */
+    public static boolean debug = false;
+
+    private static Map<Object, Provider> objects = Collections.synchronizedMap(new HashMap<Object, Provider>());
+
+    private ALPN()
+    {
+    }
+
+    /**
+     * Registers a SSLSocket with a provider.
+     *
+     * @param socket   the socket to register with the provider
+     * @param provider the provider to register with the socket
+     * @see #remove(SSLSocket)
+     */
+    public static void put(SSLSocket socket, Provider provider)
+    {
+        objects.put(socket, provider);
+    }
+
+    /**
+     * @param socket a socket registered with {@link #put(SSLSocket, Provider)}
+     * @return the provider registered with the given socket
+     */
+    public static Provider get(SSLSocket socket)
+    {
+        return objects.get(socket);
+    }
+
+    /**
+     * Unregisters the given SSLSocket.
+     *
+     * @param socket the socket to unregister
+     * @return the provider registered with the socket
+     * @see #put(SSLSocket, Provider)
+     */
+    public static Provider remove(SSLSocket socket)
+    {
+        return objects.remove(socket);
+    }
+
+    /**
+     * Registers a SSLEngine with a provider.
+     *
+     * @param engine   the engine to register with the provider
+     * @param provider the provider to register with the engine
+     * @see #remove(SSLEngine)
+     */
+    public static void put(SSLEngine engine, Provider provider)
+    {
+        objects.put(engine, provider);
+    }
+
+    /**
+     * @param engine an engine registered with {@link #put(SSLEngine, Provider)}
+     * @return the provider registered with the given engine
+     */
+    public static Provider get(SSLEngine engine)
+    {
+        return objects.get(engine);
+    }
+
+    /**
+     * Unregisters the given SSLEngine.
+     *
+     * @param engine the engine to unregister
+     * @return the provider registered with the engine
+     * @see #put(SSLEngine, Provider)
+     */
+    public static Provider remove(SSLEngine engine)
+    {
+        return objects.remove(engine);
+    }
+
+    /**
+     * Base, empty, interface for providers.
+     */
+    public interface Provider
+    {
+    }
+
+    /**
+     * The client-side provider interface that applications must
+     * implement to interact with the negotiation of the protocol.
+     */
+    public interface ClientProvider extends Provider
+    {
+        /**
+         * Callback invoked to let the implementation know whether an
+         * ALPN extension should be added to a ClientHello TLS message.
+         *
+         * @return true to add the ALPN extension, false otherwise
+         */
+        public boolean supports();
+
+        /**
+         * Callback invoked to let the implementation know the list
+         * of protocols that should be added to the ALPN extension in
+         * a ClientHello TLS message.
+         * <p/>
+         * This callback is invoked only if the {@link #supports()}
+         * returned true.
+         *
+         * @return the list of protocols supported by the client;
+         * if null or empty, the ALPN extension is not sent
+         */
+        public List<String> protocols();
+
+        /**
+         * Callback invoked to let the client application know that
+         * the server does not support ALPN.
+         */
+        public void unsupported();
+
+        /**
+         * Callback invoked to let the client application know
+         * the protocol chosen by the server.
+         *
+         * @param protocol the protocol selected by the server
+         */
+        public void selected(String protocol);
+    }
+
+    /**
+     * The server-side provider interface that applications must
+     * implement to interact with the negotiation of the protocol.
+     */
+    public interface ServerProvider extends Provider
+    {
+        /**
+         * Callback invoked to let the server application know that
+         * the client does not support ALPN.
+         */
+        public void unsupported();
+
+        /**
+         * Callback invoked to let the server application select
+         * a protocol among the ones sent by the client.
+         *
+         * @param protocols the protocols sent by the client
+         * @return the protocol selected by the server application;
+         * must not be null
+         */
+        public String select(List<String> protocols);
+    }
+}