Exemple #1
0
        public static RequestData DecryptMessage(EncryptedMessage msg, byte[] key)
        {
            try
            {
                if (msg == null)
                {
                    throw new ArgumentNullException("msg");
                }
                if (key == null)
                {
                    throw new ArgumentNullException("key");
                }

                using (var aes = Aes.Create())
                {
                    aes.IV  = Convert.FromBase64String(msg.IV);
                    aes.Key = key;
                    using (var decryptor = aes.CreateDecryptor(aes.Key, aes.IV))
                    {
                        using (var ms = new MemoryStream(Convert.FromBase64String(msg.Message)))
                        {
                            using (var cs = new CryptoStream(ms, decryptor, CryptoStreamMode.Read))
                            {
                                using (var sr = new StreamReader(cs))
                                {
                                    var json = sr.ReadToEnd();
                                    var data = JsonConvert.DeserializeObject <RequestData>(json);
                                    return(data);
                                }
                            }
                        }
                    }
                }
            } catch (Exception)
            {
                MessageBox.Show("Received a request from the browser addon which could not be read. Please consider redo the setup process in the addon.", "Could not read browser addon request", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

            return(null);
        }
Exemple #2
0
        private void EndGetContext(IAsyncResult ar)
        {
            try
            {
                var ctx = _server.EndGetContext(ar);
                _server.BeginGetContext(EndGetContext, null);

                lock (workerLock)
                {
                    if (ctx.Request.HttpMethod == "GET" && ctx.Request.RawUrl == "/connectivity/")
                    {
                        ctx.Response.StatusCode = _pHost.Database.IsOpen ? 200 : 401;
                        ctx.Response.OutputStream.Close();
                        return;
                    }

                    if (ctx.Request.HttpMethod != "POST")
                    {
                        ctx.Response.StatusCode = 405;
                        ctx.Response.OutputStream.Close();
                        return;
                    }

                    if (ctx.Request.RawUrl != "/")
                    {
                        ctx.Response.StatusCode = 400;
                        ctx.Response.OutputStream.Close();
                        return;
                    }

                    EncryptedMessage encReqData = null;
                    RequestData      reqData    = null;
                    try
                    {
                        var rawData = new StreamReader(ctx.Request.InputStream).ReadToEnd();
                        Debug.WriteLine($"Received: {rawData}");
                        encReqData = JsonConvert.DeserializeObject <EncryptedMessage>(rawData);
                    }
                    catch (Exception)
                    {
                        ctx.Response.StatusCode = 400;
                        ctx.Response.OutputStream.Close();
                        return;
                    }

                    if (encReqData == null || string.IsNullOrEmpty(encReqData.IV) || string.IsNullOrEmpty(encReqData.Message))
                    {
                        ctx.Response.StatusCode = 400;
                        ctx.Response.OutputStream.Close();
                        return;
                    }

                    reqData = CryptoHelper.DecryptMessage(encReqData, _plugin.CryptoKey);

                    if (reqData == null || string.IsNullOrEmpty(reqData.Url))
                    {
                        ctx.Response.StatusCode = 400;
                        ctx.Response.OutputStream.Close();
                        return;
                    }

                    var responseData = FindCredentials(reqData.Url);

                    var encResponseData = CryptoHelper.EncryptMessage(responseData, _plugin.CryptoKey);

                    var response = JsonConvert.SerializeObject(encResponseData);
                    var writer   = new StreamWriter(ctx.Response.OutputStream);
                    writer.Write(response);
                    Debug.WriteLine($"Returned: {response}");

                    ctx.Response.StatusCode = 200;
                    writer.Close();
                    writer.Dispose();
                }
            }
            catch (HttpListenerException)
            {
                //system is terminating, so... that's right as it is ;)
            }
        }