aboutsummaryrefslogtreecommitdiff
path: root/src/main/model/ca/CACertificate.java
blob: 36a9ac54f75906c094fecc6356c99fe4f7a4cab8 (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
package model.ca;

import model.asn1.*;
import model.asn1.exceptions.ParseException;
import model.csr.*;
import model.pki.AlgorithmIdentifier;
import model.pki.SubjectPublicKeyInfo;
import model.pki.cert.*;
import model.pki.cert.Certificate;
import model.pki.crl.CertificateList;
import model.pki.crl.CertificateListContent;
import model.pki.crl.RevokedCertificate;
import model.x501.AttributeTypeAndValue;
import model.x501.Name;
import model.x501.RelativeDistinguishedName;
import ui.Utils;

import java.math.BigInteger;
import java.security.*;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import java.security.spec.RSAPrivateKeySpec;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.*;
import java.util.stream.Stream;

/**
 * Holds a CA private key, its certificate, and signed / revoked list.
 */
public class CACertificate {
    /**
     * The key pair.
     */
    private KeyPair key;

    /**
     * The signed certificate.
     */
    private Certificate certificate;

    /**
     * Signed certificates.
     */
    private List<Certificate> signed;

    /**
     * The next serial number.
     */
    private int serial;

    /**
     * Revoked certs.
     */
    private List<RevokedCertificate> revoked;

    /**
     * EFFECT: Init with a null key and null certificate, empty signed and revoked list, and serial at 1.
     */
    public CACertificate()  {
        this.key = null;
        this.certificate = null;
        this.serial = 1;
        this.signed = new ArrayList<>();
        this.revoked = new ArrayList<>();
    }

    /**
     * EFFECTS: Generate a new RSA2048 private key.
     * REQUIRES: getPublicKey() is null (i.e., no private key had been installed)
     */
    public void generateKey() throws NoSuchAlgorithmException {
        final KeyPairGenerator gen = KeyPairGenerator.getInstance("RSA");
        gen.initialize(2048);
        this.key = gen.generateKeyPair();
    }

    /**
     * EFFECT: Install the CA certificate.
     * MODIFIES: this
     * REQUIRES:
     *   - The new certificate must have the same algorithm and public key as getPublicKey(), except for testing purpose
     *   - It must be a v3 certificate
     *   - It must have basicConstraints { cA = TRUE }
     *   - It must contain key usage Digital Signature, Certificate Sign, CRL Sign
     *   - getCertificate() must be null (i.e., no certificate is installed yet).
     */
    public void installCertificate(Certificate certificate) {
        this.certificate = certificate;
    }

    /**
     * EFFECTS: Generate a CSR based on public key. It will have subject = CN=JCA.
     */
    private CertificationRequestInfo generateCSR() throws ParseException {
        return new CertificationRequestInfo(ASN1Object.TAG_SEQUENCE, null,
                new Int(Int.TAG, null, CertificationRequestInfo.VERSION_V1),
                new Name(ASN1Object.TAG_SEQUENCE, null, new RelativeDistinguishedName[]{
                        new RelativeDistinguishedName(ASN1Object.TAG_SET, null, new AttributeTypeAndValue[]{
                                new AttributeTypeAndValue(ASN1Object.TAG_SEQUENCE, null,
                                        new ObjectIdentifier(ObjectIdentifier.TAG, null,
                                                ObjectIdentifier.OID_CN),
                                        new PrintableString(PrintableString.TAG, null, "JCA"))
                        })
                }),
                getCAPublicKeyInfo(),
                new Attributes(new Tag(TagClass.CONTEXT_SPECIFIC, true, 0), // IMPLICIT
                        null,
                        new Attribute[]{
                                new Attribute(ASN1Object.TAG_SEQUENCE, null,
                                        new ObjectIdentifier(ObjectIdentifier.TAG, null,
                                                new Integer[]{ 1, 3, 6, 1, 4, 1, 311, 13, 2, 3 }),
                                        new Values(ASN1Object.TAG_SET, null,
                                                new ASN1Object[]{
                                                        new IA5String(IA5String.TAG, null,
                                                                "10.0.20348.2")
                                                }))}));
    }

    private Byte[] getPubKeyBitStream() {
        final RSAPublicKey pub = (RSAPublicKey) key.getPublic();
        final BigInteger exponent = pub.getPublicExponent();
        byte[] modules = pub.getModulus().toByteArray();
        final Int asn1Exponent = new Int(Int.TAG, null, exponent);
        // Use OctetString to avoid leading zero issues.
        final ASN1Object asn1Modules = new OctetString(Int.TAG, null, Utils.byteToByte(modules));
        final Byte[] asn1ExponentDER = asn1Exponent.encodeDER();
        final Byte[] asn1ModulesDER = asn1Modules.encodeDER();
        return Stream.of(Arrays.asList(ASN1Object.TAG_SEQUENCE.encodeDER()),
                        Arrays.asList(new ASN1Length(asn1ModulesDER.length + asn1ExponentDER.length).encodeDER()),
                        Arrays.asList(asn1ModulesDER),
                        Arrays.asList(asn1ExponentDER))
                .flatMap(Collection::stream)
                .toArray(Byte[]::new);
    }

    /**
     * EFFECTS: Encode the RSA public key into SubjectPubicKeyInfo format (BIT STRING -> SEQUENCE -> { INT INT }).
     */
    public SubjectPublicKeyInfo getCAPublicKeyInfo() {
        return new SubjectPublicKeyInfo(ASN1Object.TAG_SEQUENCE, null,
                new AlgorithmIdentifier(ASN1Object.TAG_SEQUENCE, null,
                        new ObjectIdentifier(ObjectIdentifier.TAG, null,
                                ObjectIdentifier.OID_RSA_ENCRYPTION),
                        new Null(Null.TAG, null)),
                new BitString(BitString.TAG, null, 0, getPubKeyBitStream()));
    }

    /**
     * EFFECT: Generate CSR and sign it, so the CA can request itself a certificate.
     * REQUIRES: The CA cert must not be installed.
     */
    public CertificationRequest signCSR()
            throws ParseException, NoSuchAlgorithmException, SignatureException, InvalidKeyException {
        final CertificationRequestInfo info = generateCSR();
        return new CertificationRequest(ASN1Object.TAG_SEQUENCE, null,
                info,
                getSigningAlgorithm(),
                new BitString(BitString.TAG, null, 0, signBytes(info.encodeDER())));
    }

    /**
     * EFFECT: Return SHA256withRSA.
     */
    private AlgorithmIdentifier getSigningAlgorithm() {
        return new AlgorithmIdentifier(ASN1Object.TAG_SEQUENCE, null,
                new ObjectIdentifier(ObjectIdentifier.TAG, null,
                        ObjectIdentifier.OID_SHA256_WITH_RSA_ENCRYPTION),
                new Null(Null.TAG, null));
    }

    /**
     * EFFECTS: Sign the CSR based on the template.
     * REQUIRES: The CA cert must be installed first, req must have a subject, template must be enabled.
     * MODIFIES: this
     */
    public Certificate signCert(CertificationRequestInfo req, Template template)
            throws NoSuchAlgorithmException, SignatureException, InvalidKeyException {
        final TbsCertificate newCert = generateCert(req, template);
        final Certificate cert = new Certificate(ASN1Object.TAG_SEQUENCE, null,
                newCert,
                getSigningAlgorithm(),
                new BitString(BitString.TAG, null, 0,
                        signBytes(newCert.encodeValueDER())));
        this.signed.add(cert);
        return cert;
    }

    /**
     * EFFECTS: Hash the input message with SHA256 and sign it with RSA and get the signature.
     */
    private Byte[] signBytes(Byte[] message)
            throws NoSuchAlgorithmException, SignatureException, InvalidKeyException {
        final Signature signature = Signature.getInstance("SHA256withRSA");
        signature.initSign(key.getPrivate());
        signature.update(Utils.byteToByte(message));
        return Utils.byteToByte(signature.sign());
    }

    /**
     * EFFECTS: Apply the template.
     * For the new certificate:
     *  - Issuer will be set to CA#getCertificate()#getSubject()
     *  - The template will be applied (subject, validity, cdp)
     *  - A serial number will be generated
     */
    private TbsCertificate generateCert(CertificationRequestInfo req, Template template) {
        final ZonedDateTime now = ZonedDateTime.now(ZoneId.of("UTC"));
        return new TbsCertificate(ASN1Object.TAG_SEQUENCE, null,
                        new Int(Int.TAG, new Tag(TagClass.CONTEXT_SPECIFIC, true, 0),
                                TbsCertificate.VERSION_V3),
                        new Int(Int.TAG, null, serial++),
                        getSigningAlgorithm(),
                        certificate.getCertificate().getSubject(),
                        new Validity(ASN1Object.TAG_SEQUENCE, null,
                                new GeneralizedTime(GeneralizedTime.TAG, null, now),
                                new UtcTime(UtcTime.TAG, null,
                                        now.plusDays(template.getValidity()))),
                        template.getSubject() == null ? req.getSubject() :
                        template.getSubject(),
                        req.getSubjectPKInfo(),
                        null);
    }

    /**
     * EFFECTS: Add the revocation info to revoked list.
     * REQUIRES: revoked should have the serial of an issued certificate; its date should be current.
     * MODIFIES: this
     */
    public void revoke(RevokedCertificate rev) {
        revoked.add(rev);
    }

    /**
     * EFFECTS: Generate and sign the CRL, based on getRevokedCerts(). The CSR will have current time as thisUpdate with
     * no nextUptime, and it will have issuer same as the CA's subject.
     * REQUIRES: The CA cert must be installed first.
     */
    public CertificateList signCRL()
            throws NoSuchAlgorithmException, SignatureException, InvalidKeyException {
        CertificateListContent content = new CertificateListContent(ASN1Object.TAG_SEQUENCE, null,
                certificate.getCertificate().getSubject(),
                getSigningAlgorithm(),
                new GeneralizedTime(GeneralizedTime.TAG, null, ZonedDateTime.now(ZoneId.of("UTC"))),
                null,
                revoked.toArray(new RevokedCertificate[0]));
        return new CertificateList(ASN1Object.TAG_SEQUENCE, null,
                content,
                getSigningAlgorithm(),
                new BitString(BitString.TAG, null, 0,
                        signBytes(content.encodeValueDER())));
    }

    public Certificate getCertificate() {
        return certificate;
    }

    public List<Certificate> getSigned() {
        return signed;
    }

    /**
     * EFFECTS: Get the public key, or null if no private key is installed.
     */
    public PublicKey getPublicKey() {
        if (key == null) {
            return null;
        }
        return key.getPublic();
    }

    public List<RevokedCertificate> getRevoked() {
        return revoked;
    }

    public int getSerial() {
        return serial;
    }
}