/b-gtk/fix-viz

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/b-gtk/fix-viz

« back to all changes in this revision

Viewing changes to crypt.py

  • Committer: Daniel Schierbeck
  • Date: 2008-04-02 08:28:59 UTC
  • mto: (450.1.16 trunk)
  • mto: This revision was merged to the branch mainline in revision 458.
  • Revision ID: daniel.schierbeck@gmail.com-20080402082859-p473giuvltz6p9bb
Added comparison with revision committer.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# This program is free software; you can redistribute it and/or modify
2
 
# it under the terms of the GNU General Public License as published by
3
 
# the Free Software Foundation; either version 2 of the License, or
4
 
# (at your option) any later version.
5
 
 
6
 
# This program is distributed in the hope that it will be useful,
7
 
# but WITHOUT ANY WARRANTY; without even the implied warranty of
8
 
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
9
 
# GNU General Public License for more details.
10
 
 
11
 
# You should have received a copy of the GNU General Public License
12
 
# along with this program; if not, write to the Free Software
13
 
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
14
 
 
15
 
__copyright__ = 'Copyright (C) 2008 Daniel Schierbeck'
16
 
__author__ = 'Daniel Schierbeck <daniel.schierbeck@gmail.com>'
17
1
 
18
2
import dbus
19
3
 
28
12
KEY_TYPE_OPENPGP = 'openpgp'
29
13
KEY_TYPE_SSH = 'ssh'
30
14
 
31
 
try:
32
 
    bus = dbus.SessionBus()
33
 
except dbus.exceptions.DBusException, e:
34
 
    get_name = getattr(e, 'get_dbus_name', None)
35
 
    if get_name is not None:
36
 
        name = get_name()
37
 
    else:
38
 
        name = getattr(e, '_dbus_error_name', None)
39
 
    # DBus sometimes fails like this, just treat it as if seahorse is not
40
 
    # available rather than crashing.
41
 
    if name == "org.freedesktop.DBus.Error.Spawn.ExecFailed":
42
 
        raise ImportError
43
 
    else:
44
 
        raise
45
 
 
46
 
if hasattr(bus, 'list_activatable_names'):
47
 
    bus_names = bus.list_activatable_names()
48
 
else:
49
 
    bus_object = bus.get_object('org.freedesktop.DBus', '/org/freedesktop/DBus')
50
 
    bus_names = bus_object.ListNames(dbus_interface='org.freedesktop.DBus')
51
 
 
52
 
if BUS_NAME not in bus_names:
53
 
    raise ImportError
54
 
 
 
15
bus = dbus.SessionBus()
55
16
crypto = dbus.Interface(bus.get_object(BUS_NAME, CRYPTO_PATH), 
56
17
                        CRYPTO_INTERFACE)
57
18
openpgp = dbus.Interface(bus.get_object(BUS_NAME, OPENPGP_PATH),
58
 
                         OPENPGP_INTERFACE)
 
19
                      OPENPGP_INTERFACE)
59
20
 
60
21
FLAG_VALID = 0x0001
61
22
FLAG_CAN_ENCRYPT = 0x0002
71
32
TRUST_FULL = 5
72
33
TRUST_ULTIMATE = 10
73
34
 
74
 
LOCATION_MISSING = 10
75
 
LOCATION_SEARCHING = 20
76
 
LOCATION_REMOTE = 50
77
 
LOCATION_LOCAL = 100
78
 
 
79
 
keyset = dict()
 
35
def discover(*key_ids):
 
36
    return openpgp.DiscoverKeys(key_ids, 0)
80
37
 
81
38
def verify(crypttext):
82
39
    (cleartext, key) = crypto.VerifyText(KEY_TYPE_OPENPGP, 1, crypttext)
83
40
 
84
 
    if key != "":
85
 
        if key not in keyset:
86
 
            keyset[key] = Key(key)
87
 
 
88
 
        return keyset[key]
 
41
    return Key(key)
89
42
 
90
43
class Key:
91
44
 
92
45
    def __init__(self, key):
93
46
        self.key = key
94
 
 
95
 
        (keys, unmatched) = openpgp.MatchKeys([self.get_id()], 0x00000010)
96
 
        self.available = (key in keys)
97
 
 
98
 
        if self.available:
99
 
            fields = openpgp.GetKeyFields(key, ['fingerprint', 'trust', 'flags', 'display-name', 'location'])
 
47
        self.fingerprint = None
 
48
        self.trust = None
 
49
        self.flags = None
 
50
        self.display_name = None
 
51
 
 
52
    def get_field(self, field, default=None):
 
53
        (valid, value) = openpgp.GetKeyField(self.key, field)
 
54
 
 
55
        if valid:
 
56
            return value
100
57
        else:
101
 
            fields = dict()
102
 
 
103
 
        self.fingerprint = fields.get('fingerprint', 'N/A')
104
 
        self.trust = fields.get('trust', TRUST_UNKNOWN)
105
 
        self.flags = fields.get('flags', 0)
106
 
        self.display_name = fields.get('display-name', '')
107
 
        self.location = fields.get('location', LOCATION_MISSING)
 
58
            return default
108
59
    
109
60
    def get_flags(self):
 
61
        if self.flags is None:
 
62
            self.flags = self.get_field('flags', 0)
 
63
 
110
64
        return self.flags
111
65
 
112
66
    def get_display_name(self):
 
67
        if self.display_name is None:
 
68
            self.display_name = self.get_field('display-name')
 
69
 
113
70
        return self.display_name
114
71
 
115
72
    def get_id(self):
116
73
        return self.key.split(':')[1][8:]
117
74
 
118
75
    def get_fingerprint(self):
 
76
        if self.fingerprint is None:
 
77
            self.fingerprint = self.get_field('fingerprint')
 
78
 
119
79
        return self.fingerprint
120
80
 
121
81
    def get_trust(self):
 
82
        if self.trust is None:
 
83
            self.trust = self.get_field('trust', TRUST_UNKNOWN)
 
84
 
122
85
        return self.trust
123
86
 
124
 
    def get_location(self):
125
 
        return self.location
126
 
 
127
87
    def is_available(self):
128
 
        return self.available
 
88
        return self.key != ""
129
89
 
130
90
    def is_trusted(self):
131
 
        return self.flags & FLAG_TRUSTED != 0
 
91
        return self.get_flags() & FLAG_TRUSTED