Ejemplo n.º 1
0
        internal StandardEncryptionHeader(ILittleEndianInput is1)
        {
            Flags           = (is1.ReadInt());
            SizeExtra       = (is1.ReadInt());
            CipherAlgorithm = (CipherAlgorithm.FromEcmaId(is1.ReadInt()));
            HashAlgorithm   = (HashAlgorithm.FromEcmaId(is1.ReadInt()));
            int keySize = is1.ReadInt();

            if (keySize == 0)
            {
                // for the sake of inheritance of the cryptoAPI classes
                // see 2.3.5.1 RC4 CryptoAPI Encryption Header
                // If Set to 0x00000000, it MUST be interpreted as 0x00000028 bits.
                keySize = 0x28;
            }
            KeySize        = (keySize);
            BlockSize      = (keySize);
            CipherProvider = (CipherProvider.FromEcmaId(is1.ReadInt()));

            is1.ReadLong(); // skip reserved

            // CSPName may not always be specified
            // In some cases, the salt value of the EncryptionVerifier is the next chunk of data
            ((ByteArrayInputStream)is1).Mark(LittleEndianConsts.INT_SIZE + 1);
            int CheckForSalt = is1.ReadInt();

            ((ByteArrayInputStream)is1).Reset();

            if (CheckForSalt == 16)
            {
                CspName = ("");
            }
            else
            {
                StringBuilder builder = new StringBuilder();
                while (true)
                {
                    char c = (char)is1.ReadShort();
                    if (c == 0)
                    {
                        break;
                    }
                    builder.Append(c);
                }
                CspName = (builder.ToString());
            }

            ChainingMode = (ChainingMode.ecb);
            KeySalt      = (null);
        }
Ejemplo 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);
        }
Ejemplo 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);
            }
        }