Generates keys now!
This commit is contained in:
parent
ef9eba123b
commit
72363fe3b7
1
.gitignore
vendored
1
.gitignore
vendored
@ -152,3 +152,4 @@ fabric.properties
|
|||||||
# Android studio 3.1+ serialized cache file
|
# Android studio 3.1+ serialized cache file
|
||||||
.idea/caches/build_file_checksums.ser
|
.idea/caches/build_file_checksums.ser
|
||||||
|
|
||||||
|
certificates/
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8"?>
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
<project version="4">
|
<project version="4">
|
||||||
|
<component name="GradleMigrationSettings" migrationVersion="1" />
|
||||||
<component name="GradleSettings">
|
<component name="GradleSettings">
|
||||||
<option name="linkedExternalProjectsSettings">
|
<option name="linkedExternalProjectsSettings">
|
||||||
<GradleProjectSettings>
|
<GradleProjectSettings>
|
||||||
|
@ -2,6 +2,6 @@ package tech.nevets.sfss;
|
|||||||
|
|
||||||
public class Main {
|
public class Main {
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
|
StoreManager.generateCertificates();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
64
src/main/java/tech/nevets/sfss/StoreManager.java
Normal file
64
src/main/java/tech/nevets/sfss/StoreManager.java
Normal file
@ -0,0 +1,64 @@
|
|||||||
|
package tech.nevets.sfss;
|
||||||
|
|
||||||
|
import javax.net.ssl.SSLContext;
|
||||||
|
import javax.net.ssl.TrustManagerFactory;
|
||||||
|
import java.io.*;
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
|
import java.security.KeyStore;
|
||||||
|
import java.security.cert.CertificateFactory;
|
||||||
|
import java.security.cert.X509Certificate;
|
||||||
|
|
||||||
|
public class StoreManager {
|
||||||
|
public static final File PUBLIC_KEY = new File("./certificates/public.cer");
|
||||||
|
public static final File PRIVATE_KEY = new File("./certificates/private.key");
|
||||||
|
|
||||||
|
public static void generateCertificates() {
|
||||||
|
String[][] cmds = {
|
||||||
|
{"openssl", "genrsa", "-out", "./certificates/private.key", "2048"},
|
||||||
|
{"openssl", "req", "-new", "-x509", "-subj", "/C=US/ST=Nope/L=Haha/O=IncInc/CN=www.example.com", "-key", "./certificates/private.key", "-out", "./certificates/public.cer", "-days", "365"}
|
||||||
|
};
|
||||||
|
new File("./certificates").mkdirs();
|
||||||
|
for (String[] cmd : cmds) {
|
||||||
|
Process ps;
|
||||||
|
try {
|
||||||
|
ps = Runtime.getRuntime().exec(cmd);
|
||||||
|
int exitCode = ps.waitFor(); // Wait for the process to finish
|
||||||
|
if (exitCode != 0) {
|
||||||
|
System.err.println("Error: Process exited with code " + exitCode);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
return;
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
Thread.currentThread().interrupt();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void loadCertificates() {
|
||||||
|
CertificateFactory certFactory = CertificateFactory.getInstance("X.509");
|
||||||
|
|
||||||
|
InputStream publicKeyStream = new FileInputStream(PUBLIC_KEY);
|
||||||
|
X509Certificate publicKey = (X509Certificate) certFactory.generateCertificate(publicKeyStream);
|
||||||
|
|
||||||
|
InputStream privateKeyStream = new FileInputStream(PRIVATE_KEY);
|
||||||
|
X509Certificate privateKey = (X509Certificate) certFactory.generateCertificate(privateKeyStream);
|
||||||
|
|
||||||
|
KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType());
|
||||||
|
keyStore.load(null);
|
||||||
|
keyStore.setCertificateEntry("privateKey", privateKey);
|
||||||
|
|
||||||
|
TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
|
||||||
|
tmf.init(keyStore);
|
||||||
|
|
||||||
|
SSLContext sslContext = SSLContext.getInstance("TLS");
|
||||||
|
sslContext.init(null, tmf.getTrustManagers(), null);
|
||||||
|
|
||||||
|
|
||||||
|
KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
|
||||||
|
trustStore.load(publicKey, "".toCharArray());
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user