private KeePassHttpResponse Send(KeePassHttpRequest request)
        {
            if (this.Debug)
            {
                this.RequestList.Add(DateTime.Now, request);
            }

            try
            {
                using (WebClient webClient = new WebClient())
                {
                    webClient.Headers[HttpRequestHeader.ContentType] = "application/json";
                    string requestString         = request.ToJson();
                    KeePassHttpResponse response = webClient.UploadString(this.GetKeePassHttpUri(), requestString).FromJson <KeePassHttpResponse>();;

                    if (this.Debug)
                    {
                        this.ResponseList.Add(DateTime.Now, response);
                    }

                    return(response);
                }
            }
            catch (WebException ex)
            {
                throw new KeePassHttpException("Error connecting to KeePassHttp", ex);
            }
        }
        public void Associate()
        {
            if (this.Hash != null)
            {
                KeePassHttpRequest request = new KeePassHttpRequest();
                if (this.Key == null)
                {
                    this.GenerateKey();
                }

                request.Key = Convert.ToBase64String(this.Key);
                this.SetVerifier(request);
                request.RequestType = KeePassHttpRequestType.ASSOCIATE;
                KeePassHttpResponse response = this.Send(request);

                this.Id = response.Id;
                if (!response.Success || this.Id == null)
                {
                    throw new Exception(string.Format("KeePassHttp association failed ({0})", response.Error));
                }
            }
            else
            {
                throw new KeePassHttpException("KeePassHttp disconnected");
            }
        }
 public bool Connect()
 {
     if (this.Hash == null)
     {
         KeePassHttpRequest request = new KeePassHttpRequest {
             RequestType = KeePassHttpRequestType.TEST_ASSOCIATE
         };
         this.SetVerifier(request);
         KeePassHttpResponse response = this.Send(request);
         this.Hash = response.Hash;
         return(response.Success);
     }
     return(true);
 }
        private void SetVerifier(KeePassHttpRequest request)
        {
            this.AesManaged.GenerateIV();

            request.Id    = this.Id;
            request.Nonce = Convert.ToBase64String(AesManaged.IV);

            using (ICryptoTransform encryptor = this.AesManaged.CreateEncryptor())
            {
                byte[] bytes  = Encoding.UTF8.GetBytes(request.Nonce);
                byte[] buffer = encryptor.TransformFinalBlock(bytes, 0, bytes.Length);
                request.Verifier = Convert.ToBase64String(buffer);
            }
        }
        private KeePassCredential[] RetrieveCredentials(string searchString, KeePassHttpRequestSearchField searchField)
        {
            if (this.Key != null && this.Id != null)
            {
                KeePassHttpRequest request = new KeePassHttpRequest();
                if (searchField == KeePassHttpRequestSearchField.Url)
                {
                    request.RequestType = KeePassHttpRequestType.GET_LOGINS;
                }
                else if (searchField == KeePassHttpRequestSearchField.Any)
                {
                    request.RequestType = KeePassHttpRequestType.GET_LOGINS_CUSTOM_SEARCH;
                }

                this.SetVerifier(request);
                using (ICryptoTransform encryptor = this.AesManaged.CreateEncryptor())
                {
                    byte[] bytes  = Encoding.UTF8.GetBytes(searchString);
                    byte[] buffer = encryptor.TransformFinalBlock(bytes, 0, bytes.Length);

                    if (searchField == KeePassHttpRequestSearchField.Url)
                    {
                        request.Url = Convert.ToBase64String(buffer);
                    }
                    else if (searchField == KeePassHttpRequestSearchField.Any)
                    {
                        request.SearchString = Convert.ToBase64String(buffer);
                    }
                }

                KeePassHttpResponse response = this.Send(request);

                if (!response.Success)
                {
                    throw new KeePassHttpException(String.Format("Error requesting KeePass credentials ({0})", response.Error ?? "unknown error from KeePassHttp"));
                }

                return(response.Entries.Select(entry => this.DecryptEntry(entry, response.Nonce)).ToArray());
            }

            return(null);
        }