Search in sources :

Example 16 with DbEncryptionKeyReference

use of org.opensmartgridplatform.secretmanagement.application.domain.DbEncryptionKeyReference in project open-smart-grid-platform by OSGP.

the class SoapServiceSecretManagementIT method createTestData.

/**
 * Create test data for encrypted secrets and related encryptionkey reference(s). So that the
 * EncryptionService can encrypt and decrypt, using the JRE encryption provider.
 *
 * <p>Two secrets (for two types of meter key secrets) and one reference key (valid as of
 * now-1minute) is created.
 */
private void createTestData() {
    final DbEncryptionKeyReference encryptionKey = new DbEncryptionKeyReference();
    encryptionKey.setCreationTime(new Date());
    encryptionKey.setReference("1");
    encryptionKey.setEncryptionProviderType(EncryptionProviderType.JRE);
    encryptionKey.setValidFrom(new Date(System.currentTimeMillis() - 60000));
    encryptionKey.setVersion(1L);
    this.testEntityManager.persist(encryptionKey);
    final DbEncryptedSecret encryptedSecret = new DbEncryptedSecret();
    encryptedSecret.setCreationTime(new Date());
    encryptedSecret.setDeviceIdentification(DEVICE_IDENTIFICATION);
    encryptedSecret.setSecretType(org.opensmartgridplatform.secretmanagement.application.domain.SecretType.E_METER_AUTHENTICATION_KEY);
    encryptedSecret.setEncodedSecret(E_METER_AUTHENTICATION_KEY_ENCRYPTED_FOR_DB);
    encryptedSecret.setSecretStatus(SecretStatus.ACTIVE);
    encryptedSecret.setEncryptionKeyReference(encryptionKey);
    this.testEntityManager.persist(encryptedSecret);
    final DbEncryptedSecret encryptedSecret2 = new DbEncryptedSecret();
    encryptedSecret2.setCreationTime(new Date());
    encryptedSecret2.setDeviceIdentification(DEVICE_IDENTIFICATION);
    encryptedSecret2.setSecretType(org.opensmartgridplatform.secretmanagement.application.domain.SecretType.E_METER_ENCRYPTION_KEY_UNICAST);
    encryptedSecret2.setEncodedSecret(E_METER_ENCRYPTION_KEY_UNICAST_ENCRYPTED_FOR_DB);
    encryptedSecret2.setSecretStatus(SecretStatus.ACTIVE);
    encryptedSecret2.setEncryptionKeyReference(encryptionKey);
    this.testEntityManager.persist(encryptedSecret2);
    this.testEntityManager.flush();
}
Also used : DbEncryptionKeyReference(org.opensmartgridplatform.secretmanagement.application.domain.DbEncryptionKeyReference) DbEncryptedSecret(org.opensmartgridplatform.secretmanagement.application.domain.DbEncryptedSecret) Date(java.util.Date)

Example 17 with DbEncryptionKeyReference

use of org.opensmartgridplatform.secretmanagement.application.domain.DbEncryptionKeyReference in project open-smart-grid-platform by OSGP.

the class DbEncryptedSecretRepositoryIT method findSecretsOutdatedKeyRef.

@Test
public void findSecretsOutdatedKeyRef() {
    final Date now = new Date();
    this.dbEncryptionKeyReference.setValidTo(now);
    this.dbEncryptionKeyReference = this.entityManager.persist(this.dbEncryptionKeyReference);
    DbEncryptionKeyReference newEncryptionKeyRef = new DbEncryptionKeyReference();
    newEncryptionKeyRef.setCreationTime(now);
    newEncryptionKeyRef.setReference("keyRef2");
    newEncryptionKeyRef.setEncryptionProviderType(EncryptionProviderType.HSM);
    newEncryptionKeyRef.setValidFrom(now);
    newEncryptionKeyRef.setVersion(1L);
    newEncryptionKeyRef = this.entityManager.persist(newEncryptionKeyRef);
    final List<DbEncryptedSecret> secretsList = this.repository.findSecrets(this.dbEncryptedSecret.getDeviceIdentification(), this.dbEncryptedSecret.getSecretType(), SecretStatus.ACTIVE);
    assertThat(secretsList).hasSize(1);
    final DbEncryptedSecret secret = secretsList.get(0);
    assertThat(secret.getId()).isEqualTo(this.dbEncryptedSecret.getId());
    assertThat(secret.getEncryptionKeyReference().getId()).isEqualTo(this.dbEncryptionKeyReference.getId());
}
Also used : DbEncryptionKeyReference(org.opensmartgridplatform.secretmanagement.application.domain.DbEncryptionKeyReference) DbEncryptedSecret(org.opensmartgridplatform.secretmanagement.application.domain.DbEncryptedSecret) Date(java.util.Date) Test(org.junit.jupiter.api.Test)

Example 18 with DbEncryptionKeyReference

use of org.opensmartgridplatform.secretmanagement.application.domain.DbEncryptionKeyReference in project open-smart-grid-platform by OSGP.

the class SecretManagementServiceTest method storeSecretsMultipleKeys.

@Test
public void storeSecretsMultipleKeys() {
    final TypedSecret typedSecret = new TypedSecret("$3cr3t".getBytes(), SecretType.E_METER_MASTER_KEY);
    when(this.keyRepository.findByTypeAndValid(any(), any())).thenReturn(Arrays.asList(new DbEncryptionKeyReference(), new DbEncryptionKeyReference()));
    assertThatIllegalStateException().isThrownBy(() -> this.service.storeSecrets(SOME_DEVICE, Arrays.asList(typedSecret)));
}
Also used : DbEncryptionKeyReference(org.opensmartgridplatform.secretmanagement.application.domain.DbEncryptionKeyReference) TypedSecret(org.opensmartgridplatform.secretmanagement.application.domain.TypedSecret) Test(org.junit.jupiter.api.Test)

Example 19 with DbEncryptionKeyReference

use of org.opensmartgridplatform.secretmanagement.application.domain.DbEncryptionKeyReference in project open-smart-grid-platform by OSGP.

the class SecretManagementServiceTest method generateAndStoreSecretsWhenNewSecretsAlreadyExists.

@Test
public void generateAndStoreSecretsWhenNewSecretsAlreadyExists() throws Exception {
    final Date now = new Date();
    final String reference = "1";
    final byte[] aesSecret = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 };
    final byte[] secret = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 };
    final byte[] rsaSecret = { 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 };
    final DbEncryptionKeyReference keyReference = new DbEncryptionKeyReference();
    keyReference.setReference(reference);
    keyReference.setEncryptionProviderType(ENCRYPTION_PROVIDER_TYPE);
    keyReference.setValidFrom(now);
    final DbEncryptedSecret secretOldEncryption = this.getSecret(SecretType.E_METER_ENCRYPTION_KEY_UNICAST, 100);
    final DbEncryptedSecret secretOlderEncryption = this.getSecret(SecretType.E_METER_ENCRYPTION_KEY_UNICAST, 1000);
    final Date originalCreationTimeEncryptionSecret = secretOldEncryption.getCreationTime();
    final Date olderCreationTime = secretOlderEncryption.getCreationTime();
    when(this.keyRepository.findByTypeAndValid(any(), any())).thenReturn(Arrays.asList(keyReference));
    when(this.encryptionDelegate.generateAes128BitsSecret(ENCRYPTION_PROVIDER_TYPE, reference)).thenReturn(aesSecret);
    when(this.secretRepository.findSecrets(SOME_DEVICE, SecretType.E_METER_ENCRYPTION_KEY_UNICAST, SecretStatus.NEW)).thenReturn(Arrays.asList(secretOldEncryption, secretOlderEncryption));
    when(this.encryptionDelegate.decrypt(any(), any())).thenReturn(secret);
    when(this.encrypterForSecretManagementClient.encrypt(any())).thenReturn(rsaSecret);
    final SecretType encryptionSecretType = SecretType.E_METER_ENCRYPTION_KEY_UNICAST;
    final SecretType authenticationSecretType = SecretType.E_METER_AUTHENTICATION_KEY;
    this.service.generateAndStoreSecrets(SOME_DEVICE, Arrays.asList(encryptionSecretType, authenticationSecretType));
    verify(this.secretRepository, never()).saveAll(Arrays.asList(secretOldEncryption));
    verify(this.secretRepository, never()).saveAll(Arrays.asList(secretOldEncryption));
    assertThat(secretOldEncryption.getCreationTime()).isEqualTo(originalCreationTimeEncryptionSecret);
    assertThat(secretOldEncryption.getSecretStatus()).isEqualTo(SecretStatus.WITHDRAWN);
    assertThat(secretOlderEncryption.getCreationTime().getTime()).isEqualTo(olderCreationTime.getTime());
    assertThat(secretOlderEncryption.getSecretStatus()).isEqualTo(SecretStatus.WITHDRAWN);
}
Also used : DbEncryptionKeyReference(org.opensmartgridplatform.secretmanagement.application.domain.DbEncryptionKeyReference) SecretType(org.opensmartgridplatform.secretmanagement.application.domain.SecretType) DbEncryptedSecret(org.opensmartgridplatform.secretmanagement.application.domain.DbEncryptedSecret) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Date(java.util.Date) Test(org.junit.jupiter.api.Test)

Example 20 with DbEncryptionKeyReference

use of org.opensmartgridplatform.secretmanagement.application.domain.DbEncryptionKeyReference in project open-smart-grid-platform by OSGP.

the class SecretManagementServiceTest method generateAndStoreSecrets.

@Test
public void generateAndStoreSecrets() throws EncrypterException {
    final Date now = new Date();
    final String reference = "1";
    final byte[] aesSecret = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 };
    final byte[] secret = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 };
    final byte[] rsaSecret = { 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 };
    final DbEncryptionKeyReference keyReference = new DbEncryptionKeyReference();
    keyReference.setReference(reference);
    keyReference.setEncryptionProviderType(ENCRYPTION_PROVIDER_TYPE);
    keyReference.setValidFrom(now);
    when(this.keyRepository.findByTypeAndValid(any(), any())).thenReturn(Arrays.asList(keyReference));
    when(this.encryptionDelegate.generateAes128BitsSecret(ENCRYPTION_PROVIDER_TYPE, reference)).thenReturn(aesSecret);
    when(this.encryptionDelegate.decrypt(any(), any())).thenReturn(secret);
    when(this.encrypterForSecretManagementClient.encrypt(any())).thenReturn(rsaSecret);
    final List<TypedSecret> secrets = this.service.generateAndStoreSecrets(SOME_DEVICE, Arrays.asList(SecretType.E_METER_AUTHENTICATION_KEY));
    assertThat(secrets.size()).isEqualTo(1);
    final TypedSecret typedSecret = secrets.get(0);
    assertThat(typedSecret.getSecretType()).isEqualTo(SecretType.E_METER_AUTHENTICATION_KEY);
    assertThat(typedSecret.getSecret()).isEqualTo(rsaSecret);
}
Also used : DbEncryptionKeyReference(org.opensmartgridplatform.secretmanagement.application.domain.DbEncryptionKeyReference) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) TypedSecret(org.opensmartgridplatform.secretmanagement.application.domain.TypedSecret) Date(java.util.Date) Test(org.junit.jupiter.api.Test)

Aggregations

DbEncryptionKeyReference (org.opensmartgridplatform.secretmanagement.application.domain.DbEncryptionKeyReference)25 Date (java.util.Date)16 Test (org.junit.jupiter.api.Test)13 DbEncryptedSecret (org.opensmartgridplatform.secretmanagement.application.domain.DbEncryptedSecret)13 TypedSecret (org.opensmartgridplatform.secretmanagement.application.domain.TypedSecret)7 SecretType (org.opensmartgridplatform.secretmanagement.application.domain.SecretType)4 List (java.util.List)3 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)3 SecretBuilder (org.opensmartgridplatform.cucumber.platform.smartmetering.builders.entities.SecretBuilder)3 EncrypterException (org.opensmartgridplatform.shared.exceptionhandling.EncrypterException)3 EncryptedSecret (org.opensmartgridplatform.shared.security.EncryptedSecret)3 BeforeEach (org.junit.jupiter.api.BeforeEach)2 Given (io.cucumber.java.en.Given)1 ArrayList (java.util.ArrayList)1 NoSuchElementException (java.util.NoSuchElementException)1 Transactional (org.springframework.transaction.annotation.Transactional)1