From 62942955588d2184d1089110e50f76ee9f0ccb67 Mon Sep 17 00:00:00 2001 From: UnlegitDqrk Date: Mon, 2 Feb 2026 22:21:07 +0100 Subject: [PATCH] first commit --- .gitignore | 39 ++++ .idea/.gitignore | 10 + .idea/copilot.data.migration.agent.xml | 6 + .idea/discord.xml | 7 + .idea/encodings.xml | 7 + .idea/misc.xml | 14 ++ .idea/vcs.xml | 6 + pom.xml | 29 +++ .../oac/certificategenerator/Main.java | 187 ++++++++++++++++++ 9 files changed, 305 insertions(+) create mode 100644 .gitignore create mode 100644 .idea/.gitignore create mode 100644 .idea/copilot.data.migration.agent.xml create mode 100644 .idea/discord.xml create mode 100644 .idea/encodings.xml create mode 100644 .idea/misc.xml create mode 100644 .idea/vcs.xml create mode 100644 pom.xml create mode 100644 src/main/java/dev/unlegitdqrk/oac/certificategenerator/Main.java diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..480bdf5 --- /dev/null +++ b/.gitignore @@ -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 \ No newline at end of file diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..ab1f416 --- /dev/null +++ b/.idea/.gitignore @@ -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/ diff --git a/.idea/copilot.data.migration.agent.xml b/.idea/copilot.data.migration.agent.xml new file mode 100644 index 0000000..4ea72a9 --- /dev/null +++ b/.idea/copilot.data.migration.agent.xml @@ -0,0 +1,6 @@ + + + + + \ No newline at end of file diff --git a/.idea/discord.xml b/.idea/discord.xml new file mode 100644 index 0000000..30bab2a --- /dev/null +++ b/.idea/discord.xml @@ -0,0 +1,7 @@ + + + + + \ No newline at end of file diff --git a/.idea/encodings.xml b/.idea/encodings.xml new file mode 100644 index 0000000..aa00ffa --- /dev/null +++ b/.idea/encodings.xml @@ -0,0 +1,7 @@ + + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..001e756 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,14 @@ + + + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..e58e820 --- /dev/null +++ b/pom.xml @@ -0,0 +1,29 @@ + + + 4.0.0 + + dev.unlegitdqrk + CertificateGenerator + 1.0-SNAPSHOT + + + 23 + 23 + UTF-8 + + + + + dev.unlegitdqrk + unlegitlibrary + 1.8.0 + + + org.apache.commons + commons-compress + 1.23.0 + + + \ No newline at end of file diff --git a/src/main/java/dev/unlegitdqrk/oac/certificategenerator/Main.java b/src/main/java/dev/unlegitdqrk/oac/certificategenerator/Main.java new file mode 100644 index 0000000..4204bf9 --- /dev/null +++ b/src/main/java/dev/unlegitdqrk/oac/certificategenerator/Main.java @@ -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 optional) { + optional.ifPresent(s -> host[0] = s); + } + }); + + type.setRun(new ArgumentRun() { + @Override + public void onRun(Argument argument, Optional 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 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); + } + } +}