Example usage for java.security.cert Certificate getEncoded

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

Introduction

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

Prototype

public abstract byte[] getEncoded() throws CertificateEncodingException;

Source Link

Document

Returns the encoded form of this certificate.

Usage

From source file:org.apache.hadoop.gateway.services.security.impl.BaseKeystoreService.java

protected void writeCertificateToFile(Certificate cert, final File file)
        throws CertificateEncodingException, IOException {
    byte[] bytes = cert.getEncoded();
    final FileOutputStream out = new FileOutputStream(file);
    Base64 encoder = new Base64(76, "\n".getBytes("ASCII"));
    try {/*  w w  w  .j a  v  a2  s  .  com*/
        out.write("-----BEGIN CERTIFICATE-----\n".getBytes("ASCII"));
        out.write(encoder.encodeToString(bytes).getBytes("ASCII"));
        out.write("-----END CERTIFICATE-----\n".getBytes("ASCII"));
    } finally {
        out.close();
    }
}

From source file:org.apache.qpid.server.security.NonJavaKeyStoreTest.java

private File[] extractResourcesFromTestKeyStore(boolean pem) throws Exception {
    java.security.KeyStore ks = java.security.KeyStore.getInstance(java.security.KeyStore.getDefaultType());
    try (InputStream is = getClass().getResourceAsStream("/java_broker_keystore.jks")) {
        ks.load(is, KEYSTORE_PASSWORD.toCharArray());
    }/*  ww w. j  a v  a  2 s  . c o m*/

    File privateKeyFile = TestFileUtils.createTempFile(this, ".private-key.der");
    try (FileOutputStream kos = new FileOutputStream(privateKeyFile)) {
        Key pvt = ks.getKey("java-broker", KEYSTORE_PASSWORD.toCharArray());
        if (pem) {
            kos.write("-----BEGIN PRIVATE KEY-----\n".getBytes());
            kos.write(Base64.encodeBase64(pvt.getEncoded(), true));
            kos.write("\n-----END PRIVATE KEY-----".getBytes());
        } else {
            kos.write(pvt.getEncoded());
        }
        kos.flush();
    }

    File certificateFile = TestFileUtils.createTempFile(this, ".certificate.der");

    try (FileOutputStream cos = new FileOutputStream(certificateFile)) {
        Certificate pub = ks.getCertificate("rootca");
        if (pem) {
            cos.write("-----BEGIN CERTIFICATE-----\n".getBytes());
            cos.write(Base64.encodeBase64(pub.getEncoded(), true));
            cos.write("\n-----END CERTIFICATE-----".getBytes());
        } else {
            cos.write(pub.getEncoded());
        }
        cos.flush();
    }

    return new File[] { privateKeyFile, certificateFile };
}

From source file:org.forgerock.openidm.security.impl.KeystoreResourceProviderTest.java

private void checkKeyStoreEntry(final JsonValue result) throws UnrecoverableEntryException,
        NoSuchAlgorithmException, KeyStoreException, CertificateEncodingException {
    KeyStore.PrivateKeyEntry privateKeyEntry = (KeyStore.PrivateKeyEntry) keyStoreHandler.getStore()
            .getEntry(TEST_CERT_ALIAS, new KeyStore.PasswordProtection(KEYSTORE_PASSWORD.toCharArray()));
    assertThat(privateKeyEntry != null);

    Certificate certificate = privateKeyEntry.getCertificate();
    assertThat(certificate != null);// w ww.ja va2  s  .  c  om

    final String certAsPEM = convertCertToPEM(certificate.getEncoded());
    assertThat(certAsPEM != null || !StringUtils.isBlank(certAsPEM));
    certAsPEM.equals(result.get("cert").asString());

    PrivateKey privateKey = privateKeyEntry.getPrivateKey();
    assertThat(privateKey != null);
}

From source file:com.vmware.o11n.plugin.crypto.service.CryptoCertificateService.java

/**
 * Get SHA1 fingerprint/thumbprint of a Certificate
 * Returns the fingerprint as a colon delimited hex string
 *
 * @param cert Certificate/* w  w w  .ja  v a 2 s  . c om*/
 * @return Hex encoded sha1 fingerprint of certificate
 * @throws CertificateException
 */
public String getSha1Fingerprint(Certificate cert) throws CertificateException {
    byte[] encoded = cert.getEncoded();
    String certFinger = DigestUtils.sha1Hex(encoded);
    return fixFingerprintHex(certFinger);
}

From source file:com.vmware.o11n.plugin.crypto.service.CryptoCertificateService.java

/**
 * Get SHA256 fingerprint/thumbprint of a Certificate
 * Returns the fingerprint as a colon delimited hex string
 *
 * @param cert Certificate//w ww  .  ja  v a  2s  . co m
 * @return Hex encoded sha256 fingerprint of certificate
 * @throws CertificateException
 */
public String getSha256Fingerprint(Certificate cert) throws CertificateException {
    byte[] encoded = cert.getEncoded();
    String certFinger = DigestUtils.sha256Hex(encoded);
    return fixFingerprintHex(certFinger);
}

From source file:com.vmware.o11n.plugin.crypto.service.CryptoCertificateService.java

/**
 *
 * @param cert/*from   w w  w.j a va  2  s .co m*/
 * @return
 * @throws CertificateEncodingException
 */
public String getEncodedBase64(Certificate cert) throws CertificateEncodingException {
    byte[] encoded = cert.getEncoded();
    return Base64.encodeBase64String(encoded);
}

From source file:mitm.common.hibernate.CertificateArrayUserType.java

private Certificate cloneCertificate(Certificate original)
        throws CertificateException, NoSuchProviderException, SecurityFactoryFactoryException {
    byte[] encodedCert = original.getEncoded();

    String certificateType = original.getType();

    CertificateFactory factory = SecurityFactoryFactory.getSecurityFactory()
            .createCertificateFactory(certificateType);

    Certificate copy = factory.generateCertificate(new ByteArrayInputStream(encodedCert));

    return copy;/*from  w w w .  j  av a  2s  .  co  m*/
}

From source file:com.adaptris.security.TestCertificateGeneration.java

public void testEncodedCertificateToKeystore() throws Exception {
    String commonName = String.valueOf(random.nextInt(1000));
    CertificateBuilder builder = Config.getInstance().getBuilder(commonName);
    Certificate selfCert = builder.createSelfSignedCertificate();
    builder.getPrivateKey();//from  www . j a va 2 s. c om
    ksp = KeystoreFactory.getDefault().create(ksc);
    try {
        ksp.load();
    } catch (Exception e) {
        ;
    }

    ksp.setCertificate(commonName, new ByteArrayInputStream(selfCert.getEncoded()));
    ksp.commit();
}

From source file:org.codice.ddf.admin.insecure.defaults.service.KeystoreValidator.java

private boolean areCertificatesEqual(Certificate certificate, Certificate blacklistedCertificate)
        throws CertificateEncodingException {
    return Arrays.equals(certificate.getEncoded(), blacklistedCertificate.getEncoded());
}

From source file:test.integ.be.fedict.hsm.jca.HSMProxySignatureTest.java

@Test
public void testWriteAuthCertToFile() throws Exception {
    Security.addProvider(new BeIDProvider());
    KeyStore keyStore = KeyStore.getInstance("BeID");
    keyStore.load(null);/*from w w  w.  j  a va  2  s.c o  m*/
    Certificate authnCert = keyStore.getCertificate("Authentication");
    LOG.debug("authn cert: " + authnCert);
    File tmpFile = File.createTempFile("eid-authn-cert-", ".der");
    FileUtils.writeByteArrayToFile(tmpFile, authnCert.getEncoded());
    LOG.debug("tmp authn cert file: " + tmpFile.getAbsolutePath());
}