~ubuntu-branches/ubuntu/oneiric/libjboss-remoting-java/oneiric

« back to all changes in this revision

Viewing changes to src/org/jboss/remoting/transport/coyote/ssl/RemotingSSLSupport.java

  • Committer: Package Import Robot
  • Author(s): Torsten Werner
  • Date: 2011-09-09 14:01:03 UTC
  • mfrom: (1.1.6 upstream)
  • Revision ID: package-import@ubuntu.com-20110909140103-hqokx61534tas9rg
Tags: 2.5.3.SP1-1
* Newer but not newest upstream release. Do not build samples.
* Change debian/watch to upstream's svn repo.
* Add patch to fix compile error caused by tomcat update.
  (Closes: #628303)
* Switch to source format 3.0.
* Switch to debhelper level 7.
* Remove useless Depends.
* Update Standards-Version: 3.9.2.
* Update README.source.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/**
2
 
 * This class uses code taken directly from the org.apache.tomcat.util.net.SSLSupport class of the
3
 
 * Apache tomcat-connectors project.  Please refer to the NOTICE file included in this distribution for
4
 
 * more details.  The following is the copyright, patent, trademark, and attribution notices from the
5
 
 * SSLSupport source, which this class also maintains:
6
 
 *
7
 
 *  Copyright 1999-2004 The Apache Software Foundation
8
 
 *
9
 
 *  Licensed under the Apache License, Version 2.0 (the "License");
10
 
 *  you may not use this file except in compliance with the License.
11
 
 *  You may obtain a copy of the License at
12
 
 *
13
 
 *      http://www.apache.org/licenses/LICENSE-2.0
14
 
 *
15
 
 *  Unless required by applicable law or agreed to in writing, software
16
 
 *  distributed under the License is distributed on an "AS IS" BASIS,
17
 
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18
 
 *  See the License for the specific language governing permissions and
19
 
 *  limitations under the License.
20
 
 *
21
 
 *    @author EKR
22
 
 *    @author Craig R. McClanahan
23
 
 *    Parts cribbed from JSSECertCompat
24
 
 *    Parts cribbed from CertificatesValve
25
 
 *
26
 
 * (the full source of the org.apache.tomcat.util.net.SSLSupport can be found at
27
 
 * http://svn.apache.org/repos/asf/tomcat/connectors/trunk/util/java/org/apache/tomcat/util/net/jsse/JSSESupport.java).
28
 
 */
29
 
package org.jboss.remoting.transport.coyote.ssl;
30
 
 
31
 
import java.io.ByteArrayInputStream;
32
 
import java.io.IOException;
33
 
import java.security.cert.CertificateFactory;
34
 
import javax.net.ssl.SSLSession;
35
 
import javax.net.ssl.SSLSocket;
36
 
import javax.security.cert.X509Certificate;
37
 
import org.apache.tomcat.util.net.SSLSupport;
38
 
 
39
 
/**
40
 
 * @author <a href="mailto:tom.elrod@jboss.com">Tom Elrod</a>
41
 
 */
42
 
public class RemotingSSLSupport implements SSLSupport
43
 
{
44
 
   private SSLSocket sslSocket;
45
 
   private SSLSession session;
46
 
 
47
 
   public RemotingSSLSupport(SSLSocket socket)
48
 
   {
49
 
      this.sslSocket = socket;
50
 
      this.session = socket.getSession();
51
 
   }
52
 
   
53
 
   public RemotingSSLSupport(SSLSession session)
54
 
   {
55
 
      this.session = session;
56
 
   }
57
 
 
58
 
   /**
59
 
    * The cipher suite being used on this connection.
60
 
    */
61
 
   public String getCipherSuite() throws IOException
62
 
   {
63
 
      if(session == null)
64
 
      {
65
 
         return null;
66
 
      }
67
 
      return session.getCipherSuite();
68
 
   }
69
 
 
70
 
   /**
71
 
    * The client certificate chain (if any).
72
 
    */
73
 
   public Object[] getPeerCertificateChain() throws IOException
74
 
   {
75
 
      return getPeerCertificateChain(false);
76
 
   }
77
 
 
78
 
   public Object[] getPeerCertificateChain(boolean force)
79
 
         throws IOException
80
 
   {
81
 
      if(session == null)
82
 
      {
83
 
         return null;
84
 
      }
85
 
 
86
 
      // Convert JSSE's certificate format to the ones we need
87
 
      X509Certificate [] jsseCerts = null;
88
 
      try
89
 
      {
90
 
         jsseCerts = session.getPeerCertificateChain();
91
 
      }
92
 
      catch(Exception bex)
93
 
      {
94
 
         // ignore.
95
 
      }
96
 
      if(jsseCerts == null)
97
 
      {
98
 
         jsseCerts = new X509Certificate[0];
99
 
      }
100
 
      if(jsseCerts.length <= 0 && force)
101
 
      {
102
 
         session.invalidate();
103
 
         handShake();
104
 
         session = sslSocket.getSession();
105
 
      }
106
 
      return getX509Certificates(session);
107
 
 
108
 
   }
109
 
 
110
 
   protected void handShake() throws IOException
111
 
   {
112
 
      if (sslSocket != null)
113
 
      {
114
 
         sslSocket.setNeedClientAuth(true);
115
 
         sslSocket.startHandshake();
116
 
      }
117
 
   }
118
 
 
119
 
   protected java.security.cert.X509Certificate[] getX509Certificates(SSLSession session) throws IOException
120
 
   {
121
 
      X509Certificate jsseCerts[] = null;
122
 
      try
123
 
      {
124
 
         jsseCerts = session.getPeerCertificateChain();
125
 
      }
126
 
      catch(Throwable ex)
127
 
      {
128
 
         // Get rid of the warning in the logs when no Client-Cert is
129
 
         // available
130
 
      }
131
 
 
132
 
      if(jsseCerts == null)
133
 
      {
134
 
         jsseCerts = new X509Certificate[0];
135
 
      }
136
 
      java.security.cert.X509Certificate [] x509Certs = new java.security.cert.X509Certificate[jsseCerts.length];
137
 
      for(int i = 0; i < x509Certs.length; i++)
138
 
      {
139
 
         try
140
 
         {
141
 
            byte buffer[] = jsseCerts[i].getEncoded();
142
 
            CertificateFactory cf = CertificateFactory.getInstance("X.509");
143
 
            ByteArrayInputStream stream = new ByteArrayInputStream(buffer);
144
 
            x509Certs[i] = (java.security.cert.X509Certificate) cf.generateCertificate(stream);
145
 
         }
146
 
         catch(Exception ex)
147
 
         {
148
 
            return null;
149
 
         }
150
 
      }
151
 
 
152
 
      if(x509Certs.length < 1)
153
 
      {
154
 
         return null;
155
 
      }
156
 
      return x509Certs;
157
 
   }
158
 
 
159
 
   /**
160
 
    * Get the keysize.
161
 
    * <p/>
162
 
    * What we're supposed to put here is ill-defined by the
163
 
    * Servlet spec (S 4.7 again). There are at least 4 potential
164
 
    * values that might go here:
165
 
    * <p/>
166
 
    * (a) The size of the encryption key
167
 
    * (b) The size of the MAC key
168
 
    * (c) The size of the key-exchange key
169
 
    * (d) The size of the signature key used by the server
170
 
    * <p/>
171
 
    * Unfortunately, all of these values are nonsensical.
172
 
    */
173
 
   public Integer getKeySize() throws IOException
174
 
   {
175
 
      SSLSupport.CipherData c_aux[] = ciphers;
176
 
      if(session == null)
177
 
      {
178
 
         return null;
179
 
      }
180
 
      Integer keySize = (Integer) session.getValue(KEY_SIZE_KEY);
181
 
      if(keySize == null)
182
 
      {
183
 
         int size = 0;
184
 
         String cipherSuite = session.getCipherSuite();
185
 
         for(int i = 0; i < c_aux.length; i++)
186
 
         {
187
 
            if(cipherSuite.indexOf(c_aux[i].phrase) >= 0)
188
 
            {
189
 
               size = c_aux[i].keySize;
190
 
               break;
191
 
            }
192
 
         }
193
 
         keySize = new Integer(size);
194
 
         session.putValue(KEY_SIZE_KEY, keySize);
195
 
      }
196
 
      return keySize;
197
 
   }
198
 
 
199
 
   /**
200
 
    * The current session Id.
201
 
    */
202
 
   public String getSessionId() throws IOException
203
 
   {
204
 
      if(session == null)
205
 
      {
206
 
         return null;
207
 
      }
208
 
      // Expose ssl_session (getId)
209
 
      byte [] ssl_session = session.getId();
210
 
      if(ssl_session == null)
211
 
      {
212
 
         return null;
213
 
      }
214
 
      StringBuffer buf = new StringBuffer("");
215
 
      for(int x = 0; x < ssl_session.length; x++)
216
 
      {
217
 
         String digit = Integer.toHexString((int) ssl_session[x]);
218
 
         if(digit.length() < 2)
219
 
         {
220
 
            buf.append('0');
221
 
         }
222
 
         if(digit.length() > 2)
223
 
         {
224
 
            digit = digit.substring(digit.length() - 2);
225
 
         }
226
 
         buf.append(digit);
227
 
      }
228
 
      return buf.toString();
229
 
   }
230
 
}
 
 
b'\\ No newline at end of file'