From c85350821ee338cabd0e32d7691ed6d6aa93646d Mon Sep 17 00:00:00 2001 From: Gravit Date: Mon, 1 Jul 2019 22:47:07 +0700 Subject: [PATCH] =?UTF-8?q?[FEATURE]=20=D0=AD=D0=BB=D0=BB=D0=B8=D0=BF?= =?UTF-8?q?=D1=82=D0=B8=D1=87=D0=B5=D1=81=D0=BA=D0=B0=D1=8F=20=D0=BA=D1=80?= =?UTF-8?q?=D0=B8=D0=BF=D1=82=D0=BE=D0=B3=D1=80=D0=B0=D1=84=D0=B8=D1=8F.?= =?UTF-8?q?=20=D0=A2=D0=B5=D1=81=D1=82=D0=BE=D0=B2=D1=8B=D0=B5=20=D0=BA?= =?UTF-8?q?=D0=BE=D0=BC=D0=B0=D0=BD=D0=B4=D1=8B,=20=D0=B8=D1=81=D0=BF?= =?UTF-8?q?=D1=80=D0=B0=D0=B2=D0=BB=D0=B5=D0=BD=D0=B8=D0=B5=20=D0=BE=D1=88?= =?UTF-8?q?=D0=B8=D0=B1=D0=BE=D0=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../command/basic/TestCommand.java | 18 ++++++++++++++++ .../manangers/CertificateManager.java | 21 +++++++++++++++++-- 2 files changed, 37 insertions(+), 2 deletions(-) diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/command/basic/TestCommand.java b/LaunchServer/src/main/java/pro/gravit/launchserver/command/basic/TestCommand.java index a9d37cc7..c5deec73 100644 --- a/LaunchServer/src/main/java/pro/gravit/launchserver/command/basic/TestCommand.java +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/command/basic/TestCommand.java @@ -1,11 +1,16 @@ package pro.gravit.launchserver.command.basic; +import org.bouncycastle.cert.X509CertificateHolder; import pro.gravit.launcher.events.PingEvent; import pro.gravit.launchserver.LaunchServer; import pro.gravit.launchserver.command.Command; import pro.gravit.launchserver.socket.handlers.NettyServerSocketHandler; import pro.gravit.utils.helper.CommonHelper; +import java.nio.file.Paths; +import java.security.KeyPair; +import java.security.KeyPairGenerator; + public class TestCommand extends Command { public TestCommand(LaunchServer server) { super(server); @@ -34,5 +39,18 @@ public void invoke(String... args) throws Exception { if (args[0].equals("stop")) { handler.close(); } + if(args[0].equals("genCA")) { + server.certificateManager.generateCA(); + server.certificateManager.writePrivateKey(Paths.get("ca.key"), server.certificateManager.caKey); + server.certificateManager.writeCertificate(Paths.get("ca.crt"), server.certificateManager.ca); + } + if(args[0].equals("genCert")) { + verifyArgs(args, 2); + String name = args[1]; + KeyPair pair = server.certificateManager.generateKeyPair(); + X509CertificateHolder cert = server.certificateManager.generateCertificate(name, pair.getPublic()); + server.certificateManager.writePrivateKey(Paths.get(name.concat(".key")), pair.getPrivate()); + server.certificateManager.writeCertificate(Paths.get(name.concat(".crt")), cert); + } } } diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/manangers/CertificateManager.java b/LaunchServer/src/main/java/pro/gravit/launchserver/manangers/CertificateManager.java index f7e10deb..cae36ff7 100644 --- a/LaunchServer/src/main/java/pro/gravit/launchserver/manangers/CertificateManager.java +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/manangers/CertificateManager.java @@ -1,6 +1,7 @@ package pro.gravit.launchserver.manangers; +import org.bouncycastle.asn1.pkcs.PrivateKeyInfo; import org.bouncycastle.asn1.x500.X500Name; import org.bouncycastle.asn1.x500.X500NameBuilder; import org.bouncycastle.asn1.x500.style.BCStyle; @@ -12,6 +13,7 @@ import org.bouncycastle.crypto.params.AsymmetricKeyParameter; import org.bouncycastle.crypto.params.ECKeyParameters; import org.bouncycastle.crypto.util.PrivateKeyFactory; +import org.bouncycastle.crypto.util.PrivateKeyInfoFactory; import org.bouncycastle.jce.ECNamedCurveTable; import org.bouncycastle.jce.spec.ECParameterSpec; import org.bouncycastle.openssl.PEMWriter; @@ -46,11 +48,11 @@ public class CertificateManager { //public X509CertificateHolder server; //public AsymmetricKeyParameter serverKey; - public int validDays = 0; + public int validDays = 60; public int minusHours = 6; public X509CertificateHolder generateCertificate(String subjectName, PublicKey subjectPublicKey) throws OperatorCreationException { - SubjectPublicKeyInfo subjectPubKeyInfo = SubjectPublicKeyInfo.getInstance(subjectPublicKey); + SubjectPublicKeyInfo subjectPubKeyInfo = SubjectPublicKeyInfo.getInstance(subjectPublicKey.getEncoded()); BigInteger serial = BigInteger.valueOf(SecurityHelper.newRandom().nextLong()); Date startDate = Date.from(Instant.now().minus(minusHours, ChronoUnit.HOURS)); Date endDate = Date.from(startDate.toInstant().plus(validDays, ChronoUnit.DAYS)); @@ -87,11 +89,26 @@ public void generateCA() throws NoSuchAlgorithmException, IOException, OperatorC caKey = PrivateKeyFactory.createKey(pair.getPrivate().getEncoded()); } + public KeyPair generateKeyPair() throws InvalidAlgorithmParameterException, NoSuchAlgorithmException { + ECGenParameterSpec ecGenSpec = new ECGenParameterSpec("secp384r1"); + KeyPairGenerator generator = KeyPairGenerator.getInstance("EC"); + generator.initialize(ecGenSpec, SecurityHelper.newRandom()); + return generator.generateKeyPair(); + } + public void writePrivateKey(Path file, PrivateKey privateKey) throws IOException { try (PemWriter writer = new PemWriter(IOHelper.newWriter(file))) { writer.writeObject(new PemObject("PRIVATE KEY", privateKey.getEncoded())); } } + + public void writePrivateKey(Path file, AsymmetricKeyParameter key) throws IOException { + PrivateKeyInfo info = PrivateKeyInfoFactory.createPrivateKeyInfo(key); + try (PemWriter writer = new PemWriter(IOHelper.newWriter(file))) { + writer.writeObject(new PemObject("PRIVATE KEY", info.getEncoded())); + } + } + public void writeCertificate(Path file, X509CertificateHolder holder) throws IOException { try (PemWriter writer = new PemWriter(IOHelper.newWriter(file))) { writer.writeObject(new PemObject("CERTIFICATE", holder.toASN1Structure().getEncoded()));