コード例 #1
0
        public ConsumeContext Deserialize(ReceiveContext receiveContext)
        {
            if (receiveContext == null)
            {
                throw new ArgumentNullException(nameof(receiveContext));
            }

            try
            {
                var encryptionDataJson = receiveContext.TransportHeaders.Get <string>(EncryptedMessageSerializer.EncryptionHeaderDataKey);
                var encryptionData     = JsonConvert.DeserializeObject <EncryptionData>(encryptionDataJson);

                if (!encryptionData.EncryptionAgent.Protocol.Equals("1.0", StringComparison.OrdinalIgnoreCase))
                {
                    throw new NotSupportedException($"Version of {encryptionData?.EncryptionAgent?.Protocol ?? "(Not set)"} encryption agent is not supported.");
                }

                var resolver = this.keyResolver();

                var result = resolver.ResolveKeyAsync(encryptionData.WrappedContentKey.KeyId, receiveContext.CancellationToken).ConfigureAwait(false).GetAwaiter().GetResult();
                var key    = result.UnwrapKeyAsync(encryptionData.WrappedContentKey.EncryptedKey, encryptionData.WrappedContentKey.Algorithm, receiveContext.CancellationToken).ConfigureAwait(false).GetAwaiter().GetResult();

                MessageEnvelope envelope = null;
                if (encryptionData.EncryptionAgent.EncryptionAlgorithm == EncryptionAlgorithm.AES_CBC_256)
                {
                    using (var provider = new AesCryptoServiceProvider())
                        using (var transform = provider.CreateDecryptor(key, encryptionData.ContentEncryptionIV))
                            using (var cryptoStream = new DisposingCryptoStream(receiveContext.GetBody(), transform, CryptoStreamMode.Read))
                                using (var bsonReader = new BsonDataReader(cryptoStream))
                                {
                                    envelope = this.deserializer.Deserialize <MessageEnvelope>(bsonReader);
                                }
                }
                else
                {
                    throw new NotSupportedException($"{encryptionData?.EncryptionAgent?.EncryptionAlgorithm} encryption algorithm detected.");
                }

                return(new JsonConsumeContext(this.deserializer, this.objectTypeDeserializer, receiveContext, envelope));
            }
            catch (JsonSerializationException ex)
            {
                throw new SerializationException("A JSON serialization exception occurred while deserializing the message envelope", ex);
            }
            catch (SerializationException)
            {
                throw;
            }
            catch (Exception ex)
            {
                throw new SerializationException("An exception occurred while deserializing the message envelope", ex);
            }
        }
コード例 #2
0
        public void Serialize <T>(Stream stream, SendContext <T> context)
            where T : class
        {
            if (stream == null)
            {
                throw new ArgumentNullException(nameof(stream));
            }

            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            context.ContentType = EncryptedMessageSerializer.EncryptedContentType;
            var jsonMessageEnvelope = new JsonMessageEnvelope(context, context.Message, TypeMetadataCache <T> .MessageTypeNames);

            using (var transform = this.CreateAndSetEncryptionContext(context.Headers, context.CancellationToken).ConfigureAwait(false).GetAwaiter().GetResult())
                using (var encryptStream = new DisposingCryptoStream(stream, transform, CryptoStreamMode.Write))
                    using (var bsonWriter = new BsonDataWriter(encryptStream))
                    {
                        this.serializer.Serialize(bsonWriter, jsonMessageEnvelope, typeof(MessageEnvelope));
                        bsonWriter.Flush();
                    }
        }