Search in sources :

Example 6 with SecretType

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

the class SecretManagementEndpoint method getNewSecrets.

public GetNewSecretsResponse getNewSecrets(final GetNewSecretsRequest request) throws OsgpException {
    final GetNewSecretsResponse response = new GetNewSecretsResponse();
    final SecretTypes soapSecretTypes = request.getSecretTypes();
    final List<SecretType> secretTypeList = this.converter.convertToSecretTypes(soapSecretTypes);
    final List<TypedSecret> typedSecrets = this.secretManagementService.retrieveNewSecrets(request.getDeviceId(), secretTypeList);
    final TypedSecrets soapTypedSecrets = this.converter.convertToSoapTypedSecrets(typedSecrets);
    response.setTypedSecrets(soapTypedSecrets);
    return response;
}
Also used : GetNewSecretsResponse(org.opensmartgridplatform.ws.schema.core.secret.management.GetNewSecretsResponse) SecretType(org.opensmartgridplatform.secretmanagement.application.domain.SecretType) SecretTypes(org.opensmartgridplatform.ws.schema.core.secret.management.SecretTypes) TypedSecrets(org.opensmartgridplatform.ws.schema.core.secret.management.TypedSecrets) TypedSecret(org.opensmartgridplatform.secretmanagement.application.domain.TypedSecret)

Example 7 with SecretType

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

the class SecretManagementEndpoint method getSecrets.

public GetSecretsResponse getSecrets(final GetSecretsRequest request) throws OsgpException {
    final GetSecretsResponse response = new GetSecretsResponse();
    final SecretTypes soapSecretTypes = request.getSecretTypes();
    final List<SecretType> secretTypeList = this.converter.convertToSecretTypes(soapSecretTypes);
    final List<TypedSecret> typedSecrets = this.secretManagementService.retrieveSecrets(request.getDeviceId(), secretTypeList);
    final TypedSecrets soapTypedSecrets = this.converter.convertToSoapTypedSecrets(typedSecrets);
    response.setTypedSecrets(soapTypedSecrets);
    return response;
}
Also used : GetSecretsResponse(org.opensmartgridplatform.ws.schema.core.secret.management.GetSecretsResponse) SecretType(org.opensmartgridplatform.secretmanagement.application.domain.SecretType) SecretTypes(org.opensmartgridplatform.ws.schema.core.secret.management.SecretTypes) TypedSecrets(org.opensmartgridplatform.ws.schema.core.secret.management.TypedSecrets) TypedSecret(org.opensmartgridplatform.secretmanagement.application.domain.TypedSecret)

Example 8 with SecretType

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

use of org.opensmartgridplatform.secretmanagement.application.domain.SecretType 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)

Example 10 with SecretType

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

the class DlmsDeviceSteps method registerNewKeys.

private void registerNewKeys(final long minutesAgo, final Map<String, String> inputSettings) {
    if (!inputSettings.containsKey(PlatformSmartmeteringKeys.DEVICE_IDENTIFICATION)) {
        throw new IllegalArgumentException("No device identification provided");
    }
    final String deviceIdentification = inputSettings.get(PlatformSmartmeteringKeys.DEVICE_IDENTIFICATION);
    final List<SecretType> secretTypesToCreate = Arrays.asList(E_METER_AUTHENTICATION_KEY, E_METER_ENCRYPTION_KEY_UNICAST);
    final List<String> keyTypeInputNames = secretTypesToCreate.stream().map(this::getKeyTypeInputName).collect(Collectors.toList());
    if (Collections.disjoint(inputSettings.keySet(), keyTypeInputNames)) {
        throw new IllegalArgumentException("None of the following keys provided: " + keyTypeInputNames);
    }
    final DbEncryptionKeyReference encryptionKeyRef = this.encryptionKeyRepository.findByTypeAndValid(EncryptionProviderType.JRE, new Date()).iterator().next();
    for (int i = 0; i < secretTypesToCreate.size(); i++) {
        if (inputSettings.containsKey(keyTypeInputNames.get(i))) {
            final String inputKeyName = inputSettings.get(keyTypeInputNames.get(i));
            final String key = SecurityKey.valueOf(inputKeyName).getDatabaseKey();
            final DbEncryptedSecret secret = new SecretBuilder().withDeviceIdentification(deviceIdentification).withSecretType(secretTypesToCreate.get(i)).withKey(key).withSecretStatus(SecretStatus.NEW).withEncryptionKeyReference(encryptionKeyRef).withCreationTime(new Date(System.currentTimeMillis() - (minutesAgo * 60000L))).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)

Aggregations

SecretType (org.opensmartgridplatform.secretmanagement.application.domain.SecretType)11 DbEncryptedSecret (org.opensmartgridplatform.secretmanagement.application.domain.DbEncryptedSecret)5 Date (java.util.Date)4 DbEncryptionKeyReference (org.opensmartgridplatform.secretmanagement.application.domain.DbEncryptionKeyReference)4 TypedSecret (org.opensmartgridplatform.secretmanagement.application.domain.TypedSecret)4 SecretTypes (org.opensmartgridplatform.ws.schema.core.secret.management.SecretTypes)3 Test (org.junit.jupiter.api.Test)2 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)2 SecretBuilder (org.opensmartgridplatform.cucumber.platform.smartmetering.builders.entities.SecretBuilder)2 TypedSecrets (org.opensmartgridplatform.ws.schema.core.secret.management.TypedSecrets)2 Given (io.cucumber.java.en.Given)1 Then (io.cucumber.java.en.Then)1 GenerateAndStoreSecretsResponse (org.opensmartgridplatform.ws.schema.core.secret.management.GenerateAndStoreSecretsResponse)1 GetNewSecretsResponse (org.opensmartgridplatform.ws.schema.core.secret.management.GetNewSecretsResponse)1 GetSecretsResponse (org.opensmartgridplatform.ws.schema.core.secret.management.GetSecretsResponse)1 HasNewSecretResponse (org.opensmartgridplatform.ws.schema.core.secret.management.HasNewSecretResponse)1