Example #1
0
        /// <summary>
        /// Invokes the specified data.
        /// </summary>
        /// <typeparam name="TInput">The type of the input.</typeparam>
        /// <typeparam name="TOutput">The type of the output.</typeparam>
        /// <param name="uri">The URI.</param>
        /// <param name="data">The data.</param>
        /// <param name="rsaProvider">The RSA provider.</param>
        /// <param name="clientId">The client identifier.</param>
        /// <returns></returns>
        internal static TOutput Invoke <TInput, TOutput>(this Uri uri, ClassicVirtualSecuredRequestMessagePackage <TInput> data, RSACryptoServiceProvider rsaProvider, string clientId = null)
        {
            if (uri != null && data != null && rsaProvider != null)
            {
                try
                {
                    var httpRequest = uri.CreateHttpWebRequest(HttpConstants.HttpMethod.Post);

                    if (!string.IsNullOrWhiteSpace(clientId))
                    {
                        httpRequest.SafeSetHttpHeader(VirtualSecuredTransferProtocolConstants.headerKey_ClientId, clientId);
                    }

                    var aesKeys     = AesKeys.Create();
                    var aesProvider = aesKeys.CreateAesProvider();

                    var requestRawMessage = new VirtualSecuredRequestRawMessage
                    {
                        Data                  = Encoding.UTF8.GetBytes(data.ToJson(false)),
                        SchemaVersion         = _schemaVersion,
                        SymmetricPrimaryKey   = aesKeys.Key,
                        SymmetricSecondaryKey = aesKeys.InitializationVector
                    };

                    httpRequest.FillData(HttpConstants.HttpMethod.Post, PackToBytes(requestRawMessage, rsaProvider.GetRsaKeys().PublicKey, aesProvider));

                    var httpResult    = httpRequest.ReadResponseAsBytes();
                    var responseBytes = httpResult.Body;

                    var responseRawMessage = UnpackResponseFromBytes(responseBytes, rsaProvider, aesProvider);

                    if (httpResult.HttpStatusCode.IsOK())
                    {
                        return(Encoding.UTF8.GetString(responseRawMessage.Data).TryConvertJsonToObject <TOutput>());
                    }
                    else
                    {
                        var exception = Encoding.UTF8.GetString(responseRawMessage.Data).TryConvertJsonToObject <ExceptionInfo>();

                        if (exception != null)
                        {
                            throw exception.ToException().Handle(new { uri, data });
                        }
                        else
                        {
                            throw ExceptionFactory.CreateOperationException(new { uri, data });
                        }
                    }
                }
                catch (Exception ex)
                {
                    throw ex.Handle(new { uri, data });
                }
            }

            return(default(TOutput));
        }
        public void Test()
        {
            if (serverSideRsaProvider != null)
            {
                // Simulate client side for sending request

                var utcNow = DateTime.UtcNow;
                aesKeys = AesKeys.Create();
                var aesProvider = aesKeys.CreateAesProvider();


                VirtualSecuredRequestRawMessage testRequest = new VirtualSecuredRequestRawMessage()
                {
                    SchemaVersion         = schemaVersion,
                    Stamp                 = utcNow,
                    SymmetricPrimaryKey   = aesKeys.Key,
                    SymmetricSecondaryKey = aesKeys.InitializationVector,
                    Data = requestTestData.ToByteArray(Encoding.UTF8)
                };

                var requestBytes = VirtualSecuredTransferProtocolHelper.PackToBytes(testRequest, rsaKeys.PublicKey, aesProvider);

                Assert.IsNotNull(requestBytes);


                // Simulate server side for getting request
                RijndaelProvider rijndaelProviderByRequest;
                var requestRawMessage = VirtualSecuredTransferProtocolHelper.UnpackRequestFromBytes(requestBytes, serverSideRsaProvider, out rijndaelProviderByRequest);
                Assert.IsNotNull(requestRawMessage);
                Assert.IsNotNull(rijndaelProviderByRequest);

                Assert.AreEqual(testRequest.SchemaVersion, requestRawMessage.SchemaVersion);
                Assert.IsTrue(Math.Abs((testRequest.Stamp.Value - requestRawMessage.Stamp.Value).TotalSeconds) < 1);
                Assert.AreEqual(testRequest.SymmetricPrimaryKey, requestRawMessage.SymmetricPrimaryKey);
                Assert.AreEqual(testRequest.SymmetricSecondaryKey, requestRawMessage.SymmetricSecondaryKey);
                Assert.AreEqual(requestTestData, requestRawMessage.Data.ToUtf8String());

                // Simulate server side create AES provider
                using (var serverAesProvider = new AesKeys
                {
                    KeySize = VirtualSecuredTransferProtocolHelper.DwKeySize,
                    InitializationVector = requestRawMessage.SymmetricSecondaryKey,
                    Key = requestRawMessage.SymmetricPrimaryKey
                }.CreateAesProvider())
                {
                    // Simulate server side for responding
                    utcNow = DateTime.UtcNow;
                    var testResponse = new VirtualSecuredResponseRawMessage
                    {
                        SchemaVersion = schemaVersion,
                        Stamp         = utcNow,
                        Data          = responseTestData.ToByteArray(Encoding.UTF8)
                    };
                    var responseBytes = VirtualSecuredTransferProtocolHelper.PackToBytes(testResponse, serverSideRsaProvider, serverAesProvider);
                    Assert.IsNotNull(responseBytes);


                    // Simulate client side for getting respond
                    var responseRawMessage = VirtualSecuredTransferProtocolHelper.UnpackResponseFromBytes(responseBytes, clientSideRsaProvider, aesProvider);
                    Assert.IsNotNull(responseRawMessage);
                    Assert.AreEqual(testResponse.SchemaVersion, responseRawMessage.SchemaVersion);
                    Assert.IsTrue(Math.Abs((testResponse.Stamp.Value - responseRawMessage.Stamp.Value).TotalSeconds) < 1);
                    Assert.AreEqual(responseTestData, responseRawMessage.Data.ToUtf8String());
                }
            }
        }