1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
package org.mule.providers.ssl; |
12 | |
|
13 | |
import org.mule.impl.MuleMessage; |
14 | |
import org.mule.providers.AbstractMessageReceiver; |
15 | |
import org.mule.providers.tcp.TcpMessageReceiver; |
16 | |
import org.mule.umo.UMOComponent; |
17 | |
import org.mule.umo.endpoint.UMOEndpoint; |
18 | |
import org.mule.umo.lifecycle.InitialisationException; |
19 | |
import org.mule.umo.provider.UMOConnector; |
20 | |
|
21 | |
import java.io.IOException; |
22 | |
import java.net.Socket; |
23 | |
import java.security.cert.Certificate; |
24 | |
|
25 | |
import javax.net.ssl.HandshakeCompletedEvent; |
26 | |
import javax.net.ssl.HandshakeCompletedListener; |
27 | |
import javax.net.ssl.SSLPeerUnverifiedException; |
28 | |
import javax.net.ssl.SSLSocket; |
29 | |
import javax.resource.spi.work.Work; |
30 | |
|
31 | |
import edu.emory.mathcs.backport.java.util.concurrent.CountDownLatch; |
32 | |
import edu.emory.mathcs.backport.java.util.concurrent.TimeUnit; |
33 | |
|
34 | |
|
35 | 406 | public class SslMessageReceiver extends TcpMessageReceiver implements HandshakeCompletedListener |
36 | |
{ |
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | 16 | private CountDownLatch handshakeComplete = new CountDownLatch(1); |
43 | |
private static final long HANDSHAKE_WAIT = 30000L; |
44 | |
private Certificate[] peerCertificateChain; |
45 | |
private Certificate[] localCertificateChain; |
46 | |
|
47 | |
public SslMessageReceiver(UMOConnector connector, UMOComponent component, UMOEndpoint endpoint) |
48 | |
throws InitialisationException |
49 | |
{ |
50 | 16 | super(connector, component, endpoint); |
51 | 16 | } |
52 | |
|
53 | |
protected Work createWork(Socket socket) throws IOException |
54 | |
{ |
55 | 406 | if (endpoint.isStreaming()) |
56 | |
{ |
57 | 0 | return new SslStreamWorker(socket, this); |
58 | |
} |
59 | |
else |
60 | |
{ |
61 | 406 | return new SslWorker(socket, this); |
62 | |
} |
63 | |
} |
64 | |
|
65 | |
private void preRoute(MuleMessage message) throws Exception |
66 | |
{ |
67 | 406 | handshakeComplete.await(HANDSHAKE_WAIT, TimeUnit.MILLISECONDS); |
68 | 406 | if (0 != handshakeComplete.getCount()) |
69 | |
{ |
70 | 0 | throw new IllegalStateException("Handshake did not complete"); |
71 | |
} |
72 | 406 | if(peerCertificateChain != null) |
73 | |
{ |
74 | 0 | message.setProperty(SslConnector.PEER_CERTIFICATES, peerCertificateChain); |
75 | |
} |
76 | 406 | if(localCertificateChain != null) |
77 | |
{ |
78 | 406 | message.setProperty(SslConnector.LOCAL_CERTIFICATES, localCertificateChain); |
79 | |
} |
80 | 406 | } |
81 | |
|
82 | |
public void handshakeCompleted(HandshakeCompletedEvent event) |
83 | |
{ |
84 | |
try |
85 | |
{ |
86 | 406 | localCertificateChain = event.getLocalCertificates(); |
87 | |
try |
88 | |
{ |
89 | 406 | peerCertificateChain = event.getPeerCertificates(); |
90 | |
} |
91 | 406 | catch (SSLPeerUnverifiedException e) |
92 | |
{ |
93 | 406 | logger.debug("Cannot get peer certificate chain: "+ e.getMessage()); |
94 | 0 | } |
95 | |
} |
96 | |
finally |
97 | |
{ |
98 | 406 | handshakeComplete.countDown(); |
99 | 406 | } |
100 | 406 | } |
101 | |
|
102 | |
protected class SslWorker extends TcpWorker |
103 | |
{ |
104 | |
public SslWorker(Socket socket, AbstractMessageReceiver receiver) throws IOException |
105 | 406 | { |
106 | 406 | super(socket, receiver); |
107 | 406 | ((SSLSocket) socket).addHandshakeCompletedListener(SslMessageReceiver.this); |
108 | 406 | } |
109 | |
|
110 | |
protected void preRouteMuleMessage(MuleMessage message) throws Exception |
111 | |
{ |
112 | 406 | super.preRouteMuleMessage(message); |
113 | |
|
114 | 406 | preRoute(message); |
115 | 406 | } |
116 | |
} |
117 | |
|
118 | |
protected class SslStreamWorker extends TcpStreamWorker |
119 | |
{ |
120 | |
public SslStreamWorker(Socket socket, AbstractMessageReceiver receiver) throws IOException |
121 | 0 | { |
122 | 0 | super(socket, receiver); |
123 | 0 | ((SSLSocket) socket).addHandshakeCompletedListener(SslMessageReceiver.this); |
124 | 0 | } |
125 | |
|
126 | |
protected void preRouteMuleMessage(MuleMessage message) throws Exception |
127 | |
{ |
128 | 0 | super.preRouteMuleMessage(message); |
129 | |
|
130 | 0 | preRoute(message); |
131 | 0 | } |
132 | |
} |
133 | |
|
134 | |
} |