use of de.carne.certmgr.certs.CertObjectStore in project certmgr by hdecarne.
the class RemoteCertGenerator method generateCert.
@Override
public CertObjectStore generateCert(GenerateCertRequest request, PasswordCallback password) throws IOException {
KeyPair key = KeyHelper.generateKey(request.keyPairAlgorithm(), request.keySize());
SignatureAlgorithm signatureAlgorithm = requiredParameter(request.getSignatureAlgorithm(), "SignatureAlgorithm");
PKCS10CertificateRequest csr = PKCS10CertificateRequest.generateCSR(request.dn(), key, request.getExtensions(), signatureAlgorithm);
CertObjectStore certObjects = new CertObjectStore();
certObjects.addKey(key);
certObjects.addCSR(csr);
return certObjects;
}
use of de.carne.certmgr.certs.CertObjectStore in project certmgr by hdecarne.
the class LocalCertGenerator method generateCert.
@Override
public CertObjectStore generateCert(GenerateCertRequest request, PasswordCallback password) throws IOException {
KeyPair key = KeyHelper.generateKey(request.keyPairAlgorithm(), request.keySize());
Issuer issuer = requiredParameter(request.getIssuer(), "Issuer");
BigInteger serial = BigInteger.ONE;
X500Principal issuerDN = null;
KeyPair issuerKey = null;
X500Principal dn = request.dn();
if (!this.selfSignedIssuer.equals(issuer)) {
UserCertStoreEntry issuerEntry = Check.notNull(issuer.storeEntry());
serial = getNextSerial(issuerEntry);
issuerDN = issuerEntry.dn();
issuerKey = issuerEntry.getKey(password);
} else {
issuerKey = key;
issuerDN = dn;
}
Date notBefore = requiredParameter(request.getNotBefore(), "NotBefore");
Date notAfter = requiredParameter(request.getNotAfter(), "NotAfter");
SignatureAlgorithm signatureAlgorithm = requiredParameter(request.getSignatureAlgorithm(), "SignatureAlgorithm");
X509Certificate crt = X509CertificateHelper.generateCRT(dn, key, serial, notBefore, notAfter, request.getExtensions(), issuerDN, issuerKey, signatureAlgorithm);
CertObjectStore certObjects = new CertObjectStore();
certObjects.addKey(key);
certObjects.addCRT(crt);
return certObjects;
}
use of de.carne.certmgr.certs.CertObjectStore in project certmgr by hdecarne.
the class CertReadersWritersTest method testReaderAndWriter.
private void testReaderAndWriter(CertReader reader, URL testResourceURL, Path testPath) throws IOException {
System.out.println("Testing I/O provider: " + reader.providerName());
CertWriter writer = CertWriters.REGISTERED.get(reader.providerName());
System.out.println(reader.fileType());
System.out.println(Arrays.toString(reader.fileExtensionPatterns()));
if (writer != null) {
System.out.println("isCharWriter: " + writer.isCharWriter());
System.out.println("isEncryptionRequired: " + writer.isEncryptionRequired());
}
CertObjectStore readCertObjects1 = CertReaders.readURL(testResourceURL, Tests.password());
Assert.assertNotNull(readCertObjects1);
for (CertObjectStore.Entry entry : readCertObjects1) {
switch(entry.type()) {
case CRT:
reader.fileExtension(entry.getCRT().getClass());
break;
case KEY:
reader.fileExtension(entry.getKey().getClass());
break;
case CSR:
reader.fileExtension(entry.getCSR().getClass());
break;
case CRL:
reader.fileExtension(entry.getCRL().getClass());
break;
}
reader.fileExtension(getClass());
}
CertObjectStore readCertObjects2;
try (IOResource<InputStream> in = new IOResource<>(testResourceURL.openStream(), reader.providerName())) {
readCertObjects2 = reader.readBinary(in, Tests.password());
Assert.assertNotNull(readCertObjects2);
Assert.assertEquals(readCertObjects1.size(), readCertObjects2.size());
}
if (writer != null) {
if (!writer.isEncryptionRequired()) {
try (IOResource<OutputStream> out = IOResource.newOutputStream(writer.providerName(), testPath)) {
writer.writeBinary(out, readCertObjects2);
}
verifyWriterOutput(readCertObjects2, reader, testPath);
if (writer.isCharWriter()) {
try (IOResource<Writer> out = new IOResource<>(Files.newBufferedWriter(testPath), writer.providerName())) {
writer.writeString(out, readCertObjects2);
}
verifyWriterOutput(readCertObjects2, reader, testPath);
}
}
try (IOResource<OutputStream> out = IOResource.newOutputStream(writer.providerName(), testPath)) {
writer.writeEncryptedBinary(out, readCertObjects2, Tests.password());
}
if (writer.isCharWriter()) {
try (IOResource<Writer> out = new IOResource<>(Files.newBufferedWriter(testPath), writer.providerName())) {
writer.writeEncryptedString(out, readCertObjects2, Tests.password());
}
verifyWriterOutput(readCertObjects2, reader, testPath);
}
verifyWriterOutput(readCertObjects2, reader, testPath);
}
System.out.println();
}
use of de.carne.certmgr.certs.CertObjectStore in project certmgr by hdecarne.
the class CertReadersWritersTest method verifyWriterOutput.
private static void verifyWriterOutput(CertObjectStore readCertObjects, CertReader reader, Path testPath) throws IOException {
try (IOResource<InputStream> in = IOResource.newInputStream(testPath.toString(), testPath)) {
CertObjectStore readCertObjects2 = reader.readBinary(in, Tests.password());
Assert.assertNotNull(readCertObjects2);
Assert.assertEquals(readCertObjects.size(), readCertObjects2.size());
}
}
use of de.carne.certmgr.certs.CertObjectStore in project certmgr by hdecarne.
the class DERCertReaderWriter method readBinary.
@Override
@Nullable
public CertObjectStore readBinary(IOResource<InputStream> in, PasswordCallback password) throws IOException {
LOG.debug("Trying to read DER objects from: ''{0}''...", in);
CertObjectStore certObjects = null;
try (ASN1InputStream derStream = new ASN1InputStream(in.io())) {
ASN1Primitive derObject;
while ((derObject = derStream.readObject()) != null) {
X509Certificate crt = tryDecodeCRT(derObject);
if (crt != null) {
if (certObjects == null) {
certObjects = new CertObjectStore();
}
certObjects.addCRT(crt);
continue;
}
KeyPair key = tryDecodeKey(derObject, in.resource(), password);
if (key != null) {
if (certObjects == null) {
certObjects = new CertObjectStore();
}
certObjects.addKey(key);
continue;
}
PKCS10CertificateRequest csr = tryDecodeCSR(derObject);
if (csr != null) {
if (certObjects == null) {
certObjects = new CertObjectStore();
}
certObjects.addCSR(csr);
continue;
}
X509CRL crl = tryDecodeCRL(derObject);
if (crl != null) {
if (certObjects == null) {
certObjects = new CertObjectStore();
}
certObjects.addCRL(crl);
continue;
}
LOG.warning(CertIOI18N.STR_DER_UNKNOWN_OBJECT, derObject.getClass().getName());
}
} catch (ClassCastException e) {
// the file is not a DER stream
throw new CertProviderException(e);
}
return certObjects;
}
Aggregations