Skip to content

Commit

Permalink
Fix #954: Use proper response nonce for 3.1 protocol
Browse files Browse the repository at this point in the history
  • Loading branch information
hvge committed Aug 3, 2023
1 parent e27b718 commit 1c9f7ef
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ public PrepareActivationResponse prepareActivation(PrepareActivationRequest requ
final byte[] nonce = request.getNonce() != null ? Base64.getDecoder().decode(request.getNonce()) : null;
final String version = request.getProtocolVersion();
final Long timestamp = "3.2".equals(version) ? request.getTimestamp() : null;
final byte[] associatedData = "3.2".equals(version) ? EciesUtils.deriveAssociatedData(EciesScope.APPLICATION_SCOPE, version, applicationKey, null) : null;
final byte[] associatedData = EciesUtils.deriveAssociatedData(EciesScope.APPLICATION_SCOPE, version, applicationKey, null);
final EciesCryptogram eciesCryptogram = EciesCryptogram.builder().ephemeralPublicKey(ephemeralPublicKey).mac(mac).encryptedData(encryptedData).build();
final EciesParameters eciesParameters = EciesParameters.builder().nonce(nonce).associatedData(associatedData).timestamp(timestamp).build();
final EciesPayload eciesPayload = new EciesPayload(eciesCryptogram, eciesParameters);
Expand Down Expand Up @@ -362,7 +362,7 @@ public CreateActivationResponse createActivation(CreateActivationRequest request
final byte[] nonce = request.getNonce() != null ? Base64.getDecoder().decode(request.getNonce()) : null;
final String version = request.getProtocolVersion();
final Long timestamp = "3.2".equals(version) ? request.getTimestamp() : null;
final byte[] associatedData = "3.2".equals(version) ? EciesUtils.deriveAssociatedData(EciesScope.APPLICATION_SCOPE, version, applicationKey, null) : null;
final byte[] associatedData = EciesUtils.deriveAssociatedData(EciesScope.APPLICATION_SCOPE, version, applicationKey, null);
final EciesCryptogram eciesCryptogram = EciesCryptogram.builder().ephemeralPublicKey(ephemeralPublicKey).mac(mac).encryptedData(encryptedData).build();
final EciesParameters eciesParameters = EciesParameters.builder().nonce(nonce).associatedData(associatedData).timestamp(timestamp).build();
final EciesPayload eciesPayload = new EciesPayload(eciesCryptogram, eciesParameters);
Expand Down Expand Up @@ -694,7 +694,7 @@ public VaultUnlockResponse vaultUnlock(VaultUnlockRequest request) throws Generi

// Convert received ECIES request data to cryptogram
final Long timestamp = "3.2".equals(signatureVersion) ? request.getTimestamp() : null;
final byte[] associatedData = "3.2".equals(signatureVersion) ? EciesUtils.deriveAssociatedData(EciesScope.ACTIVATION_SCOPE, signatureVersion, applicationKey, activationId) : null;
final byte[] associatedData = EciesUtils.deriveAssociatedData(EciesScope.ACTIVATION_SCOPE, signatureVersion, applicationKey, activationId);

final EciesCryptogram eciesCryptogram = EciesCryptogram.builder().ephemeralPublicKey(ephemeralPublicKey).mac(mac).encryptedData(encryptedData).build();
final EciesParameters eciesParameters = EciesParameters.builder().nonce(nonce).associatedData(associatedData).timestamp(timestamp).build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -983,7 +983,7 @@ public PrepareActivationResponse prepareActivation(String activationCode, String
final byte[] responseData = objectMapper.writeValueAsBytes(layer2Response);

// Encrypt response data
final byte[] nonceBytesResponse = "3.2".equals(version) ? keyGenerator.generateRandomBytes(16) : null;
final byte[] nonceBytesResponse = "3.2".equals(version) ? keyGenerator.generateRandomBytes(16) : eciesPayload.getParameters().getNonce();
final Long timestampResponse = "3.2".equals(version) ? new Date().getTime() : null;
final EciesParameters parametersResponse = EciesParameters.builder().nonce(nonceBytesResponse).associatedData(eciesPayload.getParameters().getAssociatedData()).timestamp(timestampResponse).build();
final EciesEncryptor encryptorResponse = eciesFactory.getEciesEncryptor(EciesScope.APPLICATION_SCOPE,
Expand All @@ -1004,7 +1004,7 @@ public PrepareActivationResponse prepareActivation(String activationCode, String
encryptedResponse.setApplicationId(applicationId);
encryptedResponse.setEncryptedData(encryptedData);
encryptedResponse.setMac(mac);
encryptedResponse.setNonce(nonceBytesResponse != null ? Base64.getEncoder().encodeToString(nonceBytesResponse) : null);
encryptedResponse.setNonce("3.2".equals(version) && nonceBytesResponse != null ? Base64.getEncoder().encodeToString(nonceBytesResponse) : null);
encryptedResponse.setTimestamp(timestampResponse);
encryptedResponse.setActivationStatus(activationStatusConverter.convert(activationStatus));
return encryptedResponse;
Expand Down Expand Up @@ -1193,9 +1193,9 @@ public CreateActivationResponse createActivation(
final byte[] responseData = objectMapper.writeValueAsBytes(layer2Response);

// Encrypt response data
final byte[] nonceBytesResponse = "3.2".equals(version) ? keyGenerator.generateRandomBytes(16) : null;
final byte[] nonceBytesResponse = "3.2".equals(version) ? keyGenerator.generateRandomBytes(16) : eciesPayload.getParameters().getNonce();
final Long timestampResponse = "3.2".equals(version) ? new Date().getTime() : null;
final byte[] associatedData = "3.2".equals(version) ? EciesUtils.deriveAssociatedData(EciesScope.APPLICATION_SCOPE, version, applicationKey, null) : null;
final byte[] associatedData = EciesUtils.deriveAssociatedData(EciesScope.APPLICATION_SCOPE, version, applicationKey, null);
final EciesParameters parametersResponse = EciesParameters.builder().nonce(nonceBytesResponse).associatedData(associatedData).timestamp(timestampResponse).build();
final EciesEncryptor encryptorResponse = eciesFactory.getEciesEncryptor(EciesScope.APPLICATION_SCOPE,
eciesDecryptor.getEnvelopeKey(), applicationSecret, null, parametersResponse);
Expand All @@ -1211,7 +1211,7 @@ public CreateActivationResponse createActivation(
encryptedResponse.setApplicationId(applicationId);
encryptedResponse.setEncryptedData(encryptedData);
encryptedResponse.setMac(mac);
encryptedResponse.setNonce(nonceBytesResponse != null ? Base64.getEncoder().encodeToString(nonceBytesResponse) : null);
encryptedResponse.setNonce("3.2".equals(version) && nonceBytesResponse != null ? Base64.getEncoder().encodeToString(nonceBytesResponse) : null);
encryptedResponse.setTimestamp(timestampResponse);
encryptedResponse.setActivationStatus(activationStatusConverter.convert(activation.getActivationStatus()));
return encryptedResponse;
Expand Down Expand Up @@ -1616,7 +1616,7 @@ public RecoveryCodeActivationResponse createActivationUsingRecoveryCode(Recovery
final byte[] nonceBytes = request.getNonce() != null ? Base64.getDecoder().decode(request.getNonce()) : null;
final String version = request.getProtocolVersion();
final Long timestamp = "3.2".equals(version) ? request.getTimestamp() : null;
final byte[] associatedData = "3.2".equals(version) ? EciesUtils.deriveAssociatedData(EciesScope.APPLICATION_SCOPE, version, applicationKey, null) : null;
final byte[] associatedData = EciesUtils.deriveAssociatedData(EciesScope.APPLICATION_SCOPE, version, applicationKey, null);
final EciesCryptogram eciesCryptogram = EciesCryptogram.builder().ephemeralPublicKey(ephemeralPublicKeyBytes).mac(macBytes).encryptedData(encryptedDataBytes).build();
final EciesParameters eciesParameters = EciesParameters.builder().nonce(nonceBytes).associatedData(associatedData).timestamp(timestamp).build();
final EciesPayload eciesPayload = new EciesPayload(eciesCryptogram, eciesParameters);
Expand Down Expand Up @@ -1866,7 +1866,7 @@ public RecoveryCodeActivationResponse createActivationUsingRecoveryCode(Recovery
final byte[] responseData = objectMapper.writeValueAsBytes(layer2Response);

// Encrypt response data
final byte[] nonceBytesResponse = "3.2".equals(version) ? keyGenerator.generateRandomBytes(16) : null;
final byte[] nonceBytesResponse = "3.2".equals(version) ? keyGenerator.generateRandomBytes(16) : nonceBytes;
final Long timestampResponse = "3.2".equals(version) ? new Date().getTime() : null;
final EciesParameters parametersResponse = EciesParameters.builder().nonce(nonceBytesResponse).associatedData(eciesPayload.getParameters().getAssociatedData()).timestamp(timestampResponse).build();
final EciesEncryptor encryptorResponse = eciesFactory.getEciesEncryptor(EciesScope.APPLICATION_SCOPE,
Expand All @@ -1882,7 +1882,7 @@ public RecoveryCodeActivationResponse createActivationUsingRecoveryCode(Recovery
encryptedResponse.setApplicationId(applicationId);
encryptedResponse.setEncryptedData(encryptedDataResponse);
encryptedResponse.setMac(macResponse);
encryptedResponse.setNonce(nonceBytesResponse != null ? Base64.getEncoder().encodeToString(nonceBytesResponse) : null);
encryptedResponse.setNonce("3.2".equals(version) && nonceBytesResponse != null ? Base64.getEncoder().encodeToString(nonceBytesResponse) : null);
encryptedResponse.setTimestamp(timestampResponse);
encryptedResponse.setActivationStatus(activationStatusConverter.convert(activation.getActivationStatus()));
return encryptedResponse;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ private GetEciesDecryptorResponse getEciesDecryptorParametersForApplication(GetE
final byte[] nonceBytes = request.getNonce() != null ? Base64.getDecoder().decode(request.getNonce()) : null;
final String version = request.getProtocolVersion();
final Long timestamp = "3.2".equals(version) ? request.getTimestamp() : null;
final byte[] associatedData = "3.2".equals(version) ? EciesUtils.deriveAssociatedData(EciesScope.APPLICATION_SCOPE, version, applicationKey, null) : null;
final byte[] associatedData = EciesUtils.deriveAssociatedData(EciesScope.APPLICATION_SCOPE, version, applicationKey, null);
final EciesParameters eciesParameters = EciesParameters.builder().nonce(nonceBytes).timestamp(timestamp).associatedData(associatedData).build();
final byte[] ephemeralPublicKeyBytes = Base64.getDecoder().decode(request.getEphemeralPublicKey());
// Get decryptor for the application
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@ public ConfirmRecoveryCodeResponse confirmRecoveryCode(ConfirmRecoveryCodeReques
final byte[] nonceBytesRequest = nonceRequest != null ? Base64.getDecoder().decode(nonceRequest) : null;
final String version = request.getProtocolVersion();
final Long timestampRequest = "3.2".equals(version) ? request.getTimestamp() : null;
final byte[] associatedData = "3.2".equals(version) ? EciesUtils.deriveAssociatedData(EciesScope.ACTIVATION_SCOPE, version, applicationKey, activationId) : null;
final byte[] associatedData = EciesUtils.deriveAssociatedData(EciesScope.ACTIVATION_SCOPE, version, applicationKey, activationId);
// Decrypt request data
final byte[] encryptedDataBytes = Base64.getDecoder().decode(encryptedData);
final byte[] macBytes = Base64.getDecoder().decode(mac);
Expand Down Expand Up @@ -400,7 +400,7 @@ public ConfirmRecoveryCodeResponse confirmRecoveryCode(ConfirmRecoveryCodeReques
final byte[] responseBytes = objectMapper.writeValueAsBytes(responsePayload);

// Encrypt response using ECIES encryptor
final byte[] nonceBytesResponse = "3.2".equals(version) ? keyGenerator.generateRandomBytes(16) : null;
final byte[] nonceBytesResponse = "3.2".equals(version) ? keyGenerator.generateRandomBytes(16) : nonceBytesRequest;
final Long timestampResponse = "3.2".equals(version) ? new Date().getTime() : null;

final EciesParameters parametersResponse = EciesParameters.builder().nonce(nonceBytesResponse).associatedData(associatedData).timestamp(timestampResponse).build();
Expand All @@ -417,7 +417,7 @@ public ConfirmRecoveryCodeResponse confirmRecoveryCode(ConfirmRecoveryCodeReques
response.setUserId(recoveryCodeEntity.getUserId());
response.setEncryptedData(encryptedDataResponse);
response.setMac(macResponse);
response.setNonce(nonceBytesResponse != null ? Base64.getEncoder().encodeToString(nonceBytesResponse) : null);
response.setNonce("3.2".equals(version) ? Base64.getEncoder().encodeToString(nonceBytesResponse) : null);
response.setTimestamp(timestampResponse);

// Confirm recovery code and persist it
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ public CreateTokenResponse createToken(CreateTokenRequest request, KeyConvertor
final byte[] nonce = request.getNonce() != null ? Base64.getDecoder().decode(request.getNonce()) : null;
final String version = request.getProtocolVersion();
final Long timestamp = "3.2".equals(version) ? request.getTimestamp() : null;
final byte[] associatedData = "3.2".equals(version) ? EciesUtils.deriveAssociatedData(EciesScope.ACTIVATION_SCOPE, version, applicationKey, activationId) : null;
final byte[] associatedData = EciesUtils.deriveAssociatedData(EciesScope.ACTIVATION_SCOPE, version, applicationKey, activationId);
final EciesCryptogram eciesCryptogram = EciesCryptogram.builder().ephemeralPublicKey(ephemeralPublicKey).mac(mac).encryptedData(encryptedData).build();
final EciesParameters eciesParameters = EciesParameters.builder().nonce(nonce).associatedData(associatedData).timestamp(timestamp).build();
final EciesPayload eciesPayload = new EciesPayload(eciesCryptogram, eciesParameters);
Expand All @@ -147,7 +147,7 @@ public CreateTokenResponse createToken(CreateTokenRequest request, KeyConvertor
final CreateTokenResponse response = new CreateTokenResponse();
response.setMac(Base64.getEncoder().encodeToString(responseEciesPayload.getCryptogram().getMac()));
response.setEncryptedData(Base64.getEncoder().encodeToString(responseEciesPayload.getCryptogram().getEncryptedData()));
response.setNonce(responseEciesPayload.getParameters().getNonce() != null ? Base64.getEncoder().encodeToString(responseEciesPayload.getParameters().getNonce()) : null);
response.setNonce("3.2".equals(version) && responseEciesPayload.getParameters().getNonce() != null ? Base64.getEncoder().encodeToString(responseEciesPayload.getParameters().getNonce()) : null);
response.setTimestamp(responseEciesPayload.getParameters().getTimestamp());
return response;
}
Expand Down Expand Up @@ -247,7 +247,7 @@ private EciesPayload createToken(String activationId, String applicationKey, Eci
final byte[] tokenBytes = objectMapper.writeValueAsBytes(tokenInfo);

// Encrypt response using previously created ECIES decryptor
final byte[] nonceBytesResponse = "3.2".equals(version) ? keyGenerator.generateRandomBytes(16) : null;
final byte[] nonceBytesResponse = "3.2".equals(version) ? keyGenerator.generateRandomBytes(16) : eciesPayload.getParameters().getNonce();
final Long timestampResponse = "3.2".equals(version) ? new Date().getTime() : null;
final EciesParameters parametersResponse = EciesParameters.builder().nonce(nonceBytesResponse).associatedData(eciesPayload.getParameters().getAssociatedData()).timestamp(timestampResponse).build();
final EciesEncryptor encryptorResponse = eciesFactory.getEciesEncryptor(EciesScope.ACTIVATION_SCOPE,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ public StartUpgradeResponse startUpgrade(StartUpgradeRequest request) throws Gen
final byte[] nonceBytes = nonce != null ? Base64.getDecoder().decode(nonce) : null;
final String version = request.getProtocolVersion();
final Long timestamp = "3.2".equals(version) ? request.getTimestamp() : null;
final byte[] associatedData = "3.2".equals(version) ? EciesUtils.deriveAssociatedData(EciesScope.ACTIVATION_SCOPE, version, applicationKey, activationId) : null;
final byte[] associatedData = EciesUtils.deriveAssociatedData(EciesScope.ACTIVATION_SCOPE, version, applicationKey, activationId);
final EciesCryptogram eciesCryptogram = EciesCryptogram.builder().ephemeralPublicKey(ephemeralPublicKeyBytes).mac(macBytes).encryptedData(encryptedDataBytes).build();
final EciesParameters eciesParameters = EciesParameters.builder().nonce(nonceBytes).associatedData(associatedData).timestamp(timestamp).build();
final EciesPayload eciesPayload = new EciesPayload(eciesCryptogram, eciesParameters);
Expand Down Expand Up @@ -226,7 +226,7 @@ public StartUpgradeResponse startUpgrade(StartUpgradeRequest request) throws Gen
// Encrypt response payload and return it
final byte[] payloadBytes = objectMapper.writeValueAsBytes(payload);

final byte[] nonceBytesResponse = "3.2".equals(version) ? keyGenerator.generateRandomBytes(16) : null;
final byte[] nonceBytesResponse = "3.2".equals(version) ? keyGenerator.generateRandomBytes(16) : nonceBytes;
final Long timestampResponse = "3.2".equals(version) ? new Date().getTime() : null;
final EciesParameters parametersResponse = EciesParameters.builder().nonce(nonceBytesResponse).associatedData(eciesPayload.getParameters().getAssociatedData()).timestamp(timestampResponse).build();
final EciesEncryptor encryptorResponse = eciesFactory.getEciesEncryptor(EciesScope.ACTIVATION_SCOPE,
Expand All @@ -236,7 +236,7 @@ public StartUpgradeResponse startUpgrade(StartUpgradeRequest request) throws Gen
final StartUpgradeResponse response = new StartUpgradeResponse();
response.setEncryptedData(Base64.getEncoder().encodeToString(payloadResponse.getCryptogram().getEncryptedData()));
response.setMac(Base64.getEncoder().encodeToString(payloadResponse.getCryptogram().getMac()));
response.setNonce(nonceBytesResponse != null ? Base64.getEncoder().encodeToString(nonceBytesResponse) : null);
response.setNonce("3.2".equals(version) && nonceBytesResponse != null ? Base64.getEncoder().encodeToString(nonceBytesResponse) : null);
response.setTimestamp(timestampResponse);

// Save activation as last step to avoid rollbacks
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ public VaultUnlockResponse unlockVault(String activationId, String applicationKe
final byte[] reponsePayloadBytes = objectMapper.writeValueAsBytes(responsePayload);

// Encrypt response payload
final byte[] nonceBytesResponse = "3.2".equals(signatureVersion) ? keyGenerator.generateRandomBytes(16) : null;
final byte[] nonceBytesResponse = "3.2".equals(signatureVersion) ? keyGenerator.generateRandomBytes(16) : eciesPayload.getParameters().getNonce();
final Long timestampResponse = "3.2".equals(signatureVersion) ? new Date().getTime() : null;
final EciesParameters parametersResponse = EciesParameters.builder().nonce(nonceBytesResponse).associatedData(eciesPayload.getParameters().getAssociatedData()).timestamp(timestampResponse).build();
final EciesEncryptor encryptorResponse = eciesFactory.getEciesEncryptor(EciesScope.ACTIVATION_SCOPE,
Expand All @@ -239,7 +239,7 @@ public VaultUnlockResponse unlockVault(String activationId, String applicationKe
final VaultUnlockResponse response = new VaultUnlockResponse();
response.setEncryptedData(dataResponse);
response.setMac(macResponse);
response.setNonce(nonceBytesResponse != null ? Base64.getEncoder().encodeToString(nonceBytesResponse) : null);
response.setNonce("3.2".equals(signatureVersion) && nonceBytesResponse != null ? Base64.getEncoder().encodeToString(nonceBytesResponse) : null);
response.setTimestamp(timestampResponse);
response.setSignatureValid(signatureResponse.isSignatureValid());
return response;
Expand Down

0 comments on commit 1c9f7ef

Please sign in to comment.