mirror of
https://github.com/signalapp/Signal-Server.git
synced 2026-11-30 01:16:50 +00:00
Do not allow accounts without phone numbers to perform PNI key operations
This commit is contained in:
1 parent
d7447b111d
commit
29c5f8b218
7 files changed
+237
-59
No files matched your search
+18
-4
@@ -108,6 +108,7 @@ public class KeysController {
|
||||
@Operation(summary = "Get prekey count",
|
||||
description = "Gets the number of one-time prekeys uploaded for this device and still available")
|
||||
@ApiResponse(responseCode = "200", description = "Body contains the number of available one-time prekeys for the device.", useReturnTypeSchema = true)
|
||||
@ApiResponse(responseCode = "400", description = "The request was malformed or the account does not support the requested identityType")
|
||||
@ApiResponse(responseCode = "401", description = "Account authentication check failed.")
|
||||
public CompletableFuture<PreKeyCount> getStatus(@Auth final AuthenticatedDevice auth,
|
||||
@QueryParam("identity") @DefaultValue("aci") final IdentityType identityType) {
|
||||
@@ -115,12 +116,13 @@ public class KeysController {
|
||||
return accounts.getByAccountIdentifierAsync(auth.accountIdentifier())
|
||||
.thenCompose(maybeAccount -> {
|
||||
final Account account = maybeAccount.orElseThrow(() -> new WebApplicationException(Response.Status.UNAUTHORIZED));
|
||||
final UUID identifier = getIdentifier(account, identityType);
|
||||
|
||||
final CompletableFuture<Integer> ecCountFuture =
|
||||
keysManager.getEcCount(account.getIdentifier(identityType), auth.deviceId());
|
||||
keysManager.getEcCount(identifier, auth.deviceId());
|
||||
|
||||
final CompletableFuture<Integer> pqCountFuture =
|
||||
keysManager.getPqCount(account.getIdentifier(identityType), auth.deviceId());
|
||||
keysManager.getPqCount(identifier, auth.deviceId());
|
||||
|
||||
return ecCountFuture.thenCombine(pqCountFuture, PreKeyCount::new);
|
||||
});
|
||||
@@ -131,6 +133,7 @@ public class KeysController {
|
||||
@Produces(MediaType.APPLICATION_JSON)
|
||||
@Operation(summary = "Upload new prekeys", description = "Upload new pre-keys for this device.")
|
||||
@ApiResponse(responseCode = "200", description = "Indicates that new keys were successfully stored.")
|
||||
@ApiResponse(responseCode = "400", description = "The request was malformed or the account does not support the requested identityType")
|
||||
@ApiResponse(responseCode = "401", description = "Account authentication check failed.")
|
||||
@ApiResponse(responseCode = "403", description = "Attempt to change identity key from a non-primary device.")
|
||||
@ApiResponse(responseCode = "422", description = "Invalid request format.")
|
||||
@@ -154,7 +157,7 @@ public class KeysController {
|
||||
final Device device = account.getDevice(auth.deviceId())
|
||||
.orElseThrow(() -> new WebApplicationException(Response.Status.UNAUTHORIZED));
|
||||
|
||||
final UUID identifier = account.getIdentifier(identityType);
|
||||
final UUID identifier = getIdentifier(account, identityType);
|
||||
|
||||
checkSignedPreKeySignatures(setKeysRequest, account.getIdentityKey(identityType), userAgent);
|
||||
|
||||
@@ -244,6 +247,7 @@ public class KeysController {
|
||||
version byte followed by 1568 bytes of key material for a total of 1569 bytes)
|
||||
""")
|
||||
@ApiResponse(responseCode = "200", description = "Indicates that client and server have consistent views of repeated-use keys")
|
||||
@ApiResponse(responseCode = "400", description = "The request was malformed or the account does not support the requested identityType")
|
||||
@ApiResponse(responseCode = "401", description = "Account authentication check failed")
|
||||
@ApiResponse(responseCode = "409", description = """
|
||||
Indicates that client and server have inconsistent views of repeated-use keys or one or more repeated-use keys could
|
||||
@@ -258,7 +262,7 @@ public class KeysController {
|
||||
.thenCompose(maybeAccount -> {
|
||||
final Account account = maybeAccount.orElseThrow(() -> new WebApplicationException(Response.Status.UNAUTHORIZED));
|
||||
|
||||
final UUID identifier = account.getIdentifier(checkKeysRequest.identityType());
|
||||
final UUID identifier = getIdentifier(account, checkKeysRequest.identityType());
|
||||
final byte deviceId = auth.deviceId();
|
||||
|
||||
final CompletableFuture<Optional<ECSignedPreKey>> ecSignedPreKeyFuture =
|
||||
@@ -432,4 +436,14 @@ public class KeysController {
|
||||
parsedTargetDeviceId, targetRegistrationId);
|
||||
}
|
||||
|
||||
/// Get the identity of the requested `idenityType`, or throw a [BadRequestException] if `account` does not contain
|
||||
/// that type.
|
||||
private UUID getIdentifier(final Account account, final IdentityType identityType) {
|
||||
return switch (identityType) {
|
||||
case ACI -> account.getAccountIdentifier();
|
||||
case PNI -> account.getPhoneNumberIdentifierOptional().orElseThrow(() ->
|
||||
new BadRequestException("Cannot set PNI identity keys on an account without a number"));
|
||||
};
|
||||
}
|
||||
|
||||
}
|
||||
@@ -60,17 +60,21 @@ public class KeysGrpcService extends SimpleKeysGrpc.KeysImplBase {
|
||||
final AuthenticatedDevice authenticatedDevice = AuthenticationUtil.requireAuthenticatedDevice();
|
||||
final Account account = getAuthenticatedAccount(authenticatedDevice.accountIdentifier());
|
||||
|
||||
final CompletableFuture<Integer> aciEcKeyCountFuture =
|
||||
keysManager.getEcCount(account.getIdentifier(IdentityType.ACI), authenticatedDevice.deviceId());
|
||||
final UUID aci = account.getAccountIdentifier();
|
||||
|
||||
final CompletableFuture<Integer> pniEcKeyCountFuture =
|
||||
keysManager.getEcCount(account.getIdentifier(IdentityType.PNI), authenticatedDevice.deviceId());
|
||||
final CompletableFuture<Integer> aciEcKeyCountFuture =
|
||||
keysManager.getEcCount(aci, authenticatedDevice.deviceId());
|
||||
|
||||
final CompletableFuture<Integer> pniEcKeyCountFuture = account.getPhoneNumberIdentifierOptional()
|
||||
.map(pni -> keysManager.getEcCount(pni, authenticatedDevice.deviceId()))
|
||||
.orElseGet(() -> CompletableFuture.completedFuture(0));
|
||||
|
||||
final CompletableFuture<Integer> aciKemKeyCountFuture =
|
||||
keysManager.getPqCount(account.getIdentifier(IdentityType.ACI), authenticatedDevice.deviceId());
|
||||
keysManager.getPqCount(aci, authenticatedDevice.deviceId());
|
||||
|
||||
final CompletableFuture<Integer> pniKemKeyCountFuture =
|
||||
keysManager.getPqCount(account.getIdentifier(IdentityType.PNI), authenticatedDevice.deviceId());
|
||||
final CompletableFuture<Integer> pniKemKeyCountFuture = account.getPhoneNumberIdentifierOptional()
|
||||
.map(pni -> keysManager.getPqCount(pni, authenticatedDevice.deviceId()))
|
||||
.orElseGet(() -> CompletableFuture.completedFuture(0));
|
||||
|
||||
CompletableFuture.allOf(aciEcKeyCountFuture, pniEcKeyCountFuture, aciKemKeyCountFuture, pniKemKeyCountFuture).join();
|
||||
|
||||
@@ -154,11 +158,13 @@ public class KeysGrpcService extends SimpleKeysGrpc.KeysImplBase {
|
||||
|
||||
final Account account = getAuthenticatedAccount(authenticatedAccountUuid);
|
||||
|
||||
final UUID identifier = getIdentifier(account, identityType);
|
||||
|
||||
final List<K> preKeys = requestPreKeys.stream()
|
||||
.map(requestPreKey -> extractPreKeyFunction.apply(requestPreKey, account.getIdentityKey(identityType)))
|
||||
.toList();
|
||||
|
||||
storeKeysFunction.apply(account.getIdentifier(identityType), preKeys).join();
|
||||
storeKeysFunction.apply(identifier, preKeys).join();
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -166,15 +172,10 @@ public class KeysGrpcService extends SimpleKeysGrpc.KeysImplBase {
|
||||
final AuthenticatedDevice authenticatedDevice = AuthenticationUtil.requireAuthenticatedDevice();
|
||||
|
||||
storeRepeatedUseKey(authenticatedDevice.accountIdentifier(),
|
||||
request.getIdentityType(),
|
||||
IdentityTypeUtil.fromGrpcIdentityType(request.getIdentityType()),
|
||||
request.getSignedPreKey(),
|
||||
(preKey, identityKey) -> KeysGrpcHelper.checkEcSignedPreKey(preKey, identityKey, INVALID_PUBLIC_KEY_EXCEPTION, INVALID_SIGNATURE_EXCEPTION),
|
||||
(account, signedPreKey) -> {
|
||||
final IdentityType identityType = IdentityTypeUtil.fromGrpcIdentityType(request.getIdentityType());
|
||||
final UUID identifier = account.getIdentifier(identityType);
|
||||
|
||||
return keysManager.storeEcSignedPreKeys(identifier, authenticatedDevice.deviceId(), signedPreKey);
|
||||
});
|
||||
(identifier, signedPreKey) -> keysManager.storeEcSignedPreKeys(identifier, authenticatedDevice.deviceId(), signedPreKey));
|
||||
|
||||
return SetPreKeyResponse.getDefaultInstance();
|
||||
}
|
||||
@@ -184,36 +185,42 @@ public class KeysGrpcService extends SimpleKeysGrpc.KeysImplBase {
|
||||
final AuthenticatedDevice authenticatedDevice = AuthenticationUtil.requireAuthenticatedDevice();
|
||||
|
||||
storeRepeatedUseKey(authenticatedDevice.accountIdentifier(),
|
||||
request.getIdentityType(),
|
||||
IdentityTypeUtil.fromGrpcIdentityType(request.getIdentityType()),
|
||||
request.getSignedPreKey(),
|
||||
(preKey, identityKey) -> KeysGrpcHelper.checkKemSignedPreKey(preKey, identityKey, INVALID_PUBLIC_KEY_EXCEPTION, INVALID_SIGNATURE_EXCEPTION),
|
||||
(account, lastResortKey) -> {
|
||||
final UUID identifier =
|
||||
account.getIdentifier(IdentityTypeUtil.fromGrpcIdentityType(request.getIdentityType()));
|
||||
|
||||
return keysManager.storePqLastResort(identifier, authenticatedDevice.deviceId(), lastResortKey);
|
||||
});
|
||||
(identifier, lastResortKey) -> keysManager.storePqLastResort(identifier, authenticatedDevice.deviceId(), lastResortKey));
|
||||
|
||||
return SetPreKeyResponse.getDefaultInstance();
|
||||
}
|
||||
|
||||
private <K, R> void storeRepeatedUseKey(final UUID authenticatedAccountUuid,
|
||||
final org.signal.chat.common.IdentityType identityType,
|
||||
final IdentityType identityType,
|
||||
final R storeKeyRequest,
|
||||
final BiFunction<R, IdentityKey, K> extractKeyFunction,
|
||||
final BiFunction<Account, K, CompletableFuture<Void>> storeKeyFunction) {
|
||||
final BiFunction<UUID, K, CompletableFuture<Void>> storeKeyFunction) {
|
||||
|
||||
final Account account = getAuthenticatedAccount(authenticatedAccountUuid);
|
||||
|
||||
final IdentityKey identityKey = account.getIdentityKey(IdentityTypeUtil.fromGrpcIdentityType(identityType));
|
||||
final UUID identifier = getIdentifier(account, identityType);
|
||||
final IdentityKey identityKey = account.getIdentityKey(identityType);
|
||||
final K key = extractKeyFunction.apply(storeKeyRequest, identityKey);
|
||||
|
||||
storeKeyFunction.apply(account, key).join();
|
||||
storeKeyFunction.apply(identifier, key).join();
|
||||
}
|
||||
|
||||
|
||||
private Account getAuthenticatedAccount(final UUID authenticatedAccountId) {
|
||||
return accountsManager.getByAccountIdentifier(authenticatedAccountId)
|
||||
.orElseThrow(() -> GrpcExceptions.invalidCredentials("invalid credentials"));
|
||||
}
|
||||
|
||||
/// Get the identity of the requested `idenityType`, or throw an invalid arguments status if `account` does not
|
||||
/// contain that type.
|
||||
private static UUID getIdentifier(Account account, IdentityType identityType) {
|
||||
return switch (identityType) {
|
||||
case ACI -> account.getAccountIdentifier();
|
||||
case PNI -> account.getPhoneNumberIdentifierOptional()
|
||||
.orElseThrow(() -> GrpcExceptions.invalidArguments("PNI identity type not allowed for an account without a phone number"));
|
||||
};
|
||||
}
|
||||
|
||||
}
|
||||
@@ -88,11 +88,13 @@ message GetPreKeyCountResponse {
|
||||
uint32 aci_kem_pre_key_count = 2;
|
||||
|
||||
// The approximate number of one-time EC pre-keys stored for the
|
||||
// authenticated device and associated with the caller's PNI.
|
||||
// authenticated device and associated with the caller's PNI. 0 if
|
||||
// the account does not possess a phone number.
|
||||
uint32 pni_ec_pre_key_count = 3;
|
||||
|
||||
// The approximate number of one-time KEM pre-keys stored for the
|
||||
// authenticated device and associated with the caller's PNI.
|
||||
// authenticated device and associated with the caller's PNI. 0 if
|
||||
// the account does not possess a phone number.
|
||||
uint32 pni_kem_pre_key_count = 4;
|
||||
}
|
||||
|
||||
|
||||
+3
-3
@@ -214,7 +214,7 @@ class AccountControllerTest {
|
||||
when(accountsManager.getByAccountIdentifier(AuthHelper.VALID_UUID)).thenReturn(Optional.of(AuthHelper.VALID_ACCOUNT));
|
||||
when(accountsManager.getByAccountIdentifier(AuthHelper.VALID_UUID_TWO)).thenReturn(Optional.of(AuthHelper.VALID_ACCOUNT_TWO));
|
||||
when(accountsManager.getByAccountIdentifier(AuthHelper.VALID_UUID_3)).thenReturn(Optional.of(AuthHelper.VALID_ACCOUNT_3));
|
||||
when(accountsManager.getByAccountIdentifier(AuthHelper.VALID_UUID_4)).thenReturn(Optional.of(AuthHelper.VALID_ACCOUNT_4));
|
||||
when(accountsManager.getByAccountIdentifier(AuthHelper.NUMBERLESS_UUID)).thenReturn(Optional.of(AuthHelper.NUMBERLESS_ACCOUNT));
|
||||
when(accountsManager.getByAccountIdentifier(AuthHelper.UNDISCOVERABLE_UUID)).thenReturn(Optional.of(AuthHelper.UNDISCOVERABLE_ACCOUNT));
|
||||
|
||||
doAnswer(invocation -> {
|
||||
@@ -271,11 +271,11 @@ class AccountControllerTest {
|
||||
|
||||
@Test
|
||||
void testSetRegistrationLockAccountHasNoNumber() {
|
||||
doThrow(new IllegalArgumentException()).when(AuthHelper.VALID_ACCOUNT_4).setRegistrationLock(any(), any());
|
||||
doThrow(new IllegalArgumentException()).when(AuthHelper.NUMBERLESS_ACCOUNT).setRegistrationLock(any(), any());
|
||||
try (final Response response = resources.getJerseyTest()
|
||||
.target("/v1/accounts/registration_lock/")
|
||||
.request()
|
||||
.header(HttpHeaders.AUTHORIZATION, AuthHelper.getAuthHeader(AuthHelper.VALID_UUID_4, AuthHelper.VALID_PASSWORD_4))
|
||||
.header(HttpHeaders.AUTHORIZATION, AuthHelper.getAuthHeader(AuthHelper.NUMBERLESS_UUID, AuthHelper.NUMBERLESS_PASSWORD))
|
||||
.put(Entity.json(new RegistrationLock("1234567890123456789012345678901234567890123456789012345678901234")))) {
|
||||
|
||||
assertThat(response.getStatus()).isEqualTo(400);
|
||||
|
||||
+59
-2
@@ -55,6 +55,7 @@ import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.Arguments;
|
||||
import org.junit.jupiter.params.provider.EnumSource;
|
||||
import org.junit.jupiter.params.provider.MethodSource;
|
||||
import org.junit.jupiter.params.provider.ValueSource;
|
||||
import org.mockito.ArgumentCaptor;
|
||||
import org.signal.libsignal.protocol.IdentityKey;
|
||||
import org.signal.libsignal.protocol.ecc.ECKeyPair;
|
||||
@@ -215,13 +216,16 @@ class KeysControllerTest {
|
||||
when(existsAccount.isIdentifiedBy(new AciServiceIdentifier(EXISTS_UUID))).thenReturn(true);
|
||||
when(existsAccount.getPhoneNumberIdentifier()).thenReturn(EXISTS_PNI);
|
||||
when(existsAccount.isIdentifiedBy(new PniServiceIdentifier(EXISTS_PNI))).thenReturn(true);
|
||||
when(existsAccount.getIdentifier(IdentityType.ACI)).thenReturn(EXISTS_UUID);
|
||||
when(existsAccount.getIdentifier(IdentityType.PNI)).thenReturn(EXISTS_PNI);
|
||||
when(existsAccount.getIdentifier(any())).thenThrow(new UnsupportedOperationException());
|
||||
when(existsAccount.getPhoneNumberIdentifier()).thenThrow(new UnsupportedOperationException());
|
||||
when(existsAccount.getAccountIdentifier()).thenReturn(EXISTS_ACI.uuid());
|
||||
when(existsAccount.getPhoneNumberIdentifierOptional()).thenReturn(Optional.of(EXISTS_PNI));
|
||||
when(existsAccount.getDevice(SAMPLE_DEVICE_ID)).thenReturn(Optional.of(sampleDevice));
|
||||
when(existsAccount.getDevices()).thenReturn(List.of(sampleDevice));
|
||||
when(existsAccount.getIdentityKey(IdentityType.ACI)).thenReturn(IDENTITY_KEY);
|
||||
when(existsAccount.getIdentityKey(IdentityType.PNI)).thenReturn(PNI_IDENTITY_KEY);
|
||||
when(existsAccount.getNumber()).thenReturn(EXISTS_NUMBER);
|
||||
when(existsAccount.getNumberOptional()).thenReturn(Optional.of(EXISTS_NUMBER));
|
||||
when(existsAccount.getUnidentifiedAccessKey()).thenReturn(Optional.of(EXISTS_UAK));
|
||||
|
||||
when(accounts.getByServiceIdentifier(any())).thenReturn(Optional.empty());
|
||||
@@ -240,6 +244,11 @@ class KeysControllerTest {
|
||||
when(accounts.getByAccountIdentifierAsync(AuthHelper.VALID_UUID))
|
||||
.thenReturn(CompletableFuture.completedFuture(Optional.of(AuthHelper.VALID_ACCOUNT)));
|
||||
|
||||
when(accounts.getByAccountIdentifier(AuthHelper.NUMBERLESS_UUID))
|
||||
.thenReturn(Optional.of(AuthHelper.NUMBERLESS_ACCOUNT));
|
||||
when(accounts.getByAccountIdentifierAsync(AuthHelper.NUMBERLESS_UUID))
|
||||
.thenReturn(CompletableFuture.completedFuture(Optional.of(AuthHelper.NUMBERLESS_ACCOUNT)));
|
||||
|
||||
when(rateLimiters.getPreKeysLimiter()).thenReturn(rateLimiter);
|
||||
|
||||
when(KEYS.storeEcOneTimePreKeys(any(), anyByte(), any()))
|
||||
@@ -841,6 +850,23 @@ class KeysControllerTest {
|
||||
verify(KEYS).storeEcSignedPreKeys(AuthHelper.VALID_PNI, AuthHelper.VALID_DEVICE.getId(), signedPreKey);
|
||||
}
|
||||
|
||||
@Test
|
||||
void putKeysNoPhoneNumberTest() {
|
||||
final ECPreKey preKey = KeysHelper.ecPreKey(31337);
|
||||
final ECSignedPreKey signedPreKey = KeysHelper.signedECPreKey(31338, AuthHelper.VALID_PNI_IDENTITY_KEY_PAIR);
|
||||
|
||||
final SetKeysRequest setKeysRequest = new SetKeysRequest(List.of(preKey), signedPreKey, null, null);
|
||||
final Response response =
|
||||
resources.getJerseyTest()
|
||||
.target("/v2/keys")
|
||||
.queryParam("identity", "pni")
|
||||
.request()
|
||||
.header("Authorization", AuthHelper.getAuthHeader(AuthHelper.NUMBERLESS_UUID, AuthHelper.NUMBERLESS_PASSWORD))
|
||||
.put(Entity.entity(setKeysRequest, MediaType.APPLICATION_JSON_TYPE));
|
||||
|
||||
assertThat(response.getStatus()).isEqualTo(400);
|
||||
}
|
||||
|
||||
@Test
|
||||
void putKeysByPhoneNumberIdentifierPqTestV2() {
|
||||
final ECPreKey preKey = KeysHelper.ecPreKey(31337);
|
||||
@@ -1046,4 +1072,35 @@ class KeysControllerTest {
|
||||
assertEquals(422, response.getStatus());
|
||||
}
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(booleans = {false, true})
|
||||
void checkKeysPni(final boolean hasE164) throws NoSuchAlgorithmException {
|
||||
final ECSignedPreKey ecSignedPreKey = KeysHelper.signedECPreKey(17, AuthHelper.VALID_PNI_IDENTITY_KEY_PAIR);
|
||||
final KEMSignedPreKey lastResortKey = KeysHelper.signedKEMPreKey(19, AuthHelper.VALID_PNI_IDENTITY_KEY_PAIR);
|
||||
|
||||
if (hasE164) {
|
||||
when(KEYS.getEcSignedPreKey(AuthHelper.VALID_PNI, Device.PRIMARY_ID))
|
||||
.thenReturn(CompletableFuture.completedFuture(Optional.of(ecSignedPreKey)));
|
||||
|
||||
when(KEYS.getLastResort(AuthHelper.VALID_PNI, Device.PRIMARY_ID))
|
||||
.thenReturn(CompletableFuture.completedFuture(Optional.of(lastResortKey)));
|
||||
}
|
||||
|
||||
final UUID accountIdentifier = hasE164 ? AuthHelper.VALID_UUID : AuthHelper.NUMBERLESS_UUID;
|
||||
final String password = hasE164 ? AuthHelper.VALID_PASSWORD : AuthHelper.NUMBERLESS_PASSWORD;
|
||||
|
||||
final CheckKeysRequest checkKeysRequest =
|
||||
new CheckKeysRequest(IdentityType.PNI, getKeyDigest(AuthHelper.VALID_PNI_IDENTITY, ecSignedPreKey, lastResortKey));
|
||||
|
||||
try (final Response response =
|
||||
resources.getJerseyTest()
|
||||
.target("/v2/keys/check")
|
||||
.request()
|
||||
.header("Authorization", AuthHelper.getAuthHeader(accountIdentifier, password))
|
||||
.post(Entity.entity(checkKeysRequest, MediaType.APPLICATION_JSON_TYPE))) {
|
||||
|
||||
assertEquals(hasE164 ? 200 : 400, response.getStatus());
|
||||
}
|
||||
}
|
||||
}
|
||||
+92
-5
@@ -7,7 +7,6 @@ package org.whispersystems.textsecuregcm.grpc;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
@@ -16,7 +15,10 @@ import static org.mockito.ArgumentMatchers.anyString;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.Mockito.doThrow;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.times;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.verifyNoInteractions;
|
||||
import static org.mockito.Mockito.when;
|
||||
import static org.whispersystems.textsecuregcm.grpc.GrpcTestUtils.assertRateLimitExceeded;
|
||||
import static org.whispersystems.textsecuregcm.grpc.GrpcTestUtils.assertStatusException;
|
||||
@@ -33,6 +35,7 @@ import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.function.Function;
|
||||
import java.util.stream.Stream;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
@@ -99,6 +102,7 @@ class KeysGrpcServiceTest extends SimpleBaseGrpcTest<KeysGrpcService, KeysGrpc.K
|
||||
@Mock
|
||||
private Device authenticatedDevice;
|
||||
|
||||
private Account authenticatedAccount;
|
||||
|
||||
@Override
|
||||
protected KeysGrpcService createServiceBeforeEachTest() {
|
||||
@@ -109,11 +113,12 @@ class KeysGrpcServiceTest extends SimpleBaseGrpcTest<KeysGrpcService, KeysGrpc.K
|
||||
|
||||
when(authenticatedDevice.getId()).thenReturn(AUTHENTICATED_DEVICE_ID);
|
||||
|
||||
final Account authenticatedAccount = mock(Account.class);
|
||||
authenticatedAccount = mock(Account.class);
|
||||
when(authenticatedAccount.getAccountIdentifier()).thenReturn(AUTHENTICATED_ACI);
|
||||
when(authenticatedAccount.getPhoneNumberIdentifier()).thenReturn(AUTHENTICATED_PNI);
|
||||
when(authenticatedAccount.getIdentifier(IdentityType.ACI)).thenReturn(AUTHENTICATED_ACI);
|
||||
when(authenticatedAccount.getIdentifier(IdentityType.PNI)).thenReturn(AUTHENTICATED_PNI);
|
||||
when(authenticatedAccount.getPhoneNumberIdentifierOptional()).thenReturn(Optional.of(AUTHENTICATED_PNI));
|
||||
when(authenticatedAccount.getPhoneNumberIdentifier()).thenThrow(new UnsupportedOperationException());
|
||||
when(authenticatedAccount.getIdentifier(any())).thenThrow(new UnsupportedOperationException());
|
||||
|
||||
when(authenticatedAccount.getIdentityKey(IdentityType.ACI)).thenReturn(new IdentityKey(ACI_IDENTITY_KEY_PAIR.getPublicKey()));
|
||||
when(authenticatedAccount.getIdentityKey(IdentityType.PNI)).thenReturn(new IdentityKey(PNI_IDENTITY_KEY_PAIR.getPublicKey()));
|
||||
when(authenticatedAccount.getDevice(AUTHENTICATED_DEVICE_ID)).thenReturn(Optional.of(authenticatedDevice));
|
||||
@@ -150,6 +155,88 @@ class KeysGrpcServiceTest extends SimpleBaseGrpcTest<KeysGrpcService, KeysGrpc.K
|
||||
authenticatedServiceStub().getPreKeyCount(GetPreKeyCountRequest.newBuilder().build()));
|
||||
}
|
||||
|
||||
@Test
|
||||
void getPreKeyCountWithoutPhoneNumber() {
|
||||
when(authenticatedAccount.getPhoneNumberIdentifierOptional()).thenReturn(Optional.empty());
|
||||
|
||||
when(keysManager.getEcCount(AUTHENTICATED_ACI, AUTHENTICATED_DEVICE_ID))
|
||||
.thenReturn(CompletableFuture.completedFuture(1));
|
||||
|
||||
when(keysManager.getPqCount(AUTHENTICATED_ACI, AUTHENTICATED_DEVICE_ID))
|
||||
.thenReturn(CompletableFuture.completedFuture(2));
|
||||
|
||||
assertEquals(GetPreKeyCountResponse.newBuilder()
|
||||
.setAciEcPreKeyCount(1)
|
||||
.setAciKemPreKeyCount(2)
|
||||
.setPniEcPreKeyCount(0)
|
||||
.setPniKemPreKeyCount(0)
|
||||
.build(),
|
||||
authenticatedServiceStub().getPreKeyCount(GetPreKeyCountRequest.newBuilder().build()));
|
||||
|
||||
verify(keysManager, times(1)).getEcCount(any(), anyByte());
|
||||
verify(keysManager, times(1)).getPqCount(any(), anyByte());
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@MethodSource
|
||||
void setPreKeysWithoutPhoneNumber(final Function<KeysGrpc.KeysBlockingStub, Object> setPreKeysFunction) {
|
||||
when(authenticatedAccount.getPhoneNumberIdentifierOptional()).thenReturn(Optional.empty());
|
||||
|
||||
assertStatusException(Status.INVALID_ARGUMENT, () -> setPreKeysFunction.apply(authenticatedServiceStub()));
|
||||
|
||||
verifyNoInteractions(keysManager);
|
||||
}
|
||||
|
||||
private static Stream<Arguments> setPreKeysWithoutPhoneNumber() {
|
||||
final org.signal.chat.common.IdentityType identityType = org.signal.chat.common.IdentityType.IDENTITY_TYPE_PNI;
|
||||
|
||||
final ECPreKey ecPreKey = new ECPreKey(1, ECKeyPair.generate().getPublicKey());
|
||||
final KEMSignedPreKey kemPreKey = KeysHelper.signedKEMPreKey(1, PNI_IDENTITY_KEY_PAIR);
|
||||
final ECSignedPreKey ecSignedPreKey = KeysHelper.signedECPreKey(17, PNI_IDENTITY_KEY_PAIR);
|
||||
final KEMSignedPreKey lastResortPreKey = KeysHelper.signedKEMPreKey(19, PNI_IDENTITY_KEY_PAIR);
|
||||
|
||||
return Stream.of(
|
||||
Arguments.argumentSet("setOneTimeEcPreKeys", (Function<KeysGrpc.KeysBlockingStub, Object>) stub ->
|
||||
stub.setOneTimeEcPreKeys(SetOneTimeEcPreKeysRequest.newBuilder()
|
||||
.setIdentityType(identityType)
|
||||
.addPreKeys(EcPreKey.newBuilder()
|
||||
.setKeyId(KeyIdUtil.toUnsignedInt(ecPreKey.keyId()))
|
||||
.setPublicKey(ByteString.copyFrom(ecPreKey.serializedPublicKey()))
|
||||
.build())
|
||||
.build())),
|
||||
|
||||
Arguments.argumentSet("setOneTimeKemSignedPreKeys", (Function<KeysGrpc.KeysBlockingStub, Object>) stub ->
|
||||
stub.setOneTimeKemSignedPreKeys(SetOneTimeKemSignedPreKeysRequest.newBuilder()
|
||||
.setIdentityType(identityType)
|
||||
.addPreKeys(KemSignedPreKey.newBuilder()
|
||||
.setKeyId(KeyIdUtil.toUnsignedInt(kemPreKey.keyId()))
|
||||
.setPublicKey(ByteString.copyFrom(kemPreKey.serializedPublicKey()))
|
||||
.setSignature(ByteString.copyFrom(kemPreKey.signature()))
|
||||
.build())
|
||||
.build())),
|
||||
|
||||
Arguments.argumentSet("setEcSignedPreKey", (Function<KeysGrpc.KeysBlockingStub, Object>) stub ->
|
||||
stub.setEcSignedPreKey(SetEcSignedPreKeyRequest.newBuilder()
|
||||
.setIdentityType(identityType)
|
||||
.setSignedPreKey(EcSignedPreKey.newBuilder()
|
||||
.setKeyId(KeyIdUtil.toUnsignedInt(ecSignedPreKey.keyId()))
|
||||
.setPublicKey(ByteString.copyFrom(ecSignedPreKey.serializedPublicKey()))
|
||||
.setSignature(ByteString.copyFrom(ecSignedPreKey.signature()))
|
||||
.build())
|
||||
.build())),
|
||||
|
||||
Arguments.argumentSet("setKemLastResortPreKey", (Function<KeysGrpc.KeysBlockingStub, Object>) stub ->
|
||||
stub.setKemLastResortPreKey(SetKemLastResortPreKeyRequest.newBuilder()
|
||||
.setIdentityType(identityType)
|
||||
.setSignedPreKey(KemSignedPreKey.newBuilder()
|
||||
.setKeyId(KeyIdUtil.toUnsignedInt(lastResortPreKey.keyId()))
|
||||
.setPublicKey(ByteString.copyFrom(lastResortPreKey.serializedPublicKey()))
|
||||
.setSignature(ByteString.copyFrom(lastResortPreKey.signature()))
|
||||
.build())
|
||||
.build()))
|
||||
);
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@EnumSource(value = org.signal.chat.common.IdentityType.class, names = {"IDENTITY_TYPE_ACI", "IDENTITY_TYPE_PNI"})
|
||||
void setOneTimeEcPreKeys(final org.signal.chat.common.IdentityType identityType) {
|
||||
|
||||
@@ -6,6 +6,7 @@
|
||||
package org.whispersystems.textsecuregcm.tests.util;
|
||||
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.Mockito.doThrow;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.reset;
|
||||
import static org.mockito.Mockito.when;
|
||||
@@ -25,6 +26,7 @@ import java.util.Base64;
|
||||
import java.util.Collection;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.NoSuchElementException;
|
||||
import java.util.Optional;
|
||||
import java.util.Random;
|
||||
import java.util.UUID;
|
||||
@@ -79,9 +81,6 @@ public class AuthHelper {
|
||||
public static final String VALID_PASSWORD_3_PRIMARY = "3primary";
|
||||
public static final String VALID_PASSWORD_3_LINKED = "3linked";
|
||||
|
||||
public static final UUID VALID_UUID_4 = UUID.randomUUID();
|
||||
public static final String VALID_PASSWORD_4 = "blah";
|
||||
|
||||
public static final UUID INVALID_UUID = UUID.randomUUID();
|
||||
public static final String INVALID_PASSWORD = "bar";
|
||||
|
||||
@@ -90,6 +89,9 @@ public class AuthHelper {
|
||||
public static final UUID UNDISCOVERABLE_PNI = UUID.randomUUID();
|
||||
public static final String UNDISCOVERABLE_PASSWORD = "IT'S A SECRET TO EVERYBODY.";
|
||||
|
||||
public static final UUID NUMBERLESS_UUID = UUID.randomUUID();
|
||||
public static final String NUMBERLESS_PASSWORD = "numberless";
|
||||
|
||||
public static final ECKeyPair VALID_IDENTITY_KEY_PAIR = ECKeyPair.generate();
|
||||
public static final IdentityKey VALID_IDENTITY = new IdentityKey(VALID_IDENTITY_KEY_PAIR.getPublicKey());
|
||||
|
||||
@@ -101,14 +103,14 @@ public class AuthHelper {
|
||||
public static Account VALID_ACCOUNT_TWO = mock(Account.class );
|
||||
public static Account UNDISCOVERABLE_ACCOUNT = mock(Account.class );
|
||||
public static Account VALID_ACCOUNT_3 = mock(Account.class );
|
||||
public static Account VALID_ACCOUNT_4 = mock(Account.class );
|
||||
public static Account NUMBERLESS_ACCOUNT = mock(Account.class );
|
||||
|
||||
public static Device VALID_DEVICE = mock(Device.class);
|
||||
public static Device VALID_DEVICE_TWO = mock(Device.class);
|
||||
public static Device UNDISCOVERABLE_DEVICE = mock(Device.class);
|
||||
public static Device VALID_DEVICE_3_PRIMARY = mock(Device.class);
|
||||
public static Device VALID_DEVICE_3_LINKED = mock(Device.class);
|
||||
public static Device VALID_DEVICE_4 = mock(Device.class);
|
||||
public static Device NUMBERLESS_DEVICE = mock(Device.class);
|
||||
|
||||
public static final byte VALID_DEVICE_3_LINKED_ID = Device.PRIMARY_ID + 1;
|
||||
|
||||
@@ -116,8 +118,8 @@ public class AuthHelper {
|
||||
private static SaltedTokenHash VALID_CREDENTIALS_TWO = mock(SaltedTokenHash.class);
|
||||
private static SaltedTokenHash VALID_CREDENTIALS_3_PRIMARY = mock(SaltedTokenHash.class);
|
||||
private static SaltedTokenHash VALID_CREDENTIALS_3_LINKED = mock(SaltedTokenHash.class);
|
||||
private static SaltedTokenHash VALID_CREDENTIALS_4 = mock(SaltedTokenHash.class);
|
||||
private static SaltedTokenHash UNDISCOVERABLE_CREDENTIALS = mock(SaltedTokenHash.class);
|
||||
private static SaltedTokenHash NUMBERLESS_CREDENTIALS = mock(SaltedTokenHash.class);
|
||||
|
||||
private static final Collection<TestAccount> EXTENSION_TEST_ACCOUNTS = new HashSet<>();
|
||||
|
||||
@@ -126,14 +128,12 @@ public class AuthHelper {
|
||||
when(VALID_CREDENTIALS_TWO.verify("baz")).thenReturn(true);
|
||||
when(VALID_CREDENTIALS_3_PRIMARY.verify(VALID_PASSWORD_3_PRIMARY)).thenReturn(true);
|
||||
when(VALID_CREDENTIALS_3_LINKED.verify(VALID_PASSWORD_3_LINKED)).thenReturn(true);
|
||||
when(VALID_CREDENTIALS_4.verify(VALID_PASSWORD_4)).thenReturn(true);
|
||||
when(UNDISCOVERABLE_CREDENTIALS.verify(UNDISCOVERABLE_PASSWORD)).thenReturn(true);
|
||||
|
||||
when(VALID_DEVICE.getAuthTokenHash()).thenReturn(VALID_CREDENTIALS);
|
||||
when(VALID_DEVICE_TWO.getAuthTokenHash()).thenReturn(VALID_CREDENTIALS_TWO);
|
||||
when(VALID_DEVICE_3_PRIMARY.getAuthTokenHash()).thenReturn(VALID_CREDENTIALS_3_PRIMARY);
|
||||
when(VALID_DEVICE_3_LINKED.getAuthTokenHash()).thenReturn(VALID_CREDENTIALS_3_LINKED);
|
||||
when(VALID_DEVICE_4.getAuthTokenHash()).thenReturn(VALID_CREDENTIALS_4);
|
||||
when(UNDISCOVERABLE_DEVICE.getAuthTokenHash()).thenReturn(UNDISCOVERABLE_CREDENTIALS);
|
||||
|
||||
when(VALID_DEVICE.isPrimary()).thenReturn(true);
|
||||
@@ -141,14 +141,12 @@ public class AuthHelper {
|
||||
when(UNDISCOVERABLE_DEVICE.isPrimary()).thenReturn(true);
|
||||
when(VALID_DEVICE_3_PRIMARY.isPrimary()).thenReturn(true);
|
||||
when(VALID_DEVICE_3_LINKED.isPrimary()).thenReturn(false);
|
||||
when(VALID_DEVICE_4.isPrimary()).thenReturn(true);
|
||||
|
||||
when(VALID_DEVICE.getId()).thenReturn(Device.PRIMARY_ID);
|
||||
when(VALID_DEVICE_TWO.getId()).thenReturn(Device.PRIMARY_ID);
|
||||
when(UNDISCOVERABLE_DEVICE.getId()).thenReturn(Device.PRIMARY_ID);
|
||||
when(VALID_DEVICE_3_PRIMARY.getId()).thenReturn(Device.PRIMARY_ID);
|
||||
when(VALID_DEVICE_3_LINKED.getId()).thenReturn(VALID_DEVICE_3_LINKED_ID);
|
||||
when(VALID_DEVICE_4.getId()).thenReturn(Device.PRIMARY_ID);
|
||||
|
||||
when(UNDISCOVERABLE_DEVICE.isPrimary()).thenReturn(true);
|
||||
|
||||
@@ -161,14 +159,11 @@ public class AuthHelper {
|
||||
when(VALID_ACCOUNT_3.getDevice(Device.PRIMARY_ID)).thenReturn(Optional.of(VALID_DEVICE_3_PRIMARY));
|
||||
when(VALID_ACCOUNT_3.getPrimaryDevice()).thenReturn(VALID_DEVICE_3_PRIMARY);
|
||||
when(VALID_ACCOUNT_3.getDevice((byte) 2)).thenReturn(Optional.of(VALID_DEVICE_3_LINKED));
|
||||
when(VALID_ACCOUNT_4.getDevice(Device.PRIMARY_ID)).thenReturn(Optional.of(VALID_DEVICE_4));
|
||||
when(VALID_ACCOUNT_4.getPrimaryDevice()).thenReturn(VALID_DEVICE_4);
|
||||
|
||||
when(VALID_ACCOUNT.getDevices()).thenReturn(List.of(VALID_DEVICE));
|
||||
when(VALID_ACCOUNT_TWO.getDevices()).thenReturn(List.of(VALID_DEVICE_TWO));
|
||||
when(UNDISCOVERABLE_ACCOUNT.getDevices()).thenReturn(List.of(UNDISCOVERABLE_DEVICE));
|
||||
when(VALID_ACCOUNT_3.getDevices()).thenReturn(List.of(VALID_DEVICE_3_PRIMARY, VALID_DEVICE_3_LINKED));
|
||||
when(VALID_ACCOUNT_4.getDevices()).thenReturn(List.of(VALID_DEVICE_4));
|
||||
|
||||
when(VALID_ACCOUNT.getNumber()).thenReturn(VALID_NUMBER);
|
||||
when(VALID_ACCOUNT.getNumberOptional()).thenReturn(Optional.of(VALID_NUMBER));
|
||||
@@ -215,8 +210,24 @@ public class AuthHelper {
|
||||
when(VALID_ACCOUNT.getIdentityKey(IdentityType.ACI)).thenReturn(VALID_IDENTITY);
|
||||
when(VALID_ACCOUNT.getIdentityKey(IdentityType.PNI)).thenReturn(VALID_PNI_IDENTITY);
|
||||
|
||||
when(VALID_ACCOUNT_4.getAccountIdentifier()).thenReturn(VALID_UUID_4);
|
||||
when(VALID_ACCOUNT_4.getIdentifier(IdentityType.ACI)).thenReturn(VALID_UUID_4);
|
||||
when(NUMBERLESS_CREDENTIALS.verify(NUMBERLESS_PASSWORD)).thenReturn(true);
|
||||
when(NUMBERLESS_DEVICE.getAuthTokenHash()).thenReturn(NUMBERLESS_CREDENTIALS);
|
||||
when(NUMBERLESS_DEVICE.isPrimary()).thenReturn(true);
|
||||
when(NUMBERLESS_DEVICE.getId()).thenReturn(Device.PRIMARY_ID);
|
||||
when(NUMBERLESS_ACCOUNT.getDevice(Device.PRIMARY_ID)).thenReturn(Optional.of(NUMBERLESS_DEVICE));
|
||||
when(NUMBERLESS_ACCOUNT.getPrimaryDevice()).thenReturn(NUMBERLESS_DEVICE);
|
||||
when(NUMBERLESS_ACCOUNT.getDevices()).thenReturn(List.of(NUMBERLESS_DEVICE));
|
||||
when(NUMBERLESS_ACCOUNT.getAccountIdentifier()).thenReturn(NUMBERLESS_UUID);
|
||||
when(NUMBERLESS_ACCOUNT.isIdentifiedBy(new AciServiceIdentifier(NUMBERLESS_UUID))).thenReturn(true);
|
||||
when(NUMBERLESS_ACCOUNT.isDiscoverableByPhoneNumber()).thenReturn(false);
|
||||
when(NUMBERLESS_ACCOUNT.getIdentityKey(IdentityType.ACI)).thenReturn(VALID_IDENTITY);
|
||||
when(NUMBERLESS_ACCOUNT.getNumberOptional()).thenReturn(Optional.empty());
|
||||
when(NUMBERLESS_ACCOUNT.getPhoneNumberIdentifierOptional()).thenReturn(Optional.empty());
|
||||
doThrow(new NoSuchElementException()).when(NUMBERLESS_ACCOUNT).getNumber();
|
||||
doThrow(new NoSuchElementException()).when(NUMBERLESS_ACCOUNT).getPhoneNumberIdentifier();
|
||||
doThrow(new NoSuchElementException()).when(NUMBERLESS_ACCOUNT).getIdentityKey(IdentityType.PNI);
|
||||
doThrow(new NoSuchElementException()).when(NUMBERLESS_ACCOUNT).getIdentifier(IdentityType.PNI);
|
||||
when(NUMBERLESS_ACCOUNT.getIdentifier(IdentityType.ACI)).thenReturn(NUMBERLESS_UUID);
|
||||
|
||||
reset(ACCOUNTS_MANAGER);
|
||||
|
||||
@@ -235,7 +246,7 @@ public class AuthHelper {
|
||||
when(ACCOUNTS_MANAGER.getByAccountIdentifier(VALID_UUID_3)).thenReturn(Optional.of(VALID_ACCOUNT_3));
|
||||
when(ACCOUNTS_MANAGER.getByPhoneNumberIdentifier(VALID_PNI_3)).thenReturn(Optional.of(VALID_ACCOUNT_3));
|
||||
|
||||
when(ACCOUNTS_MANAGER.getByAccountIdentifier(VALID_UUID_4)).thenReturn(Optional.of(VALID_ACCOUNT_4));
|
||||
when(ACCOUNTS_MANAGER.getByAccountIdentifier(NUMBERLESS_UUID)).thenReturn(Optional.of(NUMBERLESS_ACCOUNT));
|
||||
|
||||
AccountsHelper.setupMockUpdateForAuthHelper(ACCOUNTS_MANAGER);
|
||||
|
||||
|
||||
Reference in new issue
Block a user