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
}
}
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;
}
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
}
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
}
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
}
Aggregations