summaryrefslogtreecommitdiffstats
blob: 56a183eb20da8028145e839df154288c52f6e692 (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
/*  Copyright (c) 2016, Isode Limited, London, England.
 *  All rights reserved.
 *
 *  Acquisition and use of this software and related materials for any
 *  purpose requires a written license agreement from Isode Limited,
 *  or a written license from an organisation licensed by Isode Limited
 *  to grant such a license.
 *
 */
package com.isode.stroke.tls;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.logging.Logger;
import java.util.logging.Level;

import com.isode.stroke.base.ByteArray;
import com.isode.stroke.tls.java.JavaCertificate;

public class JavaCertificateFactory implements CertificateFactory {

    private final Logger logger = Logger.getLogger(this.getClass().getName());
    
    private final java.security.cert.CertificateFactory jvmCertFactory;

    public JavaCertificateFactory() {
        java.security.cert.CertificateFactory temp = null;
        try {
            temp = java.security.cert.CertificateFactory.getInstance("X.509");
        } catch (CertificateException e) {
            logger.log(Level.WARNING,"Unable to generate X509 certificate factory",e);
            temp = null;
        }
        jvmCertFactory = temp;
    }
    
    @Override
    public Certificate createCertificateFromDER(ByteArray der) {
        if (jvmCertFactory == null) {
            return null;
        }
        InputStream derInputStream = new ByteArrayInputStream(der.getData());
        try {
            X509Certificate x509Cert = (X509Certificate)jvmCertFactory.generateCertificate(derInputStream);
            return new JavaCertificate(x509Cert);
        } catch (CertificateException e) {
            logger.log(Level.WARNING,"Unable to generate certificate from byte array "+der,e);
            return null;
        } catch (ClassCastException e) {
            // Should not get here as factory should return an x509 certificate
            logger.log(Level.WARNING,"Unable to generate X509 certificate",e);
            return null;
        }
    }

}