001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package org.apache.catalina.valves;
019:
020: import java.io.IOException;
021: import java.io.ByteArrayInputStream;
022:
023: import java.security.cert.CertificateFactory;
024: import java.security.cert.X509Certificate;
025:
026: import javax.servlet.ServletException;
027:
028: import org.apache.catalina.valves.ValveBase;
029: import org.apache.catalina.connector.Request;
030: import org.apache.catalina.connector.Response;
031: import org.apache.catalina.util.StringManager;
032:
033: /*
034: * Valve to fill the SSL informations in the request
035: * mod_header is used to fill the headers and the valve
036: * will fill the parameters of the request.
037: * In httpd.conf add the following:
038: * <IfModule ssl_module>
039: * RequestHeader set SSL_CLIENT_CERT "%{SSL_CLIENT_CERT}s"
040: * RequestHeader set SSL_CIPHER "%{SSL_CIPHER}s"
041: * RequestHeader set SSL_SESSION_ID "%{SSL_SESSION_ID}s"
042: * RequestHeader set SSL_CIPHER_USEKEYSIZE "%{SSL_CIPHER_USEKEYSIZE}s"
043: * </IfModule>
044: *
045: * @author Jean-Frederic Clere
046: * @version $Revision: 420067 $, $Date: 2006-07-08 09:16:58 +0200 (sub, 08 srp 2006) $
047: */
048:
049: public class SSLValve extends ValveBase {
050: /*
051: private static final String info =
052: "SSLValve/1.0";
053: protected static StringManager sm =
054: StringManager.getManager(Constants.Package);
055: public String getInfo() {
056: return (info);
057: }
058: public String toString() {
059: StringBuffer sb = new StringBuffer("SSLValve[");
060: if (container != null)
061: sb.append(container.getName());
062: sb.append("]");
063: return (sb.toString());
064: }
065: */
066: public String mygetHeader(Request request, String header) {
067: String strcert0 = request.getHeader(header);
068: if (strcert0 == null)
069: return null;
070: /* mod_header writes "(null)" when the ssl variable is no filled */
071: if ("(null)".equals(strcert0))
072: return null;
073: return strcert0;
074: }
075:
076: public void invoke(Request request, Response response)
077: throws IOException, ServletException {
078:
079: /* mod_header converts the '\n' into ' ' so we have to rebuild the client certificate */
080: String strcert0 = mygetHeader(request, "ssl_client_cert");
081: if (strcert0 != null && strcert0.length() > 28) {
082: String strcert1 = strcert0.replace(' ', '\n');
083: String strcert2 = strcert1.substring(28,
084: strcert1.length() - 26);
085: String strcert3 = new String(
086: "-----BEGIN CERTIFICATE-----\n");
087: String strcert4 = strcert3.concat(strcert2);
088: String strcerts = strcert4
089: .concat("\n-----END CERTIFICATE-----\n");
090: // ByteArrayInputStream bais = new ByteArrayInputStream(strcerts.getBytes("UTF-8"));
091: ByteArrayInputStream bais = new ByteArrayInputStream(
092: strcerts.getBytes());
093: X509Certificate jsseCerts[] = null;
094: try {
095: CertificateFactory cf = CertificateFactory
096: .getInstance("X.509");
097: X509Certificate cert = (X509Certificate) cf
098: .generateCertificate(bais);
099: jsseCerts = new X509Certificate[1];
100: jsseCerts[0] = cert;
101: } catch (java.security.cert.CertificateException e) {
102: System.out.println("SSLValve failed " + strcerts);
103: System.out.println("SSLValve failed " + e);
104: }
105: request.setAttribute(
106: "javax.servlet.request.X509Certificate", jsseCerts);
107: }
108: strcert0 = mygetHeader(request, "ssl_cipher");
109: if (strcert0 != null) {
110: request.setAttribute("javax.servlet.request.cipher_suite",
111: strcert0);
112: }
113: strcert0 = mygetHeader(request, "ssl_session_id");
114: if (strcert0 != null) {
115: request.setAttribute("javax.servlet.request.ssl_session",
116: strcert0);
117: }
118: strcert0 = mygetHeader(request, "ssl_cipher_usekeysize");
119: if (strcert0 != null) {
120: request.setAttribute("javax.servlet.request.key_size",
121: strcert0);
122: }
123: getNext().invoke(request, response);
124: }
125: }
|