summaryrefslogtreecommitdiffstats
path: root/pki/base/common/src/com/netscape/cmscore/dbs/CRLRepository.java
blob: f9c4369a29236168f053304cb74fcbfe1f28f0cb (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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
// --- 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.cmscore.dbs;

import java.math.BigInteger;
import java.util.Date;
import java.util.Hashtable;
import java.util.Vector;

import netscape.security.x509.RevokedCertificate;

import com.netscape.certsrv.apps.CMS;
import com.netscape.certsrv.base.EBaseException;
import com.netscape.certsrv.dbs.EDBException;
import com.netscape.certsrv.dbs.IDBRegistry;
import com.netscape.certsrv.dbs.IDBSSession;
import com.netscape.certsrv.dbs.IDBSearchResults;
import com.netscape.certsrv.dbs.IDBSubsystem;
import com.netscape.certsrv.dbs.Modification;
import com.netscape.certsrv.dbs.ModificationSet;
import com.netscape.certsrv.dbs.crldb.ICRLIssuingPointRecord;
import com.netscape.certsrv.dbs.crldb.ICRLRepository;

/**
 * A class represents a CRL repository. It stores all the
 * CRL issuing points.
 * <P>
 * 
 * @author thomask
 * @version $Revision$, $Date$
 */
public class CRLRepository extends Repository implements ICRLRepository {

    private final String mLdapCRLIssuingPointName = "cn";
    private IDBSubsystem mDBService;
    private String mBaseDN;

    /**
     * Constructs a CRL repository.
     */
    public CRLRepository(IDBSubsystem dbService, int increment, String baseDN)
            throws EDBException {
        super(dbService, increment, baseDN);
        mBaseDN = baseDN;
        mDBService = dbService;

        IDBRegistry reg = dbService.getRegistry();

        /**
         * String crlRecordOC[] = new String[1];
         * crlRecordOC[0] = Schema.LDAP_OC_CRL_RECORD;
         * reg.registerObjectClass(CRLIssuingPointRecord.class.getName(),
         * crlRecordOC);
         * reg.registerAttribute(ICRLIssuingPointRecord.ATTR_ID, new
         * StringMapper(Schema.LDAP_ATTR_CRL_ID));
         * reg.registerAttribute(ICRLIssuingPointRecord.ATTR_CRL_NUMBER, new
         * BigIntegerMapper(Schema.LDAP_ATTR_CRL_NUMBER));
         * reg.registerAttribute(ICRLIssuingPointRecord.ATTR_CRL_SIZE, new
         * LongMapper(Schema.LDAP_ATTR_CRL_SIZE));
         * reg.registerAttribute(ICRLIssuingPointRecord.ATTR_THIS_UPDATE, new
         * DateMapper(Schema.LDAP_ATTR_THIS_UPDATE));
         * reg.registerAttribute(ICRLIssuingPointRecord.ATTR_NEXT_UPDATE, new
         * DateMapper(Schema.LDAP_ATTR_NEXT_UPDATE));
         * reg.registerAttribute(ICRLIssuingPointRecord.ATTR_CRL, new
         * ByteArrayMapper(Schema.LDAP_ATTR_CRL));
         **/
    }

    /**
     * Retrieves backend database handle.
     */
    public IDBSubsystem getDBSubsystem() {
        return mDBService;
    }

    /**
     * Retrieves DN of this repository.
     */
    public String getDN() {
        return mBaseDN;
    }

    /**
     * Removes all objects with this repository.
     */
    public void removeAllObjects() throws EBaseException {
    }

    /**
     * Adds CRL issuing points.
     */
    public void addCRLIssuingPointRecord(ICRLIssuingPointRecord rec)
            throws EBaseException {
        IDBSSession s = mDBService.createSession();

        try {
            String name = mLdapCRLIssuingPointName + "=" +
                    ((CRLIssuingPointRecord) rec).getId().toString() + "," + getDN();

            s.add(name, rec);
        } finally {
            if (s != null)
                s.close();
        }
    }

    /**
     * Retrieves all issuing points' names
     */
    public Vector<String> getIssuingPointsNames() throws EBaseException {
        IDBSSession s = mDBService.createSession();
        try {
            String[] attrs = { ICRLIssuingPointRecord.ATTR_ID, "objectclass" };
            String filter = "objectclass=" + CMS.getCRLIssuingPointRecordName();
            IDBSearchResults res = s.search(getDN(), filter, attrs);
            Vector<String> v = new Vector<String>();
            while (res.hasMoreElements()) {
                ICRLIssuingPointRecord nextelement =
                        (ICRLIssuingPointRecord) res.nextElement();
                CMS.debug("CRLRepository getIssuingPointsNames(): name = "
                        + nextelement.getId());
                v.addElement(nextelement.getId());
            }

            return v;
        } finally {
            if (s != null)
                s.close();
        }
    }

    /**
     * Reads issuing point record.
     */
    public ICRLIssuingPointRecord readCRLIssuingPointRecord(String id)
            throws EBaseException {
        IDBSSession s = mDBService.createSession();
        CRLIssuingPointRecord rec = null;

        try {
            String name = mLdapCRLIssuingPointName + "=" + id +
                    "," + getDN();

            if (s != null) {
                rec = (CRLIssuingPointRecord) s.read(name);
            }
        } finally {
            if (s != null)
                s.close();
        }
        return rec;
    }

    /**
     * deletes issuing point record.
     */
    public void deleteCRLIssuingPointRecord(String id)
            throws EBaseException {
        IDBSSession s = null;

        try {
            s = mDBService.createSession();
            String name = mLdapCRLIssuingPointName + "=" + id +
                    "," + getDN();

            if (s != null)
                s.delete(name);
        } finally {
            if (s != null)
                s.close();
        }
    }

    public void modifyCRLIssuingPointRecord(String id,
            ModificationSet mods) throws EBaseException {
        IDBSSession s = mDBService.createSession();

        try {
            String name = mLdapCRLIssuingPointName + "=" + id +
                    "," + getDN();

            if (s != null)
                s.modify(name, mods);
        } finally {
            if (s != null)
                s.close();
        }
    }

    /**
     * Updates CRL issuing point record.
     */
    public void updateCRLIssuingPointRecord(String id, byte[] newCRL,
            Date thisUpdate, Date nextUpdate, BigInteger crlNumber, Long crlSize)
            throws EBaseException {
        ModificationSet mods = new ModificationSet();

        if (newCRL != null) {
            mods.add(ICRLIssuingPointRecord.ATTR_CRL,
                    Modification.MOD_REPLACE, newCRL);
        }
        if (nextUpdate != null) {
            mods.add(ICRLIssuingPointRecord.ATTR_NEXT_UPDATE,
                    Modification.MOD_REPLACE, nextUpdate);
        }
        mods.add(ICRLIssuingPointRecord.ATTR_THIS_UPDATE,
                Modification.MOD_REPLACE, thisUpdate);
        mods.add(ICRLIssuingPointRecord.ATTR_CRL_NUMBER,
                Modification.MOD_REPLACE, crlNumber);
        mods.add(ICRLIssuingPointRecord.ATTR_CRL_SIZE,
                Modification.MOD_REPLACE, crlSize);
        modifyCRLIssuingPointRecord(id, mods);
    }

    /**
     * Updates CRL issuing point record.
     */
    public void updateCRLIssuingPointRecord(String id, byte[] newCRL,
            Date thisUpdate, Date nextUpdate, BigInteger crlNumber, Long crlSize,
            Hashtable<BigInteger, RevokedCertificate> revokedCerts,
            Hashtable<BigInteger, RevokedCertificate> unrevokedCerts,
            Hashtable<BigInteger, RevokedCertificate> expiredCerts)
            throws EBaseException {
        ModificationSet mods = new ModificationSet();

        if (newCRL != null) {
            mods.add(ICRLIssuingPointRecord.ATTR_CRL,
                    Modification.MOD_REPLACE, newCRL);
        }
        if (nextUpdate != null) {
            mods.add(ICRLIssuingPointRecord.ATTR_NEXT_UPDATE,
                    Modification.MOD_REPLACE, nextUpdate);
        }
        mods.add(ICRLIssuingPointRecord.ATTR_THIS_UPDATE,
                Modification.MOD_REPLACE, thisUpdate);
        mods.add(ICRLIssuingPointRecord.ATTR_CRL_NUMBER,
                Modification.MOD_REPLACE, crlNumber);
        mods.add(ICRLIssuingPointRecord.ATTR_CRL_SIZE,
                Modification.MOD_REPLACE, crlSize);
        if (revokedCerts != null) {
            mods.add(ICRLIssuingPointRecord.ATTR_REVOKED_CERTS,
                    Modification.MOD_REPLACE, revokedCerts);
        }
        if (unrevokedCerts != null) {
            mods.add(ICRLIssuingPointRecord.ATTR_UNREVOKED_CERTS,
                    Modification.MOD_REPLACE, unrevokedCerts);
        }
        if (expiredCerts != null) {
            mods.add(ICRLIssuingPointRecord.ATTR_EXPIRED_CERTS,
                    Modification.MOD_REPLACE, expiredCerts);
        }
        if (revokedCerts != null || unrevokedCerts != null) {
            mods.add(ICRLIssuingPointRecord.ATTR_FIRST_UNSAVED,
                    Modification.MOD_REPLACE, ICRLIssuingPointRecord.CLEAN_CACHE);
        }
        modifyCRLIssuingPointRecord(id, mods);
    }

    /**
     * Updates CRL issuing point record with recently revoked certificates info.
     */
    public void updateRevokedCerts(String id,
            Hashtable<BigInteger, RevokedCertificate> revokedCerts,
            Hashtable<BigInteger, RevokedCertificate> unrevokedCerts)
            throws EBaseException {
        ModificationSet mods = new ModificationSet();

        mods.add(ICRLIssuingPointRecord.ATTR_REVOKED_CERTS,
                Modification.MOD_REPLACE, revokedCerts);
        mods.add(ICRLIssuingPointRecord.ATTR_UNREVOKED_CERTS,
                Modification.MOD_REPLACE, unrevokedCerts);
        mods.add(ICRLIssuingPointRecord.ATTR_FIRST_UNSAVED,
                Modification.MOD_REPLACE, ICRLIssuingPointRecord.CLEAN_CACHE);
        modifyCRLIssuingPointRecord(id, mods);
    }

    /**
     * Updates CRL issuing point record with recently expired certificates info.
     */
    public void updateExpiredCerts(String id, Hashtable<BigInteger, RevokedCertificate> expiredCerts)
            throws EBaseException {
        ModificationSet mods = new ModificationSet();

        mods.add(ICRLIssuingPointRecord.ATTR_EXPIRED_CERTS,
                Modification.MOD_REPLACE, expiredCerts);
        modifyCRLIssuingPointRecord(id, mods);
    }

    /**
     * Updates CRL issuing point record with CRL cache info.
     */
    public void updateCRLCache(String id, Long crlSize,
            Hashtable<BigInteger, RevokedCertificate> revokedCerts,
            Hashtable<BigInteger, RevokedCertificate> unrevokedCerts,
            Hashtable<BigInteger, RevokedCertificate> expiredCerts)
            throws EBaseException {
        ModificationSet mods = new ModificationSet();

        if (crlSize != null) {
            mods.add(ICRLIssuingPointRecord.ATTR_CRL_SIZE,
                    Modification.MOD_REPLACE, crlSize);
        }
        mods.add(ICRLIssuingPointRecord.ATTR_REVOKED_CERTS,
                Modification.MOD_REPLACE, revokedCerts);
        mods.add(ICRLIssuingPointRecord.ATTR_UNREVOKED_CERTS,
                Modification.MOD_REPLACE, unrevokedCerts);
        mods.add(ICRLIssuingPointRecord.ATTR_EXPIRED_CERTS,
                Modification.MOD_REPLACE, expiredCerts);
        mods.add(ICRLIssuingPointRecord.ATTR_FIRST_UNSAVED,
                Modification.MOD_REPLACE, ICRLIssuingPointRecord.CLEAN_CACHE);
        modifyCRLIssuingPointRecord(id, mods);
    }

    /**
     * Updates CRL issuing point record with delta-CRL.
     */
    public void updateDeltaCRL(String id, BigInteger deltaCRLNumber,
                               Long deltaCRLSize, Date nextUpdate,
                               byte[] deltaCRL)
            throws EBaseException {
        ModificationSet mods = new ModificationSet();

        if (deltaCRLNumber != null) {
            mods.add(ICRLIssuingPointRecord.ATTR_DELTA_NUMBER,
                    Modification.MOD_REPLACE, deltaCRLNumber);
        }
        if (deltaCRLSize != null) {
            mods.add(ICRLIssuingPointRecord.ATTR_DELTA_SIZE,
                    Modification.MOD_REPLACE, deltaCRLSize);
        }
        if (nextUpdate != null) {
            mods.add(ICRLIssuingPointRecord.ATTR_NEXT_UPDATE,
                    Modification.MOD_REPLACE, nextUpdate);
        }
        if (deltaCRL != null) {
            mods.add(ICRLIssuingPointRecord.ATTR_DELTA_CRL,
                    Modification.MOD_REPLACE, deltaCRL);
        }
        modifyCRLIssuingPointRecord(id, mods);
    }

    public void updateFirstUnsaved(String id, String firstUnsaved)
            throws EBaseException {
        ModificationSet mods = new ModificationSet();

        if (firstUnsaved != null) {
            mods.add(ICRLIssuingPointRecord.ATTR_FIRST_UNSAVED,
                    Modification.MOD_REPLACE, firstUnsaved);
        }
        modifyCRLIssuingPointRecord(id, mods);
    }

    public BigInteger getLastSerialNumberInRange(BigInteger serial_low_bound, BigInteger serial_upper_bound)
            throws EBaseException {

        return null;
    }
}