@@ -15,7 +15,7 @@ internal static class UID2Encryption
15
15
public const int GCM_IV_LENGTH = 12 ;
16
16
private static char [ ] BASE64_URL_SPECIAL_CHARS = { '-' , '_' } ;
17
17
18
- internal static DecryptionResponse Decrypt ( string token , KeyContainer keys , DateTime now , string domainName , IdentityScope identityScope )
18
+ internal static DecryptionResponse Decrypt ( string token , KeyContainer keys , DateTime now , string domainName , IdentityScope identityScope , bool enableDomainNameCheck )
19
19
{
20
20
if ( token . Length < 4 )
21
21
{
@@ -28,24 +28,24 @@ internal static DecryptionResponse Decrypt(string token, KeyContainer keys, Date
28
28
29
29
if ( data [ 0 ] == 2 )
30
30
{
31
- return DecryptV2 ( Convert . FromBase64String ( token ) , keys , now , domainName ) ;
31
+ return DecryptV2 ( Convert . FromBase64String ( token ) , keys , now , domainName , enableDomainNameCheck ) ;
32
32
}
33
33
34
34
if ( data [ 1 ] == ( int ) AdvertisingTokenVersion . V3 )
35
35
{
36
- return DecryptV3 ( Convert . FromBase64String ( token ) , keys , now , identityScope , 3 , domainName ) ;
36
+ return DecryptV3 ( Convert . FromBase64String ( token ) , keys , now , identityScope , 3 , domainName , enableDomainNameCheck ) ;
37
37
}
38
38
39
39
if ( data [ 1 ] == ( int ) AdvertisingTokenVersion . V4 )
40
40
{
41
41
//same as V3 but use Base64URL encoding
42
- return DecryptV3 ( UID2Base64UrlCoder . Decode ( token ) , keys , now , identityScope , 4 , domainName ) ;
42
+ return DecryptV3 ( UID2Base64UrlCoder . Decode ( token ) , keys , now , identityScope , 4 , domainName , enableDomainNameCheck ) ;
43
43
}
44
44
45
45
return DecryptionResponse . MakeError ( DecryptionStatus . VersionNotSupported ) ;
46
46
}
47
47
48
- private static DecryptionResponse DecryptV2 ( byte [ ] encryptedId , KeyContainer keys , DateTime now , string domainName )
48
+ private static DecryptionResponse DecryptV2 ( byte [ ] encryptedId , KeyContainer keys , DateTime now , string domainName , bool enableDomainNameCheck )
49
49
{
50
50
var reader = new BigEndianByteReader ( new MemoryStream ( encryptedId ) ) ;
51
51
@@ -101,15 +101,15 @@ private static DecryptionResponse DecryptV2(byte[] encryptedId, KeyContainer key
101
101
return new DecryptionResponse ( DecryptionStatus . UserOptedOut , null , established , siteId , siteKey . SiteId , null , 2 , privacyBits . IsClientSideGenerated ) ;
102
102
}
103
103
104
- if ( ! IsDomainNameAllowedForSite ( privacyBits , siteId , domainName , keys ) )
104
+ if ( enableDomainNameCheck && ! IsDomainNameAllowedForSite ( privacyBits , siteId , domainName , keys ) )
105
105
{
106
106
return new DecryptionResponse ( DecryptionStatus . DomainNameCheckFailed , null , established , siteId , siteKey . SiteId , null , 2 , privacyBits . IsClientSideGenerated ) ;
107
107
}
108
108
109
109
return new DecryptionResponse ( DecryptionStatus . Success , idString , established , siteId , siteKey . SiteId , null , 2 , privacyBits . IsClientSideGenerated ) ;
110
110
}
111
111
112
- private static DecryptionResponse DecryptV3 ( byte [ ] encryptedId , KeyContainer keys , DateTime now , IdentityScope identityScope , int advertisingTokenVersion , string domainName )
112
+ private static DecryptionResponse DecryptV3 ( byte [ ] encryptedId , KeyContainer keys , DateTime now , IdentityScope identityScope , int advertisingTokenVersion , string domainName , bool enableDomainNameCheck )
113
113
{
114
114
IdentityType identityType = GetIdentityType ( encryptedId ) ;
115
115
@@ -178,7 +178,7 @@ private static DecryptionResponse DecryptV3(byte[] encryptedId, KeyContainer key
178
178
return new DecryptionResponse ( DecryptionStatus . UserOptedOut , null , established , siteId , siteKey . SiteId , identityType , advertisingTokenVersion , privacyBits . IsClientSideGenerated ) ;
179
179
}
180
180
181
- if ( ! IsDomainNameAllowedForSite ( privacyBits , siteId , domainName , keys ) )
181
+ if ( enableDomainNameCheck && ! IsDomainNameAllowedForSite ( privacyBits , siteId , domainName , keys ) )
182
182
{
183
183
return new DecryptionResponse ( DecryptionStatus . DomainNameCheckFailed , null , established , siteId , siteKey . SiteId , identityType , advertisingTokenVersion ,
184
184
privacyBits . IsClientSideGenerated ) ;
@@ -270,9 +270,8 @@ internal static EncryptionDataResponse EncryptData(EncryptionDataRequest request
270
270
{
271
271
try
272
272
{
273
- // Decryption will fail if the token is a CSTG-derived token.
274
- // In that case the caller would have to provide siteId as part of the EncryptionDataRequest.
275
- DecryptionResponse decryptedToken = Decrypt ( request . AdvertisingToken , keys , now , domainName : null , identityScope ) ;
273
+ // if the enableDomainNameCheck param is enabled , the caller would have to provide siteId as part of the EncryptionDataRequest.
274
+ DecryptionResponse decryptedToken = Decrypt ( request . AdvertisingToken , keys , now , domainName : null , identityScope , false ) ;
276
275
if ( ! decryptedToken . Success )
277
276
{
278
277
return EncryptionDataResponse . MakeError ( EncryptionStatus . TokenDecryptFailure ) ;
0 commit comments