protected override void Read(BinaryReader reader)
        {
            base.Read(reader);

            KeyThumbprint encryptionKeyThumbprint = reader.ReadKeyThumbprint();

            if (!encryptionKeyThumbprint.Equals(_decryptionKey.Thumbprint))
            {
                throw new System.IO.IOException();
            }

            CngKey publicSourceKey = reader.ReadPublicKey().Key;

            reader.ReadBytes(PaddingLength);
            byte[] encryptedData = reader.ReadBytes(_plainTextData.Length);

            using (ECDiffieHellmanCng destinationKey = new ECDiffieHellmanCng(_decryptionKey.Key)) {
                _plainTextData = Decrypt(destinationKey, publicSourceKey, encryptedData, 0, encryptedData.Length);
            }
        }
Exemple #2
0
        /// <summary>
        /// Read this cluster from a byte array.
        /// </summary>
        /// <param name="bytes"></param>
        /// <param name="offset"></param>
        /// <param name="signatureKeys"></param>
        /// <param name="options"></param>
        public void Read(byte[] bytes, int offset, IDictionary <KeyThumbprint, PublicKey> signatureKeys, Options options)
        {
            if (bytes == null)
            {
                throw new ArgumentNullException(nameof(bytes));
            }
            if (signatureKeys == null)
            {
                throw new ArgumentNullException(nameof(signatureKeys));
            }
            if (offset < 0)
            {
                throw new ArgumentOutOfRangeException(nameof(offset));
            }
            if (offset + _clusterSizeBytes > bytes.Length)
            {
                throw new ArgumentException("Not enough space in byte array for cluster.");
            }

            using (var stream = new MemoryStream(bytes, offset, _clusterSizeBytes))
                using (var reader = new BinaryReader(stream)) {
                    if (!reader.ReadBytes(Constants.SrfsMarkerLength).SequenceEqual(Constants.SrfsMarker))
                    {
                        throw new InvalidClusterException("Invalid Marker");
                    }

                    if (!reader.ReadBytes(Constants.CurrentVersionLength).SequenceEqual(Constants.CurrentVersion))
                    {
                        throw new InvalidClusterException("Unsupported Version");
                    }

                    Signature     signature           = reader.ReadSignature();
                    byte[]        hash                = reader.ReadBytes(Constants.HashLength);
                    KeyThumbprint signatureThumbprint = reader.ReadKeyThumbprint();

                    if (options.VerifyClusterHashes() && !hash.SequenceEqual(calculateHash(bytes, offset)))
                    {
                        throw new InvalidHashException();
                    }

                    if (options.VerifyClusterSignatures())
                    {
                        PublicKey key = null;
                        if (!signatureKeys.TryGetValue(signatureThumbprint, out key))
                        {
                            throw new MissingKeyException(signatureThumbprint);
                        }

                        if (!signature.Verify(bytes, offset + HashPosition, Constants.HashLength, key.Key))
                        {
                            throw new InvalidSignatureException();
                        }
                    }

                    _volumeID    = reader.ReadGuid();
                    _clusterType = reader.ReadClusterType();

                    Read(reader);

                    _isModified = false;
                }
        }
 public MissingKeyException(KeyThumbprint thumbprint)
 {
     _thumbprint = thumbprint;
 }
Exemple #4
0
 public CngKey GetSignatureKey(KeyThumbprint thumbprint)
 {
     return(null);
 }