Example usage for javax.security.auth.x500 X500PrivateCredential X500PrivateCredential

List of usage examples for javax.security.auth.x500 X500PrivateCredential X500PrivateCredential

Introduction

In this page you can find the example usage for javax.security.auth.x500 X500PrivateCredential X500PrivateCredential.

Prototype

public X500PrivateCredential(X509Certificate cert, PrivateKey key, String alias) 

Source Link

Document

Creates an X500PrivateCredential that associates an X.509 certificate, a private key and the KeyStore alias.

Usage

From source file:it.cnr.icar.eric.client.xml.registry.util.SecurityUtil.java

public X500PrivateCredential aliasToX500PrivateCredential(String alias, String keypass) throws JAXRException {
    getKeyStore();/*from  w  ww  . j  av  a  2 s.c o m*/
    try {
        X509Certificate cert = (X509Certificate) keyStore.getCertificate(alias);

        if (cert == null) {
            //It may be that keystore h snot been reloaded since it was updated on disk.
            //Retry cert lookup after reloading keyStore.
            keyStore = loadKeyStore();
            cert = (X509Certificate) keyStore.getCertificate(alias);

            if (cert == null) {
                throw new JAXRException(
                        JAXRResourceBundle.getInstance().getString("message.error.failed.entry.alias.keystore",
                                new Object[] { alias, KeystoreUtil.getKeystoreFile().getAbsolutePath() }));
            }
        }

        // if keypass has not been provided, use property value
        if (keypass == null) {
            keypass = ProviderProperties.getInstance().getProperty("jaxr-ebxml.security.keypass");
            // if still null, use alias
            if (keypass == null) {
                keypass = alias;
            }
        }

        // keytool utility requires a six character minimum password.
        // pad passwords with < six chars
        for (int i = 1; 0 < 6 - keypass.length(); i++) {
            keypass += String.valueOf(i);
        }

        if (log.isTraceEnabled()) {
            StringBuffer sb = new StringBuffer("Retrieving key entry with alias '");
            sb.append(alias).append("' with keypass '");
            for (int i = 0; i < keypass.length(); i++) {
                sb.append('*');
            }
            sb.append("' from keystore loaded from '");
            sb.append(KeystoreUtil.getKeystoreFile().getAbsolutePath());
            sb.append("'.");
            log.trace(sb.toString());
        }

        PrivateKey privateKey = (PrivateKey) keyStore.getKey(alias, keypass.toCharArray());

        return new X500PrivateCredential(cert, privateKey, alias);
    } catch (GeneralSecurityException x) {
        throw new JAXRException(x);
    }
}

From source file:com.qut.middleware.crypto.impl.CryptoProcessorImpl.java

public KeyStore addKeyPair(KeyStore keyStore, String keyStorePassphrase, KeyPair keyPair, String keyPairName,
        String keyPairPassphrase, String keyPairSubjectDN) throws CryptoException {
    logger.debug("Adding key pair to existing key store");

    try {// w w w  .  j  a  v  a  2  s . co  m
        // Create the public key certificate for storage in the key store.
        X509Certificate cert = generateV3Certificate(keyPair, keyPairSubjectDN);
        X500PrivateCredential privateCredentials = new X500PrivateCredential(cert, keyPair.getPrivate(),
                keyPairName);

        Certificate[] certChain = new X509Certificate[1];
        certChain[0] = privateCredentials.getCertificate();

        // Load our generated key store up. They all have the same password, which we set.
        keyStore.load(null, keyStorePassphrase.toCharArray());

        /* Add certificate which contains the public key and set the private key as a key entry in the key store */
        keyStore.setCertificateEntry(privateCredentials.getAlias(), privateCredentials.getCertificate());
        keyStore.setKeyEntry(privateCredentials.getAlias(), keyPair.getPrivate(),
                keyPairPassphrase.toCharArray(), certChain);

        return keyStore;
    } catch (NoSuchAlgorithmException e) {
        this.logger.error("NoSuchAlgorithmException thrown, " + e.getLocalizedMessage());
        this.logger.debug(e.toString());
        throw new CryptoException(e.getLocalizedMessage(), e);
    } catch (CertificateException e) {
        this.logger.error("CertificateException thrown, " + e.getLocalizedMessage());
        this.logger.debug(e.toString());
        throw new CryptoException(e.getLocalizedMessage(), e);
    } catch (KeyStoreException e) {
        this.logger.error("KeyStoreException thrown, " + e.getLocalizedMessage());
        this.logger.debug(e.toString());
        throw new CryptoException(e.getLocalizedMessage(), e);
    } catch (IOException e) {
        this.logger.error("IOException thrown, " + e.getLocalizedMessage());
        this.logger.debug(e.toString());
        throw new CryptoException(e.getLocalizedMessage(), e);
    }
}

From source file:org.votingsystem.web.ejb.SignatureBean.java

public KeyStore generateUserTestKeysStore(String givenName, String surname, String nif, String userPassword)
        throws Exception {
    log.info("generateUserTestKeysStore - nif: " + nif);
    Date validFrom = Calendar.getInstance().getTime();
    Calendar today_plus_year = Calendar.getInstance();
    today_plus_year.add(Calendar.YEAR, 1);
    today_plus_year.set(Calendar.HOUR_OF_DAY, 0);
    today_plus_year.set(Calendar.MINUTE, 0);
    today_plus_year.set(Calendar.SECOND, 0);
    Date validTo = today_plus_year.getTime();
    X500PrivateCredential rootCAPrivateCredential = new X500PrivateCredential(localServerCertSigner,
            serverPrivateKey, keyAlias);
    String testUserDN = format("GIVENNAME={0}, SURNAME={1} , SERIALNUMBER={2}", givenName, surname, nif);
    //String strSubjectDN = "CN=Voting System Cert Authority , OU=VotingSystem"
    //KeyStore rootCAKeyStore = KeyStoreUtil.createRootKeyStore (validFrom.getTime(), (validTo.getTime() - validFrom.getTime()),
    //        userPassword.toCharArray(), keyAlias, strSubjectDN);
    //X509Certificate certSigner = (X509Certificate)rootCAKeyStore.getCertificate(keyAlias);
    //PrivateKey privateKeySigner = (PrivateKey)rootCAKeyStore.getKey(keyAlias, userPassword.toCharArray());
    //X500PrivateCredential rootCAPrivateCredential = new X500PrivateCredential(certSigner, privateKeySigner,  keyAlias);
    return KeyStoreUtil.createUserKeyStore(validFrom.getTime(), (validTo.getTime() - validFrom.getTime()),
            userPassword.toCharArray(), ContextVS.KEYSTORE_USER_CERT_ALIAS, rootCAPrivateCredential,
            testUserDN);/*from   ww  w  .j ava 2 s .com*/
}