Search in sources :

Example 1 with CertificateRequest

use of com.venafi.vcert.sdk.certificate.CertificateRequest in project vcert-java by Venafi.

the class CloudConnectorTest method renewCertificate.

@Test
@DisplayName("Renew a certificate with fingerprint")
void renewCertificate() throws VCertException {
    final String apiKey = "12345678-1234-1234-1234-123456789012";
    final Authentication auth = new Authentication(null, null, apiKey);
    String requestId = "request_1";
    final String thumbprint = "52030990E3DC44199DA11C2D73E41EF8EAD8A4E1";
    final RenewalRequest renewalRequest = new RenewalRequest();
    CertificateRequest request = mock(CertificateRequest.class);
    renewalRequest.request(request);
    final Cloud.CertificateSearchResponse searchResponse = mock(Cloud.CertificateSearchResponse.class);
    final CertificateStatus certificateStatus = mock(CertificateStatus.class);
    renewalRequest.thumbprint(thumbprint);
    final Cloud.Certificate certificate1 = new Cloud.Certificate();
    certificate1.certificateRequestId(requestId);
    final CloudConnector.CertificateRequestsResponse requestsResponse = mock(CloudConnector.CertificateRequestsResponse.class);
    final CloudConnector.CertificateRequestsResponseData requestsResponseData = mock(CloudConnector.CertificateRequestsResponseData.class);
    // CertificateDetails certDetails = cloud.certificateDetails(certificateId, auth.apiKey());
    CertificateDetails certDetails = new CertificateDetails();
    certDetails.id("007");
    certDetails.certificateRequestId(requestId);
    List<String> list = new ArrayList<String>();
    list.add(requestId);
    when(cloud.certificateDetails(eq(requestId), eq(apiKey))).thenReturn(certDetails);
    when(cloud.searchCertificates(eq(apiKey), searchRequestArgumentCaptor.capture())).thenReturn(searchResponse);
    when(searchResponse.certificates()).thenReturn(singletonList(certificate1));
    when(cloud.certificateStatus(requestId, apiKey)).thenReturn(certificateStatus);
    when(certificateStatus.certificateIds()).thenReturn(list);
    when(cloud.certificateRequest(eq(apiKey), any(CloudConnector.CertificateRequestsPayload.class))).thenReturn(requestsResponse);
    when(requestsResponse.certificateRequests()).thenReturn(singletonList(requestsResponseData));
    when(requestsResponseData.id()).thenReturn("certificate_result");
    String fakeCSR = "fake csr";
    byte[] bytes = fakeCSR.getBytes();
    when(renewalRequest.request().csr()).thenReturn(bytes);
    classUnderTest.authenticate(auth);
    assertThat(classUnderTest.renewCertificate(renewalRequest)).isEqualTo("certificate_result");
}
Also used : CertificateStatus(com.venafi.vcert.sdk.certificate.CertificateStatus) CertificateDetails(com.venafi.vcert.sdk.connectors.cloud.domain.CertificateDetails) ArrayList(java.util.ArrayList) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) RenewalRequest(com.venafi.vcert.sdk.certificate.RenewalRequest) Authentication(com.venafi.vcert.sdk.endpoint.Authentication) CertificateRequest(com.venafi.vcert.sdk.certificate.CertificateRequest) Test(org.junit.jupiter.api.Test) DisplayName(org.junit.jupiter.api.DisplayName)

Example 2 with CertificateRequest

use of com.venafi.vcert.sdk.certificate.CertificateRequest in project vcert-java by Venafi.

the class TppConnectorCertAT method privateKeyPKCSTest.

@Test
void privateKeyPKCSTest() throws VCertException, UnknownHostException, IOException {
    TppConnector connector = connectorResource.connector();
    ZoneConfiguration zoneConfiguration = connectorResource.zoneConfiguration();
    // By default the DataFormat of the CertificateRequest is PKCS8
    CertificateRequest certificateRequest = connectorResource.certificateRequest().csrOrigin(CsrOriginOption.ServiceGeneratedCSR).keyPassword(TestUtils.KEY_PASSWORD);
    certificateRequest = connector.generateRequest(zoneConfiguration, certificateRequest);
    String pickupId = connector.requestCertificate(certificateRequest, zoneConfiguration);
    assertThat(pickupId).isNotNull();
    // Retrieving the PemCollection
    PEMCollection pemCollectionRSAPrivateKeyPKCS8 = connector.retrieveCertificate(certificateRequest);
    // getting the PrivateKey as PEM which should be a RSA Private Key in PKCS8 Encrypted
    String privateKeyPKCS8AsEncryptedPem = pemCollectionRSAPrivateKeyPKCS8.pemPrivateKey();
    PemObject privateKeyPKCS8AsPemObject = new PemReader(new StringReader(privateKeyPKCS8AsEncryptedPem)).readPemObject();
    // evaluating that the private Key is in PKCS8 Encrypted
    assertThat(pemCollectionRSAPrivateKeyPKCS8.privateKey()).isNotNull();
    assertTrue(privateKeyPKCS8AsPemObject.getType().equals(TestUtils.PEM_HEADER_PKCS8_ENCRYPTED));
    // changing to data format Legacy in order to get the PrivateKey in PKCS1
    certificateRequest.dataFormat(DataFormat.LEGACY);
    // Retrieving the PemCollection
    PEMCollection pemCollectionRSAPrivateKey = connector.retrieveCertificate(certificateRequest);
    // getting the PrivateKey as PEM which should be a RSA Private Key Encrypted
    String privateKeyRSAAsEncryptedPem = pemCollectionRSAPrivateKey.pemPrivateKey();
    PemObject privateKeyRSAAsPemObject = new PemReader(new StringReader(privateKeyRSAAsEncryptedPem)).readPemObject();
    // evaluating that the private Key is in PKCS1 Encrypted
    assertThat(pemCollectionRSAPrivateKey.privateKey()).isNotNull();
    assertTrue(privateKeyRSAAsPemObject.getHeaders().stream().anyMatch(header -> TestUtils.PEM_RSA_PRIVATE_KEY_ENCRYPTED_HEADER_VALUE.equals(((PemHeader) header).getValue())));
}
Also used : X509Certificate(java.security.cert.X509Certificate) ImportResponse(com.venafi.vcert.sdk.certificate.ImportResponse) RenewalRequest(com.venafi.vcert.sdk.certificate.RenewalRequest) Date(java.util.Date) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) ZoneConfiguration(com.venafi.vcert.sdk.connectors.ZoneConfiguration) LocalDateTime(java.time.LocalDateTime) PEMCollection(com.venafi.vcert.sdk.certificate.PEMCollection) PemHeader(org.bouncycastle.util.io.pem.PemHeader) TestUtils(com.venafi.vcert.sdk.TestUtils) CsrOriginOption(com.venafi.vcert.sdk.certificate.CsrOriginOption) SocketException(java.net.SocketException) RegisterExtension(org.junit.jupiter.api.extension.RegisterExtension) ZoneOffset(java.time.ZoneOffset) VCertUtils(com.venafi.vcert.sdk.utils.VCertUtils) PemObject(org.bouncycastle.util.io.pem.PemObject) PemReader(org.bouncycastle.util.io.pem.PemReader) Assert.assertTrue(org.junit.Assert.assertTrue) IOException(java.io.IOException) CertificateException(java.security.cert.CertificateException) Instant(java.time.Instant) UnknownHostException(java.net.UnknownHostException) VCertException(com.venafi.vcert.sdk.VCertException) CertificateRequest(com.venafi.vcert.sdk.certificate.CertificateRequest) DisplayName(org.junit.jupiter.api.DisplayName) Test(org.junit.jupiter.api.Test) DataFormat(com.venafi.vcert.sdk.certificate.DataFormat) ImportRequest(com.venafi.vcert.sdk.certificate.ImportRequest) StringReader(java.io.StringReader) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) LocalDate(java.time.LocalDate) DigestUtils(org.apache.commons.codec.digest.DigestUtils) RevocationRequest(com.venafi.vcert.sdk.certificate.RevocationRequest) PemObject(org.bouncycastle.util.io.pem.PemObject) PemReader(org.bouncycastle.util.io.pem.PemReader) PEMCollection(com.venafi.vcert.sdk.certificate.PEMCollection) ZoneConfiguration(com.venafi.vcert.sdk.connectors.ZoneConfiguration) StringReader(java.io.StringReader) CertificateRequest(com.venafi.vcert.sdk.certificate.CertificateRequest) Test(org.junit.jupiter.api.Test)

Example 3 with CertificateRequest

use of com.venafi.vcert.sdk.certificate.CertificateRequest in project vcert-java by Venafi.

the class TppConnectorCertAT method retrieveCertificate.

@Test
void retrieveCertificate() throws VCertException, SocketException, UnknownHostException {
    TppConnector connector = connectorResource.connector();
    ZoneConfiguration zoneConfiguration = connectorResource.zoneConfiguration();
    CertificateRequest certificateRequest = connectorResource.certificateRequest();
    certificateRequest = connector.generateRequest(zoneConfiguration, certificateRequest);
    String certificateId = connector.requestCertificate(certificateRequest, zoneConfiguration);
    assertThat(certificateId).isNotNull();
    PEMCollection pemCollection = connector.retrieveCertificate(certificateRequest);
    assertThat(pemCollection.certificate()).isNotNull();
    assertThat(pemCollection.privateKey()).isNotNull();
}
Also used : PEMCollection(com.venafi.vcert.sdk.certificate.PEMCollection) ZoneConfiguration(com.venafi.vcert.sdk.connectors.ZoneConfiguration) CertificateRequest(com.venafi.vcert.sdk.certificate.CertificateRequest) Test(org.junit.jupiter.api.Test)

Example 4 with CertificateRequest

use of com.venafi.vcert.sdk.certificate.CertificateRequest in project vcert-java by Venafi.

the class TppTokenConnectorAT method generateRequest.

@Test
void generateRequest() throws VCertException, IOException {
    String commonName = TestUtils.randomCN();
    ZoneConfiguration zoneConfiguration = connectorResource.connector().readZoneConfiguration(TestUtils.TPP_ZONE);
    CertificateRequest certificateRequest = new CertificateRequest().subject(new CertificateRequest.PKIXName().commonName(commonName).organization(Collections.singletonList("Venafi, Inc.")).organizationalUnit(Arrays.asList("Engineering", "Automated Tests")).country(Collections.singletonList("US")).locality(Collections.singletonList("SLC")).province(Collections.singletonList("Utah"))).dnsNames(Collections.singletonList(InetAddress.getLocalHost().getHostName())).ipAddresses(getTestIps()).keyType(KeyType.RSA).keyLength(2048);
    certificateRequest = connectorResource.connector().generateRequest(zoneConfiguration, certificateRequest);
    assertThat(certificateRequest.csr()).isNotEmpty();
    PKCS10CertificationRequest request = (PKCS10CertificationRequest) new PEMParser(new StringReader(new String(certificateRequest.csr()))).readObject();
    // Values overridden by policy which is why they don't match the above values
    String subject = request.getSubject().toString();
    assertThat(subject).contains(format("CN=%s", commonName));
}
Also used : PKCS10CertificationRequest(org.bouncycastle.pkcs.PKCS10CertificationRequest) PEMParser(org.bouncycastle.openssl.PEMParser) ZoneConfiguration(com.venafi.vcert.sdk.connectors.ZoneConfiguration) StringReader(java.io.StringReader) CertificateRequest(com.venafi.vcert.sdk.certificate.CertificateRequest) Test(org.junit.jupiter.api.Test)

Example 5 with CertificateRequest

use of com.venafi.vcert.sdk.certificate.CertificateRequest in project vcert-java by Venafi.

the class TppTokenConnectorCertAT method privateKeyPKCSTest.

@Test
void privateKeyPKCSTest() throws VCertException, UnknownHostException, IOException {
    TppTokenConnector connector = connectorResource.connector();
    ZoneConfiguration zoneConfiguration = connectorResource.zoneConfiguration();
    // By default the DataFormat of the CertificateRequest is PKCS8
    CertificateRequest certificateRequest = connectorResource.certificateRequest().csrOrigin(CsrOriginOption.ServiceGeneratedCSR).keyPassword(TestUtils.KEY_PASSWORD);
    certificateRequest = connector.generateRequest(zoneConfiguration, certificateRequest);
    String pickupId = connector.requestCertificate(certificateRequest, zoneConfiguration);
    assertThat(pickupId).isNotNull();
    // Retrieving the PemCollection
    PEMCollection pemCollectionRSAPrivateKeyPKCS8 = connector.retrieveCertificate(certificateRequest);
    // getting the PrivateKey as PEM which should be a RSA Private Key in PKCS8 Encrypted
    String privateKeyPKCS8AsEncryptedPem = pemCollectionRSAPrivateKeyPKCS8.pemPrivateKey();
    PemObject privateKeyPKCS8AsPemObject = new PemReader(new StringReader(privateKeyPKCS8AsEncryptedPem)).readPemObject();
    // evaluating that the private Key is in PKCS8 Encrypted
    assertThat(pemCollectionRSAPrivateKeyPKCS8.privateKey()).isNotNull();
    assertTrue(privateKeyPKCS8AsPemObject.getType().equals(TestUtils.PEM_HEADER_PKCS8_ENCRYPTED));
    // changing to data format Legacy in order to get the PrivateKey in PKCS1
    certificateRequest.dataFormat(DataFormat.LEGACY);
    // Retrieving the PemCollection
    PEMCollection pemCollectionRSAPrivateKey = connector.retrieveCertificate(certificateRequest);
    // getting the PrivateKey as PEM which should be a RSA Private Key Encrypted
    String privateKeyRSAAsEncryptedPem = pemCollectionRSAPrivateKey.pemPrivateKey();
    PemObject privateKeyRSAAsPemObject = new PemReader(new StringReader(privateKeyRSAAsEncryptedPem)).readPemObject();
    // evaluating that the private Key is in PKCS1 Encrypted
    assertThat(pemCollectionRSAPrivateKey.privateKey()).isNotNull();
    assertTrue(privateKeyRSAAsPemObject.getHeaders().stream().anyMatch(header -> TestUtils.PEM_RSA_PRIVATE_KEY_ENCRYPTED_HEADER_VALUE.equals(((PemHeader) header).getValue())));
}
Also used : X509Certificate(java.security.cert.X509Certificate) ImportResponse(com.venafi.vcert.sdk.certificate.ImportResponse) RenewalRequest(com.venafi.vcert.sdk.certificate.RenewalRequest) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) ZoneConfiguration(com.venafi.vcert.sdk.connectors.ZoneConfiguration) PEMCollection(com.venafi.vcert.sdk.certificate.PEMCollection) PemHeader(org.bouncycastle.util.io.pem.PemHeader) TestUtils(com.venafi.vcert.sdk.TestUtils) ArrayList(java.util.ArrayList) CsrOriginOption(com.venafi.vcert.sdk.certificate.CsrOriginOption) SocketException(java.net.SocketException) RegisterExtension(org.junit.jupiter.api.extension.RegisterExtension) PemObject(org.bouncycastle.util.io.pem.PemObject) PemReader(org.bouncycastle.util.io.pem.PemReader) CustomField(com.venafi.vcert.sdk.certificate.CustomField) Assert.assertTrue(org.junit.Assert.assertTrue) IOException(java.io.IOException) CertificateException(java.security.cert.CertificateException) UnknownHostException(java.net.UnknownHostException) VCertException(com.venafi.vcert.sdk.VCertException) CertificateRequest(com.venafi.vcert.sdk.certificate.CertificateRequest) DisplayName(org.junit.jupiter.api.DisplayName) Test(org.junit.jupiter.api.Test) DataFormat(com.venafi.vcert.sdk.certificate.DataFormat) ImportRequest(com.venafi.vcert.sdk.certificate.ImportRequest) List(java.util.List) StringReader(java.io.StringReader) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) DigestUtils(org.apache.commons.codec.digest.DigestUtils) RevocationRequest(com.venafi.vcert.sdk.certificate.RevocationRequest) PemObject(org.bouncycastle.util.io.pem.PemObject) PemReader(org.bouncycastle.util.io.pem.PemReader) PEMCollection(com.venafi.vcert.sdk.certificate.PEMCollection) ZoneConfiguration(com.venafi.vcert.sdk.connectors.ZoneConfiguration) StringReader(java.io.StringReader) CertificateRequest(com.venafi.vcert.sdk.certificate.CertificateRequest) Test(org.junit.jupiter.api.Test)

Aggregations

CertificateRequest (com.venafi.vcert.sdk.certificate.CertificateRequest)53 Test (org.junit.jupiter.api.Test)44 ZoneConfiguration (com.venafi.vcert.sdk.connectors.ZoneConfiguration)43 DisplayName (org.junit.jupiter.api.DisplayName)24 PEMCollection (com.venafi.vcert.sdk.certificate.PEMCollection)20 RenewalRequest (com.venafi.vcert.sdk.certificate.RenewalRequest)9 Authentication (com.venafi.vcert.sdk.endpoint.Authentication)8 StringReader (java.io.StringReader)8 FeignException (feign.FeignException)7 X509Certificate (java.security.cert.X509Certificate)7 RevocationRequest (com.venafi.vcert.sdk.certificate.RevocationRequest)6 VCertException (com.venafi.vcert.sdk.VCertException)5 IOException (java.io.IOException)5 ArrayList (java.util.ArrayList)5 PEMParser (org.bouncycastle.openssl.PEMParser)5 CsrOriginOption (com.venafi.vcert.sdk.certificate.CsrOriginOption)4 DataFormat (com.venafi.vcert.sdk.certificate.DataFormat)4 Instant (java.time.Instant)4 BouncyCastleProvider (org.bouncycastle.jce.provider.BouncyCastleProvider)4 PKCS10CertificationRequest (org.bouncycastle.pkcs.PKCS10CertificationRequest)4