public void EncryptedDataCanBeSetAndRetrieved()
 {
     var request = new EncryptedResponse();
     var data = "SomeEncryptedData";
     request.EncryptedData = data;
     Assert.AreEqual(data, request.EncryptedData);
 }
 public void RequestConstructorInitialisesTheValues()
 {
     var request = new EncryptedRequest();
     var response = new EncryptedResponse(request);
     // Only check one property is set, since the properties are set by the base class
     Assert.AreEqual(request.Identifier, response.RequestIdentifier);
 }
 public void FullConstructorInitialisesTheValues()
 {
     var response1 = new EncryptedResponse();
     response1.RequestIdentifier = "12345";
     var response2 = new EncryptedResponse(response1);
     // Only check one property is set, since the properties are set by the base class
     Assert.AreEqual(response1.RequestIdentifier, response2.RequestIdentifier);
 }
        /// <summary>
        /// Processes an encrypted request.
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public Response ProcessSecureRequest(ServerRequest request)
        {
            // Validate the request
            var encryptedRequest = request as EncryptedRequest;
            if (encryptedRequest == null) throw new CruiseControlException("Incoming request is not an encrypted request");
            if (!connections.ContainsKey(request.SourceName)) throw new CruiseControlException("No secure connection for the source");
            var connection = connections[request.SourceName];

            // Decrypt the data
            var crypto = new RijndaelManaged();
            crypto.Key = connection.Key;
            crypto.IV = connection.IV;
            string data = DecryptMessage(crypto, encryptedRequest.EncryptedData);

            // Find the action and message type, extract the message and invoke the method
            var response = ExtractAndInvokeMessage(data, encryptedRequest.Action,
                new RemotingChannelSecurityInformation { IsEncrypted = true });

            // Encrypt the response
            var encryptedResponse = new EncryptedResponse(request);
            encryptedResponse.EncryptedData = response.ToString();
            encryptedResponse.EncryptedData = EncryptMessage(crypto, encryptedResponse.EncryptedData);
            encryptedResponse.Result = ResponseResult.Success;

            return encryptedResponse;
        }
        public void SendMessageEncryptsMessage()
        {
            var innerConnection = new TestConnection();
            var outerConnection = new EncryptingConnection(innerConnection);
            var request = new ServerRequest();
            var expectedResponse = new Response(request);
            var actionName = "DoSomething";
            string iv = null;
            string key = null;

            innerConnection.SendMessageAction = (a, r) =>
            {
                Response sendResponse = null;
                if (a == "RetrievePublicKey")
                {
                    sendResponse = this.GenerateKeyResponse(r);
                }
                else if (a == "InitialiseSecureConnection")
                {
                    Assert.IsInstanceOf<LoginRequest>(r);
                    sendResponse = this.GenerateConnectioResponse(r as LoginRequest, out iv, out key);
                }
                else if (a == "ProcessSecureRequest")
                {
                    Assert.IsInstanceOf<EncryptedRequest>(r);
                    var actualRequest = r as EncryptedRequest;
                    Assert.AreEqual(actionName, actualRequest.Action);

                    var crypto = new RijndaelManaged();
                    crypto.Key = Convert.FromBase64String(key);
                    crypto.IV = Convert.FromBase64String(iv);
                    var requestData = DecryptMessage(crypto, actualRequest.EncryptedData);
                    Assert.AreEqual(request.ToString(), requestData);

                    var encryptedResponse = new EncryptedResponse();
                    encryptedResponse.Result = ResponseResult.Success;
                    encryptedResponse.EncryptedData = EncryptMessage(crypto, expectedResponse.ToString());
                    sendResponse = encryptedResponse;
                }
                else
                {
                    Assert.Fail("Unknown action: " + a);
                }

                return sendResponse;
            };
            var response = outerConnection.SendMessage(actionName, request);
            Assert.IsNotNull(response);
            Assert.AreEqual(expectedResponse.RequestIdentifier, response.RequestIdentifier);
        }