コード例 #1
0
        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);
            }
        }
コード例 #2
0
        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");
            }
        }
コード例 #3
0
 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);
 }
コード例 #4
0
        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);
            }
        }
コード例 #5
0
        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);
        }