Search in sources :

Example 1 with CertException

use of com.adaptris.security.exc.CertException in project interlok by adaptris.

the class TestCertificateHandler method testExpiredCertificateRevocationCache.

@Test
public void testExpiredCertificateRevocationCache() throws Exception {
    try {
        InputStream input = new FileInputStream(config.getProperties().getProperty(Config.CERTHANDLER_EXPIRED));
        CertificateHandler handler = CertificateHandlerFactory.getInstance().generateHandler(input);
        input.close();
        assertEquals("Revocation", handler.isRevoked(), false);
        Calendar lastExpiredCheck = handler.getLastRevocationCheck();
        assertEquals("Calendar", handler.getLastRevocationCheck(), lastExpiredCheck);
    } catch (CertException e) {
        if (!(e.getCause() != null && e.getCause() instanceof UnknownHostException)) {
            throw e;
        }
    }
}
Also used : UnknownHostException(java.net.UnknownHostException) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) Calendar(java.util.Calendar) CertException(com.adaptris.security.exc.CertException) CertificateHandler(com.adaptris.security.certificate.CertificateHandler) FileInputStream(java.io.FileInputStream) Test(org.junit.Test)

Example 2 with CertException

use of com.adaptris.security.exc.CertException in project interlok by adaptris.

the class TestCertificateHandler method testExpiredCertificateRevocation.

@Test
public void testExpiredCertificateRevocation() throws Exception {
    try {
        InputStream input = new FileInputStream(config.getProperties().getProperty(Config.CERTHANDLER_EXPIRED));
        CertificateHandler handler = CertificateHandlerFactory.getInstance().generateHandler(input);
        input.close();
        assertEquals("Revocation", handler.isRevoked(), false);
        assertNotNull(handler.getLastRevocationCheck());
    } catch (CertException e) {
        if (!(e.getCause() != null && e.getCause() instanceof UnknownHostException)) {
            throw e;
        }
    }
}
Also used : UnknownHostException(java.net.UnknownHostException) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) CertException(com.adaptris.security.exc.CertException) CertificateHandler(com.adaptris.security.certificate.CertificateHandler) FileInputStream(java.io.FileInputStream) Test(org.junit.Test)

Example 3 with CertException

use of com.adaptris.security.exc.CertException in project interlok by adaptris.

the class KeystoreProxyImp method importCertificateChain.

/**
 * Import a certificate chain from a file, giving it the assigned alias.
 * <p>
 * This deals with certificate chains as used by Netscape Navigator and
 * Microsoft Internet Explorer.
 * <p>
 * Certificate Chains are only appropriate for keystore <code>keyEntry</code>
 * types.
 * <p>
 * This assumes that a <code>keyEntry</code> with the alias
 * <code>alias</code> has already been created, and the secret key
 * associated with this <code>keyEntry</code> is protected by
 * <code>keyPassword</code>
 *
 * @param keyPassword the password to access the private key
 * @param alias the alias to be assigned
 * @param in the Certificate Chain file to be imported
 * @throws AdaptrisSecurityException for any error
 * @see #setPrivateKey(String, PrivateKey, char[], Certificate[])
 */
public void importCertificateChain(String alias, char[] keyPassword, InputStream in) throws AdaptrisSecurityException {
    try (PemReader pemReader = new PemReader(new InputStreamReader(in))) {
        // ,, Constants.SECURITY_PROVIDER);
        CertificateFactory cf = CertificateFactory.getInstance(Constants.KEYSTORE_X509);
        Collection<?> certs = cf.generateCertificates(in);
        Certificate[] pkcs7b = certs.toArray(new Certificate[0]);
        PrivateKey pkey = this.getPrivateKey(alias, keyPassword);
        if (pkey == null) {
            throw new Exception("No Private key for alias " + alias);
        }
        this.setPrivateKey(alias, pkey, keyPassword, pkcs7b);
    } catch (AdaptrisSecurityException e) {
        throw e;
    } catch (Exception e) {
        throw new CertException(e.getMessage(), e);
    }
}
Also used : PemReader(org.bouncycastle.util.io.pem.PemReader) PrivateKey(java.security.PrivateKey) InputStreamReader(java.io.InputStreamReader) AdaptrisSecurityException(com.adaptris.security.exc.AdaptrisSecurityException) CertException(com.adaptris.security.exc.CertException) CertificateFactory(java.security.cert.CertificateFactory) KeystoreException(com.adaptris.security.exc.KeystoreException) IOException(java.io.IOException) CertException(com.adaptris.security.exc.CertException) AdaptrisSecurityException(com.adaptris.security.exc.AdaptrisSecurityException) GeneralSecurityException(java.security.GeneralSecurityException) X509Certificate(java.security.cert.X509Certificate) Certificate(java.security.cert.Certificate)

Example 4 with CertException

use of com.adaptris.security.exc.CertException in project interlok by adaptris.

the class CertRequestHandler method createRequest.

/**
 * Create a certificate request based on the provided certificate.
 * <p>
 * The certificate request is returned as a printable string.
 * <p>
 * The default implementation returns a PEM string which is bounded by <code>-----BEGIN NEW CERTIFICATE REQUEST-----</code> and
 * <code>-----END NEW CERTIFICATE REQUEST-----</code> with each line terminated by a \n, e.g.
 * <p>
 *
 * <pre>
 * {@code
 * -----BEGIN NEW CERTIFICATE REQUEST-----
 * MIIC1zCCAb8CAQAwgZMxGTAXBgNVBAMTEEdoaWxhaW5lIFd5bnlhcmQxFTATBgNV
 * BAsTDElyaXMgU3VwcG9ydDENMAsGA1UEChMESXJpczEQMA4GA1UEBxMHRGF0Y2hl
 * dDESMBAGA1UECBMJQmVya3NoaXJlMQswCQYDVQQGEwJVSzEdMBsGCSqGSIb3DQEJ
 * ARYOZ2x3QGlyaXMuY28udWswggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIB
 * AQCRQMSX5HCu25aQ+dtozxoGP1QtYC6JXuyGQkQ2MX2JsF1y+WM4NWUvhZvCKjmg
 * jsQL2c/rBzXifZCDRzMuHymOcmWXBaFJJr3b2XZQykzhKZKvTx6X1oU7PPcf+ws0
 * fNRQ3c1ZekPRYTUmSKyswJwIJDmpx4folU348taHwqcnK/LizekgjwrcZwSmFkk6
 * zhGTji2Ris4NMGvm/gDPGGmstxXTQIT62zOP1c61IhkKqxTtMZqmuB2HgQ8MRAza
 * xAxm7uoGbSCgUrWOjb92BHYxvQw8LKKkqKmKWRmDPPQKlBP4iR7vPUqXlb4G/keN
 * jR2EdByCJbkCUCwuVU124lJbAgMBAAEwDQYJKoZIhvcNAQEEBQADggEBADHpHomV
 * ZItNOeXjBDQGWNX9SpA9QV5IvEzm4u5mRI+XFsgYXvybeLMYC6Vrpxl9INVI6hpx
 * Nblq09Cq8lHQusyJNVEW3ibc73T5OZrCSnPTD7DUoKpwLwkDOwze7NHu+7NceUm1
 * pHCdiVe9Q3AC3+qlIdOhXGB3L5/Tn+8rYFNMAV8TQl7yRAz0g4lm+CdXQWaozTLY
 * /1MifSidgYoOq2lCE6l6JsVUv7mBTgaA52GuL0XvfopMOJrEuVUfFy7xVZMqD92L
 * ThcZUaIq5/Z3PUGLi8txXXb0Ga81SkLAHpBljgED0pV06EsrDz/N+12aH75zDcE+
 * Odt/GdYzfpeYvBg=
 * -----END NEW CERTIFICATE REQUEST-----
 * }
 *  </pre>
 *
 * @param c the certificate
 * @param key the private key
 * @return the PEM encoded string
 * @throws AdaptrisSecurityException if any error occurs
 * @see AdaptrisSecurityException
 */
public static String createRequest(Certificate c, PrivateKey key) throws AdaptrisSecurityException {
    String pemRequest = null;
    ByteArrayOutputStream out = null;
    try {
        CertificationRequest req = createCertRequest(c, key);
        out = new ByteArrayOutputStream();
        out.write("-----BEGIN NEW CERTIFICATE REQUEST-----".getBytes());
        out.write(req.getEncoded());
        out.write("-----END NEW CERTIFICATE REQUEST-----".getBytes());
        out.close();
        pemRequest = out.toString();
    } catch (Exception e) {
        throw new CertException(e);
    }
    return pemRequest;
}
Also used : CertException(com.adaptris.security.exc.CertException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) PKCS10CertificationRequest(org.bouncycastle.pkcs.PKCS10CertificationRequest) CertificationRequest(org.bouncycastle.asn1.pkcs.CertificationRequest) CertException(com.adaptris.security.exc.CertException) AdaptrisSecurityException(com.adaptris.security.exc.AdaptrisSecurityException)

Example 5 with CertException

use of com.adaptris.security.exc.CertException in project interlok by adaptris.

the class CertRequestHandler method createRequest.

/**
 * Create a CertificateRequest based on the provided certificate and write it
 * to the supplied outputStream.
 * <p>
 * The default implementation writes out the request as a DER encoded ASN.1
 * data structure
 *
 * @param c the certificate
 * @param out the OutputStream to write to
 * @param key the Private key
 * @throws AdaptrisSecurityException if any error occurs
 * @see AdaptrisSecurityException
 */
public static void createRequest(Certificate c, PrivateKey key, OutputStream out) throws AdaptrisSecurityException {
    try {
        CertificationRequest req = createCertRequest(c, key);
        out.write(req.getEncoded());
    } catch (Exception e) {
        throw new CertException(e);
    }
}
Also used : CertException(com.adaptris.security.exc.CertException) PKCS10CertificationRequest(org.bouncycastle.pkcs.PKCS10CertificationRequest) CertificationRequest(org.bouncycastle.asn1.pkcs.CertificationRequest) CertException(com.adaptris.security.exc.CertException) AdaptrisSecurityException(com.adaptris.security.exc.AdaptrisSecurityException)

Aggregations

CertException (com.adaptris.security.exc.CertException)6 AdaptrisSecurityException (com.adaptris.security.exc.AdaptrisSecurityException)4 CertificateHandler (com.adaptris.security.certificate.CertificateHandler)2 KeystoreException (com.adaptris.security.exc.KeystoreException)2 FileInputStream (java.io.FileInputStream)2 IOException (java.io.IOException)2 InputStream (java.io.InputStream)2 UnknownHostException (java.net.UnknownHostException)2 GeneralSecurityException (java.security.GeneralSecurityException)2 PrivateKey (java.security.PrivateKey)2 Certificate (java.security.cert.Certificate)2 X509Certificate (java.security.cert.X509Certificate)2 CertificationRequest (org.bouncycastle.asn1.pkcs.CertificationRequest)2 PKCS10CertificationRequest (org.bouncycastle.pkcs.PKCS10CertificationRequest)2 Test (org.junit.Test)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 InputStreamReader (java.io.InputStreamReader)1 Key (java.security.Key)1 KeyStore (java.security.KeyStore)1 CertificateFactory (java.security.cert.CertificateFactory)1