Esempio n. 1
0
def get_keys(ui, encrypt_keyids, block_error=False):
    keys = {}
    for keyid in encrypt_keyids:
        try:
            key = crypto.get_key(keyid, validate=True, encrypt=True)
        except GPGProblem as e:
            if e.code == GPGCode.AMBIGUOUS_NAME:
                possible_keys = crypto.list_keys(hint=keyid)
                tmp_choices = [k.uids[0].uid for k in possible_keys]
                choices = {
                    str(len(tmp_choices) - x): tmp_choices[x]
                    for x in range(0, len(tmp_choices))
                }
                keyid = yield ui.choice("ambiguous keyid! Which " +
                                        "key do you want to use?",
                                        choices,
                                        cancel=None)
                if keyid:
                    encrypt_keyids.append(keyid)
                continue
            else:
                ui.notify(e.message, priority='error', block=block_error)
                continue
        keys[crypto.hash_key(key)] = key
    returnValue(keys)
Esempio n. 2
0
    def apply(self, ui):
        envelope = ui.current_buffer.envelope
        if self.action == 'rmencrypt':
            try:
                for keyid in self.encrypt_keys:
                    tmp_key = crypto.get_key(keyid)
                    del envelope.encrypt_keys[crypto.hash_key(tmp_key)]
            except GPGProblem as e:
                ui.notify(e.message, priority='error')
            if not envelope.encrypt_keys:
                envelope.encrypt = False
            ui.current_buffer.rebuild()
            return
        elif self.action == 'encrypt':
            encrypt = True
        elif self.action == 'unencrypt':
            encrypt = False
        elif self.action == 'toggleencrypt':
            encrypt = not envelope.encrypt
        envelope.encrypt = encrypt
        if encrypt:
            if not self.encrypt_keys:
                for recipient in envelope.headers['To'][0].split(','):
                    if not recipient:
                        continue
                    match = re.search("<(.*@.*)>", recipient)
                    if match:
                        recipient = match.group(0)
                    self.encrypt_keys.append(recipient)

            logging.debug("encryption keys: " + str(self.encrypt_keys))
            for keyid in self.encrypt_keys:
                try:
                    key = crypto.get_key(keyid, validate=True, encrypt=True)
                except GPGProblem as e:
                    if e.code == GPGCode.AMBIGUOUS_NAME:
                        possible_keys = crypto.list_keys(hint=keyid)
                        tmp_choices = [k.uids[0].uid for k in possible_keys]
                        choices = {
                            str(len(tmp_choices) - x): tmp_choices[x]
                            for x in range(0, len(tmp_choices))
                        }
                        keyid = yield ui.choice("ambiguous keyid! Which " +
                                                "key do you want to use?",
                                                choices,
                                                cancel=None)
                        if keyid:
                            self.encrypt_keys.append(keyid)
                        continue
                    else:
                        ui.notify(e.message, priority='error')
                        continue
                envelope.encrypt_keys[crypto.hash_key(key)] = key
            if not envelope.encrypt_keys:
                envelope.encrypt = False
        # reload buffer
        ui.current_buffer.rebuild()
Esempio n. 3
0
    def apply(self, ui):
        envelope = ui.current_buffer.envelope
        if self.action == 'rmencrypt':
            try:
                for keyid in self.encrypt_keys:
                    tmp_key = crypto.get_key(keyid)
                    del envelope.encrypt_keys[crypto.hash_key(tmp_key)]
            except GPGProblem as e:
                ui.notify(e.message, priority='error')
            if not envelope.encrypt_keys:
                envelope.encrypt = False
            ui.current_buffer.rebuild()
            return
        elif self.action == 'encrypt':
            encrypt = True
        elif self.action == 'unencrypt':
            encrypt = False
        elif self.action == 'toggleencrypt':
            encrypt = not envelope.encrypt
        envelope.encrypt = encrypt
        if encrypt:
            if not self.encrypt_keys:
                for recipient in envelope.headers['To'][0].split(','):
                    if not recipient:
                        continue
                    match = re.search("<(.*@.*)>", recipient)
                    if match:
                        recipient = match.group(0)
                    self.encrypt_keys.append(recipient)

            logging.debug("encryption keys: " + str(self.encrypt_keys))
            for keyid in self.encrypt_keys:
                try:
                    key = crypto.get_key(keyid, validate=True, encrypt=True)
                except GPGProblem as e:
                    if e.code == GPGCode.AMBIGUOUS_NAME:
                        possible_keys = crypto.list_keys(hint=keyid)
                        tmp_choices = [k.uids[0].uid for k in possible_keys]
                        choices = {str(len(tmp_choices) - x): tmp_choices[x]
                                   for x in range(0, len(tmp_choices))}
                        keyid = yield ui.choice("ambiguous keyid! Which" +
                                                "key do you want to use?",
                                                choices, cancel=None)
                        if keyid:
                            self.encrypt_keys.append(keyid)
                        continue
                    else:
                        ui.notify(e.message, priority='error')
                        continue
                envelope.encrypt_keys[crypto.hash_key(key)] = key
            if not envelope.encrypt_keys:
                envelope.encrypt = False
        # reload buffer
        ui.current_buffer.rebuild()
Esempio n. 4
0
 def __init__(self, private=False):
     """
     :param private: return private keys
     :type private: bool
     """
     keys = crypto.list_keys(private=private)
     resultlist = []
     for k in keys:
         for s in k.subkeys:
             resultlist.append(s.keyid)
         for u in k.uids:
             resultlist.append(u.email)
     StringlistCompleter.__init__(self, resultlist, match_anywhere=True)
Esempio n. 5
0
 def __init__(self, private=False):
     """
     :param private: return private keys
     :type private: bool
     """
     keys = crypto.list_keys(private=private)
     resultlist = []
     for k in keys:
         for s in k.subkeys:
             resultlist.append(s.keyid)
         for u in k.uids:
             resultlist.append(u.email)
     StringlistCompleter.__init__(self, resultlist, match_anywhere=True)
Esempio n. 6
0
def get_keys(ui, encrypt_keyids, block_error=False):
    keys = {}
    for keyid in encrypt_keyids:
        try:
            key = crypto.get_key(keyid, validate=True, encrypt=True)
        except GPGProblem as e:
            if e.code == GPGCode.AMBIGUOUS_NAME:
                possible_keys = crypto.list_keys(hint=keyid)
                tmp_choices = [k.uids[0].uid for k in possible_keys]
                choices = {str(len(tmp_choices) - x): tmp_choices[x]
                           for x in range(0, len(tmp_choices))}
                keyid = yield ui.choice("ambiguous keyid! Which " +
                                        "key do you want to use?",
                                        choices, cancel=None)
                if keyid:
                    encrypt_keyids.append(keyid)
                continue
            else:
                ui.notify(e.message, priority='error', block=block_error)
                continue
        keys[crypto.hash_key(key)] = key
    returnValue(keys)
Esempio n. 7
0
    def apply(self, ui):
        envelope = ui.current_buffer.envelope
        if self.action == 'rmencrypt':
            try:
                for keyid in self.encrypt_keys:
                    tmp_key = crypto.get_key(keyid)
                    del envelope.encrypt_keys[crypto.hash_key(tmp_key)]
            except GPGProblem as e:
                ui.notify(e.message, priority='error')
            if not envelope.encrypt_keys:
                envelope.encrypt = False
            ui.current_buffer.rebuild()
            return
        elif self.action == 'encrypt':
            encrypt = True
        elif self.action == 'unencrypt':
            encrypt = False
        elif self.action == 'toggleencrypt':
            encrypt = not envelope.encrypt
        envelope.encrypt = encrypt
        if encrypt:
            if not self.encrypt_keys:
                for recipient in envelope.headers['To'][0].split(','):
                    if not recipient:
                        continue
                    match = re.search("<(.*@.*)>", recipient)
                    if match:
                        recipient = match.group(0)
                    self.encrypt_keys.append(recipient)

            logging.debug("encryption keys: " + str(self.encrypt_keys))
            for keyid in self.encrypt_keys:
                try:
                    key = crypto.get_key(keyid, validate=True, encrypt=True)
                except GPGProblem as e:
                    if e.code == GPGCode.AMBIGUOUS_NAME:
                        possible_keys = crypto.list_keys(hint=keyid)
                        tmp_choices = [k.uids[0].uid for k in possible_keys]
Esempio n. 8
0
def get_keys(ui, encrypt_keyids, block_error=False):
    """Get several keys from the GPG keyring.  The keys are selected by keyid
    and are checked if they can be used for encryption.

    :param ui: the main user interface object
    :type ui: alot.ui.UI
    :param encrypt_keyids: the key ids of the keys to get
    :type encrypt_keyids: list(str)
    :param block_error: wether error messages for the user should expire
        automatically or block the ui
    :type block_error: bool
    :returns: the available keys indexed by their key hash
    :rtype: dict(str->gpgme.Key)

    """
    keys = {}
    for keyid in encrypt_keyids:
        try:
            key = crypto.get_key(keyid, validate=True, encrypt=True)
        except GPGProblem as e:
            if e.code == GPGCode.AMBIGUOUS_NAME:
                possible_keys = crypto.list_keys(hint=keyid)
                tmp_choices = [k.uids[0].uid for k in possible_keys]
                choices = {str(len(tmp_choices) - x): tmp_choices[x]
                           for x in range(0, len(tmp_choices))}
                keyid = yield ui.choice("ambiguous keyid! Which " +
                                        "key do you want to use?",
                                        choices, cancel=None)
                if keyid:
                    encrypt_keyids.append(keyid)
                continue
            else:
                ui.notify(e.message, priority='error', block=block_error)
                continue
        keys[crypto.hash_key(key)] = key
    returnValue(keys)
Esempio n. 9
0
 def test_list_keys_private(self):
     values = list(crypto.list_keys(hint="ambigu", private=True))[0]
     self.assertEqual(values.uids[0].email, u'*****@*****.**')
     self.assertTrue(values.secret)
Esempio n. 10
0
 def test_list_keys_pub(self):
     values = list(crypto.list_keys(hint="ambigu"))[0]
     self.assertEqual(values.uids[0].email, u'*****@*****.**')
     self.assertFalse(values.secret)
Esempio n. 11
0
 def test_list_hint(self):
     values = crypto.list_keys(hint="ambig")
     self.assertEqual(len(list(values)), 2)
Esempio n. 12
0
 def test_list_no_hints(self):
     # This only tests that you get 3 keys back (the number in our test
     # keyring), it might be worth adding tests to check more about the keys
     # returned
     values = crypto.list_keys()
     self.assertEqual(len(list(values)), 3)
Esempio n. 13
0
 def test_list_keys_private(self):
     values = list(crypto.list_keys(hint="ambigu", private=True))[0]
     self.assertEqual(values.uids[0].email, u'*****@*****.**')
     self.assertTrue(values.secret)
Esempio n. 14
0
 def test_list_keys_pub(self):
     values = list(crypto.list_keys(hint="ambigu"))[0]
     self.assertEqual(values.uids[0].email, u'*****@*****.**')
     self.assertFalse(values.secret)
Esempio n. 15
0
 def test_list_hint(self):
     values = crypto.list_keys(hint="ambig")
     self.assertEqual(len(list(values)), 2)
Esempio n. 16
0
 def test_list_no_hints(self):
     # This only tests that you get 3 keys back (the number in our test
     # keyring), it might be worth adding tests to check more about the keys
     # returned
     values = crypto.list_keys()
     self.assertEqual(len(list(values)), 3)