summaryrefslogtreecommitdiffstats
path: root/ffi/lang/python/sequoia/store.py
blob: c9ce84e86da9d408e776ce5b7985c888ec17b247 (plain)
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
from _sequoia import ffi, lib

from .error import Error
from .glue import _str, _static_str, SQObject, sq_iterator, sq_time
from .openpgp import Fingerprint, TPK

class Store(SQObject):
    _del = lib.sq_store_free

    # Keys used for communications.
    REALM_CONTACTS = _static_str(lib.SQ_REALM_CONTACTS)

    # Keys used for signing software updates.
    REALM_SOFTWARE_UPDATES = _static_str(lib.SQ_REALM_SOFTWARE_UPDATES)

    @classmethod
    def server_log(cls, ctx):
        yield from sq_iterator(
            ffi.gc(
                lib.sq_store_server_log(ctx.ref()),
                lib.sq_log_iter_free),
            lib.sq_log_iter_next,
            lambda x: Log(x, context=ctx))

    @classmethod
    def list_keys(cls, ctx):
        def next_fn(i):
            fpp = ffi.new("pgp_fingerprint_t[1]")
            key = lib.sq_key_iter_next(i, fpp)
            if key == ffi.NULL:
                return ffi.NULL
            else:
                return (Fingerprint(fpp[0], ctx),
                        Key(key, ctx))

        yield from sq_iterator(
            ffi.gc(
                lib.sq_store_list_keys(ctx.ref()),
                lib.sq_key_iter_free),
            next_fn)

    @classmethod
    def open(cls, ctx, realm=REALM_CONTACTS, name="default"):
        return Store(lib.sq_store_open(ctx.ref(), realm.encode(), name.encode()), context=ctx)


    def add(self, label, fingerprint):
        return Binding(lib.sq_store_add(self.context().ref(), self.ref(),
                                        label.encode(), fingerprint.ref()),
                       context=self.context())

    def import_(self, label, tpk):
        return TPK(lib.sq_store_import(self.context().ref(), self.ref(),
                                       label.encode(), tpk.ref()),
                   context=self.context())

    def lookup(self, label):
        return Binding(lib.sq_store_lookup(self.context().ref(), self.ref(),
                                           label.encode()),
                       self.context())

    def delete(self):
        if lib.sq_store_delete(self.ref()):
            raise Error._last(self.context())
        super(Store, self)._delete(skip_free=True)

    def iter(self):
        def next_fn(i):
            labelp = ffi.new("char *[1]")
            fpp = ffi.new("pgp_fingerprint_t[1]")
            binding = lib.sq_binding_iter_next(i, labelp, fpp)
            if binding == ffi.NULL:
                return ffi.NULL
            else:
                return (_str(labelp[0]),
                        Fingerprint(fpp[0], self.context()),
                        Binding(binding, self.context()))

        yield from sq_iterator(
            ffi.gc(
                lib.sq_store_iter(self.context().ref(), self.ref()),
                lib.sq_binding_iter_free),
            next_fn)

    def log(self):
        yield from sq_iterator(
            ffi.gc(
                lib.sq_store_log(self.context().ref(), self.ref()),
                lib.sq_log_iter_free),
            lib.sq_log_iter_next,
            lambda x: Log(x, context=self.context()))

class Binding(SQObject):
    _del = lib.sq_binding_free

    def stats(self):
        return Stats(lib.sq_binding_stats(self.context().ref(), self.ref()),
                     self.context())

    def key(self):
        return Key(lib.sq_binding_key(self.context().ref(), self.ref()),
                   self.context())

    def tpk(self):
        return TPK(lib.sq_binding_tpk(self.context().ref(), self.ref()),
                   self.context())

    def import_(self, tpk):
        return TPK(lib.sq_binding_import(self.context().ref(), self.ref(), tpk),
                   self.context())

    def rotate(self, tpk):
        return TPK(lib.sq_binding_rotate(self.context().ref(), self.ref(), tpk),
                   self.context())

    def delete(self):
        if lib.sq_binding_delete(self.ref()):
            raise Error._last(self.context())
        super(Binding, self)._delete(skip_free=True)

    def log(self):
        yield from sq_iterator(
            ffi.gc(
                lib.sq_binding_log(self.context().ref(), self.ref()),
                lib.sq_log_iter_free),
            lib.sq_log_iter_next,
            lambda x: Log(x, context=self.context()))

class Key(SQObject):
    _del = lib.sq_key_free

    def stats(self):
        return Stats(lib.sq_key_stats(self.context().ref(), self.ref()),
                     self.context())

    def tpk(self):
        return TPK(lib.sq_key_tpk(self.context().ref(), self.ref()),
                   self.context())

    def import_(self, tpk):
        return TPK(lib.sq_key_import(self.context().ref(), self.ref(), tpk),
                   self.context())

    def log(self):
        yield from sq_iterator(
            ffi.gc(
                lib.sq_key_log(self.context().ref(), self.ref()),
                lib.sq_log_iter_free),
            lib.sq_log_iter_next)


class Stats(SQObject):
    _del = lib.sq_stats_free
    def __init__(self, o, context=None):
        super(Stats, self).__init__(o, context=context)
        self.encryption = Stamps(ffi.addressof(o, "encryption"))
        self.verification = Stamps(ffi.addressof(o, "verification"))

    @property
    def created(self):
        return sq_time(self.ref().created)

    @property
    def updated(self):
        return sq_time(self.ref().updated)

    def __str__(self):
        return \
            "Stats{{created={}, updated={}, encryption={}, verification={}}}" \
            .format(self.created, self.updated, self.encryption,
                    self.verification)

class Stamps(SQObject):
    @property
    def count(self):
        return self.ref().count

    @property
    def first(self):
        return sq_time(self.ref().first)

    @property
    def last(self):
        return sq_time(self.ref().last)

    def __str__(self):
        return "Stamps{{count={}, first={}, last={}}}".format(
            self.count, self.first, self.last)

class Log(SQObject):
    _del = lib.sq_log_free

    @property
    def timestamp(self):
        return sq_time(self.ref().timestamp)

    @property
    def store(self):
        if self.ref().store == ffi.NULL:
            return None
        else:
            return Store(self.ref().store, context=self.context(),
                         owner=self)

    @property
    def binding(self):
        if self.ref().binding == ffi.NULL:
            return None
        else:
            return Binding(self.ref().binding, context=self.context(),
                           owner=self)

    @property
    def key(self):
        if self.ref().key == ffi.NULL:
            return None
        else:
            return Key(self.ref().key, context=self.context(),
                       owner=self)

    @property
    def slug(self):
        return ffi.string(self.ref().slug).decode()

    @property
    def status(self