Search in sources :

Example 1 with NHINDException

use of org.nhindirect.stagent.NHINDException in project nhin-d by DirectProject.

the class CertCommands method x509CertificateToBytes.

public static byte[] x509CertificateToBytes(X509Certificate cert) {
    if (cert instanceof X509CertificateEx) {
        final ByteArrayOutputStream outStr = new ByteArrayOutputStream();
        try {
            // return as a pkcs12 file with no encryption
            final KeyStore convertKeyStore = KeyStore.getInstance("PKCS12", CryptoExtensions.getJCEProviderName());
            convertKeyStore.load(null, null);
            final char[] emptyPass = "".toCharArray();
            convertKeyStore.setKeyEntry("privCert", ((X509CertificateEx) cert).getPrivateKey(), emptyPass, new java.security.cert.Certificate[] { cert });
            convertKeyStore.store(outStr, emptyPass);
            return outStr.toByteArray();
        }///CLOVER:OFF
         catch (Exception e) {
            throw new NHINDException("Failed to convert certificate to a byte stream.", e);
        } finally ///CLOVER:ON
        {
            try {
                outStr.close();
            } catch (Exception e) {
            /* no-op */
            }
        }
    } else {
        try {
            return cert.getEncoded();
        }///CLOVER:OFF
         catch (Exception e) {
            throw new NHINDException("Failed to convert certificate to a byte stream.", e);
        }
    ///CLOVER:ON
    }
}
Also used : X509CertificateEx(org.nhindirect.stagent.cert.X509CertificateEx) ByteArrayOutputStream(java.io.ByteArrayOutputStream) KeyStore(java.security.KeyStore) NHINDException(org.nhindirect.stagent.NHINDException) IOException(java.io.IOException) NHINDException(org.nhindirect.stagent.NHINDException)

Example 2 with NHINDException

use of org.nhindirect.stagent.NHINDException in project nhin-d by DirectProject.

the class WrappedOnDemandX509CertificateEx method getPrivateKey.

/**
     * {@inheritDoc}}
     */
public synchronized PrivateKey getPrivateKey() {
    if (wrappedKey != null)
        return wrappedKey;
    final WrappableKeyProtectionManager wrapManager = (WrappableKeyProtectionManager) mgr;
    // get the key algorithm from the public key... this will be needed
    // as a parameter to the unwrap method
    final String keyAlg = this.internalCert.getPublicKey().getAlgorithm();
    try {
        wrappedKey = (PrivateKey) wrapManager.unwrapWithSecretKey((SecretKey) mgr.getPrivateKeyProtectionKey(), wrappedData, keyAlg, Cipher.PRIVATE_KEY);
    } catch (CryptoException e) {
        throw new NHINDException(AgentError.Unexpected, "Failed to access wrapped private key.", e);
    }
    return wrappedKey;
}
Also used : WrappableKeyProtectionManager(org.nhindirect.common.crypto.WrappableKeyProtectionManager) CryptoException(org.nhindirect.common.crypto.exceptions.CryptoException) NHINDException(org.nhindirect.stagent.NHINDException)

Example 3 with NHINDException

use of org.nhindirect.stagent.NHINDException in project nhin-d by DirectProject.

the class AbstractKeyStoreManagerCertificateStore method remove.

@Override
public void remove(X509Certificate cert) {
    if (!(storeMgr instanceof MutableKeyStoreProtectionManager))
        throw new IllegalStateException("The store manager is a MutableKeyStoreProtectionManager instance");
    try {
        String aliasToRemove = null;
        for (String alias : storeMgr.getAllEntries().keySet()) {
            final Entry entry = storeMgr.getEntry(alias);
            if (entry instanceof PrivateKeyEntry) {
                final PrivateKeyEntry privEntry = (PrivateKeyEntry) entry;
                if (cert.equals(privEntry.getCertificate())) {
                    aliasToRemove = alias;
                    break;
                }
            }
        }
        if (aliasToRemove != null) {
            final MutableKeyStoreProtectionManager mutMgr = (MutableKeyStoreProtectionManager) storeMgr;
            mutMgr.clearEntry(aliasToRemove);
        }
    }///CLOVER:OFF
     catch (Exception e) {
        throw new NHINDException(AgentError.Unexpected, "Failed to remove key entry from PKCS11 store.", e);
    }
///CLOVER:ON
}
Also used : PrivateKeyEntry(java.security.KeyStore.PrivateKeyEntry) Entry(java.security.KeyStore.Entry) MutableKeyStoreProtectionManager(org.nhindirect.common.crypto.MutableKeyStoreProtectionManager) PrivateKeyEntry(java.security.KeyStore.PrivateKeyEntry) NHINDException(org.nhindirect.stagent.NHINDException) NHINDException(org.nhindirect.stagent.NHINDException)

Example 4 with NHINDException

use of org.nhindirect.stagent.NHINDException in project nhin-d by DirectProject.

the class AbstractKeyStoreManagerCertificateStore method getAllCertificates.

/**
	 * {@inheritDoc}
	 */
@Override
public Collection<X509Certificate> getAllCertificates() {
    final Collection<X509Certificate> retVal = new ArrayList<X509Certificate>();
    try {
        for (Map.Entry<String, Entry> entry : storeMgr.getAllEntries().entrySet()) {
            if (entry.getValue() instanceof PrivateKeyEntry) {
                final PrivateKeyEntry privEntry = (PrivateKeyEntry) entry.getValue();
                retVal.add(X509CertificateEx.fromX509Certificate((X509Certificate) privEntry.getCertificate(), privEntry.getPrivateKey()));
            }
        }
        return retVal;
    }///CLOVER:OFF
     catch (Exception e) {
        throw new NHINDException(AgentError.Unexpected, "Failed to get key entries from PKCS11 store.", e);
    }
///CLOVER:ON
}
Also used : PrivateKeyEntry(java.security.KeyStore.PrivateKeyEntry) Entry(java.security.KeyStore.Entry) ArrayList(java.util.ArrayList) Map(java.util.Map) PrivateKeyEntry(java.security.KeyStore.PrivateKeyEntry) NHINDException(org.nhindirect.stagent.NHINDException) X509Certificate(java.security.cert.X509Certificate) NHINDException(org.nhindirect.stagent.NHINDException)

Example 5 with NHINDException

use of org.nhindirect.stagent.NHINDException in project nhin-d by DirectProject.

the class AbstractKeyStoreManagerCertificateStore method add.

@Override
public void add(X509Certificate cert) {
    if (!(storeMgr instanceof MutableKeyStoreProtectionManager))
        throw new IllegalStateException("The store manager is a MutableKeyStoreProtectionManager instance");
    if (!(cert instanceof X509CertificateEx) || !((X509CertificateEx) cert).hasPrivateKey())
        throw new IllegalArgumentException("PKCS11 certificates require a private key");
    final X509CertificateEx exCert = (X509CertificateEx) cert;
    // keys stores require aliases, and a given subject may include multiple certificates
    // to avoid possible collisions, this will use the certificate thumbprint
    final String alias = Thumbprint.toThumbprint(cert).toString();
    final PrivateKeyEntry entry = new PrivateKeyEntry(exCert.getPrivateKey(), new Certificate[] { cert });
    try {
        ((MutableKeyStoreProtectionManager) storeMgr).setEntry(alias, entry);
    }///CLOVER:OFF
     catch (Exception e) {
        throw new NHINDException(AgentError.Unexpected, "Failed to add key entry into PKCS11 store.", e);
    }
///CLOVER:ON
}
Also used : X509CertificateEx(org.nhindirect.stagent.cert.X509CertificateEx) MutableKeyStoreProtectionManager(org.nhindirect.common.crypto.MutableKeyStoreProtectionManager) PrivateKeyEntry(java.security.KeyStore.PrivateKeyEntry) NHINDException(org.nhindirect.stagent.NHINDException) NHINDException(org.nhindirect.stagent.NHINDException)

Aggregations

NHINDException (org.nhindirect.stagent.NHINDException)45 X509Certificate (java.security.cert.X509Certificate)30 ArrayList (java.util.ArrayList)14 ByteArrayInputStream (java.io.ByteArrayInputStream)13 IOException (java.io.IOException)11 Key (java.security.Key)10 PrivateKey (java.security.PrivateKey)10 KeyStore (java.security.KeyStore)9 CacheException (org.apache.jcs.access.exception.CacheException)7 X509CertificateEx (org.nhindirect.stagent.cert.X509CertificateEx)7 MessagingException (javax.mail.MessagingException)6 Collection (java.util.Collection)4 UnknownHostException (java.net.UnknownHostException)3 PrivateKeyEntry (java.security.KeyStore.PrivateKeyEntry)3 Certificate (java.security.cert.Certificate)3 InternetHeaders (javax.mail.internet.InternetHeaders)3 ASN1InputStream (org.bouncycastle.asn1.ASN1InputStream)3 ASN1OctetString (org.bouncycastle.asn1.ASN1OctetString)3 MutableKeyStoreProtectionManager (org.nhindirect.common.crypto.MutableKeyStoreProtectionManager)3 File (java.io.File)2