aboutsummaryrefslogtreecommitdiff
path: root/src/main/model/pki/crl/RevokedCertificate.java
blob: 1f0afc105e7b191bced86c2bf6e6d0f1368656bd (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
package model.pki.crl;

import model.asn1.*;

import java.util.Arrays;
import java.util.Collection;
import java.util.stream.Stream;

/**
 * Indicates the revocation status of a certificate, given its serial number, revocation date, and reason.
 * <pre>
 *     SEQUENCE {
 *        serialNumber CertificateSerialNumber,
 *        revocationDate Time,
 *        crlEntryExtensions Extensions OPTIONAL,
 *     ...}
 * </pre>
 */
public class RevokedCertificate extends ASN1Object {
    private final Int serialNumber;
    private final ASN1Time revocationDate;
    private final Reason reason;

    /**
     * EFFECT: Init with tags and parameters. See {@link ASN1Object} for tags.
     * REQUIRES: revocationDate should be either UtcTime or GeneralTime.
     */
    public RevokedCertificate(Tag tag, Tag parentTag,
                              Int serialNumber,
                              ASN1Time revocationDate,
                              Reason reason) {
        super(tag, parentTag);
        this.serialNumber = serialNumber;
        this.revocationDate = revocationDate;
        this.reason = reason;
    }

    @Override
    public Byte[] encodeValueDER() {
        final Byte[] r = new OctetString(OctetString.TAG,
                null,
                new Byte[]{0x0A, 0x01, (byte) reason.getVal()})
                .encodeDER();
        final Byte[] oid = new ObjectIdentifier(ObjectIdentifier.TAG, null, ObjectIdentifier.OID_CRL_REASON)
                .encodeDER();
        final Byte[] seqExt = Stream.of(Arrays.asList(TAG_SEQUENCE.encodeDER()),
                        Arrays.asList(new ASN1Length(r.length + oid.length).encodeDER()),
                        Arrays.asList(oid),
                        Arrays.asList(r))
                .flatMap(Collection::stream)
                .toArray(Byte[]::new);
        return Stream.of(Arrays.asList(serialNumber.encodeDER()),
                        Arrays.asList(revocationDate.encodeDER()),
                        Arrays.asList(TAG_SEQUENCE.encodeDER()),
                        Arrays.asList(new ASN1Length(seqExt.length).encodeDER()),
                        Arrays.asList(seqExt))
                .flatMap(Collection::stream)
                .toArray(Byte[]::new);
    }

    public Int getSerialNumber() {
        return serialNumber;
    }

    public ASN1Time getRevocationDate() {
        return revocationDate;
    }

    public Reason getReason() {
        return reason;
    }
}