Skip to content

Commit 2bcf6c5

Browse files
committed
Add tests for RsaOaepKeyWrap
Although RsaOeapKeyWrap identifier doesn't exist, we already released and decided to support it. That is the reason why we should keep RsaOaepKeyWrap tests around.
1 parent 5610c01 commit 2bcf6c5

File tree

4 files changed

+104
-0
lines changed

4 files changed

+104
-0
lines changed

test/Microsoft.IdentityModel.JsonWebTokens.Tests/JsonWebTokenHandlerTests.cs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -823,6 +823,14 @@ public static TheoryData<CreateTokenTheoryData> RoundTripJWEKeyWrappingTheoryDat
823823
EncryptingCredentials = new EncryptingCredentials(KeyingMaterial.RsaSecurityKey_2048, SecurityAlgorithms.RsaOaepMgf1pKeyWrap, SecurityAlgorithms.Aes128CbcHmacSha256)
824824
},
825825
new CreateTokenTheoryData()
826+
{
827+
TestId = "WrongRsaOaepKeyWrapIdentifier-Aes128CbcHmacSha256",
828+
ValidationParameters = Default.TokenValidationParameters(KeyingMaterial.RsaSecurityKey_2048, Default.SymmetricSigningKey256),
829+
Payload = Default.PayloadString,
830+
SigningCredentials = Default.SymmetricSigningCredentials,
831+
EncryptingCredentials = new EncryptingCredentials(KeyingMaterial.RsaSecurityKey_2048, SecurityAlgorithms.RsaOaepKeyWrap, SecurityAlgorithms.Aes128CbcHmacSha256)
832+
},
833+
new CreateTokenTheoryData()
826834
{
827835
TestId = "RsaOaepKeyWrap-Aes192CbcHmacSha384",
828836
ValidationParameters = Default.TokenValidationParameters(KeyingMaterial.RsaSecurityKey_2048, Default.SymmetricSigningKey256),
@@ -831,6 +839,14 @@ public static TheoryData<CreateTokenTheoryData> RoundTripJWEKeyWrappingTheoryDat
831839
EncryptingCredentials = new EncryptingCredentials(KeyingMaterial.RsaSecurityKey_2048, SecurityAlgorithms.RsaOaepMgf1pKeyWrap, SecurityAlgorithms.Aes192CbcHmacSha384)
832840
},
833841
new CreateTokenTheoryData()
842+
{
843+
TestId = "WrongRsaOaepKeyWrapIdentifier-Aes192CbcHmacSha384",
844+
ValidationParameters = Default.TokenValidationParameters(KeyingMaterial.RsaSecurityKey_2048, Default.SymmetricSigningKey256),
845+
Payload = Default.PayloadString,
846+
SigningCredentials = Default.SymmetricSigningCredentials,
847+
EncryptingCredentials = new EncryptingCredentials(KeyingMaterial.RsaSecurityKey_2048, SecurityAlgorithms.RsaOaepKeyWrap, SecurityAlgorithms.Aes192CbcHmacSha384)
848+
},
849+
new CreateTokenTheoryData()
834850
{
835851
TestId = "RsaOaepKeyWrap-Aes256CbcHmacSha512",
836852
ValidationParameters = Default.TokenValidationParameters(KeyingMaterial.RsaSecurityKey_2048, Default.SymmetricSigningKey256),
@@ -839,6 +855,14 @@ public static TheoryData<CreateTokenTheoryData> RoundTripJWEKeyWrappingTheoryDat
839855
EncryptingCredentials = new EncryptingCredentials(KeyingMaterial.RsaSecurityKey_2048, SecurityAlgorithms.RsaOaepMgf1pKeyWrap, SecurityAlgorithms.Aes256CbcHmacSha512)
840856
},
841857
new CreateTokenTheoryData()
858+
{
859+
TestId = "WrongRsaOaepKeyWrapIdentifier-Aes256CbcHmacSha512",
860+
ValidationParameters = Default.TokenValidationParameters(KeyingMaterial.RsaSecurityKey_2048, Default.SymmetricSigningKey256),
861+
Payload = Default.PayloadString,
862+
SigningCredentials = Default.SymmetricSigningCredentials,
863+
EncryptingCredentials = new EncryptingCredentials(KeyingMaterial.RsaSecurityKey_2048, SecurityAlgorithms.RsaOaepKeyWrap, SecurityAlgorithms.Aes256CbcHmacSha512)
864+
},
865+
new CreateTokenTheoryData()
842866
{
843867
TestId = "SymmetricSecurityKey2_128-Aes128KW-Aes128CbcHmacSha256",
844868
ValidationParameters = Default.TokenValidationParameters(KeyingMaterial.SymmetricSecurityKey2_128, Default.SymmetricSigningKey256),
@@ -861,6 +885,14 @@ public static TheoryData<CreateTokenTheoryData> RoundTripJWEKeyWrappingTheoryDat
861885
Payload = Default.PayloadString,
862886
SigningCredentials = Default.SymmetricSigningCredentials,
863887
EncryptingCredentials = new EncryptingCredentials(KeyingMaterial.RsaSecurityKey_2048, SecurityAlgorithms.RsaOaepMgf1pKeyWrap, SecurityAlgorithms.Aes192CbcHmacSha384)
888+
},
889+
new CreateTokenTheoryData()
890+
{
891+
TestId = "WrongRsaOaepKeyWrapIdentifier-Aes192CbcHmacSha384",
892+
ValidationParameters = Default.TokenValidationParameters(KeyingMaterial.RsaSecurityKey_2048, Default.SymmetricSigningKey256),
893+
Payload = Default.PayloadString,
894+
SigningCredentials = Default.SymmetricSigningCredentials,
895+
EncryptingCredentials = new EncryptingCredentials(KeyingMaterial.RsaSecurityKey_2048, SecurityAlgorithms.RsaOaepKeyWrap, SecurityAlgorithms.Aes192CbcHmacSha384)
864896
}
865897
};
866898
}

test/Microsoft.IdentityModel.Tokens.Saml.Tests/Saml2SecurityTokenHandlerTests.cs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1713,6 +1713,9 @@ public static TheoryData<Saml2TheoryData> RoundTripEncryptedTokenTheoryData
17131713
var encryptingCredentials_KeyWrap_128_RSAOAEP = new X509EncryptingCredentials(cert, SecurityAlgorithms.RsaOaepMgf1pKeyWrap, SecurityAlgorithms.Aes128Gcm);
17141714
var encryptingCredentials_KeyWrap_192_RSAOAEP = new X509EncryptingCredentials(cert, SecurityAlgorithms.RsaOaepMgf1pKeyWrap, SecurityAlgorithms.Aes192Gcm);
17151715
var encryptingCredentials_KeyWrap_256_RSAOAEP = new X509EncryptingCredentials(cert, SecurityAlgorithms.RsaOaepMgf1pKeyWrap, SecurityAlgorithms.Aes256Gcm);
1716+
var encryptingCredentials_KeyWrap_128_Wrong_RSAOAEP_Identifier = new X509EncryptingCredentials(cert, SecurityAlgorithms.RsaOaepKeyWrap, SecurityAlgorithms.Aes128Gcm);
1717+
var encryptingCredentials_KeyWrap_192_Wrong_RSAOAEP_Identifier = new X509EncryptingCredentials(cert, SecurityAlgorithms.RsaOaepKeyWrap, SecurityAlgorithms.Aes192Gcm);
1718+
var encryptingCredentials_KeyWrap_256_Wrong_RSAOAEP_Identifier = new X509EncryptingCredentials(cert, SecurityAlgorithms.RsaOaepKeyWrap, SecurityAlgorithms.Aes256Gcm);
17161719

17171720
//SET HELPER CRYPTO PROVIDER FACTORY - remove when AES-GCM is released and supported
17181721
encryptingCredentials128_PreShared.CryptoProviderFactory = new AesGcmProviderFactory();
@@ -1721,6 +1724,9 @@ public static TheoryData<Saml2TheoryData> RoundTripEncryptedTokenTheoryData
17211724
encryptingCredentials_KeyWrap_128_RSAOAEP.CryptoProviderFactory = new AesGcmProviderFactory();
17221725
encryptingCredentials_KeyWrap_192_RSAOAEP.CryptoProviderFactory = new AesGcmProviderFactory();
17231726
encryptingCredentials_KeyWrap_256_RSAOAEP.CryptoProviderFactory = new AesGcmProviderFactory();
1727+
encryptingCredentials_KeyWrap_128_Wrong_RSAOAEP_Identifier.CryptoProviderFactory = new AesGcmProviderFactory();
1728+
encryptingCredentials_KeyWrap_192_Wrong_RSAOAEP_Identifier.CryptoProviderFactory = new AesGcmProviderFactory();
1729+
encryptingCredentials_KeyWrap_256_Wrong_RSAOAEP_Identifier.CryptoProviderFactory = new AesGcmProviderFactory();
17241730

17251731
// token descriptors (Pre_Shared for one scenario and KeyWrap for another one)
17261732
var tokenDescriptor_128_PreShared = CreateTokenDescriptor(signingCredentials, encryptingCredentials128_PreShared);
@@ -1729,6 +1735,9 @@ public static TheoryData<Saml2TheoryData> RoundTripEncryptedTokenTheoryData
17291735
var tokenDescriptor_KeyWrap_128_RSAOAEP = CreateTokenDescriptor(signingCredentials, encryptingCredentials_KeyWrap_128_RSAOAEP);
17301736
var tokenDescriptor_KeyWrap_192_RSAOAEP = CreateTokenDescriptor(signingCredentials, encryptingCredentials_KeyWrap_192_RSAOAEP);
17311737
var tokenDescriptor_KeyWrap_256_RSAOAEP = CreateTokenDescriptor(signingCredentials, encryptingCredentials_KeyWrap_256_RSAOAEP);
1738+
var tokenDescriptor_KeyWrap_128_Wrong_RSAOAEP_Identifier = CreateTokenDescriptor(signingCredentials, encryptingCredentials_KeyWrap_128_Wrong_RSAOAEP_Identifier);
1739+
var tokenDescriptor_KeyWrap_192_Wrong_RSAOAEP_Identifier = CreateTokenDescriptor(signingCredentials, encryptingCredentials_KeyWrap_192_Wrong_RSAOAEP_Identifier);
1740+
var tokenDescriptor_KeyWrap_256_Wrong_RSAOAEP_Identifier = CreateTokenDescriptor(signingCredentials, encryptingCredentials_KeyWrap_256_Wrong_RSAOAEP_Identifier);
17321741

17331742
var tokenDescriptor_KeyWrap_Signed = new SecurityTokenDescriptor
17341743
{
@@ -1794,6 +1803,30 @@ public static TheoryData<Saml2TheoryData> RoundTripEncryptedTokenTheoryData
17941803
TestId = nameof(tokenDescriptor_KeyWrap_256_RSAOAEP),
17951804
});
17961805

1806+
theoryData.Add(new Saml2TheoryData
1807+
{
1808+
SecurityToken = tokenHandler.CreateToken(tokenDescriptor_KeyWrap_128_Wrong_RSAOAEP_Identifier) as Saml2SecurityToken,
1809+
ValidationParameters = CreateTokenValidationParameters(signingKey, KeyingMaterial.DefaultX509Key_2048_With_KeyId),
1810+
ExpectedException = ExpectedException.NoExceptionExpected,
1811+
TestId = nameof(tokenDescriptor_KeyWrap_128_Wrong_RSAOAEP_Identifier),
1812+
});
1813+
1814+
theoryData.Add(new Saml2TheoryData
1815+
{
1816+
SecurityToken = tokenHandler.CreateToken(tokenDescriptor_KeyWrap_192_Wrong_RSAOAEP_Identifier) as Saml2SecurityToken,
1817+
ValidationParameters = CreateTokenValidationParameters(signingKey, KeyingMaterial.DefaultX509Key_2048_With_KeyId),
1818+
ExpectedException = ExpectedException.NoExceptionExpected,
1819+
TestId = nameof(tokenDescriptor_KeyWrap_192_Wrong_RSAOAEP_Identifier),
1820+
});
1821+
1822+
theoryData.Add(new Saml2TheoryData
1823+
{
1824+
SecurityToken = tokenHandler.CreateToken(tokenDescriptor_KeyWrap_256_Wrong_RSAOAEP_Identifier) as Saml2SecurityToken,
1825+
ValidationParameters = CreateTokenValidationParameters(signingKey, KeyingMaterial.DefaultX509Key_2048_With_KeyId),
1826+
ExpectedException = ExpectedException.NoExceptionExpected,
1827+
TestId = nameof(tokenDescriptor_KeyWrap_256_Wrong_RSAOAEP_Identifier),
1828+
});
1829+
17971830
return theoryData;
17981831
}
17991832
}

test/Microsoft.IdentityModel.Tokens.Tests/EncryptingCredentialsTests.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,13 @@ public static TheoryData<EncryptingCredentialsTheoryData> ConstructorATheoryData
124124
Alg = SecurityAlgorithms.RsaOaepMgf1pKeyWrap,
125125
Enc = SecurityAlgorithms.Aes128CbcHmacSha256,
126126
TestId = "ValidTest"
127+
},
128+
new EncryptingCredentialsTheoryData
129+
{
130+
Key = Default.AsymmetricEncryptionKeyPublic,
131+
Alg = SecurityAlgorithms.RsaOaepKeyWrap,
132+
Enc = SecurityAlgorithms.Aes128CbcHmacSha256,
133+
TestId = "ValidTest_WrongRsaOaepKeyWrapIdentifier"
127134
}
128135
};
129136
}

test/System.IdentityModel.Tokens.Jwt.Tests/CreateAndValidateTokens.cs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -801,6 +801,14 @@ public static TheoryData<string, SecurityTokenDescriptor, TokenValidationParamet
801801
ExpectedException.NoExceptionExpected
802802
);
803803

804+
encryptingCredentials = new EncryptingCredentials(KeyingMaterial.RsaSecurityKey_2048, SecurityAlgorithms.RsaOaepKeyWrap, SecurityAlgorithms.Aes128CbcHmacSha256);
805+
theoryData.Add(
806+
"WrongRsaOaepKeyWrapIdentifier-Aes128CbcHmacSha256",
807+
Default.SecurityTokenDescriptor(encryptingCredentials, Default.SymmetricSigningCredentials, ClaimSets.DefaultClaims),
808+
Default.TokenValidationParameters(KeyingMaterial.RsaSecurityKey_2048, Default.SymmetricSigningKey256),
809+
ExpectedException.NoExceptionExpected
810+
);
811+
804812
encryptingCredentials = new EncryptingCredentials(KeyingMaterial.RsaSecurityKey_2048, SecurityAlgorithms.RsaOaepMgf1pKeyWrap, SecurityAlgorithms.Aes192CbcHmacSha384);
805813
theoryData.Add(
806814
"RsaOaepKeyWrap-Aes192CbcHmacSha384",
@@ -809,6 +817,14 @@ public static TheoryData<string, SecurityTokenDescriptor, TokenValidationParamet
809817
ExpectedException.NoExceptionExpected
810818
);
811819

820+
encryptingCredentials = new EncryptingCredentials(KeyingMaterial.RsaSecurityKey_2048, SecurityAlgorithms.RsaOaepKeyWrap, SecurityAlgorithms.Aes192CbcHmacSha384);
821+
theoryData.Add(
822+
"WrongRsaOaepKeyWrapIdentifier-Aes192CbcHmacSha384",
823+
Default.SecurityTokenDescriptor(encryptingCredentials, Default.SymmetricSigningCredentials, ClaimSets.DefaultClaims),
824+
Default.TokenValidationParameters(KeyingMaterial.RsaSecurityKey_2048, Default.SymmetricSigningKey256),
825+
ExpectedException.NoExceptionExpected
826+
);
827+
812828
encryptingCredentials = new EncryptingCredentials(KeyingMaterial.RsaSecurityKey_2048, SecurityAlgorithms.RsaOaepMgf1pKeyWrap, SecurityAlgorithms.Aes256CbcHmacSha512);
813829
theoryData.Add(
814830
"RsaOaepKeyWrap-Aes256CbcHmacSha512",
@@ -817,6 +833,14 @@ public static TheoryData<string, SecurityTokenDescriptor, TokenValidationParamet
817833
ExpectedException.NoExceptionExpected
818834
);
819835

836+
encryptingCredentials = new EncryptingCredentials(KeyingMaterial.RsaSecurityKey_2048, SecurityAlgorithms.RsaOaepKeyWrap, SecurityAlgorithms.Aes256CbcHmacSha512);
837+
theoryData.Add(
838+
"WrongRsaOaepKeyWrapIdentifier-Aes256CbcHmacSha512",
839+
Default.SecurityTokenDescriptor(encryptingCredentials, Default.SymmetricSigningCredentials, ClaimSets.DefaultClaims),
840+
Default.TokenValidationParameters(KeyingMaterial.RsaSecurityKey_2048, Default.SymmetricSigningKey256),
841+
ExpectedException.NoExceptionExpected
842+
);
843+
820844
// signing key not found
821845
theoryData.Add(
822846
"SigningKey-Not-Found",
@@ -866,6 +890,14 @@ public static TheoryData<string, SecurityTokenDescriptor, TokenValidationParamet
866890
ExpectedException.NoExceptionExpected
867891
);
868892

893+
encryptingCredentials = new EncryptingCredentials(KeyingMaterial.RsaSecurityKey_2048, SecurityAlgorithms.RsaOaepKeyWrap, SecurityAlgorithms.Aes192CbcHmacSha384);
894+
theoryData.Add(
895+
"WrongRsaOaepKeyWrapIdentifier-Aes192CbcHmacSha384",
896+
Default.SecurityTokenDescriptor(encryptingCredentials, Default.SymmetricSigningCredentials, ClaimSets.DefaultClaims),
897+
Default.TokenValidationParameters(KeyingMaterial.RsaSecurityKey_2048, Default.SymmetricSigningKey256),
898+
ExpectedException.NoExceptionExpected
899+
);
900+
869901
return theoryData;
870902
}
871903

0 commit comments

Comments
 (0)