public IContentResponse HandlePasswordSet(NameValueCollection headers, Stream inputStream)
        {
            INameValueStore store = new RegistryStorage(Settings.RegistryPath);

            int contentLen = int.Parse(headers["Content-Length"]);

            if (contentLen == 0)
            {
                using (RSAPrivateKey _temporaryKey = new RSAPrivateKey(2048))
                {
                    store.Write("Transfers", "temp-key", Convert.ToBase64String(_temporaryKey.ToArray()));
                    return(new DynamicResponse("application/public-key", _temporaryKey.PublicKey.ToArray()));
                }
            }

            string tempkey;

            if (contentLen <= 2048 && store.Read("Transfers", "temp-key", out tempkey))
            {
                byte[] bytes = IOStream.Read(inputStream, contentLen);
                using (RSAPrivateKey _temporaryKey = RSAPrivateKey.FromBytes(Convert.FromBase64String(tempkey)))
                    bytes = _temporaryKey.Decrypt(bytes);

                _content.KeyPair.SetServerPassword(bytes);
            }
            return(DynamicResponse.Empty);
        }
Esempio n. 2
0
            public void SetPassword(byte[] passbytes)
            {
                Check.ArraySize(passbytes, 1, 2048);
                Check.Assert <InvalidOperationException>(_passwordRequired && _privateBits != null && _privateKey == null);
                try
                {
                    byte[] keybytes;
                    using (Password pwd = new Password(false, passbytes))
                    {
                        pwd.IV   = IV.ToByteArray();
                        keybytes = pwd.Decrypt(_privateBits, Salt.Size.b256);
                    }
                    Check.Assert <InvalidDataException>(_privateHash.Equals(Hash.SHA256(keybytes)));
                    _privateKey = RSAPrivateKey.FromBytes(keybytes);
                    Check.Assert <InvalidDataException>(_publicHash.Equals(Hash.SHA256(_privateKey.PublicKey.ToArray())));

                    passbytes = Encryption.CurrentUser.Encrypt(passbytes);
                    RegistryStorage store = new RegistryStorage(Settings.RegistryPath);
                    store.Write("CryptoKey", _privateHash.ToString(), Convert.ToBase64String(passbytes));
                }
                catch (Exception err)
                {
                    Log.Error(err);
                    throw new InvalidDataException();
                }
            }
Esempio n. 3
0
        private void SaveSettings(string userId, string uri, string password)
        {
            INameValueStore storage = new RegistryStorage();

            try
            {
                if (String.IsNullOrEmpty(password))
                {
                    storage.Delete(uri, "UserName");
                    storage.Delete(uri, "Password");
                }
                else
                {
                    storage.Write(uri, "UserName", userId);
                    storage.Write(uri, "Password", Encryption.CurrentUser.Encrypt(password));
                }
            }
            catch
            {
            }
        }