summaryrefslogtreecommitdiffstats
path: root/pki/base/common/src/com/netscape/cms/publish/publishers/OCSPPublisher.java
blob: 551bb4d6f370f6cfd1eb4a31c4a9e6585a4daab6 (plain)
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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
// --- BEGIN COPYRIGHT BLOCK ---
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; version 2 of the License.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License along
// with this program; if not, write to the Free Software Foundation, Inc.,
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
//
// (C) 2007 Red Hat, Inc.
// All rights reserved.
// --- END COPYRIGHT BLOCK ---
package com.netscape.cms.publish.publishers;

import java.io.DataInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.Socket;
import java.net.URLEncoder;
import java.security.cert.CRLException;
import java.security.cert.X509CRL;
import java.util.Locale;
import java.util.StringTokenizer;
import java.util.Vector;

import netscape.ldap.LDAPConnection;

import com.netscape.certsrv.apps.CMS;
import com.netscape.certsrv.base.EBaseException;
import com.netscape.certsrv.base.IConfigStore;
import com.netscape.certsrv.base.IExtendedPluginInfo;
import com.netscape.certsrv.ldap.ELdapException;
import com.netscape.certsrv.logging.ILogger;
import com.netscape.certsrv.publish.ILdapPublisher;
import com.netscape.cmsutil.http.HttpRequest;
import com.netscape.cmsutil.http.JssSSLSocketFactory;

/**
 * This publisher writes certificate and CRL into
 * a directory.
 * 
 * @version $Revision$, $Date$
 */
public class OCSPPublisher implements ILdapPublisher, IExtendedPluginInfo {
    private static final String PROP_HOST = "host";
    private static final String PROP_PORT = "port";
    private static final String PROP_PATH = "path";
    private static final String PROP_NICK = "nickName";
    private static final String PROP_CLIENT_AUTH_ENABLE = "enableClientAuth";

    private IConfigStore mConfig = null;
    private String mHost = null;
    private String mPort = null;
    private String mPath = null;
    private String mNickname = null;
    private boolean mClientAuthEnabled = true;
    private ILogger mLogger = CMS.getLogger();

    /**
     * Returns the implementation name.
     */
    public String getImplName() {
        return "OCSPPublisher";
    }

    /**
     * Returns the description of the ldap publisher.
     */
    public String getDescription() {
        return "This publisher writes the CRL to CMS's OCSP server.";
    }

    public String[] getExtendedPluginInfo(Locale locale) {
        String[] params = {
                PROP_HOST + ";string;Host of CMS's OCSP Secure agent service",
                PROP_PORT + ";string;Port of CMS's OCSP Secure agent service",
                PROP_PATH + ";string;URI of CMS's OCSP Secure agent service",
                PROP_NICK + ";string;Nickname of cert used for client authentication",
                PROP_CLIENT_AUTH_ENABLE + ";boolean;Client Authentication enabled",
                IExtendedPluginInfo.HELP_TOKEN +
                        ";configuration-ldappublish-publisher-ocsppublisher",
                IExtendedPluginInfo.HELP_TEXT +
                        ";Publishes CRLs to a Online Certificate Status Manager, an OCSP responder provided by CMS."
            };

        return params;
    }

    /**
     * Returns the current instance parameters.
     */
    public Vector getInstanceParams() {
        Vector v = new Vector();
        String host = "";
        String port = "";
        String path = "";
        String nickname = "";
        String clientAuthEnabled = "";

        try {
            host = mConfig.getString(PROP_HOST);
        } catch (EBaseException e) {
        }
        v.addElement(PROP_HOST + "=" + host);
        try {
            port = mConfig.getString(PROP_PORT);
        } catch (EBaseException e) {
        }
        v.addElement(PROP_PORT + "=" + port);
        try {
            path = mConfig.getString(PROP_PATH);
        } catch (EBaseException e) {
        }
        v.addElement(PROP_PATH + "=" + path);
        try {
            nickname = mConfig.getString(PROP_NICK);
        } catch (EBaseException e) {
        }
        v.addElement(PROP_NICK + "=" + nickname);
        try {
            clientAuthEnabled = mConfig.getString(PROP_CLIENT_AUTH_ENABLE);
        } catch (EBaseException e) {
        }
        v.addElement(PROP_CLIENT_AUTH_ENABLE + "=" + clientAuthEnabled);
        return v;
    }

    /**
     * Returns the initial default parameters.
     */
    public Vector getDefaultParams() {
        Vector v = new Vector();

        IConfigStore config = CMS.getConfigStore();
        String nickname = "";
        // get subsystem cert nickname as default for client auth
        try {
            nickname = config.getString("ca.subsystem.nickname", "");
            String tokenname = config.getString("ca.subsystem.tokenname", "");
            if (!tokenname.equals("internal") && !tokenname.equals("Internal Key Storage Token"))
                nickname = tokenname + ":" + nickname;
        } catch (Exception e) {
        }

        v.addElement(PROP_HOST + "=");
        v.addElement(PROP_PORT + "=");
        v.addElement(PROP_PATH + "=/ocsp/agent/ocsp/addCRL");
        v.addElement(PROP_CLIENT_AUTH_ENABLE + "=true");
        v.addElement(PROP_NICK + "=" + nickname);
        return v;
    }

    /**
     * Initializes this plugin.
     */
    public void init(IConfigStore config) {
        mConfig = config;
        try {
            mHost = mConfig.getString(PROP_HOST, "");
            mPort = mConfig.getString(PROP_PORT, "");
            mPath = mConfig.getString(PROP_PATH, "");
            mNickname = mConfig.getString(PROP_NICK, "");
            mClientAuthEnabled = mConfig.getBoolean(PROP_CLIENT_AUTH_ENABLE, true);
        } catch (EBaseException e) {
        }
    }

    public IConfigStore getConfigStore() {
        return mConfig;
    }

    protected Socket Connect(String host, boolean secure, JssSSLSocketFactory factory) {
        Socket socket = null;
        StringTokenizer st = new StringTokenizer(host, " ");
        while (st.hasMoreTokens()) {
            String hp = st.nextToken(); // host:port
            StringTokenizer st1 = new StringTokenizer(hp, ":");
            String h = st1.nextToken();
            int p = Integer.parseInt(st1.nextToken());
            try {
                if (secure) {
                    socket = factory.makeSocket(h, p);
                } else {
                    socket = new Socket(h, p);
                }
                return socket;
            } catch (Exception e) {
            }
            try {
                Thread.sleep(5000); // 5 seconds delay
            } catch (Exception e) {
            }
        }
        return null;
    }

    /**
     * Publishs a object to the ldap directory.
     * 
     * @param conn a Ldap connection
     *            (null if LDAP publishing is not enabled)
     * @param dn dn of the ldap entry to publish cert
     *            (null if LDAP publishing is not enabled)
     * @param object object to publish
     *            (java.security.cert.X509Certificate or,
     *            java.security.cert.X509CRL)
     */
    public synchronized void publish(LDAPConnection conn, String dn, Object object)
            throws ELdapException {
        try {
            if (!(object instanceof X509CRL))
                return;
            X509CRL crl = (X509CRL) object;

            // talk to agent port of CMS

            // open the connection and prepare it to POST
            boolean secure = true;

            String host = mHost;
            int port = Integer.parseInt(mPort);
            String path = mPath;

            mLogger.log(ILogger.EV_SYSTEM, ILogger.S_OTHER,
                    ILogger.LL_INFO, "OCSPPublisher: " +
                            "Host='" + host + "' Port='" + port +
                            "' URL='" + path + "'");
            CMS.debug("OCSPPublisher: " +
                    "Host='" + host + "' Port='" + port +
                    "' URL='" + path + "'");

            StringBuffer query = new StringBuffer();
            query.append("crl=");
            query.append(URLEncoder.encode("-----BEGIN CERTIFICATE REVOCATION LIST-----\n"));
            query.append(URLEncoder.encode(CMS.BtoA(crl.getEncoded())));
            query.append(URLEncoder.encode("\n-----END CERTIFICATE REVOCATION LIST-----"));
            query.append("&noui=true");

            Socket socket = null;
            JssSSLSocketFactory factory;

            if (mClientAuthEnabled) {
                factory = new JssSSLSocketFactory(mNickname);
            } else {
                factory = new JssSSLSocketFactory();
            }

            if (mHost != null && mHost.indexOf(' ') != -1) {
                // support failover hosts configuration
                // host parameter can be
                // "directory.knowledge.com:1050 people.catalog.com 199.254.1.2"
                do {
                    socket = Connect(mHost, secure, factory);
                } while (socket == null);
            } else {
                if (secure) {
                    socket = factory.makeSocket(host, port);
                } else {
                    socket = new Socket(host, port);
                }
            }

            if (socket == null) {
                CMS.debug("OCSPPublisher::publish() - socket is null!");
                throw new ELdapException("socket is null");
            }

            // use HttpRequest and POST
            HttpRequest httpReq = new HttpRequest();

            httpReq.setMethod("POST");
            httpReq.setURI(path);
            httpReq.setHeader("Connection", "Keep-Alive");

            httpReq.setHeader("Content-Type",
                    "application/x-www-form-urlencoded");
            httpReq.setHeader("Content-Transfer-Encoding", "7bit");

            httpReq.setHeader("Content-Length",
                    Integer.toString(query.length()));
            httpReq.setContent(query.toString());
            OutputStream os = socket.getOutputStream();
            OutputStreamWriter outputStreamWriter = new OutputStreamWriter(os, "UTF8");

            mLogger.log(ILogger.EV_SYSTEM, ILogger.S_OTHER,
                    ILogger.LL_INFO, "OCSPPublisher: start sending CRL");
            long startTime = CMS.getCurrentDate().getTime();
            CMS.debug("OCSPPublisher: start CRL sending startTime=" + startTime);
            httpReq.write(outputStreamWriter);
            long endTime = CMS.getCurrentDate().getTime();
            CMS.debug("OCSPPublisher: done CRL sending endTime=" + endTime + " diff=" + (endTime - startTime));

            // Read the response
            mLogger.log(ILogger.EV_SYSTEM, ILogger.S_OTHER,
                    ILogger.LL_INFO, "OCSPPublisher: start getting response");
            DataInputStream dis = new DataInputStream(socket.getInputStream());
            String nextline;
            String line = "";
            String error = "";
            boolean status = false;

            while ((nextline = dis.readLine()) != null) {
                if (nextline.startsWith("status=")) {
                    if (nextline.substring(7, nextline.length()).equals("0")) {
                        status = true;
                    }
                }
                if (nextline.startsWith("error=")) {
                    error = nextline.substring(6, nextline.length());
                }
            }
            dis.close();
            if (status) {
                mLogger.log(ILogger.EV_SYSTEM, ILogger.S_OTHER,
                        ILogger.LL_INFO, "OCSPPublisher: successful");
            } else {
                mLogger.log(ILogger.EV_SYSTEM, ILogger.S_OTHER,
                        ILogger.LL_INFO, "OCSPPublisher: failed - " + error);
            }

        } catch (IOException e) {
            CMS.debug("OCSPPublisher: publish failed " + e.toString());
            mLogger.log(ILogger.EV_SYSTEM, ILogger.S_OTHER,
                    ILogger.LL_FAILURE, CMS.getLogMessage("PUBLISH_OCSP_PUBLISHER_ERROR", e.toString()));
        } catch (CRLException e) {
            CMS.debug("OCSPPublisher: publish failed " + e.toString());
            mLogger.log(ILogger.EV_SYSTEM, ILogger.S_OTHER,
                    ILogger.LL_FAILURE, CMS.getLogMessage("PUBLISH_OCSP_PUBLISHER_ERROR", e.toString()));
        } catch (Exception e) {
            CMS.debug("OCSPPublisher: publish failed " + e.toString());
            mLogger.log(ILogger.EV_SYSTEM, ILogger.S_OTHER,
                    ILogger.LL_FAILURE, CMS.getLogMessage("PUBLISH_OCSP_PUBLISHER_ERROR", e.toString()));
        }
    }

    /**
     * Unpublishs a object to the ldap directory.
     * 
     * @param conn the Ldap connection
     *            (null if LDAP publishing is not enabled)
     * @param dn dn of the ldap entry to unpublish cert
     *            (null if LDAP publishing is not enabled)
     * @param object object to unpublish
     *            (java.security.cert.X509Certificate)
     */
    public void unpublish(LDAPConnection conn, String dn, Object object)
            throws ELdapException {
        // NOT USED
    }
}