Skip to content

Commit

Permalink
Merge pull request #62 from jentfoo/sslUtilsAcceptString
Browse files Browse the repository at this point in the history
SSLUtils: Accept String's in addition to File's
  • Loading branch information
jentfoo authored Jun 20, 2018
2 parents 32d1cd2 + 5f5d75c commit abc4505
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 10 deletions.
4 changes: 2 additions & 2 deletions gradle.properties
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
group = org.threadly
version = 4.5
threadlyVersion = 5.24
version = 4.6
threadlyVersion = 5.25
26 changes: 19 additions & 7 deletions src/main/java/org/threadly/litesockets/utils/SSLUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public class SSLUtils {
}

public static TrustManager[] getOpenTrustManager() {
return new TrustManager [] {new SSLUtils.FullTrustManager() };
return new TrustManager [] { new SSLUtils.FullTrustManager() };
}


Expand Down Expand Up @@ -98,7 +98,10 @@ public static String fileToString(File file, int max) throws IOException {
}

public static List<X509Certificate> getPEMFileCerts(File certFile) throws CertificateException, IOException {
String certString = fileToString(certFile, MAX_PEM_FILE_SIZE);
return getPEMCerts(fileToString(certFile, MAX_PEM_FILE_SIZE));
}

public static List<X509Certificate> getPEMCerts(String certString) throws CertificateException, IOException {
List<X509Certificate> certs = new ArrayList<X509Certificate>();
int certPos = certString.indexOf(PEM_CERT_START);
CertificateFactory factory = CertificateFactory.getInstance("X.509");
Expand All @@ -112,7 +115,10 @@ public static List<X509Certificate> getPEMFileCerts(File certFile) throws Certif
}

public static RSAPrivateKey getPEMFileKey(File keyFile) throws IOException, InvalidKeySpecException, NoSuchAlgorithmException {
String keyString = fileToString(keyFile, MAX_PEM_FILE_SIZE);
return getPEMKey(fileToString(keyFile, MAX_PEM_FILE_SIZE));
}

public static RSAPrivateKey getPEMKey(String keyString) throws IOException, InvalidKeySpecException, NoSuchAlgorithmException {
int keyPos = keyString.indexOf(PEM_KEY_START)+PEM_KEY_START.length();
int keyEnd = keyString.indexOf(PEM_KEY_END);
if(keyPos == -1 || keyEnd == -1) {
Expand All @@ -133,12 +139,18 @@ public static RSAPrivateKey getPEMFileKey(File keyFile) throws IOException, Inva
* @return a {@link KeyManagerFactory} using the provided cert and file.
* @throws KeyStoreException Thrown if there is any kind of error opening or parsing the PEM files.
*/
public static KeyManagerFactory generateKeyStoreFromPEM(File certFile, File keyFile) throws KeyStoreException{
char[] password = UUID.randomUUID().toString().toCharArray();
public static KeyManagerFactory generateKeyStoreFromPEM(File certFile, File keyFile) throws KeyStoreException {
try {
List<X509Certificate> certs = getPEMFileCerts(certFile);
RSAPrivateKey key = getPEMFileKey(keyFile);
return generateKeyStore(getPEMFileCerts(certFile), getPEMFileKey(keyFile));
} catch (CertificateException | InvalidKeySpecException | NoSuchAlgorithmException |
IOException e) {
throw new KeyStoreException(e);
}
}

public static KeyManagerFactory generateKeyStore(List<X509Certificate> certs, RSAPrivateKey key) throws KeyStoreException {
char[] password = UUID.randomUUID().toString().toCharArray();
try {
KeyStore keystore = KeyStore.getInstance("JKS");
keystore.load(null);
for(int i=0; i<certs.size(); i++) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ public void setConnectionTimeout(int timeout) {

@Override
public int getTimeout() {
return 0;
return 10;
}

@Override
Expand Down

0 comments on commit abc4505

Please sign in to comment.