public void DecryptKey_RSA_KeyDataNull()
        {
            var keyGen = GeneratorUtilities.GetKeyPairGenerator("RSA");

            keyGen.Init(new KeyGenerationParameters(new SecureRandom(), 1024));
            var pair = keyGen.GenerateKeyPair();

            Assert.Throws <ArgumentNullException>(() => XmlDecryption.DecryptKey(null, (RsaKeyParameters)pair.Private, false));
        }
        public void EncryptKey_RSA_UseOAEP()
        {
            byte[] data   = Encoding.ASCII.GetBytes("12345678");
            var    keyGen = GeneratorUtilities.GetKeyPairGenerator("RSA");

            keyGen.Init(new KeyGenerationParameters(new SecureRandom(), 1024));
            var pair = keyGen.GenerateKeyPair();

            byte[] encryptedData = XmlEncryption.EncryptKey(data, (RsaKeyParameters)pair.Public, true);
            byte[] decryptedData = XmlDecryption.DecryptKey(encryptedData, (RsaKeyParameters)pair.Private, true);
            Assert.Equal(data, decryptedData);
        }
        public void DecryptKey_KeyNull()
        {
            var random  = new SecureRandom();
            var ivdata  = new byte[128 / 8];
            var keydata = new byte[256 / 8];

            random.NextBytes(ivdata);
            random.NextBytes(keydata);
            var param = new ParametersWithIV(new KeyParameter(keydata), ivdata);

            Assert.Throws <ArgumentNullException>(() => XmlDecryption.DecryptKey(null, new KeyParameter(keydata)));
        }
        public void EncryptKey_AES()
        {
            var random  = new SecureRandom();
            var keydata = new byte[256 / 8];

            random.NextBytes(keydata);
            var param = new KeyParameter(keydata);

            byte[] key = Encoding.ASCII.GetBytes("123456781234567812345678");

            byte[] encryptedKey = XmlEncryption.EncryptKey(key, param);

            Assert.NotNull(encryptedKey);
            Assert.Equal(key, XmlDecryption.DecryptKey(encryptedKey, param));
        }
        public Stream Decrypt(EncryptionMethod encryptionMethod, KeyInfo keyInfo, Stream toDecrypt)
        {
            Assert.NotNull(encryptionMethod);
            Assert.NotNull(keyInfo);
            Assert.NotNull(toDecrypt);
            Assert.True(encryptionMethod.KeyAlgorithm == NS.XmlEncAES128Url ||
                        encryptionMethod.KeyAlgorithm == NS.XmlEncAES192Url ||
                        encryptionMethod.KeyAlgorithm == NS.XmlEncAES256Url);

            Assert.Equal(keyInfo.Count, 1);

            byte[] decryptedKey = null;

            foreach (KeyInfoClause clause in keyInfo)
            {
                if (clause is KeyInfoEncryptedKey)
                {
                    KeyInfoEncryptedKey encryptedKeyInfo = clause as KeyInfoEncryptedKey;
                    EncryptedKey        encryptedKey     = encryptedKeyInfo.GetEncryptedKey();

                    Assert.Equal(encryptedKey.EncryptionMethod.KeyAlgorithm, NS.XmlEncRSAOAEPUrl);
                    Assert.Equal(encryptedKey.KeyInfo.Count, 1);
                    Assert.NotEqual(_asymmetricKeys.Count, 0);

                    RsaKeyParameters rsaParams      = null;
                    RsaKeyParameters rsaInputParams = null;

                    foreach (KeyInfoClause rsa in encryptedKey.KeyInfo)
                    {
                        if (rsa is RsaKeyValue)
                        {
                            rsaParams = (rsa as RsaKeyValue).GetKey();
                            break;
                        }
                        else
                        {
                            Assert.True(false, "Invalid License - MalformedKeyInfoClause");
                        }
                    }

                    bool keyMismatch = true;
                    foreach (AsymmetricCipherKeyPair key in _asymmetricKeys)
                    {
                        RsaKeyParameters rsaKey = key.Private as RsaKeyParameters;
                        Assert.NotNull(rsaKey);

                        rsaInputParams = key.Public as RsaKeyParameters;
                        Assert.NotNull(rsaInputParams);

                        if (!PublicKeysEqual(rsaParams, rsaInputParams))
                        {
                            continue;
                        }

                        keyMismatch = false;


                        byte[] encryptedKeyValue = encryptedKey.CipherData.CipherValue;

                        if (encryptedKeyValue == null)
                        {
                            throw new System.Security.Cryptography.CryptographicException("MissingKeyCipher");
                        }

                        decryptedKey = XmlDecryption.DecryptKey(encryptedKeyValue,
                                                                rsaKey, true);
                        break;
                    }

                    if (keyMismatch)
                    {
                        throw new Exception("Invalid License - AsymmetricKeyMismatch");
                    }
                }
                else if (clause is KeyInfoName)
                {
                    Assert.True(false, "This test should not have KeyInfoName clauses");
                }
                else
                {
                    throw new System.Security.Cryptography.CryptographicException("MalformedKeyInfoClause");
                }

                break;
            }

            if (decryptedKey == null)
            {
                throw new System.Security.Cryptography.CryptographicException("KeyDecryptionFailure");
            }

            return(DecryptStream(toDecrypt, new KeyParameter(decryptedKey), "AES/CBC/PKCS7"));
        }
 public void DecryptKey_SymmetricAlgorithmNull()
 {
     Assert.Throws <ArgumentNullException>(() => XmlDecryption.DecryptKey(new byte[16], null));
 }
 public void DecryptKey_RSA_RSANull()
 {
     Assert.Throws <ArgumentNullException>(() => XmlDecryption.DecryptKey(new byte[16], null, false));
 }
        public void DecryptKey_AESCorruptedKey8Bytes()
        {
            var random  = new SecureRandom();
            var keydata = new byte[256 / 8];

            random.NextBytes(keydata);
            var param = new KeyParameter(keydata);

            byte[] key = Encoding.ASCII.GetBytes("12345678");

            byte[] encryptedKey = XmlEncryption.EncryptKey(key, param);
            encryptedKey[0] ^= 0xFF;

            Assert.Throws <System.Security.Cryptography.CryptographicException>(() => XmlDecryption.DecryptKey(encryptedKey, param));
        }
        public void DecryptKey_AESWrongKeySize()
        {
            var random  = new SecureRandom();
            var keydata = new byte[256 / 8];

            random.NextBytes(keydata);
            var param = new KeyParameter(keydata);

            byte[] key = Encoding.ASCII.GetBytes("123");

            Assert.Throws <System.Security.Cryptography.CryptographicException>(() => XmlDecryption.DecryptKey(key, param));
        }