Search in sources :

Example 1 with TypedSecret

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

the class SecretManagementService method getNewKey.

/**
 * Retrieve a new (not yet activated) key of a certain type for a specified device
 *
 * @param messageMetadata the metadata of the request message
 * @param deviceIdentification the device identification string of the device
 * @param keyType the requested key type
 * @return the key or NULL if not present
 */
public byte[] getNewKey(final MessageMetadata messageMetadata, final String deviceIdentification, final SecurityKeyType keyType) {
    if (LOGGER.isInfoEnabled()) {
        LOGGER.info("Retrieving new {} for device {}", keyType.name(), deviceIdentification);
    }
    final GetNewSecretsRequest getNewSecretsRequest = this.createGetNewSecretsRequest(deviceIdentification, Arrays.asList(keyType));
    final GetNewSecretsResponse getNewSecretsResponse = this.secretManagementClient.getNewSecretsRequest(messageMetadata, getNewSecretsRequest);
    final List<TypedSecret> typedSecrets = getNewSecretsResponse.getTypedSecrets().getTypedSecret();
    if (typedSecrets.isEmpty()) {
        return null;
    }
    return this.convertSoapSecretsToSecretMapByType(typedSecrets).get(keyType);
}
Also used : GetNewSecretsResponse(org.opensmartgridplatform.ws.schema.core.secret.management.GetNewSecretsResponse) GetNewSecretsRequest(org.opensmartgridplatform.ws.schema.core.secret.management.GetNewSecretsRequest) TypedSecret(org.opensmartgridplatform.ws.schema.core.secret.management.TypedSecret)

Example 2 with TypedSecret

use of org.opensmartgridplatform.ws.schema.core.secret.management.TypedSecret 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 3 with TypedSecret

use of org.opensmartgridplatform.ws.schema.core.secret.management.TypedSecret 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 4 with TypedSecret

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

TypedSecret (org.opensmartgridplatform.ws.schema.core.secret.management.TypedSecret)4 SecurityKeyType (org.opensmartgridplatform.adapter.protocol.dlms.domain.entities.SecurityKeyType)3 GenerateAndStoreSecretsRequest (org.opensmartgridplatform.ws.schema.core.secret.management.GenerateAndStoreSecretsRequest)2 GenerateAndStoreSecretsResponse (org.opensmartgridplatform.ws.schema.core.secret.management.GenerateAndStoreSecretsResponse)2 GetNewSecretsRequest (org.opensmartgridplatform.ws.schema.core.secret.management.GetNewSecretsRequest)2 GetNewSecretsResponse (org.opensmartgridplatform.ws.schema.core.secret.management.GetNewSecretsResponse)2 TypedSecrets (org.opensmartgridplatform.ws.schema.core.secret.management.TypedSecrets)2 ArrayList (java.util.ArrayList)1 EnumMap (java.util.EnumMap)1 Map (java.util.Map)1 GetSecretsRequest (org.opensmartgridplatform.ws.schema.core.secret.management.GetSecretsRequest)1 GetSecretsResponse (org.opensmartgridplatform.ws.schema.core.secret.management.GetSecretsResponse)1 SecretTypes (org.opensmartgridplatform.ws.schema.core.secret.management.SecretTypes)1 StoreSecretsRequest (org.opensmartgridplatform.ws.schema.core.secret.management.StoreSecretsRequest)1 StoreSecretsResponse (org.opensmartgridplatform.ws.schema.core.secret.management.StoreSecretsResponse)1