Example usage for java.security.cert CertificateException CertificateException

List of usage examples for java.security.cert CertificateException CertificateException

Introduction

In this page you can find the example usage for java.security.cert CertificateException CertificateException.

Prototype

public CertificateException(Throwable cause) 

Source Link

Document

Creates a CertificateException with the specified cause and a detail message of (cause==null ?

Usage

From source file:Main.java

private static boolean verifyCertificateValidity(X509Certificate certificate) throws CertificateException {
    if (certificate != null) {
        if (certificate.getNotAfter().compareTo(new Date(System.currentTimeMillis())) >= 0) {
            return true;
        }//from w  ww . java  2 s . c  o  m
        throw new CertificateException("Certificate has expired and is not valid for signing document.");
    }
    return false;
}

From source file:org.xdi.oxauth.crypto.cert.CertificateParser.java

public static X509Certificate parsePem(String pemEncodedCert) throws CertificateException {
    StringReader sr = new StringReader(pemEncodedCert);
    PEMReader pemReader = new PEMReader(sr);
    try {//from   w  w  w  . j  a v a 2 s .c  o m
        X509Certificate cert = (X509CertificateObject) pemReader.readObject();

        return cert;
    } catch (IOException ex) {
        throw new CertificateException(ex);
    } finally {
        IOUtils.closeQuietly(pemReader);
    }
}

From source file:Main.java

/**
 * Generate the fingerprint for a dedicated type.
 * @param cert the certificate//from   ww w .  j av  a  2s .c om
 * @param type the type
 * @return the fingerprint
 * @throws CertificateException
 */
private static byte[] generateFingerprint(X509Certificate cert, String type) throws CertificateException {
    final byte[] fingerprint;
    final MessageDigest md;
    try {
        md = MessageDigest.getInstance(type);
    } catch (Exception e) {
        // This really *really* shouldn't throw, as java should always have a SHA-256 and SHA-1 impl.
        throw new CertificateException(e);
    }

    fingerprint = md.digest(cert.getEncoded());

    return fingerprint;
}

From source file:org.xdi.oxauth.crypto.cert.CertificateParser.java

public static X509Certificate parseDer(InputStream is) throws CertificateException {
    try {/*from  w  ww.  j ava 2 s  .  com*/
        return (X509Certificate) CertificateFactory.getInstance("X.509", "BC").generateCertificate(is);
    } catch (NoSuchProviderException ex) {
        throw new CertificateException(ex);
    }
}

From source file:org.wso2.carbon.identity.authenticator.inbound.saml2sso.util.Utils.java

/**
 * TODO: ideally this method must be in identity.commons. However the one in identity.commons uses
 * TODO: java.util.Base64 which doesn't work here. Only the OpenSAML Base64 decoder works. Until then duplicating
 * TODO: this method.// w w  w.  j  a va2 s  .  co  m
 * Decode X509 certificate.
 *
 * @param encodedCert Base64 encoded certificate
 * @return Decoded <code>Certificate</code>
 * @throws java.security.cert.CertificateException Error when decoding certificate
 */
public static Certificate decodeCertificate(String encodedCert) throws CertificateException {

    if (StringUtils.isNotBlank(encodedCert)) {
        byte[] bytes = Base64.decode(encodedCert);
        if (bytes == null || bytes.length == 0) {
            throw new CertificateException("Encoded certificate is invalid: " + encodedCert);
        }
        CertificateFactory factory = CertificateFactory.getInstance("X.509");
        X509Certificate cert = (X509Certificate) factory.generateCertificate(new ByteArrayInputStream(bytes));
        return cert;
    } else {
        throw new CertificateException("Encoded certificate is empty: " + encodedCert);
    }
}

From source file:se.inera.axel.shs.broker.validation.certificate.PemConverter.java

/**
 * Converts a PEM formatted certificate to a X509Certificate.
 * //from w ww  .  j  av  a2  s  .com
 * @param pemCertificate the PEM certificat that should get converted.
 * 
 * @return the X509Certificate.
 * 
 * @throws CertificateException
 */
public static X509Certificate convertPemToX509Certificate(String pemCertificate) throws CertificateException {
    if (containsCorrectPemHeaders(pemCertificate)) {
        String certificateInfo = extractCertificate(pemCertificate);
        X509Certificate certificate = generateX509Certificate(certificateInfo);
        return (X509Certificate) certificate;
    } else {
        throw new CertificateException("PEM headers missing in certificate.");
    }
}

From source file:com.dtolabs.rundeck.jetty.jaas.HostnameVerifyingTrustManager.java

@Override
public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
    if (chain.length > 0) {
        X509Certificate serverCert = chain[0];
        String host = HostnameVerifyingSSLSocketFactory.getTargetHost();
        try {//w ww.  ja v  a 2 s. co  m
            verifier.check(host, serverCert);
        } catch (SSLException e) {
            throw new CertificateException(e);
        }
    }
    realTrustManager.checkServerTrusted(chain, authType);
}

From source file:se.inera.axel.shs.broker.validation.certificate.CertificateExtractorImpl.java

@Override
public String extractSender(Object certificate) throws CertificateException {

    if (certificate instanceof X509Certificate) {
        return extractSenderFromX509Certificate(certificate);
    } else if (certificate instanceof String && PemConverter.containsCorrectPemHeaders((String) certificate)) {
        return extractSenderFromPemCertificate(certificate);
    } else {//  www . j av a  2  s  .  c o  m
        log.error("Unknown certificate type found");
        throw new CertificateException("Unknown certificate type found");
    }
}

From source file:dk.nversion.jwt.CryptoUtils.java

public static PublicKey loadCertificate(String filename) throws FileNotFoundException, IOException,
        InvalidKeySpecException, NoSuchAlgorithmException, CertificateException {
    PublicKey key = null;/*from w  w  w . j  a  va 2 s.c  o m*/
    InputStream is = null;
    try {
        is = new FileInputStream(filename);
        BufferedReader br = new BufferedReader(new InputStreamReader(is));
        StringBuilder builder = new StringBuilder();
        boolean inKey = false;

        String line;
        while ((line = br.readLine()) != null) {
            if (!inKey) {
                if (line.startsWith("-----BEGIN CERTIFICATE-----")) {
                    inKey = true;
                }
            } else {
                if (line.startsWith("-----END CERTIFICATE-----")) {
                    break;
                }
                builder.append(line);
            }
        }

        if (builder.length() == 0) {
            throw new CertificateException("Did not find a certificate in the file");
        }

        byte[] encoded = Base64.decodeBase64(builder.toString());
        CertificateFactory cf = CertificateFactory.getInstance("X.509");
        X509Certificate certificate = (X509Certificate) cf
                .generateCertificate(new ByteArrayInputStream(encoded));
        key = certificate.getPublicKey();

    } finally {
        if (is != null) {
            try {
                is.close();
            } catch (IOException ex) {
                // Ignore
            }
        }
    }
    return key;
}

From source file:me.figo.internal.FigoTrustManager.java

@Override
public void checkServerTrusted(X509Certificate[] certs, String authType) throws CertificateException {
    if (certs.length == 0) {
        throw new CertificateException("No certificate found");
    } else {//from   www  .  j ava  2 s  .c o  m
        String thumbprint = getThumbPrint(certs[0]);
        if (!VALID_FINGERPRINTS.contains(thumbprint) && !this.getFingerprintsFromEnv().contains(thumbprint)) {
            throw new CertificateException();
        }
    }
}