1 |
/*
|
1 |
/*
|
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
|
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
|
3 |
*
|
3 |
*
|
4 |
* Copyright 2011 OpenConcerto, by ILM Informatique. All rights reserved.
|
4 |
* Copyright 2011 OpenConcerto, by ILM Informatique. All rights reserved.
|
5 |
*
|
5 |
*
|
6 |
* The contents of this file are subject to the terms of the GNU General Public License Version 3
|
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
|
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
|
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.
|
9 |
* language governing permissions and limitations under the License.
|
10 |
*
|
10 |
*
|
11 |
* When distributing the software, include this License Header Notice in each file.
|
11 |
* When distributing the software, include this License Header Notice in each file.
|
12 |
*/
|
12 |
*/
|
13 |
|
13 |
|
14 |
package org.openconcerto.utils;
|
14 |
package org.openconcerto.utils;
|
15 |
|
15 |
|
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.GeneralSecurityException;
|
22 |
import java.security.KeyFactory;
|
22 |
import java.security.KeyFactory;
|
23 |
import java.security.KeyStore;
|
23 |
import java.security.KeyStore;
|
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;
|
29 |
import java.security.interfaces.RSAPrivateKey;
|
29 |
import java.security.interfaces.RSAPrivateKey;
|
30 |
import java.security.spec.InvalidKeySpecException;
|
30 |
import java.security.spec.InvalidKeySpecException;
|
31 |
import java.security.spec.PKCS8EncodedKeySpec;
|
31 |
import java.security.spec.PKCS8EncodedKeySpec;
|
32 |
import java.util.ArrayList;
|
32 |
import java.util.ArrayList;
|
33 |
import java.util.List;
|
33 |
import java.util.List;
|
34 |
|
34 |
|
35 |
import javax.net.ssl.KeyManager;
|
35 |
import javax.net.ssl.KeyManager;
|
36 |
import javax.net.ssl.KeyManagerFactory;
|
36 |
import javax.net.ssl.KeyManagerFactory;
|
37 |
import javax.net.ssl.SSLContext;
|
37 |
import javax.net.ssl.SSLContext;
|
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 IOException, GeneralSecurityException {
|
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();
|
49 |
context.init(km, null, null);
|
49 |
context.init(km, null, null);
|
50 |
return context.getServerSocketFactory();
|
50 |
return context.getServerSocketFactory();
|
51 |
}
|
51 |
}
|
52 |
|
52 |
|
53 |
/**
|
53 |
/**
|
54 |
* Create a KeyStore from standard PEM files
|
54 |
* Create a KeyStore from standard PEM files
|
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) throws IOException, GeneralSecurityException {
|
60 |
public static KeyStore createKeyStore(File privateKeyPem, File certificatePem, final String password) throws IOException, GeneralSecurityException {
|
- |
|
61 |
if (!privateKeyPem.exists()) {
|
- |
|
62 |
throw new IllegalArgumentException("private key file missing : " + privateKeyPem.getAbsolutePath());
|
- |
|
63 |
}
|
- |
|
64 |
if (!certificatePem.exists()) {
|
- |
|
65 |
throw new IllegalArgumentException("certificate file missing : " + certificatePem.getAbsolutePath());
|
- |
|
66 |
}
|
61 |
final X509Certificate[] cert = createCertificates(certificatePem);
|
67 |
final X509Certificate[] cert = createCertificates(certificatePem);
|
62 |
final KeyStore keystore = KeyStore.getInstance("JKS");
|
68 |
final KeyStore keystore = KeyStore.getInstance("JKS");
|
63 |
keystore.load(null);
|
69 |
keystore.load(null);
|
64 |
// Import private key
|
70 |
// Import private key
|
65 |
final PrivateKey key = createPrivateKey(privateKeyPem);
|
71 |
final PrivateKey key = createPrivateKey(privateKeyPem);
|
66 |
keystore.setKeyEntry(privateKeyPem.getName(), key, password.toCharArray(), cert);
|
72 |
keystore.setKeyEntry(privateKeyPem.getName(), key, password.toCharArray(), cert);
|
67 |
return keystore;
|
73 |
return keystore;
|
68 |
}
|
74 |
}
|
69 |
|
75 |
|
70 |
private static PrivateKey createPrivateKey(File privateKeyPem) throws IOException, GeneralSecurityException {
|
76 |
private static PrivateKey createPrivateKey(File privateKeyPem) throws IOException, GeneralSecurityException {
|
71 |
final BufferedReader r = new BufferedReader(new FileReader(privateKeyPem));
|
77 |
final BufferedReader r = new BufferedReader(new FileReader(privateKeyPem));
|
72 |
String s = r.readLine();
|
78 |
String s = r.readLine();
|
73 |
if (s == null || !s.contains("BEGIN PRIVATE KEY")) {
|
79 |
if (s == null || !s.contains("BEGIN PRIVATE KEY")) {
|
74 |
r.close();
|
80 |
r.close();
|
75 |
throw new IllegalArgumentException("No PRIVATE KEY found");
|
81 |
throw new IllegalArgumentException("No PRIVATE KEY found");
|
76 |
}
|
82 |
}
|
77 |
final StringBuilder b = new StringBuilder();
|
83 |
final StringBuilder b = new StringBuilder();
|
78 |
s = "";
|
84 |
s = "";
|
79 |
while (s != null) {
|
85 |
while (s != null) {
|
80 |
if (s.contains("END PRIVATE KEY")) {
|
86 |
if (s.contains("END PRIVATE KEY")) {
|
81 |
break;
|
87 |
break;
|
82 |
}
|
88 |
}
|
83 |
b.append(s);
|
89 |
b.append(s);
|
84 |
s = r.readLine();
|
90 |
s = r.readLine();
|
85 |
}
|
91 |
}
|
86 |
r.close();
|
92 |
r.close();
|
87 |
final String hexString = b.toString();
|
93 |
final String hexString = b.toString();
|
88 |
final byte[] bytes = DatatypeConverter.parseBase64Binary(hexString);
|
94 |
final byte[] bytes = DatatypeConverter.parseBase64Binary(hexString);
|
89 |
return generatePrivateKeyFromDER(bytes);
|
95 |
return generatePrivateKeyFromDER(bytes);
|
90 |
}
|
96 |
}
|
91 |
|
97 |
|
92 |
private static X509Certificate[] createCertificates(File certificatePem) throws IOException, CertificateException {
|
98 |
private static X509Certificate[] createCertificates(File certificatePem) throws IOException, CertificateException {
|
93 |
final List<X509Certificate> result = new ArrayList<X509Certificate>();
|
99 |
final List<X509Certificate> result = new ArrayList<X509Certificate>();
|
94 |
final BufferedReader r = new BufferedReader(new FileReader(certificatePem));
|
100 |
final BufferedReader r = new BufferedReader(new FileReader(certificatePem));
|
95 |
String s = r.readLine();
|
101 |
String s = r.readLine();
|
96 |
if (s == null || !s.contains("BEGIN CERTIFICATE")) {
|
102 |
if (s == null || !s.contains("BEGIN CERTIFICATE")) {
|
97 |
r.close();
|
103 |
r.close();
|
98 |
throw new IllegalArgumentException("No CERTIFICATE found");
|
104 |
throw new IllegalArgumentException("No CERTIFICATE found");
|
99 |
}
|
105 |
}
|
100 |
StringBuilder b = new StringBuilder();
|
106 |
StringBuilder b = new StringBuilder();
|
101 |
while (s != null) {
|
107 |
while (s != null) {
|
102 |
if (s.contains("END CERTIFICATE")) {
|
108 |
if (s.contains("END CERTIFICATE")) {
|
103 |
String hexString = b.toString();
|
109 |
String hexString = b.toString();
|
104 |
final byte[] bytes = DatatypeConverter.parseBase64Binary(hexString);
|
110 |
final byte[] bytes = DatatypeConverter.parseBase64Binary(hexString);
|
105 |
X509Certificate cert = generateCertificateFromDER(bytes);
|
111 |
X509Certificate cert = generateCertificateFromDER(bytes);
|
106 |
result.add(cert);
|
112 |
result.add(cert);
|
107 |
b = new StringBuilder();
|
113 |
b = new StringBuilder();
|
108 |
} else {
|
114 |
} else {
|
109 |
if (!s.startsWith("----")) {
|
115 |
if (!s.startsWith("----")) {
|
110 |
b.append(s);
|
116 |
b.append(s);
|
111 |
}
|
117 |
}
|
112 |
}
|
118 |
}
|
113 |
s = r.readLine();
|
119 |
s = r.readLine();
|
114 |
}
|
120 |
}
|
115 |
r.close();
|
121 |
r.close();
|
116 |
|
122 |
|
117 |
return result.toArray(new X509Certificate[result.size()]);
|
123 |
return result.toArray(new X509Certificate[result.size()]);
|
118 |
}
|
124 |
}
|
119 |
|
125 |
|
120 |
private static RSAPrivateKey generatePrivateKeyFromDER(byte[] keyBytes) throws InvalidKeySpecException, NoSuchAlgorithmException {
|
126 |
private static RSAPrivateKey generatePrivateKeyFromDER(byte[] keyBytes) throws InvalidKeySpecException, NoSuchAlgorithmException {
|
121 |
final PKCS8EncodedKeySpec spec = new PKCS8EncodedKeySpec(keyBytes);
|
127 |
final PKCS8EncodedKeySpec spec = new PKCS8EncodedKeySpec(keyBytes);
|
122 |
final KeyFactory factory = KeyFactory.getInstance("RSA");
|
128 |
final KeyFactory factory = KeyFactory.getInstance("RSA");
|
123 |
return (RSAPrivateKey) factory.generatePrivate(spec);
|
129 |
return (RSAPrivateKey) factory.generatePrivate(spec);
|
124 |
}
|
130 |
}
|
125 |
|
131 |
|
126 |
private static X509Certificate generateCertificateFromDER(byte[] certBytes) throws CertificateException {
|
132 |
private static X509Certificate generateCertificateFromDER(byte[] certBytes) throws CertificateException {
|
127 |
final CertificateFactory factory = CertificateFactory.getInstance("X.509");
|
133 |
final CertificateFactory factory = CertificateFactory.getInstance("X.509");
|
128 |
return (X509Certificate) factory.generateCertificate(new ByteArrayInputStream(certBytes));
|
134 |
return (X509Certificate) factory.generateCertificate(new ByteArrayInputStream(certBytes));
|
129 |
}
|
135 |
}
|
130 |
|
136 |
|
131 |
}
|
137 |
}
|