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.opendaylight.aaa.cert.impl.ODLMdsalKeyTool.java

public String getCertificate(final KeyStore keyStore, final String keyStorePwd, final String certAlias,
        final boolean withTag) {
    try {/*  ww  w  . j  a v a  2s. com*/
        if (keyStore.containsAlias(certAlias)) {
            final X509Certificate odlCert = (X509Certificate) keyStore.getCertificate(certAlias);
            final String cert = DatatypeConverter.printBase64Binary(odlCert.getEncoded());
            if (withTag) {
                final StringBuilder sb = new StringBuilder();
                sb.append(KeyStoreConstant.BEGIN_CERTIFICATE);
                sb.append("\n");
                sb.append(cert);
                sb.append("\n");
                sb.append(KeyStoreConstant.END_CERTIFICATE);
                return sb.toString();
            }
            return cert;
        }
        LOG.info("KeyStore does not contain alias {}", certAlias);
        return null;
    } catch (final CertificateException | KeyStoreException e) {
        LOG.error("Failed to get Certificate", e);
        return null;
    }
}

From source file:ee.ria.xroad.common.conf.globalconf.SharedParameters.java

X509Certificate getCaCertForSubject(X509Certificate subject) throws Exception {
    X509CertificateHolder certHolder = new X509CertificateHolder(subject.getEncoded());
    if (certHolder.getSubject().equals(certHolder.getIssuer())) {
        return null;
    }//from  w  w  w  . j a v  a 2s.  co m

    return subjectsAndCaCerts.get(certHolder.getIssuer());
}

From source file:ee.ria.xroad.common.conf.globalconf.SharedParametersV2.java

private void cacheCaCerts() throws CertificateException, IOException {
    List<X509Certificate> allCaCerts = new ArrayList<>();

    for (ApprovedCATypeV2 caType : confType.getApprovedCA()) {
        List<CaInfoType> topCAs = Arrays.asList(caType.getTopCA());
        List<CaInfoType> intermediateCAs = caType.getIntermediateCA();

        cacheOcspData(topCAs);/*from w w  w.  j a  v a  2s  .  c  o m*/
        cacheOcspData(intermediateCAs);

        List<X509Certificate> pkiCaCerts = new ArrayList<>();

        pkiCaCerts.addAll(getTopOrIntermediateCaCerts(topCAs));
        pkiCaCerts.addAll(getTopOrIntermediateCaCerts(intermediateCAs));

        Boolean authenticationOnly = caType.isAuthenticationOnly();
        if (authenticationOnly == null || !authenticationOnly) {
            verificationCaCerts.addAll(pkiCaCerts);
        }

        for (X509Certificate pkiCaCert : pkiCaCerts) {
            caCertsAndCertProfiles.put(pkiCaCert, caType.getCertificateProfileInfo());
        }
        allCaCerts.addAll(pkiCaCerts);
    }

    for (X509Certificate cert : allCaCerts) {
        X509CertificateHolder certHolder = new X509CertificateHolder(cert.getEncoded());
        subjectsAndCaCerts.put(certHolder.getSubject(), cert);
    }
}

From source file:mx.bigdata.sat.cfd.CFDv2.java

public void sellar(PrivateKey key, X509Certificate cert) throws Exception {
    cert.checkValidity();/*from   w w  w.j  ava2s.c om*/
    String signature = getSignature(key);
    document.setSello(signature);
    byte[] bytes = cert.getEncoded();
    Base64 b64 = new Base64(-1);
    String certStr = b64.encodeToString(bytes);
    document.setCertificado(certStr);
    BigInteger bi = cert.getSerialNumber();
    document.setNoCertificado(new String(bi.toByteArray()));
}

From source file:ee.ria.xroad.common.conf.globalconf.SharedParameters.java

private void cacheCaCerts() throws CertificateException, IOException {
    List<X509Certificate> allCaCerts = new ArrayList<>();

    for (ApprovedCAType caType : confType.getApprovedCA()) {
        List<CaInfoType> topCAs = Arrays.asList(caType.getTopCA());
        List<CaInfoType> intermediateCAs = caType.getIntermediateCA();

        cacheOcspData(topCAs);//  w w w  .  j a  va  2 s.  co  m
        cacheOcspData(intermediateCAs);

        List<X509Certificate> pkiCaCerts = new ArrayList<>();

        pkiCaCerts.addAll(getTopOrIntermediateCaCerts(topCAs));
        pkiCaCerts.addAll(getTopOrIntermediateCaCerts(intermediateCAs));

        Boolean authenticationOnly = caType.isAuthenticationOnly();
        if (authenticationOnly == null || !authenticationOnly) {
            verificationCaCerts.addAll(pkiCaCerts);
        }

        IdentifierDecoderType identifierDecoder = caType.getIdentifierDecoder();

        for (X509Certificate pkiCaCert : pkiCaCerts) {
            caCertsAndIdentifierDecoders.put(pkiCaCert, identifierDecoder);
        }
        allCaCerts.addAll(pkiCaCerts);
    }

    for (X509Certificate cert : allCaCerts) {
        X509CertificateHolder certHolder = new X509CertificateHolder(cert.getEncoded());
        subjectsAndCaCerts.put(certHolder.getSubject(), cert);
    }
}

From source file:org.sinekartads.dto.domain.CertificateDTO.java

public void certificateToHex(X509Certificate certificate) {
    try {/*from w w  w .j  a va2s . c om*/
        if (certificate != null) {
            hexCertificate = HexUtils.encodeHex(certificate.getEncoded());
        } else {
            hexCertificate = null;
        }
    } catch (CertificateEncodingException e) {
        // it should be never thrown
        throw new RuntimeException(e);
    }
}

From source file:net.maritimecloud.identityregistry.controllers.BaseControllerWithCertificate.java

protected PemCertificate issueCertificate(CertificateModel certOwner, Organization org, String type,
        HttpServletRequest request) throws McBasicRestException {
    // Generate keypair for user
    KeyPair userKeyPair = CertificateUtil.generateKeyPair();
    // Find special MC attributes to put in the certificate
    HashMap<String, String> attrs = getAttr(certOwner);

    String o = org.getMrn();//from w ww  .j  a  v a2s  .  co  m
    String name = getName(certOwner);
    String email = getEmail(certOwner);
    String uid = getUid(certOwner);
    if (uid == null || uid.trim().isEmpty()) {
        throw new McBasicRestException(HttpStatus.BAD_REQUEST, MCIdRegConstants.ENTITY_ORG_ID_MISSING,
                request.getServletPath());
    }
    BigInteger serialNumber = certUtil.generateSerialNumber();
    X509Certificate userCert = certUtil.generateCertForEntity(serialNumber, org.getCountry(), o, type, name,
            email, uid, userKeyPair.getPublic(), attrs);
    String pemCertificate;
    try {
        pemCertificate = CertificateUtil.getPemFromEncoded("CERTIFICATE", userCert.getEncoded()).replace("\n",
                "\\n");
    } catch (CertificateEncodingException e) {
        throw new RuntimeException(e.getMessage(), e);
    }
    String pemPublicKey = CertificateUtil.getPemFromEncoded("PUBLIC KEY", userKeyPair.getPublic().getEncoded())
            .replace("\n", "\\n");
    String pemPrivateKey = CertificateUtil
            .getPemFromEncoded("PRIVATE KEY", userKeyPair.getPrivate().getEncoded()).replace("\n", "\\n");
    PemCertificate ret = new PemCertificate(pemPrivateKey, pemPublicKey, pemCertificate);

    // Create the certificate
    Certificate newMCCert = new Certificate();
    certOwner.assignToCert(newMCCert);
    newMCCert.setCertificate(pemCertificate);
    newMCCert.setSerialNumber(serialNumber);
    // The dates we extract from the cert is in localtime, so they are converted to UTC before saving into the DB
    Calendar cal = Calendar.getInstance();
    long offset = cal.get(Calendar.ZONE_OFFSET) + cal.get(Calendar.DST_OFFSET);
    newMCCert.setStart(new Date(userCert.getNotBefore().getTime() - offset));
    newMCCert.setEnd(new Date(userCert.getNotAfter().getTime() - offset));
    this.certificateService.saveCertificate(newMCCert);
    return ret;
}

From source file:localca.certstore.MockCertificateStorage.java

@Override
public void update(String commonName, X509Certificate certificate) throws CertificateStorageException {
    if (!map.containsKey(commonName)) {
        throw new CertificateStorageException(CertificateStorageExceptionType.NOT_FOUND, commonName);
    }//from  w w  w  . j  a va  2s  .c o  m
    try {
        map.put(commonName,
                new StoredCertificate(commonName, Base64.encodeBase64String(certificate.getEncoded())));
    } catch (CertificateEncodingException e) {
        throw new CertificateStorageException(e, CertificateStorageExceptionType.ENCODING_ERROR, commonName);
    }
}

From source file:org.ejbca.extra.db.PKCS10Response.java

/**
 * Default constructor that should be used.
 * //from ww  w  . jav a2s . c  om
 * @param requestId
 * @param success
 * @param failinfo
 * @param certificate the generated certificate, or null if request failed.
 * @param pkcs7 the generated certificate in a pkcs7 signed by the CA andincluding the certificate chain, or null if request or pkcs7 generation failed.
 *  
 */
public PKCS10Response(long requestId, boolean success, String failinfo, X509Certificate certificate,
        byte[] pkcs7) {
    super(requestId, success, failinfo);
    try {
        data.put(CLASSTYPE, Integer.valueOf(CLASS_TYPE));
        data.put(VERSION, Float.valueOf(LATEST_VERSION));
        if (certificate != null) {
            String certstring = new String(Base64.encode(certificate.getEncoded()));
            data.put(CERTIFICATE, certstring);
        }
        if (pkcs7 != null) {
            String pkcs7str = new String(Base64.encode(pkcs7));
            data.put(PKCS7, pkcs7str);
        }
    } catch (CertificateEncodingException e) {
        log.error("Certificate encoding failed", e);
    }
}

From source file:localca.certstore.MockCertificateStorage.java

@Override
public void insert(String commonName, X509Certificate certificate) throws CertificateStorageException {
    if (map.containsKey(commonName)) {
        throw new CertificateStorageException(CertificateStorageExceptionType.ALREADY_EXISTS, commonName);
    }//from   w  w w  .j  a v  a  2  s  .  co m
    try {
        map.put(commonName,
                new StoredCertificate(commonName, Base64.encodeBase64String(certificate.getEncoded())));
    } catch (CertificateEncodingException e) {
        throw new CertificateStorageException(e, CertificateStorageExceptionType.ENCODING_ERROR, commonName);
    }
}