Search in sources :

Example 1 with CertificateDTO

use of com.stnetix.ariaddna.commonutils.dto.CertificateDTO 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 CertificateDTO

use of com.stnetix.ariaddna.commonutils.dto.CertificateDTO in project ariADDna by StnetixDevTeam.

the class CertificateServiceImplTest method getDisableCertificatesTest.

@Test
public void getDisableCertificatesTest() throws Exception {
    UUID uuid1 = UUID.randomUUID();
    UUID uuid2 = UUID.randomUUID();
    UUID uuid3 = UUID.randomUUID();
    CertificateDTO certificateDTO1 = new CertificateDTO();
    CertificateDTO certificateDTO2 = new CertificateDTO();
    CertificateDTO certificateDTO3 = new CertificateDTO();
    certificateDTO1.setActive(true);
    certificateDTO1.setUuid(uuid1.toString());
    certificateDTO2.setUuid(uuid2.toString());
    certificateDTO3.setUuid(uuid3.toString());
    certificateService.save(certificateDTO1);
    certificateService.save(certificateDTO2);
    certificateService.save(certificateDTO3);
    List<CertificateDTO> certificateDTOList = certificateService.getDisableCertificates();
    assertEquals(certificateDTOList.size(), 2);
}
Also used : CertificateDTO(com.stnetix.ariaddna.commonutils.dto.CertificateDTO) UUID(java.util.UUID) SpringBootTest(org.springframework.boot.test.context.SpringBootTest) Test(org.junit.Test)

Example 3 with CertificateDTO

use of com.stnetix.ariaddna.commonutils.dto.CertificateDTO in project ariADDna by StnetixDevTeam.

the class CertificateServiceImplTest method saveTest.

@Test
public void saveTest() throws Exception {
    CertificateDTO certificateDTO = new CertificateDTO();
    certificateDTO.setActive(true);
    UUID uuid = UUID.randomUUID();
    certificateDTO.setUuid(uuid.toString());
    CertificateDTO savedCertDTO = certificateService.save(certificateDTO);
    assertNotNull(savedCertDTO);
    assertNotNull(savedCertDTO.getId());
}
Also used : CertificateDTO(com.stnetix.ariaddna.commonutils.dto.CertificateDTO) UUID(java.util.UUID) SpringBootTest(org.springframework.boot.test.context.SpringBootTest) Test(org.junit.Test)

Example 4 with CertificateDTO

use of com.stnetix.ariaddna.commonutils.dto.CertificateDTO in project ariADDna by StnetixDevTeam.

the class CertificateServiceImplTest method getActiveCertificatesTest.

@Test
public void getActiveCertificatesTest() throws Exception {
    UUID uuid1 = UUID.randomUUID();
    UUID uuid2 = UUID.randomUUID();
    UUID uuid3 = UUID.randomUUID();
    CertificateDTO certificateDTO1 = new CertificateDTO();
    CertificateDTO certificateDTO2 = new CertificateDTO();
    CertificateDTO certificateDTO3 = new CertificateDTO();
    certificateDTO1.setActive(true);
    certificateDTO3.setActive(true);
    certificateDTO1.setUuid(uuid1.toString());
    certificateDTO2.setUuid(uuid2.toString());
    certificateDTO3.setUuid(uuid3.toString());
    certificateService.save(certificateDTO1);
    certificateService.save(certificateDTO2);
    certificateService.save(certificateDTO3);
    List<CertificateDTO> certificateDTOList = certificateService.getActiveCertificates();
    assertEquals(certificateDTOList.size(), 2);
}
Also used : CertificateDTO(com.stnetix.ariaddna.commonutils.dto.CertificateDTO) UUID(java.util.UUID) SpringBootTest(org.springframework.boot.test.context.SpringBootTest) Test(org.junit.Test)

Example 5 with CertificateDTO

use of com.stnetix.ariaddna.commonutils.dto.CertificateDTO in project ariADDna by StnetixDevTeam.

the class CertificateServiceImplTest method getAllCertificatesTest.

@Test
public void getAllCertificatesTest() throws Exception {
    UUID uuid1 = UUID.randomUUID();
    UUID uuid2 = UUID.randomUUID();
    UUID uuid3 = UUID.randomUUID();
    CertificateDTO certificateDTO1 = new CertificateDTO();
    CertificateDTO certificateDTO2 = new CertificateDTO();
    CertificateDTO certificateDTO3 = new CertificateDTO();
    certificateDTO1.setActive(true);
    certificateDTO1.setUuid(uuid1.toString());
    certificateDTO2.setUuid(uuid2.toString());
    certificateDTO3.setUuid(uuid3.toString());
    certificateService.save(certificateDTO1);
    certificateService.save(certificateDTO2);
    certificateService.save(certificateDTO3);
    List<CertificateDTO> certificateDTOList = certificateService.getAllCertificates();
    assertEquals(certificateDTOList.size(), 3);
}
Also used : CertificateDTO(com.stnetix.ariaddna.commonutils.dto.CertificateDTO) UUID(java.util.UUID) SpringBootTest(org.springframework.boot.test.context.SpringBootTest) Test(org.junit.Test)

Aggregations

CertificateDTO (com.stnetix.ariaddna.commonutils.dto.CertificateDTO)9 UUID (java.util.UUID)6 Test (org.junit.Test)6 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)4 Certificate (com.stnetix.ariaddna.persistence.entities.Certificate)2 KeyStoreException (com.stnetix.ariaddna.keystore.exceptions.KeyStoreException)1 File (java.io.File)1 FileOutputStream (java.io.FileOutputStream)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