summaryrefslogtreecommitdiffstats
path: root/pki/base/util/src/netscape/security/x509/CRLDistributionPoint.java
blob: 6cbca4ad00bb0827729efd2f2c49b8c71c8a92c6 (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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
// --- 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 netscape.security.x509;

import netscape.security.util.*;
import netscape.security.x509.*;
import org.mozilla.jss.asn1.*;
import java.io.*;

/**
 * <pre>
 * DistributionPoint ::= SEQUENCE {
 *      distributionPoint       [0]     DistributionPointName OPTIONAL,
 *      reasons                 [1]     ReasonFlags OPTIONAL,
 *      cRLIssuer               [2]     GeneralNames OPTIONAL }
 *
 * DistributionPointName ::= CHOICE {
 *      fullName                [0]     GeneralNames,
 *      nameRelativeToCRLIssuer [1]     RelativeDistinguishedName }
 *
 * ReasonFlags ::= BIT STRING {
 *      unused                  (0),
 *      keyCompromise           (1),
 *      cACompromise            (2),
 *      affiliationChanged      (3),
 *      superseded              (4),
 *      cessationOfOperation    (5),
 *      certificateHold         (6) }
 * </pre>
 */
public class CRLDistributionPoint implements ASN1Value {

    // at most one of the two following may be specified:
    private GeneralNames fullName;
    private RDN relativeName;

    // cache encoding of fullName
    private ANY fullNameEncoding;

    private BitArray reasons; // optional, may be null
    private GeneralNames CRLIssuer; // optional, may be null
    private ANY CRLIssuerEncoding;

    // default constructor does nothing.

    /**
     * Returns the <code>fullName</code> of the
     * <code>DistributionPointName</code>, which may be <code>null</code>.
     */
    public GeneralNames getFullName() {
        return fullName;
    }

    /**
     * Returns the <code>relativeName</code> of the
     * <code>DistributionPointName</code>, which may be <code>null</code>.
     */
    public RDN getRelativeName() {
        return relativeName;
    }

    /**
     * Sets the <code>fullName</code> of the
     * <code>DistributionPointName</code>.  It may be set to <code>null</code>.
     * If it is set to a non-null value, <code>relativeName</code> will be
     * set to <code>null</code>, because at most one of these two attributes
     * can be specified at a time.
     * @exception GeneralNamesException If an error occurs encoding the
     *      name.
     */
    public void setFullName(GeneralNames fullName)
            throws GeneralNamesException, IOException
    {
        this.fullName = fullName;
        if( fullName != null ) {
            // encode the name to catch any problems with it
            DerOutputStream derOut = new DerOutputStream();
            fullName.encode(derOut);
            try {
                ANY raw = new ANY(derOut.toByteArray());
                ByteArrayOutputStream bos = new ByteArrayOutputStream();
                raw.encodeWithAlternateTag( Tag.get(0), bos );
                fullNameEncoding = new ANY( bos.toByteArray() );
            } catch(InvalidBERException e) {
                // assume this won't happen, since it would imply a bug
                // in DerOutputStream
                throw new GeneralNamesException( e.toString() );
            }

            this.relativeName = null;
        }
    }

    /**
     * Sets the <code>relativeName</code> of the
     * <code>DistributionPointName</code>.  It may be set to <code>null</code>.
     * If it is set to a non-null value, <code>fullName</code> will be
     * set to <code>null</code>, because at most one of these two attributes
     * can be specified at a time.
     */
    public void setRelativeName(RDN relativeName) {
        this.relativeName = relativeName;
        if( relativeName != null ) {
            this.fullName = null;
        }
    }

    /**
     * Returns the reason flags for this distribution point.  May be
     * <code>null</code>.
     */
    public BitArray getReasons() {
        return reasons;
    }

    /**
     * Sets the reason flags for this distribution point.  May be set to
     * <code>null</code>.
     */
    public void setReasons(BitArray reasons) {
        this.reasons = reasons;
    }


    /**
     * Returns the CRLIssuer for the CRL at this distribution point.
     * May be <code>null</code>.
     */
    public GeneralNames getCRLIssuer() {
        return CRLIssuer;
    }

    /**
     * Sets the CRLIssuer for the CRL at this distribution point.
     * May be set to <code>null</code>.
     * @exception GeneralNamesException If an error occurs encoding the name.
     */
    public void setCRLIssuer(GeneralNames CRLIssuer)
            throws GeneralNamesException, IOException
    {
        this.CRLIssuer = CRLIssuer;
        
        if( CRLIssuer != null ) {
            // encode the name to catch any problems with it
            DerOutputStream derOut = new DerOutputStream();
            CRLIssuer.encode(derOut);
            try {
                ANY raw = new ANY( derOut.toByteArray() );
                ByteArrayOutputStream bos = new ByteArrayOutputStream();
                raw.encodeWithAlternateTag( Tag.get(2), bos);
                CRLIssuerEncoding = new ANY(bos.toByteArray());
            } catch(InvalidBERException e) {
                throw new GeneralNamesException(e.toString());
            }
        }
    }

    /////////////////////////////////////////////////////////////
    // DER encoding
    /////////////////////////////////////////////////////////////

    private static final Tag TAG = SEQUENCE.TAG;

    public Tag getTag() {
        return TAG;
    }

    public void encode(OutputStream ostream) throws IOException {
        encode(TAG, ostream);
    }

    public void encode(Tag implicitTag, OutputStream ostream)
        throws IOException
    {
        SEQUENCE seq = new SEQUENCE();
        DerOutputStream derOut;

      try {

        // Encodes the DistributionPointName.  Because DistributionPointName
        // is a CHOICE, the [0] tag is forced to be EXPLICIT.
        if( fullName != null ) {
            EXPLICIT distPoint = new EXPLICIT( Tag.get(0), fullNameEncoding);
            seq.addElement( distPoint );
        } else if( relativeName != null ) {
            derOut = new DerOutputStream();
            relativeName.encode(derOut);
            ANY rn = new ANY(derOut.toByteArray());
            EXPLICIT raw = new EXPLICIT( Tag.get(1), rn );
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            raw.encode( bos );
            ANY distPointName = new ANY(bos.toByteArray());
            EXPLICIT distPoint = new EXPLICIT( Tag.get(0), distPointName);
            seq.addElement( distPoint );
        }

        // Encodes the ReasonFlags.
        if( reasons != null ) {
            derOut = new DerOutputStream();
            derOut.putUnalignedBitString(reasons);
            ANY raw = new ANY(derOut.toByteArray());
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            raw.encodeWithAlternateTag(Tag.get(1), bos);
            ANY reasonEncoding = new ANY(bos.toByteArray());
            seq.addElement( Tag.get(1), reasonEncoding);
        }

        // Encodes the CRLIssuer
        if( CRLIssuer != null ) {
            seq.addElement( Tag.get(2), CRLIssuerEncoding );
        }

        seq.encode(implicitTag, ostream);

      } catch(InvalidBERException e) {
            // this shouldn't happen unless there is a bug in one of 
            // the Sun encoding classes
            throw new IOException(e.toString());
      }
    }

    // Template singleton
    private static Template templateInstance = new Template();

    /**
     * Returns an instance of a template for decoding a CRLDistributionPoint.
     */
    public static Template getTemplate() {
        return templateInstance;
    }

    public static void main(String args[]) {
      try {
        if( args.length != 1 ) {
            System.out.println("Usage: CRLDistributionPoint <outfile>");
            System.exit(-1);
        }

        ByteArrayOutputStream bos = new ByteArrayOutputStream();

        SEQUENCE cdps = new SEQUENCE();

        // URI only
        CRLDistributionPoint cdp = new CRLDistributionPoint();
        URIName uri = new URIName("http://www.mycrl.com/go/here");
        GeneralNames generalNames = new GeneralNames();
        generalNames.addElement(uri);
        cdp.setFullName(generalNames);
        cdps.addElement(cdp);

        // DN only
        cdp = new CRLDistributionPoint();
        X500Name dn = new X500Name("CN=Otis Smith,E=otis@fedoraproject.org"+
            ",OU=Certificate Server,O=Fedora,C=US");
        generalNames = new GeneralNames();
        generalNames.addElement(dn);
        cdp.setFullName(generalNames);
        cdps.addElement(cdp);

        // DN + reason
        BitArray ba = new BitArray(5, new byte[] {(byte)0x28} );
        cdp = new CRLDistributionPoint();
        cdp.setFullName(generalNames);
        cdp.setReasons(ba);
        cdps.addElement(cdp);
        

        // relative DN + reason + crlIssuer
        cdp = new CRLDistributionPoint();
        RDN rdn = new RDN("OU=foobar dept");
        cdp.setRelativeName(rdn);
        cdp.setReasons(ba);
        cdp.setCRLIssuer(generalNames);
        cdps.addElement(cdp);

        cdps.encode(bos);

        byte[] encoded = bos.toByteArray();
        (new FileOutputStream(args[0])).write(encoded);

        SEQUENCE.OF_Template seqt = new SEQUENCE.OF_Template(getTemplate());

        cdps = (SEQUENCE) ASN1Util.decode(seqt, encoded);

        int size = cdps.size();
        System.out.println("Total number of CDPs: " + size);
        for( int i = 0; i < size; i++) {
            System.out.println("\nCDP " + i);
            cdp = (CRLDistributionPoint) cdps.elementAt(i);
            GeneralNames gn = cdp.getFullName();
            if( gn == null ) {
                System.out.println("No full name");
            } else {
                System.out.println(gn);
            }
            rdn = cdp.getRelativeName();
            if( rdn == null ) {
                System.out.println("No relative name");
            } else {
                System.out.println(rdn);
            }
            if( cdp.getReasons() == null ) {
                System.out.println("No reasons");
            } else {
                System.out.println(cdp.getReasons());
            }
            gn = cdp.getCRLIssuer();
            if( gn == null ) {
                System.out.println("No cRLIssuer");
            } else {
                System.out.println(gn);
            }
        }
        System.out.println("Done");


      } catch(Exception e) {
            e.printStackTrace();
      }
    }


/**
 * Template for decoding CRLDistributionPoint.
 */
public static class Template implements ASN1Template {

    public boolean tagMatch(Tag tag) {
        return TAG.equals(tag);
    }

    public ASN1Value decode(InputStream istream)
        throws IOException, InvalidBERException
    {
        return decode(TAG, istream);
    }

    public ASN1Value decode(Tag implicitTag, InputStream istream)
        throws IOException, InvalidBERException
    {
        CRLDistributionPoint cdp = new CRLDistributionPoint();

        //
        // construct the top-level sequence
        //

        SEQUENCE.Template seqt = SEQUENCE.getTemplate();

        // distributionPoint
        seqt.addOptionalElement(
            new EXPLICIT.Template(Tag.get(0), ANY.getTemplate()) );

        // reasons
        seqt.addOptionalElement( Tag.get(1), BIT_STRING.getTemplate());

        // cRLIssuer
        // This will have a tag of 2, but we can't say that here
        // because ANYs can't have implicit tags.  We don't need to say
        // it, because we do check the tags on the other two elements
        // in the sequence, so we'll know if we get this one.
        seqt.addOptionalElement(  ANY.getTemplate() );

        //
        // decode the top-level sequence
        //
        SEQUENCE top = (SEQUENCE) seqt.decode(implicitTag, istream);


        // decode the distribution point name
        if( top.elementAt(0) != null ) {
            EXPLICIT exp = (EXPLICIT) top.elementAt(0);
            ANY distPoint = (ANY) exp.getContent();
            if( distPoint.getTag().equals(Tag.get(0)) ) {
                // fullName
                try {
                    DerValue dv = new DerValue(distPoint.getEncoded());
                    //toFile("encodedFullName", distPoint.getEncoded());
                    dv.resetTag(dv.tag_Sequence);
                    cdp.setFullName( new GeneralNames(dv) );
                } catch(GeneralNamesException e) {
                    throw new InvalidBERException( "fullName: " + e.toString());
                } catch(IOException e) {
                    throw new InvalidBERException( "fullName: " + e.toString());
                }
            } else if( distPoint.getTag().equals(Tag.get(1)) ) {
                // relative name
                try {
                    DerValue dv = new DerValue(distPoint.getEncoded());
                    /* dv is as follows:
   0   12: [1] {
   2   10:   SET {
   4    8:     SEQUENCE {
   6    3:       OBJECT IDENTIFIER commonName (2 5 4 3)
  11    1:       PrintableString 'x'
         :       }
         :     }
         :   }
                     */
                    dv = dv.data.getDerValue(); // skipping the tag
                    /* after the skipping, we have:
   0   10: SET {
   2    8:   SEQUENCE {
   4    3:     OBJECT IDENTIFIER commonName (2 5 4 3)
   9    1:     PrintableString 'x'
         :     }
         :   }
                     */
                    dv.resetTag(dv.tag_Set);
                    cdp.setRelativeName( new RDN(dv) );
                } catch(IOException e) {
                    throw new InvalidBERException( "relativeName " +
                        e.toString() );
                }
            } else {
                throw new InvalidBERException(
                    "Unknown tag " + distPoint.getTag() +
                    " in distributionPoint" );
            }
        }

        // decode the reasons
        if( top.elementAt(1) != null ) {
            BIT_STRING bs = (BIT_STRING) top.elementAt(1);
            byte[] bits = bs.getBits();
            cdp.setReasons(
                new BitArray( (bits.length * 8) - bs.getPadCount(), bits) );
        }

        // decode the cRLIssuer
        if( top.elementAt(2) != null ) {
            ANY issuer = (ANY) top.elementAt(2);
            if( ! issuer.getTag().equals(Tag.get(2)) ) {
                throw new InvalidBERException("Invalid tag " + issuer.getTag());
            }
            try {
                DerValue dv = new DerValue( issuer.getEncoded() );
                dv.resetTag(dv.tag_Sequence);
                cdp.setCRLIssuer( new GeneralNames(dv) );
            } catch(GeneralNamesException e) {
                throw new InvalidBERException( "cRLIssuer " + e.toString() );
            } catch(IOException e) {
                throw new InvalidBERException( "cRLIssuer " + e.toString() );
            }
        }

        return cdp;

    }
}

private static void toFile(String filename, byte[] bytes) throws IOException{
        FileOutputStream fos = new FileOutputStream(filename);
        fos.write(bytes);
        fos.close();
}


}