[FEATURE] Чтение сертификатов

This commit is contained in:
Gravit 2019-07-11 01:45:05 +07:00
parent 27b54cccba
commit 5a889e0471
No known key found for this signature in database
GPG key ID: 061981E1E85D3216
2 changed files with 31 additions and 6 deletions

View file

@ -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];

View file

@ -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;
}
}