Example usage for java.security.cert X509Certificate getEncoded

List of usage examples for java.security.cert X509Certificate getEncoded

Introduction

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

Prototype

public abstract byte[] getEncoded() throws CertificateEncodingException;

Source Link

Document

Returns the encoded form of this certificate.

Usage

From source file:org.tolven.key.bean.UserKeyBean.java

@Override
public String getUserX509CertificateString(String uid, String realm) {
    X509Certificate x509Certificate = getUserX509Certificate(uid, realm);
    if (x509Certificate == null) {
        return null;
    }/*w w w.  ja va2  s .  c  om*/
    try {
        StringBuffer buff = new StringBuffer();
        buff.append("-----BEGIN CERTIFICATE-----");
        buff.append("\n");
        String pemFormat = new String(Base64.encodeBase64Chunked(x509Certificate.getEncoded()));
        buff.append(pemFormat);
        buff.append("\n");
        buff.append("-----END CERTIFICATE-----");
        buff.append("\n");
        return buff.toString();
    } catch (Exception ex) {
        throw new RuntimeException("Could not convert X509Certificate into a String", ex);
    }
}

From source file:test.integ.be.agiv.security.PKCS12Test.java

@Test
public void testLoadPKCS12() throws Exception {
    Config config = new Config();
    String pkcs12Path = config.getPKCS12Path();
    String pkcs12Password = config.getPKCS12Password();

    InputStream pkcs12InputStream = new FileInputStream(pkcs12Path);
    assertNotNull(pkcs12InputStream);

    LOG.debug("loading PKCS12 keystore");
    KeyStore keyStore = KeyStore.getInstance("PKCS12");
    keyStore.load(pkcs12InputStream, pkcs12Password.toCharArray());

    Enumeration<String> aliases = keyStore.aliases();
    while (aliases.hasMoreElements()) {
        String alias = aliases.nextElement();
        LOG.debug("alias: " + alias);
        X509Certificate certificate = (X509Certificate) keyStore.getCertificate(alias);
        LOG.debug("certificate: " + certificate);
        PrivateKey privateKey = (PrivateKey) keyStore.getKey(alias, pkcs12Password.toCharArray());
        LOG.debug("private key algo: " + privateKey.getAlgorithm());
        assertEquals("RSA", privateKey.getAlgorithm());
        LOG.debug("certificate fingerprint: " + DigestUtils.shaHex(certificate.getEncoded()));
    }//from   w  ww  .  jav a2s .c om
}

From source file:at.gv.egiz.bku.slcommands.impl.cms.Signature.java

private void createSignerInfo(X509Certificate signingCertificate)
        throws CertificateEncodingException, CertificateException {
    iaik.x509.X509Certificate sigcert = new iaik.x509.X509Certificate(signingCertificate.getEncoded());
    CertificateIdentifier signerIdentifier = new IssuerAndSerialNumber(sigcert);
    PrivateKey privateKey = new STALPrivateKey(signatureAlgorithmURI, digestAlgorithmURI);
    signerInfo = new SignerInfo(signerIdentifier, digestAlgorithm, signatureAlgorithm, privateKey);
}

From source file:edu.duke.cabig.c3pr.web.security.SecureWebServiceHandler.java

/**
 * Store the certificate in a temporary file for further investigation.
 * //from  w w w  .  j a va2  s . co  m
 * @param cert
 */
private void store(X509Certificate cert, boolean binary) {
    try {
        byte[] buf = cert.getEncoded();
        final File file = new File(SystemUtils.JAVA_IO_TMPDIR,
                "Certificate_" + System.currentTimeMillis() + ".crt");
        log.error("Storing X.509 certificate in the file: " + file.getCanonicalPath());
        OutputStream os = new FileOutputStream(file);
        if (binary) {
            os.write(buf);
            os.flush();
        } else {
            Writer wr = new OutputStreamWriter(os, Charset.forName("UTF-8"));
            wr.write("-----BEGIN CERTIFICATE-----\n");
            wr.write(new sun.misc.BASE64Encoder().encode(buf));
            wr.write("\n-----END CERTIFICATE-----\n");
            wr.flush();
        }
        os.close();
    } catch (Exception e) {
        log.info(e.toString(), e);
    }
}

From source file:com.subgraph.vega.internal.http.proxy.ssl.CertificateCreator.java

private String createPemCertificate(X509Certificate certificate) throws CertificateException {
    final StringBuilder sb = new StringBuilder();
    final Base64 b64 = new Base64(64);

    sb.append("-----BEGIN CERTIFICATE-----\r\n");
    sb.append(b64.encodeToString(certificate.getEncoded()));
    sb.append("-----END CERTIFICATE-----\r\n");
    return sb.toString();
}

From source file:org.codice.ddf.security.ocsp.checker.OcspChecker.java

/**
 * Converts a {@link java.security.cert.X509Certificate} to a {@link Certificate}.
 *
 * @param cert - the X509Certificate to convert.
 * @return a {@link Certificate}.//from   ww w .j  a v  a  2s. c  o  m
 * @throws OcspCheckerException after posting an alert to the admin console, if any error occurs.
 */
@VisibleForTesting
Certificate convertToBouncyCastleCert(X509Certificate cert) throws OcspCheckerException {
    try {
        byte[] data = cert.getEncoded();
        return Certificate.getInstance(data);
    } catch (CertificateEncodingException e) {
        throw new OcspCheckerException(
                "Unable to convert X509 certificate to a Bouncy Castle certificate." + NOT_VERIFIED_MSG, e);
    }
}

From source file:com.tremolosecurity.scalejs.KubectlTokenLoader.java

private String cert2pem(String certificateName) {
    X509Certificate cert = GlobalEntries.getGlobalEntries().getConfigManager().getCertificate(certificateName);
    if (cert == null) {
        return null;
    } else {//from   ww  w  .j  a v  a2  s . c  om
        Base64 encoder = new Base64(64);
        StringBuffer b = new StringBuffer();
        b.append("-----BEGIN CERTIFICATE-----\n");
        try {
            b.append(encoder.encodeAsString(cert.getEncoded()));
        } catch (CertificateEncodingException e) {
            logger.warn("Could not decode certificate", e);
            return null;
        }
        b.append("-----END CERTIFICATE-----");
        return b.toString();
    }

}

From source file:be.fedict.trust.MemoryCertificateRepository.java

public boolean isTrustPoint(X509Certificate certificate) {
    String fingerprint = getFingerprint(certificate);
    X509Certificate trustPoint = this.trustPoints.get(fingerprint);
    if (null == trustPoint) {
        return false;
    }//from w  w w  .j  av  a 2s.c  o  m
    try {
        /*
         * We cannot used certificate.equals(trustPoint) here as the
         * certificates might be loaded by different security providers.
         */
        return Arrays.equals(certificate.getEncoded(), trustPoint.getEncoded());
    } catch (CertificateEncodingException e) {
        throw new IllegalArgumentException("certificate encoding error: " + e.getMessage(), e);
    }
}

From source file:org.opensaml.security.credential.criteria.impl.EvaluableX509DigestCredentialCriterion.java

/** {@inheritDoc} */
@Nullable/*from  ww w  . ja  v  a  2 s  .  c  om*/
public boolean apply(@Nullable final Credential target) {
    if (target == null) {
        log.error("Credential target was null");
        return isNullInputSatisfies();
    } else if (!(target instanceof X509Credential)) {
        log.info("Credential is not an X509Credential, does not satisfy X.509 digest criteria");
        return false;
    }

    X509Certificate entityCert = ((X509Credential) target).getEntityCertificate();
    if (entityCert == null) {
        log.info("X509Credential did not contain an entity certificate, does not satisfy criteria");
        return false;
    }

    try {
        MessageDigest hasher = MessageDigest.getInstance(algorithm);
        byte[] hashed = hasher.digest(entityCert.getEncoded());
        return Arrays.equals(hashed, x509digest);
    } catch (CertificateEncodingException e) {
        log.error("Unable to encode certificate for digest operation", e);
    } catch (NoSuchAlgorithmException e) {
        log.error("Unable to obtain a digest implementation for algorithm {" + algorithm + "}", e);
    }

    return isUnevaluableSatisfies();
}