/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 15:32:02 UTC
  • mto: (450.1.16 trunk)
  • mto: This revision was merged to the branch mainline in revision 458.
  • Revision ID: daniel.schierbeck@gmail.com-20080402153202-zebltqzuw5sbbe4d
Updated NEWS.

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
 
    crypto = dbus.Interface(bus.get_object(BUS_NAME, CRYPTO_PATH), 
34
 
                            CRYPTO_INTERFACE)
35
 
    openpgp = dbus.Interface(bus.get_object(BUS_NAME, OPENPGP_PATH),
36
 
                             OPENPGP_INTERFACE)
37
 
except dbus.exceptions.DBusException, e:
38
 
    get_name = getattr(e, 'get_dbus_name', None)
39
 
    if get_name is not None:
40
 
        name = get_name()
41
 
    else:
42
 
        name = getattr(e, '_dbus_error_name', None)
43
 
    # DBus sometimes fails like this, just treat it as if seahorse is not
44
 
    # available rather than crashing.
45
 
    if name in ("org.freedesktop.DBus.Error.Spawn.ExecFailed", 
46
 
                "org.freedesktop.DBus.Error.ServiceUnknown"):
47
 
        raise ImportError
48
 
    else:
49
 
        raise
 
15
bus = dbus.SessionBus()
 
16
crypto = dbus.Interface(bus.get_object(BUS_NAME, CRYPTO_PATH), 
 
17
                        CRYPTO_INTERFACE)
 
18
openpgp = dbus.Interface(bus.get_object(BUS_NAME, OPENPGP_PATH),
 
19
                         OPENPGP_INTERFACE)
50
20
 
51
21
FLAG_VALID = 0x0001
52
22
FLAG_CAN_ENCRYPT = 0x0002
67
37
LOCATION_REMOTE = 50
68
38
LOCATION_LOCAL = 100
69
39
 
70
 
keyset = dict()
 
40
def discover(*key_ids):
 
41
    return openpgp.DiscoverKeys(key_ids, 0)
71
42
 
72
43
def verify(crypttext):
73
44
    (cleartext, key) = crypto.VerifyText(KEY_TYPE_OPENPGP, 1, crypttext)
74
45
 
75
 
    if key != "":
76
 
        if key not in keyset:
77
 
            keyset[key] = Key(key)
78
 
 
79
 
        return (cleartext, keyset[key])
80
 
 
81
 
    return (cleartext, None)
82
 
 
 
46
    return Key(key)
83
47
 
84
48
class Key:
85
49
 
86
50
    def __init__(self, key):
87
51
        self.key = key
88
 
 
89
 
        (keys, unmatched) = openpgp.MatchKeys([self.get_id()], 0x00000010)
90
 
        self.available = (key in keys)
91
 
 
92
 
        if self.available:
93
 
            fields = openpgp.GetKeyFields(key, ['fingerprint', 'trust', 'flags', 'display-name', 'location'])
 
52
        self.fingerprint = None
 
53
        self.trust = None
 
54
        self.flags = None
 
55
        self.display_name = None
 
56
        self.location = None
 
57
 
 
58
        discover(key)
 
59
 
 
60
    def get_field(self, field, default=None):
 
61
        (valid, value) = openpgp.GetKeyField(self.key, field)
 
62
 
 
63
        if valid:
 
64
            return value
94
65
        else:
95
 
            fields = dict()
96
 
 
97
 
        self.fingerprint = fields.get('fingerprint', 'N/A')
98
 
        self.trust = fields.get('trust', TRUST_UNKNOWN)
99
 
        self.flags = fields.get('flags', 0)
100
 
        self.display_name = fields.get('display-name', '')
101
 
        self.location = fields.get('location', LOCATION_MISSING)
 
66
            return default
102
67
    
103
68
    def get_flags(self):
 
69
        if self.flags is None:
 
70
            self.flags = self.get_field('flags', 0)
 
71
 
104
72
        return self.flags
105
73
 
106
74
    def get_display_name(self):
 
75
        if self.display_name is None:
 
76
            self.display_name = self.get_field('display-name')
 
77
 
107
78
        return self.display_name
108
79
 
109
80
    def get_id(self):
110
81
        return self.key.split(':')[1][8:]
111
82
 
112
83
    def get_fingerprint(self):
 
84
        if self.fingerprint is None:
 
85
            self.fingerprint = self.get_field('fingerprint')
 
86
 
113
87
        return self.fingerprint
114
88
 
115
89
    def get_trust(self):
 
90
        if self.trust is None:
 
91
            self.trust = self.get_field('trust', TRUST_UNKNOWN)
 
92
 
116
93
        return self.trust
117
94
 
118
95
    def get_location(self):
 
96
        if self.location is None:
 
97
            self.location = self.get_field('location', LOCATION_MISSING)
 
98
 
119
99
        return self.location
120
100
 
121
101
    def is_available(self):
122
 
        return self.available
 
102
        return self.key != ""
123
103
 
124
104
    def is_trusted(self):
125
 
        return self.flags & FLAG_TRUSTED != 0
 
105
        return self.get_flags() & FLAG_TRUSTED