use of com.github.zhenwei.core.asn1.x509.Certificate in project gravitee-access-management by gravitee-io.
the class CertificateServiceImpl method create.
@Override
public Single<Certificate> create(String domain, NewCertificate newCertificate, User principal) {
LOGGER.debug("Create a new certificate {} for domain {}", newCertificate, domain);
Single<Certificate> certificateSingle = certificatePluginService.getSchema(newCertificate.getType()).switchIfEmpty(Maybe.error(new CertificatePluginSchemaNotFoundException(newCertificate.getType()))).map(schema -> objectMapper.readValue(schema, CertificateSchema.class)).flatMapSingle(new Function<CertificateSchema, SingleSource<Certificate>>() {
@Override
public SingleSource<Certificate> apply(CertificateSchema certificateSchema) throws Exception {
return Single.create(emitter -> {
String certificateId = RandomString.generate();
Certificate certificate = new Certificate();
certificate.setId(certificateId);
certificate.setDomain(domain);
certificate.setName(newCertificate.getName());
certificate.setType(newCertificate.getType());
// handle file
try {
JsonNode certificateConfiguration = objectMapper.readTree(newCertificate.getConfiguration());
certificateSchema.getProperties().entrySet().stream().filter(map -> map.getValue().getWidget() != null && "file".equals(map.getValue().getWidget())).map(map -> map.getKey()).forEach(key -> {
try {
JsonNode file = objectMapper.readTree(certificateConfiguration.get(key).asText());
byte[] data = Base64.getDecoder().decode(file.get("content").asText());
certificate.setMetadata(Collections.singletonMap(CertificateMetadata.FILE, data));
// update configuration to set the file name
((ObjectNode) certificateConfiguration).put(key, file.get("name").asText());
newCertificate.setConfiguration(objectMapper.writeValueAsString(certificateConfiguration));
} catch (IOException ex) {
LOGGER.error("An error occurs while trying to create certificate binaries", ex);
emitter.onError(ex);
}
});
certificate.setConfiguration(newCertificate.getConfiguration());
certificate.setCreatedAt(new Date());
certificate.setUpdatedAt(certificate.getCreatedAt());
} catch (Exception ex) {
LOGGER.error("An error occurs while trying to create certificate configuration", ex);
emitter.onError(ex);
}
emitter.onSuccess(certificate);
});
}
});
return certificateSingle.flatMap(certificate -> certificateRepository.create(certificate)).flatMap(certificate -> {
Event event = new Event(Type.CERTIFICATE, new Payload(certificate.getId(), ReferenceType.DOMAIN, certificate.getDomain(), Action.CREATE));
return eventService.create(event).flatMap(__ -> Single.just(certificate));
}).doOnError(ex -> {
LOGGER.error("An error occurs while trying to create a certificate", ex);
throw new TechnicalManagementException("An error occurs while trying to create a certificate", ex);
});
}
use of com.github.zhenwei.core.asn1.x509.Certificate in project efm-integrasjonspunkt by felleslosninger.
the class CmsUtilTest method generateCertificate.
private Certificate generateCertificate(PublicKey subjectPublicKey, PrivateKey issuerPrivateKey) throws ParseException, OperatorCreationException, CertificateException, IOException {
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
X500Name issuer = new X500Name("CN=Issuer and subject (self signed)");
BigInteger serial = new BigInteger("100");
Date notBefore = df.parse("2010-01-01");
Date notAfter = df.parse("2050-01-01");
SubjectPublicKeyInfo publicKeyInfo = SubjectPublicKeyInfo.getInstance(ASN1Sequence.getInstance(subjectPublicKey.getEncoded()));
X509v3CertificateBuilder certBuilder = new X509v3CertificateBuilder(issuer, serial, notBefore, notAfter, issuer, publicKeyInfo);
ContentSigner signer = new JcaContentSignerBuilder("SHA256withRSA").build(issuerPrivateKey);
X509CertificateHolder holder = certBuilder.build(signer);
CertificateFactory factory = CertificateFactory.getInstance("X.509");
return factory.generateCertificate(new ByteArrayInputStream(holder.getEncoded()));
}
use of com.github.zhenwei.core.asn1.x509.Certificate in project eblocker by eblocker.
the class OcspCache method getOcspResponderUrl.
private String getOcspResponderUrl(X509Certificate certificate) throws IOException {
byte[] extensionValue = certificate.getExtensionValue(Extension.authorityInfoAccess.getId());
if (extensionValue == null) {
return null;
}
ASN1OctetString octetString = ASN1OctetString.getInstance(extensionValue);
ASN1Primitive primitive = ASN1Primitive.fromByteArray(octetString.getOctets());
AuthorityInformationAccess authorityInformationAccessExtension = AuthorityInformationAccess.getInstance(primitive);
for (AccessDescription i : authorityInformationAccessExtension.getAccessDescriptions()) {
if (i.getAccessMethod().equals(OCSPObjectIdentifiers.id_pkix_ocsp)) {
return i.getAccessLocation().getName().toString();
}
}
return null;
}
use of com.github.zhenwei.core.asn1.x509.Certificate in project interlok by adaptris.
the class X509Builder method build.
private X509Certificate build() throws NoSuchAlgorithmException, CertificateException, OperatorCreationException {
X509Certificate result = null;
if (privateKey == null) {
createKeyPair();
}
// The certificate is self-signed, so use the current
// subject as the issuer
X500Name name = certificateParm.getSubjectInfo();
// The certificate is self-signed, do we exactly care what
// the serial number that uniquely identifies is
BigInteger serial = BigInteger.valueOf(Integer.valueOf(SecurityUtil.getSecureRandom().nextInt(10000)).longValue());
GregorianCalendar valid = new GregorianCalendar();
Date notBefore = valid.getTime();
valid.add(Calendar.MONTH, 12);
Date notAfter = valid.getTime();
SubjectPublicKeyInfo pubKeyInfo = SubjectPublicKeyInfo.getInstance(ASN1Sequence.getInstance(publicKey.getEncoded()));
X509v3CertificateBuilder certGen = new X509v3CertificateBuilder(name, serial, notBefore, notAfter, name, pubKeyInfo);
String alg = certificateParm.getSignatureAlgorithm();
JcaContentSignerBuilder builder = new JcaContentSignerBuilder(alg);
// build and sign the certificate
X509CertificateHolder certHolder = certGen.build(builder.build(privateKey));
result = new JcaX509CertificateConverter().getCertificate(certHolder);
return result;
}
use of com.github.zhenwei.core.asn1.x509.Certificate in project kubernetes-client by fabric8io.
the class V1CertificateCrudTest method shouldListAndGetCertificate.
@Test
void shouldListAndGetCertificate() {
Certificate certificate2 = new CertificateBuilder().withNewMetadata().withName("cert2").endMetadata().build();
client.v1().certificates().inNamespace("ns2").create(certificate2);
CertificateList certificateList = client.v1().certificates().inNamespace("ns2").list();
assertNotNull(certificateList);
assertEquals(1, certificateList.getItems().size());
}
Aggregations