summaryrefslogtreecommitdiffstats
path: root/pki/base/common/src/com/netscape/cms/crl/CMSFreshestCRLExtension.java
blob: 3f500b46431a019d58e50f73fb001d36c7b90ffb (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
// --- 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.crl;


import java.io.*;
import java.util.*;
import java.math.BigInteger;
import java.security.cert.CertificateException;
import netscape.security.x509.PKIXExtensions;
import netscape.security.x509.CRLExtensions;
import netscape.security.x509.Extension;
import netscape.security.x509.X500Name;
import netscape.security.x509.URIName;
import com.netscape.certsrv.ca.*;
import netscape.security.x509.GeneralNames;
import netscape.security.x509.GeneralNamesException;
import netscape.security.x509.CRLDistributionPoint;
import netscape.security.x509.FreshestCRLExtension;
import netscape.security.util.BitArray;
import com.netscape.certsrv.base.IConfigStore;
import com.netscape.certsrv.base.IExtendedPluginInfo;
import com.netscape.certsrv.base.EBaseException;
import com.netscape.certsrv.base.EPropertyNotFound;
import com.netscape.certsrv.common.NameValuePairs;
import com.netscape.certsrv.dbs.crldb.*;
import com.netscape.certsrv.logging.*;
import com.netscape.certsrv.apps.*;


/**
 * This represents a freshest CRL extension.
 *
 * @version $Revision$, $Date$
 */
public class CMSFreshestCRLExtension
    implements ICMSCRLExtension, IExtendedPluginInfo {
    public static final String PROP_NUM_POINTS = "numPoints";
    public static final String PROP_POINTTYPE = "pointType";
    public static final String PROP_POINTNAME = "pointName";
    public static final String PROP_DIRNAME = "DirectoryName";
    public static final String PROP_URINAME = "URI";

    private ILogger mLogger = CMS.getLogger();

    public CMSFreshestCRLExtension() {
    }

    public Extension setCRLExtensionCriticality(Extension ext,
        boolean critical) {
        FreshestCRLExtension freshestCRLExt = (FreshestCRLExtension) ext;

        freshestCRLExt.setCritical(critical);

        return freshestCRLExt;
    }

    public Extension getCRLExtension(IConfigStore config, Object ip,
        boolean critical) {
        ICRLIssuingPoint crlIssuingPoint = (ICRLIssuingPoint) ip;
        FreshestCRLExtension freshestCRLExt = null;

        int numPoints = 0;

        try {
            numPoints = config.getInteger("numPoints", 0);
        } catch (EBaseException e) {
            log(ILogger.LL_FAILURE, CMS.getLogMessage("CRL_CREATE_ISSUER_INVALID_NUM_NAMES", e.toString()));
        }

        if (numPoints > 0) {

            for (int i = 0; i < numPoints; i++) {
                CRLDistributionPoint crlDP = new CRLDistributionPoint();
                GeneralNames names = new GeneralNames();
                String pointType = null;

                try {
                    pointType = config.getString(PROP_POINTTYPE + i);
                } catch (EPropertyNotFound e) {
                    log(ILogger.LL_FAILURE, CMS.getLogMessage("CRL_CREATE_DIST_POINT_UNDEFINED", e.toString()));
                } catch (EBaseException e) {
                    log(ILogger.LL_FAILURE, CMS.getLogMessage("CRL_CREATE_DIST_POINT_INVALID", e.toString()));
                }

                if (pointType != null) {
                    String pointName = null;

                    try {
                        pointName = config.getString(PROP_POINTNAME + i);
                    } catch (EPropertyNotFound e) {
                        log(ILogger.LL_FAILURE, CMS.getLogMessage("CRL_CREATE_DIST_POINT_UNDEFINED", e.toString()));
                    } catch (EBaseException e) {
                        log(ILogger.LL_FAILURE, CMS.getLogMessage("CRL_CREATE_DIST_POINT_INVALID", e.toString()));
                    }

                    if (pointName != null && pointName.length() > 0) {
                        if (pointType.equalsIgnoreCase(PROP_DIRNAME)) {
                            try {
                                X500Name dirName = new X500Name(pointName);

                                names.addElement(dirName);
                            } catch (IOException e) {
                                log(ILogger.LL_FAILURE, CMS.getLogMessage("CRL_CREATE_INVALID_500NAME", e.toString()));
                            }
                        } else if (pointType.equalsIgnoreCase(PROP_URINAME)) {
                            URIName uriName = new URIName(pointName);

                            names.addElement(uriName);
                        } else {
                            log(ILogger.LL_FAILURE, CMS.getLogMessage("CRL_INVALID_POTINT_TYPE", pointType));
                        }
                    }
                }

                if (names.size() > 0) {
                    try {
                        crlDP.setFullName(names);
                    } catch (IOException e) {
                        log(ILogger.LL_FAILURE, CMS.getLogMessage("CRL_CANNOT_SET_NAME", e.toString()));
                    } catch (GeneralNamesException e) {
                        log(ILogger.LL_FAILURE, CMS.getLogMessage("CRL_CANNOT_SET_NAME", e.toString()));
                    }
                }

                if (i > 0) {
                    freshestCRLExt.addPoint(crlDP);
                } else {
                    freshestCRLExt = new FreshestCRLExtension(crlDP);
                }
            }
        }

        return freshestCRLExt;
    }

    public String getCRLExtOID() {
        return PKIXExtensions.FreshestCRL_Id.toString();
    }

    public void getConfigParams(IConfigStore config, NameValuePairs nvp) {

        int numPoints = 0;

        try {
            numPoints = config.getInteger(PROP_NUM_POINTS, 0);
        } catch (EBaseException e) {
            log(ILogger.LL_FAILURE, "Invalid numPoints property for CRL " +
                "Freshest CRL extension - " + e);
        }
        nvp.add(PROP_NUM_POINTS, String.valueOf(numPoints));

        for (int i = 0; i < numPoints; i++) {
            String pointType = null;

            try {
                pointType = config.getString(PROP_POINTTYPE + i);
            } catch (EPropertyNotFound e) {
                log(ILogger.LL_FAILURE, CMS.getLogMessage("CRL_CREATE_DIST_POINT_UNDEFINED", e.toString()));
            } catch (EBaseException e) {
                log(ILogger.LL_FAILURE, CMS.getLogMessage("CRL_CREATE_DIST_POINT_INVALID", e.toString()));
            }

            if (pointType != null && pointType.length() > 0) {
                nvp.add(PROP_POINTTYPE + i, pointType);
            } else {
                nvp.add(PROP_POINTTYPE + i, "");
            }

            String pointName = null;

            try {
                pointName = config.getString(PROP_POINTNAME + i);
            } catch (EPropertyNotFound e) {
                log(ILogger.LL_FAILURE, CMS.getLogMessage("CRL_CREATE_DIST_POINT_UNDEFINED", e.toString()));
            } catch (EBaseException e) {
                log(ILogger.LL_FAILURE, CMS.getLogMessage("CRL_CREATE_DIST_POINT_INVALID", e.toString()));
            }

            if (pointName != null && pointName.length() > 0) {
                nvp.add(PROP_POINTNAME + i, pointName);
            } else {
                nvp.add(PROP_POINTNAME + i, "");
            }
        }
    }

    public String[] getExtendedPluginInfo(Locale locale) {
        String[] params = {
                "enable;boolean;Check to enable Freshest CRL extension.",
                "critical;boolean;Set criticality for Freshest CRL extension.",
                PROP_NUM_POINTS + ";number;Set number of CRL distribution points.",
                PROP_POINTTYPE + "0;choice(" + PROP_DIRNAME + "," + PROP_URINAME +
                ");Select CRL distribution point name type.",
                PROP_POINTNAME + "0;string;Enter CRL distribution point name " +
                "corresponding to the selected point type.",
                IExtendedPluginInfo.HELP_TOKEN +
                ";configuration-ca-edit-crlextension-issuingdistributionpoint",
                PROP_POINTTYPE + "1;choice(" + PROP_DIRNAME + "," + PROP_URINAME +
                ");Select CRL distribution point name type.",
                PROP_POINTNAME + "1;string;Enter CRL distribution point name " +
                "corresponding to the selected point type.",
                IExtendedPluginInfo.HELP_TOKEN +
                ";configuration-ca-edit-crlextension-issuingdistributionpoint",
                PROP_POINTTYPE + "2;choice(" + PROP_DIRNAME + "," + PROP_URINAME +
                ");Select CRL distribution point name type.",
                PROP_POINTNAME + "2;string;Enter CRL distribution point name " +
                "corresponding to the selected point type.",
                IExtendedPluginInfo.HELP_TOKEN +
                ";configuration-ca-edit-crlextension-issuingdistributionpoint",
                IExtendedPluginInfo.HELP_TEXT +
                ";The Freshest CRL is a non critical CRL extension " +
                "that identifies the delta CRL distribution points for a particular CRL."
            };

        return params;
    }

    private void log(int level, String msg) {
        mLogger.log(ILogger.EV_SYSTEM, null, ILogger.S_CA, level,
            "CMSFreshestCRLExtension - " + msg);
    }
}