@@ -52,7 +52,7 @@ public class SSLUtils {
5252 }
5353
5454 public static TrustManager [] getOpenTrustManager () {
55- return new TrustManager [] {new SSLUtils .FullTrustManager () };
55+ return new TrustManager [] { new SSLUtils .FullTrustManager () };
5656 }
5757
5858
@@ -98,7 +98,10 @@ public static String fileToString(File file, int max) throws IOException {
9898 }
9999
100100 public static List <X509Certificate > getPEMFileCerts (File certFile ) throws CertificateException , IOException {
101- String certString = fileToString (certFile , MAX_PEM_FILE_SIZE );
101+ return getPEMCerts (fileToString (certFile , MAX_PEM_FILE_SIZE ));
102+ }
103+
104+ public static List <X509Certificate > getPEMCerts (String certString ) throws CertificateException , IOException {
102105 List <X509Certificate > certs = new ArrayList <X509Certificate >();
103106 int certPos = certString .indexOf (PEM_CERT_START );
104107 CertificateFactory factory = CertificateFactory .getInstance ("X.509" );
@@ -112,7 +115,10 @@ public static List<X509Certificate> getPEMFileCerts(File certFile) throws Certif
112115 }
113116
114117 public static RSAPrivateKey getPEMFileKey (File keyFile ) throws IOException , InvalidKeySpecException , NoSuchAlgorithmException {
115- String keyString = fileToString (keyFile , MAX_PEM_FILE_SIZE );
118+ return getPEMKey (fileToString (keyFile , MAX_PEM_FILE_SIZE ));
119+ }
120+
121+ public static RSAPrivateKey getPEMKey (String keyString ) throws IOException , InvalidKeySpecException , NoSuchAlgorithmException {
116122 int keyPos = keyString .indexOf (PEM_KEY_START )+PEM_KEY_START .length ();
117123 int keyEnd = keyString .indexOf (PEM_KEY_END );
118124 if (keyPos == -1 || keyEnd == -1 ) {
@@ -133,12 +139,18 @@ public static RSAPrivateKey getPEMFileKey(File keyFile) throws IOException, Inva
133139 * @return a {@link KeyManagerFactory} using the provided cert and file.
134140 * @throws KeyStoreException Thrown if there is any kind of error opening or parsing the PEM files.
135141 */
136- public static KeyManagerFactory generateKeyStoreFromPEM (File certFile , File keyFile ) throws KeyStoreException {
137- char [] password = UUID .randomUUID ().toString ().toCharArray ();
142+ public static KeyManagerFactory generateKeyStoreFromPEM (File certFile , File keyFile ) throws KeyStoreException {
138143 try {
139- List <X509Certificate > certs = getPEMFileCerts (certFile );
140- RSAPrivateKey key = getPEMFileKey (keyFile );
144+ return generateKeyStore (getPEMFileCerts (certFile ), getPEMFileKey (keyFile ));
145+ } catch (CertificateException | InvalidKeySpecException | NoSuchAlgorithmException |
146+ IOException e ) {
147+ throw new KeyStoreException (e );
148+ }
149+ }
141150
151+ public static KeyManagerFactory generateKeyStore (List <X509Certificate > certs , RSAPrivateKey key ) throws KeyStoreException {
152+ char [] password = UUID .randomUUID ().toString ().toCharArray ();
153+ try {
142154 KeyStore keystore = KeyStore .getInstance ("JKS" );
143155 keystore .load (null );
144156 for (int i =0 ; i <certs .size (); i ++) {
0 commit comments