OpenConcerto

Dépôt officiel du code source de l'ERP OpenConcerto
sonarqube

svn://code.openconcerto.org/openconcerto

Rev

Rev 144 | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 144 Rev 177
Line 16... Line 16...
16
import java.io.BufferedReader;
16
import java.io.BufferedReader;
17
import java.io.ByteArrayInputStream;
17
import java.io.ByteArrayInputStream;
18
import java.io.File;
18
import java.io.File;
19
import java.io.FileReader;
19
import java.io.FileReader;
20
import java.io.IOException;
20
import java.io.IOException;
-
 
21
import java.security.GeneralSecurityException;
21
import java.security.KeyFactory;
22
import java.security.KeyFactory;
22
import java.security.KeyStore;
23
import java.security.KeyStore;
23
import java.security.KeyStoreException;
-
 
24
import java.security.NoSuchAlgorithmException;
24
import java.security.NoSuchAlgorithmException;
25
import java.security.PrivateKey;
25
import java.security.PrivateKey;
26
import java.security.cert.CertificateException;
26
import java.security.cert.CertificateException;
27
import java.security.cert.CertificateFactory;
27
import java.security.cert.CertificateFactory;
28
import java.security.cert.X509Certificate;
28
import java.security.cert.X509Certificate;
Line 38... Line 38...
38
import javax.net.ssl.SSLServerSocketFactory;
38
import javax.net.ssl.SSLServerSocketFactory;
39
import javax.xml.bind.DatatypeConverter;
39
import javax.xml.bind.DatatypeConverter;
40
 
40
 
41
public class PEMImporter {
41
public class PEMImporter {
42
 
42
 
43
    public static SSLServerSocketFactory createSSLFactory(File privateKeyPem, File certificatePem, String password) throws Exception {
43
    public static SSLServerSocketFactory createSSLFactory(File privateKeyPem, File certificatePem, String password) throws IOException, GeneralSecurityException {
44
        final SSLContext context = SSLContext.getInstance("TLS");
44
        final SSLContext context = SSLContext.getInstance("TLS");
45
        final KeyStore keystore = createKeyStore(privateKeyPem, certificatePem, password);
45
        final KeyStore keystore = createKeyStore(privateKeyPem, certificatePem, password);
46
        final KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509");
46
        final KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509");
47
        kmf.init(keystore, password.toCharArray());
47
        kmf.init(keystore, password.toCharArray());
48
        final KeyManager[] km = kmf.getKeyManagers();
48
        final KeyManager[] km = kmf.getKeyManagers();
Line 55... Line 55...
55
     * 
55
     * 
56
     * @param privateKeyPem the private key PEM file
56
     * @param privateKeyPem the private key PEM file
57
     * @param certificatePem the certificate(s) PEM file
57
     * @param certificatePem the certificate(s) PEM file
58
     * @param the password to set to protect the private key
58
     * @param the password to set to protect the private key
59
     */
59
     */
60
    public static KeyStore createKeyStore(File privateKeyPem, File certificatePem, final String password)
60
    public static KeyStore createKeyStore(File privateKeyPem, File certificatePem, final String password) throws IOException, GeneralSecurityException {
61
            throws Exception, KeyStoreException, IOException, NoSuchAlgorithmException, CertificateException {
-
 
62
        final X509Certificate[] cert = createCertificates(certificatePem);
61
        final X509Certificate[] cert = createCertificates(certificatePem);
63
        final KeyStore keystore = KeyStore.getInstance("JKS");
62
        final KeyStore keystore = KeyStore.getInstance("JKS");
64
        keystore.load(null);
63
        keystore.load(null);
65
        // Import private key
64
        // Import private key
66
        final PrivateKey key = createPrivateKey(privateKeyPem);
65
        final PrivateKey key = createPrivateKey(privateKeyPem);
67
        keystore.setKeyEntry(privateKeyPem.getName(), key, password.toCharArray(), cert);
66
        keystore.setKeyEntry(privateKeyPem.getName(), key, password.toCharArray(), cert);
68
        return keystore;
67
        return keystore;
69
    }
68
    }
70
 
69
 
71
    private static PrivateKey createPrivateKey(File privateKeyPem) throws Exception {
70
    private static PrivateKey createPrivateKey(File privateKeyPem) throws IOException, GeneralSecurityException {
72
        final BufferedReader r = new BufferedReader(new FileReader(privateKeyPem));
71
        final BufferedReader r = new BufferedReader(new FileReader(privateKeyPem));
73
        String s = r.readLine();
72
        String s = r.readLine();
74
        if (s == null || !s.contains("BEGIN PRIVATE KEY")) {
73
        if (s == null || !s.contains("BEGIN PRIVATE KEY")) {
75
            r.close();
74
            r.close();
76
            throw new IllegalArgumentException("No PRIVATE KEY found");
75
            throw new IllegalArgumentException("No PRIVATE KEY found");
Line 88... Line 87...
88
        final String hexString = b.toString();
87
        final String hexString = b.toString();
89
        final byte[] bytes = DatatypeConverter.parseBase64Binary(hexString);
88
        final byte[] bytes = DatatypeConverter.parseBase64Binary(hexString);
90
        return generatePrivateKeyFromDER(bytes);
89
        return generatePrivateKeyFromDER(bytes);
91
    }
90
    }
92
 
91
 
93
    private static X509Certificate[] createCertificates(File certificatePem) throws Exception {
92
    private static X509Certificate[] createCertificates(File certificatePem) throws IOException, CertificateException {
94
        final List<X509Certificate> result = new ArrayList<X509Certificate>();
93
        final List<X509Certificate> result = new ArrayList<X509Certificate>();
95
        final BufferedReader r = new BufferedReader(new FileReader(certificatePem));
94
        final BufferedReader r = new BufferedReader(new FileReader(certificatePem));
96
        String s = r.readLine();
95
        String s = r.readLine();
97
        if (s == null || !s.contains("BEGIN CERTIFICATE")) {
96
        if (s == null || !s.contains("BEGIN CERTIFICATE")) {
98
            r.close();
97
            r.close();