Search in sources :

Example 11 with SecurityKeyType

use of org.opensmartgridplatform.adapter.protocol.dlms.domain.entities.SecurityKeyType in project open-smart-grid-platform by OSGP.

the class SecretManagementService method generate128BitsKeysAndStoreAsNewKeys.

/**
 * Generates a new key that can be used as DLMS master key, authentication key, global unicast
 * encryption key, M-Bus Default key or M-Bus User key.
 *
 * <p>The master keys (DLMS master or M-Bus Default) cannot be changed on a device, but can be
 * generated for use in tests or with simulated devices.
 *
 * @param messageMetadata the metadata of the request message
 * @param deviceIdentification the device identification for which to generate the keys
 * @param keyTypes the requested key types
 * @return a new 128bits key, unencrypted.
 */
public Map<SecurityKeyType, byte[]> generate128BitsKeysAndStoreAsNewKeys(final MessageMetadata messageMetadata, final String deviceIdentification, final List<SecurityKeyType> keyTypes) {
    final SecretTypes secretTypes = new SecretTypes();
    final GenerateAndStoreSecretsRequest request = this.createGenerateAndStoreSecretsRequest(deviceIdentification, secretTypes);
    secretTypes.getSecretType().addAll(keyTypes.stream().map(SecurityKeyType::toSecretType).collect(toList()));
    final GenerateAndStoreSecretsResponse response = this.secretManagementClient.generateAndStoreSecrets(messageMetadata, request);
    final TypedSecrets typedSecrets = response.getTypedSecrets();
    final List<TypedSecret> typedSecretList = typedSecrets.getTypedSecret();
    this.validateGenerateAndStoreResponse(keyTypes, response, typedSecretList);
    return this.convertSoapSecretsToSecretMapByType(typedSecrets.getTypedSecret());
}
Also used : GenerateAndStoreSecretsRequest(org.opensmartgridplatform.ws.schema.core.secret.management.GenerateAndStoreSecretsRequest) GenerateAndStoreSecretsResponse(org.opensmartgridplatform.ws.schema.core.secret.management.GenerateAndStoreSecretsResponse) SecretTypes(org.opensmartgridplatform.ws.schema.core.secret.management.SecretTypes) TypedSecrets(org.opensmartgridplatform.ws.schema.core.secret.management.TypedSecrets) SecurityKeyType(org.opensmartgridplatform.adapter.protocol.dlms.domain.entities.SecurityKeyType) TypedSecret(org.opensmartgridplatform.ws.schema.core.secret.management.TypedSecret)

Example 12 with SecurityKeyType

use of org.opensmartgridplatform.adapter.protocol.dlms.domain.entities.SecurityKeyType in project open-smart-grid-platform by OSGP.

the class GetKeysServiceTest method getKeysWhenKeyNotFound.

@Test
void getKeysWhenKeyNotFound() {
    final Map<SecurityKeyType, byte[]> keys = new EnumMap<>(SecurityKeyType.class);
    keys.put(SecurityKeyType.E_METER_MASTER, KEY_1_UNENCRYPTED);
    keys.put(SecurityKeyType.E_METER_AUTHENTICATION, null);
    when(this.secretManagementService.getKeys(messageMetadata, DEVICE_ID, Arrays.asList(SecurityKeyType.E_METER_MASTER, SecurityKeyType.E_METER_AUTHENTICATION))).thenReturn(keys);
    when(this.rsaEncrypter.encrypt(KEY_1_UNENCRYPTED)).thenReturn(KEY_1_ENCRYPTED);
    final GetKeysResponseDto response = this.getKeysService.getKeys(DEVICE, REQUEST, messageMetadata);
    final GetKeysResponseDto expectedResponse = new GetKeysResponseDto(Arrays.asList(new KeyDto(SecretTypeDto.E_METER_MASTER_KEY, KEY_1_ENCRYPTED), new KeyDto(SecretTypeDto.E_METER_AUTHENTICATION_KEY, null)));
    assertThat(response).usingRecursiveComparison().isEqualTo(expectedResponse);
}
Also used : KeyDto(org.opensmartgridplatform.dto.valueobjects.smartmetering.KeyDto) GetKeysResponseDto(org.opensmartgridplatform.dto.valueobjects.smartmetering.GetKeysResponseDto) SecurityKeyType(org.opensmartgridplatform.adapter.protocol.dlms.domain.entities.SecurityKeyType) EnumMap(java.util.EnumMap) Test(org.junit.jupiter.api.Test)

Example 13 with SecurityKeyType

use of org.opensmartgridplatform.adapter.protocol.dlms.domain.entities.SecurityKeyType in project open-smart-grid-platform by OSGP.

the class SecretManagementServiceTest method testStoreNewKeys.

@Test
public void testStoreNewKeys() {
    final Map<SecurityKeyType, byte[]> keys = new HashMap<>();
    keys.put(KEY_TYPE, UNENCRYPTED_SECRET);
    final StoreSecretsResponse response = new StoreSecretsResponse();
    response.setResult(OsgpResultType.OK);
    when(this.encrypterForSecretManagement.encrypt(UNENCRYPTED_SECRET)).thenReturn(SOAP_SECRET);
    when(this.secretManagementClient.storeSecretsRequest(same(messageMetadata), any())).thenReturn(response);
    // EXECUTE
    this.secretManagementTestService.storeNewKeys(messageMetadata, DEVICE_IDENTIFICATION, keys);
    // ASSERT
    verify(this.secretManagementClient, times(1)).storeSecretsRequest(same(messageMetadata), any(StoreSecretsRequest.class));
}
Also used : HashMap(java.util.HashMap) StoreSecretsResponse(org.opensmartgridplatform.ws.schema.core.secret.management.StoreSecretsResponse) GenerateAndStoreSecretsResponse(org.opensmartgridplatform.ws.schema.core.secret.management.GenerateAndStoreSecretsResponse) StoreSecretsRequest(org.opensmartgridplatform.ws.schema.core.secret.management.StoreSecretsRequest) SecurityKeyType(org.opensmartgridplatform.adapter.protocol.dlms.domain.entities.SecurityKeyType) Test(org.junit.jupiter.api.Test)

Aggregations

SecurityKeyType (org.opensmartgridplatform.adapter.protocol.dlms.domain.entities.SecurityKeyType)13 Test (org.junit.jupiter.api.Test)6 EnumMap (java.util.EnumMap)4 GenerateAndStoreSecretsResponse (org.opensmartgridplatform.ws.schema.core.secret.management.GenerateAndStoreSecretsResponse)4 TypedSecrets (org.opensmartgridplatform.ws.schema.core.secret.management.TypedSecrets)4 TypedSecret (org.opensmartgridplatform.ws.schema.core.secret.management.TypedSecret)3 GetKeysResponseDto (org.opensmartgridplatform.dto.valueobjects.smartmetering.GetKeysResponseDto)2 KeyDto (org.opensmartgridplatform.dto.valueobjects.smartmetering.KeyDto)2 FunctionalException (org.opensmartgridplatform.shared.exceptionhandling.FunctionalException)2 GenerateAndStoreSecretsRequest (org.opensmartgridplatform.ws.schema.core.secret.management.GenerateAndStoreSecretsRequest)2 GetSecretsRequest (org.opensmartgridplatform.ws.schema.core.secret.management.GetSecretsRequest)2 GetSecretsResponse (org.opensmartgridplatform.ws.schema.core.secret.management.GetSecretsResponse)2 StoreSecretsRequest (org.opensmartgridplatform.ws.schema.core.secret.management.StoreSecretsRequest)2 StoreSecretsResponse (org.opensmartgridplatform.ws.schema.core.secret.management.StoreSecretsResponse)2 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 NoSuchElementException (java.util.NoSuchElementException)1 SecuritySuite (org.openmuc.jdlms.SecuritySuite)1 ProtocolAdapterException (org.opensmartgridplatform.adapter.protocol.dlms.exceptions.ProtocolAdapterException)1