Search in sources :

Example 21 with DbEncryptedSecret

use of org.opensmartgridplatform.secretmanagement.application.domain.DbEncryptedSecret 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 22 with DbEncryptedSecret

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

the class SecretManagementServiceTest method activateSecretsNoCurrentActiveSecret.

@Test
public void activateSecretsNoCurrentActiveSecret() {
    final DbEncryptedSecret newSecret = new DbEncryptedSecret();
    newSecret.setId(1L);
    newSecret.setSecretType(SecretType.E_METER_MASTER_KEY);
    newSecret.setSecretStatus(SecretStatus.NEW);
    when(this.secretRepository.findSecrets(SOME_DEVICE, SecretType.E_METER_MASTER_KEY, SecretStatus.NEW)).thenReturn(Arrays.asList(newSecret));
    when(this.secretRepository.saveAll(Arrays.asList(newSecret))).thenReturn(Arrays.asList(newSecret));
    this.service.activateNewSecrets(SOME_DEVICE, Arrays.asList(SecretType.E_METER_MASTER_KEY));
    assertThat(newSecret.getSecretStatus()).isEqualTo(SecretStatus.ACTIVE);
    final ArgumentCaptor<List<DbEncryptedSecret>> secretListArgumentCaptor = this.getListOfDbEncryptedSecretsArgumentCaptor();
    verify(this.secretRepository).saveAll(secretListArgumentCaptor.capture());
    final List<DbEncryptedSecret> savedSecrets = secretListArgumentCaptor.getValue();
    assertThat(savedSecrets).isNotNull();
    assertThat(savedSecrets.size()).isEqualTo(1);
    final DbEncryptedSecret savedSecret = savedSecrets.get(0);
    assertThat(savedSecret.getSecretType()).isEqualTo(newSecret.getSecretType());
    assertThat(savedSecret.getSecretStatus()).isEqualTo(SecretStatus.ACTIVE);
}
Also used : DbEncryptedSecret(org.opensmartgridplatform.secretmanagement.application.domain.DbEncryptedSecret) List(java.util.List) Test(org.junit.jupiter.api.Test)

Example 23 with DbEncryptedSecret

use of org.opensmartgridplatform.secretmanagement.application.domain.DbEncryptedSecret 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 24 with DbEncryptedSecret

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

the class SecretManagementServiceTest method storeSecrets.

@Test
public void storeSecrets() throws Exception {
    final TypedSecret typedSecret = new TypedSecret("rsaSecret00000001".getBytes(), SecretType.E_METER_ENCRYPTION_KEY_UNICAST);
    final DbEncryptionKeyReference keyReference = new DbEncryptionKeyReference();
    keyReference.setEncryptionProviderType(ENCRYPTION_PROVIDER_TYPE);
    keyReference.setReference("1");
    final EncryptedSecret encryptedSecret = new EncryptedSecret(ENCRYPTION_PROVIDER_TYPE, "aesSecret0000001".getBytes());
    when(this.keyRepository.findByTypeAndValid(any(), any())).thenReturn(Arrays.asList(keyReference));
    when(this.keyRepository.findByTypeAndReference(ENCRYPTION_PROVIDER_TYPE, "1")).thenReturn(keyReference);
    when(this.encryptionDelegate.encrypt(any(), any(), anyString())).thenReturn(encryptedSecret);
    this.service.storeSecrets(SOME_DEVICE, Arrays.asList(typedSecret));
    final ArgumentCaptor<List<DbEncryptedSecret>> secretListArgumentCaptor = this.getListOfDbEncryptedSecretsArgumentCaptor();
    verify(this.secretRepository).saveAll(secretListArgumentCaptor.capture());
    final List<DbEncryptedSecret> savedSecrets = secretListArgumentCaptor.getValue();
    assertThat(savedSecrets).isNotNull();
    assertThat(savedSecrets.size()).isEqualTo(1);
    final DbEncryptedSecret savedSecret = savedSecrets.get(0);
    assertThat(savedSecret.getDeviceIdentification()).isEqualTo(SOME_DEVICE);
    assertThat(savedSecret.getSecretType()).isEqualTo(typedSecret.getSecretType());
    assertThat(savedSecret.getEncodedSecret()).isEqualTo(HexUtils.toHexString(encryptedSecret.getSecret()));
    assertThat(savedSecret.getEncryptionKeyReference()).isEqualTo(keyReference);
    assertThat(savedSecret.getCreationTime()).isNotNull();
}
Also used : DbEncryptionKeyReference(org.opensmartgridplatform.secretmanagement.application.domain.DbEncryptionKeyReference) DbEncryptedSecret(org.opensmartgridplatform.secretmanagement.application.domain.DbEncryptedSecret) List(java.util.List) TypedSecret(org.opensmartgridplatform.secretmanagement.application.domain.TypedSecret) EncryptedSecret(org.opensmartgridplatform.shared.security.EncryptedSecret) DbEncryptedSecret(org.opensmartgridplatform.secretmanagement.application.domain.DbEncryptedSecret) Test(org.junit.jupiter.api.Test)

Example 25 with DbEncryptedSecret

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

the class DlmsDeviceSteps method simulateFailureOfChangeFromPreviousKeyOfDevice.

@Given("simulate failure of change from previous key of device \"{}\"")
public void simulateFailureOfChangeFromPreviousKeyOfDevice(final String id, final Map<String, String> inputSettings) {
    for (final String keyTypeInputName : inputSettings.keySet()) {
        final String securityTypeInputName = inputSettings.get(keyTypeInputName);
        final SecretType secretType = this.getSecretTypeByKeyTypeInputName(keyTypeInputName);
        final String key = SecurityKey.valueOf(securityTypeInputName).getDatabaseKey();
        final List<DbEncryptedSecret> currentlyActiveKeys = this.encryptedSecretRepository.findSecrets(id, secretType, SecretStatus.ACTIVE);
        for (final DbEncryptedSecret currentlyActiveKey : currentlyActiveKeys) {
            currentlyActiveKey.setSecretStatus(SecretStatus.NEW);
            this.encryptedSecretRepository.save(currentlyActiveKey);
        }
        final DbEncryptionKeyReference encryptionKeyRef = this.encryptionKeyRepository.findByTypeAndValid(EncryptionProviderType.JRE, new Date()).iterator().next();
        final DbEncryptedSecret secret = new SecretBuilder().withDeviceIdentification(id).withSecretType(secretType).withKey(key).withSecretStatus(SecretStatus.ACTIVE).withEncryptionKeyReference(encryptionKeyRef).withCreationTime(new Date()).build();
        this.encryptedSecretRepository.save(secret);
    }
}
Also used : DbEncryptionKeyReference(org.opensmartgridplatform.secretmanagement.application.domain.DbEncryptionKeyReference) SecretBuilder(org.opensmartgridplatform.cucumber.platform.smartmetering.builders.entities.SecretBuilder) SecretType(org.opensmartgridplatform.secretmanagement.application.domain.SecretType) DbEncryptedSecret(org.opensmartgridplatform.secretmanagement.application.domain.DbEncryptedSecret) Date(java.util.Date) Given(io.cucumber.java.en.Given)

Aggregations

DbEncryptedSecret (org.opensmartgridplatform.secretmanagement.application.domain.DbEncryptedSecret)29 DbEncryptionKeyReference (org.opensmartgridplatform.secretmanagement.application.domain.DbEncryptionKeyReference)13 Test (org.junit.jupiter.api.Test)12 Date (java.util.Date)11 Then (io.cucumber.java.en.Then)5 SecretType (org.opensmartgridplatform.secretmanagement.application.domain.SecretType)5 List (java.util.List)4 TypedSecret (org.opensmartgridplatform.secretmanagement.application.domain.TypedSecret)4 DlmsDevice (org.opensmartgridplatform.adapter.protocol.dlms.domain.entities.DlmsDevice)3 EncryptedSecret (org.opensmartgridplatform.shared.security.EncryptedSecret)3 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)2 SecretBuilder (org.opensmartgridplatform.cucumber.platform.smartmetering.builders.entities.SecretBuilder)2 Given (io.cucumber.java.en.Given)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 ArrayList (java.util.ArrayList)1 BeforeEach (org.junit.jupiter.api.BeforeEach)1 EncrypterException (org.opensmartgridplatform.shared.exceptionhandling.EncrypterException)1 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)1 ClassPathResource (org.springframework.core.io.ClassPathResource)1 Resource (org.springframework.core.io.Resource)1