Esempio n. 1
0
        public void Test()
        {
            Assert.AreEqual(128, CipherAlgorithm.aes128.defaultKeySize);

            foreach (CipherAlgorithm alg in CipherAlgorithm.Values)
            {
                Assert.AreEqual(alg, CipherAlgorithm.ValueOf(alg.ToString()));
            }

            Assert.AreEqual(CipherAlgorithm.aes128, CipherAlgorithm.FromEcmaId(0x660E));
            Assert.AreEqual(CipherAlgorithm.aes192, CipherAlgorithm.FromXmlId("AES", 192));

            try
            {
                CipherAlgorithm.FromEcmaId(0);
                Assert.Fail("Should throw exception");
            }
            catch (EncryptedDocumentException)
            {
                // expected
            }

            try
            {
                CipherAlgorithm.FromXmlId("AES", 1);
                Assert.Fail("Should throw exception");
            }
            catch (EncryptedDocumentException)
            {
                // expected
            }

            try
            {
                CipherAlgorithm.FromXmlId("RC1", 0x40);
                Assert.Fail("Should throw exception");
            }
            catch (EncryptedDocumentException)
            {
                // expected
            }
        }
Esempio n. 2
0
        protected internal AgileEncryptionHeader(EncryptionDocument ed)
        {
            CT_KeyData keyData;

            try
            {
                keyData = ed.GetEncryption().keyData;
                if (keyData == null)
                {
                    throw new NullReferenceException("keyData not Set");
                }
            }
            catch (Exception e)
            {
                throw new EncryptedDocumentException("Unable to parse keyData");
            }

            KeySize   = ((int)keyData.keyBits);
            Flags     = (0);
            SizeExtra = (0);
            CspName   = (null);
            BlockSize = (int)(keyData.blockSize);

            int keyBits = (int)keyData.keyBits;

            CipherAlgorithm ca = CipherAlgorithm.FromXmlId(keyData.cipherAlgorithm.ToString(), keyBits);

            CipherAlgorithm = (ca);
            CipherProvider  = (ca.provider);

            switch (keyData.cipherChaining)
            {
            case ST_CipherChaining.ChainingModeCBC:
                ChainingMode = (ChainingMode.cbc);
                break;

            case ST_CipherChaining.ChainingModeCFB:
                ChainingMode = (ChainingMode.cfb);
                break;

            default:
                throw new EncryptedDocumentException("Unsupported chaining mode - " + keyData.cipherChaining.ToString());
            }

            int hashSize = (int)keyData.hashSize;

            HashAlgorithm ha = HashAlgorithm.FromEcmaId(keyData.hashAlgorithm.ToString());

            HashAlgorithm = (ha);

            if (HashAlgorithm.hashSize != hashSize)
            {
                throw new EncryptedDocumentException("Unsupported hash algorithm: " +
                                                     keyData.hashAlgorithm + " @ " + hashSize + " bytes");
            }

            int saltLength = (int)keyData.saltSize;

            SetKeySalt(keyData.saltValue);
            if (KeySalt.Length != saltLength)
            {
                throw new EncryptedDocumentException("Invalid salt length");
            }

            CT_DataIntegrity di = ed.GetEncryption().dataIntegrity;

            SetEncryptedHmacKey(di.encryptedHmacKey);
            SetEncryptedHmacValue(di.encryptedHmacValue);
        }
Esempio n. 3
0
        protected internal AgileEncryptionVerifier(EncryptionDocument ed)
        {
            IEnumerator <CT_KeyEncryptor> encList = ed.GetEncryption().keyEncryptors.keyEncryptor.GetEnumerator();
            CT_PasswordKeyEncryptor       keyData;

            try
            {
                //keyData = encList.Next().EncryptedPasswordKey;
                encList.MoveNext();
                keyData = encList.Current.Item as CT_PasswordKeyEncryptor;
                if (keyData == null)
                {
                    throw new NullReferenceException("encryptedKey not Set");
                }
            }
            catch (Exception e)
            {
                throw new EncryptedDocumentException("Unable to parse keyData", e);
            }

            int keyBits = (int)keyData.keyBits;

            CipherAlgorithm ca = CipherAlgorithm.FromXmlId(keyData.cipherAlgorithm.ToString(), keyBits);

            CipherAlgorithm = (ca);

            int hashSize = (int)keyData.hashSize;

            HashAlgorithm ha = HashAlgorithm.FromEcmaId(keyData.hashAlgorithm.ToString());

            HashAlgorithm = (ha);

            if (HashAlgorithm.hashSize != hashSize)
            {
                throw new EncryptedDocumentException("Unsupported hash algorithm: " +
                                                     keyData.hashAlgorithm + " @ " + hashSize + " bytes");
            }

            SpinCount         = (int)(keyData.spinCount);
            EncryptedVerifier = (keyData.encryptedVerifierHashInput);
            Salt                  = (keyData.saltValue);
            EncryptedKey          = (keyData.encryptedKeyValue);
            EncryptedVerifierHash = (keyData.encryptedVerifierHashValue);

            int saltSize = (int)keyData.saltSize;

            if (saltSize != Salt.Length)
            {
                throw new EncryptedDocumentException("Invalid salt size");
            }

            switch (keyData.cipherChaining)
            {
            case ST_CipherChaining.ChainingModeCBC:
                ChainingMode = (ChainingMode.cbc);
                break;

            case ST_CipherChaining.ChainingModeCFB:
                ChainingMode = (ChainingMode.cfb);
                break;

            default:
                throw new EncryptedDocumentException("Unsupported chaining mode - " + keyData.cipherChaining.ToString());
            }
            //if (!encList.HasNext()) return;

            try
            {
                //CertificateFactory cf = CertificateFactory.GetInstance("X.509");
                while (encList.MoveNext())
                {
                    CT_CertificateKeyEncryptor certKey = encList.Current.Item as CT_CertificateKeyEncryptor;
                    AgileCertificateEntry      ace     = new AgileCertificateEntry();
                    ace.certVerifier = certKey.certVerifier;
                    ace.encryptedKey = certKey.encryptedKeyValue;
                    ace.x509         = new X509Certificate(X509CertificateStructure.GetInstance(certKey.X509Certificate));
                    certList.Add(ace);
                }
            }
            catch (Exception e)
            {
                throw new EncryptedDocumentException("can't parse X509 certificate", e);
            }
        }