Line 1... |
Line 1... |
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-2019 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.
|
Line 11... |
Line 11... |
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 static java.util.Base64.getMimeDecoder;
|
- |
|
17 |
|
16 |
import java.io.BufferedReader;
|
18 |
import java.io.BufferedReader;
|
17 |
import java.io.ByteArrayInputStream;
|
19 |
import java.io.ByteArrayInputStream;
|
18 |
import java.io.File;
|
20 |
import java.io.File;
|
19 |
import java.io.FileReader;
|
21 |
import java.io.FileReader;
|
20 |
import java.io.IOException;
|
22 |
import java.io.IOException;
|
Line 34... |
Line 36... |
34 |
|
36 |
|
35 |
import javax.net.ssl.KeyManager;
|
37 |
import javax.net.ssl.KeyManager;
|
36 |
import javax.net.ssl.KeyManagerFactory;
|
38 |
import javax.net.ssl.KeyManagerFactory;
|
37 |
import javax.net.ssl.SSLContext;
|
39 |
import javax.net.ssl.SSLContext;
|
38 |
import javax.net.ssl.SSLServerSocketFactory;
|
40 |
import javax.net.ssl.SSLServerSocketFactory;
|
39 |
import javax.xml.bind.DatatypeConverter;
|
- |
|
40 |
|
41 |
|
41 |
public class PEMImporter {
|
42 |
public class PEMImporter {
|
42 |
|
43 |
|
43 |
public static SSLServerSocketFactory createSSLFactory(File privateKeyPem, File certificatePem, String password) throws IOException, GeneralSecurityException {
|
44 |
public static SSLServerSocketFactory createSSLFactory(File privateKeyPem, File certificatePem, String password) throws IOException, GeneralSecurityException {
|
44 |
final SSLContext context = SSLContext.getInstance("TLS");
|
45 |
final SSLContext context = SSLContext.getInstance("TLS");
|
Line 89... |
Line 90... |
89 |
b.append(s);
|
90 |
b.append(s);
|
90 |
s = r.readLine();
|
91 |
s = r.readLine();
|
91 |
}
|
92 |
}
|
92 |
r.close();
|
93 |
r.close();
|
93 |
final String hexString = b.toString();
|
94 |
final String hexString = b.toString();
|
94 |
final byte[] bytes = DatatypeConverter.parseBase64Binary(hexString);
|
95 |
final byte[] bytes = getMimeDecoder().decode(hexString);
|
95 |
return generatePrivateKeyFromDER(bytes);
|
96 |
return generatePrivateKeyFromDER(bytes);
|
96 |
}
|
97 |
}
|
97 |
|
98 |
|
98 |
private static X509Certificate[] createCertificates(File certificatePem) throws IOException, CertificateException {
|
99 |
private static X509Certificate[] createCertificates(File certificatePem) throws IOException, CertificateException {
|
99 |
final List<X509Certificate> result = new ArrayList<X509Certificate>();
|
100 |
final List<X509Certificate> result = new ArrayList<X509Certificate>();
|
Line 105... |
Line 106... |
105 |
}
|
106 |
}
|
106 |
StringBuilder b = new StringBuilder();
|
107 |
StringBuilder b = new StringBuilder();
|
107 |
while (s != null) {
|
108 |
while (s != null) {
|
108 |
if (s.contains("END CERTIFICATE")) {
|
109 |
if (s.contains("END CERTIFICATE")) {
|
109 |
String hexString = b.toString();
|
110 |
String hexString = b.toString();
|
110 |
final byte[] bytes = DatatypeConverter.parseBase64Binary(hexString);
|
111 |
final byte[] bytes = getMimeDecoder().decode(hexString);
|
111 |
X509Certificate cert = generateCertificateFromDER(bytes);
|
112 |
X509Certificate cert = generateCertificateFromDER(bytes);
|
112 |
result.add(cert);
|
113 |
result.add(cert);
|
113 |
b = new StringBuilder();
|
114 |
b = new StringBuilder();
|
114 |
} else {
|
115 |
} else {
|
115 |
if (!s.startsWith("----")) {
|
116 |
if (!s.startsWith("----")) {
|