142 |
ilm |
1 |
/*
|
|
|
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
|
|
|
3 |
*
|
185 |
ilm |
4 |
* Copyright 2011-2019 OpenConcerto, by ILM Informatique. All rights reserved.
|
142 |
ilm |
5 |
*
|
|
|
6 |
* The contents of this file are subject to the terms of the GNU General Public License Version 3
|
|
|
7 |
* only ("GPL"). You may not use this file except in compliance with the License. You can obtain a
|
|
|
8 |
* copy of the License at http://www.gnu.org/licenses/gpl-3.0.html See the License for the specific
|
|
|
9 |
* language governing permissions and limitations under the License.
|
|
|
10 |
*
|
|
|
11 |
* When distributing the software, include this License Header Notice in each file.
|
|
|
12 |
*/
|
|
|
13 |
|
|
|
14 |
package org.openconcerto.utils;
|
|
|
15 |
|
185 |
ilm |
16 |
import static java.util.Base64.getMimeDecoder;
|
|
|
17 |
|
142 |
ilm |
18 |
import java.io.BufferedReader;
|
|
|
19 |
import java.io.ByteArrayInputStream;
|
|
|
20 |
import java.io.File;
|
|
|
21 |
import java.io.FileReader;
|
|
|
22 |
import java.io.IOException;
|
177 |
ilm |
23 |
import java.security.GeneralSecurityException;
|
142 |
ilm |
24 |
import java.security.KeyFactory;
|
|
|
25 |
import java.security.KeyStore;
|
|
|
26 |
import java.security.NoSuchAlgorithmException;
|
|
|
27 |
import java.security.PrivateKey;
|
|
|
28 |
import java.security.cert.CertificateException;
|
|
|
29 |
import java.security.cert.CertificateFactory;
|
|
|
30 |
import java.security.cert.X509Certificate;
|
|
|
31 |
import java.security.interfaces.RSAPrivateKey;
|
|
|
32 |
import java.security.spec.InvalidKeySpecException;
|
|
|
33 |
import java.security.spec.PKCS8EncodedKeySpec;
|
|
|
34 |
import java.util.ArrayList;
|
|
|
35 |
import java.util.List;
|
|
|
36 |
|
|
|
37 |
import javax.net.ssl.KeyManager;
|
|
|
38 |
import javax.net.ssl.KeyManagerFactory;
|
|
|
39 |
import javax.net.ssl.SSLContext;
|
|
|
40 |
import javax.net.ssl.SSLServerSocketFactory;
|
|
|
41 |
|
|
|
42 |
public class PEMImporter {
|
|
|
43 |
|
177 |
ilm |
44 |
public static SSLServerSocketFactory createSSLFactory(File privateKeyPem, File certificatePem, String password) throws IOException, GeneralSecurityException {
|
142 |
ilm |
45 |
final SSLContext context = SSLContext.getInstance("TLS");
|
|
|
46 |
final KeyStore keystore = createKeyStore(privateKeyPem, certificatePem, password);
|
|
|
47 |
final KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509");
|
|
|
48 |
kmf.init(keystore, password.toCharArray());
|
|
|
49 |
final KeyManager[] km = kmf.getKeyManagers();
|
|
|
50 |
context.init(km, null, null);
|
|
|
51 |
return context.getServerSocketFactory();
|
|
|
52 |
}
|
|
|
53 |
|
|
|
54 |
/**
|
|
|
55 |
* Create a KeyStore from standard PEM files
|
|
|
56 |
*
|
|
|
57 |
* @param privateKeyPem the private key PEM file
|
|
|
58 |
* @param certificatePem the certificate(s) PEM file
|
|
|
59 |
* @param the password to set to protect the private key
|
|
|
60 |
*/
|
177 |
ilm |
61 |
public static KeyStore createKeyStore(File privateKeyPem, File certificatePem, final String password) throws IOException, GeneralSecurityException {
|
180 |
ilm |
62 |
if (!privateKeyPem.exists()) {
|
|
|
63 |
throw new IllegalArgumentException("private key file missing : " + privateKeyPem.getAbsolutePath());
|
|
|
64 |
}
|
|
|
65 |
if (!certificatePem.exists()) {
|
|
|
66 |
throw new IllegalArgumentException("certificate file missing : " + certificatePem.getAbsolutePath());
|
|
|
67 |
}
|
142 |
ilm |
68 |
final X509Certificate[] cert = createCertificates(certificatePem);
|
|
|
69 |
final KeyStore keystore = KeyStore.getInstance("JKS");
|
|
|
70 |
keystore.load(null);
|
|
|
71 |
// Import private key
|
|
|
72 |
final PrivateKey key = createPrivateKey(privateKeyPem);
|
|
|
73 |
keystore.setKeyEntry(privateKeyPem.getName(), key, password.toCharArray(), cert);
|
|
|
74 |
return keystore;
|
|
|
75 |
}
|
|
|
76 |
|
177 |
ilm |
77 |
private static PrivateKey createPrivateKey(File privateKeyPem) throws IOException, GeneralSecurityException {
|
142 |
ilm |
78 |
final BufferedReader r = new BufferedReader(new FileReader(privateKeyPem));
|
|
|
79 |
String s = r.readLine();
|
|
|
80 |
if (s == null || !s.contains("BEGIN PRIVATE KEY")) {
|
|
|
81 |
r.close();
|
|
|
82 |
throw new IllegalArgumentException("No PRIVATE KEY found");
|
|
|
83 |
}
|
144 |
ilm |
84 |
final StringBuilder b = new StringBuilder();
|
142 |
ilm |
85 |
s = "";
|
|
|
86 |
while (s != null) {
|
|
|
87 |
if (s.contains("END PRIVATE KEY")) {
|
|
|
88 |
break;
|
|
|
89 |
}
|
|
|
90 |
b.append(s);
|
|
|
91 |
s = r.readLine();
|
|
|
92 |
}
|
|
|
93 |
r.close();
|
|
|
94 |
final String hexString = b.toString();
|
185 |
ilm |
95 |
final byte[] bytes = getMimeDecoder().decode(hexString);
|
142 |
ilm |
96 |
return generatePrivateKeyFromDER(bytes);
|
|
|
97 |
}
|
|
|
98 |
|
177 |
ilm |
99 |
private static X509Certificate[] createCertificates(File certificatePem) throws IOException, CertificateException {
|
142 |
ilm |
100 |
final List<X509Certificate> result = new ArrayList<X509Certificate>();
|
|
|
101 |
final BufferedReader r = new BufferedReader(new FileReader(certificatePem));
|
|
|
102 |
String s = r.readLine();
|
|
|
103 |
if (s == null || !s.contains("BEGIN CERTIFICATE")) {
|
|
|
104 |
r.close();
|
|
|
105 |
throw new IllegalArgumentException("No CERTIFICATE found");
|
|
|
106 |
}
|
144 |
ilm |
107 |
StringBuilder b = new StringBuilder();
|
142 |
ilm |
108 |
while (s != null) {
|
|
|
109 |
if (s.contains("END CERTIFICATE")) {
|
|
|
110 |
String hexString = b.toString();
|
185 |
ilm |
111 |
final byte[] bytes = getMimeDecoder().decode(hexString);
|
142 |
ilm |
112 |
X509Certificate cert = generateCertificateFromDER(bytes);
|
|
|
113 |
result.add(cert);
|
144 |
ilm |
114 |
b = new StringBuilder();
|
142 |
ilm |
115 |
} else {
|
|
|
116 |
if (!s.startsWith("----")) {
|
|
|
117 |
b.append(s);
|
|
|
118 |
}
|
|
|
119 |
}
|
|
|
120 |
s = r.readLine();
|
|
|
121 |
}
|
|
|
122 |
r.close();
|
|
|
123 |
|
|
|
124 |
return result.toArray(new X509Certificate[result.size()]);
|
|
|
125 |
}
|
|
|
126 |
|
|
|
127 |
private static RSAPrivateKey generatePrivateKeyFromDER(byte[] keyBytes) throws InvalidKeySpecException, NoSuchAlgorithmException {
|
|
|
128 |
final PKCS8EncodedKeySpec spec = new PKCS8EncodedKeySpec(keyBytes);
|
|
|
129 |
final KeyFactory factory = KeyFactory.getInstance("RSA");
|
|
|
130 |
return (RSAPrivateKey) factory.generatePrivate(spec);
|
|
|
131 |
}
|
|
|
132 |
|
|
|
133 |
private static X509Certificate generateCertificateFromDER(byte[] certBytes) throws CertificateException {
|
|
|
134 |
final CertificateFactory factory = CertificateFactory.getInstance("X.509");
|
|
|
135 |
return (X509Certificate) factory.generateCertificate(new ByteArrayInputStream(certBytes));
|
|
|
136 |
}
|
|
|
137 |
|
|
|
138 |
}
|