/b-gtk/fix-viz

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/b-gtk/fix-viz
399.3.28 by Daniel Schierbeck
Added license and copyright information to crypt module.
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
399.3.32 by Daniel Schierbeck
Fixed encoding error.
15
__copyright__ = 'Copyright (C) 2008 Daniel Schierbeck'
399.3.28 by Daniel Schierbeck
Added license and copyright information to crypt module.
16
__author__ = 'Daniel Schierbeck <daniel.schierbeck@gmail.com>'
399.3.4 by Daniel Schierbeck
Moved crypt code to crypt.py.
17
18
import dbus
19
20
BUS_NAME = 'org.gnome.seahorse'
399.3.5 by Daniel Schierbeck
Added support for key fingerprints.
21
399.3.4 by Daniel Schierbeck
Moved crypt code to crypt.py.
22
CRYPTO_INTERFACE = 'org.gnome.seahorse.CryptoService'
23
CRYPTO_PATH = '/org/gnome/seahorse/crypto'
24
399.3.5 by Daniel Schierbeck
Added support for key fingerprints.
25
OPENPGP_INTERFACE = 'org.gnome.seahorse.Keys'
26
OPENPGP_PATH = '/org/gnome/seahorse/keys/openpgp'
27
399.3.4 by Daniel Schierbeck
Moved crypt code to crypt.py.
28
KEY_TYPE_OPENPGP = 'openpgp'
29
KEY_TYPE_SSH = 'ssh'
30
450.5.1 by Daniel Schierbeck
Fixed check for Seahorse.
31
try:
32
    dbus.validate_bus_name(BUS_NAME)
33
except ValueError:
34
    raise ImportError
35
399.3.4 by Daniel Schierbeck
Moved crypt code to crypt.py.
36
bus = dbus.SessionBus()
399.3.27 by Daniel Schierbeck
Only show Signature tab if DBus and Seahorse are installed.
37
399.3.4 by Daniel Schierbeck
Moved crypt code to crypt.py.
38
crypto = dbus.Interface(bus.get_object(BUS_NAME, CRYPTO_PATH), 
39
                        CRYPTO_INTERFACE)
399.3.5 by Daniel Schierbeck
Added support for key fingerprints.
40
openpgp = dbus.Interface(bus.get_object(BUS_NAME, OPENPGP_PATH),
399.3.25 by Daniel Schierbeck
Added discovery of unknown keys.
41
                         OPENPGP_INTERFACE)
399.3.4 by Daniel Schierbeck
Moved crypt code to crypt.py.
42
399.3.7 by Daniel Schierbeck
Made Seahorse discover unknown keys, and added trust level to the UI.
43
FLAG_VALID = 0x0001
44
FLAG_CAN_ENCRYPT = 0x0002
45
FLAG_CAN_SIGN = 0x0004
46
FLAG_EXPIRED = 0x0100
47
FLAG_REVOKED = 0x0200
48
FLAG_DISABLED = 0x0400
49
FLAG_TRUSTED = 0x1000
50
399.3.6 by Daniel Schierbeck
Added support for key trust.
51
TRUST_NEVER = -1
52
TRUST_UNKNOWN = 0
53
TRUST_MARGINAL = 1
54
TRUST_FULL = 5
55
TRUST_ULTIMATE = 10
56
399.3.24 by Daniel Schierbeck
Added key location getter.
57
LOCATION_MISSING = 10
58
LOCATION_SEARCHING = 20
59
LOCATION_REMOTE = 50
60
LOCATION_LOCAL = 100
61
450.5.3 by Daniel Schierbeck
Made the signature checking code not try to discover the signature key.
62
keyset = dict()
399.3.7 by Daniel Schierbeck
Made Seahorse discover unknown keys, and added trust level to the UI.
63
399.3.4 by Daniel Schierbeck
Moved crypt code to crypt.py.
64
def verify(crypttext):
399.3.8 by Daniel Schierbeck
Moved crypt code into a Key class.
65
    (cleartext, key) = crypto.VerifyText(KEY_TYPE_OPENPGP, 1, crypttext)
66
450.5.3 by Daniel Schierbeck
Made the signature checking code not try to discover the signature key.
67
    if key != "":
68
        if key not in keyset:
69
            keyset[key] = Key(key)
70
71
        return keyset[key]
399.3.8 by Daniel Schierbeck
Moved crypt code into a Key class.
72
73
class Key:
74
75
    def __init__(self, key):
76
        self.key = key
77
        self.fingerprint = None
78
        self.trust = None
79
        self.flags = None
399.3.10 by Daniel Schierbeck
Added getter for display name field.
80
        self.display_name = None
399.3.24 by Daniel Schierbeck
Added key location getter.
81
        self.location = None
450.5.3 by Daniel Schierbeck
Made the signature checking code not try to discover the signature key.
82
        
83
        (keys, unmatched) = openpgp.MatchKeys([self.get_id()], 0x00000010)
84
        self.available = (key in keys)
399.3.25 by Daniel Schierbeck
Added discovery of unknown keys.
85
399.3.8 by Daniel Schierbeck
Moved crypt code into a Key class.
86
    def get_field(self, field, default=None):
87
        (valid, value) = openpgp.GetKeyField(self.key, field)
88
89
        if valid:
90
            return value
91
        else:
92
            return default
93
    
94
    def get_flags(self):
95
        if self.flags is None:
96
            self.flags = self.get_field('flags', 0)
97
98
        return self.flags
99
399.3.10 by Daniel Schierbeck
Added getter for display name field.
100
    def get_display_name(self):
101
        if self.display_name is None:
102
            self.display_name = self.get_field('display-name')
103
104
        return self.display_name
105
399.3.8 by Daniel Schierbeck
Moved crypt code into a Key class.
106
    def get_id(self):
107
        return self.key.split(':')[1][8:]
108
109
    def get_fingerprint(self):
110
        if self.fingerprint is None:
111
            self.fingerprint = self.get_field('fingerprint')
112
113
        return self.fingerprint
114
115
    def get_trust(self):
116
        if self.trust is None:
117
            self.trust = self.get_field('trust', TRUST_UNKNOWN)
118
119
        return self.trust
120
399.3.24 by Daniel Schierbeck
Added key location getter.
121
    def get_location(self):
122
        if self.location is None:
123
            self.location = self.get_field('location', LOCATION_MISSING)
124
125
        return self.location
126
399.3.11 by Daniel Schierbeck
Fixed error with unavailable keys.
127
    def is_available(self):
450.5.3 by Daniel Schierbeck
Made the signature checking code not try to discover the signature key.
128
        return self.available
399.3.8 by Daniel Schierbeck
Moved crypt code into a Key class.
129
130
    def is_trusted(self):
131
        return self.get_flags() & FLAG_TRUSTED