use of com.venafi.vcert.sdk.certificate.PEMCollection in project vcert-java by Venafi.
the class CloudConnectorTest method retrieveCertificate.
@Test
void retrieveCertificate() throws VCertException, IOException {
Security.addProvider(new BouncyCastleProvider());
String apiKey = "12345678-1234-1234-1234-123456789012";
final Authentication auth = new Authentication(null, null, apiKey);
classUnderTest.authenticate(auth);
String body = readResourceAsString("certificates/certWithKey.pem");
PEMCollection pemCollection = PEMCollection.fromStringPEMCollection(body, ChainOption.ChainOptionIgnore, null, null);
CertificateRequest request = new CertificateRequest().subject(new CertificateRequest.PKIXName().commonName("random name").organization(singletonList("Venafi, Inc.")).organizationalUnit(singletonList("Automated Tests")));
request.pickupId("jackpot").keyType(KeyType.RSA).keyPair(new KeyPair(pemCollection.certificate().getPublicKey(), pemCollection.privateKey())).keyPassword(KEY_SECRET);
List<String> list = new ArrayList<String>();
list.add("jackpot");
CertificateStatus status = new CertificateStatus().status("ISSUED").certificateIds(list);
CertificateDetails certificateDetails = new CertificateDetails().dekHash("12345");
EdgeEncryptionKey edgeEncryptionKey = new EdgeEncryptionKey();
cloud.certificateDetails(eq("jackpot"), eq(apiKey));
when(cloud.certificateStatus(eq("jackpot"), eq(apiKey))).thenReturn(status);
when(cloud.retrieveCertificate(eq("jackpot"), eq(apiKey), eq("ROOT_FIRST"))).thenReturn(Response.builder().request(Request.create(Request.HttpMethod.GET, "http://localhost", new HashMap<String, Collection<String>>(), null, null)).status(200).body(body, Charset.forName("UTF-8")).build());
when(cloud.certificateDetails(eq("jackpot"), eq(apiKey))).thenReturn(certificateDetails);
when(cloud.retrieveEdgeEncryptionKey(eq("12345"), eq(apiKey))).thenReturn(edgeEncryptionKey);
PEMCollection pemCollection2 = classUnderTest.retrieveCertificate(request);
assertThat(pemCollection2).isNotNull();
assertThat(pemCollection2.certificate()).isNotNull();
assertThat(pemCollection2.privateKey()).isNotNull();
assertThat(pemCollection2.privateKeyPassword()).isEqualTo(KEY_SECRET);
}
use of com.venafi.vcert.sdk.certificate.PEMCollection in project vcert-java by Venafi.
the class CloudConnectorCertAT method privateKeyPKCSTest.
@Test
void privateKeyPKCSTest() throws VCertException, UnknownHostException, IOException {
CloudConnector 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())));
}
use of com.venafi.vcert.sdk.certificate.PEMCollection in project vcert-java by Venafi.
the class CloudConnectorCertAT method createCertificateValidateValidityHours.
@Test
@DisplayName("Create a certificate and validate specified validity hours - Cloud")
public void createCertificateValidateValidityHours() throws VCertException {
CloudConnector connector = connectorResource.connector();
ZoneConfiguration zoneConfiguration = connectorResource.zoneConfiguration();
CertificateRequest certificateRequest = connectorResource.certificateRequest().keyType(KeyType.RSA).validityHours(TestUtils.VALID_HOURS);
certificateRequest = connector.generateRequest(zoneConfiguration, certificateRequest);
connector.requestCertificate(certificateRequest, zoneConfiguration);
// Retrieve PEM collection from Venafi
PEMCollection pemCollection = connector.retrieveCertificate(certificateRequest);
Date notAfter = pemCollection.certificate().getNotAfter();
LocalDate notAfterDate = notAfter.toInstant().atOffset(ZoneOffset.UTC).toLocalDate();
Instant now = Instant.now();
LocalDateTime utcDateTime = LocalDateTime.ofInstant(now, ZoneOffset.UTC);
int validityDays = VCertUtils.getValidityDays(TestUtils.VALID_HOURS);
utcDateTime = utcDateTime.plusDays(validityDays);
LocalDate nowDateInUTC = utcDateTime.toLocalDate();
// Dates should be equals if not then it will fail
assertTrue(notAfterDate.compareTo(nowDateInUTC) == 0);
}
use of com.venafi.vcert.sdk.certificate.PEMCollection in project vcert-java by Venafi.
the class TppClient method main.
public static void main(String[] args) throws VCertException, CertificateEncodingException, NoSuchAlgorithmException, KeyManagementException {
String url = System.getenv("TPPURL");
String zone = System.getenv("TPPZONE");
String appInfo = System.getenv("PRODUCT");
String tpp_user = System.getenv("TPPUSER");
String tpp_passwd = System.getenv("TPPPASSWORD");
if (tpp_user == null)
tpp_user = "local:admin";
if (tpp_passwd == null)
tpp_passwd = "password";
if (url == null)
url = "https://tpp.venafi.example/vedsdk";
if (zone == null)
zone = "Certificates\\vcert\\";
if (appInfo == null)
appInfo = "CompanyName AppName";
// Configuration
Config config = Config.builder().connectorType(ConnectorType.TPP).baseUrl(url).appInfo(appInfo).build();
Authentication auth = Authentication.builder().user(tpp_user).password(tpp_passwd).build();
VCertClient client = new VCertClient(config);
client.authenticate(auth);
ZoneConfiguration zoneConfiguration = client.readZoneConfiguration(zone);
// Generate a certificate
CertificateRequest certificateRequest = new CertificateRequest().subject(new CertificateRequest.PKIXName().commonName("vcert-java.venafi.example").organization(Collections.singletonList("Venafi, Inc.")).organizationalUnit(Arrays.asList("Product Management")).country(Collections.singletonList("US")).locality(Collections.singletonList("Salt Lake City")).province(Collections.singletonList("Utah"))).keyType(KeyType.RSA).keyLength(2048);
certificateRequest = client.generateRequest(zoneConfiguration, certificateRequest);
// Submit the certificate request
client.requestCertificate(certificateRequest, zoneConfiguration);
// Retrieve PEM collection from Venafi
PEMCollection pemCollection = client.retrieveCertificate(certificateRequest);
System.out.println(pemCollection.certificate());
}
use of com.venafi.vcert.sdk.certificate.PEMCollection in project vcert-java by Venafi.
the class TppTokenClient method main.
public static void main(String[] args) throws VCertException, CertificateEncodingException, NoSuchAlgorithmException, KeyManagementException {
String url = System.getenv("TPP_TOKEN_URL");
String zone = System.getenv("TPPZONE");
String appInfo = System.getenv("PRODUCT");
String tpp_user = System.getenv("TPPUSER");
String tpp_passwd = System.getenv("TPPPASSWORD");
if (tpp_user == null)
tpp_user = "local:admin";
if (tpp_passwd == null)
tpp_passwd = "password";
if (url == null)
url = "https://tpp.venafi.example/vedsdk";
if (zone == null)
zone = "Certificates\\vcert\\";
if (appInfo == null)
appInfo = "CompanyName AppName";
// Configuration
Config config = Config.builder().connectorType(ConnectorType.TPP_TOKEN).baseUrl(url).appInfo(appInfo).build();
Authentication auth = Authentication.builder().user(tpp_user).password(tpp_passwd).build();
VCertTknClient client = new VCertTknClient(config);
TokenInfo tknInfo = client.getAccessToken(auth);
ZoneConfiguration zoneConfiguration = client.readZoneConfiguration(zone);
// Generate a certificate
CertificateRequest certificateRequest = new CertificateRequest().subject(new CertificateRequest.PKIXName().commonName("vcert-java.venafi.example").organization(Collections.singletonList("Venafi, Inc.")).organizationalUnit(Arrays.asList("Product Management")).country(Collections.singletonList("US")).locality(Collections.singletonList("Salt Lake City")).province(Collections.singletonList("Utah"))).keyType(KeyType.RSA).keyLength(2048);
certificateRequest = client.generateRequest(zoneConfiguration, certificateRequest);
// Submit the certificate request
client.requestCertificate(certificateRequest, zoneConfiguration);
// Retrieve PEM collection from Venafi
PEMCollection pemCollection = client.retrieveCertificate(certificateRequest);
System.out.println(pemCollection.certificate());
}
Aggregations