use of com.stnetix.ariaddna.keystore.exceptions.KeyStoreException in project ariADDna by StnetixDevTeam.
the class CertFactory method getNewCertificate.
public File getNewCertificate(String alias) throws KeyStoreException {
KeyPairGenerator keyPairGenerator = null;
try {
keyPairGenerator = KeyPairGenerator.getInstance(CRYPTO_ALGORITHM_RSA);
keyPairGenerator.initialize(CERTIFICATE_SIZE);
KeyPair keyPair = keyPairGenerator.generateKeyPair();
PrivateKey privateKey = keyPair.getPrivate();
X509CertInfo certInfo = new X509CertInfo();
CertificateValidity interval = new CertificateValidity(FROM, TO);
BigInteger sn = new BigInteger(64, new SecureRandom());
X500Name owner = new X500Name(SUBJECT_CN + alias + ", " + SUBJECT_L_C);
certInfo.set(X509CertInfo.VALIDITY, interval);
certInfo.set(X509CertInfo.SERIAL_NUMBER, new CertificateSerialNumber(sn));
certInfo.set(X509CertInfo.SUBJECT, owner);
certInfo.set(X509CertInfo.ISSUER, owner);
certInfo.set(X509CertInfo.KEY, new CertificateX509Key(keyPair.getPublic()));
certInfo.set(X509CertInfo.VERSION, new CertificateVersion(CertificateVersion.V3));
AlgorithmId algorithm = new AlgorithmId(AlgorithmId.md2WithRSAEncryption_oid);
certInfo.set(X509CertInfo.ALGORITHM_ID, new CertificateAlgorithmId(algorithm));
X509CertImpl cert = new X509CertImpl(certInfo);
cert.sign(privateKey, CRYPTO_ALGORITHM_SHA1RSA);
algorithm = (AlgorithmId) cert.get(X509CertImpl.SIG_ALG);
certInfo.set(CertificateAlgorithmId.NAME + "." + CertificateAlgorithmId.ALGORITHM, algorithm);
cert = new X509CertImpl(certInfo);
cert.sign(privateKey, CRYPTO_ALGORITHM_SHA1RSA);
File certFile = new File(alias + ".cer");
if (certFile.createNewFile()) {
FileOutputStream fos = new FileOutputStream(certFile);
fos.write(cert.getEncoded());
fos.close();
}
LOGGER.info("Certificate generated with filename {}", certFile.getAbsolutePath());
CertificateDTO storedCert = persistHelper.storeCertificete(new CertificateDTO(alias, true));
LOGGER.info("Certificate stored id DB with id {}", storedCert.getId());
return certFile;
} catch (Exception e) {
LOGGER.error("Exception: ", e);
throw new KeyStoreException("Caused by: ", e);
}
}
use of com.stnetix.ariaddna.keystore.exceptions.KeyStoreException in project ariADDna by StnetixDevTeam.
the class CertFactory method isValid.
public boolean isValid(File certFile) throws KeyStoreException {
try {
X509CertImpl cert = (X509CertImpl) getCertByFile(certFile);
long notBefore = cert.getNotBefore().getTime();
long notAfter = cert.getNotAfter().getTime();
long now = System.currentTimeMillis();
LOGGER.info("Certificate {} is " + (now >= notBefore && now <= notAfter ? "valid" : "not valid"), certFile.getAbsolutePath());
boolean isActive = persistHelper.isActiveCertificate(getCertSubjectName(cert));
return now >= notBefore && now <= notAfter && isActive;
} catch (Exception e) {
LOGGER.error("Exception: ", e);
throw new KeyStoreException("Caused by: ", e);
}
}
use of com.stnetix.ariaddna.keystore.exceptions.KeyStoreException in project ariADDna by StnetixDevTeam.
the class KeyFactory method storeCertToKeyStore.
public void storeCertToKeyStore(File certFile, File keyStoreFile) throws KeyStoreException {
try {
X509CertImpl cert = (X509CertImpl) certFactory.getCertByFile(certFile);
String alias = certFactory.getCertSubjectName(cert);
LOGGER.info("Certificate with filename {} has Subject name {}", certFile.getAbsolutePath(), alias);
FileInputStream fis = new FileInputStream(keyStoreFile);
KeyStore keyStore = KeyStore.getInstance(KEYSTORE_FORMAT);
keyStore.load(fis, pass);
LOGGER.info("KeyStore load successful");
fis.close();
keyStore.setCertificateEntry(alias, cert);
FileOutputStream fos = new FileOutputStream(keyStoreFile);
keyStore.store(fos, pass);
LOGGER.info("Certificate with filename {} stored in keyStore with filename {}", certFile.getAbsolutePath(), keyStoreFile.getAbsolutePath());
fos.close();
} catch (Exception e) {
LOGGER.error("Exception: ", e);
throw new KeyStoreException("Caused by: ", e);
}
}
use of com.stnetix.ariaddna.keystore.exceptions.KeyStoreException in project ariADDna by StnetixDevTeam.
the class KeyFactory method generateKeyStoreByName.
private File generateKeyStoreByName(String name) throws KeyStoreException {
KeyStore keyStore = null;
try (FileOutputStream fos = new FileOutputStream(name)) {
keyStore = KeyStore.getInstance(KEYSTORE_FORMAT);
keyStore.load(null, pass);
keyStore.store(fos, pass);
File keyStoreFile = new File(name);
LOGGER.info("KeyStore was create with file name {}", keyStoreFile.getAbsolutePath());
return keyStoreFile;
} catch (Exception e) {
LOGGER.error("KeyStore object is not create. Exception: ", e);
throw new KeyStoreException("Caused by: ", e);
}
}
use of com.stnetix.ariaddna.keystore.exceptions.KeyStoreException in project ariADDna by StnetixDevTeam.
the class KeyFactory method getCertByAlias.
public File getCertByAlias(String alias, File keyStoreFile) throws KeyStoreException {
try {
FileInputStream fis = new FileInputStream(keyStoreFile);
KeyStore keyStore = KeyStore.getInstance(KEYSTORE_FORMAT);
keyStore.load(fis, pass);
LOGGER.info("KeyStore {} loaded successful.", keyStoreFile.getAbsolutePath());
fis.close();
X509CertImpl cert = (X509CertImpl) keyStore.getCertificate(alias);
File certFile = new File(alias + ".cer");
FileOutputStream fos = new FileOutputStream(certFile);
fos.write(cert.getEncoded());
LOGGER.info("Certificate {} loaded successful.", certFile.getAbsolutePath());
fos.close();
return certFile;
} catch (Exception e) {
LOGGER.error("Exception: ", e);
throw new KeyStoreException("Caused by: ", e);
}
}
Aggregations