Search in sources :

Example 6 with SecurityKeyType

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);
}
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 7 with SecurityKeyType

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);
}
Also used : SecuritySuite(org.openmuc.jdlms.SecuritySuite) SecurityKeyType(org.opensmartgridplatform.adapter.protocol.dlms.domain.entities.SecurityKeyType)

Example 8 with SecurityKeyType

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);
    }
}
Also used : SetKeysRequestDto(org.opensmartgridplatform.dto.valueobjects.smartmetering.SetKeysRequestDto) EncrypterException(org.opensmartgridplatform.shared.exceptionhandling.EncrypterException) FunctionalException(org.opensmartgridplatform.shared.exceptionhandling.FunctionalException) SecurityKeyType(org.opensmartgridplatform.adapter.protocol.dlms.domain.entities.SecurityKeyType)

Example 9 with SecurityKeyType

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);
}
Also used : GetNewSecretsResponse(org.opensmartgridplatform.ws.schema.core.secret.management.GetNewSecretsResponse) GetSecretsResponse(org.opensmartgridplatform.ws.schema.core.secret.management.GetSecretsResponse) GetSecretsRequest(org.opensmartgridplatform.ws.schema.core.secret.management.GetSecretsRequest) GetNewSecretsRequest(org.opensmartgridplatform.ws.schema.core.secret.management.GetNewSecretsRequest) ArrayList(java.util.ArrayList) SecurityKeyType(org.opensmartgridplatform.adapter.protocol.dlms.domain.entities.SecurityKeyType) TypedSecret(org.opensmartgridplatform.ws.schema.core.secret.management.TypedSecret)

Example 10 with SecurityKeyType

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()));
    }
}
Also used : StoreSecretsResponse(org.opensmartgridplatform.ws.schema.core.secret.management.StoreSecretsResponse) GenerateAndStoreSecretsResponse(org.opensmartgridplatform.ws.schema.core.secret.management.GenerateAndStoreSecretsResponse) TypedSecrets(org.opensmartgridplatform.ws.schema.core.secret.management.TypedSecrets) GenerateAndStoreSecretsRequest(org.opensmartgridplatform.ws.schema.core.secret.management.GenerateAndStoreSecretsRequest) StoreSecretsRequest(org.opensmartgridplatform.ws.schema.core.secret.management.StoreSecretsRequest) SecurityKeyType(org.opensmartgridplatform.adapter.protocol.dlms.domain.entities.SecurityKeyType) TypedSecret(org.opensmartgridplatform.ws.schema.core.secret.management.TypedSecret) Map(java.util.Map) EnumMap(java.util.EnumMap)

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