Merge with keysync keysync-windows
authorMarkus Schaber <markus@pep-security.net>
Tue, 04 Oct 2016 20:56:24 +0200
branchkeysync-windows
changeset 1242e7b0ff022963
parent 1232 8ca2cf56f8af
parent 1241 4e6ff02a9257
child 1245 d0c63eb5bfa6
child 1246 fb9c55250d80
Merge with keysync
src/pEpEngine.c
src/sync_impl.c
     1.1 --- a/src/pEpEngine.c	Mon Oct 03 17:16:33 2016 +0200
     1.2 +++ b/src/pEpEngine.c	Tue Oct 04 20:56:24 2016 +0200
     1.3 @@ -6,7 +6,6 @@
     1.4  #include "sync_fsm.h"
     1.5  
     1.6  static int init_count = -1;
     1.7 -char sync_uuid[37];
     1.8  
     1.9  static int user_version(void *_version, int count, char **text, char **name)
    1.10  {
    1.11 @@ -29,6 +28,8 @@
    1.12      static const char *sql_trustword;
    1.13      static const char *sql_get_identity;
    1.14      static const char *sql_set_person;
    1.15 +    static const char *sql_set_device_group;
    1.16 +    static const char *sql_get_device_group;
    1.17      static const char *sql_set_pgp_keypair;
    1.18      static const char *sql_set_identity;
    1.19      static const char *sql_set_identity_flags;
    1.20 @@ -133,7 +134,6 @@
    1.21  #define _DDL_USER_VERSION "3"
    1.22  
    1.23      if (in_first) {
    1.24 -        memset(sync_uuid, 0, 37);
    1.25  
    1.26          int_result = sqlite3_exec(
    1.27              _session->db,
    1.28 @@ -322,6 +322,12 @@
    1.29                           "    (select coalesce((select main_key_id from person "
    1.30                           "      where id = ?1), upper(replace(?4,' ',''))))) ;";
    1.31  
    1.32 +        sql_set_device_group = "update person set device_group = ?1 "
    1.33 +                               "where id = '" PEP_OWN_USERID "';";
    1.34 +
    1.35 +        sql_get_device_group = "select device_group from person "
    1.36 +                               "where id = '" PEP_OWN_USERID "';";
    1.37 +
    1.38          sql_set_pgp_keypair = "insert or replace into pgp_keypair (fpr) "
    1.39                                "values (upper(replace(?1,' ',''))) ;";
    1.40  
    1.41 @@ -416,6 +422,14 @@
    1.42              (int)strlen(sql_set_person), &_session->set_person, NULL);
    1.43      assert(int_result == SQLITE_OK);
    1.44  
    1.45 +    int_result = sqlite3_prepare_v2(_session->db, sql_set_device_group,
    1.46 +            (int)strlen(sql_set_device_group), &_session->set_device_group, NULL);
    1.47 +    assert(int_result == SQLITE_OK);
    1.48 +
    1.49 +    int_result = sqlite3_prepare_v2(_session->db, sql_get_device_group,
    1.50 +            (int)strlen(sql_get_device_group), &_session->get_device_group, NULL);
    1.51 +    assert(int_result == SQLITE_OK);
    1.52 +
    1.53      int_result = sqlite3_prepare_v2(_session->db, sql_set_pgp_keypair,
    1.54              (int)strlen(sql_set_pgp_keypair), &_session->set_pgp_keypair,
    1.55              NULL);
    1.56 @@ -582,6 +596,10 @@
    1.57                  sqlite3_finalize(session->get_identity);
    1.58              if (session->set_person)
    1.59                  sqlite3_finalize(session->set_person);
    1.60 +            if (session->set_device_group)
    1.61 +                sqlite3_finalize(session->set_device_group);
    1.62 +            if (session->get_device_group)
    1.63 +                sqlite3_finalize(session->get_device_group);
    1.64              if (session->set_pgp_keypair)
    1.65                  sqlite3_finalize(session->set_pgp_keypair);
    1.66              if (session->set_identity)
    1.67 @@ -1077,6 +1095,61 @@
    1.68          return PEP_COMMIT_FAILED;
    1.69  }
    1.70  
    1.71 +DYNAMIC_API PEP_STATUS set_device_group(
    1.72 +        PEP_SESSION session,
    1.73 +        const char *group_name
    1.74 +    )
    1.75 +{
    1.76 +    int result;
    1.77 +
    1.78 +    assert(session);
    1.79 +    assert(group_name);
    1.80 +
    1.81 +    if (!(session && group_name))
    1.82 +        return PEP_ILLEGAL_VALUE;
    1.83 +
    1.84 +    sqlite3_reset(session->set_device_group);
    1.85 +    sqlite3_bind_text(session->set_device_group, 1, group_name, -1,
    1.86 +            SQLITE_STATIC);
    1.87 +    result = sqlite3_step(session->set_device_group);
    1.88 +    sqlite3_reset(session->set_device_group);
    1.89 +    if (result != SQLITE_DONE)
    1.90 +        return PEP_CANNOT_SET_PERSON;
    1.91 +
    1.92 +    return PEP_STATUS_OK;
    1.93 +}
    1.94 +
    1.95 +DYNAMIC_API PEP_STATUS get_device_group(PEP_SESSION session, char **group_name)
    1.96 +{
    1.97 +    PEP_STATUS status = PEP_STATUS_OK;
    1.98 +    int result;
    1.99 +
   1.100 +    assert(session);
   1.101 +    assert(group_name);
   1.102 +
   1.103 +    if (!(session && group_name))
   1.104 +        return PEP_ILLEGAL_VALUE;
   1.105 +
   1.106 +    sqlite3_reset(session->get_device_group);
   1.107 +
   1.108 +    result = sqlite3_step(session->get_device_group);
   1.109 +    switch (result) {
   1.110 +    case SQLITE_ROW: {
   1.111 +        *group_name = strdup(
   1.112 +            (const char *) sqlite3_column_text(session->get_device_group, 0));
   1.113 +            if(*group_name == NULL)
   1.114 +                status = PEP_OUT_OF_MEMORY;
   1.115 +        break;
   1.116 +    }
   1.117 + 
   1.118 +    default:
   1.119 +        status = PEP_RECORD_NOT_FOUND;
   1.120 +    }
   1.121 +
   1.122 +    sqlite3_reset(session->get_device_group);
   1.123 +    return status;
   1.124 +}
   1.125 +
   1.126  DYNAMIC_API PEP_STATUS set_identity_flags(
   1.127          PEP_SESSION session,
   1.128          pEp_identity *identity,
   1.129 @@ -1824,7 +1897,7 @@
   1.130          own = 1;
   1.131      }
   1.132      else {
   1.133 -        if (name == sync_uuid || strcmp(name, sync_uuid) == 0)
   1.134 +        if (name == session->sync_uuid || strcmp(name, session->sync_uuid) == 0)
   1.135              own = 1;
   1.136      }
   1.137  
     2.1 --- a/src/pEpEngine.h	Mon Oct 03 17:16:33 2016 +0200
     2.2 +++ b/src/pEpEngine.h	Tue Oct 04 20:56:24 2016 +0200
     2.3 @@ -530,6 +530,38 @@
     2.4          PEP_SESSION session, const pEp_identity *identity
     2.5      );
     2.6  
     2.7 +// set_device_group() - update own person's device group
     2.8 +//
     2.9 +//    parameters:
    2.10 +//        session (in)        session handle
    2.11 +//        group_name (in)     new group name
    2.12 +//
    2.13 +//    return value:
    2.14 +//        PEP_STATUS_OK = 0             device group was updated
    2.15 +//        PEP_CANNOT_SET_PERSON         update failed
    2.16 +
    2.17 +DYNAMIC_API PEP_STATUS set_device_group(
    2.18 +        PEP_SESSION session,
    2.19 +        const char *group_name
    2.20 +    );
    2.21 +
    2.22 +// get_device_group() - get own person's device group
    2.23 +//
    2.24 +//    parameters:
    2.25 +//        session (in)        session handle
    2.26 +//        group_name (in)     new group name
    2.27 +//
    2.28 +//    return value:
    2.29 +//        PEP_STATUS_OK = 0             couldn't get device group
    2.30 +//        PEP_RECORD_NOT_FOUND          update failed
    2.31 +//
    2.32 +//    caveat:
    2.33 +//        the ownerships of group_name is going to the caller
    2.34 +
    2.35 +DYNAMIC_API PEP_STATUS get_device_group(
    2.36 +        PEP_SESSION session, 
    2.37 +        char **group_name
    2.38 +    );
    2.39  
    2.40  // set_identity_flags() - update identity flags on existing identity
    2.41  //
     3.1 --- a/src/pEp_internal.h	Mon Oct 03 17:16:33 2016 +0200
     3.2 +++ b/src/pEp_internal.h	Tue Oct 04 20:56:24 2016 +0200
     3.3 @@ -77,8 +77,6 @@
     3.4  
     3.5  #define NOT_IMPLEMENTED assert(0); return PEP_UNKNOWN_ERROR;
     3.6  
     3.7 -extern char sync_uuid[37];
     3.8 -
     3.9  typedef struct _pEpSession {
    3.10      const char *version;
    3.11  #ifdef USE_GPG
    3.12 @@ -97,6 +95,8 @@
    3.13      sqlite3_stmt *trustword;
    3.14      sqlite3_stmt *get_identity;
    3.15      sqlite3_stmt *set_person;
    3.16 +    sqlite3_stmt *set_device_group;
    3.17 +    sqlite3_stmt *get_device_group;
    3.18      sqlite3_stmt *set_pgp_keypair;
    3.19      sqlite3_stmt *set_identity;
    3.20      sqlite3_stmt *set_identity_flags;
    3.21 @@ -124,7 +124,7 @@
    3.22      sqlite3_stmt *sequence_value2;
    3.23      sqlite3_stmt *sequence_value3;
    3.24  
    3.25 -    // sequence value
    3.26 +    // revoked keys
    3.27      sqlite3_stmt *set_revoked;
    3.28      sqlite3_stmt *get_revoked;
    3.29  
    3.30 @@ -137,8 +137,9 @@
    3.31      inject_sync_msg_t inject_sync_msg;
    3.32      retrieve_next_sync_msg_t retrieve_next_sync_msg;
    3.33  
    3.34 -    // state machines
    3.35 +    // key sync
    3.36      DeviceState_state sync_state;
    3.37 +    char sync_uuid[37];
    3.38  
    3.39      // runtime config
    3.40  
     4.1 --- a/src/sync.c	Mon Oct 03 17:16:33 2016 +0200
     4.2 +++ b/src/sync.c	Tue Oct 04 20:56:24 2016 +0200
     4.3 @@ -6,9 +6,6 @@
     4.4  #include "asn1_helper.h"
     4.5  #include "../asn.1/DeviceGroup-Protocol.h"
     4.6  
     4.7 -static void *static_sync_obj = NULL;
     4.8 -static inject_sync_msg_t static_inject_sync_msg = NULL;
     4.9 -
    4.10  // receive_sync_msg is defined in the sync_actions
    4.11  
    4.12  PEP_STATUS receive_sync_msg(
    4.13 @@ -25,9 +22,13 @@
    4.14          retrieve_next_sync_msg_t retrieve_next_sync_msg
    4.15      )
    4.16  {
    4.17 +    assert(session && obj && messageToSend && showHandshake && inject_sync_msg && retrieve_next_sync_msg);
    4.18 +    if (!(session && obj && messageToSend && showHandshake && inject_sync_msg && retrieve_next_sync_msg))
    4.19 +        return PEP_ILLEGAL_VALUE;
    4.20 +
    4.21      unsigned char uuid[16];
    4.22      uuid_generate_random(uuid);
    4.23 -    uuid_unparse_upper(uuid, sync_uuid);
    4.24 +    uuid_unparse_upper(uuid, session->sync_uuid);
    4.25  
    4.26      session->sync_obj = obj;
    4.27      session->messageToSend = messageToSend;
    4.28 @@ -35,9 +36,6 @@
    4.29      session->inject_sync_msg = inject_sync_msg;
    4.30      session->retrieve_next_sync_msg = retrieve_next_sync_msg;
    4.31  
    4.32 -    static_sync_obj = obj;
    4.33 -    static_inject_sync_msg = inject_sync_msg;
    4.34 -
    4.35      // start state machine
    4.36      session->sync_state = InitState;
    4.37      PEP_STATUS status = fsm_DeviceState_inject(session, Init, NULL, NULL);
    4.38 @@ -47,24 +45,49 @@
    4.39      return status;
    4.40  }
    4.41  
    4.42 +DYNAMIC_API PEP_STATUS attach_sync_session(
    4.43 +        PEP_SESSION session,
    4.44 +        PEP_SESSION sync_session
    4.45 +    )
    4.46 +{
    4.47 +    assert(session && sync_session && sync_session->sync_obj && sync_session->inject_sync_msg );
    4.48 +    if (!(session && sync_session && sync_session->sync_obj && sync_session->inject_sync_msg ))
    4.49 +        return PEP_ILLEGAL_VALUE;
    4.50 +
    4.51 +    memcpy(session->sync_uuid, sync_session->sync_uuid, 37);
    4.52 +
    4.53 +    session->sync_obj = sync_session->sync_obj;
    4.54 +    session->inject_sync_msg = sync_session->inject_sync_msg;
    4.55 +
    4.56 +    return PEP_STATUS_OK;
    4.57 +}
    4.58 +
    4.59 +DYNAMIC_API PEP_STATUS detach_sync_session(PEP_SESSION session)
    4.60 +{
    4.61 +    assert(session && session->sync_obj && session->inject_sync_msg );
    4.62 +    if (!(session && session->sync_obj && session->inject_sync_msg ))
    4.63 +        return PEP_ILLEGAL_VALUE;
    4.64 +
    4.65 +    memset(session->sync_uuid, 0, 37);
    4.66 +
    4.67 +    session->sync_obj = NULL;
    4.68 +    session->inject_sync_msg = NULL;
    4.69 +
    4.70 +    return PEP_STATUS_OK;
    4.71 +}
    4.72 +
    4.73  int call_inject_sync_msg(PEP_SESSION session, void *msg)
    4.74  {
    4.75      if(session->inject_sync_msg && session->sync_obj)
    4.76          return session->inject_sync_msg(msg, session->sync_obj);
    4.77 -    else if(static_inject_sync_msg && static_sync_obj)
    4.78 -        return static_inject_sync_msg(msg, static_sync_obj);
    4.79      else
    4.80         return PEP_SYNC_NO_INJECT_CALLBACK;
    4.81  }
    4.82  
    4.83 -
    4.84  DYNAMIC_API void unregister_sync_callbacks(PEP_SESSION session) {
    4.85      // stop state machine
    4.86      session->sync_state = DeviceState_state_NONE;
    4.87  
    4.88 -    static_sync_obj = NULL;
    4.89 -    static_inject_sync_msg = NULL;
    4.90 -
    4.91      // unregister
    4.92      session->sync_obj = NULL;
    4.93      session->messageToSend = NULL;
     5.1 --- a/src/sync.h	Mon Oct 03 17:16:33 2016 +0200
     5.2 +++ b/src/sync.h	Tue Oct 04 20:56:24 2016 +0200
     5.3 @@ -115,15 +115,38 @@
     5.4          retrieve_next_sync_msg_t retrieve_next_sync_msg
     5.5      );
     5.6  
     5.7 +// attach_sync_session() - attach session to a session running keysync state machine 
     5.8 +//
     5.9 +//  parameters:
    5.10 +//      session (in)                session to attach
    5.11 +//      sync_session (in)           session running keysync
    5.12 +//
    5.13 +//  return value:
    5.14 +//      PEP_STATUS_OK or any other value on errror
    5.15 +//
    5.16 +//  caveat:
    5.17 +//      register_sync_callbacks must have been called on sync_session
    5.18 +//      call that BEFORE you're using that session in any other part of the engine
    5.19 +
    5.20 +DYNAMIC_API PEP_STATUS attach_sync_session(
    5.21 +        PEP_SESSION session,
    5.22 +        PEP_SESSION sync_session
    5.23 +    );
    5.24 +
    5.25 +// detach_sync_session() - detach previously attached sync session
    5.26 +//
    5.27 +//  parameters:
    5.28 +//      session (in)                session to detach 
    5.29 +
    5.30 +DYNAMIC_API PEP_STATUS detach_sync_session(PEP_SESSION session);
    5.31  
    5.32  // unregister_sync_callbacks() - unregister adapter's callbacks
    5.33  //
    5.34  //  parameters:
    5.35 -//      session (in)                session where to store obj handle
    5.36 +//      session (in)                session to unregister
    5.37  
    5.38  DYNAMIC_API void unregister_sync_callbacks(PEP_SESSION session);
    5.39  
    5.40 -
    5.41  // do_sync_protocol() - function to be run on an extra thread
    5.42  //
    5.43  //  parameters:
     6.1 --- a/src/sync_fsm.c	Mon Oct 03 17:16:33 2016 +0200
     6.2 +++ b/src/sync_fsm.c	Tue Oct 04 20:56:24 2016 +0200
     6.3 @@ -152,6 +152,13 @@
     6.4                      if (status != PEP_STATUS_OK)
     6.5                          return (int) invalid_action;
     6.6                      return HandshakingGrouped;
     6.7 +                case GroupKeys:
     6.8 +                    status = storeGroupKeys(session, state, partner, extra /*keys*/);
     6.9 +                    if (status == PEP_OUT_OF_MEMORY)
    6.10 +                        return (int) invalid_out_of_memory;
    6.11 +                    if (status != PEP_STATUS_OK)
    6.12 +                        return (int) invalid_action;
    6.13 +                    return Grouped;
    6.14                  default:
    6.15                      return (DeviceState_state) invalid_event;
    6.16              }
     7.1 --- a/src/sync_impl.c	Mon Oct 03 17:16:33 2016 +0200
     7.2 +++ b/src/sync_impl.c	Tue Oct 04 20:56:24 2016 +0200
     7.3 @@ -198,7 +198,7 @@
     7.4                      // HandshakeRequest needs encryption
     7.5                      case DeviceGroup_Protocol__payload_PR_handshakeRequest:
     7.6                          if (rating < PEP_rating_reliable ||
     7.7 -                            strncmp(sync_uuid,
     7.8 +                            strncmp(session->sync_uuid,
     7.9                                      (const char *)msg->payload.choice.handshakeRequest.partner.user_id->buf,
    7.10                                      msg->payload.choice.handshakeRequest.partner.user_id->size) != 0){
    7.11                              ASN_STRUCT_FREE(asn_DEF_DeviceGroup_Protocol, msg);
    7.12 @@ -209,7 +209,7 @@
    7.13                      // accepting GroupKeys needs encryption and trust
    7.14                      case DeviceGroup_Protocol__payload_PR_groupKeys:
    7.15                          if (!keylist || rating < PEP_rating_reliable ||
    7.16 -                            strncmp(sync_uuid,
    7.17 +                            strncmp(session->sync_uuid,
    7.18                                      (const char *)msg->payload.choice.groupKeys.partner.user_id->buf,
    7.19                                      msg->payload.choice.groupKeys.partner.user_id->size) != 0){
    7.20                              ASN_STRUCT_FREE(asn_DEF_DeviceGroup_Protocol, msg);
    7.21 @@ -352,7 +352,7 @@
    7.22      
    7.23      int32_t seq = 0;
    7.24  
    7.25 -    status = sequence_value(session, sync_uuid, &seq);
    7.26 +    status = sequence_value(session, session->sync_uuid, &seq);
    7.27      if (status != PEP_OWN_SEQUENCE && status != PEP_STATUS_OK)
    7.28          goto error;
    7.29  
    7.30 @@ -363,7 +363,7 @@
    7.31          goto enomem;
    7.32  
    7.33      free(_me->user_id);
    7.34 -    _me->user_id = strndup(sync_uuid, 36);
    7.35 +    _me->user_id = strndup(session->sync_uuid, 36);
    7.36      assert(_me->user_id);
    7.37      if (!_me->user_id)
    7.38          goto enomem;
    7.39 @@ -480,6 +480,7 @@
    7.40      if (status != PEP_STATUS_OK)
    7.41          return status;
    7.42  
    7.43 +    // FIXME: exclude previously rejected identities
    7.44      for (identity_list *_i = own_identities; _i && _i->ident; _i = _i->next) {
    7.45          pEp_identity *me = _i->ident;
    7.46  
     8.1 --- a/src/sync_send_actions.c	Mon Oct 03 17:16:33 2016 +0200
     8.2 +++ b/src/sync_send_actions.c	Tue Oct 04 20:56:24 2016 +0200
     8.3 @@ -135,13 +135,13 @@
     8.4      status = own_identities_retrieve(session, &kl);
     8.5      if (status != PEP_STATUS_OK)
     8.6          goto error;
     8.7 +    if (IdentityList_from_identity_list(kl, &msg->payload.choice.groupKeys.ownIdentities) == NULL)
     8.8 +        goto enomem;
     8.9 +
    8.10      if (Identity_from_Struct(partner,
    8.11                               &msg->payload.choice.groupKeys.partner) == NULL)
    8.12          goto enomem;
    8.13  
    8.14 -    if (IdentityList_from_identity_list(kl, &msg->payload.choice.groupKeys.ownIdentities) == NULL)
    8.15 -        goto enomem;
    8.16 -
    8.17      bool encrypted = true;
    8.18      status = unicast_msg(session, partner, state, msg, encrypted);
    8.19      if (status != PEP_STATUS_OK)
     9.1 --- a/sync/devicegroup.fsm	Mon Oct 03 17:16:33 2016 +0200
     9.2 +++ b/sync/devicegroup.fsm	Tue Oct 04 20:56:24 2016 +0200
     9.3 @@ -78,6 +78,11 @@
     9.4                  do sendHandshakeRequest(partner);
     9.5                  go HandshakingGrouped(partner);
     9.6              }
     9.7 +            on GroupKeys(Identity partner, Stringlist keys) {
     9.8 +                do storeGroupKeys(partner, keys);
     9.9 +                // TODO : add a callback to signal finished waiting for group keys
    9.10 +                go Grouped;
    9.11 +            }
    9.12          }
    9.13  
    9.14          state HandshakingGrouped(Identity partner) {
    10.1 --- a/sync/gen_actions_skeleton.ysl2	Mon Oct 03 17:16:33 2016 +0200
    10.2 +++ b/sync/gen_actions_skeleton.ysl2	Tue Oct 04 20:56:24 2016 +0200
    10.3 @@ -176,6 +176,14 @@
    10.4                  |>> goto enomem;
    10.5              }
    10.6          }
    10.7 +        choose {
    10.8 +            when "$name='GroupKeys' or $name='HandshakeRequest'" {
    10.9 +                |
   10.10 +                |> if (Identity_from_Struct(partner,
   10.11 +                |>                          &msg->payload.choice.«$lname».partner) == NULL)
   10.12 +                |>> goto enomem;
   10.13 +            }
   10.14 +        }
   10.15          ||
   10.16  
   10.17          ||
    11.1 --- a/test/sequence_test.cc	Mon Oct 03 17:16:33 2016 +0200
    11.2 +++ b/test/sequence_test.cc	Tue Oct 04 20:56:24 2016 +0200
    11.3 @@ -33,7 +33,7 @@
    11.4  
    11.5      cout << "test sequence: " << value2 << "\n";
    11.6      assert(value2 == value1 + 1);
    11.7 -    free(name1);
    11.8 +//    free(name1);
    11.9  
   11.10      cout << "testing sequence violation\n";
   11.11      int32_t value3 = value2 - 1;