mirror of
https://repo.open-autonomous-connection.org/UnlegitDqrk/CertificateGenerator.git
synced 2026-03-02 13:19:18 +00:00
first commit
This commit is contained in:
39
.gitignore
vendored
Normal file
39
.gitignore
vendored
Normal file
@@ -0,0 +1,39 @@
|
|||||||
|
target/
|
||||||
|
!.mvn/wrapper/maven-wrapper.jar
|
||||||
|
!**/src/main/**/target/
|
||||||
|
!**/src/test/**/target/
|
||||||
|
.kotlin
|
||||||
|
|
||||||
|
### IntelliJ IDEA ###
|
||||||
|
.idea/modules.xml
|
||||||
|
.idea/jarRepositories.xml
|
||||||
|
.idea/compiler.xml
|
||||||
|
.idea/libraries/
|
||||||
|
*.iws
|
||||||
|
*.iml
|
||||||
|
*.ipr
|
||||||
|
|
||||||
|
### Eclipse ###
|
||||||
|
.apt_generated
|
||||||
|
.classpath
|
||||||
|
.factorypath
|
||||||
|
.project
|
||||||
|
.settings
|
||||||
|
.springBeans
|
||||||
|
.sts4-cache
|
||||||
|
|
||||||
|
### NetBeans ###
|
||||||
|
/nbproject/private/
|
||||||
|
/nbbuild/
|
||||||
|
/dist/
|
||||||
|
/nbdist/
|
||||||
|
/.nb-gradle/
|
||||||
|
build/
|
||||||
|
!**/src/main/**/build/
|
||||||
|
!**/src/test/**/build/
|
||||||
|
|
||||||
|
### VS Code ###
|
||||||
|
.vscode/
|
||||||
|
|
||||||
|
### Mac OS ###
|
||||||
|
.DS_Store
|
||||||
10
.idea/.gitignore
generated
vendored
Normal file
10
.idea/.gitignore
generated
vendored
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
# Default ignored files
|
||||||
|
/shelf/
|
||||||
|
/workspace.xml
|
||||||
|
# Ignored default folder with query files
|
||||||
|
/queries/
|
||||||
|
# Datasource local storage ignored files
|
||||||
|
/dataSources/
|
||||||
|
/dataSources.local.xml
|
||||||
|
# Editor-based HTTP Client requests
|
||||||
|
/httpRequests/
|
||||||
6
.idea/copilot.data.migration.agent.xml
generated
Normal file
6
.idea/copilot.data.migration.agent.xml
generated
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="AgentMigrationStateService">
|
||||||
|
<option name="migrationStatus" value="COMPLETED" />
|
||||||
|
</component>
|
||||||
|
</project>
|
||||||
7
.idea/discord.xml
generated
Normal file
7
.idea/discord.xml
generated
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="DiscordProjectSettings">
|
||||||
|
<option name="show" value="ASK" />
|
||||||
|
<option name="description" value="" />
|
||||||
|
</component>
|
||||||
|
</project>
|
||||||
7
.idea/encodings.xml
generated
Normal file
7
.idea/encodings.xml
generated
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="Encoding">
|
||||||
|
<file url="file://$PROJECT_DIR$/src/main/java" charset="UTF-8" />
|
||||||
|
<file url="file://$PROJECT_DIR$/src/main/resources" charset="UTF-8" />
|
||||||
|
</component>
|
||||||
|
</project>
|
||||||
14
.idea/misc.xml
generated
Normal file
14
.idea/misc.xml
generated
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="ExternalStorageConfigurationManager" enabled="true" />
|
||||||
|
<component name="MavenProjectsManager">
|
||||||
|
<option name="originalFiles">
|
||||||
|
<list>
|
||||||
|
<option value="$PROJECT_DIR$/pom.xml" />
|
||||||
|
</list>
|
||||||
|
</option>
|
||||||
|
</component>
|
||||||
|
<component name="ProjectRootManager" version="2" languageLevel="JDK_23" default="true" project-jdk-name="23" project-jdk-type="JavaSDK">
|
||||||
|
<output url="file://$PROJECT_DIR$/out" />
|
||||||
|
</component>
|
||||||
|
</project>
|
||||||
6
.idea/vcs.xml
generated
Normal file
6
.idea/vcs.xml
generated
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="VcsDirectoryMappings">
|
||||||
|
<mapping directory="$PROJECT_DIR$" vcs="Git" />
|
||||||
|
</component>
|
||||||
|
</project>
|
||||||
29
pom.xml
Normal file
29
pom.xml
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||||
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
|
<groupId>dev.unlegitdqrk</groupId>
|
||||||
|
<artifactId>CertificateGenerator</artifactId>
|
||||||
|
<version>1.0-SNAPSHOT</version>
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<maven.compiler.source>23</maven.compiler.source>
|
||||||
|
<maven.compiler.target>23</maven.compiler.target>
|
||||||
|
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||||
|
</properties>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>dev.unlegitdqrk</groupId>
|
||||||
|
<artifactId>unlegitlibrary</artifactId>
|
||||||
|
<version>1.8.0</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.apache.commons</groupId>
|
||||||
|
<artifactId>commons-compress</artifactId>
|
||||||
|
<version>1.23.0</version>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
</project>
|
||||||
187
src/main/java/dev/unlegitdqrk/oac/certificategenerator/Main.java
Normal file
187
src/main/java/dev/unlegitdqrk/oac/certificategenerator/Main.java
Normal file
@@ -0,0 +1,187 @@
|
|||||||
|
package dev.unlegitdqrk.oac.certificategenerator;
|
||||||
|
|
||||||
|
import dev.unlegitdqrk.unlegitlibrary.argument.Argument;
|
||||||
|
import dev.unlegitdqrk.unlegitlibrary.argument.ArgumentParser;
|
||||||
|
import dev.unlegitdqrk.unlegitlibrary.argument.ArgumentRun;
|
||||||
|
import dev.unlegitdqrk.unlegitlibrary.network.utils.NetworkUtils;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.nio.file.Files;
|
||||||
|
import java.nio.file.Paths;
|
||||||
|
import java.nio.file.StandardOpenOption;
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
|
public class Main {
|
||||||
|
public static void main(String[] args) throws IOException {
|
||||||
|
Argument type = new Argument("--type", "The Certificate Type", true, true, false, List.of("ca", "server", "ins", "client"));
|
||||||
|
Argument ip = new Argument("--ip", "The target public IP", false, true, false, new ArrayList<>());
|
||||||
|
final String[] host = {NetworkUtils.getPublicIPAddress()};
|
||||||
|
|
||||||
|
ip.setRun(new ArgumentRun() {
|
||||||
|
@Override
|
||||||
|
public void onRun(Argument argument, Optional<String> optional) {
|
||||||
|
optional.ifPresent(s -> host[0] = s);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
type.setRun(new ArgumentRun() {
|
||||||
|
@Override
|
||||||
|
public void onRun(Argument argument, Optional<String> optional) {
|
||||||
|
try {
|
||||||
|
if (optional.isEmpty()) {
|
||||||
|
System.err.println("Missing required argument: type");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
ensureOpenSslAvailable();
|
||||||
|
|
||||||
|
String selected = optional.get();
|
||||||
|
final String caKey = "certificates/private/ca/ca_ins_" + host[0] + ".key";
|
||||||
|
final String caPem = "certificates/public/ca/ca_ins_" + host[0] + ".pem";
|
||||||
|
|
||||||
|
if (selected.equalsIgnoreCase("ca")) {
|
||||||
|
ensureDirectories();
|
||||||
|
executeProcess("genrsa", "-out", caKey, "4096");
|
||||||
|
executeProcess(
|
||||||
|
"req", "-x509", "-new",
|
||||||
|
"-key", caKey,
|
||||||
|
"-sha256",
|
||||||
|
"-days", "365",
|
||||||
|
"-out", caPem,
|
||||||
|
"-addext", "basicConstraints=critical,CA:TRUE",
|
||||||
|
"-addext", "keyUsage=critical,keyCertSign,cRLSign"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (selected.equalsIgnoreCase("ins")) {
|
||||||
|
if (!Files.exists(Paths.get(caKey)) || !Files.exists(Paths.get(caPem))) {
|
||||||
|
System.err.println("CA files not found. Generate CA first with --type ca.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
String certKey = "certificates/private/server/cert_ins_" + host[0] + ".key";
|
||||||
|
String certCrt = "certificates/public/server/cert_ins_" + host[0] + ".crt";
|
||||||
|
String certCsr = "certificates/public/server/cert_ins_" + host[0] + ".csr";
|
||||||
|
|
||||||
|
ensureDirectories();
|
||||||
|
executeProcess("genrsa", "-out", certKey, "2048");
|
||||||
|
executeProcess("req", "-new", "-key", certKey, "-out", certCsr);
|
||||||
|
executeProcess(
|
||||||
|
"x509", "-req",
|
||||||
|
"-in", certCsr,
|
||||||
|
"-CA", caPem,
|
||||||
|
"-CAkey", caKey,
|
||||||
|
"-CAcreateserial",
|
||||||
|
"-out", certCrt,
|
||||||
|
"-days", "365",
|
||||||
|
"-sha256"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (selected.equalsIgnoreCase("server")) {
|
||||||
|
if (!Files.exists(Paths.get(caKey)) || !Files.exists(Paths.get(caPem))) {
|
||||||
|
System.err.println("CA files not found. Generate CA first with --type ca.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
String certKey = "certificates/private/server/cert_server_" + host[0] + ".key";
|
||||||
|
String certCrt = "certificates/public/server/cert_server_" + host[0] + ".crt";
|
||||||
|
String certCsr = "certificates/public/server/cert_server_" + host[0] + ".csr";
|
||||||
|
|
||||||
|
ensureDirectories();
|
||||||
|
executeProcess("genrsa", "-out", certKey, "2048");
|
||||||
|
executeProcess("req", "-new", "-key", certKey, "-out", certCsr);
|
||||||
|
executeProcess(
|
||||||
|
"x509", "-req",
|
||||||
|
"-in", certCsr,
|
||||||
|
"-CA", caPem,
|
||||||
|
"-CAkey", caKey,
|
||||||
|
"-CAcreateserial",
|
||||||
|
"-out", certCrt,
|
||||||
|
"-days", "365",
|
||||||
|
"-sha256"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (selected.equalsIgnoreCase("client")) {
|
||||||
|
if (!Files.exists(Paths.get(caKey)) || !Files.exists(Paths.get(caPem))) {
|
||||||
|
System.err.println("CA files not found. Generate CA first with --type ca.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
String certKey = "certificates/private/client/cert_client_" + host[0] + ".key";
|
||||||
|
String certCrt = "certificates/public/client/cert_client_" + host[0] + ".crt";
|
||||||
|
String certCsr = "certificates/public/client/cert_client_" + host[0] + ".csr";
|
||||||
|
|
||||||
|
ensureClientDirectories();
|
||||||
|
executeProcess("genrsa", "-out", certKey, "2048");
|
||||||
|
executeProcess("req", "-new", "-key", certKey, "-out", certCsr);
|
||||||
|
executeProcess(
|
||||||
|
"x509", "-req",
|
||||||
|
"-in", certCsr,
|
||||||
|
"-CA", caPem,
|
||||||
|
"-CAkey", caKey,
|
||||||
|
"-CAcreateserial",
|
||||||
|
"-out", certCrt,
|
||||||
|
"-days", "365",
|
||||||
|
"-sha256"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
ArgumentParser parser = new ArgumentParser(args);
|
||||||
|
parser.registerArgument(ip);
|
||||||
|
parser.registerArgument(type);
|
||||||
|
parser.runArguments();
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void ensureOpenSslAvailable() throws IOException {
|
||||||
|
try {
|
||||||
|
Process prc = new ProcessBuilder("openssl", "version").inheritIO().start();
|
||||||
|
int code = prc.waitFor();
|
||||||
|
if (code != 0) throw new IOException("OpenSSL not available on PATH");
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
Thread.currentThread().interrupt();
|
||||||
|
throw new IOException("OpenSSL check interrupted", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void ensureClientDirectories() throws IOException {
|
||||||
|
Files.createDirectories(Paths.get("certificates", "private", "ca"));
|
||||||
|
Files.createDirectories(Paths.get("certificates", "private", "client"));
|
||||||
|
Files.createDirectories(Paths.get("certificates", "public", "ca"));
|
||||||
|
Files.createDirectories(Paths.get("certificates", "public", "client"));
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void ensureDirectories() throws IOException {
|
||||||
|
Files.createDirectories(Paths.get("certificates", "private", "ca"));
|
||||||
|
Files.createDirectories(Paths.get("certificates", "private", "server"));
|
||||||
|
Files.createDirectories(Paths.get("certificates", "public", "ca"));
|
||||||
|
Files.createDirectories(Paths.get("certificates", "public", "server"));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private static boolean isIpAddress(String host) {
|
||||||
|
if (host == null || host.isEmpty()) return false;
|
||||||
|
if (host.contains(":")) return true;
|
||||||
|
return host.matches("\\d{1,3}(\\.\\d{1,3}){3}");
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void executeProcess(String... args) throws IOException {
|
||||||
|
List<String> cmd = new ArrayList<>();
|
||||||
|
cmd.add("openssl");
|
||||||
|
cmd.addAll(List.of(args));
|
||||||
|
Process prc = new ProcessBuilder(cmd).inheritIO().start();
|
||||||
|
try {
|
||||||
|
int code = prc.waitFor();
|
||||||
|
if (code != 0) throw new IOException("OpenSSL failed with exit code " + code);
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
Thread.currentThread().interrupt();
|
||||||
|
throw new IOException("OpenSSL interrupted", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user