mirror of
https://github.com/GravitLauncher/Launcher
synced 2024-11-15 11:39:11 +03:00
[FEATURE] Эллиптическая криптография. Тестовые команды, исправление ошибок
This commit is contained in:
parent
aebb96b32e
commit
c85350821e
2 changed files with 37 additions and 2 deletions
|
@ -1,11 +1,16 @@
|
||||||
package pro.gravit.launchserver.command.basic;
|
package pro.gravit.launchserver.command.basic;
|
||||||
|
|
||||||
|
import org.bouncycastle.cert.X509CertificateHolder;
|
||||||
import pro.gravit.launcher.events.PingEvent;
|
import pro.gravit.launcher.events.PingEvent;
|
||||||
import pro.gravit.launchserver.LaunchServer;
|
import pro.gravit.launchserver.LaunchServer;
|
||||||
import pro.gravit.launchserver.command.Command;
|
import pro.gravit.launchserver.command.Command;
|
||||||
import pro.gravit.launchserver.socket.handlers.NettyServerSocketHandler;
|
import pro.gravit.launchserver.socket.handlers.NettyServerSocketHandler;
|
||||||
import pro.gravit.utils.helper.CommonHelper;
|
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 class TestCommand extends Command {
|
||||||
public TestCommand(LaunchServer server) {
|
public TestCommand(LaunchServer server) {
|
||||||
super(server);
|
super(server);
|
||||||
|
@ -34,5 +39,18 @@ public void invoke(String... args) throws Exception {
|
||||||
if (args[0].equals("stop")) {
|
if (args[0].equals("stop")) {
|
||||||
handler.close();
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
package pro.gravit.launchserver.manangers;
|
package pro.gravit.launchserver.manangers;
|
||||||
|
|
||||||
|
|
||||||
|
import org.bouncycastle.asn1.pkcs.PrivateKeyInfo;
|
||||||
import org.bouncycastle.asn1.x500.X500Name;
|
import org.bouncycastle.asn1.x500.X500Name;
|
||||||
import org.bouncycastle.asn1.x500.X500NameBuilder;
|
import org.bouncycastle.asn1.x500.X500NameBuilder;
|
||||||
import org.bouncycastle.asn1.x500.style.BCStyle;
|
import org.bouncycastle.asn1.x500.style.BCStyle;
|
||||||
|
@ -12,6 +13,7 @@
|
||||||
import org.bouncycastle.crypto.params.AsymmetricKeyParameter;
|
import org.bouncycastle.crypto.params.AsymmetricKeyParameter;
|
||||||
import org.bouncycastle.crypto.params.ECKeyParameters;
|
import org.bouncycastle.crypto.params.ECKeyParameters;
|
||||||
import org.bouncycastle.crypto.util.PrivateKeyFactory;
|
import org.bouncycastle.crypto.util.PrivateKeyFactory;
|
||||||
|
import org.bouncycastle.crypto.util.PrivateKeyInfoFactory;
|
||||||
import org.bouncycastle.jce.ECNamedCurveTable;
|
import org.bouncycastle.jce.ECNamedCurveTable;
|
||||||
import org.bouncycastle.jce.spec.ECParameterSpec;
|
import org.bouncycastle.jce.spec.ECParameterSpec;
|
||||||
import org.bouncycastle.openssl.PEMWriter;
|
import org.bouncycastle.openssl.PEMWriter;
|
||||||
|
@ -46,11 +48,11 @@ public class CertificateManager {
|
||||||
//public X509CertificateHolder server;
|
//public X509CertificateHolder server;
|
||||||
//public AsymmetricKeyParameter serverKey;
|
//public AsymmetricKeyParameter serverKey;
|
||||||
|
|
||||||
public int validDays = 0;
|
public int validDays = 60;
|
||||||
public int minusHours = 6;
|
public int minusHours = 6;
|
||||||
|
|
||||||
public X509CertificateHolder generateCertificate(String subjectName, PublicKey subjectPublicKey) throws OperatorCreationException {
|
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());
|
BigInteger serial = BigInteger.valueOf(SecurityHelper.newRandom().nextLong());
|
||||||
Date startDate = Date.from(Instant.now().minus(minusHours, ChronoUnit.HOURS));
|
Date startDate = Date.from(Instant.now().minus(minusHours, ChronoUnit.HOURS));
|
||||||
Date endDate = Date.from(startDate.toInstant().plus(validDays, ChronoUnit.DAYS));
|
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());
|
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 {
|
public void writePrivateKey(Path file, PrivateKey privateKey) throws IOException {
|
||||||
try (PemWriter writer = new PemWriter(IOHelper.newWriter(file))) {
|
try (PemWriter writer = new PemWriter(IOHelper.newWriter(file))) {
|
||||||
writer.writeObject(new PemObject("PRIVATE KEY", privateKey.getEncoded()));
|
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 {
|
public void writeCertificate(Path file, X509CertificateHolder holder) throws IOException {
|
||||||
try (PemWriter writer = new PemWriter(IOHelper.newWriter(file))) {
|
try (PemWriter writer = new PemWriter(IOHelper.newWriter(file))) {
|
||||||
writer.writeObject(new PemObject("CERTIFICATE", holder.toASN1Structure().getEncoded()));
|
writer.writeObject(new PemObject("CERTIFICATE", holder.toASN1Structure().getEncoded()));
|
||||||
|
|
Loading…
Reference in a new issue