From 5a889e047161ef0dba183098972304b7babe0b87 Mon Sep 17 00:00:00 2001 From: Gravit Date: Thu, 11 Jul 2019 01:45:05 +0700 Subject: [PATCH] =?UTF-8?q?[FEATURE]=20=D0=A7=D1=82=D0=B5=D0=BD=D0=B8?= =?UTF-8?q?=D0=B5=20=D1=81=D0=B5=D1=80=D1=82=D0=B8=D1=84=D0=B8=D0=BA=D0=B0?= =?UTF-8?q?=D1=82=D0=BE=D0=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../command/basic/TestCommand.java | 4 +++ .../manangers/CertificateManager.java | 33 +++++++++++++++---- 2 files changed, 31 insertions(+), 6 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 c5deec73..be1c528a 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 @@ -44,6 +44,10 @@ public void invoke(String... args) throws Exception { server.certificateManager.writePrivateKey(Paths.get("ca.key"), server.certificateManager.caKey); server.certificateManager.writeCertificate(Paths.get("ca.crt"), server.certificateManager.ca); } + if(args[0].equals("readCA")) { + server.certificateManager.ca = server.certificateManager.readCertificate(Paths.get("ca.crt")); + server.certificateManager.caKey = server.certificateManager.readPrivateKey(Paths.get("ca.key")); + } if(args[0].equals("genCert")) { verifyArgs(args, 2); String name = args[1]; 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 cae36ff7..f2a15df4 100644 --- a/LaunchServer/src/main/java/pro/gravit/launchserver/manangers/CertificateManager.java +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/manangers/CertificateManager.java @@ -7,32 +7,29 @@ import org.bouncycastle.asn1.x500.style.BCStyle; import org.bouncycastle.asn1.x509.AlgorithmIdentifier; import org.bouncycastle.asn1.x509.SubjectPublicKeyInfo; -import org.bouncycastle.cert.CertIOException; import org.bouncycastle.cert.X509CertificateHolder; import org.bouncycastle.cert.X509v3CertificateBuilder; 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; import org.bouncycastle.operator.ContentSigner; import org.bouncycastle.operator.DefaultDigestAlgorithmIdentifierFinder; import org.bouncycastle.operator.OperatorCreationException; import org.bouncycastle.operator.bc.BcECContentSignerBuilder; import org.bouncycastle.operator.jcajce.JcaContentSignerBuilder; import org.bouncycastle.util.io.pem.PemObject; +import org.bouncycastle.util.io.pem.PemReader; import org.bouncycastle.util.io.pem.PemWriter; import pro.gravit.utils.helper.IOHelper; import pro.gravit.utils.helper.SecurityHelper; -import java.io.FileWriter; +import java.io.ByteArrayInputStream; import java.io.IOException; import java.math.BigInteger; import java.nio.file.Path; import java.security.*; import java.security.cert.CertificateException; +import java.security.cert.CertificateFactory; import java.security.spec.ECGenParameterSpec; import java.time.Instant; import java.time.LocalDate; @@ -114,4 +111,28 @@ public void writeCertificate(Path file, X509CertificateHolder holder) throws IOE writer.writeObject(new PemObject("CERTIFICATE", holder.toASN1Structure().getEncoded())); } } + + public AsymmetricKeyParameter readPrivateKey(Path file) throws IOException { + AsymmetricKeyParameter ret; + try(PemReader reader = new PemReader(IOHelper.newReader(file))) + { + byte[] bytes = reader.readPemObject().getContent(); + try(ByteArrayInputStream inputStream = new ByteArrayInputStream(bytes)) + { + + ret = PrivateKeyFactory.createKey(inputStream); + } + } + return ret; + } + + public X509CertificateHolder readCertificate(Path file) throws IOException { + X509CertificateHolder ret; + try(PemReader reader = new PemReader(IOHelper.newReader(file))) + { + byte[] bytes = reader.readPemObject().getContent(); + ret = new X509CertificateHolder(bytes); + } + return ret; + } }