-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkeys.py
80 lines (67 loc) · 2.62 KB
/
keys.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import pdb
from pyme import core, pygpgme
class PgpKeyError(BaseException):
def __init__(self, message = ''):
self.message = message
class KeyManager():
def __init__(self):
self.context = core.Context()
self.context.set_armor(1)
def export_keyring(self):
cypher = core.Data()
result = self.context.op_export('', 0, cypher)
cypher.seek(0,0)
return cypher.read()
def get(self, key_id):
cypher = core.Data()
result = self.context.op_export(key_id,0, cypher)
if result is None:
cypher.seek(0,0)
return cypher.read()
else:
raise PgpKeyError("Could not find key")
def search(self, search, get_sigs = False):
if get_sigs:
self.context.set_keylist_mode(pygpgme.GPGME_KEYLIST_MODE_SIG_NOTATIONS | pygpgme.GPGME_KEYLIST_MODE_SIGS)
self.context.op_keylist_start(search, 0 )
keys = []
while True:
key = self.context.op_keylist_next()
if key is None:
break
keys.append(PGPKey(key))
return keys
def add(self, key):
cypher = core.Data()
cypher.new_from_mem(key)
self.context.op_import(cypher)
result = self.context.op_import_result()
if result is not None and result.imports is not None and len(result.imports) > 0:
return len([x for x in result.imports if x.status == 0])
raise PgpKeyError("Error storing key")
class PGPKey():
def __init__(self, key):
self.uids = key.uids
self.pub_key = key.subkeys[0] #the first key is the public key
self.sub_keys = key.subkeys[1:]
def build_flags(self, obj):
result = ''
result += 'r' if hasattr(obj, 'revoked') and self.pub_key.revoked != 0 else ''
result += 'd' if hasattr(obj, 'disabled') and self.pub_key.disabled != 0 else ''
result += 'e' if hasattr(obj, 'expired') and self.pub_key.expired != 0 else ''
return result
def __str__(self):
result = ''
result += 'pub:%s:%d:%d:%s:%s:%s\n' % (
self.pub_key.keyid[8:],
self.pub_key.length,
self.pub_key.pubkey_algo,
self.pub_key.timestamp if self.pub_key.timestamp is not 0 else '',
self.pub_key.expired if self.pub_key.timestamp is not 0 else '',
self.build_flags(self.pub_key))
for uid in self.uids:
result += 'uid:%s:%d::%s\n' % (
str(uid.uid),
self.pub_key.timestamp,
self.build_flags(uid))
return result