@@ -20,7 +20,7 @@ class Uid2Encryption {
2020 public static final int GCM_AUTHTAG_LENGTH = 16 ;
2121 public static final int GCM_IV_LENGTH = 12 ;
2222
23- static DecryptionResponse decrypt (String token , KeyContainer keys , Instant now , IdentityScope identityScope , String domainName , ClientType clientType ) throws Exception {
23+ static DecryptionResponse decrypt (String token , KeyContainer keys , Instant now , IdentityScope identityScope , String domainOrAppName , ClientType clientType ) throws Exception {
2424
2525 if (token .length () < 4 )
2626 {
@@ -33,18 +33,18 @@ static DecryptionResponse decrypt(String token, KeyContainer keys, Instant now,
3333
3434 if (data [0 ] == 2 )
3535 {
36- return decryptV2 (Base64 .getDecoder ().decode (token ), keys , now , domainName , clientType );
36+ return decryptV2 (Base64 .getDecoder ().decode (token ), keys , now , domainOrAppName , clientType );
3737 }
3838 //java byte is signed so we wanna convert to unsigned before checking the enum
3939 int unsignedByte = ((int ) data [1 ]) & 0xff ;
4040 if (unsignedByte == AdvertisingTokenVersion .V3 .value ())
4141 {
42- return decryptV3 (Base64 .getDecoder ().decode (token ), keys , now , identityScope , domainName , clientType , 3 );
42+ return decryptV3 (Base64 .getDecoder ().decode (token ), keys , now , identityScope , domainOrAppName , clientType , 3 );
4343 }
4444 else if (unsignedByte == AdvertisingTokenVersion .V4 .value ())
4545 {
4646 // Accept either base64 or base64url encoding.
47- return decryptV3 (Base64 .getDecoder ().decode (base64UrlToBase64 (token )), keys , now , identityScope , domainName , clientType , 4 );
47+ return decryptV3 (Base64 .getDecoder ().decode (base64UrlToBase64 (token )), keys , now , identityScope , domainOrAppName , clientType , 4 );
4848 }
4949
5050 return DecryptionResponse .makeError (DecryptionStatus .VERSION_NOT_SUPPORTED );
@@ -56,7 +56,7 @@ static String base64UrlToBase64(String value) {
5656 .replace ('_' , '/' );
5757 }
5858
59- static DecryptionResponse decryptV2 (byte [] encryptedId , KeyContainer keys , Instant now , String domainName , ClientType clientType ) throws Exception {
59+ static DecryptionResponse decryptV2 (byte [] encryptedId , KeyContainer keys , Instant now , String domainOrAppName , ClientType clientType ) throws Exception {
6060 try {
6161 ByteBuffer rootReader = ByteBuffer .wrap (encryptedId );
6262 int version = (int ) rootReader .get ();
@@ -108,6 +108,9 @@ static DecryptionResponse decryptV2(byte[] encryptedId, KeyContainer keys, Insta
108108 if (now .isAfter (expiry )) {
109109 return DecryptionResponse .makeError (DecryptionStatus .EXPIRED_TOKEN , established , siteId , siteKey .getSiteId (), null , advertisingTokenVersion , privacyBits .isClientSideGenerated (), expiry );
110110 }
111+ if (!isDomainOrAppNameAllowedForSite (clientType , privacyBits .isClientSideGenerated (), siteId , domainOrAppName , keys )) {
112+ return DecryptionResponse .makeError (DecryptionStatus .DOMAIN_OR_APP_NAME_CHECK_FAILED , established , siteId , siteKey .getSiteId (), null , advertisingTokenVersion , privacyBits .isClientSideGenerated (), expiry );
113+ }
111114
112115 if (!doesTokenHaveValidLifetime (clientType , keys , now , expiry , now )) {
113116 return DecryptionResponse .makeError (DecryptionStatus .INVALID_TOKEN_LIFETIME , established , siteId , siteKey .getSiteId (), null , advertisingTokenVersion , privacyBits .isClientSideGenerated (), expiry );
@@ -119,7 +122,7 @@ static DecryptionResponse decryptV2(byte[] encryptedId, KeyContainer keys, Insta
119122 }
120123 }
121124
122- static DecryptionResponse decryptV3 (byte [] encryptedId , KeyContainer keys , Instant now , IdentityScope identityScope , String domainName , ClientType clientType , int advertisingTokenVersion ) {
125+ static DecryptionResponse decryptV3 (byte [] encryptedId , KeyContainer keys , Instant now , IdentityScope identityScope , String domainOrAppName , ClientType clientType , int advertisingTokenVersion ) {
123126 try {
124127 final IdentityType identityType = getIdentityType (encryptedId );
125128 final ByteBuffer rootReader = ByteBuffer .wrap (encryptedId );
@@ -174,6 +177,9 @@ static DecryptionResponse decryptV3(byte[] encryptedId, KeyContainer keys, Insta
174177 if (now .isAfter (expiry )) {
175178 return DecryptionResponse .makeError (DecryptionStatus .EXPIRED_TOKEN , established , siteId , siteKey .getSiteId (), identityType , advertisingTokenVersion , privacyBits .isClientSideGenerated (), expiry );
176179 }
180+ if (!isDomainOrAppNameAllowedForSite (clientType , privacyBits .isClientSideGenerated (), siteId , domainOrAppName , keys )) {
181+ return DecryptionResponse .makeError (DecryptionStatus .DOMAIN_OR_APP_NAME_CHECK_FAILED , established , siteId , siteKey .getSiteId (), identityType , advertisingTokenVersion , privacyBits .isClientSideGenerated (), expiry );
182+ }
177183
178184 if (!doesTokenHaveValidLifetime (clientType , keys , generated , expiry , now )) {
179185 return DecryptionResponse .makeError (DecryptionStatus .INVALID_TOKEN_LIFETIME , generated , siteId , siteKey .getSiteId (), identityType , advertisingTokenVersion , privacyBits .isClientSideGenerated (), expiry );
@@ -220,7 +226,7 @@ else if (!keys.isValid(now))
220226 }
221227
222228
223- static EncryptionDataResponse encryptData (EncryptionDataRequest request , KeyContainer keys , IdentityScope identityScope , String domainName , ClientType clientType ) {
229+ static EncryptionDataResponse encryptData (EncryptionDataRequest request , KeyContainer keys , IdentityScope identityScope , String domainOrAppName , ClientType clientType ) {
224230 if (request .getData () == null ) {
225231 throw new IllegalArgumentException ("data to encrypt must not be null" );
226232 }
@@ -241,7 +247,7 @@ static EncryptionDataResponse encryptData(EncryptionDataRequest request, KeyCont
241247 siteKeySiteId = siteId ;
242248 } else {
243249 try {
244- DecryptionResponse decryptedToken = decrypt (request .getAdvertisingToken (), keys , now , identityScope , domainName , clientType );
250+ DecryptionResponse decryptedToken = decrypt (request .getAdvertisingToken (), keys , now , identityScope , domainOrAppName , clientType );
245251 if (!decryptedToken .isSuccess ()) {
246252 return EncryptionDataResponse .makeError (EncryptionStatus .TOKEN_DECRYPT_FAILURE );
247253 }
@@ -408,6 +414,16 @@ public CryptoException(Throwable inner) {
408414 }
409415 }
410416
417+ private static boolean isDomainOrAppNameAllowedForSite (ClientType clientType , boolean isClientSideGenerated , Integer siteId , String domainOrAppName , KeyContainer keys ) {
418+ if (!isClientSideGenerated ) {
419+ return true ;
420+ } else if (!clientType .equals (ClientType .BIDSTREAM ) && !clientType .equals (ClientType .LEGACY )) {
421+ return true ;
422+ } else {
423+ return keys .isDomainOrAppNameAllowedForSite (siteId , domainOrAppName );
424+ }
425+ }
426+
411427 private static boolean doesTokenHaveValidLifetime (ClientType clientType , KeyContainer keys , Instant generatedOrNow , Instant expiry , Instant now ) {
412428 long maxLifetimeSeconds ;
413429 switch (clientType ) {
0 commit comments