Replace use of Sequoia's backend with a custom key store.
- Sequoia's key store doesn't meet pep's needs (in particular, the
ability to search on a key's user id) and trying to shoehorn pep's
needs onto Sequoia's key store abstractions is just introducing
overhead with no appreciable gain in functionality.
- This patch changes the Sequoia backend to use a local sqlite
database to store the public keys.
1 // This file is under GNU General Public License 3.0
4 // generate conditions and actions
6 // Copyleft (c) 2017, 2018, p≡p foundation
8 // Written by Volker Birk
11 include ./sql_func.yml2
13 // condition: PEP_STATUS «@name»(PEP_SESSION session, bool *result)
15 condition deviceGrouped {
16 call "exec_sql_int" with "sql"
17 > "select count(*) from identity where is_own = 1 and (flags & 4) = 4;"
18 |> *result = _result > 0;
23 TID_t *t1 = &session->sync_state.keysync.challenge;
24 TID_t *t2 = &session->own_sync_state.challenge;
26 *result = _TID_greater(t1, t2);
29 condition partnerIsGrouped
30 |> *result = session->sync_state.keysync.is_group;
32 condition challengeAccepted
34 TID_t *t1 = &session->sync_state.keysync.challenge;
35 TID_t *t2 = &session->own_sync_state.challenge;
37 *result = t1->size == t2->size && memcmp(t1->buf, t2->buf, t1->size) == 0;
40 condition keyElectionWon
42 pEp_identity *from = session->sync_state.common.from;
44 assert(from && from->fpr && from->fpr[0] && from->address &&
45 from->address[0] && from->user_id && from->user_id[0]);
46 if (!(from && from->fpr && from->fpr[0] && from->address &&
47 from->address[0] && from->user_id && from->user_id[0]))
48 return PEP_ILLEGAL_VALUE;
50 pEp_identity *me = NULL;
51 PEP_STATUS status = get_identity(session, from->address, from->user_id, &me);
52 assert(status == PEP_STATUS_OK);
56 assert(me->fpr && me->fpr[0]);
57 if (!(me->fpr && me->fpr[0])) {
59 return PEP_ILLEGAL_VALUE;
62 size_t len = MIN(strlen(from->fpr), strlen(me->fpr));
63 *result = strncasecmp(from->fpr, me->fpr, len) > 0;
67 // action: PEP_STATUS «@name»(PEP_SESSION session)
73 uuid_generate_random(c);
75 OCTET_STRING_fromBuf(«$dst», (char *) c, 16);
79 function "copy_UUID" {
80 param "src", param "dst";
85 assert(src->size == 16);
86 if (!(src->size == 16))
87 return PEP_UNKNOWN_ERROR;
89 OCTET_STRING_fromBuf(dst, (char *) src->buf, src->size);
94 call "new_UUID" with "dst" > &session->own_sync_state.challenge
96 action storeChallenge call "copy_UUID" {
97 with "src" > &session->sync_state.keysync.challenge
98 with "dst" > &session->own_sync_state.challenge
101 action openTransaction
102 call "new_UUID" with "dst" > &session->sync_state.keysync.transaction
104 action storeTransaction call "copy_UUID" {
105 with "src" > &session->sync_state.keysync.transaction
106 with "dst" > &session->own_sync_state.transaction
109 function "show_handshake" {
112 assert(session->notifyHandshake);
113 if (!session->notifyHandshake)
114 return PEP_SYNC_NO_NOTIFY_CALLBACK;
116 assert(session->sync_state.common.from);
117 if (!session->sync_state.common.from)
118 return PEP_ILLEGAL_VALUE;
120 pEp_identity *from = session->sync_state.common.from;
121 pEp_identity *me = NULL;
122 PEP_STATUS status = get_identity(session, from->address, from->user_id, &me);
123 assert(status == PEP_STATUS_OK);
127 assert(me->fpr && me->fpr[0]);
128 if (!(me->fpr && me->fpr[0])) {
130 return PEP_ILLEGAL_VALUE;
133 pEp_identity *partner = identity_dup(from);
136 return PEP_OUT_OF_MEMORY;
139 status = session->notifyHandshake(me, partner, «$type»);
145 action showSoleHandshake
146 call "show_handshake" with "type" > SYNC_NOTIFY_INIT_FORM_GROUP
148 action showJoinGroupHandshake
149 call "show_handshake" with "type" > SYNC_NOTIFY_INIT_ADD_OUR_DEVICE
151 action showGroupedHandshake
152 call "show_handshake" with "type" > SYNC_NOTIFY_INIT_ADD_OTHER_DEVICE
155 call "show_handshake" with "type" > SYNC_NOTIFY_TIMEOUT
159 identity_list *il = IdentityList_to_identity_list(&session->sync_state.keysync.identities, NULL);
161 return PEP_OUT_OF_MEMORY;
163 // BUG: this should be a transaction and been rolled back completely on error
164 for (identity_list *_il = il; _il && _il->ident; _il = _il->next) {
165 PEP_STATUS status = set_identity(session, _il->ident);
167 free_identity_list(il);
172 free_identity_list(il);
175 action ownKeysAreGroupKeys {
176 call "init_sql" with "sql" {
178 "select fpr, username, comm_type, lang,"
179 " identity.flags | pgp_keypair.flags"
181 " join person on id = identity.user_id"
182 " join pgp_keypair on fpr = identity.main_key_id"
183 " join trust on id = trust.user_id"
184 " and pgp_keypair_fpr = identity.main_key_id"
185 " where identity.is_own = true ;"
190 identity_list *il = new_identity_list(NULL);
192 return PEP_OUT_OF_MEMORY;
194 pEp_identity *from = session->sync_state.common.from;
195 identity_list *_il = il;
199 result = sqlite3_step(_sql);
200 pEp_identity *_identity = NULL;
203 _identity = new_identity(
205 (const char *) sqlite3_column_text(_sql, 0),
207 (const char *) sqlite3_column_text(_sql, 1)
210 if (_identity == NULL)
211 return PEP_OUT_OF_MEMORY;
213 _identity->comm_type = (PEP_comm_type)
214 sqlite3_column_int(_sql, 2);
215 const char* const _lang = (const char *)
216 sqlite3_column_text(_sql, 3);
217 if (_lang && _lang[0]) {
218 assert(_lang[0] >= 'a' && _lang[0] <= 'z');
219 assert(_lang[1] >= 'a' && _lang[1] <= 'z');
220 assert(_lang[2] == 0);
221 _identity->lang[0] = _lang[0];
222 _identity->lang[1] = _lang[1];
223 _identity->lang[2] = 0;
225 _identity->flags = (unsigned int)
226 sqlite3_column_int(_sql, 4);
228 _il = identity_list_add(_il, _identity);
230 free_identity_list(il);
231 free_identity(_identity);
232 return PEP_OUT_OF_MEMORY;
240 free_identity_list(il);
241 return PEP_UNKNOWN_ERROR;
243 } while (result != SQLITE_DONE);
245 IdentityList_t *r = IdentityList_from_identity_list(il, &session->sync_state.keysync.identities);
246 free_identity_list(il);
248 return PEP_OUT_OF_MEMORY;