Search in sources :

Example 1 with KeyStoreException

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);
    }
}
Also used : KeyPair(java.security.KeyPair) PrivateKey(java.security.PrivateKey) X509CertInfo(sun.security.x509.X509CertInfo) SecureRandom(java.security.SecureRandom) CertificateVersion(sun.security.x509.CertificateVersion) CertificateValidity(sun.security.x509.CertificateValidity) KeyPairGenerator(java.security.KeyPairGenerator) X500Name(sun.security.x509.X500Name) KeyStoreException(com.stnetix.ariaddna.keystore.exceptions.KeyStoreException) CertificateX509Key(sun.security.x509.CertificateX509Key) KeyStoreException(com.stnetix.ariaddna.keystore.exceptions.KeyStoreException) CertificateSerialNumber(sun.security.x509.CertificateSerialNumber) CertificateDTO(com.stnetix.ariaddna.commonutils.dto.CertificateDTO) CertificateAlgorithmId(sun.security.x509.CertificateAlgorithmId) AlgorithmId(sun.security.x509.AlgorithmId) X509CertImpl(sun.security.x509.X509CertImpl) FileOutputStream(java.io.FileOutputStream) BigInteger(java.math.BigInteger) CertificateAlgorithmId(sun.security.x509.CertificateAlgorithmId) File(java.io.File)

Example 2 with KeyStoreException

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);
    }
}
Also used : X509CertImpl(sun.security.x509.X509CertImpl) KeyStoreException(com.stnetix.ariaddna.keystore.exceptions.KeyStoreException) KeyStoreException(com.stnetix.ariaddna.keystore.exceptions.KeyStoreException)

Example 3 with KeyStoreException

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);
    }
}
Also used : X509CertImpl(sun.security.x509.X509CertImpl) FileOutputStream(java.io.FileOutputStream) KeyStoreException(com.stnetix.ariaddna.keystore.exceptions.KeyStoreException) KeyStore(java.security.KeyStore) FileInputStream(java.io.FileInputStream) KeyStoreException(com.stnetix.ariaddna.keystore.exceptions.KeyStoreException)

Example 4 with KeyStoreException

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);
    }
}
Also used : FileOutputStream(java.io.FileOutputStream) KeyStoreException(com.stnetix.ariaddna.keystore.exceptions.KeyStoreException) KeyStore(java.security.KeyStore) File(java.io.File) KeyStoreException(com.stnetix.ariaddna.keystore.exceptions.KeyStoreException)

Example 5 with KeyStoreException

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);
    }
}
Also used : X509CertImpl(sun.security.x509.X509CertImpl) FileOutputStream(java.io.FileOutputStream) KeyStoreException(com.stnetix.ariaddna.keystore.exceptions.KeyStoreException) KeyStore(java.security.KeyStore) File(java.io.File) FileInputStream(java.io.FileInputStream) KeyStoreException(com.stnetix.ariaddna.keystore.exceptions.KeyStoreException)

Aggregations

KeyStoreException (com.stnetix.ariaddna.keystore.exceptions.KeyStoreException)7 X509CertImpl (sun.security.x509.X509CertImpl)6 FileOutputStream (java.io.FileOutputStream)5 KeyStore (java.security.KeyStore)5 FileInputStream (java.io.FileInputStream)4 File (java.io.File)3 CertificateDTO (com.stnetix.ariaddna.commonutils.dto.CertificateDTO)1 BigInteger (java.math.BigInteger)1 KeyPair (java.security.KeyPair)1 KeyPairGenerator (java.security.KeyPairGenerator)1 PrivateKey (java.security.PrivateKey)1 SecureRandom (java.security.SecureRandom)1 AlgorithmId (sun.security.x509.AlgorithmId)1 CertificateAlgorithmId (sun.security.x509.CertificateAlgorithmId)1 CertificateSerialNumber (sun.security.x509.CertificateSerialNumber)1 CertificateValidity (sun.security.x509.CertificateValidity)1 CertificateVersion (sun.security.x509.CertificateVersion)1 CertificateX509Key (sun.security.x509.CertificateX509Key)1 X500Name (sun.security.x509.X500Name)1 X509CertInfo (sun.security.x509.X509CertInfo)1