Search in sources :

Example 1 with EncryptedSecret

use of org.opensmartgridplatform.shared.security.EncryptedSecret in project open-smart-grid-platform by OSGP.

the class AbstractEncryptionProvider method encrypt.

@Override
public EncryptedSecret encrypt(final byte[] secret, final String keyReference) {
    try {
        final Cipher cipher = this.getCipher();
        cipher.init(Cipher.ENCRYPT_MODE, this.getSecretEncryptionKey(keyReference, Cipher.ENCRYPT_MODE), this.getAlgorithmParameterSpec());
        return new EncryptedSecret(this.getType(), cipher.doFinal(secret));
    } catch (final Exception e) {
        throw new EncrypterException("Could not encrypt secret with keyReference " + keyReference, e);
    }
}
Also used : EncrypterException(org.opensmartgridplatform.shared.exceptionhandling.EncrypterException) Cipher(javax.crypto.Cipher) EncryptedSecret(org.opensmartgridplatform.shared.security.EncryptedSecret) EncrypterException(org.opensmartgridplatform.shared.exceptionhandling.EncrypterException)

Example 2 with EncryptedSecret

use of org.opensmartgridplatform.shared.security.EncryptedSecret in project open-smart-grid-platform by OSGP.

the class SecretManagementServiceTest method storeSecretsExistingSecret.

@Test
public void storeSecretsExistingSecret() throws Exception {
    final TypedSecret typedSecret = new TypedSecret("n3w$3cr3t0000001".getBytes(), SecretType.E_METER_MASTER_KEY);
    final DbEncryptionKeyReference keyReference = new DbEncryptionKeyReference();
    keyReference.setEncryptionProviderType(ENCRYPTION_PROVIDER_TYPE);
    keyReference.setReference("1");
    final EncryptedSecret encryptedSecret = new EncryptedSecret(ENCRYPTION_PROVIDER_TYPE, "n3w$3cr3t0000001".getBytes());
    final DbEncryptedSecret existingDbSecret = new DbEncryptedSecret();
    existingDbSecret.setCreationTime(new Date());
    existingDbSecret.setSecretType(SecretType.E_METER_MASTER_KEY);
    existingDbSecret.setEncodedSecret("1234567890ABCDEF");
    existingDbSecret.setDeviceIdentification(SOME_DEVICE);
    existingDbSecret.setEncryptionKeyReference(keyReference);
    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(// encrypt new DB secret
    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).isNotNull();
    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) Date(java.util.Date) Test(org.junit.jupiter.api.Test)

Example 3 with EncryptedSecret

use of org.opensmartgridplatform.shared.security.EncryptedSecret in project open-smart-grid-platform by OSGP.

the class SecretManagementServiceTest method storeSecretWhenNewSecretAlreadyExists.

@Test
public void storeSecretWhenNewSecretAlreadyExists() 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());
    final DbEncryptedSecret dbEncryptedSecret = this.getSecret(SecretType.E_METER_ENCRYPTION_KEY_UNICAST, 100);
    when(this.keyRepository.findByTypeAndValid(any(), any())).thenReturn(Arrays.asList(keyReference));
    when(this.keyRepository.findByTypeAndReference(ENCRYPTION_PROVIDER_TYPE, "1")).thenReturn(keyReference);
    when(this.secretRepository.findSecrets(SOME_DEVICE, SecretType.E_METER_ENCRYPTION_KEY_UNICAST, SecretStatus.NEW)).thenReturn(Arrays.asList(dbEncryptedSecret));
    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();
    final ArgumentCaptor<DbEncryptedSecret> secretToWithdrawArgumentCaptor = ArgumentCaptor.forClass(DbEncryptedSecret.class);
    verify(this.secretRepository, times(1)).save(secretToWithdrawArgumentCaptor.capture());
    final DbEncryptedSecret savedSecretToWithdraw = secretToWithdrawArgumentCaptor.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.getEncryptionKeyReference()).isEqualTo(keyReference);
    assertThat(savedSecret.getCreationTime()).isNotNull();
    assertThat(savedSecret.getEncodedSecret()).isEqualTo(HexUtils.toHexString(encryptedSecret.getSecret()));
    assertThat(savedSecret.getSecretStatus()).isEqualTo(SecretStatus.NEW);
    assertThat(savedSecretToWithdraw.getDeviceIdentification()).isEqualTo(SOME_DEVICE);
    assertThat(savedSecretToWithdraw.getSecretType()).isEqualTo(typedSecret.getSecretType());
    assertThat(savedSecretToWithdraw.getEncryptionKeyReference()).isEqualTo(dbEncryptedSecret.getEncryptionKeyReference());
    assertThat(savedSecretToWithdraw.getCreationTime()).isNotNull();
    assertThat(savedSecretToWithdraw.getEncodedSecret()).isEqualTo(dbEncryptedSecret.getEncodedSecret());
    assertThat(savedSecretToWithdraw.getSecretStatus()).isEqualTo(SecretStatus.WITHDRAWN);
}
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 4 with EncryptedSecret

use of org.opensmartgridplatform.shared.security.EncryptedSecret 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 5 with EncryptedSecret

use of org.opensmartgridplatform.shared.security.EncryptedSecret in project open-smart-grid-platform by OSGP.

the class JreEncryptionProviderTest method generateKeyAndCheckLengths.

@Test
public void generateKeyAndCheckLengths() {
    final byte[] encryptedSecretBytes = this.jreEncryptionProvider.generateAes128BitsSecret(JRE_KEY_REF);
    final EncryptedSecret encryptedSecret = new EncryptedSecret(this.jreEncryptionProvider.getType(), encryptedSecretBytes);
    final byte[] unencryptedSecretBytes = this.jreEncryptionProvider.decrypt(encryptedSecret, JRE_KEY_REF);
    final String encryptedSecretAsString = HexUtils.toHexString(encryptedSecretBytes);
    assertEquals(16, unencryptedSecretBytes.length);
    assertEquals(32, encryptedSecretBytes.length);
    assertEquals(64, encryptedSecretAsString.length());
}
Also used : EncryptedSecret(org.opensmartgridplatform.shared.security.EncryptedSecret) Test(org.junit.jupiter.api.Test)

Aggregations

EncryptedSecret (org.opensmartgridplatform.shared.security.EncryptedSecret)7 Test (org.junit.jupiter.api.Test)6 List (java.util.List)3 DbEncryptedSecret (org.opensmartgridplatform.secretmanagement.application.domain.DbEncryptedSecret)3 DbEncryptionKeyReference (org.opensmartgridplatform.secretmanagement.application.domain.DbEncryptionKeyReference)3 TypedSecret (org.opensmartgridplatform.secretmanagement.application.domain.TypedSecret)3 Date (java.util.Date)1 Cipher (javax.crypto.Cipher)1 EncrypterException (org.opensmartgridplatform.shared.exceptionhandling.EncrypterException)1