Example usage for java.security Certificate getPublicKey

List of usage examples for java.security Certificate getPublicKey

Introduction

In this page you can find the example usage for java.security Certificate getPublicKey.

Prototype

public abstract PublicKey getPublicKey();

Source Link

Document

Returns the key of the principal-key pair being guaranteed by the guarantor.

Usage

From source file:cloudeventbus.pki.CertificateUtils.java

public static Certificate signCertificate(Certificate issuer, PrivateKey issuerPrivateKey,
        Certificate certificate) {
    if (issuer.getSerialNumber() != certificate.getIssuer()) {
        throw new CertificateIssuerMismatchException(
                "The authority certificate serial number doesn't much the certificate issuer.");
    }//from  ww  w . j  a  v  a2 s . c o m
    validatePermissions(issuer, certificate);
    final byte[] hash = certificate.hash();

    try {
        Cipher cipher = Cipher.getInstance("RSA");
        cipher.init(Cipher.ENCRYPT_MODE, issuerPrivateKey);
        final byte[] signature = cipher.doFinal(hash);
        return new Certificate(certificate.getType(), certificate.getSerialNumber(), certificate.getIssuer(),
                certificate.getExpirationDate(), certificate.getPublicKey(),
                certificate.getSubscribePermissions(), certificate.getPublishPermissions(),
                certificate.getComment(), signature);
    } catch (GeneralSecurityException e) {
        throw new CertificateSecurityException(e);
    }
}