use of org.nhindirect.stagent.cert.X509CertificateEx in project nhin-d by DirectProject.
the class CertCommands method writeCertsToFiles.
protected void writeCertsToFiles(org.nhind.config.Certificate[] certs) throws IOException {
int idx = 1;
for (org.nhind.config.Certificate cert : certs) {
CertUtils.CertContainer cont = CertUtils.toCertContainer(cert.getData());
X509Certificate transCert = cont.getCert();
String certFileName = "";
String extension = (transCert instanceof X509CertificateEx) ? ".p12" : ".der";
String certFileHold = CryptoExtensions.getSubjectAddress(transCert) + extension;
if (certs.length > 1) {
int index = certFileHold.lastIndexOf(".");
if (index < 0)
certFileHold += "(" + idx + ")";
else {
certFileName = certFileHold.substring(0, index - 1) + "(" + idx + ")" + certFileHold.substring(index);
}
} else
certFileName = certFileHold;
File certFile = new File(certFileName);
if (certFile.exists())
certFile.delete();
System.out.println("Writing cert file: " + certFile.getAbsolutePath());
try {
FileUtils.writeByteArrayToFile(certFile, transCert.getEncoded());
} catch (Exception e) {
System.out.println("Failed to write cert file: " + certFile.getAbsolutePath() + " :" + e.getMessage());
}
++idx;
}
}
use of org.nhindirect.stagent.cert.X509CertificateEx in project nhin-d by DirectProject.
the class CertCommands method importPrivateCertWithWrappedKey.
@Command(name = "AddPrivateCertWithWrappedKey", usage = IMPORT_PRIVATE_CERT_W_WRAPPEDKEY_USAGE)
public void importPrivateCertWithWrappedKey(String[] args) {
final String certFileLoc = StringArrayUtil.getRequiredValue(args, 0);
final String keyFileLoc = StringArrayUtil.getRequiredValue(args, 1);
try {
final byte[] certFileBytes = FileUtils.readFileToByteArray(new File(certFileLoc));
final byte[] keyFileBytes = FileUtils.readFileToByteArray(new File(keyFileLoc));
final X509Certificate cert = CertUtils.toX509Certificate(certFileBytes);
byte[] certBytes = org.nhindirect.config.model.utils.CertUtils.certAndWrappedKeyToRawByteFormat(keyFileBytes, cert);
org.nhind.config.Certificate addCert = new org.nhind.config.Certificate();
addCert.setData(certBytes);
addCert.setOwner(CryptoExtensions.getSubjectAddress(cert));
addCert.setPrivateKey(cert instanceof X509CertificateEx);
addCert.setStatus(EntityStatus.ENABLED);
proxy.addCertificates(new org.nhind.config.Certificate[] { addCert });
System.out.println("Successfully imported certificate.");
} catch (IOException e) {
System.out.println("Error reading file: " + e.getMessage());
return;
} catch (Exception e) {
System.out.println("Error importing certificate " + e.getMessage());
}
}
use of org.nhindirect.stagent.cert.X509CertificateEx in project nhin-d by DirectProject.
the class CertCommands method importPrivateCert.
@Command(name = "AddPrivateCert", usage = IMPORT_PRIVATE_CERT_USAGE)
public void importPrivateCert(String[] args) {
final String fileLoc = StringArrayUtil.getRequiredValue(args, 0);
final String passPhrase = StringArrayUtil.getOptionalValue(args, 1, "");
try {
final byte[] certBytes = FileUtils.readFileToByteArray(new File(fileLoc));
final byte[] insertBytes = (passPhrase == null || passPhrase.isEmpty()) ? certBytes : CertUtils.pkcs12ToStrippedPkcs12(certBytes, passPhrase);
final X509Certificate cert = CertUtils.toX509Certificate(insertBytes);
org.nhind.config.Certificate addCert = new org.nhind.config.Certificate();
addCert.setData(certBytes);
addCert.setOwner(CryptoExtensions.getSubjectAddress(cert));
addCert.setPrivateKey(cert instanceof X509CertificateEx);
addCert.setStatus(EntityStatus.ENABLED);
proxy.addCertificates(new org.nhind.config.Certificate[] { addCert });
System.out.println("Successfully imported private certificate.");
} catch (IOException e) {
System.out.println("Error reading file " + fileLoc + " : " + e.getMessage());
return;
} catch (Exception e) {
System.out.println("Error importing certificate " + fileLoc + " : " + e.getMessage());
}
}
use of org.nhindirect.stagent.cert.X509CertificateEx in project nhin-d by DirectProject.
the class CacheableKeyStoreManagerCertificateStore_addTest method testAdd_nonMutableStore_assertException.
public void testAdd_nonMutableStore_assertException() throws Exception {
if (store != null) {
final BootstrappedKeyStoreProtectionManager mgr = new BootstrappedKeyStoreProtectionManager();
store.setKeyStoreManager(mgr);
boolean exceptionOccured = false;
try {
final X509CertificateEx user1 = (X509CertificateEx) TestUtils.getInternalCert("user1");
store.add(user1);
} catch (IllegalStateException ex) {
exceptionOccured = true;
}
assertTrue(exceptionOccured);
}
}
use of org.nhindirect.stagent.cert.X509CertificateEx in project nhin-d by DirectProject.
the class CacheableKeyStoreManagerCertificateStore_addTest method testAdd_addNewCert_assertAdded.
public void testAdd_addNewCert_assertAdded() throws Exception {
if (store != null) {
// add a certificate
final X509CertificateEx user1 = (X509CertificateEx) TestUtils.getInternalCert("user1");
store.add(user1);
final Collection<X509Certificate> retrievedCerts = store.getAllCertificates();
assertEquals(1, retrievedCerts.size());
final X509Certificate retrievedCert = retrievedCerts.iterator().next();
assertTrue(retrievedCert instanceof X509CertificateEx);
assertEquals(user1, retrievedCert);
}
}
Aggregations