use of org.opensmartgridplatform.adapter.protocol.dlms.domain.entities.SecurityKeyType in project open-smart-grid-platform by OSGP.
the class GetKeysServiceTest method getKeys.
@Test
void getKeys() {
final Map<SecurityKeyType, byte[]> keys = new EnumMap<>(SecurityKeyType.class);
keys.put(SecurityKeyType.E_METER_MASTER, KEY_1_UNENCRYPTED);
keys.put(SecurityKeyType.E_METER_AUTHENTICATION, KEY_2_UNENCRYPTED);
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);
when(this.rsaEncrypter.encrypt(KEY_2_UNENCRYPTED)).thenReturn(KEY_2_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, KEY_2_ENCRYPTED)));
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 Hls5Connector method setSecurity.
@Override
protected void setSecurity(final MessageMetadata messageMetadata, final DlmsDevice device, final SecurityKeyProvider provider, final TcpConnectionBuilder tcpConnectionBuilder) throws FunctionalException {
final Map<SecurityKeyType, byte[]> encryptedKeys = provider.getKeys(messageMetadata, device.getDeviceIdentification(), Arrays.asList(E_METER_AUTHENTICATION, E_METER_ENCRYPTION));
final byte[] dlmsAuthenticationKey = encryptedKeys.get(E_METER_AUTHENTICATION);
final byte[] dlmsEncryptionKey = encryptedKeys.get(E_METER_ENCRYPTION);
// Validate keys before JDLMS does and throw a FunctionalException if
// necessary
this.validateKeys(dlmsAuthenticationKey, dlmsEncryptionKey);
this.configureIvData(tcpConnectionBuilder, device);
final SecuritySuite securitySuite = SecuritySuite.builder().setAuthenticationKey(dlmsAuthenticationKey).setAuthenticationMechanism(AuthenticationMechanism.HLS5_GMAC).setGlobalUnicastEncryptionKey(dlmsEncryptionKey).setEncryptionMechanism(EncryptionMechanism.AES_GCM_128).build();
tcpConnectionBuilder.setSecuritySuite(securitySuite).setClientId(this.clientId);
}
use of org.opensmartgridplatform.adapter.protocol.dlms.domain.entities.SecurityKeyType in project open-smart-grid-platform by OSGP.
the class GenerateAndReplaceKeyCommandExecutor method generateSetKeysRequest.
private SetKeysRequestDto generateSetKeysRequest(final MessageMetadata messageMetadata, final String deviceIdentification) throws FunctionalException {
try {
final List<SecurityKeyType> keyTypes = Arrays.asList(E_METER_AUTHENTICATION, E_METER_ENCRYPTION);
final Map<SecurityKeyType, byte[]> generatedKeys = this.secretManagementService.generate128BitsKeysAndStoreAsNewKeys(messageMetadata, deviceIdentification, keyTypes);
final SetKeysRequestDto setKeysRequest = new SetKeysRequestDto(generatedKeys.get(E_METER_AUTHENTICATION), generatedKeys.get(E_METER_ENCRYPTION));
setKeysRequest.setGeneratedKeys(true);
return setKeysRequest;
} catch (final EncrypterException e) {
throw new FunctionalException(FunctionalExceptionType.ENCRYPTION_EXCEPTION, ComponentType.PROTOCOL_DLMS, e);
}
}
use of org.opensmartgridplatform.adapter.protocol.dlms.domain.entities.SecurityKeyType in project open-smart-grid-platform by OSGP.
the class SecretManagementService method getNewOrActiveKeyPerSecretType.
/**
* Requests the New key for a specific device identification. Depending on the New key type
* (Authentication or Encryption) that will be retrieved, the other Active key type
* (Authentication or Encryption) will be requested. Once both key types are retrieved, this new
* keypair can be returned for connection with this device.
*
* @param messageMetadata the metadata of the request message
* @param deviceIdentification the device identification string of the device
* @param keyTypes the requested key types
* @return the requested keys in a map by key type, with value NULL if not present
*/
public Map<SecurityKeyType, byte[]> getNewOrActiveKeyPerSecretType(final MessageMetadata messageMetadata, final String deviceIdentification, final List<SecurityKeyType> keyTypes) {
final List<TypedSecret> newKeyPairForConnection = new ArrayList<>();
final GetNewSecretsRequest getNewSecretsRequest = this.createGetNewSecretsRequest(deviceIdentification, keyTypes);
final GetNewSecretsResponse getNewSecretsResponse = this.secretManagementClient.getNewSecretsRequest(messageMetadata, getNewSecretsRequest);
this.validateGetNewResponse(keyTypes, getNewSecretsResponse);
for (final TypedSecret secretTypeNewKey : getNewSecretsResponse.getTypedSecrets().getTypedSecret()) {
if (secretTypeNewKey.getSecret() != null && secretTypeNewKey.getSecret().length() > 0) {
newKeyPairForConnection.add(secretTypeNewKey);
} else {
final SecurityKeyType keyTypeActiveKey = SecurityKeyType.fromSecretType(secretTypeNewKey.getType());
final GetSecretsRequest getSecretsRequest = this.createGetSecretsRequest(deviceIdentification, Arrays.asList(keyTypeActiveKey));
final GetSecretsResponse getSecretsResponse = this.secretManagementClient.getSecretsRequest(messageMetadata, getSecretsRequest);
this.validateGetResponse(Arrays.asList(keyTypeActiveKey), getSecretsResponse);
newKeyPairForConnection.add(getSecretsResponse.getTypedSecrets().getTypedSecret().get(0));
}
}
return this.convertSoapSecretsToSecretMapByType(newKeyPairForConnection);
}
use of org.opensmartgridplatform.adapter.protocol.dlms.domain.entities.SecurityKeyType in project open-smart-grid-platform by OSGP.
the class SecretManagementService method storeNewKeys.
public void storeNewKeys(final MessageMetadata messageMetadata, final String deviceIdentification, final Map<SecurityKeyType, byte[]> keysByType) {
this.validateKeys(keysByType);
final TypedSecrets typedSecrets = new TypedSecrets();
final List<TypedSecret> typedSecretList = typedSecrets.getTypedSecret();
for (final Map.Entry<SecurityKeyType, byte[]> entry : keysByType.entrySet()) {
final TypedSecret ts = new TypedSecret();
ts.setType(entry.getKey().toSecretType());
ts.setSecret(this.encryptSoapSecret(entry.getValue(), true));
typedSecretList.add(ts);
}
final StoreSecretsRequest request = this.createStoreSecretsRequest(deviceIdentification, typedSecrets);
StoreSecretsResponse response = null;
try {
response = this.secretManagementClient.storeSecretsRequest(messageMetadata, request);
} catch (final RuntimeException exc) {
throw new IllegalStateException("Could not store keys: unexpected exception occured", exc);
}
if (response == null) {
throw new IllegalStateException("Could not store keys: NULL response");
} else if (!OsgpResultType.OK.equals(response.getResult())) {
throw new IllegalStateException(String.format("Could not store keys: result=%s; fault=%s", response.getResult(), response.getTechnicalFault()));
}
}
Aggregations