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.ejbca.core.protocol.cmp.CmpMessageHelper.java

public static byte[] signPKIMessage(PKIMessage myPKIMessage, Collection<Certificate> signCertChain,
        PrivateKey signKey, String digestAlg, String provider)
        throws InvalidKeyException, NoSuchProviderException, NoSuchAlgorithmException, SecurityException,
        SignatureException, CertificateEncodingException {
    if (LOG.isTraceEnabled()) {
        LOG.trace(">signPKIMessage()");
    }/*  www . java 2  s .com*/
    CMPCertificate[] extraCerts = new CMPCertificate[signCertChain.size()];
    Iterator<Certificate> itr = signCertChain.iterator();
    int i = 0;
    while (itr.hasNext()) {
        X509Certificate tmp = (X509Certificate) itr.next();
        ASN1InputStream asn1InputStream = null;
        try {
            try {
                asn1InputStream = new ASN1InputStream(new ByteArrayInputStream(tmp.getEncoded()));
                CMPCertificate signStruct = CMPCertificate.getInstance(asn1InputStream.readObject());
                extraCerts[i] = signStruct;
            } finally {
                asn1InputStream.close();
            }
        } catch (IOException e) {
            throw new IllegalStateException("Caught unexpected IOException", e);
        }
        i++;
    }
    myPKIMessage = CmpMessageHelper.buildCertBasedPKIProtection(myPKIMessage, extraCerts, signKey, digestAlg,
            provider);
    if (LOG.isTraceEnabled()) {
        LOG.trace("<signPKIMessage()");
    }
    // Return response as byte array 
    return CmpMessageHelper.pkiMessageToByteArray(myPKIMessage);

}

From source file:org.apache.nifi.registry.security.util.CertificateUtils.java

/**
 * Accepts a legacy {@link javax.security.cert.X509Certificate} and returns an {@link X509Certificate}. The {@code javax.*} package certificate classes are for legacy compatibility and should
 * not be used for new development.//from w w  w .  java2  s  .  c  o m
 *
 * @param legacyCertificate the {@code javax.security.cert.X509Certificate}
 * @return a new {@code java.security.cert.X509Certificate}
 * @throws CertificateException if there is an error generating the new certificate
 */
public static X509Certificate convertLegacyX509Certificate(
        javax.security.cert.X509Certificate legacyCertificate) throws CertificateException {
    if (legacyCertificate == null) {
        throw new IllegalArgumentException("The X.509 certificate cannot be null");
    }

    try {
        return formX509Certificate(legacyCertificate.getEncoded());
    } catch (javax.security.cert.CertificateEncodingException e) {
        throw new CertificateException(e);
    }
}

From source file:de.duenndns.ssl.MemorizingTrustManager.java

private static String certHash(final X509Certificate cert, String digest) {
    try {//from  w  ww  .j  a va 2  s. co m
        MessageDigest md = MessageDigest.getInstance(digest);
        md.update(cert.getEncoded());
        return hexString(md.digest());
    } catch (java.security.cert.CertificateEncodingException e) {
        return e.getMessage();
    } catch (java.security.NoSuchAlgorithmException e) {
        return e.getMessage();
    }
}

From source file:nu.yona.server.subscriptions.rest.AppleMobileConfigSigner.java

private X509CertificateHolder inHolder(X509Certificate certificate) {
    try {/*  ww  w  . j a  va 2s.  co m*/
        return new X509CertificateHolder(certificate.getEncoded());
    } catch (CertificateEncodingException | IOException e) {
        throw YonaException.unexpected(e);
    }
}

From source file:de.duenndns.ssl.MemorizingTrustManager.java

private static String getBase64Hash(X509Certificate certificate, String digest)
        throws CertificateEncodingException {
    MessageDigest md;/*  w  ww  . j  a  v a 2 s  . com*/
    try {
        md = MessageDigest.getInstance(digest);
    } catch (NoSuchAlgorithmException e) {
        return null;
    }
    md.update(certificate.getEncoded());
    return Base64.encodeToString(md.digest(), Base64.NO_WRAP);
}

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

private String getFingerprint(X509Certificate certificate) {
    byte[] encodedCertificate;
    try {/*from w  ww  . ja  v a 2 s.c  o  m*/
        encodedCertificate = certificate.getEncoded();
    } catch (CertificateEncodingException e) {
        throw new IllegalArgumentException("certificate encoding error: " + e.getMessage(), e);
    }
    String fingerprint = DigestUtils.shaHex(encodedCertificate);
    return fingerprint;
}

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

private String getFingerprint(X509Certificate certificate) {
    byte[] encodedCertificate;
    try {/*w  w  w . ja v a  2s .  c  o  m*/
        encodedCertificate = certificate.getEncoded();
    } catch (CertificateEncodingException e) {
        throw new IllegalArgumentException("certificate encoding error: " + e.getMessage(), e);
    }
    String fingerprint = DigestUtils.sha1Hex(encodedCertificate);
    return fingerprint;
}

From source file:test.unit.be.fedict.eid.tsl.FingerprintTest.java

@Test
public void testECFingerprint() throws Exception {
    // setup// w  ww . java2  s.c  om
    Document euTSLDocument = TrustTestUtils.loadDocumentFromResource("eu/tl-mp-2.xml");
    TrustServiceList euTSL = TrustServiceListFactory.newInstance(euTSLDocument);
    X509Certificate euCertificate = euTSL.verifySignature();

    // operate
    LOG.debug("EC SHA-1 fingerprint: " + DigestUtils.shaHex(euCertificate.getEncoded()));
    LOG.debug("EC SHA-256 fingerprint: " + DigestUtils.sha256Hex(euCertificate.getEncoded()));
}

From source file:test.unit.be.fedict.eid.tsl.FingerprintTest.java

@Test
public void testNewECFingerprint() throws Exception {
    // setup/*  w w  w  .j  a  va 2  s . com*/
    Document euTSLDocument = TrustTestUtils.loadDocumentFromResource("eu/tl-mp-33.xml");
    TrustServiceList euTSL = TrustServiceListFactory.newInstance(euTSLDocument);
    X509Certificate euCertificate = euTSL.verifySignature();

    // operate
    LOG.debug("EC SHA-1 fingerprint: " + DigestUtils.shaHex(euCertificate.getEncoded()));
    LOG.debug("EC SHA-256 fingerprint: " + DigestUtils.sha256Hex(euCertificate.getEncoded()));
}

From source file:be.fedict.hsm.ws.impl.JAASSOAPHandler.java

private void login(SOAPMessageContext context) throws LoginException, CertificateEncodingException {
    X509Certificate certificate = WSSecuritySOAPHandler.getAuthenticatedCertificate(context);
    byte[] encodedCertificate = certificate.getEncoded();
    NamePasswordCallbackHandler usernamePasswordHandler = new NamePasswordCallbackHandler(encodedCertificate);
    LoginContext loginContext = new LoginContext(ApplicationClientSecurityDomain.NAME, usernamePasswordHandler);
    context.put(LOGIN_CONTEXT_ATTRIBUTE, loginContext);
    loginContext.login();//from w  ww.  ja  va2  s  . co m
}