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());
}
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);
}
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));
}
Aggregations