summaryrefslogtreecommitdiffstats
path: root/pki/base/common/src/com/netscape/cms/crl/CMSCertificateIssuerExtension.java
blob: 601e15d2fcae53937c6a78519346c4fb66ca9136 (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
// --- 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.IOException;
import java.util.Locale;

import netscape.security.x509.CertificateIssuerExtension;
import netscape.security.x509.Extension;
import netscape.security.x509.GeneralNames;
import netscape.security.x509.PKIXExtensions;
import netscape.security.x509.URIName;
import netscape.security.x509.X500Name;

import com.netscape.certsrv.apps.CMS;
import com.netscape.certsrv.base.EBaseException;
import com.netscape.certsrv.base.EPropertyNotFound;
import com.netscape.certsrv.base.IConfigStore;
import com.netscape.certsrv.base.IExtendedPluginInfo;
import com.netscape.certsrv.ca.ICMSCRLExtension;
import com.netscape.certsrv.ca.ICRLIssuingPoint;
import com.netscape.certsrv.common.NameValuePairs;
import com.netscape.certsrv.logging.ILogger;

/**
 * This represents a certificate issuer extension.
 *
 * @version $Revision$, $Date$
 */
public class CMSCertificateIssuerExtension
    implements ICMSCRLExtension, IExtendedPluginInfo {
    private ILogger mLogger = CMS.getLogger();

    public CMSCertificateIssuerExtension() {
    }

    public Extension setCRLExtensionCriticality(Extension ext,
        boolean critical) {
        CertificateIssuerExtension certIssuerExt = null;
        GeneralNames names = null;

        try {
            names = (GeneralNames) ((CertificateIssuerExtension) ext).get(
                        CertificateIssuerExtension.CERTIFICATE_ISSUER);
            certIssuerExt = new CertificateIssuerExtension(Boolean.valueOf(critical),
                        names);
        } catch (IOException e) {
            log(ILogger.LL_FAILURE, CMS.getLogMessage("CRL_CREATE_CERT_ISSUER_EXT", e.toString()));
        }
        return certIssuerExt;
    }

    public Extension getCRLExtension(IConfigStore config,
        Object ip,
        boolean critical) {
        CertificateIssuerExtension certIssuerExt = null;
        int numNames = 0;

        ICRLIssuingPoint crlIssuingPoint = (ICRLIssuingPoint) ip;

        try {
            numNames = config.getInteger("numNames", 0);
        } catch (EBaseException e) {
            log(ILogger.LL_FAILURE, CMS.getLogMessage("CRL_CREATE_INVALID_NUM_NAMES", e.toString()));
        }
        if (numNames > 0) {
            GeneralNames names = new GeneralNames();

            for (int i = 0; i < numNames; i++) {
                String nameType = null;

                try {
                    nameType = config.getString("nameType" + i);
                } catch (EPropertyNotFound e) {
                    log(ILogger.LL_FAILURE, CMS.getLogMessage("CRL_CREATE_UNDEFINED_TYPE", Integer.toString(i), e.toString()));
                } catch (EBaseException e) {
                    log(ILogger.LL_FAILURE, CMS.getLogMessage("CRL_CREATE_INVALID_TYPE", Integer.toString(i), e.toString()));
                }

                if (nameType != null) {
                    String name = null;

                    try {
                        name = config.getString("name" + i);
                    } catch (EPropertyNotFound e) {
                        log(ILogger.LL_FAILURE, CMS.getLogMessage("CRL_CREATE_UNDEFINED_TYPE", Integer.toString(i), e.toString()));
                    } catch (EBaseException e) {
                        log(ILogger.LL_FAILURE, CMS.getLogMessage("CRL_CREATE_INVALID_TYPE", Integer.toString(i), e.toString()));
                    }

                    if (name != null && name.length() > 0) {
                        if (nameType.equalsIgnoreCase("DirectoryName")) {
                            try {
                                X500Name dirName = new X500Name(name);

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

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

            if (names.size() > 0) {
                try {
                    certIssuerExt = new CertificateIssuerExtension(
                                Boolean.valueOf(critical), names);
                } catch (IOException e) {
                    log(ILogger.LL_FAILURE, CMS.getLogMessage("CRL_CREATE_CERT_ISSUER_EXT", e.toString()));
                }
            }
        }

        return certIssuerExt;
    }

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

    public void getConfigParams(IConfigStore config, NameValuePairs nvp) {
        int numNames = 0;

        try {
            numNames = config.getInteger("numNames", 0);
        } catch (EBaseException e) {
            log(ILogger.LL_FAILURE, CMS.getLogMessage("CRL_CREATE_INVALID_NUM_NAMES", e.toString()));
        }
        nvp.add("numNames", String.valueOf(numNames));

        for (int i = 0; i < numNames; i++) {
            String nameType = null;

            try {
                nameType = config.getString("nameType" + i);
            } catch (EPropertyNotFound e) {
                log(ILogger.LL_FAILURE, CMS.getLogMessage("CRL_CREATE_UNDEFINED_TYPE", Integer.toString(i), e.toString()));
            } catch (EBaseException e) {
                log(ILogger.LL_FAILURE, CMS.getLogMessage("CRL_CREATE_INVALID_TYPE", Integer.toString(i), e.toString()));
            }

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

            String name = null;

            try {
                name = config.getString("name" + i);
            } catch (EPropertyNotFound e) {
                log(ILogger.LL_FAILURE, CMS.getLogMessage("CRL_CREATE_UNDEFINED_TYPE", Integer.toString(i), e.toString()));
            } catch (EBaseException e) {
                log(ILogger.LL_FAILURE, CMS.getLogMessage("CRL_CREATE_INVALID_TYPE", Integer.toString(i), e.toString()));
            }

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

        if (numNames < 3) {
            for (int i = numNames; i < 3; i++) {
                nvp.add("nameType" + i, "");
                nvp.add("name" + i, "");
            }
        }
    }

    public String[] getExtendedPluginInfo(Locale locale) {
        String[] params = {
                //"type;choice(CRLExtension,CRLEntryExtension);CRL Entry Extension type."+
                //" This field is not editable.",
                "enable;boolean;Check to enable Certificate Issuer CRL entry extension.",
                "critical;boolean;Set criticality for Certificate Issuer CRL entry extension.",
                "numNames;number;Set number of certificate issuer names for the CRL entry.",
                "nameType0;choice(DirectoryName,URI);Select Certificate Issuer name type.",
                "name0;string;Enter Certificate Issuer name corresponding to the selected name type.",
                "nameType1;choice(DirectoryName,URI);Select Certificate Issuer name type.",
                "name1;string;Enter Certificate Issuer name corresponding to the selected name type.",
                "nameType2;choice(DirectoryName,URI);Select Certificate Issuer name type.",
                "name2;string;Enter Certificate Issuer name corresponding to the selected name type.",
                IExtendedPluginInfo.HELP_TOKEN +
                ";configuration-ca-edit-crlextension-certificateissuer",
                IExtendedPluginInfo.HELP_TEXT +
                ";This CRL entry extension identifies the certificate issuer" +
                " associated with an entry in an indirect CRL."
            };

        return params;
    }

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