forked from scalepoint-tech/oauth-token-java-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TestCertificateHelper.java
34 lines (30 loc) · 1.56 KB
/
TestCertificateHelper.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
package com.scalepoint.oauth_token_client;
import java.io.FileInputStream;
import java.io.IOException;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.UnrecoverableKeyException;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.security.interfaces.RSAPrivateKey;
import java.util.Properties;
class TestCertificateHelper {
public static CertificateWithPrivateKey load() {
try {
Properties config = new Properties();
config.load(new FileInputStream("config.properties"));
return getKeyPair(config.getProperty("keyStoreFileName"), config.getProperty("keyStorePassword"));
} catch (Exception e) {
throw new RuntimeException(e);
}
}
private static CertificateWithPrivateKey getKeyPair(String keyStoreFileName, String keyStorePassword) throws KeyStoreException, IOException, UnrecoverableKeyException, NoSuchAlgorithmException, CertificateException {
KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType()); // or KeyStore.getInstance("pkcs12", "SunJSSE") to load .pfx
keyStore.load(new FileInputStream(keyStoreFileName), null);
String a = keyStore.aliases().nextElement();
RSAPrivateKey privateKey = (RSAPrivateKey) keyStore.getKey(a, keyStorePassword.toCharArray());
X509Certificate certificate = (X509Certificate) keyStore.getCertificate(a);
return new CertificateWithPrivateKey(privateKey, certificate);
}
}