Search in sources :

Example 61 with Certificate

use of com.google.cloud.security.privateca.v1.Certificate in project java-security-private-ca by googleapis.

the class DeleteCertificateAuthority method deleteCertificateAuthority.

// Delete the Certificate Authority from the specified CA pool.
// Before deletion, the CA must be disabled and must not contain any active certificates.
public static void deleteCertificateAuthority(String project, String location, String pool_Id, String certificateAuthorityName) throws IOException, ExecutionException, InterruptedException {
    // clean up any remaining background resources.
    try (CertificateAuthorityServiceClient certificateAuthorityServiceClient = CertificateAuthorityServiceClient.create()) {
        // Create the Certificate Authority Name.
        CertificateAuthorityName certificateAuthorityNameParent = CertificateAuthorityName.newBuilder().setProject(project).setLocation(location).setCaPool(pool_Id).setCertificateAuthority(certificateAuthorityName).build();
        // Check if the CA is enabled.
        State caState = certificateAuthorityServiceClient.getCertificateAuthority(certificateAuthorityNameParent).getState();
        if (caState == State.ENABLED) {
            System.out.println("Please disable the Certificate Authority before deletion ! Current state: " + caState);
            return;
        }
        // Create the DeleteCertificateAuthorityRequest.
        // Setting the setIgnoreActiveCertificates() to true, will delete the CA
        // even if it contains active certificates. Care should be taken to re-anchor
        // the certificates to new CA before deleting.
        DeleteCertificateAuthorityRequest deleteCertificateAuthorityRequest = DeleteCertificateAuthorityRequest.newBuilder().setName(certificateAuthorityNameParent.toString()).setIgnoreActiveCertificates(false).build();
        // Delete the Certificate Authority.
        ApiFuture<Operation> futureCall = certificateAuthorityServiceClient.deleteCertificateAuthorityCallable().futureCall(deleteCertificateAuthorityRequest);
        Operation response = futureCall.get();
        if (response.hasError()) {
            System.out.println("Error while deleting Certificate Authority !" + response.getError());
            return;
        }
        // Check if the CA has been deleted.
        caState = certificateAuthorityServiceClient.getCertificateAuthority(certificateAuthorityNameParent).getState();
        if (caState == State.DELETED) {
            System.out.println("Successfully deleted Certificate Authority : " + certificateAuthorityName);
        } else {
            System.out.println("Unable to delete Certificate Authority. Please try again ! Current state: " + caState);
        }
    }
}
Also used : State(com.google.cloud.security.privateca.v1.CertificateAuthority.State) CertificateAuthorityServiceClient(com.google.cloud.security.privateca.v1.CertificateAuthorityServiceClient) CertificateAuthorityName(com.google.cloud.security.privateca.v1.CertificateAuthorityName) Operation(com.google.longrunning.Operation) DeleteCertificateAuthorityRequest(com.google.cloud.security.privateca.v1.DeleteCertificateAuthorityRequest)

Example 62 with Certificate

use of com.google.cloud.security.privateca.v1.Certificate in project java-security-private-ca by googleapis.

the class EnableCertificateAuthority method enableCertificateAuthority.

// Enable the Certificate Authority present in the given ca pool.
// CA cannot be enabled if it has been already deleted.
public static void enableCertificateAuthority(String project, String location, String pool_Id, String certificateAuthorityName) throws IOException, ExecutionException, InterruptedException {
    try (CertificateAuthorityServiceClient certificateAuthorityServiceClient = CertificateAuthorityServiceClient.create()) {
        // Create the Certificate Authority Name.
        CertificateAuthorityName certificateAuthorityParent = CertificateAuthorityName.newBuilder().setProject(project).setLocation(location).setCaPool(pool_Id).setCertificateAuthority(certificateAuthorityName).build();
        // Create the Enable Certificate Authority Request.
        EnableCertificateAuthorityRequest enableCertificateAuthorityRequest = EnableCertificateAuthorityRequest.newBuilder().setName(certificateAuthorityParent.toString()).build();
        // Enable the Certificate Authority.
        ApiFuture<Operation> futureCall = certificateAuthorityServiceClient.enableCertificateAuthorityCallable().futureCall(enableCertificateAuthorityRequest);
        Operation response = futureCall.get();
        if (response.hasError()) {
            System.out.println("Error while enabling Certificate Authority !" + response.getError());
            return;
        }
        // Get the current CA state.
        State caState = certificateAuthorityServiceClient.getCertificateAuthority(certificateAuthorityParent).getState();
        // Check if the CA is enabled.
        if (caState == State.ENABLED) {
            System.out.println("Enabled Certificate Authority : " + certificateAuthorityName);
        } else {
            System.out.println("Cannot enable the Certificate Authority ! Current CA State: " + caState);
        }
    }
}
Also used : State(com.google.cloud.security.privateca.v1.CertificateAuthority.State) CertificateAuthorityServiceClient(com.google.cloud.security.privateca.v1.CertificateAuthorityServiceClient) CertificateAuthorityName(com.google.cloud.security.privateca.v1.CertificateAuthorityName) EnableCertificateAuthorityRequest(com.google.cloud.security.privateca.v1.EnableCertificateAuthorityRequest) Operation(com.google.longrunning.Operation)

Example 63 with Certificate

use of com.google.cloud.security.privateca.v1.Certificate in project java-security-private-ca by googleapis.

the class RevokeCertificate method revokeCertificate.

// Revoke an issued certificate. Once revoked, the certificate will become invalid and will expire
// post its lifetime.
public static void revokeCertificate(String project, String location, String pool_Id, String certificateName) throws IOException, ExecutionException, InterruptedException {
    // clean up any remaining background resources.
    try (CertificateAuthorityServiceClient certificateAuthorityServiceClient = CertificateAuthorityServiceClient.create()) {
        // Create Certificate Name.
        CertificateName certificateNameParent = CertificateName.newBuilder().setProject(project).setLocation(location).setCaPool(pool_Id).setCertificate(certificateName).build();
        // Create Revoke Certificate Request and specify the appropriate revocation reason.
        RevokeCertificateRequest revokeCertificateRequest = RevokeCertificateRequest.newBuilder().setName(certificateNameParent.toString()).setReason(RevocationReason.PRIVILEGE_WITHDRAWN).build();
        // Revoke certificate.
        ApiFuture<Certificate> response = certificateAuthorityServiceClient.revokeCertificateCallable().futureCall(revokeCertificateRequest);
        Certificate certificateResponse = response.get();
        System.out.println("Certificate Revoked: " + certificateResponse.getName());
    }
}
Also used : CertificateAuthorityServiceClient(com.google.cloud.security.privateca.v1.CertificateAuthorityServiceClient) RevokeCertificateRequest(com.google.cloud.security.privateca.v1.RevokeCertificateRequest) CertificateName(com.google.cloud.security.privateca.v1.CertificateName) Certificate(com.google.cloud.security.privateca.v1.Certificate)

Example 64 with Certificate

use of com.google.cloud.security.privateca.v1.Certificate in project robovm by robovm.

the class X509CertPathImpl method getCertPathFromContentInfo.

/**
     * Extract a CertPath from a PKCS#7 {@code contentInfo} object.
     */
private static X509CertPathImpl getCertPathFromContentInfo(ContentInfo contentInfo) throws CertificateException {
    final SignedData sd = contentInfo.getSignedData();
    if (sd == null) {
        throw new CertificateException("Incorrect PKCS7 encoded form: missing signed data");
    }
    List<Certificate> certs = sd.getCertificates();
    if (certs == null) {
        certs = Collections.emptyList();
    }
    final List<X509Certificate> result = new ArrayList<X509Certificate>(certs.size());
    for (Certificate cert : certs) {
        result.add(new X509CertImpl(cert));
    }
    return new X509CertPathImpl(result, Encoding.PKCS7);
}
Also used : SignedData(org.apache.harmony.security.pkcs7.SignedData) ArrayList(java.util.ArrayList) CertificateException(java.security.cert.CertificateException) X509Certificate(java.security.cert.X509Certificate) X509Certificate(java.security.cert.X509Certificate) Certificate(org.apache.harmony.security.x509.Certificate)

Example 65 with Certificate

use of com.google.cloud.security.privateca.v1.Certificate in project jasn1 by openmuc.

the class OtherSignedNotification method decode.

public int decode(InputStream is, boolean withTag) throws IOException {
    int codeLength = 0;
    int subCodeLength = 0;
    BerTag berTag = new BerTag();
    if (withTag) {
        codeLength += tag.decodeAndCheck(is);
    }
    BerLength length = new BerLength();
    codeLength += length.decode(is);
    int totalLength = length.val;
    if (totalLength == -1) {
        subCodeLength += berTag.decode(is);
        if (berTag.tagNumber == 0 && berTag.tagClass == 0 && berTag.primitive == 0) {
            int nextByte = is.read();
            if (nextByte != 0) {
                if (nextByte == -1) {
                    throw new EOFException("Unexpected end of input stream.");
                }
                throw new IOException("Decoded sequence has wrong end of contents octets");
            }
            codeLength += subCodeLength + 1;
            return codeLength;
        }
        if (berTag.equals(NotificationMetadata.tag)) {
            tbsOtherNotification = new NotificationMetadata();
            subCodeLength += tbsOtherNotification.decode(is, false);
            subCodeLength += berTag.decode(is);
        }
        if (berTag.tagNumber == 0 && berTag.tagClass == 0 && berTag.primitive == 0) {
            int nextByte = is.read();
            if (nextByte != 0) {
                if (nextByte == -1) {
                    throw new EOFException("Unexpected end of input stream.");
                }
                throw new IOException("Decoded sequence has wrong end of contents octets");
            }
            codeLength += subCodeLength + 1;
            return codeLength;
        }
        if (berTag.equals(BerTag.APPLICATION_CLASS, BerTag.PRIMITIVE, 55)) {
            euiccNotificationSignature = new BerOctetString();
            subCodeLength += euiccNotificationSignature.decode(is, false);
            subCodeLength += berTag.decode(is);
        }
        if (berTag.tagNumber == 0 && berTag.tagClass == 0 && berTag.primitive == 0) {
            int nextByte = is.read();
            if (nextByte != 0) {
                if (nextByte == -1) {
                    throw new EOFException("Unexpected end of input stream.");
                }
                throw new IOException("Decoded sequence has wrong end of contents octets");
            }
            codeLength += subCodeLength + 1;
            return codeLength;
        }
        if (berTag.equals(Certificate.tag)) {
            euiccCertificate = new Certificate();
            subCodeLength += euiccCertificate.decode(is, false);
            subCodeLength += berTag.decode(is);
        }
        if (berTag.tagNumber == 0 && berTag.tagClass == 0 && berTag.primitive == 0) {
            int nextByte = is.read();
            if (nextByte != 0) {
                if (nextByte == -1) {
                    throw new EOFException("Unexpected end of input stream.");
                }
                throw new IOException("Decoded sequence has wrong end of contents octets");
            }
            codeLength += subCodeLength + 1;
            return codeLength;
        }
        if (berTag.equals(Certificate.tag)) {
            eumCertificate = new Certificate();
            subCodeLength += eumCertificate.decode(is, false);
            subCodeLength += berTag.decode(is);
        }
        int nextByte = is.read();
        if (berTag.tagNumber != 0 || berTag.tagClass != 0 || berTag.primitive != 0 || nextByte != 0) {
            if (nextByte == -1) {
                throw new EOFException("Unexpected end of input stream.");
            }
            throw new IOException("Decoded sequence has wrong end of contents octets");
        }
        codeLength += subCodeLength + 1;
        return codeLength;
    }
    codeLength += totalLength;
    subCodeLength += berTag.decode(is);
    if (berTag.equals(NotificationMetadata.tag)) {
        tbsOtherNotification = new NotificationMetadata();
        subCodeLength += tbsOtherNotification.decode(is, false);
        subCodeLength += berTag.decode(is);
    } else {
        throw new IOException("Tag does not match the mandatory sequence element tag.");
    }
    if (berTag.equals(BerTag.APPLICATION_CLASS, BerTag.PRIMITIVE, 55)) {
        euiccNotificationSignature = new BerOctetString();
        subCodeLength += euiccNotificationSignature.decode(is, false);
        subCodeLength += berTag.decode(is);
    } else {
        throw new IOException("Tag does not match the mandatory sequence element tag.");
    }
    if (berTag.equals(Certificate.tag)) {
        euiccCertificate = new Certificate();
        subCodeLength += euiccCertificate.decode(is, false);
        subCodeLength += berTag.decode(is);
    } else {
        throw new IOException("Tag does not match the mandatory sequence element tag.");
    }
    if (berTag.equals(Certificate.tag)) {
        eumCertificate = new Certificate();
        subCodeLength += eumCertificate.decode(is, false);
        if (subCodeLength == totalLength) {
            return codeLength;
        }
    }
    throw new IOException("Unexpected end of sequence, length tag: " + totalLength + ", actual sequence length: " + subCodeLength);
}
Also used : EOFException(java.io.EOFException) IOException(java.io.IOException) Certificate(org.openmuc.jasn1.compiler.pkix1explicit88.Certificate)

Aggregations

Certificate (org.bouncycastle.asn1.x509.Certificate)53 IOException (java.io.IOException)40 X509Certificate (java.security.cert.X509Certificate)37 CertificateException (java.security.cert.CertificateException)27 CertificateAuthorityServiceClient (com.google.cloud.security.privateca.v1.CertificateAuthorityServiceClient)24 Test (org.junit.Test)14 Operation (com.google.longrunning.Operation)13 File (java.io.File)11 BigInteger (java.math.BigInteger)9 CertificateEncodingException (java.security.cert.CertificateEncodingException)9 TBSCertificate (org.bouncycastle.asn1.x509.TBSCertificate)9 Test (org.junit.jupiter.api.Test)9 Certificate (com.google.cloud.security.privateca.v1.Certificate)8 SQLException (java.sql.SQLException)8 X500Name (org.bouncycastle.asn1.x500.X500Name)8 ASN1Sequence (org.bouncycastle.asn1.ASN1Sequence)7 Certificate (com.beanit.asn1bean.compiler.pkix1explicit88.Certificate)6 Extension (org.bouncycastle.asn1.x509.Extension)6 OperatorCreationException (org.bouncycastle.operator.OperatorCreationException)6 Date (java.util.Date)5