diff --git a/service/src/main/java/org/whispersystems/textsecuregcm/controllers/RegistrationController.java b/service/src/main/java/org/whispersystems/textsecuregcm/controllers/RegistrationController.java index d7b9710c6..8b0fb9fcc 100644 --- a/service/src/main/java/org/whispersystems/textsecuregcm/controllers/RegistrationController.java +++ b/service/src/main/java/org/whispersystems/textsecuregcm/controllers/RegistrationController.java @@ -141,7 +141,7 @@ public class RegistrationController { """) @ApiResponse(responseCode = "200", description = "Account creation succeeded", useReturnTypeSchema = true) @ApiResponse(responseCode = "400", description = "Invalid session ID or receipt credential presentation") - @ApiResponse(responseCode = "401", description = "The session identified in the request is not verified") + @ApiResponse(responseCode = "401", description = "The session identified in the request is not verified, or the receipt credential presented fails verification, is expired, or is already redeemed") @ApiResponse(responseCode = "403", description = "Verification failed for the provided Registration Recovery Password") @ApiResponse(responseCode = "409", description = "The caller has not explicitly elected to skip transferring data from another device, but a device transfer is technically possible") @ApiResponse(responseCode = "422", description = "The request did not pass validation") @@ -348,12 +348,12 @@ public class RegistrationController { try { serverZkReceiptOperations.verifyReceiptCredentialPresentation(receiptCredentialPresentation); } catch (VerificationFailedException _) { - throw new BadRequestException("Receipt credential presentation verification failed"); + throw new NotAuthorizedException("Receipt credential presentation verification failed"); } final Instant receiptExpiration = Instant.ofEpochSecond(receiptCredentialPresentation.getReceiptExpirationTime()); if (clock.instant().isAfter(receiptExpiration)) { - throw new BadRequestException("Receipt is already expired"); + throw new NotAuthorizedException("Receipt is already expired"); } final long receiptLevel = receiptCredentialPresentation.getReceiptLevel(); @@ -386,7 +386,7 @@ public class RegistrationController { final AccountIdentityResponse accountIdentityResponse = new AccountIdentityResponseBuilder(account).build(); return new AccountCreationResponse(accountIdentityResponse, false); } catch (ReceiptAlreadyRedeemedException _) { - throw new BadRequestException("Receipt already redeemed"); + throw new NotAuthorizedException("Receipt already redeemed"); } } diff --git a/service/src/test/java/org/whispersystems/textsecuregcm/controllers/RegistrationControllerTest.java b/service/src/test/java/org/whispersystems/textsecuregcm/controllers/RegistrationControllerTest.java index 0128c3502..f54eabad4 100644 --- a/service/src/test/java/org/whispersystems/textsecuregcm/controllers/RegistrationControllerTest.java +++ b/service/src/test/java/org/whispersystems/textsecuregcm/controllers/RegistrationControllerTest.java @@ -67,8 +67,10 @@ import org.junitpioneer.jupiter.cartesian.CartesianTest; import org.signal.libsignal.protocol.IdentityKey; import org.signal.libsignal.protocol.ecc.ECKeyPair; import org.signal.libsignal.zkgroup.InvalidInputException; +import org.signal.libsignal.zkgroup.ServerSecretParams; import org.signal.libsignal.zkgroup.VerificationFailedException; import org.signal.libsignal.zkgroup.receipts.ReceiptCredentialPresentation; +import org.signal.libsignal.zkgroup.receipts.ReceiptSerial; import org.signal.libsignal.zkgroup.receipts.ServerZkReceiptOperations; import org.whispersystems.textsecuregcm.auth.InvalidRegistrationSessionException; import org.whispersystems.textsecuregcm.auth.PhoneVerificationTokenManager; @@ -102,6 +104,7 @@ import org.whispersystems.textsecuregcm.storage.DeviceIdentityInfo; import org.whispersystems.textsecuregcm.storage.DeviceSpec; import org.whispersystems.textsecuregcm.storage.DynamicConfigurationManager; import org.whispersystems.textsecuregcm.storage.ReceiptAlreadyRedeemedException; +import org.whispersystems.textsecuregcm.storage.ReceiptCredentialTestUtil; import org.whispersystems.textsecuregcm.subscriptions.ReceiptLevel; import org.whispersystems.textsecuregcm.tests.util.AuthHelper; import org.whispersystems.textsecuregcm.tests.util.KeysHelper; @@ -147,8 +150,8 @@ class RegistrationControllerTest { .setMapper(SystemMapper.jsonMapper()) .setTestContainerFactory(new GrizzlyWebTestContainerFactory()) .addResource(new RegistrationController(accountsManager, phoneVerificationTokenManager, - registrationLockVerificationManager, rateLimiters, registrationFraudChecker, ReceiptCredentialPresentation::new, mock( - ServerZkReceiptOperations.class), CLOCK, DYNAMIC_CONFIGURATION_MANAGER)) + registrationLockVerificationManager, rateLimiters, registrationFraudChecker, ReceiptCredentialPresentation::new, + new ServerZkReceiptOperations(ReceiptCredentialTestUtil.RECEIPT_PARAMS), CLOCK, DYNAMIC_CONFIGURATION_MANAGER)) .build(); @BeforeEach @@ -880,7 +883,7 @@ class RegistrationControllerTest { @ParameterizedTest @MethodSource - void registerAccountBadReceipt(final byte[] receiptCredentialPresentation) { + void registerAccountBadReceipt(final byte[] receiptCredentialPresentation, final int expectedStatusCode) { final ECKeyPair aciIdentityKeyPair = ECKeyPair.generate(); final IdentityKey aciIdentityKey = new IdentityKey(aciIdentityKeyPair.getPublicKey()); final ECSignedPreKey aciSignedPreKey = KeysHelper.signedECPreKey(1, aciIdentityKeyPair); @@ -923,7 +926,7 @@ class RegistrationControllerTest { .header(HttpHeaders.AUTHORIZATION, AuthHelper.getProvisioningAuthHeader(NUMBER, PASSWORD)); try (Response response = request.post(Entity.json(requestToJson(registrationRequest)))) { - assertEquals(400, response.getStatus()); + assertEquals(expectedStatusCode, response.getStatus()); } verifyNoInteractions(phoneVerificationTokenManager); @@ -932,9 +935,14 @@ class RegistrationControllerTest { static Stream registerAccountBadReceipt() throws InvalidInputException, VerificationFailedException { return Stream.of( - Arguments.argumentSet("malformed receipt", new byte[]{0, 0}), - Arguments.argumentSet("expired receipt", receiptPresentation(CLOCK.instant().minusSeconds(5), ReceiptLevel.LOGIN.getValue()).serialize()), - Arguments.argumentSet("wrong receipt level", receiptPresentation(CLOCK.instant().plus(Duration.ofDays(30)), ReceiptLevel.BACKUP_PAID.getValue()).serialize()) + Arguments.argumentSet("malformed receipt", new byte[]{0, 0}, 400), + Arguments.argumentSet("expired receipt", + receiptPresentation(CLOCK.instant().minusSeconds(5), ReceiptLevel.LOGIN.getValue()).serialize(), 401), + Arguments.argumentSet("receipt signed by wrong params", receiptPresentation( + ServerSecretParams.generate(), new ReceiptSerial(TestRandomUtil.nextBytes(ReceiptSerial.SIZE)), + CLOCK.instant().plus(Duration.ofDays(30)), ReceiptLevel.LOGIN.getValue()).serialize(), 401), + Arguments.argumentSet("wrong receipt level", receiptPresentation(CLOCK.instant().plus(Duration.ofDays(30)), + ReceiptLevel.BACKUP_PAID.getValue()).serialize(), 400) ); } @@ -1123,7 +1131,7 @@ class RegistrationControllerTest { final RegistrationRequest registrationRequest = new RegistrationRequest(null, new byte[0], - receiptPresentation(CLOCK.instant().plus(Duration.ofDays(30)), 1).serialize(), + receiptPresentation(CLOCK.instant().plus(Duration.ofDays(30)), ReceiptLevel.LOGIN.getValue()).serialize(), null, accountAttributes, true, @@ -1145,7 +1153,8 @@ class RegistrationControllerTest { .header(HttpHeaders.AUTHORIZATION, AuthHelper.getProvisioningAuthHeader(NUMBER, PASSWORD)); try (Response response = request.post(Entity.json(requestToJson(registrationRequest)))) { - assertEquals(400, response.getStatus()); + assertEquals(401, response.getStatus()); + verify(accountsManager).create(any(), any(), any(), any(), any()); } } diff --git a/service/src/test/java/org/whispersystems/textsecuregcm/storage/ReceiptCredentialTestUtil.java b/service/src/test/java/org/whispersystems/textsecuregcm/storage/ReceiptCredentialTestUtil.java index 65832c87e..e9eb6caa2 100644 --- a/service/src/test/java/org/whispersystems/textsecuregcm/storage/ReceiptCredentialTestUtil.java +++ b/service/src/test/java/org/whispersystems/textsecuregcm/storage/ReceiptCredentialTestUtil.java @@ -15,7 +15,7 @@ import java.time.Instant; import java.time.temporal.ChronoUnit; public class ReceiptCredentialTestUtil { - private static final ServerSecretParams RECEIPT_PARAMS = ServerSecretParams.generate(); + public static final ServerSecretParams RECEIPT_PARAMS = ServerSecretParams.generate(); public static ReceiptCredentialPresentation receiptPresentation() throws InvalidInputException, VerificationFailedException { @@ -30,12 +30,16 @@ public class ReceiptCredentialTestUtil { return receiptPresentation(new ReceiptSerial(TestRandomUtil.nextBytes(ReceiptSerial.SIZE)), expiresAt, receiptLevel); } - public static ReceiptCredentialPresentation receiptPresentation(final ReceiptSerial receiptSerial, final Instant expiresAt, final long receiptLevel) + public static ReceiptCredentialPresentation receiptPresentation(final ReceiptSerial receiptSerial, final Instant expiresAt, final long receiptLevel) throws VerificationFailedException { + return receiptPresentation(RECEIPT_PARAMS, receiptSerial, expiresAt, receiptLevel); + } + + public static ReceiptCredentialPresentation receiptPresentation(final ServerSecretParams receiptParams, final ReceiptSerial receiptSerial, final Instant expiresAt, final long receiptLevel) throws VerificationFailedException { - final ServerZkReceiptOperations serverOperations = new ServerZkReceiptOperations(RECEIPT_PARAMS); + final ServerZkReceiptOperations serverOperations = new ServerZkReceiptOperations(receiptParams); final ClientZkReceiptOperations clientOperations = - new ClientZkReceiptOperations(RECEIPT_PARAMS.getPublicParams()); + new ClientZkReceiptOperations(receiptParams.getPublicParams()); final ReceiptCredentialRequestContext requestContext = clientOperations.createReceiptCredentialRequestContext(receiptSerial);