Exemplo n.º 1
0
        private async Task <BinaryData> ClientSideDecryptInternal(BinaryData downloadedMessage, bool async, CancellationToken cancellationToken)
        {
            if (!EncryptedMessageSerializer.TryDeserialize(downloadedMessage, out var encryptedMessage))
            {
                return(downloadedMessage); // not recognized as client-side encrypted message
            }

            var encryptedMessageStream = new MemoryStream(Convert.FromBase64String(encryptedMessage.EncryptedMessageText));
            var decryptedMessageStream = await _decryptor.DecryptReadInternal(
                encryptedMessageStream,
                encryptedMessage.EncryptionData,
                ivInStream : false,
                noPadding : false,
                async : async,
                cancellationToken).ConfigureAwait(false);

            // if we got back the stream we put in, then we couldn't decrypt and are supposed to return the original
            // message to the user
            if (encryptedMessageStream == decryptedMessageStream)
            {
                return(downloadedMessage);
            }

            return(async ?
                   await BinaryData.FromStreamAsync(decryptedMessageStream, cancellationToken).ConfigureAwait(false) :
                   BinaryData.FromStream(decryptedMessageStream));
        }
        public void TryDeserializeGracefulOnBadInput(string input)
        {
            bool tryResult = EncryptedMessageSerializer.TryDeserialize(new BinaryData(input), out var parsedEncryptedMessage);

            Assert.AreEqual(false, tryResult);
            Assert.IsNull(parsedEncryptedMessage?.EncryptedMessageText);
            Assert.IsNull(parsedEncryptedMessage?.EncryptionData);
            Assert.IsNull(parsedEncryptedMessage);
        }
Exemplo n.º 3
0
        public async Task <BinaryData> ClientSideEncryptInternal(BinaryData messageToUpload, bool async, CancellationToken cancellationToken)
        {
            byte[] bytesToEncrypt = messageToUpload.ToArray();
            (byte[] ciphertext, EncryptionData encryptionData) = await _encryptor.BufferedEncryptInternal(
                new MemoryStream(bytesToEncrypt),
                async,
                cancellationToken).ConfigureAwait(false);

            return(EncryptedMessageSerializer.Serialize(new EncryptedMessage
            {
                EncryptedMessageText = Convert.ToBase64String(ciphertext),
                EncryptionData = encryptionData
            }));
        }
        public async Task <string> ClientSideEncryptInternal(string messageToUpload, bool async, CancellationToken cancellationToken)
        {
            var bytesToEncrypt = Encoding.UTF8.GetBytes(messageToUpload);

            (byte[] ciphertext, EncryptionData encryptionData) = await _encryptor.BufferedEncryptInternal(
                new MemoryStream(bytesToEncrypt),
                async,
                cancellationToken).ConfigureAwait(false);

            return(EncryptedMessageSerializer.Serialize(new EncryptedMessage
            {
                EncryptedMessageText = Convert.ToBase64String(ciphertext),
                EncryptionData = encryptionData
            }));
        }
        public void SerializeEncryptedMessage()
        {
            var result = new ClientSideEncryptor(new ClientSideEncryptionOptions(ClientSideEncryptionVersion.V1_0)
            {
                KeyEncryptionKey = GetIKeyEncryptionKey().Object,
                KeyWrapAlgorithm = KeyWrapAlgorithm
            }).BufferedEncryptInternal(
                new MemoryStream(Encoding.UTF8.GetBytes(TestMessage)),
                async: false,
                default).EnsureCompleted();
            var encryptedMessage = new EncryptedMessage()
            {
                EncryptedMessageText = Convert.ToBase64String(result.Ciphertext),
                EncryptionData       = result.EncryptionData
            };

            var serializedMessage = EncryptedMessageSerializer.Serialize(encryptedMessage);

            // success = don't throw. test values in another test with deserialization (can't control serialization order)
        }
        public void DeserializeEncryptedMessage()
        {
            var result = new ClientSideEncryptor(new ClientSideEncryptionOptions(ClientSideEncryptionVersion.V1_0)
            {
                KeyEncryptionKey = GetIKeyEncryptionKey().Object,
                KeyWrapAlgorithm = KeyWrapAlgorithm
            }).BufferedEncryptInternal(
                new MemoryStream(Encoding.UTF8.GetBytes(TestMessage)),
                async: false,
                default).EnsureCompleted();
            var encryptedMessage = new EncryptedMessage()
            {
                EncryptedMessageText = Convert.ToBase64String(result.Ciphertext),
                EncryptionData       = result.EncryptionData
            };
            var serializedMessage = EncryptedMessageSerializer.Serialize(encryptedMessage);

            var parsedEncryptedMessage = EncryptedMessageSerializer.Deserialize(serializedMessage);

            Assert.IsTrue(AreEqual(encryptedMessage, parsedEncryptedMessage));
        }
        public void TryDeserializeGracefulOnBadInput(string input)
        {
            bool tryResult = EncryptedMessageSerializer.TryDeserialize(input, out var parsedEncryptedMessage);

            Assert.AreEqual(false, tryResult);
            Assert.AreEqual(default, parsedEncryptedMessage);