Skip to content

Commit a83a0aa

Browse files
committed
Changed null advertising ID to throw runtime exception instead
1 parent bad6b68 commit a83a0aa

File tree

5 files changed

+76
-130
lines changed

5 files changed

+76
-130
lines changed

src/main/java/com/uid2/operator/service/TokenUtils.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,16 +58,16 @@ public static byte[] getAdvertisingIdV3FromIdentityHash(IdentityScope scope, Ide
5858
return getAdvertisingIdV3(scope, type, getFirstLevelHashFromIdentityHash(identityString, firstLevelSalt), rotatingSalt);
5959
}
6060

61-
public static byte[] getAdvertisingIdV4(IdentityScope scope, IdentityType type, IdentityEnvironment environment, byte[] firstLevelHash, SaltEntry.KeyMaterial encryptingKey) throws Exception {
61+
public static byte[] getAdvertisingIdV4(IdentityScope scope, IdentityType type, IdentityEnvironment environment, byte[] firstLevelHash, SaltEntry.KeyMaterial encryptingKey) {
6262
byte metadata = encodeV4Metadata(scope, type, environment);
6363
return V4TokenUtils.buildAdvertisingIdV4(metadata, firstLevelHash, encryptingKey.id(), encryptingKey.key(), encryptingKey.salt());
6464
}
6565

66-
public static byte[] getAdvertisingIdV4FromIdentity(IdentityScope scope, IdentityType type, IdentityEnvironment environment, String identityString, String firstLevelSalt, SaltEntry.KeyMaterial encryptingKey) throws Exception {
66+
public static byte[] getAdvertisingIdV4FromIdentity(IdentityScope scope, IdentityType type, IdentityEnvironment environment, String identityString, String firstLevelSalt, SaltEntry.KeyMaterial encryptingKey) {
6767
return getAdvertisingIdV4(scope, type, environment, getFirstLevelHashFromIdentity(identityString, firstLevelSalt), encryptingKey);
6868
}
6969

70-
public static byte[] getAdvertisingIdV4FromIdentityHash(IdentityScope scope, IdentityType type, IdentityEnvironment environment, String identityString, String firstLevelSalt, SaltEntry.KeyMaterial encryptingKey) throws Exception {
70+
public static byte[] getAdvertisingIdV4FromIdentityHash(IdentityScope scope, IdentityType type, IdentityEnvironment environment, String identityString, String firstLevelSalt, SaltEntry.KeyMaterial encryptingKey) {
7171
return getAdvertisingIdV4(scope, type, environment, getFirstLevelHashFromIdentityHash(identityString, firstLevelSalt), encryptingKey);
7272
}
7373

src/main/java/com/uid2/operator/service/UIDOperatorService.java

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -252,13 +252,8 @@ private byte[] getAdvertisingId(UserIdentity firstLevelHashIdentity, String salt
252252
incrementAdvertisingIdVersionCounter(IdentityVersion.V2);
253253
}
254254
} else {
255-
try {
256-
advertisingId = TokenUtils.getAdvertisingIdV4(firstLevelHashIdentity.identityScope, firstLevelHashIdentity.identityType, env, firstLevelHashIdentity.id, key);
257-
incrementAdvertisingIdVersionCounter(IdentityVersion.V4);
258-
} catch (Exception e) {
259-
LOGGER.error("Exception when generating V4 advertising ID", e);
260-
advertisingId = null;
261-
}
255+
advertisingId = TokenUtils.getAdvertisingIdV4(firstLevelHashIdentity.identityScope, firstLevelHashIdentity.identityType, env, firstLevelHashIdentity.id, key);
256+
incrementAdvertisingIdVersionCounter(IdentityVersion.V4);
262257
}
263258

264259
return advertisingId;

src/main/java/com/uid2/operator/service/V4TokenUtils.java

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -29,21 +29,25 @@ private static byte[] getKeyIdBytes(int keyId) {
2929
};
3030
}
3131

32-
public static byte[] buildAdvertisingIdV4(byte metadata, byte[] firstLevelHash, int keyId, String key, String salt) throws Exception {
33-
byte[] firstLevelHashLast16Bytes = Arrays.copyOfRange(firstLevelHash, firstLevelHash.length - 16, firstLevelHash.length);
34-
byte[] iv = V4TokenUtils.generateIV(salt, firstLevelHashLast16Bytes, metadata, keyId);
35-
byte[] encryptedFirstLevelHash = V4TokenUtils.encryptHash(key, firstLevelHashLast16Bytes, iv);
32+
public static byte[] buildAdvertisingIdV4(byte metadata, byte[] firstLevelHash, int keyId, String key, String salt) {
33+
try {
34+
byte[] firstLevelHashLast16Bytes = Arrays.copyOfRange(firstLevelHash, firstLevelHash.length - 16, firstLevelHash.length);
35+
byte[] iv = V4TokenUtils.generateIV(salt, firstLevelHashLast16Bytes, metadata, keyId);
36+
byte[] encryptedFirstLevelHash = V4TokenUtils.encryptHash(key, firstLevelHashLast16Bytes, iv);
3637

37-
Buffer buffer = Buffer.buffer();
38-
buffer.appendByte(metadata);
39-
buffer.appendBytes(getKeyIdBytes(keyId));
40-
buffer.appendBytes(iv);
41-
buffer.appendBytes(encryptedFirstLevelHash);
38+
Buffer buffer = Buffer.buffer();
39+
buffer.appendByte(metadata);
40+
buffer.appendBytes(getKeyIdBytes(keyId));
41+
buffer.appendBytes(iv);
42+
buffer.appendBytes(encryptedFirstLevelHash);
4243

43-
byte checksum = generateChecksum(buffer.getBytes());
44-
buffer.appendByte(checksum);
44+
byte checksum = generateChecksum(buffer.getBytes());
45+
buffer.appendByte(checksum);
4546

46-
return buffer.getBytes();
47+
return buffer.getBytes();
48+
} catch (Exception e) {
49+
throw new RuntimeException(e);
50+
}
4751
}
4852

4953
public static byte[] generateIV(String salt, byte[] firstLevelHashLast16Bytes, byte metadata, int keyId) throws Exception {

src/test/java/com/uid2/operator/EncryptionTest.java

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@
55
import com.uid2.shared.model.EncryptedPayload;
66
import com.uid2.shared.encryption.AesGcm;
77
import com.uid2.shared.model.KeysetKey;
8-
import junit.framework.TestCase;
9-
import org.junit.Assert;
8+
import org.junit.jupiter.api.Test;
109

1110
import javax.crypto.SecretKey;
1211
import javax.crypto.spec.SecretKeySpec;
@@ -16,23 +15,26 @@
1615
import java.time.Instant;
1716
import java.util.concurrent.ThreadLocalRandom;
1817

19-
public class EncryptionTest extends TestCase {
18+
import static org.junit.jupiter.api.Assertions.assertEquals;
19+
import static org.junit.jupiter.api.Assertions.assertNotSame;
2020

21+
class EncryptionTest {
2122
private int count = 0;
2223

23-
public void testEncryption() throws Exception {
24-
24+
@Test
25+
void testEncryption() {
2526
final KeysetKey key = new KeysetKey(1, Random.getRandomKeyBytes(), Instant.now(), Instant.now(), Instant.now(), 10);
2627
final String testString = "[email protected]";
2728

2829
final EncryptedPayload payload = AesCbc.encrypt(testString, key);
2930
final byte[] decrypted = AesCbc.decrypt(payload.getPayload(), key);
3031

3132
final String decryptedString = new String(decrypted, StandardCharsets.UTF_8);
32-
Assert.assertEquals(testString, decryptedString);
33+
assertEquals(testString, decryptedString);
3334
}
3435

35-
public void testBenchmark() throws Exception {
36+
@Test
37+
void testBenchmark() {
3638
if (System.getenv("SLOW_DEV_URANDOM") != null) {
3739
System.err.println("ignore this test since environment variable SLOW_DEV_URANDOM is set");
3840
return;
@@ -73,10 +75,14 @@ public void testBenchmark() throws Exception {
7375
System.out.println("Decryption Overhead per Entry (ms) = " + overheadPerEntry / (1000000 * 1.0));
7476

7577
// System.out.println("Entries = "+runs+", Base Operation Execution Time (ms) = " + baseTime/(1000000*1.0) + ", With Decryption(ms) = " + decryptTime/(1000000*1.0) + ", Overhead/Entry (ms) = " + ((decryptTime-baseTime)/(runs*1.0)/(1000000*1.0)));
78+
}
7679

80+
private void doSomething(EncryptedPayload ep) {
81+
count++;
7782
}
7883

79-
public void testSecureRandom() throws NoSuchAlgorithmException {
84+
@Test
85+
void testSecureRandom() {
8086
if (System.getenv("SLOW_DEV_URANDOM") != null) {
8187
System.err.println("ignore this test since environment variable SLOW_DEV_URANDOM is set");
8288
return;
@@ -109,17 +115,15 @@ public void testSecureRandom() throws NoSuchAlgorithmException {
109115
}
110116
}
111117

112-
public void testNewInstancesReturned() throws NoSuchAlgorithmException {
118+
@Test
119+
void testNewInstancesReturned() throws NoSuchAlgorithmException {
113120
SecureRandom r1 = SecureRandom.getInstance("SHA1PRNG");
114121
SecureRandom r2 = SecureRandom.getInstance("SHA1PRNG");
115122
assertNotSame(r1, r2);
116123
}
117124

118-
public void doSomething(EncryptedPayload loag) {
119-
count++;
120-
}
121-
122-
public void testGCMEncryptionDecryption() {
125+
@Test
126+
void testGCMEncryptionDecryption() {
123127
final KeysetKey key = new KeysetKey(1, Random.getRandomKeyBytes(), Instant.now(), Instant.now(), Instant.now(), 10);
124128
String plaintxt = "hello world";
125129
EncryptedPayload payload = AesGcm.encrypt(plaintxt.getBytes(StandardCharsets.UTF_8), key);

0 commit comments

Comments
 (0)