Skip to content

Commit abc4505

Browse files
authored
Merge pull request #62 from jentfoo/sslUtilsAcceptString
SSLUtils: Accept String's in addition to File's
2 parents 32d1cd2 + 5f5d75c commit abc4505

3 files changed

Lines changed: 22 additions & 10 deletions

File tree

gradle.properties

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
group = org.threadly
2-
version = 4.5
3-
threadlyVersion = 5.24
2+
version = 4.6
3+
threadlyVersion = 5.25

src/main/java/org/threadly/litesockets/utils/SSLUtils.java

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -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++) {

src/test/java/org/threadly/litesockets/SSLProcessorTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ public void setConnectionTimeout(int timeout) {
262262

263263
@Override
264264
public int getTimeout() {
265-
return 0;
265+
return 10;
266266
}
267267

268268
@Override

0 commit comments

Comments
 (0)