Search in sources :

Example 1 with TypedSecrets

use of org.opensmartgridplatform.ws.schema.core.secret.management.TypedSecrets in project open-smart-grid-platform by OSGP.

the class SecretManagementServiceTest method testGenerateAndStoreKeys.

@Test
public void testGenerateAndStoreKeys() {
    final List<SecurityKeyType> keyTypes = Arrays.asList(KEY_TYPE);
    final GenerateAndStoreSecretsResponse response = new GenerateAndStoreSecretsResponse();
    response.setResult(OsgpResultType.OK);
    response.setTypedSecrets(new TypedSecrets());
    response.getTypedSecrets().getTypedSecret().add(TYPED_SECRET);
    when(this.secretManagementClient.generateAndStoreSecrets(same(messageMetadata), any())).thenReturn(response);
    when(this.decrypterForProtocolAdapterDlms.decrypt(SOAP_SECRET)).thenReturn(UNENCRYPTED_SECRET);
    // EXECUTE
    final Map<SecurityKeyType, byte[]> keys = this.secretManagementTestService.generate128BitsKeysAndStoreAsNewKeys(messageMetadata, DEVICE_IDENTIFICATION, keyTypes);
    // ASSERT
    assertThat(keys.get(KEY_TYPE)).isEqualTo(UNENCRYPTED_SECRET);
}
Also used : GenerateAndStoreSecretsResponse(org.opensmartgridplatform.ws.schema.core.secret.management.GenerateAndStoreSecretsResponse) TypedSecrets(org.opensmartgridplatform.ws.schema.core.secret.management.TypedSecrets) SecurityKeyType(org.opensmartgridplatform.adapter.protocol.dlms.domain.entities.SecurityKeyType) Test(org.junit.jupiter.api.Test)

Example 2 with TypedSecrets

use of org.opensmartgridplatform.ws.schema.core.secret.management.TypedSecrets in project open-smart-grid-platform by OSGP.

the class SecretManagementServiceTest method testGetKeys.

@Test
public void testGetKeys() {
    // SETUP
    final List<SecurityKeyType> keyTypes = Arrays.asList(KEY_TYPE);
    final GetSecretsResponse response = new GetSecretsResponse();
    response.setResult(OsgpResultType.OK);
    response.setTypedSecrets(new TypedSecrets());
    response.getTypedSecrets().getTypedSecret().add(TYPED_SECRET);
    when(this.secretManagementClient.getSecretsRequest(same(messageMetadata), any(GetSecretsRequest.class))).thenReturn(response);
    when(this.decrypterForProtocolAdapterDlms.decrypt(SOAP_SECRET)).thenReturn(UNENCRYPTED_SECRET);
    // EXECUTE
    final Map<SecurityKeyType, byte[]> result = this.secretManagementTestService.getKeys(messageMetadata, DEVICE_IDENTIFICATION, keyTypes);
    // ASSERT
    assertThat(result).isNotNull();
    assertThat(result.size()).isEqualTo(1);
    assertThat(result.keySet().iterator().next()).isEqualTo(KEY_TYPE);
    assertThat(result.values().iterator().next()).isEqualTo(UNENCRYPTED_SECRET);
}
Also used : GetSecretsResponse(org.opensmartgridplatform.ws.schema.core.secret.management.GetSecretsResponse) GetSecretsRequest(org.opensmartgridplatform.ws.schema.core.secret.management.GetSecretsRequest) TypedSecrets(org.opensmartgridplatform.ws.schema.core.secret.management.TypedSecrets) SecurityKeyType(org.opensmartgridplatform.adapter.protocol.dlms.domain.entities.SecurityKeyType) Test(org.junit.jupiter.api.Test)

Example 3 with TypedSecrets

use of org.opensmartgridplatform.ws.schema.core.secret.management.TypedSecrets in project open-smart-grid-platform by OSGP.

the class SecretManagementEndpoint method storeSecrets.

public StoreSecretsResponse storeSecrets(final StoreSecretsRequest request) throws OsgpException {
    final TypedSecrets soapTypedSecrets = request.getTypedSecrets();
    final List<TypedSecret> typedSecretList = this.converter.convertToTypedSecrets(soapTypedSecrets);
    this.secretManagementService.storeSecrets(request.getDeviceId(), typedSecretList);
    return new StoreSecretsResponse();
}
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) TypedSecret(org.opensmartgridplatform.secretmanagement.application.domain.TypedSecret)

Example 4 with TypedSecrets

use of org.opensmartgridplatform.ws.schema.core.secret.management.TypedSecrets 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)

Example 5 with TypedSecrets

use of org.opensmartgridplatform.ws.schema.core.secret.management.TypedSecrets 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)

Aggregations

TypedSecrets (org.opensmartgridplatform.ws.schema.core.secret.management.TypedSecrets)8 SecurityKeyType (org.opensmartgridplatform.adapter.protocol.dlms.domain.entities.SecurityKeyType)4 TypedSecret (org.opensmartgridplatform.secretmanagement.application.domain.TypedSecret)4 GenerateAndStoreSecretsResponse (org.opensmartgridplatform.ws.schema.core.secret.management.GenerateAndStoreSecretsResponse)4 SecretTypes (org.opensmartgridplatform.ws.schema.core.secret.management.SecretTypes)3 Test (org.junit.jupiter.api.Test)2 SecretType (org.opensmartgridplatform.secretmanagement.application.domain.SecretType)2 GenerateAndStoreSecretsRequest (org.opensmartgridplatform.ws.schema.core.secret.management.GenerateAndStoreSecretsRequest)2 GetSecretsResponse (org.opensmartgridplatform.ws.schema.core.secret.management.GetSecretsResponse)2 StoreSecretsResponse (org.opensmartgridplatform.ws.schema.core.secret.management.StoreSecretsResponse)2 TypedSecret (org.opensmartgridplatform.ws.schema.core.secret.management.TypedSecret)2 EnumMap (java.util.EnumMap)1 Map (java.util.Map)1 GetNewSecretsResponse (org.opensmartgridplatform.ws.schema.core.secret.management.GetNewSecretsResponse)1 GetSecretsRequest (org.opensmartgridplatform.ws.schema.core.secret.management.GetSecretsRequest)1 StoreSecretsRequest (org.opensmartgridplatform.ws.schema.core.secret.management.StoreSecretsRequest)1