Example usage for java.security KeyStore getCertificate

List of usage examples for java.security KeyStore getCertificate

Introduction

In this page you can find the example usage for java.security KeyStore getCertificate.

Prototype

public final Certificate getCertificate(String alias) throws KeyStoreException 

Source Link

Document

Returns the certificate associated with the given alias.

Usage

From source file:org.apache.abdera.security.util.KeyHelper.java

@SuppressWarnings("unchecked")
public static <T extends Certificate> T getCertificate(KeyStore ks, String alias) throws KeyStoreException {
    return (T) ks.getCertificate(alias);
}

From source file:no.difi.sdp.client.domain.Sertifikat.java

public static Sertifikat fraKeyStore(KeyStore keyStore, String alias) {
    Certificate certificate;//from w  w  w . ja v  a  2s .co  m
    try {
        certificate = keyStore.getCertificate(alias);
    } catch (KeyStoreException e) {
        throw new SertifikatException("Klarte ikke lese sertifikat fra keystore", e);
    }

    if (certificate == null) {
        throw new SertifikatException(
                "Kunne ikke finne sertifikat i keystore. Er du sikker p at det er brukt keystore med et sertifikat og at du har oppgitt riktig alias?");
    }

    if (!(certificate instanceof X509Certificate)) {
        throw new SertifikatException("Klienten sttter kun X509-sertifikater. Fikk sertifikat av typen "
                + certificate.getClass().getSimpleName());
    }

    return new Sertifikat((X509Certificate) certificate);
}

From source file:no.difi.meldingsutveksling.domain.Sertifikat.java

public static Sertifikat fraKeyStore(KeyStore keyStore, String alias) {
    Certificate certificate;//from www.j av a  2 s.com
    try {
        certificate = keyStore.getCertificate(alias);
    } catch (KeyStoreException e) {
        throw new MeldingsUtvekslingRuntimeException("Klarte ikke lese sertifikat fra keystore", e);
    }

    if (certificate == null) {
        throw new MeldingsUtvekslingRuntimeException(
                "Kunne ikke finne sertifikat i keystore. Er du sikker p at det er brukt keystore med et sertifikat og at du har oppgitt riktig alias?");
    }

    if (!(certificate instanceof X509Certificate)) {
        throw new MeldingsUtvekslingRuntimeException(
                "Klienten sttter kun X509-sertifikater. Fikk sertifikat av typen "
                        + certificate.getClass().getSimpleName());
    }

    return new Sertifikat((X509Certificate) certificate);
}

From source file:Main.java

@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
private static String encryptStringImpl(Context context, final String plainText) {
    String encryptedText = plainText;
    try {//from w  w w  .j a va2 s . co  m
        final KeyStore keyStore = getKeyStore(context);

        PublicKey publicKey = keyStore.getCertificate(KEY_ALIAS).getPublicKey();

        String algorithm = ALGORITHM_OLD;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            algorithm = ALGORITHM;
        }
        Cipher cipher = Cipher.getInstance(algorithm);
        cipher.init(Cipher.ENCRYPT_MODE, publicKey);

        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        CipherOutputStream cipherOutputStream = new CipherOutputStream(outputStream, cipher);
        cipherOutputStream.write(plainText.getBytes("UTF-8"));
        cipherOutputStream.close();

        byte[] bytes = outputStream.toByteArray();
        encryptedText = Base64.encodeToString(bytes, Base64.DEFAULT);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return encryptedText;
}

From source file:org.wso2.carbon.identity.sso.saml.TestUtils.java

public static Certificate getCertificate(KeyStore keyStore, String alias) throws KeyStoreException {

    return keyStore.getCertificate(alias);
}

From source file:org.digidoc4j.testutils.TestSigningHelper.java

public static X509Certificate getSigningCert(String pkiContainer, String pkiContainerPassword) {
    try {/*from  w  w  w  .j  a  v  a  2  s .  co m*/
        KeyStore keyStore = KeyStore.getInstance("PKCS12");
        try (FileInputStream stream = new FileInputStream(pkiContainer)) {
            keyStore.load(stream, pkiContainerPassword.toCharArray());
        }
        return (X509Certificate) keyStore.getCertificate("1");
    } catch (Exception e) {
        throw new DigiDoc4JException("Loading signer cert failed");
    }
}

From source file:at.gv.egovernment.moa.id.protocols.oauth20.json.OAuth20SignatureUtil.java

public static OAuthSigner loadSigner(String issuer) throws OAuth20Exception {
    OAuth20Configuration globalConfig = OAuth20Configuration.getInstance();

    if (StringUtils.isEmpty(globalConfig.getJWTKeyStore())) {
        throw new OAuth20CertificateErrorException("keystore");
    }/*from  ww  w. j  av a2s.  c o m*/

    if (StringUtils.isEmpty(globalConfig.getJWTKeyName())) {
        throw new OAuth20CertificateErrorException("key name");
    }

    try {
        KeyStore ks = KeyStoreUtils.loadKeyStore(globalConfig.getJWTKeyStore(),
                globalConfig.getJWTKeyStorePassword());

        X509Certificate certificate = (X509Certificate) ks.getCertificate(globalConfig.getJWTKeyName());

        PrivateKey privateKey = (PrivateKey) ks.getKey(globalConfig.getJWTKeyName(),
                globalConfig.getJWTKeyPassword().toCharArray());
        BasicX509Credential credential = new BasicX509Credential();
        credential.setEntityCertificate(certificate);
        credential.setPrivateKey(privateKey);

        // Logger.debug("Going to use X509Certificate:");
        // Logger.debug(certificate);
        // Logger.debug("Going to use private key:");
        // Logger.debug(privateKey);

        return new OAuth20SHA256Signer(issuer, globalConfig.getJWTKeyName(), credential.getPrivateKey());

    } catch (Exception e) {
        Logger.error(e.getMessage(), e);
        throw new OAuth20CertificateErrorException("keystore");
    }

}

From source file:org.bankinterface.util.KeyStoreUtil.java

static PublicKey getPublicKey(String url, String alias) {
    Object[] store = signVerifyStore.get(url);
    try {//from w  w  w  . j a v  a2 s  . c  o  m
        KeyStore ks = (KeyStore) store[0];
        return ks.getCertificate(alias).getPublicKey();
    } catch (Exception e) {
        logger.error("Get PublicKey Erorr, URL : " + url + ", Alias :" + alias);
        return null;
    }
}

From source file:prototype.samples.AsyncSigning.java

private static X509Certificate getSignerCert() {
    try {/*from w  w  w  .  j  a v  a 2s  .c  o  m*/
        KeyStore keyStore = KeyStore.getInstance("PKCS12");
        try (FileInputStream stream = new FileInputStream("testFiles/signout.p12")) {
            keyStore.load(stream, "test".toCharArray());
        }
        return (X509Certificate) keyStore.getCertificate("1");
    } catch (Exception e) {
        throw new DigiDoc4JException("Loading signer cert failed");
    }
}

From source file:ee.sk.hwcrypto.demo.signature.TestSigningData.java

private static X509Certificate getSigningCert(String pkiContainer, String pkiContainerPassword) {
    try {/*  w w  w . ja va 2s  .com*/
        KeyStore keyStore = KeyStore.getInstance("PKCS12");
        try (FileInputStream stream = new FileInputStream(pkiContainer)) {
            keyStore.load(stream, pkiContainerPassword.toCharArray());
        }
        return (X509Certificate) keyStore.getCertificate("1");
    } catch (Exception e) {
        throw new RuntimeException("Loading signer cert failed");
    }
}