def get_export_data(self, kgk_manager):
        """
        This gives you a base64 encoded string of encrypted settings data (the blob).

        :param kgk_manager: kgk manager
        :type kgk_manager: KgkManager
        :return: encrypted settings blob
        :rtype: str
        """
        kgk_block = kgk_manager.get_fresh_encrypted_kgk()
        settings_list = self.get_settings_as_dict()['settings']
        if self.remote_data:
            for domain_name in self.remote_data.keys():
                data_set = self.remote_data[domain_name]
                if 'deleted' in data_set and data_set['deleted']:
                    for i, setting_dict in enumerate(settings_list):
                        if setting_dict['domain'] == setting_dict['domain'] and datetime.strptime(
                                data_set['mDate'], "%Y-%m-%dT%H:%M:%S") > datetime.strptime(
                                setting_dict['mDate'], "%Y-%m-%dT%H:%M:%S"):
                            settings_list[i] = data_set
                if domain_name not in settings_list.keys():
                    settings_list[domain_name] = {
                        'mDate': datetime.now(),
                        'deleted': True
                    }
        settings_crypter = self.get_settings_crypter(kgk_manager)
        return b64encode(b'\x01' + kgk_manager.get_kgk_crypter_salt() + kgk_block + settings_crypter.encrypt(
            Packer.compress(json.dumps(settings_list))))
    def get_export_data(self, kgk_manager):
        """
        This gives you a base64 encoded string of encrypted settings data (the blob).

        :param kgk_manager: kgk manager
        :type kgk_manager: KgkManager
        :return: encrypted settings blob
        :rtype: str
        """
        kgk_block = kgk_manager.get_fresh_encrypted_kgk()
        settings_list = self.get_settings_as_dict()['settings']
        if self.remote_data:
            for domain_name in self.remote_data.keys():
                data_set = self.remote_data[domain_name]
                if 'deleted' in data_set and data_set['deleted']:
                    for i, setting_dict in enumerate(settings_list):
                        if setting_dict['domain'] == setting_dict['domain'] and \
                                PasswordSetting.convert_ISO_date(data_set['mDate']) > \
                                PasswordSetting.convert_ISO_date(setting_dict['mDate']):
                            settings_list[i] = data_set
                if domain_name not in settings_list.keys():
                    settings_list[domain_name] = {
                        'mDate': datetime.now(),
                        'deleted': True
                    }
        settings_crypter = self.get_settings_crypter(kgk_manager)
        return b64encode(b'\x01' + kgk_manager.get_kgk_crypter_salt() +
                         kgk_block + settings_crypter.encrypt(
                             Packer.compress(json.dumps(settings_list))))
 def test_get_export_data(self):
     settings = {
         'settings': {
             'unit.test': {
                 'domain': 'unit.test',
                 'extras': '#!"§$%&/()[]{}=-_+*<>;:.',
                 'passwordTemplate': 'xnxoaAxxxx',
                 'iterations': 5000,
                 'notes': 'Nice note!',
                 'salt': 'cGVwcGVy',
                 'cDate': '2011-02-12T11:07:31',
                 'mDate': '2011-02-12T11:07:32'
             },
             'some.domain': {
                 'domain': 'some.domain',
                 'extras': '6478593021',
                 'passwordTemplate': 'xnxoaA',
                 'iterations': 4096,
                 'salt': 'cGVwcGVy',
                 'cDate': '2013-06-17T04:03:41',
                 'mDate': '2014-08-02T10:37:12'
             }
         },
         'synced': []
     }
     salt = os.urandom(32)
     kgk_manager = KgkManager()
     kgk_manager.set_preference_manager(self.preference_manager)
     kgk_manager.create_new_kgk()
     kgk_block = kgk_manager.create_and_save_new_kgk_block(
         Crypter(Crypter.createIvKey(b'xyz', salt, iterations=3)))
     crypter = PasswordSettingsManager.get_settings_crypter(kgk_manager)
     f = open(os.path.expanduser('~/.ctSESAM_test.pws'), 'bw')
     f.write(salt + kgk_block + crypter.encrypt(
         struct.pack('!I', 0) +
         Packer.compress(json.dumps(settings).encode('utf-8'))))
     f.close()
     self.preference_manager.read_file()
     self.manager.load_local_settings(kgk_manager)
     data = b64decode(self.manager.get_export_data(kgk_manager))
     self.assertEqual(b'\x01', data[:1])
     salt = data[1:33]
     kgk_crypter = Crypter(Crypter.createIvKey(b'xyz', salt, iterations=3))
     kgk_manager2 = KgkManager()
     kgk_manager2.set_preference_manager(self.preference_manager)
     kgk_manager2.decrypt_kgk(data[33:145], kgk_crypter)
     settings_crypter = PasswordSettingsManager.get_settings_crypter(
         kgk_manager2)
     self.assertEqual(
         settings['settings'],
         json.loads(
             str(Packer.decompress(settings_crypter.decrypt(data[145:])),
                 encoding='utf-8')))
 def test_get_domain_list(self):
     settings = {
         'settings': {
             'unit.test': {
                 'domain': 'unit.test',
                 'extras': '#!"§$%&/()[]{}=-_+*<>;:.',
                 'passwordTemplate': 'xxxaoxxAxxn',
                 'iterations': 5000,
                 'notes': 'Nice note!',
                 'salt': 'cGVwcGVy',
                 'cDate': '2011-02-12T11:07:31',
                 'mDate': '2011-02-12T11:07:32'
             },
             'some.domain': {
                 'domain': 'some.domain',
                 'extras': '#!"§$%&/()[]{}=-_+*<>;:.',
                 'passwordTemplate': 'xxxo',
                 'iterations': 4096,
                 'salt': 'cGVwcGVy',
                 'cDate': '2013-06-17T04:03:41',
                 'mDate': '2014-08-02T10:37:12'
             }
         },
         'synced': []
     }
     salt = os.urandom(32)
     f = open(os.path.expanduser('~/.ctSESAM_test.pws'), 'bw')
     data = json.dumps(settings).encode('utf-8')
     kgk_manager = KgkManager()
     kgk_manager.set_preference_manager(self.preference_manager)
     kgk_manager.create_new_kgk()
     kgk_block = kgk_manager.create_and_save_new_kgk_block(
         Crypter(Crypter.createIvKey(b'xyz', salt, iterations=3)))
     crypter = PasswordSettingsManager.get_settings_crypter(kgk_manager)
     f.write(salt + kgk_block +
             crypter.encrypt(struct.pack('!I', 0) + Packer.compress(data)))
     f.close()
     self.preference_manager.read_file()
     self.manager.load_local_settings(kgk_manager)
     self.assertIn('settings', self.manager.get_settings_as_dict())
     self.assertIn('unit.test',
                   self.manager.get_settings_as_dict()['settings'])
     self.assertEqual(
         settings['settings']['unit.test'],
         self.manager.get_settings_as_dict()['settings']['unit.test'])
     self.assertIn('some.domain',
                   self.manager.get_settings_as_dict()['settings'])
     self.assertEqual(
         settings['settings']['some.domain'],
         self.manager.get_settings_as_dict()['settings']['some.domain'])
     self.assertEqual(settings, self.manager.get_settings_as_dict())
 def test_load_settings_from_file(self):
     settings = {
         'settings': {
             'unit.test': {
                 'domain': 'unit.test',
                 'passwordTemplate': 'xxxxxxxxxxo',
                 'extras': '#OWspx6;3gov0/1',
                 'iterations': 5000,
                 'notes': 'Nice note!',
                 'cDate': '2011-02-12T11:07:31',
                 'mDate': '2011-02-12T11:07:32'
             },
             'some.domain': {
                 'domain': 'some.domain',
                 'passwordTemplate': 'oxxx',
                 'extras': '6478593021',
                 'cDate': '2013-06-17T04:03:41',
                 'mDate': '2014-08-02T10:37:12'
             }
         },
         'synced': []
     }
     salt = os.urandom(32)
     data = json.dumps(settings).encode('utf-8')
     kgk_manager = KgkManager()
     kgk_manager.set_preference_manager(self.preference_manager)
     kgk_manager.create_new_kgk()
     kgk_block = kgk_manager.create_and_save_new_kgk_block(
         Crypter(Crypter.createIvKey(b'xyz', salt, iterations=3)))
     crypter = PasswordSettingsManager.get_settings_crypter(kgk_manager)
     f = open(os.path.expanduser('~/.ctSESAM_test.pws'), 'bw')
     f.write(salt + kgk_block +
             crypter.encrypt(struct.pack('!I', 0) + Packer.compress(data)))
     f.close()
     self.preference_manager.read_file()
     self.manager.load_local_settings(kgk_manager)
     self.assertIn('unit.test', self.manager.get_domain_list())
     self.assertIn('some.domain', self.manager.get_domain_list())
     self.assertEqual('xxxxxxxxxxo',
                      self.manager.get_setting('unit.test').get_template())
     self.assertEqual(
         5000,
         self.manager.get_setting('unit.test').get_iterations())
     self.assertEqual('Nice note!',
                      self.manager.get_setting('unit.test').get_notes())
     self.assertEqual(
         'oxxx',
         self.manager.get_setting('some.domain').get_template())
     self.assertEqual(
         '6478593021',
         self.manager.get_setting('some.domain').get_character_set())
    def store_local_settings(self, kgk_manager):
        """
        This actually saves the settings to a file on the disk. The file is encrypted so you need to supply the
        password.

        :param kgk_manager: kgk manager
        :type kgk_manager: KgkManager
        """
        kgk_manager.fresh_salt2()
        kgk_manager.fresh_iv2()
        settings_crypter = PasswordSettingsManager.get_settings_crypter(kgk_manager)
        sync_settings = self.sync_manager.get_binary_sync_settings()
        self.preference_manager.store_settings_data(settings_crypter.encrypt(
            struct.pack('!I', len(sync_settings)) + sync_settings +
            Packer.compress(json.dumps(self.get_settings_as_dict()))))
        kgk_manager.store_local_kgk_block()
Beispiel #7
0
    def get_binary_sync_settings(self):
        """
        returns packed sync settings

        :return: binary settings
        :rtype: bytes
        """
        if self.sync:
            return Packer.compress(json.dumps({
                "server-address": self.server_address,
                "username": self.username,
                "password": self.password,
                "certificate": self.certificate
            }).encode('utf-8'))
        else:
            return b''
Beispiel #8
0
    def store_local_settings(self, kgk_manager):
        """
        This actually saves the settings to a file on the disk. The file is encrypted so you need to supply the
        password.

        :param kgk_manager: kgk manager
        :type kgk_manager: KgkManager
        """
        kgk_manager.fresh_salt2()
        kgk_manager.fresh_iv2()
        settings_crypter = PasswordSettingsManager.get_settings_crypter(kgk_manager)
        sync_settings = self.sync_manager.get_binary_sync_settings()
        self.preference_manager.store_settings_data(settings_crypter.encrypt(
            struct.pack('!I', len(sync_settings)) + sync_settings +
            Packer.compress(json.dumps(self.get_settings_as_dict()))))
        kgk_manager.store_local_kgk_block()
    def pull(self):
        """
        Returns some mock data tor the sync test.

        :return: base64 mock data blob
        :rtype: (bool, str)
        """
        remote_data = {
            'unit.test': {
                'domain': 'unit.test',
                'length': 12,
                'iterations': 5001,
                'notes': 'another note!',
                'salt': 'cGVwcGVy',
                'usedCharacters':
                'abcdefghijklmnopqrstuvwxyzABCDEFGHJKLMNPQRTUVWXYZ0123456789',
                'cDate': '2011-02-12T11:07:31',
                'mDate': '2013-07-12T14:46:11'
            },
            'some.domain': {
                'domain': 'some.domain',
                'length': 4,
                'iterations': 4097,
                'salt': 'cGVwcGVy',
                'usedCharacters': '6478593021',
                'cDate': '2013-06-17T04:03:41',
                'mDate': '2014-08-02T10:37:11'
            },
            'third.domain': {
                'domain': 'third.domain',
                'length': 10,
                'iterations': 4098,
                'salt': 'cGVwcGVy',
                'usedCharacters': 'aeiou',
                'cDate': '2013-06-17T04:03:41',
                'mDate': '2014-08-02T10:37:11'
            }
        }
        salt = os.urandom(32)
        kgk_block = self.kgk_manager.create_and_save_new_kgk_block(
            self.kgk_manager.get_kgk_crypter(b'xyz', salt))
        settings_crypter = PasswordSettingsManager.get_settings_crypter(
            self.kgk_manager)
        return True, str(
            b64encode(b'\x01' + salt + kgk_block + settings_crypter.encrypt(
                Packer.compress(json.dumps(remote_data).encode('utf-8')))),
            encoding='utf-8')
 def test_update_from_sync(self):
     settings = {
         'settings': {
             'unit.test': {
                 'domain':
                 'unit.test',
                 'passwordTemplate':
                 'xxaAnoxxxxx',
                 'iterations':
                 5000,
                 'notes':
                 'Nice note!',
                 'salt':
                 'cGVwcGVy',
                 'usedCharacters':
                 'abcdefghijklmnopqrstuvwxyzABCDEFGHJKLMNPQRTUVWXYZ0123456789'
                 + '#!"§$%&/()[]{}=-_+*<>;:.',
                 'cDate':
                 '2011-02-12T11:07:31',
                 'mDate':
                 '2011-02-12T11:07:32'
             },
             'some.domain': {
                 'domain': 'some.domain',
                 'passwordTemplate': 'oanA',
                 'iterations': 4096,
                 'salt': 'cGVwcGVy',
                 'usedCharacters': '6478593021',
                 'cDate': '2013-06-17T04:03:41',
                 'mDate': '2014-08-02T10:37:12'
             }
         },
         'synced': []
     }
     salt = os.urandom(32)
     kgk_manager = KgkManager()
     kgk_manager.set_preference_manager(self.preference_manager)
     kgk_manager.create_new_kgk()
     kgk_block = kgk_manager.create_and_save_new_kgk_block(
         Crypter(Crypter.createIvKey('xyz'.encode('utf-8'), salt)))
     crypter = PasswordSettingsManager.get_settings_crypter(kgk_manager)
     f = open(os.path.expanduser('~/.ctSESAM_test.pws'), 'bw')
     f.write(salt + kgk_block + crypter.encrypt(
         struct.pack('!I', 0) +
         Packer.compress(json.dumps(settings).encode('utf-8'))))
     f.close()
     self.preference_manager.read_file()
     self.manager.sync_manager = MockSyncManager(kgk_manager.get_kgk())
     self.manager.load_settings(kgk_manager, 'xyz')
     self.assertIn('unit.test', self.manager.get_domain_list())
     self.assertIn('some.domain', self.manager.get_domain_list())
     self.assertIn('third.domain', self.manager.get_domain_list())
     self.assertEqual(
         5001,
         self.manager.get_setting('unit.test').get_iterations())
     self.assertEqual(
         4096,
         self.manager.get_setting('some.domain').get_iterations())
     self.assertEqual(
         4098,
         self.manager.get_setting('third.domain').get_iterations())
     file = os.path.expanduser('~/.ctSESAM_test_extra.pws')
     if os.path.isfile(file):
         try:
             import win32con
             import win32api
             win32api.SetFileAttributes(file,
                                        win32con.FILE_ATTRIBUTE_NORMAL)
         except ImportError:
             pass
         os.remove(file)
 def test_compress(self):
     packed_data = Packer.compress("Some packable information")
     self.assertEqual(
         b'AAAAGXjaC87PTVUoSEzOTkzKSVXIzEvLL8pNLMnMzwMAedUJrg==',
         b64encode(packed_data))
Beispiel #12
0
 def test_compress(self):
     packed_data = Packer.compress("Some packable information")
     self.assertEqual(b'AAAAGXjaC87PTVUoSEzOTkzKSVXIzEvLL8pNLMnMzwMAedUJrg==', b64encode(packed_data))