Search in sources :

Example 11 with Certificate

use of org.nhindirect.config.store.Certificate in project nhin-d by DirectProject.

the class ConfigurationServiceTest method testContains.

/**
     * Test the contains method.
     */
public void testContains() throws Exception {
    final CertificateService certificateService = context.mock(CertificateService.class);
    final Certificate certificate = new Certificate();
    context.checking(new Expectations() {

        {
            oneOf(certificateService).contains(certificate);
        }
    });
    ConfigurationServiceImpl service = new ConfigurationServiceImpl();
    service.setCertSvc(certificateService);
    try {
        service.contains(certificate);
    } catch (Exception e) {
        fail("Exception thrown");
    }
}
Also used : Expectations(org.jmock.Expectations) ConfigurationServiceImpl(org.nhindirect.config.service.impl.ConfigurationServiceImpl) Certificate(org.nhindirect.config.store.Certificate)

Example 12 with Certificate

use of org.nhindirect.config.store.Certificate in project nhin-d by DirectProject.

the class CertificateServiceTest method testGetCertificates.

/**
     * Test the getCertificates method.
     */
public void testGetCertificates() {
    final CertificateDao certificateDao = context.mock(CertificateDao.class);
    final Collection<Long> certificateIds = Arrays.asList(7L, 8L);
    final CertificateGetOptions certificateOptions = CertificateGetOptions.DEFAULT;
    context.checking(new Expectations() {

        {
            oneOf(certificateDao).list(new ArrayList<Long>(certificateIds));
            will(returnValue(Collections.<Certificate>emptyList()));
        }
    });
    CertificateServiceImpl service = new CertificateServiceImpl();
    service.setDao(certificateDao);
    try {
        Collection<Certificate> output = service.getCertificates(certificateIds, certificateOptions);
        assertEquals("Output does not match expected", Collections.<Certificate>emptyList(), output);
    } catch (Exception e) {
        fail("Exception thrown");
    }
}
Also used : Expectations(org.jmock.Expectations) CertificateGetOptions(org.nhindirect.config.service.impl.CertificateGetOptions) CertificateDao(org.nhindirect.config.store.dao.CertificateDao) ArrayList(java.util.ArrayList) CertificateServiceImpl(org.nhindirect.config.service.impl.CertificateServiceImpl) Certificate(org.nhindirect.config.store.Certificate)

Example 13 with Certificate

use of org.nhindirect.config.store.Certificate in project nhin-d by DirectProject.

the class TrustBundleServiceTest method testUpdateTrustBundleSigningCertificate.

public void testUpdateTrustBundleSigningCertificate() throws Exception {
    X509Certificate cert = mock(X509Certificate.class);
    CertContainer container = mock(CertContainer.class);
    when(container.getCert()).thenReturn(cert);
    Certificate confCert = mock(Certificate.class);
    when(confCert.toCredential()).thenReturn(container);
    impl.updateTrustBundleSigningCertificate(1234, confCert);
    verify(dao, times(1)).updateTrustBundleSigningCertificate(eq((long) 1234), (X509Certificate) any());
}
Also used : X509Certificate(java.security.cert.X509Certificate) CertContainer(org.nhindirect.config.store.Certificate.CertContainer) X509Certificate(java.security.cert.X509Certificate) Certificate(org.nhindirect.config.store.Certificate)

Example 14 with Certificate

use of org.nhindirect.config.store.Certificate in project nhin-d by DirectProject.

the class CertificateDaoImpl method save.

/*
     * (non-Javadoc)
     * 
     * @see org.nhindirect.config.store.dao.CertificateDao#save(java.util.List)
     */
@Transactional(readOnly = false)
public void save(List<Certificate> certList) {
    if (log.isDebugEnabled())
        log.debug("Enter");
    if (certList != null && certList.size() > 0) {
        for (Certificate cert : certList) {
            cert.setCreateTime(Calendar.getInstance());
            try {
                CertUtils.CertContainer container = null;
                X509Certificate xcert = null;
                try {
                    // this might be an X509Certificate or a P12 key store.. assume there is no protection for P12 key stores... 
                    container = CertUtils.toCertContainer(cert.getData());
                    xcert = container.getCert();
                } catch (Exception e) {
                // probably not a certificate but an IPKIX URL
                }
                if (cert.getValidStartDate() == null && xcert != null) {
                    Calendar startDate = Calendar.getInstance();
                    startDate.setTime(xcert.getNotBefore());
                    cert.setValidStartDate(startDate);
                }
                if (cert.getValidEndDate() == null && xcert != null) {
                    Calendar endDate = Calendar.getInstance();
                    endDate.setTime(xcert.getNotAfter());
                    cert.setValidEndDate(endDate);
                }
                if (cert.getStatus() == null)
                    cert.setStatus(EntityStatus.NEW);
                cert.setPrivateKey(container != null && (container.getKey() != null || container.getWrappedKeyData() != null));
                // if the key store protection manager is set and this is a P12 file, convert the cert data into a protected P12 file
                if (cert.isPrivateKey() && kspMgr != null && container.getKey() != null) {
                    try {
                        final String newKeystorePassPhrase = new String(kspMgr.getKeyStoreProtectionKey().getEncoded());
                        final String newPrivateKeyPassPhrase = new String(kspMgr.getPrivateKeyProtectionKey().getEncoded());
                        cert.setRawData(CertUtils.changePkcs12Protection(cert.getData(), "".toCharArray(), "".toCharArray(), newKeystorePassPhrase.toCharArray(), newPrivateKeyPassPhrase.toCharArray()));
                    } catch (Exception e) {
                        throw new RuntimeException("Error converting P12 to encrypted/protected format", e);
                    }
                }
            } catch (CertificateException e) {
            }
            if (log.isDebugEnabled())
                log.debug("Calling JPA to persist the Certificate");
            entityManager.persist(cert);
            if (log.isDebugEnabled())
                log.debug("Returned from JPA: Certificate ID=" + cert.getId());
        }
        entityManager.flush();
    }
    if (log.isDebugEnabled())
        log.debug("Exit");
}
Also used : CertUtils(org.nhindirect.config.model.utils.CertUtils) Calendar(java.util.Calendar) CertificateException(org.nhindirect.config.store.CertificateException) X509Certificate(java.security.cert.X509Certificate) CertificateConversionException(org.nhindirect.config.model.exceptions.CertificateConversionException) CertificateException(org.nhindirect.config.store.CertificateException) X509Certificate(java.security.cert.X509Certificate) Certificate(org.nhindirect.config.store.Certificate) Transactional(org.springframework.transaction.annotation.Transactional)

Example 15 with Certificate

use of org.nhindirect.config.store.Certificate in project nhin-d by DirectProject.

the class CertificateDaoImpl method list.

/*
     * (non-Javadoc)
     * 
     * @see org.nhindirect.config.store.dao.CertificateDao#list(java.lang.String)
     */
@SuppressWarnings({ "unchecked", "rawtypes" })
@Transactional(readOnly = true)
public List<Certificate> list(String owner) {
    if (log.isDebugEnabled())
        log.debug("Enter");
    List<Certificate> result = Collections.emptyList();
    Query select = null;
    if (owner == null) {
        select = entityManager.createQuery("SELECT c from Certificate c");
    } else if (owner != null) {
        select = entityManager.createQuery("SELECT c from Certificate c WHERE UPPER(c.owner) = ?1");
        select.setParameter(1, owner.toUpperCase(Locale.getDefault()));
    }
    List rs = select.getResultList();
    if ((rs.size() != 0) && (rs.get(0) instanceof Certificate)) {
        result = (List<Certificate>) rs;
    }
    for (Certificate cert : result) stripP12Protection(cert);
    if (log.isDebugEnabled())
        log.debug("Exit");
    return result;
}
Also used : Query(javax.persistence.Query) List(java.util.List) X509Certificate(java.security.cert.X509Certificate) Certificate(org.nhindirect.config.store.Certificate) Transactional(org.springframework.transaction.annotation.Transactional)

Aggregations

Certificate (org.nhindirect.config.store.Certificate)19 X509Certificate (java.security.cert.X509Certificate)10 Expectations (org.jmock.Expectations)7 CertificateDao (org.nhindirect.config.store.dao.CertificateDao)7 Transactional (org.springframework.transaction.annotation.Transactional)6 ArrayList (java.util.ArrayList)5 CertificateServiceImpl (org.nhindirect.config.service.impl.CertificateServiceImpl)5 List (java.util.List)4 CertificateGetOptions (org.nhindirect.config.service.impl.CertificateGetOptions)4 Query (javax.persistence.Query)3 DNSRecord (org.nhindirect.config.store.DNSRecord)3 Setting (org.nhindirect.config.store.Setting)3 ConfigurationServiceException (org.nhindirect.config.service.ConfigurationServiceException)2 ConfigurationServiceImpl (org.nhindirect.config.service.impl.ConfigurationServiceImpl)2 Address (org.nhindirect.config.store.Address)2 Anchor (org.nhindirect.config.store.Anchor)2 CertPolicy (org.nhindirect.config.store.CertPolicy)2 CertPolicyGroup (org.nhindirect.config.store.CertPolicyGroup)2 CertContainer (org.nhindirect.config.store.Certificate.CertContainer)2 Domain (org.nhindirect.config.store.Domain)2