Removed all 2-party keysync components from engine.
authorKrista Bennett <krista@pep-project.org>
Thu, 02 Nov 2017 17:30:01 +0100
changeset 226573c0738de68c
parent 2264 3be5262e5e4a
child 2266 af92716a2978
child 2271 25bfd109320c
Removed all 2-party keysync components from engine.
Makefile
src/Makefile
src/keymanagement.c
src/pEpEngine.c
src/pEp_internal.h
src/sync.c
src/sync.h
src/sync_actions.c
src/sync_app.h
src/sync_impl.c
src/sync_impl.h
sync/Makefile
sync/devicegroup.fsm
sync/fsm.yml2
sync/functions.ysl2
sync/gen_actions.ysl2
sync/gen_dot.ysl2
sync/gen_statemachine.ysl2
sync/generated/README
sync/generated/sync_driver.c
sync/generated/sync_fsm.c
sync/generated/sync_fsm.h
sync/generated/sync_send_actions.c
sync/skeletons/sync_actions.c
     1.1 --- a/Makefile	Thu Nov 02 17:02:05 2017 +0100
     1.2 +++ b/Makefile	Thu Nov 02 17:30:01 2017 +0100
     1.3 @@ -23,7 +23,6 @@
     1.4  all:
     1.5  	$(MAKE) -C asn.1 generate
     1.6  	$(MAKE) -C asn.1
     1.7 -	$(MAKE) -C sync
     1.8  	$(MAKE) -C src all
     1.9  
    1.10  .PHONY: install
    1.11 @@ -41,7 +40,6 @@
    1.12  	$(MAKE) -C src clean
    1.13  	$(MAKE) -C test clean
    1.14  	$(MAKE) -C db clean
    1.15 -	$(MAKE) -C sync clean
    1.16  	$(MAKE) -C asn.1 clean
    1.17  	rm -rf test_home
    1.18  
     2.1 --- a/src/Makefile	Thu Nov 02 17:02:05 2017 +0100
     2.2 +++ b/src/Makefile	Thu Nov 02 17:30:01 2017 +0100
     2.3 @@ -86,7 +86,7 @@
     2.4  	mkdir -p "$(PREFIX)/lib/"
     2.5  	cp $< $(PREFIX)/lib/
     2.6  	mkdir -p $(PREFIX)/include/pEp
     2.7 -	cp pEpEngine.h keymanagement.h message_api.h dynamic_api.h stringlist.h timestamp.h identity_list.h bloblist.h stringpair.h message.h mime.h cryptotech.h sync.h sync_fsm.h sync_app.h blacklist.h openpgp_compat.h $(PREFIX)/include/pEp/
     2.8 +	cp pEpEngine.h keymanagement.h message_api.h dynamic_api.h stringlist.h timestamp.h identity_list.h bloblist.h stringpair.h message.h mime.h cryptotech.h blacklist.h openpgp_compat.h $(PREFIX)/include/pEp/
     2.9  
    2.10  .PHONY: uninstall
    2.11  uninstall:
     3.1 --- a/src/keymanagement.c	Thu Nov 02 17:02:05 2017 +0100
     3.2 +++ b/src/keymanagement.c	Thu Nov 02 17:30:01 2017 +0100
     3.3 @@ -12,7 +12,6 @@
     3.4  #include "pEp_internal.h"
     3.5  #include "keymanagement.h"
     3.6  
     3.7 -#include "sync_fsm.h"
     3.8  #include "blacklist.h"
     3.9  
    3.10  #ifndef EMPTYSTR
    3.11 @@ -555,8 +554,6 @@
    3.12          }
    3.13      }
    3.14     
    3.15 -    bool new_key_generated = false;
    3.16 -
    3.17      if (EMPTYSTR(identity->fpr) || revoked)
    3.18      {
    3.19          if(!do_keygen){
    3.20 @@ -581,7 +578,6 @@
    3.21              return ADD_TO_LOG(status);
    3.22          }
    3.23  
    3.24 -        new_key_generated = true;
    3.25          
    3.26          if(revoked)
    3.27          {
    3.28 @@ -621,15 +617,6 @@
    3.29          return status;
    3.30      }
    3.31  
    3.32 -    if(new_key_generated)
    3.33 -    {
    3.34 -        // if a state machine for keysync is in place, inject notify
    3.35 -        status = inject_DeviceState_event(session, KeyGen, NULL, NULL);
    3.36 -        if (status == PEP_OUT_OF_MEMORY){
    3.37 -            return PEP_OUT_OF_MEMORY;
    3.38 -        }
    3.39 -    }
    3.40 -
    3.41      return ADD_TO_LOG(PEP_STATUS_OK);
    3.42  }
    3.43  
     4.1 --- a/src/pEpEngine.c	Thu Nov 02 17:02:05 2017 +0100
     4.2 +++ b/src/pEpEngine.c	Thu Nov 02 17:30:01 2017 +0100
     4.3 @@ -6,7 +6,6 @@
     4.4  #include "cryptotech.h"
     4.5  #include "transport.h"
     4.6  #include "blacklist.h"
     4.7 -#include "sync_fsm.h"
     4.8  
     4.9  #include <time.h>
    4.10  #include <stdlib.h>
    4.11 @@ -826,8 +825,6 @@
    4.12          out_last = true;
    4.13  
    4.14      if (session) {
    4.15 -        if (session->sync_state != DeviceState_state_NONE)
    4.16 -            unregister_sync_callbacks(session);
    4.17  
    4.18          if (session->db) {
    4.19              if (session->log)
     5.1 --- a/src/pEp_internal.h	Thu Nov 02 17:02:05 2017 +0100
     5.2 +++ b/src/pEp_internal.h	Thu Nov 02 17:30:01 2017 +0100
     5.3 @@ -101,7 +101,6 @@
     5.4  #include "keymanagement.h"
     5.5  #include "cryptotech.h"
     5.6  #include "transport.h"
     5.7 -#include "sync.h"
     5.8  
     5.9  #define NOT_IMPLEMENTED assert(0); return PEP_UNKNOWN_ERROR;
    5.10  
    5.11 @@ -168,14 +167,9 @@
    5.12      void *examine_management;
    5.13      void *sync_management;
    5.14      void *sync_obj;
    5.15 -    messageToSend_t messageToSend;
    5.16 -    notifyHandshake_t notifyHandshake;
    5.17 -    inject_sync_msg_t inject_sync_msg;
    5.18 -    retrieve_next_sync_msg_t retrieve_next_sync_msg;
    5.19  
    5.20      // key sync
    5.21      pEpSession* sync_session;
    5.22 -    DeviceState_state sync_state;
    5.23      void* sync_state_payload;
    5.24      char sync_uuid[37];
    5.25      time_t LastCannotDecrypt;
     6.1 --- a/src/sync.c	Thu Nov 02 17:02:05 2017 +0100
     6.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     6.3 @@ -1,288 +0,0 @@
     6.4 -// This file is under GNU General Public License 3.0
     6.5 -// see LICENSE.txt
     6.6 -
     6.7 -#include "pEp_internal.h"
     6.8 -
     6.9 -#include <memory.h>
    6.10 -#include <assert.h>
    6.11 -
    6.12 -#include "asn1_helper.h"
    6.13 -#include "../asn.1/DeviceGroup-Protocol.h"
    6.14 -
    6.15 -// receive_sync_msg is defined in the sync_impl
    6.16 -
    6.17 -PEP_STATUS receive_sync_msg(
    6.18 -        PEP_SESSION session,
    6.19 -        sync_msg_t *sync_msg,
    6.20 -        time_t *timeout
    6.21 -    );
    6.22 -
    6.23 -DYNAMIC_API PEP_STATUS register_sync_callbacks(
    6.24 -        PEP_SESSION session,
    6.25 -        void *management,
    6.26 -        messageToSend_t messageToSend,
    6.27 -        notifyHandshake_t notifyHandshake,
    6.28 -        inject_sync_msg_t inject_sync_msg,
    6.29 -        retrieve_next_sync_msg_t retrieve_next_sync_msg
    6.30 -    )
    6.31 -{
    6.32 -    assert(session && management && messageToSend && notifyHandshake && inject_sync_msg && retrieve_next_sync_msg);
    6.33 -    if (!(session && management && messageToSend && notifyHandshake && inject_sync_msg && retrieve_next_sync_msg))
    6.34 -        return PEP_ILLEGAL_VALUE;
    6.35 -
    6.36 -    pEpUUID uuid;
    6.37 -    uuid_generate_random(uuid);
    6.38 -    uuid_unparse_upper(uuid, session->sync_uuid);
    6.39 -
    6.40 -    session->sync_management = management;
    6.41 -    session->messageToSend = messageToSend;
    6.42 -    session->notifyHandshake = notifyHandshake;
    6.43 -    session->inject_sync_msg = inject_sync_msg;
    6.44 -    session->retrieve_next_sync_msg = retrieve_next_sync_msg;
    6.45 -
    6.46 -    // start state machine
    6.47 -    session->sync_state = InitState;
    6.48 -    time_t unused = 0;
    6.49 -    PEP_STATUS status = fsm_DeviceState_inject(session, Init, NULL, NULL, &unused);
    6.50 -    if (status != PEP_STATUS_OK)
    6.51 -        unregister_sync_callbacks(session);
    6.52 -
    6.53 -    return status;
    6.54 -}
    6.55 -
    6.56 -DYNAMIC_API PEP_STATUS attach_sync_session(
    6.57 -        PEP_SESSION session,
    6.58 -        PEP_SESSION sync_session
    6.59 -    )
    6.60 -{
    6.61 -    assert(session && sync_session && sync_session->sync_management && sync_session->inject_sync_msg );
    6.62 -    if (!(session && sync_session && sync_session->sync_management && sync_session->inject_sync_msg ))
    6.63 -        return PEP_ILLEGAL_VALUE;
    6.64 -
    6.65 -    session->sync_session = sync_session;
    6.66 -    // memcpy(session->sync_uuid, sync_session->sync_uuid, 37);
    6.67 -
    6.68 -    // session->sync_management = sync_session->sync_management;
    6.69 -    // session->inject_sync_msg = sync_session->inject_sync_msg;
    6.70 -
    6.71 -    return PEP_STATUS_OK;
    6.72 -}
    6.73 -
    6.74 -DYNAMIC_API PEP_STATUS detach_sync_session(PEP_SESSION session)
    6.75 -{
    6.76 -    assert(session);
    6.77 -    if (!(session))
    6.78 -        return PEP_ILLEGAL_VALUE;
    6.79 -
    6.80 -    session->sync_session = session;
    6.81 -    // memset(session->sync_uuid, 0, 37);
    6.82 -
    6.83 -    // session->sync_management = NULL;
    6.84 -    // session->inject_sync_msg = NULL;
    6.85 -
    6.86 -    return PEP_STATUS_OK;
    6.87 -}
    6.88 -
    6.89 -int call_inject_sync_msg(PEP_SESSION session, void *msg)
    6.90 -{
    6.91 -    if(session->sync_session->inject_sync_msg &&
    6.92 -       session->sync_session->sync_management)
    6.93 -        return session->sync_session->inject_sync_msg(msg, 
    6.94 -            session->sync_session->sync_management);
    6.95 -    else
    6.96 -       return PEP_SYNC_NO_INJECT_CALLBACK;
    6.97 -}
    6.98 -
    6.99 -DYNAMIC_API void unregister_sync_callbacks(PEP_SESSION session) {
   6.100 -    // stop state machine
   6.101 -    session->sync_state = DeviceState_state_NONE;
   6.102 -
   6.103 -    // unregister
   6.104 -    session->sync_management = NULL;
   6.105 -    session->messageToSend = NULL;
   6.106 -    session->notifyHandshake = NULL;
   6.107 -    session->inject_sync_msg = NULL;
   6.108 -    session->retrieve_next_sync_msg = NULL;
   6.109 -}
   6.110 -
   6.111 -DYNAMIC_API PEP_STATUS deliverHandshakeResult(
   6.112 -        PEP_SESSION session,
   6.113 -        Identity partner,
   6.114 -        sync_handshake_result result
   6.115 -    )
   6.116 -{
   6.117 -    assert(session);
   6.118 -    if (!session)
   6.119 -        return PEP_ILLEGAL_VALUE;
   6.120 -
   6.121 -    PEP_STATUS status = PEP_STATUS_OK;
   6.122 -
   6.123 -    DeviceState_event event;
   6.124 -    bool need_partner = false;
   6.125 -
   6.126 -    switch (result) {
   6.127 -        case SYNC_HANDSHAKE_CANCEL:
   6.128 -            event = Cancel;
   6.129 -            break;
   6.130 -        case SYNC_HANDSHAKE_ACCEPTED:
   6.131 -        {
   6.132 -            event = HandshakeAccepted;
   6.133 -            need_partner = true;
   6.134 -            break;
   6.135 -        }
   6.136 -        case SYNC_HANDSHAKE_REJECTED:
   6.137 -        {
   6.138 -            event = HandshakeRejected;
   6.139 -            need_partner = true;
   6.140 -            break;
   6.141 -        }
   6.142 -        default:
   6.143 -            return PEP_ILLEGAL_VALUE;
   6.144 -    }
   6.145 -
   6.146 -    pEp_identity *_partner = NULL;
   6.147 -    if(need_partner){
   6.148 -        _partner = identity_dup(partner);
   6.149 -        if (_partner == NULL)
   6.150 -            return PEP_OUT_OF_MEMORY;
   6.151 -    }
   6.152 -    status = inject_DeviceState_event(session, event, _partner, NULL);
   6.153 -
   6.154 -    return status;
   6.155 -}
   6.156 -
   6.157 -DYNAMIC_API PEP_STATUS do_sync_protocol(
   6.158 -        PEP_SESSION session,
   6.159 -        void *obj
   6.160 -    )
   6.161 -{
   6.162 -    sync_msg_t *msg = NULL;
   6.163 -    PEP_STATUS status = PEP_STATUS_OK;
   6.164 -    time_t timeout = 0;
   6.165 -
   6.166 -    assert(session && session->retrieve_next_sync_msg);
   6.167 -    assert(obj);
   6.168 -
   6.169 -    if (!(session && session->retrieve_next_sync_msg) || !obj)
   6.170 -        return PEP_ILLEGAL_VALUE;
   6.171 -
   6.172 -    log_event(session, "sync_protocol thread started", "pEp sync protocol", NULL, NULL);
   6.173 -
   6.174 -    session->sync_obj = obj;
   6.175 -
   6.176 -    while (true) 
   6.177 -    {
   6.178 -        msg = (sync_msg_t *) session->retrieve_next_sync_msg(session->sync_management, &timeout);
   6.179 -        if(msg == NULL && timeout == 0)
   6.180 -            break;
   6.181 -        else if(msg == NULL && timeout != 0){
   6.182 -            status = fsm_DeviceState_inject(session, Timeout, NULL, NULL, &timeout);
   6.183 -#ifndef NDEBUG
   6.184 -            char buffer[MAX_LINELENGTH];
   6.185 -            memset(buffer, 0, MAX_LINELENGTH);
   6.186 -            snprintf(buffer, MAX_LINELENGTH, "problem with timeout event : %d\n", (int) status);
   6.187 -            log_event(session, buffer, "pEp sync protocol", NULL, NULL);
   6.188 -            continue;
   6.189 -#endif
   6.190 -        }
   6.191 -        else {
   6.192 -            status = receive_sync_msg(session, msg, &timeout);
   6.193 -            if (status != PEP_STATUS_OK && status != PEP_MESSAGE_IGNORE) {
   6.194 -#ifndef NDEBUG
   6.195 -                char buffer[MAX_LINELENGTH];
   6.196 -                memset(buffer, 0, MAX_LINELENGTH);
   6.197 -                snprintf(buffer, MAX_LINELENGTH, "problem with msg received: %d\n", (int) status);
   6.198 -                log_event(session, buffer, "pEp sync protocol", NULL, NULL);
   6.199 -#endif
   6.200 -            }
   6.201 -        }
   6.202 -    }
   6.203 -
   6.204 -    log_event(session, "sync_protocol thread shutdown", "pEp sync protocol", NULL, NULL);
   6.205 -
   6.206 -    session->sync_obj = NULL;
   6.207 -
   6.208 -    return PEP_STATUS_OK;
   6.209 -}
   6.210 -
   6.211 -DYNAMIC_API PEP_STATUS decode_sync_msg(
   6.212 -        const char *data,
   6.213 -        size_t size,
   6.214 -        char **text
   6.215 -    )
   6.216 -{
   6.217 -    PEP_STATUS status = PEP_STATUS_OK;
   6.218 -
   6.219 -    assert(data && text);
   6.220 -    if (!(data && text))
   6.221 -        return PEP_ILLEGAL_VALUE;
   6.222 -
   6.223 -    *text = NULL;
   6.224 -
   6.225 -    DeviceGroup_Protocol_t *msg = NULL;
   6.226 -    uper_decode_complete(NULL, &asn_DEF_DeviceGroup_Protocol, (void **) &msg,
   6.227 -            data, size);
   6.228 -    if (!msg)
   6.229 -        return PEP_SYNC_ILLEGAL_MESSAGE;
   6.230 -
   6.231 -    growing_buf_t *dst = new_growing_buf();
   6.232 -    if (!dst) {
   6.233 -        status = PEP_OUT_OF_MEMORY;
   6.234 -        goto the_end;
   6.235 -    }
   6.236 -
   6.237 -    asn_enc_rval_t er = xer_encode(&asn_DEF_DeviceGroup_Protocol, msg,
   6.238 -            XER_F_BASIC, (asn_app_consume_bytes_f *) consume_bytes, (void *) dst);
   6.239 -    if (er.encoded == -1) {
   6.240 -        status = PEP_CANNOT_ENCODE;
   6.241 -        goto the_end;
   6.242 -    }
   6.243 -
   6.244 -    *text = dst->data;
   6.245 -    dst->data = NULL;
   6.246 -
   6.247 -the_end:
   6.248 -    free_growing_buf(dst);
   6.249 -    ASN_STRUCT_FREE(asn_DEF_DeviceGroup_Protocol, msg);
   6.250 -    return status;
   6.251 -}
   6.252 -
   6.253 -DYNAMIC_API PEP_STATUS encode_sync_msg(
   6.254 -        const char *text,
   6.255 -        char **data,
   6.256 -        size_t *size
   6.257 -    )
   6.258 -{
   6.259 -    PEP_STATUS status = PEP_STATUS_OK;
   6.260 -
   6.261 -    assert(text && data && size);
   6.262 -    if (!(text && data && size))
   6.263 -        return PEP_ILLEGAL_VALUE;
   6.264 -
   6.265 -    *data = NULL;
   6.266 -    *size = 0;
   6.267 -
   6.268 -    DeviceGroup_Protocol_t *msg = NULL;
   6.269 -    asn_dec_rval_t dr = xer_decode(NULL, &asn_DEF_DeviceGroup_Protocol,
   6.270 -            (void **) &msg, (const void *) text, strlen(text));
   6.271 -    if (dr.code != RC_OK) {
   6.272 -        status = PEP_SYNC_ILLEGAL_MESSAGE;
   6.273 -        goto the_end;
   6.274 -    }
   6.275 -
   6.276 -    char *payload = NULL;
   6.277 -    ssize_t _size = uper_encode_to_new_buffer(&asn_DEF_DeviceGroup_Protocol,
   6.278 -            NULL, msg, (void **) &payload);
   6.279 -    if (_size == -1) {
   6.280 -        status = PEP_CANNOT_ENCODE;
   6.281 -        goto the_end;
   6.282 -    }
   6.283 -
   6.284 -    *data = payload;
   6.285 -    *size = (size_t) _size;
   6.286 -
   6.287 -the_end:
   6.288 -    ASN_STRUCT_FREE(asn_DEF_DeviceGroup_Protocol, msg);
   6.289 -    return status;
   6.290 -}
   6.291 -
     7.1 --- a/src/sync.h	Thu Nov 02 17:02:05 2017 +0100
     7.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     7.3 @@ -1,417 +0,0 @@
     7.4 -// This file is under GNU General Public License 3.0
     7.5 -// see LICENSE.txt
     7.6 -
     7.7 -/*
     7.8 -====================================
     7.9 -Engine/adapter/app KeySync interface 
    7.10 -====================================
    7.11 -
    7.12 -
    7.13 -         Engine         |          Adapter            |          App
    7.14 -                        |                             |
    7.15 - . . . . . . . . . . . .|. . . . . . . . . . . . . . .|. . Attached session .  
    7.16 -     ,---------,        |                             |
    7.17 -   ,-| decrypt |<--------------------------------------- Incomming message 
    7.18 -   | | message |        |                             |
    7.19 -   | '---------'        |                             |
    7.20 -   | ,----------,       |                             |
    7.21 -   |-| myself   |<-------------------------------------- Create new account
    7.22 -   | | (keygen) |       |                             |
    7.23 -   | '----------'       |                             |
    7.24 -   | ,-----------,      |                             |
    7.25 -   |-| deliver   |<------------------------------------------- Accept/reject
    7.26 -   | | handshake |      |                     KeySync |            handshake
    7.27 -   | | result    |      |                     Message |
    7.28 -   | '-----------'      |                      Queue  |
    7.29 -   |                    |                      ,---,  |
    7.30 -   '-----------------------inject_sync_msg---->|   |  |
    7.31 - . . . . . . . . . . . .|. . . . . . . . . . . |---| .|. . . . Sync session .  
    7.32 - *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *|   |* | 
    7.33 -                        |                      |---|  | 
    7.34 - *   ,------------------retrieve_next_sync_msg-|   |* | 
    7.35 -   ,-v--------,         |                      '---'  | 
    7.36 - * | Driver   |         |                           * |
    7.37 -   '----------'         |                             |
    7.38 - *  ||'-event-----,     |                           * |
    7.39 -    |'--partner--,|     |                             |
    7.40 - *  '---extra---,||     |           SYNC THREAD     *<-------------- Start Sync
    7.41 -            ,---vvv---, |                             |
    7.42 - *     ,----|   FSM   | |                           * |
    7.43 -       |    '---------' |                             |
    7.44 - *     |  ,-------,     |                           * |
    7.45 -       '->|actions|---------messageToSend-------------------> Send mail to self
    7.46 - *        '-------'     |                           * |
    7.47 -              '-------------notifyHandshake-----------------> Ask for handshake
    7.48 - *                      |                           * |    display group status
    7.49 -                        |                             |
    7.50 - *  *  *  *  *  *  *  * |*  *  *  *  *  *  *  *  *  * |
    7.51 -                        |                             |
    7.52 -
    7.53 -Emails to self
    7.54 ---------------
    7.55 -
    7.56 -With e-mail as a transport KeySync message handling is done when an incoming 
    7.57 -email to self is passed to decrypt_message(), assuming that all incoming email
    7.58 -messages are passed to decrypt_massage(). 
    7.59 -
    7.60 -In case of an email containing a KeySync paload, KeySync may consume or ignore
    7.61 -the message. decrypt_message() signals this to the app with decrypt flags
    7.62 -PEP_decrypt_flag_consume and PEP_decrypt_flag_ignore.
    7.63 -
    7.64 -In case of PEP_decrypt_flag_consume, app should delete the message.
    7.65 -In case of PEP_decrypt_flag_ignore, app should ignore message.
    7.66 -In both cases, message should be hidden.
    7.67 -
    7.68 -States, events, actions
    7.69 ------------------------
    7.70 -
    7.71 -In the engine, KeySync is implemented through a finite state machine (FSM) [1].
    7.72 -KeySync state machine is driven [2] by events, triggering actions [3] and
    7.73 -transitions to new states.
    7.74 -
    7.75 -Events happen on :
    7.76 -
    7.77 - - decryption of email messages
    7.78 -
    7.79 - - key generation
    7.80 -
    7.81 - - user interaction through the app 
    7.82 -
    7.83 - - timeout when staying too long in some particular states.
    7.84 -
    7.85 -[1] sync/devicegroup.fsm , src/sync_fsm.c (generated)
    7.86 -[2] src/sync_driver.c (generated)
    7.87 -[3] src/sync_actions.c , src/sync_send_actions.c (generated)
    7.88 -
    7.89 -Sync session, attached sessions
    7.90 --------------------------------
    7.91 -
    7.92 -To use KeySync, the adapter has to create a session dedicated to handle the
    7.93 -protocol, register some callbacks [4] to the engine, and then call protocol's
    7.94 -event consumer loop [5] in a dedicated thread. KeySync actions are executed as
    7.95 -callback invoked from that thread.
    7.96 -
    7.97 -When a session is attached [6] to a KeySync session, decryption of pEp email
    7.98 -messages in the attached session may trigger operations in KeySync session. In
    7.99 -case of an adapter capable to serve multiple apps, each app is associated to a
   7.100 -different KeySync session, and sessions created for use in that app are
   7.101 -attached to that session.
   7.102 -
   7.103 -Adapters present different approaches regarding session and client abstraction,
   7.104 -and may not propose to explicitely create or attach session or sync session.
   7.105 -
   7.106 -[4] register_sync_callbacks()
   7.107 -[5] do_sync_protocol()
   7.108 -[6] attach_sync_session()
   7.109 -
   7.110 -KeySync Messages and queue
   7.111 ---------------------------
   7.112 -
   7.113 -KeySync messages [7], not to be confused with pEp (email) messages, are either
   7.114 -directly events to be processed by the state machine, or KeySync payloads
   7.115 -collected from decrypted messages. 
   7.116 -
   7.117 -KeySync messages can be emitted by different sessions, and could naturally come
   7.118 -from different threads. They must be serialized in a locked queue. 
   7.119 -KeySync messages queue is implemented by adapter, along with thread handling
   7.120 -KeySync protocol. 
   7.121 -
   7.122 -Attached sessions inject [8] KeySync messages in the queue. Protocol loop
   7.123 -retrieves [9] them from the queue. KeySync message is received [10] by the
   7.124 -state machine, where event, partner and extra parameters are eventually deduced
   7.125 -from payload.
   7.126 -
   7.127 -A state timeout event is a particular case. It doesn't traverse the queue, and
   7.128 -isn't emitted by a session. It is triggered by a timeout on the retrieve
   7.129 -operation. Value of the timeout is determined when entering a new state, and is
   7.130 -passed as a parameter of the call to the blocking queue retrieve operation on 
   7.131 -next protocol loop iteraton.
   7.132 -
   7.133 -[7] type sync_msg_t
   7.134 -[8] callback inject_sync_msg
   7.135 -[9] callback retrieve_next_sync_msg
   7.136 -[10] receive_sync_msg() (src/sync_impl.c)
   7.137 -
   7.138 -Application callbacks
   7.139 ----------------------
   7.140 -
   7.141 -Some Keysync actions require the application to act, through callbacks :
   7.142 -
   7.143 - - messageToSend : send pEp messages through app's transport. 
   7.144 -   Messages are already encrypted and just need to be passed as-is to
   7.145 -   transport for transmission, as for messages returned by encrypt_message().
   7.146 -
   7.147 - - notifyHandshake : display KeySync status and handshake to the user.
   7.148 -   notifyHandshake callback receives 2 identities, 'me' and 'partner', together
   7.149 -   with a sync_handshake_signal enum :
   7.150 -
   7.151 -    SYNC_NOTIFY_INIT_ADD_OUR_DEVICE :
   7.152 -        Device (me) is sole, about to enter a group (partner).
   7.153 -        App displays trustwords, and requests user accept or reject
   7.154 -        App calls deliverHandshakeResult with user's answer
   7.155 -
   7.156 -    SYNC_NOTIFY_INIT_ADD_OTHER_DEVICE :
   7.157 -        Device (me) is grouped, another device (partner) wants to join group.
   7.158 -        App displays trustwords, and requests user accept or reject
   7.159 -        App calls deliverHandshakeResult with user's answer
   7.160 -
   7.161 -    SYNC_NOTIFY_INIT_FORM_GROUP :
   7.162 -        Device (me) is forming a group, including another device (partner)
   7.163 -        App displays trustwords, and requests user accept or reject
   7.164 -        App calls deliverHandshakeResult with user's answer
   7.165 -
   7.166 -    SYNC_NOTIFY_INIT_MOVE_OUR_DEVICE
   7.167 -        Device (me) is grouped and will leave current group to join another
   7.168 -        device's (partner) group.
   7.169 -        App displays trustwords, and requests user accept or reject
   7.170 -        App calls deliverHandshakeResult with user's answer
   7.171 -
   7.172 -
   7.173 -    SYNC_NOTIFY_TIMEOUT :
   7.174 -        KeySync operation timed out.
   7.175 -        Identities are set reflecting peers involved in aborted operation.
   7.176 -        App displays error message. No feedback to engine.
   7.177 -
   7.178 -    SYNC_NOTIFY_ACCEPTED_DEVICE_ADDED,
   7.179 -        New device was added to group.
   7.180 -        App displays message. No feedback to engine.
   7.181 -
   7.182 -    SYNC_NOTIFY_ACCEPTED_GROUP_CREATED
   7.183 -        New group created.
   7.184 -        App displays message. No feedback to engine.
   7.185 -
   7.186 -    SYNC_NOTIFY_ACCEPTED_DEVICE_MOVED
   7.187 -        New device was moved from one group to another.
   7.188 -        App displays message. No feedback to engine.
   7.189 -
   7.190 -   To deliver handshake result back to engine once user reacted,
   7.191 -   deliver_handshake_result is used. Result can be :
   7.192 -
   7.193 -    SYNC_HANDSHAKE_CANCEL
   7.194 -        Gives no answer. User doesn't know id TrustWord are good or bad.
   7.195 -        For example in case peering device is away.
   7.196 -        Handshake will re-appear later.
   7.197 -
   7.198 -    SYNC_HANDSHAKE_ACCEPTED
   7.199 -        Trustwords match with other device and user accepts handshake.
   7.200 -
   7.201 -    SYNC_HANDSHAKE_REJECTED
   7.202 -        Trustwords do not match with any device and user rejects handshake.
   7.203 -*/
   7.204 -
   7.205 -#pragma once
   7.206 -
   7.207 -#include "message.h"
   7.208 -#include "sync_fsm.h"
   7.209 -#include "sync_app.h"
   7.210 -
   7.211 -// this module is for being used WITHOUT the Transport API in transport.h
   7.212 -// DO NOT USE IT WHEN USING Transport API!
   7.213 -
   7.214 -
   7.215 -#ifdef __cplusplus
   7.216 -extern "C" {
   7.217 -#endif
   7.218 -
   7.219 -// messageToSend() - send a message
   7.220 -//
   7.221 -//  parameters:
   7.222 -//      obj (in)        object handle (implementation defined)
   7.223 -//      msg (in)        message struct with message to send
   7.224 -//
   7.225 -//  return value:
   7.226 -//      PEP_STATUS_OK or any other value on error
   7.227 -//
   7.228 -//  caveat:
   7.229 -//      the ownership of msg goes to the callee
   7.230 -
   7.231 -typedef PEP_STATUS (*messageToSend_t)(void *obj, message *msg);
   7.232 -
   7.233 -// notifyHandshake() - notify UI about sync handshaking process
   7.234 -//
   7.235 -//  parameters:
   7.236 -//      obj (in)        object handle (implementation defined)
   7.237 -//      me (in)         own identity
   7.238 -//      partner (in)    identity of partner
   7.239 -//      signal (in)     reason of the notification
   7.240 -//
   7.241 -//  return value:
   7.242 -//      PEP_STATUS_OK or any other value on error
   7.243 -//
   7.244 -//  caveat:
   7.245 -//      ownership of self and partner go to the callee
   7.246 -
   7.247 -typedef PEP_STATUS (*notifyHandshake_t)(
   7.248 -        void *obj,
   7.249 -        pEp_identity *me,
   7.250 -        pEp_identity *partner,
   7.251 -        sync_handshake_signal signal
   7.252 -    );
   7.253 -
   7.254 -typedef enum _sync_handshake_result {
   7.255 -    SYNC_HANDSHAKE_CANCEL = -1,
   7.256 -    SYNC_HANDSHAKE_ACCEPTED = 0,
   7.257 -    SYNC_HANDSHAKE_REJECTED = 1
   7.258 -} sync_handshake_result;
   7.259 -
   7.260 -// deliverHandshakeResult() - give the result of the handshake dialog
   7.261 -//
   7.262 -//  parameters:
   7.263 -//      session (in)        session handle
   7.264 -//      result (in)         handshake result
   7.265 -
   7.266 -DYNAMIC_API PEP_STATUS deliverHandshakeResult(
   7.267 -        PEP_SESSION session,
   7.268 -        Identity partner,
   7.269 -        sync_handshake_result result
   7.270 -    );
   7.271 -
   7.272 -// sync_msg_t - items queued for serialized handling by protocol engine
   7.273 -typedef struct _sync_msg_t sync_msg_t;
   7.274 -
   7.275 -// inject_sync_msg - inject sync protocol message
   7.276 -//
   7.277 -//  parameters:
   7.278 -//      msg (in)            message to inject
   7.279 -//      management (in)     application defined
   7.280 -//
   7.281 -//  *** BEWARE: msg is 1st parameter, obj is second!!! ***
   7.282 -//  return value:
   7.283 -//      0 if msg could be stored successfully or nonzero otherwise
   7.284 -
   7.285 -typedef int (*inject_sync_msg_t)(void *msg, void *management);
   7.286 -
   7.287 -
   7.288 -// retrieve_next_sync_msg - receive next sync message
   7.289 -//
   7.290 -//  parameters:
   7.291 -//      management (in)     application defined
   7.292 -//      timeout (in,out)    do not wait longer than timeout for message
   7.293 -//                          timeout == NULL or *timeout == 0 is blocking
   7.294 -//
   7.295 -//  return value:
   7.296 -//      next message, then timeout[out] == remaining time
   7.297 -//      NULL and timeout[out] != 0 for timeout occurence
   7.298 -//      NULL and timeout[out] == 0 for termination
   7.299 -
   7.300 -typedef void *(*retrieve_next_sync_msg_t)(void *management, time_t *timeout);
   7.301 -
   7.302 -
   7.303 -// register_sync_callbacks() - register adapter's callbacks
   7.304 -//
   7.305 -//  parameters:
   7.306 -//      session (in)                session where to store obj handle
   7.307 -//      management (in)             application defined
   7.308 -//      messageToSend (in)          callback for sending message
   7.309 -//      notifyHandshake (in)        callback for doing the handshake
   7.310 -//      retrieve_next_sync_msg (in) callback for receiving sync messages
   7.311 -//
   7.312 -//  return value:
   7.313 -//      PEP_STATUS_OK or any other value on errror
   7.314 -//
   7.315 -//  caveat:
   7.316 -//      call that BEFORE you're using any other part of the engine
   7.317 -
   7.318 -DYNAMIC_API PEP_STATUS register_sync_callbacks(
   7.319 -        PEP_SESSION session,
   7.320 -        void *management,
   7.321 -        messageToSend_t messageToSend,
   7.322 -        notifyHandshake_t notifyHandshake,
   7.323 -        inject_sync_msg_t inject_sync_msg,
   7.324 -        retrieve_next_sync_msg_t retrieve_next_sync_msg
   7.325 -    );
   7.326 -
   7.327 -// attach_sync_session() - attach session to a session running keysync state machine 
   7.328 -//
   7.329 -//  parameters:
   7.330 -//      session (in)                session to attach
   7.331 -//      sync_session (in)           session running keysync
   7.332 -//
   7.333 -//  return value:
   7.334 -//      PEP_STATUS_OK or any other value on errror
   7.335 -//
   7.336 -//  caveat:
   7.337 -//      register_sync_callbacks must have been called on sync_session
   7.338 -//      call that BEFORE you're using that session in any other part of the engine
   7.339 -
   7.340 -DYNAMIC_API PEP_STATUS attach_sync_session(
   7.341 -        PEP_SESSION session,
   7.342 -        PEP_SESSION sync_session
   7.343 -    );
   7.344 -
   7.345 -// detach_sync_session() - detach previously attached sync session
   7.346 -//
   7.347 -//  parameters:
   7.348 -//      session (in)                session to detach 
   7.349 -
   7.350 -DYNAMIC_API PEP_STATUS detach_sync_session(PEP_SESSION session);
   7.351 -
   7.352 -// unregister_sync_callbacks() - unregister adapter's callbacks
   7.353 -//
   7.354 -//  parameters:
   7.355 -//      session (in)                session to unregister
   7.356 -
   7.357 -DYNAMIC_API void unregister_sync_callbacks(PEP_SESSION session);
   7.358 -
   7.359 -// do_sync_protocol() - function to be run on an extra thread
   7.360 -//
   7.361 -//  parameters:
   7.362 -//      session                 pEp session to use
   7.363 -//      retrieve_next_sync_msg  pointer to retrieve_next_identity() callback
   7.364 -//                              which returns at least a valid address field in
   7.365 -//                              the identity struct
   7.366 -//      obj                     application defined sync object
   7.367 -//
   7.368 -//  return value:
   7.369 -//      PEP_STATUS_OK if thread has to terminate successfully or any other
   7.370 -//      value on failure
   7.371 -//
   7.372 -//  caveat:
   7.373 -//      to ensure proper working of this library, a thread has to be started
   7.374 -//      with this function immediately after initialization
   7.375 -
   7.376 -DYNAMIC_API PEP_STATUS do_sync_protocol(
   7.377 -        PEP_SESSION session,
   7.378 -        void *obj
   7.379 -    );
   7.380 -
   7.381 -// free_sync_msg() - free sync_msg_t struct when not passed to do_sync_protocol  
   7.382 -//
   7.383 -//  parameters:
   7.384 -//      sync_msg (in)            pointer to sync_msg_t struct to free.
   7.385 -//                               pointer can be NULL.
   7.386 -
   7.387 -DYNAMIC_API void free_sync_msg(sync_msg_t *sync_msg);
   7.388 -
   7.389 -// decode_sync_msg() - decode sync message from PER into XER
   7.390 -//
   7.391 -//  parameters:
   7.392 -//      data (in)               PER encoded data
   7.393 -//      size (in)               size of PER encoded data
   7.394 -//      text (out)              XER text of the same sync message
   7.395 -
   7.396 -DYNAMIC_API PEP_STATUS decode_sync_msg(
   7.397 -        const char *data,
   7.398 -        size_t size,
   7.399 -        char **text
   7.400 -    );
   7.401 -
   7.402 -
   7.403 -// encode_sync_msg() - encode sync message from XER into PER
   7.404 -//
   7.405 -//  parameters:
   7.406 -//      text (in)               string with XER text of the sync message
   7.407 -//      data (out)              PER encoded data
   7.408 -//      size (out)              size of PER encoded data
   7.409 -
   7.410 -DYNAMIC_API PEP_STATUS encode_sync_msg(
   7.411 -        const char *text,
   7.412 -        char **data,
   7.413 -        size_t *size
   7.414 -    );
   7.415 -
   7.416 -
   7.417 -#ifdef __cplusplus
   7.418 -}
   7.419 -#endif
   7.420 -
     8.1 --- a/src/sync_actions.c	Thu Nov 02 17:02:05 2017 +0100
     8.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     8.3 @@ -1,434 +0,0 @@
     8.4 -// This file is under GNU General Public License 3.0
     8.5 -// see LICENSE.txt
     8.6 -
     8.7 -// Actions for DeviceState state machine
     8.8 -
     8.9 -#include <assert.h>
    8.10 -#include "pEp_internal.h"
    8.11 -#include "message.h"
    8.12 -#include "sync_fsm.h"
    8.13 -#include "sync_impl.h"
    8.14 -#include "map_asn1.h"
    8.15 -#include "baseprotocol.h"
    8.16 -
    8.17 -// conditions
    8.18 -
    8.19 -int deviceGrouped(PEP_SESSION session)
    8.20 -{
    8.21 -    assert(session);
    8.22 -    if (!session)
    8.23 -        return invalid_condition; // error
    8.24 -
    8.25 -    char *devgrp = NULL;
    8.26 -    int res = 0;
    8.27 -    PEP_STATUS status;
    8.28 -
    8.29 -    status = get_device_group(session, &devgrp);
    8.30 -
    8.31 -    if (status == PEP_STATUS_OK && devgrp && devgrp[0])
    8.32 -        res = 1;
    8.33 -
    8.34 -    free(devgrp);
    8.35 -
    8.36 -    return res;
    8.37 -}
    8.38 -
    8.39 -int keyElectionWon(PEP_SESSION session, Identity partner)
    8.40 -{
    8.41 -    assert(session);
    8.42 -    assert(partner);
    8.43 -    if (!(session && partner))
    8.44 -        return invalid_condition; // error
    8.45 -
    8.46 -    int partner_is_group = partner->flags & PEP_idf_devicegroup;
    8.47 -
    8.48 -    if (deviceGrouped(session)){
    8.49 -        // existing group always wins against sole device
    8.50 -        if(!partner_is_group)
    8.51 -            return 1;
    8.52 -    } else {
    8.53 -        // sole device always loses against group
    8.54 -        if(partner_is_group)
    8.55 -            return 0;
    8.56 -    }
    8.57 -
    8.58 -    // two groups or two sole are elected based on key age
    8.59 -    // key created first wins
    8.60 -
    8.61 -    Identity me = NULL;
    8.62 -    PEP_STATUS status = get_identity(session, partner->address, PEP_OWN_USERID,
    8.63 -            &me);
    8.64 -    if (status == PEP_OUT_OF_MEMORY)
    8.65 -        return invalid_out_of_memory;
    8.66 -    if (status != PEP_STATUS_OK)
    8.67 -        return invalid_condition; // error
    8.68 -
    8.69 -    int result = invalid_condition; // error state has to be overwritten
    8.70 -
    8.71 -    time_t own_created;
    8.72 -    time_t partners_created;
    8.73 -
    8.74 -    status = key_created(session, me->fpr, &own_created);
    8.75 -    if (status != PEP_STATUS_OK)
    8.76 -        goto the_end;
    8.77 -
    8.78 -    status = key_created(session, partner->fpr, &partners_created);
    8.79 -    if (status != PEP_STATUS_OK)
    8.80 -        goto the_end;
    8.81 -
    8.82 -    if (own_created > partners_created)
    8.83 -        result = 0;
    8.84 -    else
    8.85 -        result = 1;
    8.86 -
    8.87 -the_end:
    8.88 -    free_identity(me);
    8.89 -    return result;
    8.90 -}
    8.91 -
    8.92 -int sameIdentities(PEP_SESSION session, Identity a, Identity b)
    8.93 -{
    8.94 -    assert(session);
    8.95 -    assert(a);
    8.96 -    assert(b);
    8.97 -
    8.98 -    if (!(session && a && b))
    8.99 -        return invalid_condition; // error
   8.100 -
   8.101 -    if (a->fpr == NULL || b->fpr == NULL ||
   8.102 -        (!_same_fpr(a->fpr, strlen(a->fpr), b->fpr, strlen(b->fpr))) ||
   8.103 -        a->address == NULL || b->address == NULL ||
   8.104 -        strcmp(a->address, b->address) != 0 ||
   8.105 -        a->user_id == NULL || b->user_id == NULL ||
   8.106 -        strcmp(a->user_id, b->user_id) != 0)
   8.107 -            return 0;
   8.108 -    return 1;
   8.109 -}
   8.110 -
   8.111 -int sameKeyAndAddress(PEP_SESSION session, Identity a, Identity b)
   8.112 -{
   8.113 -    assert(session);
   8.114 -    assert(a);
   8.115 -    assert(b);
   8.116 -
   8.117 -    if (!(session && a && b))
   8.118 -        return invalid_condition; // error
   8.119 -
   8.120 -    if (a->fpr == NULL || b->fpr == NULL ||
   8.121 -        (!_same_fpr(a->fpr, strlen(a->fpr), b->fpr, strlen(b->fpr))) ||
   8.122 -        a->address == NULL || b->address == NULL ||
   8.123 -        strcmp(a->address, b->address) != 0)
   8.124 -            return 0;
   8.125 -    return 1;
   8.126 -}
   8.127 -
   8.128 -// actions
   8.129 -
   8.130 -PEP_STATUS _notifyHandshake(
   8.131 -        PEP_SESSION session,
   8.132 -        Identity partner,
   8.133 -        sync_handshake_signal signal
   8.134 -    )
   8.135 -{
   8.136 -    PEP_STATUS status = PEP_STATUS_OK;
   8.137 -
   8.138 -    assert(session);
   8.139 -    assert(partner);
   8.140 -
   8.141 -    if (!(session && partner))
   8.142 -        return PEP_ILLEGAL_VALUE;
   8.143 -
   8.144 -    assert(session->notifyHandshake);
   8.145 -    if (!session->notifyHandshake)
   8.146 -        return PEP_SYNC_NO_NOTIFY_CALLBACK;
   8.147 -
   8.148 -    // notifyHandshake take ownership of given identities
   8.149 -    pEp_identity *me = NULL;
   8.150 -    status = get_identity(session, partner->address, PEP_OWN_USERID, &me);
   8.151 -    if (status != PEP_STATUS_OK)
   8.152 -        goto error;
   8.153 -    
   8.154 -    pEp_identity *_partner = NULL;
   8.155 -    _partner = identity_dup(partner);
   8.156 -    if (_partner == NULL){
   8.157 -        status = PEP_OUT_OF_MEMORY;
   8.158 -        goto error;
   8.159 -    }
   8.160 -
   8.161 -    status = session->notifyHandshake(session->sync_obj, me, _partner, signal);
   8.162 -    if (status != PEP_STATUS_OK)
   8.163 -        goto error;
   8.164 -
   8.165 -    return status;
   8.166 -
   8.167 -error:
   8.168 -    free_identity(me);
   8.169 -    return status;
   8.170 -}
   8.171 -
   8.172 -// acceptHandshake() - stores acception of partner
   8.173 -//
   8.174 -//  params:
   8.175 -//      session (in)        session handle
   8.176 -//      state (in)          state the state machine is in
   8.177 -//      partner (in)        partner to communicate with
   8.178 -//
   8.179 -//  returns:
   8.180 -//      PEP_STATUS_OK or any other value on error
   8.181 -
   8.182 -PEP_STATUS acceptHandshake(
   8.183 -        PEP_SESSION session,
   8.184 -        DeviceState_state state,
   8.185 -        Identity partner,
   8.186 -        void *extra
   8.187 -    )
   8.188 -{
   8.189 -    PEP_STATUS status = PEP_STATUS_OK;
   8.190 -
   8.191 -    assert(session);
   8.192 -    assert(partner);
   8.193 -    assert(extra == NULL);
   8.194 -    if (!(session && partner))
   8.195 -        return PEP_ILLEGAL_VALUE;
   8.196 -
   8.197 -    status = trust_personal_key(session, partner);
   8.198 -
   8.199 -    return status;
   8.200 -}
   8.201 -
   8.202 -
   8.203 -// rejectHandshake() - stores rejection of partner
   8.204 -//
   8.205 -//  params:
   8.206 -//      session (in)        session handle
   8.207 -//      state (in)          state the state machine is in
   8.208 -//      partner (in)        partner to communicate with
   8.209 -//
   8.210 -//  returns:
   8.211 -//      PEP_STATUS_OK or any other value on error
   8.212 -
   8.213 -PEP_STATUS rejectHandshake(
   8.214 -        PEP_SESSION session,
   8.215 -        DeviceState_state state,
   8.216 -        Identity partner,
   8.217 -        void *extra
   8.218 -    )
   8.219 -{
   8.220 -    PEP_STATUS status = PEP_STATUS_OK;
   8.221 -
   8.222 -    assert(session);
   8.223 -    assert(partner);
   8.224 -    assert(extra == NULL);
   8.225 -    if (!(session && partner))
   8.226 -        return PEP_ILLEGAL_VALUE;
   8.227 -
   8.228 -    // TODO : disable sync globally if not in a group
   8.229 -    status = set_identity_flags(session, partner,
   8.230 -            partner->flags | PEP_idf_not_for_sync);
   8.231 -
   8.232 -    return status;
   8.233 -}
   8.234 -
   8.235 -PEP_STATUS _storeGroupKeys(
   8.236 -        PEP_SESSION session,
   8.237 -        identity_list *group_keys
   8.238 -    )
   8.239 -{
   8.240 -    PEP_STATUS status = PEP_STATUS_OK;
   8.241 -
   8.242 -    for (identity_list *il = group_keys; il && il->ident; il = il->next) {
   8.243 -
   8.244 -        if (strcmp(il->ident->user_id, PEP_OWN_USERID)!=0) {
   8.245 -            assert(0);
   8.246 -            continue;
   8.247 -        }
   8.248 -        // Check that identity isn't excluded from sync.
   8.249 -        pEp_identity *stored_identity = NULL;
   8.250 -        status = get_identity(session, il->ident->address, PEP_OWN_USERID,
   8.251 -                &stored_identity);
   8.252 -        if (status == PEP_STATUS_OK) {
   8.253 -            if(stored_identity->flags & PEP_idf_not_for_sync){
   8.254 -                free_identity(stored_identity);
   8.255 -                continue;
   8.256 -            }
   8.257 -            free_identity(stored_identity);
   8.258 -        }
   8.259 -
   8.260 -        status = set_identity(session, il->ident);
   8.261 -        if (status != PEP_STATUS_OK)
   8.262 -            break;
   8.263 -    }
   8.264 -
   8.265 -    return status;
   8.266 -}
   8.267 -    
   8.268 -
   8.269 -// storeGroupKeys() - 
   8.270 -//
   8.271 -//  params:
   8.272 -//      session (in)        session handle
   8.273 -//      state (in)          state the state machine is in
   8.274 -//      partner (in)        partner to communicate with
   8.275 -//      _group_keys (in)    group keys received from partner
   8.276 -//
   8.277 -//  returns:
   8.278 -//      PEP_STATUS_OK or any other value on error
   8.279 -
   8.280 -PEP_STATUS storeGroupKeys(
   8.281 -        PEP_SESSION session,
   8.282 -        DeviceState_state state,
   8.283 -        Identity partner,
   8.284 -        void *group_keys_extra_
   8.285 -    )
   8.286 -{
   8.287 -    PEP_STATUS status = PEP_STATUS_OK;
   8.288 -
   8.289 -    assert(session);
   8.290 -    assert(partner);
   8.291 -    assert(group_keys_extra_);
   8.292 -    if (!(session && partner && group_keys_extra_))
   8.293 -        return PEP_ILLEGAL_VALUE;
   8.294 -
   8.295 -    group_keys_extra_t *group_keys_extra = 
   8.296 -        (group_keys_extra_t*) group_keys_extra_;
   8.297 -    identity_list *group_keys = group_keys_extra->group_keys;
   8.298 -    char *group_id = group_keys_extra->group_id;
   8.299 -
   8.300 -    status = _storeGroupKeys(session, group_keys);
   8.301 -    if (status != PEP_STATUS_OK)
   8.302 -        return status;
   8.303 -
   8.304 -    // set group id according to given group-id
   8.305 -    status = set_device_group(session, group_id);
   8.306 -    if (status != PEP_STATUS_OK)
   8.307 -        return status;
   8.308 -    
   8.309 -    return status;
   8.310 -}
   8.311 -
   8.312 -// storeGroupUpdate() - 
   8.313 -//
   8.314 -//  params:
   8.315 -//      session (in)        session handle
   8.316 -//      state (in)          state the state machine is in
   8.317 -//      partner (in)        partner to communicate with
   8.318 -//      _group_keys (in)    group keys received from partner
   8.319 -//
   8.320 -//  returns:
   8.321 -//      PEP_STATUS_OK or any other value on error
   8.322 -
   8.323 -PEP_STATUS storeGroupUpdate(
   8.324 -        PEP_SESSION session,
   8.325 -        DeviceState_state state,
   8.326 -        Identity partner,
   8.327 -        void *group_keys_
   8.328 -    )
   8.329 -{
   8.330 -    PEP_STATUS status = PEP_STATUS_OK;
   8.331 -
   8.332 -    assert(session);
   8.333 -    assert(partner);
   8.334 -    assert(group_keys_);
   8.335 -    if (!(session && partner && group_keys_))
   8.336 -        return PEP_ILLEGAL_VALUE;
   8.337 -
   8.338 -    identity_list *group_keys = (identity_list*) group_keys_;
   8.339 -
   8.340 -    status = _storeGroupKeys(session, group_keys);
   8.341 -
   8.342 -
   8.343 -    return status;
   8.344 -}
   8.345 -
   8.346 -// makeGroup() - 
   8.347 -//
   8.348 -//  params:
   8.349 -//      session (in)        session handle
   8.350 -//      state (in)          state the state machine is in
   8.351 -//      partner (in)        ignored
   8.352 -//      extra (in)          ignored
   8.353 -//
   8.354 -//  returns:
   8.355 -//      PEP_STATUS_OK or any other value on error
   8.356 -
   8.357 -PEP_STATUS makeGroup(
   8.358 -        PEP_SESSION session,
   8.359 -        DeviceState_state state,
   8.360 -        Identity partner,
   8.361 -        void *extra
   8.362 -    )
   8.363 -{
   8.364 -    PEP_STATUS status = PEP_STATUS_OK;
   8.365 -
   8.366 -    assert(session);
   8.367 -   
   8.368 -    // make a new uuid 
   8.369 -    char new_uuid[37];
   8.370 -    pEpUUID uuid;
   8.371 -    uuid_generate_random(uuid);
   8.372 -    uuid_unparse_upper(uuid, new_uuid);
   8.373 -
   8.374 -    // take that new uuid as group-id
   8.375 -    status = set_device_group(session, new_uuid);
   8.376 -
   8.377 -    return status;
   8.378 -}
   8.379 -
   8.380 -// renewUUID() - 
   8.381 -//
   8.382 -//  params:
   8.383 -//      session (in)        session handle
   8.384 -//      state (in)          state the state machine is in
   8.385 -//      partner (in)        ignored
   8.386 -//      extra (in)          ignored
   8.387 -//
   8.388 -//  returns:
   8.389 -//      PEP_STATUS_OK or any other value on error
   8.390 -
   8.391 -PEP_STATUS renewUUID(
   8.392 -        PEP_SESSION session,
   8.393 -        DeviceState_state state,
   8.394 -        Identity partner,
   8.395 -        void *extra
   8.396 -    )
   8.397 -{
   8.398 -    PEP_STATUS status = PEP_STATUS_OK;
   8.399 -
   8.400 -    assert(session);
   8.401 -
   8.402 -    // change sync_uuid when entering group 
   8.403 -    // thus ignoring unprocessed handshakes
   8.404 -    // addressed to previous self (sole) once in.
   8.405 -    pEpUUID uuid;
   8.406 -    uuid_generate_random(uuid);
   8.407 -    uuid_unparse_upper(uuid, session->sync_uuid);
   8.408 -    
   8.409 -    return status;
   8.410 -}
   8.411 -
   8.412 -// leaveGroup() - 
   8.413 -//
   8.414 -//  params:
   8.415 -//      session (in)        session handle
   8.416 -//      state (in)          state the state machine is in
   8.417 -//      partner (in)        ignored
   8.418 -//      extra (in)          ignored
   8.419 -//
   8.420 -//  returns:
   8.421 -//      PEP_STATUS_OK or any other value on error
   8.422 -
   8.423 -PEP_STATUS leaveGroup(
   8.424 -        PEP_SESSION session,
   8.425 -        DeviceState_state state,
   8.426 -        Identity partner,
   8.427 -        void *extra
   8.428 -    )
   8.429 -{
   8.430 -    PEP_STATUS status = PEP_STATUS_OK;
   8.431 -
   8.432 -    assert(session);
   8.433 -
   8.434 -    status = set_device_group(session, NULL);
   8.435 -    
   8.436 -    return status;
   8.437 -}
     9.1 --- a/src/sync_app.h	Thu Nov 02 17:02:05 2017 +0100
     9.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     9.3 @@ -1,34 +0,0 @@
     9.4 -//
     9.5 -//  sync_app.h
     9.6 -//  pEpEngine
     9.7 -//
     9.8 -//  Created by Dirk Zimmermann on 16.05.17.
     9.9 -//  Copyright © 2017 Edouard Tisserant. All rights reserved.
    9.10 -//
    9.11 -
    9.12 -#ifndef sync_app_h
    9.13 -#define sync_app_h
    9.14 -
    9.15 -// TODO add this to generated code.
    9.16 -typedef enum _sync_handshake_signal {
    9.17 -    SYNC_NOTIFY_UNDEFINED = 0,
    9.18 -
    9.19 -    // request show handshake dialog
    9.20 -    SYNC_NOTIFY_INIT_ADD_OUR_DEVICE,
    9.21 -    SYNC_NOTIFY_INIT_ADD_OTHER_DEVICE,
    9.22 -    SYNC_NOTIFY_INIT_FORM_GROUP,
    9.23 -    SYNC_NOTIFY_INIT_MOVE_OUR_DEVICE,
    9.24 -
    9.25 -    // handshake process timed out
    9.26 -    SYNC_NOTIFY_TIMEOUT,
    9.27 -
    9.28 -    // handshake accepted by user
    9.29 -    SYNC_NOTIFY_ACCEPTED_DEVICE_ADDED,
    9.30 -    SYNC_NOTIFY_ACCEPTED_GROUP_CREATED,
    9.31 -    SYNC_NOTIFY_ACCEPTED_DEVICE_MOVED,
    9.32 -
    9.33 -    // handshake dialog must be closed
    9.34 -    SYNC_NOTIFY_OVERTAKEN
    9.35 -} sync_handshake_signal;
    9.36 -
    9.37 -#endif /* sync_app_h */
    10.1 --- a/src/sync_impl.c	Thu Nov 02 17:02:05 2017 +0100
    10.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
    10.3 @@ -1,971 +0,0 @@
    10.4 -// This file is under GNU General Public License 3.0
    10.5 -// see LICENSE.txt
    10.6 -
    10.7 -#include "platform.h"
    10.8 -
    10.9 -// it seems pEp_internal.h needs to be the first pEp include due to the 
   10.10 -// #define for the dllimport / dllexport DYNAMIC_API stuff.
   10.11 -#include "pEp_internal.h"
   10.12 -
   10.13 -#include "sync_impl.h"
   10.14 -#include "keymanagement.h"
   10.15 -#include "message_api.h"
   10.16 -#include "map_asn1.h"
   10.17 -#include "baseprotocol.h"
   10.18 -
   10.19 -#define SYNC_VERSION_MAJOR 1
   10.20 -#define SYNC_VERSION_MINOR 0
   10.21 -
   10.22 -#define SYNC_INHIBIT_TIME (60*10)
   10.23 -#define SYNC_MSG_EXPIRE_TIME (60 * 10)
   10.24 -
   10.25 -struct _sync_msg_t {
   10.26 -    bool is_a_message;
   10.27 -    union {
   10.28 -        DeviceGroup_Protocol_t *message;
   10.29 -        struct {
   10.30 -            DeviceState_event event;
   10.31 -            Identity partner;
   10.32 -            void *extra;
   10.33 -        } event;
   10.34 -    } u;
   10.35 -};
   10.36 -
   10.37 -static bool _is_own_uuid( PEP_SESSION session, UTF8String_t *uuid)
   10.38 -{
   10.39 -    return strncmp(session->sync_session->sync_uuid,
   10.40 -                   (const char*)uuid->buf, uuid->size) == 0;
   10.41 -}
   10.42 -
   10.43 -static bool _is_own_group_uuid( PEP_SESSION session, UTF8String_t *uuid, char** our_group)
   10.44 -{
   10.45 -    PEP_STATUS status = PEP_STATUS_OK;
   10.46 -    char *devgrp = NULL;
   10.47 -
   10.48 -    if(our_group == NULL || *our_group == NULL)
   10.49 -        status = get_device_group(session, &devgrp);
   10.50 -    else
   10.51 -        devgrp = *our_group;
   10.52 -
   10.53 -    bool res = (status == PEP_STATUS_OK && devgrp && devgrp[0] &&
   10.54 -        strncmp(devgrp,(const char*)uuid->buf, uuid->size) == 0);
   10.55 -
   10.56 -    if(our_group == NULL)
   10.57 -        free(devgrp);
   10.58 -    else if(*our_group == NULL)
   10.59 -        *our_group = devgrp;
   10.60 -
   10.61 -    return res;
   10.62 -}
   10.63 -
   10.64 -PEP_STATUS receive_sync_msg(
   10.65 -        PEP_SESSION session,
   10.66 -        sync_msg_t *sync_msg,
   10.67 -        time_t *timeout
   10.68 -    )
   10.69 -{
   10.70 -    PEP_STATUS status;
   10.71 -    void *extra = NULL;
   10.72 -    Identity partner = NULL;
   10.73 -    DeviceState_event event = DeviceState_event_NONE;
   10.74 -    assert(session && sync_msg);
   10.75 -    if (!(session && sync_msg))
   10.76 -        return PEP_ILLEGAL_VALUE;
   10.77 -
   10.78 -    bool msgIsFromGroup = false;
   10.79 -    if(sync_msg->is_a_message){
   10.80 -        DeviceGroup_Protocol_t *msg = sync_msg->u.message;
   10.81 -        assert(msg && msg->payload.present != DeviceGroup_Protocol__payload_PR_NOTHING);
   10.82 -        if (!(msg && msg->payload.present != DeviceGroup_Protocol__payload_PR_NOTHING)){
   10.83 -            status = PEP_OUT_OF_MEMORY;
   10.84 -            goto error;
   10.85 -        }
   10.86 -
   10.87 -        partner = Identity_to_Struct(&msg->header.me, NULL);
   10.88 -        if (!partner){
   10.89 -            status = PEP_OUT_OF_MEMORY;
   10.90 -            ASN_STRUCT_FREE(asn_DEF_DeviceGroup_Protocol, msg);
   10.91 -            goto error;
   10.92 -        }
   10.93 -
   10.94 -        msgIsFromGroup = msg->header.devicegroup;
   10.95 -
   10.96 -        switch (msg->payload.present) {
   10.97 -            case DeviceGroup_Protocol__payload_PR_beacon:
   10.98 -                event = Beacon;
   10.99 -                break;
  10.100 -
  10.101 -            case DeviceGroup_Protocol__payload_PR_handshakeRequest:
  10.102 -            {
  10.103 -                // re-check uuid in case sync_uuid or group changed while in the queue
  10.104 -                char *own_group_id = NULL;
  10.105 -                bool is_for_me = _is_own_uuid(session, 
  10.106 -                    msg->payload.choice.handshakeRequest.partner_id);
  10.107 -                bool is_for_group = !is_for_me && _is_own_group_uuid(session, 
  10.108 -                    msg->payload.choice.handshakeRequest.partner_id, &own_group_id);
  10.109 -                if (!(is_for_me || is_for_group)){
  10.110 -                    status = PEP_MESSAGE_IGNORE;
  10.111 -                    goto error;
  10.112 -                }
  10.113 -
  10.114 -                UTF8String_t *guuid = msg->payload.choice.handshakeRequest.group_id;
  10.115 -                if(msgIsFromGroup && guuid && guuid->buf && guuid->size) {
  10.116 -                    bool is_from_own_group = _is_own_group_uuid(session, 
  10.117 -                                                                guuid, &own_group_id);
  10.118 -
  10.119 -                    // Filter handshake requests from own group
  10.120 -                    if(is_from_own_group) {
  10.121 -                        status = PEP_MESSAGE_IGNORE;
  10.122 -                        goto error;
  10.123 -                    }
  10.124 -
  10.125 -                    // if it comes from another group, 
  10.126 -                    // we want to communicate with that group
  10.127 -                    // insert group_id given in handshake request 
  10.128 -                    // into partner's id
  10.129 -
  10.130 -                    free(partner->user_id);
  10.131 -                    partner->user_id = strndup((const char*)guuid->buf, guuid->size);
  10.132 -                    if(partner->user_id == NULL){
  10.133 -                        status = PEP_OUT_OF_MEMORY;
  10.134 -                        goto error;
  10.135 -                    }
  10.136 -
  10.137 -                    // if it comes from another group, and we are grouped,
  10.138 -                    // then this is groupmerge
  10.139 -                }
  10.140 -
  10.141 -                event = HandshakeRequest;
  10.142 -                break;
  10.143 -
  10.144 -            }
  10.145 -            case DeviceGroup_Protocol__payload_PR_updateRequest:
  10.146 -                event = UpdateRequest;
  10.147 -                break;
  10.148 -
  10.149 -            case DeviceGroup_Protocol__payload_PR_groupKeys:
  10.150 -            {
  10.151 -                // re-check uuid in case sync_uuid or group_uuid changed while in the queue
  10.152 -                char *own_group_id = NULL;
  10.153 -                UTF8String_t *puuid = msg->payload.choice.groupKeys.partner_id;
  10.154 -                bool is_for_me = _is_own_uuid(session, puuid);
  10.155 -                bool is_for_group = !is_for_me &&
  10.156 -                                    _is_own_group_uuid(session, 
  10.157 -                                        puuid, &own_group_id);
  10.158 -                if (!(is_for_me || is_for_group)){
  10.159 -                    status = PEP_MESSAGE_IGNORE;
  10.160 -                    goto error;
  10.161 -                }
  10.162 -
  10.163 -                UTF8String_t *guuid = msg->payload.choice.groupKeys.group_id;
  10.164 -
  10.165 -                // GroupKeys come from groups, no choice
  10.166 -                if(!(msgIsFromGroup && guuid && guuid->buf && guuid->size)) {
  10.167 -                    status = PEP_SYNC_ILLEGAL_MESSAGE;
  10.168 -                    goto error;
  10.169 -                }
  10.170 -
  10.171 -                // Filter groupKeys from own group
  10.172 -                bool is_from_own_group = _is_own_group_uuid(session, 
  10.173 -                                                            guuid, 
  10.174 -                                                            &own_group_id);
  10.175 -                if(is_from_own_group) {
  10.176 -                    // FixMe : protocol shouldn't allow this
  10.177 -                    status = PEP_SYNC_ILLEGAL_MESSAGE;
  10.178 -                    goto error;
  10.179 -                }
  10.180 -
  10.181 -                // insert group_id given in groupKeys into partner's id
  10.182 -                free(partner->user_id);
  10.183 -                partner->user_id = strndup((const char*)guuid->buf, guuid->size);
  10.184 -                if(partner->user_id == NULL){
  10.185 -                    status = PEP_OUT_OF_MEMORY;
  10.186 -                    goto error;
  10.187 -                }
  10.188 -
  10.189 -                // if it comes from another group, and we are grouped,
  10.190 -                // then this is groupmerge's groupKeys
  10.191 -
  10.192 -                group_keys_extra_t *group_keys_extra;
  10.193 -                group_keys_extra = malloc(sizeof(group_keys_extra_t));
  10.194 -                if(group_keys_extra == NULL){
  10.195 -                    status = PEP_OUT_OF_MEMORY;
  10.196 -                    ASN_STRUCT_FREE(asn_DEF_DeviceGroup_Protocol, msg);
  10.197 -                    goto error;
  10.198 -                }
  10.199 -
  10.200 -                char *group_id = strndup((char*)guuid->buf, guuid->size);
  10.201 -
  10.202 -                if (!group_id){
  10.203 -                    status = PEP_OUT_OF_MEMORY;
  10.204 -                    free(group_keys_extra);
  10.205 -                    ASN_STRUCT_FREE(asn_DEF_DeviceGroup_Protocol, msg);
  10.206 -                    goto error;
  10.207 -                }
  10.208 -                group_keys_extra->group_id = group_id;
  10.209 -
  10.210 -                identity_list *group_keys = IdentityList_to_identity_list(
  10.211 -                        &msg->payload.choice.groupKeys.ownIdentities,
  10.212 -                        NULL);
  10.213 -                if (!group_keys) {
  10.214 -                    status = PEP_OUT_OF_MEMORY;
  10.215 -                    free(group_id);
  10.216 -                    free(group_keys_extra);
  10.217 -                    ASN_STRUCT_FREE(asn_DEF_DeviceGroup_Protocol, msg);
  10.218 -                    goto error;
  10.219 -                }
  10.220 -                group_keys_extra->group_keys = group_keys;
  10.221 -
  10.222 -                extra = (void *) group_keys_extra;
  10.223 -                event = GroupKeys;
  10.224 -
  10.225 -                break;
  10.226 -            }
  10.227 -            case DeviceGroup_Protocol__payload_PR_groupUpdate:
  10.228 -            {
  10.229 -                identity_list *group_keys = IdentityList_to_identity_list(
  10.230 -                        &msg->payload.choice.groupUpdate.ownIdentities, NULL);
  10.231 -                if (!group_keys) {
  10.232 -                    status = PEP_OUT_OF_MEMORY;
  10.233 -                    ASN_STRUCT_FREE(asn_DEF_DeviceGroup_Protocol, msg);
  10.234 -                    goto error;
  10.235 -                }
  10.236 -                extra = (void *) group_keys;
  10.237 -                event = GroupUpdate;
  10.238 -                break;
  10.239 -            }
  10.240 -
  10.241 -            default:
  10.242 -                status = PEP_SYNC_ILLEGAL_MESSAGE;
  10.243 -                ASN_STRUCT_FREE(asn_DEF_DeviceGroup_Protocol, msg);
  10.244 -                goto error;
  10.245 -        }
  10.246 -
  10.247 -    }
  10.248 -    else{
  10.249 -        partner = sync_msg->u.event.partner;
  10.250 -        extra = sync_msg->u.event.extra;
  10.251 -        event = sync_msg->u.event.event;
  10.252 -    }
  10.253 -
  10.254 -    // Event inhibition, to limit mailbox and prevent cycles
  10.255 -    time_t *last = NULL;
  10.256 -    switch(event){
  10.257 -        case CannotDecrypt:
  10.258 -            last = &session->LastCannotDecrypt;
  10.259 -            break;
  10.260 -
  10.261 -        case UpdateRequest:
  10.262 -            last = &session->LastUpdateRequest;
  10.263 -            break;
  10.264 -
  10.265 -        default:
  10.266 -            break;
  10.267 -    }
  10.268 -
  10.269 -    if(last != NULL){
  10.270 -        time_t now = time(NULL);
  10.271 -        if(*last != 0 && (*last + SYNC_INHIBIT_TIME) > now ){
  10.272 -            status = PEP_STATEMACHINE_INHIBITED_EVENT;
  10.273 -            goto error;
  10.274 -        }
  10.275 -        *last = now;
  10.276 -    }
  10.277 -
  10.278 -    // partner identity must be explicitely added DB to later
  10.279 -    // be able to communicate securely with it.
  10.280 -    if(partner){
  10.281 -        // protect virtual user IDs 
  10.282 -        if((strncmp("TOFU_", partner->user_id, 6) == 0 &&
  10.283 -           strlen(partner->user_id) == strlen(partner->address) + 6 &&
  10.284 -           strcmp(partner->user_id + 6, partner->address)) ||
  10.285 -        // protect own ID 
  10.286 -           (strcmp(PEP_OWN_USERID, partner->user_id) == 0)){
  10.287 -            status = PEP_SYNC_ILLEGAL_MESSAGE;
  10.288 -            goto error;
  10.289 -        }
  10.290 -
  10.291 -        // partner IDs are UUIDs bound to session lifespan
  10.292 -        // and therefore partner identities are not supposed
  10.293 -        // to mutate over time, but just not be used anymore.
  10.294 -        // It should then be safe to accept given identity if not 
  10.295 -        // already pre-existing
  10.296 -        pEp_identity *stored_identity = NULL;
  10.297 -        status = get_identity(session,
  10.298 -                              partner->address,
  10.299 -                              partner->user_id,
  10.300 -                              &stored_identity);
  10.301 -
  10.302 -        if (!stored_identity) {
  10.303 -            // make a safe copy of partner, with no flags or comm_type
  10.304 -            pEp_identity *tmpident = new_identity(partner->address,
  10.305 -                                                  partner->fpr,
  10.306 -                                                  partner->user_id,
  10.307 -                                                  partner->username);
  10.308 -            if (tmpident == NULL){
  10.309 -                status = PEP_OUT_OF_MEMORY;
  10.310 -                goto error;
  10.311 -            }
  10.312 -
  10.313 -            // finaly add partner to DB
  10.314 -            status = set_identity(session, tmpident);
  10.315 -            assert(status == PEP_STATUS_OK);
  10.316 -            if(status == PEP_STATUS_OK && msgIsFromGroup)
  10.317 -                status = set_identity_flags(session, tmpident, PEP_idf_devicegroup);
  10.318 -            free_identity(tmpident);
  10.319 -            assert(status == PEP_STATUS_OK);
  10.320 -            if (status != PEP_STATUS_OK) {
  10.321 -                goto error;
  10.322 -            }
  10.323 -        }
  10.324 -        else if (status == PEP_STATUS_OK) {
  10.325 -            free_identity(stored_identity);
  10.326 -        } 
  10.327 -        else
  10.328 -            goto error;
  10.329 -    }
  10.330 -
  10.331 -    status = fsm_DeviceState_inject(session, event, partner, extra, timeout);
  10.332 -
  10.333 -error:
  10.334 -
  10.335 -    free_identity(partner);
  10.336 -
  10.337 -    switch(event){
  10.338 -        case GroupKeys:
  10.339 -        {
  10.340 -            free_group_keys_extra((group_keys_extra_t*)extra);
  10.341 -            break;
  10.342 -        }
  10.343 -        case GroupUpdate:
  10.344 -        {
  10.345 -            identity_list *group_keys = (identity_list*) extra;
  10.346 -            free_identity_list(group_keys);
  10.347 -            break;
  10.348 -        }
  10.349 -        default:
  10.350 -            assert(extra==NULL);
  10.351 -            break;
  10.352 -    }
  10.353 -
  10.354 -    free(sync_msg);
  10.355 -
  10.356 -    return status;
  10.357 -}
  10.358 -
  10.359 -// TODO: DYNAMIC_API was here, but broke the windows build. 
  10.360 -// We need to check whether it belongs here or it's a bug.
  10.361 -/* DYNAMIC_API */ void free_sync_msg(sync_msg_t *sync_msg)
  10.362 -{
  10.363 -    if (!sync_msg)
  10.364 -        return;
  10.365 -
  10.366 -    if(sync_msg->is_a_message){
  10.367 -        DeviceGroup_Protocol_t *msg = sync_msg->u.message;
  10.368 -        assert(msg);
  10.369 -        if (!(msg))
  10.370 -            return;
  10.371 -
  10.372 -        ASN_STRUCT_FREE(asn_DEF_DeviceGroup_Protocol, msg);
  10.373 -    }
  10.374 -    else{
  10.375 -        Identity partner = NULL;
  10.376 -        partner = sync_msg->u.event.partner;
  10.377 -        if(partner != NULL)
  10.378 -            free_identity(partner);
  10.379 -    }
  10.380 -
  10.381 -    free(sync_msg);
  10.382 -
  10.383 -    return;
  10.384 -}
  10.385 -
  10.386 -// from sync.c
  10.387 -int call_inject_sync_msg(PEP_SESSION session, void *msg);
  10.388 -
  10.389 -PEP_STATUS inject_DeviceState_event(
  10.390 -    PEP_SESSION session, 
  10.391 -    DeviceState_event event,
  10.392 -    Identity partner,
  10.393 -    void *extra)
  10.394 -{
  10.395 -    PEP_STATUS status;
  10.396 -
  10.397 -    assert(session);
  10.398 -    if (!(session))
  10.399 -        return PEP_ILLEGAL_VALUE;
  10.400 -
  10.401 -    sync_msg_t *sync_msg = malloc(sizeof(sync_msg_t));
  10.402 -    if(sync_msg == NULL)
  10.403 -        return PEP_OUT_OF_MEMORY;
  10.404 -
  10.405 -    sync_msg->is_a_message = false;
  10.406 -    sync_msg->u.event.partner = partner;
  10.407 -    sync_msg->u.event.extra = extra;
  10.408 -    sync_msg->u.event.event = event;
  10.409 -
  10.410 -    status = call_inject_sync_msg(session, sync_msg);
  10.411 -    if (status == PEP_SYNC_NO_INJECT_CALLBACK){
  10.412 -        free(sync_msg);
  10.413 -    }
  10.414 -
  10.415 -    return status;
  10.416 -}
  10.417 -
  10.418 -PEP_STATUS receive_DeviceState_msg(
  10.419 -    PEP_SESSION session, 
  10.420 -    message *src, 
  10.421 -    PEP_rating rating, 
  10.422 -    stringlist_t *keylist)
  10.423 -{
  10.424 -    assert(session && src);
  10.425 -    if (!(session && src))
  10.426 -        return PEP_ILLEGAL_VALUE;
  10.427 -
  10.428 -    bool consume = false;
  10.429 -    bool discard = false;
  10.430 -    bool force_keep_msg = false;
  10.431 -
  10.432 -    for (bloblist_t *bl = src->attachments; bl && bl->value; bl = bl->next) {
  10.433 -        if (bl->mime_type && strcasecmp(bl->mime_type, "application/pEp.sync") == 0
  10.434 -                && bl->size) {
  10.435 -            DeviceGroup_Protocol_t *msg = NULL;
  10.436 -            uper_decode_complete(NULL, &asn_DEF_DeviceGroup_Protocol, (void **)
  10.437 -                    &msg, bl->value, bl->size);
  10.438 -
  10.439 -            if (msg) {
  10.440 -                PEP_STATUS status = PEP_STATUS_OK;
  10.441 -
  10.442 -                char *user_id = strndup((char *) msg->header.me.user_id->buf,
  10.443 -                        msg->header.me.user_id->size);
  10.444 -                assert(user_id);
  10.445 -                if (!user_id) {
  10.446 -                    ASN_STRUCT_FREE(asn_DEF_DeviceGroup_Protocol, msg);
  10.447 -                    return PEP_OUT_OF_MEMORY;
  10.448 -                }
  10.449 -
  10.450 -                // detect and mitigate address spoofing
  10.451 -                Identity check_me = NULL;
  10.452 -                char* null_terminated_address = 
  10.453 -                    strndup((char *) msg->header.me.address->buf,
  10.454 -                            msg->header.me.address->size);
  10.455 -
  10.456 -                if(null_terminated_address){
  10.457 -                    status = get_identity(session, 
  10.458 -                                          null_terminated_address, 
  10.459 -                                          PEP_OWN_USERID, 
  10.460 -                                          &check_me);
  10.461 -                    free(null_terminated_address);
  10.462 -                } 
  10.463 -                else
  10.464 -                    status = PEP_OUT_OF_MEMORY;
  10.465 -
  10.466 -                if (status == PEP_OUT_OF_MEMORY)
  10.467 -                    goto free_all;
  10.468 -
  10.469 -                free_identity(check_me);
  10.470 -
  10.471 -                bool not_own_address = status != PEP_STATUS_OK;
  10.472 -                status = PEP_STATUS_OK;
  10.473 -
  10.474 -                if (not_own_address || 
  10.475 -                    strncmp(src->from->address,
  10.476 -                            (char *) msg->header.me.address->buf,
  10.477 -                            msg->header.me.address->size) != 0 ||
  10.478 -                    strncmp(src->to->ident->address,
  10.479 -                            (char *) msg->header.me.address->buf,
  10.480 -                            msg->header.me.address->size) != 0) {
  10.481 -                    consume = true;
  10.482 -                    goto free_all;
  10.483 -                }
  10.484 -
  10.485 -                // if encrypted, ensure that header.me.fpr match signer's fpr
  10.486 -                if (rating >= PEP_rating_reliable && (
  10.487 -                        !keylist ||
  10.488 -                        !_same_fpr((char *) msg->header.me.fpr.buf,
  10.489 -                                   msg->header.me.fpr.size,
  10.490 -                                   keylist->value,
  10.491 -                                   strlen(keylist->value)))) {
  10.492 -                    consume = true;
  10.493 -                    goto free_all;
  10.494 -                }
  10.495 -
  10.496 -                // check message expiry 
  10.497 -                if(src->recv) {
  10.498 -                    time_t expiry = timegm(src->recv) + SYNC_MSG_EXPIRE_TIME;
  10.499 -                    time_t now = time(NULL);
  10.500 -                    if(expiry != 0 && now != 0 && expiry < now){
  10.501 -                        consume = true;
  10.502 -                        goto free_all;
  10.503 -                    }
  10.504 -                }
  10.505 -
  10.506 -                int32_t value = (int32_t) msg->header.sequence;
  10.507 -                if (value < 1) {
  10.508 -                    status = PEP_SEQUENCE_VIOLATED;
  10.509 -                } else {
  10.510 -                    status = sequence_value(session, (char *) user_id,
  10.511 -                            &value);
  10.512 -                }
  10.513 -
  10.514 -                if (status == PEP_STATUS_OK) {
  10.515 -                    switch (msg->payload.present) {
  10.516 -                        // HandshakeRequest needs encryption
  10.517 -                        case DeviceGroup_Protocol__payload_PR_handshakeRequest:
  10.518 -                        {
  10.519 -                            UTF8String_t *puuid = 
  10.520 -                              msg->payload.choice.handshakeRequest.partner_id;
  10.521 -                            bool is_for_me = _is_own_uuid(session, puuid);
  10.522 -                            bool is_for_group = !is_for_me && 
  10.523 -                                                _is_own_group_uuid(
  10.524 -                                                    session, puuid, NULL);
  10.525 -
  10.526 -                            // Reject handshake requests not addressed to us
  10.527 -                            if (rating < PEP_rating_reliable ||
  10.528 -                                !(is_for_me || is_for_group)){
  10.529 -                                discard = true;
  10.530 -                                goto free_all;
  10.531 -                            }
  10.532 -
  10.533 -                            // do not consume handshake request for group
  10.534 -                            if(is_for_group){ 
  10.535 -                                force_keep_msg = true;
  10.536 -                            }
  10.537 -                            break;
  10.538 -                        }
  10.539 -                        // accepting GroupKeys needs encryption and trust of peer device
  10.540 -                        case DeviceGroup_Protocol__payload_PR_groupKeys:
  10.541 -                        {
  10.542 -                            UTF8String_t *puuid = msg->payload.choice.groupKeys.partner_id;
  10.543 -                            bool is_for_me = _is_own_uuid(session, puuid);
  10.544 -                            bool is_for_group = !is_for_me &&
  10.545 -                                                _is_own_group_uuid(session, 
  10.546 -                                                    puuid, NULL);
  10.547 -                            if (!keylist || rating < PEP_rating_reliable ||
  10.548 -                                // message is only consumed by instance it is addressed to
  10.549 -                                !(is_for_me || is_for_group)){
  10.550 -                                discard = true;
  10.551 -                                goto free_all;
  10.552 -                            }
  10.553 -
  10.554 -                            // do not consume groupKeys for group
  10.555 -                            if(is_for_group){ 
  10.556 -                                // This happens in case case of groupmerge
  10.557 -                                force_keep_msg = true;
  10.558 -                            }
  10.559 -
  10.560 -                            // Trust check disabled here but it still it should be safe.
  10.561 -                            // SameIdentity checks in state machine ensures that we only
  10.562 -                            // store groupkeys signed by device or group that have been 
  10.563 -                            // previously accepted in handshake.
  10.564 -                            //
  10.565 -                            // // check trust of identity using user_id given in msg.header.me
  10.566 -                            // // to exacly match identity of device, the one trusted in
  10.567 -                            // // case of accepted handshake from a sole device
  10.568 -                            // pEp_identity *_from = new_identity(NULL, 
  10.569 -                            //                                    keylist->value,
  10.570 -                            //                                    user_id,
  10.571 -                            //                                    NULL);
  10.572 -                            // if (_from == NULL){
  10.573 -                            //     status = PEP_OUT_OF_MEMORY;
  10.574 -                            //     goto free_all;
  10.575 -                            // }
  10.576 -                            // status = get_trust(session, _from);
  10.577 -                            // if (status != PEP_STATUS_OK || _from->comm_type < PEP_ct_strong_encryption) {
  10.578 -
  10.579 -                            //     // re-try with group_id instead, in case of handshake with pre-existing group
  10.580 -                            //     UTF8String_t *guuid = msg->payload.choice.groupKeys.group_id;
  10.581 -                            //     free(_from->user_id);
  10.582 -                            //     if ((_from->user_id = strndup((const char*)guuid->buf, guuid->size)) == NULL){
  10.583 -                            //         free_identity(_from);
  10.584 -                            //         status = PEP_OUT_OF_MEMORY;
  10.585 -                            //         goto free_all;
  10.586 -                            //     }
  10.587 -                            //     _from->comm_type = PEP_ct_unknown;
  10.588 -
  10.589 -                            //     status = get_trust(session, _from);
  10.590 -                            //     if (status != PEP_STATUS_OK || _from->comm_type < PEP_ct_strong_encryption) {
  10.591 -                            //         status = PEP_STATUS_OK;
  10.592 -                            //         free_identity(_from);
  10.593 -                            //         discard = true;
  10.594 -                            //         goto free_all;
  10.595 -                            //     }
  10.596 -                            // }
  10.597 -                            // free_identity(_from);
  10.598 -                            break;
  10.599 -                        }
  10.600 -                        case DeviceGroup_Protocol__payload_PR_groupUpdate:
  10.601 -                        case DeviceGroup_Protocol__payload_PR_updateRequest:
  10.602 -                        {
  10.603 -                            // inject message but don't consume it, so 
  10.604 -                            // that other group members can also be updated
  10.605 -                            force_keep_msg = true;
  10.606 -                            
  10.607 -                            if (!keylist || rating < PEP_rating_reliable){
  10.608 -                                discard = true;
  10.609 -                                goto free_all;
  10.610 -                            }
  10.611 -                            // GroupUpdate and UpdateRequests come from group.
  10.612 -                            // check trust relation in between signer key and 
  10.613 -                            // own id to be sure.
  10.614 -                            pEp_identity *_from = new_identity(NULL, 
  10.615 -                                                               keylist->value,
  10.616 -                                                               PEP_OWN_USERID,
  10.617 -                                                               NULL);
  10.618 -                            if (_from == NULL){
  10.619 -                                status = PEP_OUT_OF_MEMORY;
  10.620 -                                goto free_all;
  10.621 -                            }
  10.622 -                            status = get_trust(session, _from);
  10.623 -                            if (status != PEP_STATUS_OK || _from->comm_type < PEP_ct_pEp) {
  10.624 -                                status = PEP_STATUS_OK;
  10.625 -                                free_identity(_from);
  10.626 -                                discard = true;
  10.627 -                                goto free_all;
  10.628 -                            }
  10.629 -                            free_identity(_from);
  10.630 -                        }
  10.631 -                        default:
  10.632 -                            break;
  10.633 -                    }
  10.634 -
  10.635 -
  10.636 -                    consume = true;
  10.637 -                    sync_msg_t *sync_msg = malloc(sizeof(sync_msg_t));
  10.638 -                    if(sync_msg == NULL){
  10.639 -                        status = PEP_OUT_OF_MEMORY;
  10.640 -                        goto free_all;
  10.641 -                    }
  10.642 -                    sync_msg->is_a_message = true;
  10.643 -                    sync_msg->u.message = msg;
  10.644 -                    status = call_inject_sync_msg(session, sync_msg);
  10.645 -                    if (status != PEP_STATUS_OK){
  10.646 -                        if (status == PEP_SYNC_NO_INJECT_CALLBACK){
  10.647 -                            free(sync_msg);
  10.648 -                        }
  10.649 -                        goto free_all;
  10.650 -                    }
  10.651 -                    // don't free message now that it is in the queue
  10.652 -                    goto free_userid;
  10.653 -                }
  10.654 -                else if (status == PEP_OWN_SEQUENCE || status == PEP_SEQUENCE_VIOLATED) {
  10.655 -                    status = PEP_STATUS_OK;
  10.656 -                    discard = true;
  10.657 -                    goto free_all;
  10.658 -                }
  10.659 -
  10.660 -            free_all:
  10.661 -                ASN_STRUCT_FREE(asn_DEF_DeviceGroup_Protocol, msg);
  10.662 -            free_userid:
  10.663 -                free(user_id);
  10.664 -
  10.665 -                if (status != PEP_STATUS_OK)
  10.666 -                    return status;
  10.667 -            }
  10.668 -        }
  10.669 -    }
  10.670 -
  10.671 -    if (force_keep_msg) {
  10.672 -        return PEP_MESSAGE_IGNORE;
  10.673 -    }
  10.674 -
  10.675 -    if (consume && !session->keep_sync_msg) {
  10.676 -        for (stringpair_list_t *spl = src->opt_fields ; spl && spl->value ;
  10.677 -                spl = spl->next) {
  10.678 -            if (spl->value->key &&
  10.679 -                    strcasecmp(spl->value->key, "pEp-auto-consume") == 0) {
  10.680 -                if (spl->value->value &&
  10.681 -                        strcasecmp(spl->value->value, "yes") == 0)
  10.682 -                    return PEP_MESSAGE_CONSUME;
  10.683 -            }
  10.684 -        }
  10.685 -        return PEP_MESSAGE_IGNORE;
  10.686 -    }
  10.687 -
  10.688 -    if(discard)
  10.689 -        return PEP_MESSAGE_IGNORE;
  10.690 -
  10.691 -    if (!session->keep_sync_msg) {
  10.692 -        bloblist_t *last = NULL;
  10.693 -        for (bloblist_t *bl = src->attachments; bl && bl->value; ) {
  10.694 -            if (bl->mime_type && strcasecmp(bl->mime_type, "application/pEp.sync") == 0) {
  10.695 -                bloblist_t *b = bl;
  10.696 -                bl = bl->next;
  10.697 -                if (!last)
  10.698 -                    src->attachments = bl;
  10.699 -                else
  10.700 -                    last->next = bl;
  10.701 -                free(b->mime_type);
  10.702 -                free(b->filename);
  10.703 -                free(b->value);
  10.704 -                free(b);
  10.705 -            }
  10.706 -            else {
  10.707 -                last = bl;
  10.708 -                bl = bl->next;
  10.709 -            }
  10.710 -        }
  10.711 -    }
  10.712 -
  10.713 -    return PEP_STATUS_OK;
  10.714 -}
  10.715 -
  10.716 -DeviceGroup_Protocol_t *new_DeviceGroup_Protocol_msg(DeviceGroup_Protocol__payload_PR type)
  10.717 -{
  10.718 -    DeviceGroup_Protocol_t *msg = (DeviceGroup_Protocol_t *)
  10.719 -            calloc(1, sizeof(DeviceGroup_Protocol_t));
  10.720 -    assert(msg);
  10.721 -    if (!msg)
  10.722 -        return NULL;
  10.723 -    msg->payload.present = type;
  10.724 -    return msg;
  10.725 -}
  10.726 -
  10.727 -void free_DeviceGroup_Protocol_msg(DeviceGroup_Protocol_t *msg)
  10.728 -{
  10.729 -    ASN_STRUCT_FREE(asn_DEF_DeviceGroup_Protocol, msg);
  10.730 -}
  10.731 -
  10.732 -
  10.733 -#ifndef NDEBUG
  10.734 -static int _append(const void *buffer, size_t size, void *appkey)
  10.735 -{
  10.736 -    char **dest_ptr = (char **)appkey;
  10.737 -    size_t osize = strlen(*dest_ptr);
  10.738 -    size_t nsize = size + osize;
  10.739 -    *dest_ptr = realloc(*dest_ptr, nsize + 1);
  10.740 -    if(*dest_ptr == NULL) return -1;
  10.741 -    memcpy(*dest_ptr + osize, buffer, size);
  10.742 -    (*dest_ptr)[nsize] = '\0';
  10.743 -    return 0;
  10.744 -}
  10.745 -#endif
  10.746 -
  10.747 -PEP_STATUS unicast_msg(
  10.748 -        PEP_SESSION session,
  10.749 -        const Identity partner,
  10.750 -        DeviceState_state state,
  10.751 -        DeviceGroup_Protocol_t *msg,
  10.752 -        bool encrypted
  10.753 -    )
  10.754 -{
  10.755 -    PEP_STATUS status = PEP_STATUS_OK;
  10.756 -    char *payload = NULL;
  10.757 -    message *_message = NULL;
  10.758 -    pEp_identity *me = NULL;
  10.759 -    pEp_identity *_me = NULL;
  10.760 -
  10.761 -    assert(session && partner && state && msg);
  10.762 -    if (!(session && partner && state && msg))
  10.763 -        return PEP_ILLEGAL_VALUE;
  10.764 -
  10.765 -    assert(session->messageToSend);
  10.766 -    if (!session->messageToSend) {
  10.767 -        status = PEP_SEND_FUNCTION_NOT_REGISTERED;
  10.768 -        goto error;
  10.769 -    }
  10.770 -
  10.771 -    msg->header.version.major = SYNC_VERSION_MAJOR;
  10.772 -    msg->header.version.minor = SYNC_VERSION_MINOR;
  10.773 -
  10.774 -    status = get_identity(session, partner->address, PEP_OWN_USERID, &me);
  10.775 -    if (status != PEP_STATUS_OK)
  10.776 -        goto error;
  10.777 -    
  10.778 -    int32_t seq = 0;
  10.779 -
  10.780 -    status = sequence_value(session, session->sync_session->sync_uuid, &seq);
  10.781 -    if (status != PEP_OWN_SEQUENCE && status != PEP_STATUS_OK)
  10.782 -        goto error;
  10.783 -
  10.784 -    msg->header.sequence = (long) seq;
  10.785 -
  10.786 -    _me = identity_dup(me);
  10.787 -    if (!_me)
  10.788 -        goto enomem;
  10.789 -
  10.790 -    free(_me->user_id);
  10.791 -    _me->user_id = strndup(session->sync_session->sync_uuid, 36);
  10.792 -    assert(_me->user_id);
  10.793 -    if (!_me->user_id)
  10.794 -        goto enomem;
  10.795 -
  10.796 -    if (Identity_from_Struct(_me, &msg->header.me) == NULL)
  10.797 -        goto enomem;
  10.798 -
  10.799 -    free_identity(_me);
  10.800 -    _me = NULL;
  10.801 -
  10.802 -    msg->header.state = (long) state;
  10.803 -
  10.804 -    bool devicegroup = deviceGrouped(session);
  10.805 -    if (devicegroup)
  10.806 -        msg->header.devicegroup = 1;
  10.807 -    else
  10.808 -        msg->header.devicegroup = 0;
  10.809 -
  10.810 -    if (asn_check_constraints(&asn_DEF_DeviceGroup_Protocol, msg, NULL, NULL)) {
  10.811 -        status = PEP_CONTRAINTS_VIOLATED;
  10.812 -        goto error;
  10.813 -    }
  10.814 -
  10.815 -    ssize_t size = uper_encode_to_new_buffer(&asn_DEF_DeviceGroup_Protocol,
  10.816 -            NULL, msg, (void **) &payload);
  10.817 -    if (size == -1) {
  10.818 -        status = PEP_CANNOT_ENCODE;
  10.819 -        goto error;
  10.820 -    }
  10.821 -
  10.822 -    status = prepare_message(me, partner, payload, size, &_message);
  10.823 -    if (status != PEP_STATUS_OK)
  10.824 -        goto error;
  10.825 -    payload = NULL;
  10.826 -    free_identity(me);
  10.827 -    me = NULL;
  10.828 -
  10.829 -#ifndef NDEBUG
  10.830 -    asn_enc_rval_t er;
  10.831 -    er = xer_encode(&asn_DEF_DeviceGroup_Protocol, msg, 
  10.832 -                    XER_F_BASIC, _append, &_message->longmsg);
  10.833 -    if(er.encoded == -1)
  10.834 -        goto error;
  10.835 -#endif
  10.836 -
  10.837 -    if (encrypted) {
  10.838 -        if (msg->payload.present == DeviceGroup_Protocol__payload_PR_groupKeys || 
  10.839 -            msg->payload.present == DeviceGroup_Protocol__payload_PR_groupUpdate) {
  10.840 -            PEP_rating rating = PEP_rating_undefined;
  10.841 -            status = outgoing_message_rating(session, _message, &rating);
  10.842 -            if (status != PEP_STATUS_OK)
  10.843 -                goto error;
  10.844 -            if (rating < PEP_rating_trusted) {
  10.845 -                status = PEP_SYNC_NO_TRUST;
  10.846 -                goto error;
  10.847 -            }
  10.848 -            
  10.849 -            stringlist_t *keylist = NULL;
  10.850 -            status = _own_keys_retrieve(session, &keylist, PEP_idf_not_for_sync);
  10.851 -            if (status != PEP_STATUS_OK)
  10.852 -                goto error;
  10.853 -
  10.854 -            for (stringlist_t *_keylist=keylist; _keylist!=NULL; _keylist=_keylist->next) {
  10.855 -                char *fpr = _keylist->value;
  10.856 -                static char filename[MAX_LINELENGTH];
  10.857 -                int result = snprintf(filename, MAX_LINELENGTH, "file://%s-sec.asc", fpr);
  10.858 -                if (result < 0)
  10.859 -                    goto enomem;
  10.860 -                char *key = NULL;
  10.861 -                size_t size = 0;
  10.862 -                status = export_secrect_key(session, fpr, &key, &size);
  10.863 -                if (status != PEP_STATUS_OK)
  10.864 -                    goto error;
  10.865 -                bloblist_t *bl = bloblist_add(_message->attachments,
  10.866 -                        (char *) key, size, "application/pgp-keys", filename);
  10.867 -                if (!bl)
  10.868 -                    goto enomem;
  10.869 -                if (!_message->attachments)
  10.870 -                    _message->attachments = bl;
  10.871 -            }
  10.872 -        }
  10.873 -
  10.874 -        message *_encrypted = NULL;
  10.875 -        status = encrypt_message(session, _message, NULL, &_encrypted, PEP_enc_PEP, 0);
  10.876 -        if (status != PEP_STATUS_OK)
  10.877 -            goto error;
  10.878 -        free_message(_message);
  10.879 -        _message = _encrypted;
  10.880 -    }
  10.881 -    else {
  10.882 -        attach_own_key(session, _message);
  10.883 -    }
  10.884 -
  10.885 -    status = session->messageToSend(session->sync_obj, _message);
  10.886 -    return status;
  10.887 -
  10.888 -enomem:
  10.889 -    status = PEP_OUT_OF_MEMORY;
  10.890 -error:
  10.891 -    free_identity(_me);
  10.892 -    free(payload);
  10.893 -    free_message(_message);
  10.894 -    free_identity(me);
  10.895 -    return status;
  10.896 -}
  10.897 -
  10.898 -PEP_STATUS multicast_self_msg(
  10.899 -        PEP_SESSION session,
  10.900 -        DeviceState_state state,
  10.901 -        DeviceGroup_Protocol_t *msg,
  10.902 -        bool encrypted
  10.903 -    )
  10.904 -{
  10.905 -    PEP_STATUS status = PEP_STATUS_OK;
  10.906 -
  10.907 -    assert(session && state && msg);
  10.908 -    if (!(session && state && msg))
  10.909 -        return PEP_ILLEGAL_VALUE;
  10.910 -
  10.911 -    identity_list *own_identities = NULL;
  10.912 -    status = _own_identities_retrieve(session, &own_identities, PEP_idf_not_for_sync);
  10.913 -    if (status != PEP_STATUS_OK)
  10.914 -        return status;
  10.915 -
  10.916 -    for (identity_list *_i = own_identities; _i && _i->ident; _i = _i->next) {
  10.917 -        pEp_identity *me = _i->ident;
  10.918 -
  10.919 -        // FIXME: no deep copy for multicast supported yet
  10.920 -        // DeviceGroup_Protocol_t *_msg = malloc(sizeof(DeviceGroup_Protocol_t));
  10.921 -        // assert(_msg);
  10.922 -        // if (_msg == NULL){
  10.923 -        //     status = PEP_OUT_OF_MEMORY;
  10.924 -        //     goto error;
  10.925 -        // }
  10.926 -        // memcpy(_msg, msg, sizeof(DeviceGroup_Protocol_t));
  10.927 -        status = unicast_msg(session, me, state, msg, encrypted);
  10.928 -        //status = unicast_msg(session, me, state, _msg, encrypted);
  10.929 -        //free_DeviceGroup_Protocol_msg(_msg);
  10.930 -    }
  10.931 -
  10.932 -    free_identity_list(own_identities);
  10.933 -    return PEP_STATUS_OK;
  10.934 -
  10.935 -// error:
  10.936 -//     free_identity_list(own_identities);
  10.937 -//     return status;
  10.938 -}
  10.939 -
  10.940 -void free_group_keys_extra(group_keys_extra_t* group_keys_extra)
  10.941 -{
  10.942 -    identity_list *group_keys = group_keys_extra->group_keys;
  10.943 -    char *group_id = group_keys_extra->group_id;
  10.944 -    free_identity_list(group_keys);
  10.945 -    free(group_id);
  10.946 -    free(group_keys_extra);
  10.947 -}
  10.948 -
  10.949 -group_keys_extra_t* group_keys_extra_dup(group_keys_extra_t* group_key_extra_src)
  10.950 -{
  10.951 -    group_keys_extra_t *group_key_extra_dst;
  10.952 -    group_key_extra_dst = calloc(1,sizeof(group_keys_extra_t));
  10.953 -    if(group_key_extra_dst == NULL){
  10.954 -        return NULL;
  10.955 -    }
  10.956 -
  10.957 -    char *group_id = strdup(group_key_extra_src->group_id);
  10.958 -
  10.959 -    if (group_key_extra_dst->group_id && !group_id){
  10.960 -        free(group_key_extra_dst);
  10.961 -        return NULL;
  10.962 -    }
  10.963 -    group_key_extra_dst->group_id = group_id;
  10.964 -
  10.965 -    identity_list *group_keys = identity_list_dup(group_key_extra_src->group_keys);;
  10.966 -    if (!group_keys) {
  10.967 -        free(group_id);
  10.968 -        free(group_key_extra_dst);
  10.969 -        return NULL;
  10.970 -    }
  10.971 -    group_key_extra_dst->group_keys = group_keys;
  10.972 -
  10.973 -    return group_key_extra_dst;
  10.974 -}
    11.1 --- a/src/sync_impl.h	Thu Nov 02 17:02:05 2017 +0100
    11.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
    11.3 @@ -1,64 +0,0 @@
    11.4 -// This file is under GNU General Public License 3.0
    11.5 -// see LICENSE.txt
    11.6 -
    11.7 -#pragma once
    11.8 -
    11.9 -#include "../asn.1/DeviceGroup-Protocol.h"
   11.10 -#include "message.h"
   11.11 -#include "sync.h"
   11.12 -#include "sync_fsm.h"
   11.13 -
   11.14 -#ifdef __cplusplus
   11.15 -extern "C" {
   11.16 -#endif
   11.17 -
   11.18 -typedef struct _group_keys_extra {
   11.19 -    identity_list *group_keys;
   11.20 -    char *group_id;
   11.21 -} group_keys_extra_t;
   11.22 -
   11.23 -void free_group_keys_extra(group_keys_extra_t* groupkeys);
   11.24 -group_keys_extra_t* group_keys_extra_dup(group_keys_extra_t* groupkeys);
   11.25 -
   11.26 -PEP_STATUS receive_sync_msg(
   11.27 -        PEP_SESSION session,
   11.28 -        sync_msg_t *sync_msg,
   11.29 -        time_t *timeout
   11.30 -    );
   11.31 -
   11.32 -PEP_STATUS inject_DeviceState_event(
   11.33 -    PEP_SESSION session, 
   11.34 -    DeviceState_event event,
   11.35 -    Identity partner,
   11.36 -    void *extra);
   11.37 -
   11.38 -PEP_STATUS receive_DeviceState_msg(
   11.39 -    PEP_SESSION session, 
   11.40 -    message *src, 
   11.41 -    PEP_rating rating, 
   11.42 -    stringlist_t *keylist);
   11.43 -
   11.44 -DeviceGroup_Protocol_t *new_DeviceGroup_Protocol_msg(DeviceGroup_Protocol__payload_PR type);
   11.45 -void free_DeviceGroup_Protocol_msg(DeviceGroup_Protocol_t *msg);
   11.46 -
   11.47 -PEP_STATUS unicast_msg(
   11.48 -        PEP_SESSION session,
   11.49 -        const Identity partner,
   11.50 -        DeviceState_state state,
   11.51 -        DeviceGroup_Protocol_t *msg,
   11.52 -        bool encrypted
   11.53 -    );
   11.54 -
   11.55 -PEP_STATUS multicast_self_msg(
   11.56 -        PEP_SESSION session,
   11.57 -        DeviceState_state state,
   11.58 -        DeviceGroup_Protocol_t *msg,
   11.59 -        bool encrypted
   11.60 -    );
   11.61 -
   11.62 -bool is_double(DeviceGroup_Protocol_t *msg);
   11.63 -
   11.64 -#ifdef __cplusplus
   11.65 -}
   11.66 -#endif
   11.67 -
    12.1 --- a/sync/Makefile	Thu Nov 02 17:02:05 2017 +0100
    12.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
    12.3 @@ -1,38 +0,0 @@
    12.4 -# Copyright 2017, pEp Foundation
    12.5 -# This file is part of pEpEngine
    12.6 -# This file may be used under the terms of the GNU General Public License version 3
    12.7 -# see LICENSE.txt
    12.8 -
    12.9 -include ../default.conf
   12.10 -
   12.11 -.PHONY: all
   12.12 -all: .codegen
   12.13 -
   12.14 -# Currently not in use, kept for historic reasons
   12.15 -skeleton: .actions
   12.16 -
   12.17 -.codegen: .statemachines .actions
   12.18 -	cp -f generated/*.* ../src
   12.19 -	touch $@
   12.20 -
   12.21 -.actions: devicegroup.fsm gen_actions.ysl2 fsm.yml2 functions.ysl2
   12.22 -	$(YML2_PROC) $(YML2_OPTS) -y gen_actions.ysl2 $< -o $@
   12.23 -
   12.24 -.statemachines: devicegroup.fsm gen_statemachine.ysl2 fsm.yml2 functions.ysl2
   12.25 -	$(YML2_PROC) $(YML2_OPTS) -y gen_statemachine.ysl2 $< -o $@
   12.26 -
   12.27 -.PHONY: clean
   12.28 -clean:
   12.29 -	rm -f *.xml *.xsl *.dot *.svg \
   12.30 -	$(patsubst generated/%,../src/%,$(wildcard generated/*.*)) \
   12.31 -	../generated/* ../skeletons/* .statemachines .actions .codegen \
   12.32 -	generated/Makefile.protocols
   12.33 -
   12.34 -%.xml: %.fsm
   12.35 -	$(YML2_PATH)/yml2c $< -o $@
   12.36 -
   12.37 -%.dot: gen_dot.ysl2 devicegroup.fsm
   12.38 -	$(YML2_PROC) $(YML2_OPTS) -y $^
   12.39 -
   12.40 -%.svg: %.dot
   12.41 -	dot -Tsvg -o $@ $<
    13.1 --- a/sync/devicegroup.fsm	Thu Nov 02 17:02:05 2017 +0100
    13.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
    13.3 @@ -1,354 +0,0 @@
    13.4 -// This file is under GNU General Public License 3.0
    13.5 -// see LICENSE.txt
    13.6 -
    13.7 -// DeviceGroup protocol for p≡p
    13.8 -
    13.9 -// Copyleft (c) 2016, p≡p foundation
   13.10 -
   13.11 -// Written by Volker Birk
   13.12 -
   13.13 -include ./fsm.yml2
   13.14 -
   13.15 -protocol DeviceGroup {
   13.16 -    // all messages have a timestamp, time out and are removed after timeout
   13.17 -
   13.18 -    broadcast sendBeacon;
   13.19 -    broadcast sendGroupUpdate;
   13.20 -    broadcast sendUpdateRequest;
   13.21 -    unencrypted sendBeacon;
   13.22 -
   13.23 -    fsm DeviceState filename=sync {
   13.24 -        condition deviceGrouped();
   13.25 -        condition keyElectionWon(Identity partner);
   13.26 -        condition sameIdentities(Identity a, Identity b);
   13.27 -        condition sameKeyAndAddress(Identity a, Identity b);
   13.28 -
   13.29 -        state InitState {
   13.30 -            on Init {
   13.31 -                if deviceGrouped()
   13.32 -                    go Grouped;
   13.33 -                go Sole;
   13.34 -            }
   13.35 -        }
   13.36 -
   13.37 -        state Sole end=1 {
   13.38 -            on KeyGen {
   13.39 -                do sendBeacon;
   13.40 -                go SoleWaiting;
   13.41 -            }
   13.42 -            on CannotDecrypt {
   13.43 -                do sendBeacon;
   13.44 -                go SoleWaiting;
   13.45 -            }
   13.46 -            on Beacon(Identity partner){
   13.47 -                do sendHandshakeRequest(partner);
   13.48 -                go SoleBeaconed(partner);
   13.49 -            }
   13.50 -            on HandshakeRequest(Identity partner) {
   13.51 -                do sendHandshakeRequest(partner);
   13.52 -                go HandshakingSole(partner);
   13.53 -            }
   13.54 -        }
   13.55 -
   13.56 -        // copy of sole state with a timeout to enable fast polling for a second
   13.57 -        // TODO use more YSLT power here (substates ?) 
   13.58 -        state SoleWaiting timeout=60 {
   13.59 -            on KeyGen {
   13.60 -                do sendBeacon;
   13.61 -            }
   13.62 -            on CannotDecrypt {
   13.63 -                do sendBeacon;
   13.64 -            }
   13.65 -            on Beacon(Identity partner){
   13.66 -                do sendHandshakeRequest(partner);
   13.67 -                go SoleBeaconed(partner);
   13.68 -            }
   13.69 -            on HandshakeRequest(Identity partner) {
   13.70 -                do sendHandshakeRequest(partner);
   13.71 -                go HandshakingSole(partner);
   13.72 -            }
   13.73 -            on Timeout go Sole;
   13.74 -        }
   13.75 -
   13.76 -        state SoleBeaconed timeout=600 (Identity expected) {
   13.77 -            on KeyGen{
   13.78 -                do sendBeacon;
   13.79 -                go Sole;
   13.80 -            }
   13.81 -            on CannotDecrypt{
   13.82 -                do sendBeacon;
   13.83 -                go Sole;
   13.84 -            }
   13.85 -            on Beacon(Identity partner) {
   13.86 -                do sendHandshakeRequest(partner);
   13.87 -                go SoleBeaconed(partner);
   13.88 -            }
   13.89 -            on HandshakeRequest(Identity partner) {
   13.90 -                if sameIdentities(partner, expected) {
   13.91 -                    // do nothing, to avoid sending handshake request twice 
   13.92 -                } else {
   13.93 -                    do sendHandshakeRequest(partner);
   13.94 -                }
   13.95 -                go HandshakingSole(partner);
   13.96 -            }
   13.97 -            on Timeout go Sole;
   13.98 -        }
   13.99 -
  13.100 -        state HandshakingSole timeout=600 (Identity expected) {
  13.101 -            on Init{
  13.102 -                if keyElectionWon(expected) {
  13.103 -                    do notifyInitFormGroup(expected);
  13.104 -                } else {
  13.105 -                    do notifyInitAddOurDevice(expected);
  13.106 -                }
  13.107 -            }
  13.108 -            on HandshakeRejected(Identity partner) {
  13.109 -                do rejectHandshake(partner);
  13.110 -                go Sole;
  13.111 -            }
  13.112 -            on HandshakeAccepted(Identity partner) {
  13.113 -                if sameIdentities(partner, expected) {
  13.114 -                    do acceptHandshake(partner); 
  13.115 -                    if keyElectionWon(partner) {
  13.116 -                        do makeGroup;
  13.117 -                        do sendGroupKeys(partner);
  13.118 -                        do renewUUID;
  13.119 -                        do notifyAcceptedGroupCreated(partner);
  13.120 -                        go Grouped;
  13.121 -                    }
  13.122 -                    go WaitForGroupKeysSole(partner);
  13.123 -                }
  13.124 -                go Sole;
  13.125 -            }
  13.126 -            on Cancel go Sole;
  13.127 -            on GroupKeys(Identity partner, GroupKeys groupkeys) {
  13.128 -                if keyElectionWon(expected) {
  13.129 -                    // not supposed to receive groupkeys - ignore
  13.130 -                } else {
  13.131 -                    // UUID changes in between, so we can only check for same address and fpr
  13.132 -                    if sameKeyAndAddress(partner, expected) {
  13.133 -                        go WaitForAcceptSole(partner, groupkeys);
  13.134 -                    }
  13.135 -                }
  13.136 -            }
  13.137 -            on Timeout {
  13.138 -                do notifyTimeout(expected);
  13.139 -                do sendBeacon;
  13.140 -                go Sole;
  13.141 -            }
  13.142 -        }
  13.143 -    
  13.144 -        state WaitForGroupKeysSole timeout=600 (Identity expected) {
  13.145 -            on GroupKeys(Identity partner, GroupKeys groupkeys) {
  13.146 -                // UUID changes in between, so we can only check for same address and fpr
  13.147 -                if sameKeyAndAddress(partner, expected) {
  13.148 -                    do storeGroupKeys(partner, groupkeys);
  13.149 -                    do sendGroupUpdate;
  13.150 -                    do renewUUID;
  13.151 -                    do notifyAcceptedDeviceAdded(partner);
  13.152 -                    go Grouped;
  13.153 -                }
  13.154 -            }
  13.155 -            on Timeout {
  13.156 -                do notifyTimeout(expected);
  13.157 -                go Sole;
  13.158 -            }
  13.159 -        }
  13.160 -
  13.161 -        state WaitForAcceptSole timeout=600 (Identity expected, GroupKeys groupkeys) {
  13.162 -            on HandshakeRejected(Identity partner) {
  13.163 -                do rejectHandshake(partner);
  13.164 -                go Sole;
  13.165 -            }
  13.166 -            on HandshakeAccepted(Identity partner) {
  13.167 -                // UUID changes in between, so we can only check for same address and fpr
  13.168 -                if sameKeyAndAddress(partner, expected) {
  13.169 -                    do acceptHandshake(partner); 
  13.170 -                    do storeGroupKeys(partner, groupkeys);
  13.171 -                    do sendGroupUpdate;
  13.172 -                    do renewUUID;
  13.173 -                    do notifyAcceptedDeviceAdded(partner);
  13.174 -                    go Grouped;
  13.175 -                }
  13.176 -                go Sole;
  13.177 -            }
  13.178 -            on Cancel go Sole;
  13.179 -            on Timeout {
  13.180 -                do notifyTimeout(expected);
  13.181 -                go Sole;
  13.182 -            }
  13.183 -        }
  13.184 -
  13.185 -        state Grouped end=1 {
  13.186 -            on KeyGen
  13.187 -                do sendGroupUpdate;
  13.188 -            on CannotDecrypt {
  13.189 -                do sendUpdateRequest;
  13.190 -                do sendBeacon;
  13.191 -                go GroupWaiting;
  13.192 -            }
  13.193 -            on UpdateRequest
  13.194 -                do sendGroupUpdate;
  13.195 -            on Beacon(Identity partner){
  13.196 -                do sendHandshakeRequest(partner);
  13.197 -                go GroupedBeaconed(partner);
  13.198 -            }
  13.199 -            on HandshakeRequest(Identity partner) {
  13.200 -                do sendHandshakeRequest(partner);
  13.201 -                go HandshakingGrouped(partner);
  13.202 -            }
  13.203 -            on GroupUpdate(Identity partner, IdentityList keys)
  13.204 -                do storeGroupUpdate(partner, keys);
  13.205 -        }
  13.206 -
  13.207 -        // copy of grouped state, with a timeout to enable fast poling for a minut
  13.208 -        state GroupWaiting timeout=60 {
  13.209 -            on KeyGen
  13.210 -                do sendGroupUpdate;
  13.211 -            on CannotDecrypt {
  13.212 -                do sendUpdateRequest;
  13.213 -                do sendBeacon;
  13.214 -            }
  13.215 -            on UpdateRequest
  13.216 -                do sendGroupUpdate;
  13.217 -            on Beacon(Identity partner){
  13.218 -                do sendHandshakeRequest(partner);
  13.219 -                go GroupedBeaconed(partner);
  13.220 -            }
  13.221 -            on HandshakeRequest(Identity partner) {
  13.222 -                do sendHandshakeRequest(partner);
  13.223 -                go HandshakingGrouped(partner);
  13.224 -            }
  13.225 -            on GroupUpdate(Identity partner, IdentityList keys)
  13.226 -                do storeGroupUpdate(partner, keys);
  13.227 -            on Timeout go Grouped;
  13.228 -        }
  13.229 -
  13.230 -        state GroupedBeaconed timeout=600 (Identity expected){
  13.231 -            on KeyGen
  13.232 -                do sendGroupUpdate;
  13.233 -            on CannotDecrypt {
  13.234 -                do sendUpdateRequest;
  13.235 -                do sendBeacon;
  13.236 -            }
  13.237 -            on UpdateRequest
  13.238 -                do sendGroupUpdate;
  13.239 -            on Beacon(Identity partner){
  13.240 -                do sendHandshakeRequest(partner);
  13.241 -                go GroupedBeaconed(partner);
  13.242 -            }
  13.243 -            on HandshakeRequest(Identity partner) {
  13.244 -                if sameIdentities(partner, expected) {
  13.245 -                    // do nothing, to avoid sending handshake request twice 
  13.246 -                } else {
  13.247 -                    do sendHandshakeRequest(partner);
  13.248 -                }
  13.249 -                go HandshakingGrouped(partner);
  13.250 -            }
  13.251 -            on GroupUpdate(Identity partner, IdentityList keys)
  13.252 -                do storeGroupUpdate(partner, keys);
  13.253 -            on Timeout go Grouped;
  13.254 -        }
  13.255 -
  13.256 -        state HandshakingGrouped timeout=600 (Identity expected) {
  13.257 -            // HandshakeRequest from same group are filtered in receive_sync_msg
  13.258 -            on Init{
  13.259 -                if keyElectionWon(expected) {
  13.260 -                    do notifyInitAddOtherDevice(partner);
  13.261 -                } else {
  13.262 -                    do notifyInitMoveOurDevice(partner);
  13.263 -                }
  13.264 -            }
  13.265 -            on HandshakeRejected(Identity partner) {
  13.266 -                do rejectHandshake(partner);             // stores rejection of partner
  13.267 -                do sendGroupUpdate;
  13.268 -                go Grouped;
  13.269 -            }
  13.270 -            on HandshakeAccepted(Identity partner) {
  13.271 -                do acceptHandshake(partner); 
  13.272 -                do sendGroupUpdate;
  13.273 -                if keyElectionWon(partner) {
  13.274 -                    do sendGroupKeys(partner);
  13.275 -                    do notifyAcceptedDeviceAdded(partner);
  13.276 -                    go Grouped;
  13.277 -                }
  13.278 -                go WaitForGroupKeysGrouped(partner);
  13.279 -            }
  13.280 -            on Cancel go Grouped;
  13.281 -            on GroupKeys(Identity partner, GroupKeys groupkeys) {
  13.282 -                if keyElectionWon(expected) {
  13.283 -                    // not supposed to receive groupkeys - ignore
  13.284 -                } else {
  13.285 -                    // UUID changes in between, so we can only check for same address and fpr
  13.286 -                    if sameKeyAndAddress(partner, expected) {
  13.287 -                        go WaitForAcceptGrouped(partner, groupkeys);
  13.288 -                    }
  13.289 -                }
  13.290 -            }
  13.291 -            on GroupUpdate(Identity partner, IdentityList keys) {
  13.292 -                do notifyOvertaken(partner);
  13.293 -                do storeGroupUpdate(partner, keys);
  13.294 -                go Grouped;
  13.295 -            }
  13.296 -            on Timeout {
  13.297 -                do notifyTimeout(expected);
  13.298 -                go Grouped;
  13.299 -            }
  13.300 -        }
  13.301 -
  13.302 -        state WaitForGroupKeysGrouped timeout=600 (Identity expected) {
  13.303 -            on GroupKeys(Identity partner, GroupKeys groupkeys) {
  13.304 -                if sameIdentities(partner, expected) {
  13.305 -                    do storeGroupKeys(partner, groupkeys);
  13.306 -                    do sendGroupUpdate;
  13.307 -                    do renewUUID;
  13.308 -                    do notifyAcceptedDeviceMoved(partner);
  13.309 -                    go Grouped;
  13.310 -                }
  13.311 -            }
  13.312 -            on GroupUpdate(Identity partner, IdentityList keys) {
  13.313 -                do notifyOvertaken(partner);
  13.314 -                do storeGroupUpdate(partner, keys);
  13.315 -                go Grouped;
  13.316 -            }
  13.317 -            on Timeout {
  13.318 -                do notifyTimeout(expected);
  13.319 -                go Grouped;
  13.320 -            }
  13.321 -        }
  13.322 -
  13.323 -        state WaitForAcceptGrouped timeout=600 (Identity expected, GroupKeys groupkeys) {
  13.324 -            on HandshakeRejected(Identity partner) {
  13.325 -                do rejectHandshake(partner);
  13.326 -                do sendGroupUpdate;
  13.327 -                go Grouped;
  13.328 -            }
  13.329 -            on HandshakeAccepted(Identity partner) {
  13.330 -                if sameIdentities(partner, expected) {
  13.331 -                    do acceptHandshake(partner); 
  13.332 -                    do storeGroupKeys(partner, groupkeys);
  13.333 -                    do sendGroupUpdate;
  13.334 -                    do renewUUID;
  13.335 -                    do notifyAcceptedDeviceMoved(partner);
  13.336 -                }
  13.337 -                go Grouped;
  13.338 -            }
  13.339 -            on Cancel go Grouped;
  13.340 -            on GroupUpdate(Identity partner, IdentityList keys) {
  13.341 -                do notifyOvertaken(partner);
  13.342 -                do storeGroupUpdate(partner, keys);
  13.343 -                go Grouped;
  13.344 -            }
  13.345 -            on Timeout {
  13.346 -                do notifyTimeout(expected);
  13.347 -                go Grouped;
  13.348 -            }
  13.349 -        }
  13.350 -
  13.351 -        tag Init 1;
  13.352 -        tag Beacon 2;
  13.353 -        tag HandshakeRequest 3;
  13.354 -        tag GroupKeys 4;
  13.355 -    }
  13.356 -}
  13.357 -
    14.1 --- a/sync/fsm.yml2	Thu Nov 02 17:02:05 2017 +0100
    14.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
    14.3 @@ -1,19 +0,0 @@
    14.4 -// This file is under GNU General Public License 3.0
    14.5 -// see LICENSE.txt
    14.6 -
    14.7 -// FSM Y language 1.0
    14.8 -
    14.9 -// Copyleft (c) 2016, p≡p foundation
   14.10 -
   14.11 -// Written by Volker Birk
   14.12 -
   14.13 -decl protocol @name;
   14.14 -decl fsm @name;
   14.15 -decl state @name (timeout=0);
   14.16 -decl event @name, on is event;
   14.17 -decl transition @target, go is transition;
   14.18 -decl action @name, do is action;
   14.19 -decl condition @name, if is condition;
   14.20 -decl alternative, else is alternative;
   14.21 -decl interface @name;
   14.22 -decl tag @name (id);
    15.1 --- a/sync/functions.ysl2	Thu Nov 02 17:02:05 2017 +0100
    15.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
    15.3 @@ -1,15 +0,0 @@
    15.4 -// This file is under GNU General Public License 3.0
    15.5 -// see LICENSE.txt
    15.6 -
    15.7 -// function library
    15.8 -
    15.9 -def "func:distinctName" {
   15.10 -    param "nodes", "/..";
   15.11 -    choose {
   15.12 -        when "not($nodes)"
   15.13 -            result "/..";
   15.14 -        otherwise {
   15.15 -            result "$nodes[1] | func:distinctName($nodes[position() > 1])[@name != $nodes[1]/@name]";
   15.16 -        }
   15.17 -    }
   15.18 -}
    16.1 --- a/sync/gen_actions.ysl2	Thu Nov 02 17:02:05 2017 +0100
    16.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
    16.3 @@ -1,300 +0,0 @@
    16.4 -// This file is under GNU General Public License 3.0
    16.5 -// see LICENSE.txt
    16.6 -
    16.7 -// generate actions skeleton
    16.8 -
    16.9 -// Copyleft (c) 2016, p≡p foundation
   16.10 -
   16.11 -// Written by Volker Birk
   16.12 -
   16.13 -include yslt.yml2
   16.14 -
   16.15 -tstylesheet {
   16.16 -    include standardlib.ysl2
   16.17 -    include ./functions.ysl2
   16.18 -
   16.19 -    template "/protocol" {
   16.20 -        apply "fsm", mode=send, 0;
   16.21 -        apply "fsm", mode=other, 0;
   16.22 -    }
   16.23 -
   16.24 -    template "fsm", mode=send document "generated/{@filename}_send_actions.c", "text" {
   16.25 -        const "name", "@name";
   16.26 -        const "filename", "@filename";
   16.27 -        ||
   16.28 -        // Send Actions for «@name» state machine
   16.29 -
   16.30 -        #include <assert.h>
   16.31 -        #include "pEp_internal.h"
   16.32 -        #include "keymanagement.h"
   16.33 -        #include "message.h"
   16.34 -        #include "«@filename»_fsm.h"
   16.35 -        #include "baseprotocol.h"
   16.36 -        #include "map_asn1.h"
   16.37 -        #include "../asn.1/DeviceGroup-Protocol.h"
   16.38 -        #include "sync_impl.h"
   16.39 -        ||
   16.40 -        for "func:distinctName(//action)"
   16.41 -            if "substring(@name, 1, 4) = 'send'"
   16.42 -                | #include "../asn.1/«substring(@name, 5, 255)».h"
   16.43 -        |
   16.44 -        for "func:distinctName(//action)"
   16.45 -            if "substring(@name, 1, 4) = 'send'"
   16.46 -                call "send_action"
   16.47 -                    with "action", ".",
   16.48 -                    with "fsm", "$name",
   16.49 -                    with "filename", "$filename";
   16.50 -
   16.51 -        ||
   16.52 -
   16.53 -        PEP_STATUS _notifyHandshake(
   16.54 -                PEP_SESSION session,
   16.55 -                Identity partner,
   16.56 -                sync_handshake_signal signal
   16.57 -            );
   16.58 -        ||
   16.59 -
   16.60 -        for "func:distinctName(//action)"
   16.61 -            if "substring(@name, 1, 6) = 'notify'"
   16.62 -                call "notify_action"
   16.63 -                    with "action", ".",
   16.64 -                    with "fsm", "$name",
   16.65 -                    with "filename", "$filename";
   16.66 -    }
   16.67 -
   16.68 -    template "fsm", mode=other document "skeletons/{@filename}_actions.c", "text" {
   16.69 -        const "name", "@name";
   16.70 -        const "filename", "@filename";
   16.71 -        ||
   16.72 -        // Actions for «@name» state machine
   16.73 -
   16.74 -        #include <assert.h>
   16.75 -        #include "pEp_internal.h"
   16.76 -        #include "keymanagement.h"
   16.77 -        #include "message.h"
   16.78 -        #include "«@filename»_fsm.h"
   16.79 -        #include "../asn.1/DeviceGroup-Protocol.h"
   16.80 -
   16.81 -        ||
   16.82 -        for "func:distinctName(//action)"
   16.83 -            if "substring(@name, 1, 4) != 'send'"
   16.84 -                call "other_action"
   16.85 -                    with "action", ".",
   16.86 -                    with "fsm", "$name",
   16.87 -                    with "filename", "$filename";
   16.88 -    }
   16.89 -
   16.90 -    function "paramcheck" {
   16.91 -        param "partner";
   16.92 -        |> assert(session);
   16.93 -        choose {
   16.94 -            when "$partner"
   16.95 -            ||
   16.96 -                assert(partner);
   16.97 -                if (!(session && partner))
   16.98 -                    return PEP_ILLEGAL_VALUE;
   16.99 -            ||
  16.100 -            otherwise
  16.101 -            ||
  16.102 -                assert(!partner);
  16.103 -                if (!(session && !partner))
  16.104 -                    return PEP_ILLEGAL_VALUE;
  16.105 -            ||
  16.106 -        }
  16.107 -    }
  16.108 -
  16.109 -    function "other_action" {
  16.110 -        param "action";
  16.111 -        param "fsm";
  16.112 -        param "filename", "'###'";
  16.113 -
  16.114 -        ||
  16.115 -
  16.116 -        // «$action/@name»() - 
  16.117 -        //
  16.118 -        //  params:
  16.119 -        //      session (in)        session handle
  16.120 -        //      state (in)          state the state machine is in
  16.121 -        `` if "parm"        | //      partner (in)        partner to communicate with
  16.122 -        `` if "not(parm)"   | //      partner (in)        (must be NULL)
  16.123 -        //
  16.124 -        //  returns:
  16.125 -        //      PEP_STATUS_OK or any other value on error
  16.126 -
  16.127 -        PEP_STATUS «$action/@name»(
  16.128 -                PEP_SESSION session,
  16.129 -                «$fsm»_state state,
  16.130 -                Identity partner,
  16.131 -                void *extra
  16.132 -            )
  16.133 -        {
  16.134 -            PEP_STATUS status = PEP_STATUS_OK;
  16.135 -
  16.136 -            `` call "paramcheck" with "partner", "parm/partner";
  16.137 -
  16.138 -            // working code
  16.139 -
  16.140 -            // free extra
  16.141 -            return status;
  16.142 -
  16.143 -        enomem:
  16.144 -            status = PEP_OUT_OF_MEMORY;
  16.145 -        error:
  16.146 -            // free extra
  16.147 -            return status;
  16.148 -        }
  16.149 -
  16.150 -        ||
  16.151 -    }
  16.152 -
  16.153 -    function "send_action" {
  16.154 -        param "action";
  16.155 -        param "fsm";
  16.156 -        param "filename", "'###'";
  16.157 -        const "name", "substring($action/@name, 5, 255)";
  16.158 -        const "lname", "concat(yml:lcase(substring($name, 1, 1)), substring($name, 2))";
  16.159 -
  16.160 -        ||
  16.161 -
  16.162 -        // «$action/@name»() - send «$name» message
  16.163 -        //
  16.164 -        //  params:
  16.165 -        //      session (in)        session handle
  16.166 -        //      state (in)          state the state machine is in
  16.167 -        `` if "parm"        | //      partner (in)        partner to communicate with
  16.168 -        `` if "not(parm)"   | //      partner (in)        (must be NULL)
  16.169 -        //
  16.170 -        //  returns:
  16.171 -        //      PEP_STATUS_OK or any other value on error
  16.172 -
  16.173 -        PEP_STATUS «$action/@name»(
  16.174 -                PEP_SESSION session,
  16.175 -                «$fsm»_state state,
  16.176 -                Identity partner,
  16.177 -                void *extra
  16.178 -            )
  16.179 -        {
  16.180 -            assert(session && state);
  16.181 -            if (!(session && state))
  16.182 -                return PEP_ILLEGAL_VALUE;
  16.183 -
  16.184 -            PEP_STATUS status = PEP_STATUS_OK;
  16.185 -            `` if "$name='GroupKeys' or $name='GroupUpdate'" |> identity_list *kl = new_identity_list(NULL);
  16.186 -
  16.187 -            DeviceGroup_Protocol_t *msg = new_DeviceGroup_Protocol_msg(DeviceGroup_Protocol__payload_PR_«$lname»);
  16.188 -            if (!msg)
  16.189 -                goto enomem;
  16.190 -        ||
  16.191 -        choose {
  16.192 -            when "$name='GroupKeys' or $name='GroupUpdate'" {
  16.193 -                |
  16.194 -                |> status = _own_identities_retrieve(session, &kl, PEP_idf_not_for_sync);
  16.195 -                |> if (status != PEP_STATUS_OK)
  16.196 -                |>> goto error;
  16.197 -                |> if (IdentityList_from_identity_list(kl, &msg->payload.choice.«$lname».ownIdentities) == NULL)
  16.198 -                |>> goto enomem;
  16.199 -            }
  16.200 -        }
  16.201 -        choose {
  16.202 -            when "$name='GroupKeys' or $name='HandshakeRequest'" {
  16.203 -                |
  16.204 -                |> msg->payload.choice.«$lname».partner_id = 
  16.205 -                |>     OCTET_STRING_new_fromBuf(&asn_DEF_UTF8String,
  16.206 -                |>                              partner->user_id, -1);
  16.207 -                |> if (partner->user_id && !msg->payload.choice.«$lname».partner_id)
  16.208 -                |>    goto enomem;
  16.209 -                |
  16.210 -                |> char *devgrp = NULL;
  16.211 -                |> status = get_device_group(session, &devgrp);
  16.212 -                |> if (status == PEP_STATUS_OK && devgrp && devgrp[0])
  16.213 -                |> msg->payload.choice.«$lname».group_id = 
  16.214 -                |>     OCTET_STRING_new_fromBuf(&asn_DEF_UTF8String,
  16.215 -                |>                              devgrp, -1);
  16.216 -                |> free(devgrp);
  16.217 -                |> if (devgrp && !msg->payload.choice.«$lname».partner_id)
  16.218 -                |>    goto enomem;
  16.219 -            }
  16.220 -        }
  16.221 -        ||
  16.222 -
  16.223 -        ||
  16.224 -        choose {
  16.225 -            when "count(/protocol/unencrypted/*[name()=$action/@name]) = 0"
  16.226 -                |> bool encrypted = true;
  16.227 -            otherwise
  16.228 -                |> bool encrypted = false;
  16.229 -        }
  16.230 -        choose {
  16.231 -            when "count(/protocol/broadcast/*[name()=$action/@name]) = 0"
  16.232 -                |> status = unicast_msg(session, partner, state, msg, encrypted);
  16.233 -            otherwise
  16.234 -                |> status = multicast_self_msg(session, state, msg, encrypted);
  16.235 -        }
  16.236 -        ||
  16.237 -            if (status != PEP_STATUS_OK)
  16.238 -                goto error;
  16.239 -
  16.240 -            `` if "$name='GroupKeys' or $name='GroupUpdate'" |> free_identity_list(kl);
  16.241 -            free_DeviceGroup_Protocol_msg(msg);
  16.242 -            return PEP_STATUS_OK;
  16.243 -
  16.244 -        enomem:
  16.245 -            status = PEP_OUT_OF_MEMORY;
  16.246 -        error:
  16.247 -            free_DeviceGroup_Protocol_msg(msg);
  16.248 -            `` if "$name='GroupKeys'" |> free_identity_list(kl);
  16.249 -            return status;
  16.250 -        }
  16.251 -
  16.252 -        ||
  16.253 -    }
  16.254 -
  16.255 -    function "UnCamelUp" {
  16.256 -        param "text";
  16.257 -        const "tokens", "str:tokenize($text, '')";
  16.258 -
  16.259 -        for "$tokens" {
  16.260 -            choose {
  16.261 -                when "contains('ABCDEFGHIJKLMNOPQRSTUVWXYZ',.)" > _«.»
  16.262 -                otherwise value "yml:ucase(.)";
  16.263 -            }
  16.264 -        }
  16.265 -    }
  16.266 -
  16.267 -    function "notify_action" {
  16.268 -        param "action";
  16.269 -        param "fsm";
  16.270 -        param "filename", "'###'";
  16.271 -        const "name", "substring($action/@name, 7, 255)";
  16.272 -        const "uname" call "UnCamelUp" with "text", "$name";
  16.273 -        ||
  16.274 -
  16.275 -        // «$action/@name»() - notify «$name» to app
  16.276 -        //
  16.277 -        //  params:
  16.278 -        //      session (in)        session handle
  16.279 -        //      state (in)          state the state machine is in
  16.280 -        //      partner (in)        partner to communicate with
  16.281 -        //
  16.282 -        //  returns:
  16.283 -        //      PEP_STATUS_OK or any other value on error
  16.284 -
  16.285 -        PEP_STATUS «$action/@name»(
  16.286 -                PEP_SESSION session,
  16.287 -                «$fsm»_state state,
  16.288 -                Identity partner,
  16.289 -                void *extra
  16.290 -            )
  16.291 -        {
  16.292 -            assert(session && state);
  16.293 -            assert(extra == NULL);
  16.294 -            if (!(session && state && extra == NULL))
  16.295 -                return PEP_ILLEGAL_VALUE;
  16.296 -
  16.297 -            return _notifyHandshake(session, partner, SYNC_NOTIFY«$uname»);
  16.298 -        }
  16.299 -
  16.300 -        ||
  16.301 -    }
  16.302 -}
  16.303 -
    17.1 --- a/sync/gen_dot.ysl2	Thu Nov 02 17:02:05 2017 +0100
    17.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
    17.3 @@ -1,42 +0,0 @@
    17.4 -// This file is under GNU General Public License 3.0
    17.5 -// see LICENSE.txt
    17.6 -
    17.7 -include yslt.yml2
    17.8 -
    17.9 -tstylesheet {
   17.10 -    template "protocol/fsm" document "{@filename}.dot", "text"
   17.11 -    ||
   17.12 -    digraph finite_state_machine {
   17.13 -        rankdir=LR;
   17.14 -        node [shape = doublecircle];
   17.15 -        `apply "state[@end='1']" mode=end`;
   17.16 -        node [shape = circle];
   17.17 -
   17.18 -        `` apply "state" mode=do
   17.19 -    }
   17.20 -    ||
   17.21 -
   17.22 -    template "state", mode=end > «@name» 
   17.23 -    template "state", mode=do apply "event", 0 with "state", "@name";
   17.24 -
   17.25 -    template "event" {
   17.26 -        param "state";
   17.27 -        const "transitions", "transition|descendant::condition/transition|descendant::alternative/transition";
   17.28 -        choose {
   17.29 -            when "count($transitions) > 0"
   17.30 -                apply "$transitions", 0
   17.31 -                    with "state", "$state", with "event", "@name";
   17.32 -            otherwise
   17.33 -                if "@name != 'Init'"
   17.34 -                    | «$state» -> «$state» [ label="«@name»" ];
   17.35 -        }
   17.36 -    }
   17.37 -
   17.38 -    template "transition" {
   17.39 -        param "state";
   17.40 -        param "event";
   17.41 -
   17.42 -        | «$state» -> «@target» [ label ="«$event»" ];
   17.43 -    }
   17.44 -}
   17.45 -
    18.1 --- a/sync/gen_statemachine.ysl2	Thu Nov 02 17:02:05 2017 +0100
    18.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
    18.3 @@ -1,415 +0,0 @@
    18.4 -// This file is under GNU General Public License 3.0
    18.5 -// see LICENSE.txt
    18.6 -
    18.7 -// generate state machine code
    18.8 -
    18.9 -// Copyleft (c) 2016, p≡p foundation
   18.10 -
   18.11 -// Written by Volker Birk
   18.12 -
   18.13 -include yslt.yml2
   18.14 -
   18.15 -tstylesheet {
   18.16 -    include standardlib.ysl2
   18.17 -    include ./functions.ysl2
   18.18 -
   18.19 -    template "/protocol" {
   18.20 -        document "generated/Makefile.protocols", "text"
   18.21 -            apply "fsm", 0, mode="make";
   18.22 -        apply "fsm", 0, mode=gen;
   18.23 -    }
   18.24 -
   18.25 -    template "fsm", mode=make
   18.26 -    ||
   18.27 -    «@filename»_fsm.c: ../sync/devicegroup.fsm
   18.28 -    \tmake -C ../«@filename»
   18.29 -    ||
   18.30 -
   18.31 -    template "fsm", mode=gen {
   18.32 -        document "generated/{@filename}_fsm.h", "text" {
   18.33 -        ||
   18.34 -        #pragma once
   18.35 -
   18.36 -        // state machine for «@name»
   18.37 -
   18.38 -        #include "message_api.h"
   18.39 -        
   18.40 -        #ifdef __cplusplus
   18.41 -        extern "C" {
   18.42 -        #endif
   18.43 -
   18.44 -        // types
   18.45 -
   18.46 -        typedef pEp_identity * Identity;
   18.47 -        typedef stringlist_t * Stringlist;
   18.48 -
   18.49 -        // error values
   18.50 -
   18.51 -        typedef enum _fsm_error {
   18.52 -            // these error values are corresponding to
   18.53 -            // PEP_SYNC_STATEMACHINE_ERROR - value
   18.54 -            invalid_state = -2,
   18.55 -            invalid_event = -3,
   18.56 -            invalid_condition = -4,
   18.57 -            invalid_action = -5,
   18.58 -
   18.59 -            // out of memory condition
   18.60 -            invalid_out_of_memory = -128
   18.61 -        } fsm_error;
   18.62 -
   18.63 -        // conditions
   18.64 -
   18.65 -        `` for "func:distinctName(condition)" | int «@name»(PEP_SESSION session`apply "parm", 0`);
   18.66 -
   18.67 -        // states
   18.68 -
   18.69 -        typedef enum _«@name»_state {
   18.70 -            // error values also in this namespace
   18.71 -            «@name»_state_invalid_state = (int) invalid_state,
   18.72 -            «@name»_state_invalid_event = (int) invalid_event,
   18.73 -            «@name»_state_invalid_condition = (int) invalid_condition,
   18.74 -            «@name»_state_invalid_action = (int) invalid_action,
   18.75 -            «@name»_state_invalid_out_of_memory = (int) invalid_out_of_memory,
   18.76 -
   18.77 -            «@name»_state_NONE = 0,
   18.78 -        `` for "func:distinctName(state)" |> «@name»`if "position()!=last()" > , `
   18.79 -        } «@name»_state;
   18.80 -
   18.81 -        // events
   18.82 -
   18.83 -        typedef enum _«@name»_event {
   18.84 -            «@name»_event_NONE = 0,
   18.85 -        ||
   18.86 -        for "func:distinctName(state/event[not(not(/protocol/fsm/tag/@name=@name))])" {
   18.87 -            const "name", "@name";
   18.88 -            |> «$name» = «/protocol/fsm/tag[@name=$name]/@id»,
   18.89 -        }
   18.90 -        for "func:distinctName(state/event[not(/protocol/fsm/tag/@name=@name)])"
   18.91 -            |> «@name»`if "position()!=last()" > , `
   18.92 -        ||
   18.93 -        } «@name»_event;
   18.94 -
   18.95 -        // actions
   18.96 -
   18.97 -        `` const "name", "@name"
   18.98 -        `` for "func:distinctName(//action)" | PEP_STATUS «@name»(PEP_SESSION session, «$name»_state state, Identity partner, void *extra);
   18.99 -
  18.100 -        // event injector
  18.101 -
  18.102 -        PEP_STATUS inject_DeviceState_event(
  18.103 -            PEP_SESSION session, 
  18.104 -            DeviceState_event event,
  18.105 -            Identity partner,
  18.106 -            void *extra);
  18.107 -
  18.108 -        // message receiver
  18.109 -        
  18.110 -        PEP_STATUS receive_DeviceState_msg(
  18.111 -                PEP_SESSION session, 
  18.112 -                message *src, 
  18.113 -                PEP_rating rating, 
  18.114 -                stringlist_t *keylist
  18.115 -            );
  18.116 -
  18.117 -        // state machine
  18.118 -
  18.119 -        «@name»_state fsm_«@name»(
  18.120 -                PEP_SESSION session,
  18.121 -                «@name»_state state,
  18.122 -                «@name»_event event,
  18.123 -                Identity partner,
  18.124 -                void *extra,
  18.125 -                time_t *timeout
  18.126 -            );
  18.127 -
  18.128 -        // driver
  18.129 -
  18.130 -        DYNAMIC_API PEP_STATUS fsm_«@name»_inject(
  18.131 -                PEP_SESSION session,
  18.132 -                «@name»_event event,
  18.133 -                Identity partner,
  18.134 -                void *extra,
  18.135 -                time_t *timeout
  18.136 -            );
  18.137 -
  18.138 -        #ifdef __cplusplus
  18.139 -        }
  18.140 -        #endif
  18.141 -
  18.142 -        ||
  18.143 -        }
  18.144 -        document "generated/{@filename}_driver.c", "text"
  18.145 -        ||
  18.146 -        // Driver for «@name» state machine
  18.147 -
  18.148 -        #include <assert.h>
  18.149 -        #include "pEp_internal.h"
  18.150 -
  18.151 -
  18.152 -        DYNAMIC_API PEP_STATUS fsm_«@name»_inject(
  18.153 -                PEP_SESSION session,
  18.154 -                «@name»_event event,
  18.155 -                Identity partner,
  18.156 -                void *extra,
  18.157 -                time_t *timeout
  18.158 -            )
  18.159 -        {
  18.160 -            assert(session);
  18.161 -            if (!session)
  18.162 -                return PEP_ILLEGAL_VALUE;
  18.163 -
  18.164 -            while(true)
  18.165 -            {
  18.166 -                «@name»_state new_state = fsm_«@name»(session,
  18.167 -                    session->«@filename»_state, event, partner, extra, timeout);
  18.168 -
  18.169 -                if (new_state == «@name»_state_invalid_out_of_memory)
  18.170 -                    return PEP_OUT_OF_MEMORY;
  18.171 -
  18.172 -                if (new_state < 0)
  18.173 -                    return PEP_SYNC_STATEMACHINE_ERROR - new_state;
  18.174 -                
  18.175 -                if (new_state == session->«@filename»_state)
  18.176 -                    break;
  18.177 -
  18.178 -                event = Init;
  18.179 -                extra = NULL;
  18.180 -                session->«@filename»_state = new_state;
  18.181 -            } 
  18.182 -
  18.183 -            return PEP_STATUS_OK;
  18.184 -        }
  18.185 -
  18.186 -        ||
  18.187 -        document "generated/{@filename}_fsm.c", "text"
  18.188 -        ||
  18.189 -        #include "pEp_internal.h"
  18.190 -        #include "«@filename»_fsm.h"
  18.191 -        #include "«@filename»_impl.h"
  18.192 -
  18.193 -        // local definitions for «@name»'s state machine 
  18.194 -
  18.195 -        `` apply "state", 0 mode="declStatePayload"
  18.196 -
  18.197 -        // state machine for «@name»
  18.198 -
  18.199 -        «@name»_state fsm_«@name»(
  18.200 -                PEP_SESSION session,
  18.201 -                «@name»_state state,
  18.202 -                «@name»_event event,
  18.203 -                Identity partner,
  18.204 -                void *extra,
  18.205 -                time_t *timeout
  18.206 -            )
  18.207 -        {
  18.208 -            PEP_STATUS status = PEP_STATUS_OK;
  18.209 -
  18.210 -            switch (state) {
  18.211 -            `` apply "state", 2
  18.212 -                default:
  18.213 -                    return («@name»_state) invalid_state;
  18.214 -            }
  18.215 -
  18.216 -            return state;
  18.217 -        }
  18.218 -
  18.219 -        ||
  18.220 -    }
  18.221 -
  18.222 -    template "state" {
  18.223 -        ||
  18.224 -        case «@name»:
  18.225 -        {
  18.226 -            DEBUG_LOG("Entering FSM state", "«../@filename»_fsm.c", "state=«@name»")
  18.227 -        ||
  18.228 -
  18.229 -        if "count(parm) > 0" 
  18.230 -        || 
  18.231 -            assert(session->sync_state_payload);
  18.232 -            if(!session->sync_state_payload) return («../@name»_state) invalid_state;
  18.233 -            `` apply "parm", 1 mode="unpackStatePayloadParm" with "stateName", "@name"
  18.234 -        ||
  18.235 -
  18.236 -        ||
  18.237 -            switch (event) {
  18.238 -        ||
  18.239 -
  18.240 -        if "not(event[@name='Init'])" 
  18.241 -        ||
  18.242 -                case Init: 
  18.243 -                    DEBUG_LOG("FSM event", "«../@filename»_fsm.c, state=«@name»", "event=Init") 
  18.244 -                    *timeout = «@timeout»;
  18.245 -                    break;
  18.246 -        ||
  18.247 -
  18.248 -        apply "event", 2;
  18.249 -
  18.250 -        ||
  18.251 -                default:
  18.252 -                    return («../@name»_state) invalid_event;
  18.253 -            }
  18.254 -            break;
  18.255 -        }
  18.256 -        ||
  18.257 -    }
  18.258 -
  18.259 -    function "pEp_type" {
  18.260 -        param "type";
  18.261 -
  18.262 -        choose {
  18.263 -            when "$type = 'Identity'" > Identity
  18.264 -            when "$type = 'IdentityList'" > identity_list*
  18.265 -            when "$type = 'GroupKeys'" > group_keys_extra_t*
  18.266 -            otherwise value "$type";
  18.267 -        }
  18.268 -    }
  18.269 -
  18.270 -    function "pEp_type_op_radix" {
  18.271 -        param "type";
  18.272 -
  18.273 -        choose {
  18.274 -            when "$type = 'Identity'" > identity
  18.275 -            when "$type = 'IdentityList'" > identity_list
  18.276 -            when "$type = 'GroupKeys'" > group_keys_extra
  18.277 -            otherwise call "pEp_type" with "type", "$type";
  18.278 -        }
  18.279 -    }
  18.280 -
  18.281 -    template "parm" mode="unpackStatePayloadParm" 
  18.282 -    {
  18.283 -        param "stateName";
  18.284 -        const "pEpType" call "pEp_type" with "type","name(*[1])"; 
  18.285 -        | «$pEpType» «name(*[2])» = ((«$stateName»_state_payload_t*)session->sync_state_payload)->«name(*[2])»;
  18.286 -    }
  18.287 -
  18.288 -    template "state" mode="declStatePayload" if "count(parm) > 0"
  18.289 -    ||
  18.290 -    typedef struct _«@name»_state_payload {
  18.291 -        `` apply "parm", 1 mode="declStatePayloadParm"
  18.292 -    } «@name»_state_payload_t;
  18.293 -
  18.294 -    ||
  18.295 -
  18.296 -    template "parm" mode="declStatePayloadParm" {
  18.297 -        const "pEpType" call "pEp_type" with "type","name(*[1])"; 
  18.298 -        | «$pEpType» «name(*[2])»;
  18.299 -    }
  18.300 -
  18.301 -    template "event" {
  18.302 -        ||
  18.303 -        case «@name»:
  18.304 -        {
  18.305 -            DEBUG_LOG("FSM event", "«../../@filename»_fsm.c, state=«../@name»", "event=«@name»")
  18.306 -        `` if "@name='Init'" |> *timeout = «../@timeout»;
  18.307 -        ||
  18.308 -
  18.309 -        if "count(parm) > 1" {
  18.310 -            // TODO get ride of void *extra, pass per-event struct incl all params.
  18.311 -            const "extrapEpType" call "pEp_type" with "type","name(parm[2]/*[1])"; 
  18.312 -            const "extraArgName","name(parm[2]/*[2])"; 
  18.313 -            |> «$extrapEpType» «$extraArgName» = («$extrapEpType»)extra;
  18.314 -        }
  18.315 -
  18.316 -        ||
  18.317 -        `` apply "action|transition|condition";
  18.318 -        `` if "name(*[position()=last()]) != 'transition'" |> break;
  18.319 -        }
  18.320 -        ||
  18.321 -    }
  18.322 -
  18.323 -    template "action" {
  18.324 -        | DEBUG_LOG("FSM action", "«ancestor::fsm/@filename»_fsm.c, state=«ancestor::state/@name», event=«ancestor::event/@name»", "action=«@name»")
  18.325 -        indent(0);
  18.326 -        > status = «@name»(session, state, 
  18.327 -        choose {
  18.328 -            when "parm" > «name(parm/*)»
  18.329 -            otherwise > NULL
  18.330 -        }
  18.331 -        choose {
  18.332 -            when "count(parm) > 1" > , «name(parm[2]/*)»
  18.333 -            otherwise > , NULL
  18.334 -        }
  18.335 -        > );\n
  18.336 -        | if (status == PEP_OUT_OF_MEMORY)
  18.337 -        |> return (int) invalid_out_of_memory;
  18.338 -        | if (status != PEP_STATUS_OK)
  18.339 -        |> return (int) invalid_action;
  18.340 -    }
  18.341 -
  18.342 -    template "condition" {
  18.343 -        | {
  18.344 -        |> int cond_result = «@name»(session`apply "parm", 0`);
  18.345 -        |> #ifndef NDEBUG
  18.346 -        |> char resstr[11] = {0,};
  18.347 -        |> snprintf(resstr,10,"result=%d",cond_result);
  18.348 -        |> #endif
  18.349 -        |> DEBUG_LOG("FSM condition", "«ancestor::fsm/@filename»_fsm.c, state=«ancestor::state/@name», event=«ancestor::event/@name», condition=«@name»", resstr)
  18.350 -        |> if (cond_result < 0)
  18.351 -        |>> return cond_result;
  18.352 -        |> if (cond_result) {
  18.353 -        apply "action|transition|condition";
  18.354 -        |> }
  18.355 -        const "alternative", "./following-sibling::*[position()=1][name(.)='alternative']";
  18.356 -        if "$alternative" {
  18.357 -        |> else {
  18.358 -        apply "$alternative/action|$alternative/transition|$alternative/condition";
  18.359 -        |> }
  18.360 -        }
  18.361 -        | }
  18.362 -    }
  18.363 -
  18.364 -    template "parm" choose {
  18.365 -        when "count(*) = 1"
  18.366 -            > , «name(*)»
  18.367 -        otherwise
  18.368 -            > , «name(*[1])» «name(*[2])»
  18.369 -    }
  18.370 -
  18.371 -    template "transition"{
  18.372 -        const "stateparm", "ancestor::state/child::parm";
  18.373 -        if "count($stateparm) > 0" {
  18.374 -            ||
  18.375 -            assert(session->sync_state_payload);
  18.376 -            if(!session->sync_state_payload) return («ancestor::fsm/@name»_state) invalid_state;
  18.377 -            `` apply "$stateparm", 0 mode="freeStatePayloadParm" with "stateName", "ancestor::state/@name"
  18.378 -            free(session->sync_state_payload);
  18.379 -            session->sync_state_payload = NULL;
  18.380 -            ||
  18.381 -        }
  18.382 -        if "count(parm) > 0" {
  18.383 -            const "nextstatename", "@target";
  18.384 -            const "nextstateparm", "ancestor::fsm/child::state[@name = $nextstatename]/child::parm";
  18.385 -            if "count(parm) != count($nextstateparm)" 
  18.386 -                error > different state parameters and transition parameters count state=«ancestor::state/@name», event=«ancestor::event/@name» target=«@target»
  18.387 -            ||
  18.388 -            session->sync_state_payload = malloc(sizeof(«$nextstatename»_state_payload_t));
  18.389 -            assert(session->sync_state_payload);
  18.390 -            if(!session->sync_state_payload) return («ancestor::fsm/@name»_state) invalid_out_of_memory;
  18.391 -            ||
  18.392 -            apply "$nextstateparm", 0 mode="dupStatePayloadParm" {
  18.393 -                with "stateName", "$nextstatename";
  18.394 -                with "transitionParms", "parm";
  18.395 -            }
  18.396 -        }
  18.397 -        | DEBUG_LOG("FSM transition", "«ancestor::fsm/@filename»_fsm.c, state=«ancestor::state/@name», event=«ancestor::event/@name»", "target=«@target»")
  18.398 -        | return «@target»;
  18.399 -    }
  18.400 -
  18.401 -    template "parm" mode="freeStatePayloadParm" 
  18.402 -    {
  18.403 -        param "stateName";
  18.404 -        const "pEpTypeOpRadix" call "pEp_type_op_radix" with "type","name(*[1])"; 
  18.405 -        | free_«$pEpTypeOpRadix»(((«$stateName»_state_payload_t*)session->sync_state_payload)->«name(*[2])»);
  18.406 -    }
  18.407 -
  18.408 -    template "parm" mode="dupStatePayloadParm" 
  18.409 -    {
  18.410 -        param "stateName";
  18.411 -        param "transitionParms";
  18.412 -        const "pEpTypeOpRadix" call "pEp_type_op_radix" with "type","name(*[1])"; 
  18.413 -        const "pos", "position()";
  18.414 -        | ((«$stateName»_state_payload_t*)session->sync_state_payload)->«name(*[2])» =
  18.415 -        |     «$pEpTypeOpRadix»_dup(«name($transitionParms[$pos]/*)»);
  18.416 -    }
  18.417 -}
  18.418 -
    19.1 --- a/sync/generated/README	Thu Nov 02 17:02:05 2017 +0100
    19.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
    19.3 @@ -1,11 +0,0 @@
    19.4 -Code is generated here before beeing copied to /src during build process.
    19.5 -
    19.6 -This code is intended to be commited and reviewed, while the original files 
    19.7 -in src/ aren't tracked in mercurial (.hgignore). 
    19.8 -
    19.9 -Generated code must be generated out of source code systematically at build,
   19.10 -since generation could be influenced by build environment and parameters.
   19.11 -
   19.12 -Depending on build host, version control tool or unharchiving tool, generated
   19.13 -files timestamp may prevent re-generation. They must then be excluded from repo.
   19.14 -
    20.1 --- a/sync/generated/sync_driver.c	Thu Nov 02 17:02:05 2017 +0100
    20.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
    20.3 @@ -1,40 +0,0 @@
    20.4 -// Driver for DeviceState state machine
    20.5 -
    20.6 -#include <assert.h>
    20.7 -#include "pEp_internal.h"
    20.8 -
    20.9 -
   20.10 -DYNAMIC_API PEP_STATUS fsm_DeviceState_inject(
   20.11 -        PEP_SESSION session,
   20.12 -        DeviceState_event event,
   20.13 -        Identity partner,
   20.14 -        void *extra,
   20.15 -        time_t *timeout
   20.16 -    )
   20.17 -{
   20.18 -    assert(session);
   20.19 -    if (!session)
   20.20 -        return PEP_ILLEGAL_VALUE;
   20.21 -
   20.22 -    while(true)
   20.23 -    {
   20.24 -        DeviceState_state new_state = fsm_DeviceState(session,
   20.25 -            session->sync_state, event, partner, extra, timeout);
   20.26 -
   20.27 -        if (new_state == DeviceState_state_invalid_out_of_memory)
   20.28 -            return PEP_OUT_OF_MEMORY;
   20.29 -
   20.30 -        if (new_state < 0)
   20.31 -            return PEP_SYNC_STATEMACHINE_ERROR - new_state;
   20.32 -        
   20.33 -        if (new_state == session->sync_state)
   20.34 -            break;
   20.35 -
   20.36 -        event = Init;
   20.37 -        extra = NULL;
   20.38 -        session->sync_state = new_state;
   20.39 -    } 
   20.40 -
   20.41 -    return PEP_STATUS_OK;
   20.42 -}
   20.43 -
    21.1 --- a/sync/generated/sync_fsm.c	Thu Nov 02 17:02:05 2017 +0100
    21.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
    21.3 @@ -1,1610 +0,0 @@
    21.4 -#include "pEp_internal.h"
    21.5 -#include "sync_fsm.h"
    21.6 -#include "sync_impl.h"
    21.7 -
    21.8 -// local definitions for DeviceState's state machine 
    21.9 -
   21.10 -typedef struct _SoleBeaconed_state_payload {
   21.11 -    Identity expected;
   21.12 -} SoleBeaconed_state_payload_t;
   21.13 -
   21.14 -typedef struct _HandshakingSole_state_payload {
   21.15 -    Identity expected;
   21.16 -} HandshakingSole_state_payload_t;
   21.17 -
   21.18 -typedef struct _WaitForGroupKeysSole_state_payload {
   21.19 -    Identity expected;
   21.20 -} WaitForGroupKeysSole_state_payload_t;
   21.21 -
   21.22 -typedef struct _WaitForAcceptSole_state_payload {
   21.23 -    Identity expected;
   21.24 -    group_keys_extra_t* groupkeys;
   21.25 -} WaitForAcceptSole_state_payload_t;
   21.26 -
   21.27 -typedef struct _GroupedBeaconed_state_payload {
   21.28 -    Identity expected;
   21.29 -} GroupedBeaconed_state_payload_t;
   21.30 -
   21.31 -typedef struct _HandshakingGrouped_state_payload {
   21.32 -    Identity expected;
   21.33 -} HandshakingGrouped_state_payload_t;
   21.34 -
   21.35 -typedef struct _WaitForGroupKeysGrouped_state_payload {
   21.36 -    Identity expected;
   21.37 -} WaitForGroupKeysGrouped_state_payload_t;
   21.38 -
   21.39 -typedef struct _WaitForAcceptGrouped_state_payload {
   21.40 -    Identity expected;
   21.41 -    group_keys_extra_t* groupkeys;
   21.42 -} WaitForAcceptGrouped_state_payload_t;
   21.43 -
   21.44 -
   21.45 -// state machine for DeviceState
   21.46 -
   21.47 -DeviceState_state fsm_DeviceState(
   21.48 -        PEP_SESSION session,
   21.49 -        DeviceState_state state,
   21.50 -        DeviceState_event event,
   21.51 -        Identity partner,
   21.52 -        void *extra,
   21.53 -        time_t *timeout
   21.54 -    )
   21.55 -{
   21.56 -    PEP_STATUS status = PEP_STATUS_OK;
   21.57 -
   21.58 -    switch (state) {
   21.59 -        case InitState:
   21.60 -        {
   21.61 -            DEBUG_LOG("Entering FSM state", "sync_fsm.c", "state=InitState")
   21.62 -            switch (event) {
   21.63 -                case Init:
   21.64 -                {
   21.65 -                    DEBUG_LOG("FSM event", "sync_fsm.c, state=InitState", "event=Init")
   21.66 -                    *timeout = 0;
   21.67 -                    {
   21.68 -                        int cond_result = deviceGrouped(session);
   21.69 -                        #ifndef NDEBUG
   21.70 -                        char resstr[11] = {0,};
   21.71 -                        snprintf(resstr,10,"result=%d",cond_result);
   21.72 -                        #endif
   21.73 -                        DEBUG_LOG("FSM condition", "sync_fsm.c, state=InitState, event=Init, condition=deviceGrouped", resstr)
   21.74 -                        if (cond_result < 0)
   21.75 -                            return cond_result;
   21.76 -                        if (cond_result) {
   21.77 -                        DEBUG_LOG("FSM transition", "sync_fsm.c, state=InitState, event=Init", "target=Grouped")
   21.78 -                        return Grouped;
   21.79 -                        }
   21.80 -                    }
   21.81 -                    DEBUG_LOG("FSM transition", "sync_fsm.c, state=InitState, event=Init", "target=Sole")
   21.82 -                    return Sole;
   21.83 -                }
   21.84 -                default:
   21.85 -                    return (DeviceState_state) invalid_event;
   21.86 -            }
   21.87 -            break;
   21.88 -        }
   21.89 -        case Sole:
   21.90 -        {
   21.91 -            DEBUG_LOG("Entering FSM state", "sync_fsm.c", "state=Sole")
   21.92 -            switch (event) {
   21.93 -                case Init: 
   21.94 -                    DEBUG_LOG("FSM event", "sync_fsm.c, state=Sole", "event=Init") 
   21.95 -                    *timeout = 0;
   21.96 -                    break;
   21.97 -                case KeyGen:
   21.98 -                {
   21.99 -                    DEBUG_LOG("FSM event", "sync_fsm.c, state=Sole", "event=KeyGen")
  21.100 -                    DEBUG_LOG("FSM action", "sync_fsm.c, state=Sole, event=KeyGen", "action=sendBeacon")
  21.101 -                    status = sendBeacon(session, state, NULL, NULL);
  21.102 -                    if (status == PEP_OUT_OF_MEMORY)
  21.103 -                        return (int) invalid_out_of_memory;
  21.104 -                    if (status != PEP_STATUS_OK)
  21.105 -                        return (int) invalid_action;
  21.106 -                    DEBUG_LOG("FSM transition", "sync_fsm.c, state=Sole, event=KeyGen", "target=SoleWaiting")
  21.107 -                    return SoleWaiting;
  21.108 -                }
  21.109 -                case CannotDecrypt:
  21.110 -                {
  21.111 -                    DEBUG_LOG("FSM event", "sync_fsm.c, state=Sole", "event=CannotDecrypt")
  21.112 -                    DEBUG_LOG("FSM action", "sync_fsm.c, state=Sole, event=CannotDecrypt", "action=sendBeacon")
  21.113 -                    status = sendBeacon(session, state, NULL, NULL);
  21.114 -                    if (status == PEP_OUT_OF_MEMORY)
  21.115 -                        return (int) invalid_out_of_memory;
  21.116 -                    if (status != PEP_STATUS_OK)
  21.117 -                        return (int) invalid_action;
  21.118 -                    DEBUG_LOG("FSM transition", "sync_fsm.c, state=Sole, event=CannotDecrypt", "target=SoleWaiting")
  21.119 -                    return SoleWaiting;
  21.120 -                }
  21.121 -                case Beacon:
  21.122 -                {
  21.123 -                    DEBUG_LOG("FSM event", "sync_fsm.c, state=Sole", "event=Beacon")
  21.124 -                    DEBUG_LOG("FSM action", "sync_fsm.c, state=Sole, event=Beacon", "action=sendHandshakeRequest")
  21.125 -                    status = sendHandshakeRequest(session, state, partner, NULL);
  21.126 -                    if (status == PEP_OUT_OF_MEMORY)
  21.127 -                        return (int) invalid_out_of_memory;
  21.128 -                    if (status != PEP_STATUS_OK)
  21.129 -                        return (int) invalid_action;
  21.130 -                    session->sync_state_payload = malloc(sizeof(SoleBeaconed_state_payload_t));
  21.131 -                    assert(session->sync_state_payload);
  21.132 -                    if(!session->sync_state_payload) return (DeviceState_state) invalid_out_of_memory;
  21.133 -                    ((SoleBeaconed_state_payload_t*)session->sync_state_payload)->expected =
  21.134 -                        identity_dup(partner);
  21.135 -                    DEBUG_LOG("FSM transition", "sync_fsm.c, state=Sole, event=Beacon", "target=SoleBeaconed")
  21.136 -                    return SoleBeaconed;
  21.137 -                }
  21.138 -                case HandshakeRequest:
  21.139 -                {
  21.140 -                    DEBUG_LOG("FSM event", "sync_fsm.c, state=Sole", "event=HandshakeRequest")
  21.141 -                    DEBUG_LOG("FSM action", "sync_fsm.c, state=Sole, event=HandshakeRequest", "action=sendHandshakeRequest")
  21.142 -                    status = sendHandshakeRequest(session, state, partner, NULL);
  21.143 -                    if (status == PEP_OUT_OF_MEMORY)
  21.144 -                        return (int) invalid_out_of_memory;
  21.145 -                    if (status != PEP_STATUS_OK)
  21.146 -                        return (int) invalid_action;
  21.147 -                    session->sync_state_payload = malloc(sizeof(HandshakingSole_state_payload_t));
  21.148 -                    assert(session->sync_state_payload);
  21.149 -                    if(!session->sync_state_payload) return (DeviceState_state) invalid_out_of_memory;
  21.150 -                    ((HandshakingSole_state_payload_t*)session->sync_state_payload)->expected =
  21.151 -                        identity_dup(partner);
  21.152 -                    DEBUG_LOG("FSM transition", "sync_fsm.c, state=Sole, event=HandshakeRequest", "target=HandshakingSole")
  21.153 -                    return HandshakingSole;
  21.154 -                }
  21.155 -                default:
  21.156 -                    return (DeviceState_state) invalid_event;
  21.157 -            }
  21.158 -            break;
  21.159 -        }
  21.160 -        case SoleWaiting:
  21.161 -        {
  21.162 -            DEBUG_LOG("Entering FSM state", "sync_fsm.c", "state=SoleWaiting")
  21.163 -            switch (event) {
  21.164 -                case Init: 
  21.165 -                    DEBUG_LOG("FSM event", "sync_fsm.c, state=SoleWaiting", "event=Init") 
  21.166 -                    *timeout = 60;
  21.167 -                    break;
  21.168 -                case KeyGen:
  21.169 -                {
  21.170 -                    DEBUG_LOG("FSM event", "sync_fsm.c, state=SoleWaiting", "event=KeyGen")
  21.171 -                    DEBUG_LOG("FSM action", "sync_fsm.c, state=SoleWaiting, event=KeyGen", "action=sendBeacon")
  21.172 -                    status = sendBeacon(session, state, NULL, NULL);
  21.173 -                    if (status == PEP_OUT_OF_MEMORY)
  21.174 -                        return (int) invalid_out_of_memory;
  21.175 -                    if (status != PEP_STATUS_OK)
  21.176 -                        return (int) invalid_action;
  21.177 -                    break;
  21.178 -                }
  21.179 -                case CannotDecrypt:
  21.180 -                {
  21.181 -                    DEBUG_LOG("FSM event", "sync_fsm.c, state=SoleWaiting", "event=CannotDecrypt")
  21.182 -                    DEBUG_LOG("FSM action", "sync_fsm.c, state=SoleWaiting, event=CannotDecrypt", "action=sendBeacon")
  21.183 -                    status = sendBeacon(session, state, NULL, NULL);
  21.184 -                    if (status == PEP_OUT_OF_MEMORY)
  21.185 -                        return (int) invalid_out_of_memory;
  21.186 -                    if (status != PEP_STATUS_OK)
  21.187 -                        return (int) invalid_action;
  21.188 -                    break;
  21.189 -                }
  21.190 -                case Beacon:
  21.191 -                {
  21.192 -                    DEBUG_LOG("FSM event", "sync_fsm.c, state=SoleWaiting", "event=Beacon")
  21.193 -                    DEBUG_LOG("FSM action", "sync_fsm.c, state=SoleWaiting, event=Beacon", "action=sendHandshakeRequest")
  21.194 -                    status = sendHandshakeRequest(session, state, partner, NULL);
  21.195 -                    if (status == PEP_OUT_OF_MEMORY)
  21.196 -                        return (int) invalid_out_of_memory;
  21.197 -                    if (status != PEP_STATUS_OK)
  21.198 -                        return (int) invalid_action;
  21.199 -                    session->sync_state_payload = malloc(sizeof(SoleBeaconed_state_payload_t));
  21.200 -                    assert(session->sync_state_payload);
  21.201 -                    if(!session->sync_state_payload) return (DeviceState_state) invalid_out_of_memory;
  21.202 -                    ((SoleBeaconed_state_payload_t*)session->sync_state_payload)->expected =
  21.203 -                        identity_dup(partner);
  21.204 -                    DEBUG_LOG("FSM transition", "sync_fsm.c, state=SoleWaiting, event=Beacon", "target=SoleBeaconed")
  21.205 -                    return SoleBeaconed;
  21.206 -                }
  21.207 -                case HandshakeRequest:
  21.208 -                {
  21.209 -                    DEBUG_LOG("FSM event", "sync_fsm.c, state=SoleWaiting", "event=HandshakeRequest")
  21.210 -                    DEBUG_LOG("FSM action", "sync_fsm.c, state=SoleWaiting, event=HandshakeRequest", "action=sendHandshakeRequest")
  21.211 -                    status = sendHandshakeRequest(session, state, partner, NULL);
  21.212 -                    if (status == PEP_OUT_OF_MEMORY)
  21.213 -                        return (int) invalid_out_of_memory;
  21.214 -                    if (status != PEP_STATUS_OK)
  21.215 -                        return (int) invalid_action;
  21.216 -                    session->sync_state_payload = malloc(sizeof(HandshakingSole_state_payload_t));
  21.217 -                    assert(session->sync_state_payload);
  21.218 -                    if(!session->sync_state_payload) return (DeviceState_state) invalid_out_of_memory;
  21.219 -                    ((HandshakingSole_state_payload_t*)session->sync_state_payload)->expected =
  21.220 -                        identity_dup(partner);
  21.221 -                    DEBUG_LOG("FSM transition", "sync_fsm.c, state=SoleWaiting, event=HandshakeRequest", "target=HandshakingSole")
  21.222 -                    return HandshakingSole;
  21.223 -                }
  21.224 -                case Timeout:
  21.225 -                {
  21.226 -                    DEBUG_LOG("FSM event", "sync_fsm.c, state=SoleWaiting", "event=Timeout")
  21.227 -                    DEBUG_LOG("FSM transition", "sync_fsm.c, state=SoleWaiting, event=Timeout", "target=Sole")
  21.228 -                    return Sole;
  21.229 -                }
  21.230 -                default:
  21.231 -                    return (DeviceState_state) invalid_event;
  21.232 -            }
  21.233 -            break;
  21.234 -        }
  21.235 -        case SoleBeaconed:
  21.236 -        {
  21.237 -            DEBUG_LOG("Entering FSM state", "sync_fsm.c", "state=SoleBeaconed")
  21.238 -            assert(session->sync_state_payload);
  21.239 -            if(!session->sync_state_payload) return (DeviceState_state) invalid_state;
  21.240 -            Identity expected = ((SoleBeaconed_state_payload_t*)session->sync_state_payload)->expected;
  21.241 -            switch (event) {
  21.242 -                case Init: 
  21.243 -                    DEBUG_LOG("FSM event", "sync_fsm.c, state=SoleBeaconed", "event=Init") 
  21.244 -                    *timeout = 600;
  21.245 -                    break;
  21.246 -                case KeyGen:
  21.247 -                {
  21.248 -                    DEBUG_LOG("FSM event", "sync_fsm.c, state=SoleBeaconed", "event=KeyGen")
  21.249 -                    DEBUG_LOG("FSM action", "sync_fsm.c, state=SoleBeaconed, event=KeyGen", "action=sendBeacon")
  21.250 -                    status = sendBeacon(session, state, NULL, NULL);
  21.251 -                    if (status == PEP_OUT_OF_MEMORY)
  21.252 -                        return (int) invalid_out_of_memory;
  21.253 -                    if (status != PEP_STATUS_OK)
  21.254 -                        return (int) invalid_action;
  21.255 -                    assert(session->sync_state_payload);
  21.256 -                    if(!session->sync_state_payload) return (DeviceState_state) invalid_state;
  21.257 -                    free_identity(((SoleBeaconed_state_payload_t*)session->sync_state_payload)->expected);
  21.258 -                    free(session->sync_state_payload);
  21.259 -                    session->sync_state_payload = NULL;
  21.260 -                    DEBUG_LOG("FSM transition", "sync_fsm.c, state=SoleBeaconed, event=KeyGen", "target=Sole")
  21.261 -                    return Sole;
  21.262 -                }
  21.263 -                case CannotDecrypt:
  21.264 -                {
  21.265 -                    DEBUG_LOG("FSM event", "sync_fsm.c, state=SoleBeaconed", "event=CannotDecrypt")
  21.266 -                    DEBUG_LOG("FSM action", "sync_fsm.c, state=SoleBeaconed, event=CannotDecrypt", "action=sendBeacon")
  21.267 -                    status = sendBeacon(session, state, NULL, NULL);
  21.268 -                    if (status == PEP_OUT_OF_MEMORY)
  21.269 -                        return (int) invalid_out_of_memory;
  21.270 -                    if (status != PEP_STATUS_OK)
  21.271 -                        return (int) invalid_action;
  21.272 -                    assert(session->sync_state_payload);
  21.273 -                    if(!session->sync_state_payload) return (DeviceState_state) invalid_state;
  21.274 -                    free_identity(((SoleBeaconed_state_payload_t*)session->sync_state_payload)->expected);
  21.275 -                    free(session->sync_state_payload);
  21.276 -                    session->sync_state_payload = NULL;
  21.277 -                    DEBUG_LOG("FSM transition", "sync_fsm.c, state=SoleBeaconed, event=CannotDecrypt", "target=Sole")
  21.278 -                    return Sole;
  21.279 -                }
  21.280 -                case Beacon:
  21.281 -                {
  21.282 -                    DEBUG_LOG("FSM event", "sync_fsm.c, state=SoleBeaconed", "event=Beacon")
  21.283 -                    DEBUG_LOG("FSM action", "sync_fsm.c, state=SoleBeaconed, event=Beacon", "action=sendHandshakeRequest")
  21.284 -                    status = sendHandshakeRequest(session, state, partner, NULL);
  21.285 -                    if (status == PEP_OUT_OF_MEMORY)
  21.286 -                        return (int) invalid_out_of_memory;
  21.287 -                    if (status != PEP_STATUS_OK)
  21.288 -                        return (int) invalid_action;
  21.289 -                    assert(session->sync_state_payload);
  21.290 -                    if(!session->sync_state_payload) return (DeviceState_state) invalid_state;
  21.291 -                    free_identity(((SoleBeaconed_state_payload_t*)session->sync_state_payload)->expected);
  21.292 -                    free(session->sync_state_payload);
  21.293 -                    session->sync_state_payload = NULL;
  21.294 -                    session->sync_state_payload = malloc(sizeof(SoleBeaconed_state_payload_t));
  21.295 -                    assert(session->sync_state_payload);
  21.296 -                    if(!session->sync_state_payload) return (DeviceState_state) invalid_out_of_memory;
  21.297 -                    ((SoleBeaconed_state_payload_t*)session->sync_state_payload)->expected =
  21.298 -                        identity_dup(partner);
  21.299 -                    DEBUG_LOG("FSM transition", "sync_fsm.c, state=SoleBeaconed, event=Beacon", "target=SoleBeaconed")
  21.300 -                    return SoleBeaconed;
  21.301 -                }
  21.302 -                case HandshakeRequest:
  21.303 -                {
  21.304 -                    DEBUG_LOG("FSM event", "sync_fsm.c, state=SoleBeaconed", "event=HandshakeRequest")
  21.305 -                    {
  21.306 -                        int cond_result = sameIdentities(session, partner, expected);
  21.307 -                        #ifndef NDEBUG
  21.308 -                        char resstr[11] = {0,};
  21.309 -                        snprintf(resstr,10,"result=%d",cond_result);
  21.310 -                        #endif
  21.311 -                        DEBUG_LOG("FSM condition", "sync_fsm.c, state=SoleBeaconed, event=HandshakeRequest, condition=sameIdentities", resstr)
  21.312 -                        if (cond_result < 0)
  21.313 -                            return cond_result;
  21.314 -                        if (cond_result) {
  21.315 -                        }
  21.316 -                        else {
  21.317 -                        DEBUG_LOG("FSM action", "sync_fsm.c, state=SoleBeaconed, event=HandshakeRequest", "action=sendHandshakeRequest")
  21.318 -                        status = sendHandshakeRequest(session, state, partner, NULL);
  21.319 -                        if (status == PEP_OUT_OF_MEMORY)
  21.320 -                            return (int) invalid_out_of_memory;
  21.321 -                        if (status != PEP_STATUS_OK)
  21.322 -                            return (int) invalid_action;
  21.323 -                        }
  21.324 -                    }
  21.325 -                    assert(session->sync_state_payload);
  21.326 -                    if(!session->sync_state_payload) return (DeviceState_state) invalid_state;
  21.327 -                    free_identity(((SoleBeaconed_state_payload_t*)session->sync_state_payload)->expected);
  21.328 -                    free(session->sync_state_payload);
  21.329 -                    session->sync_state_payload = NULL;
  21.330 -                    session->sync_state_payload = malloc(sizeof(HandshakingSole_state_payload_t));
  21.331 -                    assert(session->sync_state_payload);
  21.332 -                    if(!session->sync_state_payload) return (DeviceState_state) invalid_out_of_memory;
  21.333 -                    ((HandshakingSole_state_payload_t*)session->sync_state_payload)->expected =
  21.334 -                        identity_dup(partner);
  21.335 -                    DEBUG_LOG("FSM transition", "sync_fsm.c, state=SoleBeaconed, event=HandshakeRequest", "target=HandshakingSole")
  21.336 -                    return HandshakingSole;
  21.337 -                }
  21.338 -                case Timeout:
  21.339 -                {
  21.340 -                    DEBUG_LOG("FSM event", "sync_fsm.c, state=SoleBeaconed", "event=Timeout")
  21.341 -                    assert(session->sync_state_payload);
  21.342 -                    if(!session->sync_state_payload) return (DeviceState_state) invalid_state;
  21.343 -                    free_identity(((SoleBeaconed_state_payload_t*)session->sync_state_payload)->expected);
  21.344 -                    free(session->sync_state_payload);
  21.345 -                    session->sync_state_payload = NULL;
  21.346 -                    DEBUG_LOG("FSM transition", "sync_fsm.c, state=SoleBeaconed, event=Timeout", "target=Sole")
  21.347 -                    return Sole;
  21.348 -                }
  21.349 -                default:
  21.350 -                    return (DeviceState_state) invalid_event;
  21.351 -            }
  21.352 -            break;
  21.353 -        }
  21.354 -        case HandshakingSole:
  21.355 -        {
  21.356 -            DEBUG_LOG("Entering FSM state", "sync_fsm.c", "state=HandshakingSole")
  21.357 -            assert(session->sync_state_payload);
  21.358 -            if(!session->sync_state_payload) return (DeviceState_state) invalid_state;
  21.359 -            Identity expected = ((HandshakingSole_state_payload_t*)session->sync_state_payload)->expected;
  21.360 -            switch (event) {
  21.361 -                case Init:
  21.362 -                {
  21.363 -                    DEBUG_LOG("FSM event", "sync_fsm.c, state=HandshakingSole", "event=Init")
  21.364 -                    *timeout = 600;
  21.365 -                    {
  21.366 -                        int cond_result = keyElectionWon(session, expected);
  21.367 -                        #ifndef NDEBUG
  21.368 -                        char resstr[11] = {0,};
  21.369 -                        snprintf(resstr,10,"result=%d",cond_result);
  21.370 -                        #endif
  21.371 -                        DEBUG_LOG("FSM condition", "sync_fsm.c, state=HandshakingSole, event=Init, condition=keyElectionWon", resstr)
  21.372 -                        if (cond_result < 0)
  21.373 -                            return cond_result;
  21.374 -                        if (cond_result) {
  21.375 -                        DEBUG_LOG("FSM action", "sync_fsm.c, state=HandshakingSole, event=Init", "action=notifyInitFormGroup")
  21.376 -                        status = notifyInitFormGroup(session, state, expected, NULL);
  21.377 -                        if (status == PEP_OUT_OF_MEMORY)
  21.378 -                            return (int) invalid_out_of_memory;
  21.379 -                        if (status != PEP_STATUS_OK)
  21.380 -                            return (int) invalid_action;
  21.381 -                        }
  21.382 -                        else {
  21.383 -                        DEBUG_LOG("FSM action", "sync_fsm.c, state=HandshakingSole, event=Init", "action=notifyInitAddOurDevice")
  21.384 -                        status = notifyInitAddOurDevice(session, state, expected, NULL);
  21.385 -                        if (status == PEP_OUT_OF_MEMORY)
  21.386 -                            return (int) invalid_out_of_memory;
  21.387 -                        if (status != PEP_STATUS_OK)
  21.388 -                            return (int) invalid_action;
  21.389 -                        }
  21.390 -                    }
  21.391 -                    break;
  21.392 -                }
  21.393 -                case HandshakeRejected:
  21.394 -                {
  21.395 -                    DEBUG_LOG("FSM event", "sync_fsm.c, state=HandshakingSole", "event=HandshakeRejected")
  21.396 -                    DEBUG_LOG("FSM action", "sync_fsm.c, state=HandshakingSole, event=HandshakeRejected", "action=rejectHandshake")
  21.397 -                    status = rejectHandshake(session, state, partner, NULL);
  21.398 -                    if (status == PEP_OUT_OF_MEMORY)
  21.399 -                        return (int) invalid_out_of_memory;
  21.400 -                    if (status != PEP_STATUS_OK)
  21.401 -                        return (int) invalid_action;
  21.402 -                    assert(session->sync_state_payload);
  21.403 -                    if(!session->sync_state_payload) return (DeviceState_state) invalid_state;
  21.404 -                    free_identity(((HandshakingSole_state_payload_t*)session->sync_state_payload)->expected);
  21.405 -                    free(session->sync_state_payload);
  21.406 -                    session->sync_state_payload = NULL;
  21.407 -                    DEBUG_LOG("FSM transition", "sync_fsm.c, state=HandshakingSole, event=HandshakeRejected", "target=Sole")
  21.408 -                    return Sole;
  21.409 -                }
  21.410 -                case HandshakeAccepted:
  21.411 -                {
  21.412 -                    DEBUG_LOG("FSM event", "sync_fsm.c, state=HandshakingSole", "event=HandshakeAccepted")
  21.413 -                    {
  21.414 -                        int cond_result = sameIdentities(session, partner, expected);
  21.415 -                        #ifndef NDEBUG
  21.416 -                        char resstr[11] = {0,};
  21.417 -                        snprintf(resstr,10,"result=%d",cond_result);
  21.418 -                        #endif
  21.419 -                        DEBUG_LOG("FSM condition", "sync_fsm.c, state=HandshakingSole, event=HandshakeAccepted, condition=sameIdentities", resstr)
  21.420 -                        if (cond_result < 0)
  21.421 -                            return cond_result;
  21.422 -                        if (cond_result) {
  21.423 -                        DEBUG_LOG("FSM action", "sync_fsm.c, state=HandshakingSole, event=HandshakeAccepted", "action=acceptHandshake")
  21.424 -                        status = acceptHandshake(session, state, partner, NULL);
  21.425 -                        if (status == PEP_OUT_OF_MEMORY)
  21.426 -                            return (int) invalid_out_of_memory;
  21.427 -                        if (status != PEP_STATUS_OK)
  21.428 -                            return (int) invalid_action;
  21.429 -                        {
  21.430 -                            int cond_result = keyElectionWon(session, partner);
  21.431 -                            #ifndef NDEBUG
  21.432 -                            char resstr[11] = {0,};
  21.433 -                            snprintf(resstr,10,"result=%d",cond_result);
  21.434 -                            #endif
  21.435 -                            DEBUG_LOG("FSM condition", "sync_fsm.c, state=HandshakingSole, event=HandshakeAccepted, condition=keyElectionWon", resstr)
  21.436 -                            if (cond_result < 0)
  21.437 -                                return cond_result;
  21.438 -                            if (cond_result) {
  21.439 -                            DEBUG_LOG("FSM action", "sync_fsm.c, state=HandshakingSole, event=HandshakeAccepted", "action=makeGroup")
  21.440 -                            status = makeGroup(session, state, NULL, NULL);
  21.441 -                            if (status == PEP_OUT_OF_MEMORY)
  21.442 -                                return (int) invalid_out_of_memory;
  21.443 -                            if (status != PEP_STATUS_OK)
  21.444 -                                return (int) invalid_action;
  21.445 -                            DEBUG_LOG("FSM action", "sync_fsm.c, state=HandshakingSole, event=HandshakeAccepted", "action=sendGroupKeys")
  21.446 -                            status = sendGroupKeys(session, state, partner, NULL);
  21.447 -                            if (status == PEP_OUT_OF_MEMORY)
  21.448 -                                return (int) invalid_out_of_memory;
  21.449 -                            if (status != PEP_STATUS_OK)
  21.450 -                                return (int) invalid_action;
  21.451 -                            DEBUG_LOG("FSM action", "sync_fsm.c, state=HandshakingSole, event=HandshakeAccepted", "action=renewUUID")
  21.452 -                            status = renewUUID(session, state, NULL, NULL);
  21.453 -                            if (status == PEP_OUT_OF_MEMORY)
  21.454 -                                return (int) invalid_out_of_memory;
  21.455 -                            if (status != PEP_STATUS_OK)
  21.456 -                                return (int) invalid_action;
  21.457 -                            DEBUG_LOG("FSM action", "sync_fsm.c, state=HandshakingSole, event=HandshakeAccepted", "action=notifyAcceptedGroupCreated")
  21.458 -                            status = notifyAcceptedGroupCreated(session, state, partner, NULL);
  21.459 -                            if (status == PEP_OUT_OF_MEMORY)
  21.460 -                                return (int) invalid_out_of_memory;
  21.461 -                            if (status != PEP_STATUS_OK)
  21.462 -                                return (int) invalid_action;
  21.463 -                            assert(session->sync_state_payload);
  21.464 -                            if(!session->sync_state_payload) return (DeviceState_state) invalid_state;
  21.465 -                            free_identity(((HandshakingSole_state_payload_t*)session->sync_state_payload)->expected);
  21.466 -                            free(session->sync_state_payload);
  21.467 -                            session->sync_state_payload = NULL;
  21.468 -                            DEBUG_LOG("FSM transition", "sync_fsm.c, state=HandshakingSole, event=HandshakeAccepted", "target=Grouped")
  21.469 -                            return Grouped;
  21.470 -                            }
  21.471 -                        }
  21.472 -                        assert(session->sync_state_payload);
  21.473 -                        if(!session->sync_state_payload) return (DeviceState_state) invalid_state;
  21.474 -                        free_identity(((HandshakingSole_state_payload_t*)session->sync_state_payload)->expected);
  21.475 -                        free(session->sync_state_payload);
  21.476 -                        session->sync_state_payload = NULL;
  21.477 -                        session->sync_state_payload = malloc(sizeof(WaitForGroupKeysSole_state_payload_t));
  21.478 -                        assert(session->sync_state_payload);
  21.479 -                        if(!session->sync_state_payload) return (DeviceState_state) invalid_out_of_memory;
  21.480 -                        ((WaitForGroupKeysSole_state_payload_t*)session->sync_state_payload)->expected =
  21.481 -                            identity_dup(partner);
  21.482 -                        DEBUG_LOG("FSM transition", "sync_fsm.c, state=HandshakingSole, event=HandshakeAccepted", "target=WaitForGroupKeysSole")
  21.483 -                        return WaitForGroupKeysSole;
  21.484 -                        }
  21.485 -                    }
  21.486 -                    assert(session->sync_state_payload);
  21.487 -                    if(!session->sync_state_payload) return (DeviceState_state) invalid_state;
  21.488 -                    free_identity(((HandshakingSole_state_payload_t*)session->sync_state_payload)->expected);
  21.489 -                    free(session->sync_state_payload);
  21.490 -                    session->sync_state_payload = NULL;
  21.491 -                    DEBUG_LOG("FSM transition", "sync_fsm.c, state=HandshakingSole, event=HandshakeAccepted", "target=Sole")
  21.492 -                    return Sole;
  21.493 -                }
  21.494 -                case Cancel:
  21.495 -                {
  21.496 -                    DEBUG_LOG("FSM event", "sync_fsm.c, state=HandshakingSole", "event=Cancel")
  21.497 -                    assert(session->sync_state_payload);
  21.498 -                    if(!session->sync_state_payload) return (DeviceState_state) invalid_state;
  21.499 -                    free_identity(((HandshakingSole_state_payload_t*)session->sync_state_payload)->expected);
  21.500 -                    free(session->sync_state_payload);
  21.501 -                    session->sync_state_payload = NULL;
  21.502 -                    DEBUG_LOG("FSM transition", "sync_fsm.c, state=HandshakingSole, event=Cancel", "target=Sole")
  21.503 -                    return Sole;
  21.504 -                }
  21.505 -                case GroupKeys:
  21.506 -                {
  21.507 -                    DEBUG_LOG("FSM event", "sync_fsm.c, state=HandshakingSole", "event=GroupKeys")
  21.508 -                    group_keys_extra_t* groupkeys = (group_keys_extra_t*)extra;
  21.509 -                    {
  21.510 -                        int cond_result = keyElectionWon(session, expected);
  21.511 -                        #ifndef NDEBUG
  21.512 -                        char resstr[11] = {0,};
  21.513 -                        snprintf(resstr,10,"result=%d",cond_result);
  21.514 -                        #endif
  21.515 -                        DEBUG_LOG("FSM condition", "sync_fsm.c, state=HandshakingSole, event=GroupKeys, condition=keyElectionWon", resstr)
  21.516 -                        if (cond_result < 0)
  21.517 -                            return cond_result;
  21.518 -                        if (cond_result) {
  21.519 -                        }
  21.520 -                        else {
  21.521 -                        {
  21.522 -                            int cond_result = sameKeyAndAddress(session, partner, expected);
  21.523 -                            #ifndef NDEBUG
  21.524 -                            char resstr[11] = {0,};
  21.525 -                            snprintf(resstr,10,"result=%d",cond_result);
  21.526 -                            #endif
  21.527 -                            DEBUG_LOG("FSM condition", "sync_fsm.c, state=HandshakingSole, event=GroupKeys, condition=sameKeyAndAddress", resstr)
  21.528 -                            if (cond_result < 0)
  21.529 -                                return cond_result;
  21.530 -                            if (cond_result) {
  21.531 -                            assert(session->sync_state_payload);
  21.532 -                            if(!session->sync_state_payload) return (DeviceState_state) invalid_state;
  21.533 -                            free_identity(((HandshakingSole_state_payload_t*)session->sync_state_payload)->expected);
  21.534 -                            free(session->sync_state_payload);
  21.535 -                            session->sync_state_payload = NULL;
  21.536 -                            session->sync_state_payload = malloc(sizeof(WaitForAcceptSole_state_payload_t));
  21.537 -                            assert(session->sync_state_payload);
  21.538 -                            if(!session->sync_state_payload) return (DeviceState_state) invalid_out_of_memory;
  21.539 -                            ((WaitForAcceptSole_state_payload_t*)session->sync_state_payload)->expected =
  21.540 -                                identity_dup(partner);
  21.541 -                            ((WaitForAcceptSole_state_payload_t*)session->sync_state_payload)->groupkeys =
  21.542 -                                group_keys_extra_dup(groupkeys);
  21.543 -                            DEBUG_LOG("FSM transition", "sync_fsm.c, state=HandshakingSole, event=GroupKeys", "target=WaitForAcceptSole")
  21.544 -                            return WaitForAcceptSole;
  21.545 -                            }
  21.546 -                        }
  21.547 -                        }
  21.548 -                    }
  21.549 -                    break;
  21.550 -                }
  21.551 -                case Timeout:
  21.552 -                {
  21.553 -                    DEBUG_LOG("FSM event", "sync_fsm.c, state=HandshakingSole", "event=Timeout")
  21.554 -                    DEBUG_LOG("FSM action", "sync_fsm.c, state=HandshakingSole, event=Timeout", "action=notifyTimeout")
  21.555 -                    status = notifyTimeout(session, state, expected, NULL);
  21.556 -                    if (status == PEP_OUT_OF_MEMORY)
  21.557 -                        return (int) invalid_out_of_memory;
  21.558 -                    if (status != PEP_STATUS_OK)
  21.559 -                        return (int) invalid_action;
  21.560 -                    DEBUG_LOG("FSM action", "sync_fsm.c, state=HandshakingSole, event=Timeout", "action=sendBeacon")
  21.561 -                    status = sendBeacon(session, state, NULL, NULL);
  21.562 -                    if (status == PEP_OUT_OF_MEMORY)
  21.563 -                        return (int) invalid_out_of_memory;
  21.564 -                    if (status != PEP_STATUS_OK)
  21.565 -                        return (int) invalid_action;
  21.566 -                    assert(session->sync_state_payload);
  21.567 -                    if(!session->sync_state_payload) return (DeviceState_state) invalid_state;
  21.568 -                    free_identity(((HandshakingSole_state_payload_t*)session->sync_state_payload)->expected);
  21.569 -                    free(session->sync_state_payload);
  21.570 -                    session->sync_state_payload = NULL;
  21.571 -                    DEBUG_LOG("FSM transition", "sync_fsm.c, state=HandshakingSole, event=Timeout", "target=Sole")
  21.572 -                    return Sole;
  21.573 -                }
  21.574 -                default:
  21.575 -                    return (DeviceState_state) invalid_event;
  21.576 -            }
  21.577 -            break;
  21.578 -        }
  21.579 -        case WaitForGroupKeysSole:
  21.580 -        {
  21.581 -            DEBUG_LOG("Entering FSM state", "sync_fsm.c", "state=WaitForGroupKeysSole")
  21.582 -            assert(session->sync_state_payload);
  21.583 -            if(!session->sync_state_payload) return (DeviceState_state) invalid_state;
  21.584 -            Identity expected = ((WaitForGroupKeysSole_state_payload_t*)session->sync_state_payload)->expected;
  21.585 -            switch (event) {
  21.586 -                case Init: 
  21.587 -                    DEBUG_LOG("FSM event", "sync_fsm.c, state=WaitForGroupKeysSole", "event=Init") 
  21.588 -                    *timeout = 600;
  21.589 -                    break;
  21.590 -                case GroupKeys:
  21.591 -                {
  21.592 -                    DEBUG_LOG("FSM event", "sync_fsm.c, state=WaitForGroupKeysSole", "event=GroupKeys")
  21.593 -                    group_keys_extra_t* groupkeys = (group_keys_extra_t*)extra;
  21.594 -                    {
  21.595 -                        int cond_result = sameKeyAndAddress(session, partner, expected);
  21.596 -                        #ifndef NDEBUG
  21.597 -                        char resstr[11] = {0,};
  21.598 -                        snprintf(resstr,10,"result=%d",cond_result);
  21.599 -                        #endif
  21.600 -                        DEBUG_LOG("FSM condition", "sync_fsm.c, state=WaitForGroupKeysSole, event=GroupKeys, condition=sameKeyAndAddress", resstr)
  21.601 -                        if (cond_result < 0)
  21.602 -                            return cond_result;
  21.603 -                        if (cond_result) {
  21.604 -                        DEBUG_LOG("FSM action", "sync_fsm.c, state=WaitForGroupKeysSole, event=GroupKeys", "action=storeGroupKeys")
  21.605 -                        status = storeGroupKeys(session, state, partner, groupkeys);
  21.606 -                        if (status == PEP_OUT_OF_MEMORY)
  21.607 -                            return (int) invalid_out_of_memory;
  21.608 -                        if (status != PEP_STATUS_OK)
  21.609 -                            return (int) invalid_action;
  21.610 -                        DEBUG_LOG("FSM action", "sync_fsm.c, state=WaitForGroupKeysSole, event=GroupKeys", "action=sendGroupUpdate")
  21.611 -                        status = sendGroupUpdate(session, state, NULL, NULL);
  21.612 -                        if (status == PEP_OUT_OF_MEMORY)
  21.613 -                            return (int) invalid_out_of_memory;
  21.614 -                        if (status != PEP_STATUS_OK)
  21.615 -                            return (int) invalid_action;
  21.616 -                        DEBUG_LOG("FSM action", "sync_fsm.c, state=WaitForGroupKeysSole, event=GroupKeys", "action=renewUUID")
  21.617 -                        status = renewUUID(session, state, NULL, NULL);
  21.618 -                        if (status == PEP_OUT_OF_MEMORY)
  21.619 -                            return (int) invalid_out_of_memory;
  21.620 -                        if (status != PEP_STATUS_OK)
  21.621 -                            return (int) invalid_action;
  21.622 -                        DEBUG_LOG("FSM action", "sync_fsm.c, state=WaitForGroupKeysSole, event=GroupKeys", "action=notifyAcceptedDeviceAdded")
  21.623 -                        status = notifyAcceptedDeviceAdded(session, state, partner, NULL);
  21.624 -                        if (status == PEP_OUT_OF_MEMORY)
  21.625 -                            return (int) invalid_out_of_memory;
  21.626 -                        if (status != PEP_STATUS_OK)
  21.627 -                            return (int) invalid_action;
  21.628 -                        assert(session->sync_state_payload);
  21.629 -                        if(!session->sync_state_payload) return (DeviceState_state) invalid_state;
  21.630 -                        free_identity(((WaitForGroupKeysSole_state_payload_t*)session->sync_state_payload)->expected);
  21.631 -                        free(session->sync_state_payload);
  21.632 -                        session->sync_state_payload = NULL;
  21.633 -                        DEBUG_LOG("FSM transition", "sync_fsm.c, state=WaitForGroupKeysSole, event=GroupKeys", "target=Grouped")
  21.634 -                        return Grouped;
  21.635 -                        }
  21.636 -                    }
  21.637 -                    break;
  21.638 -                }
  21.639 -                case Timeout:
  21.640 -                {
  21.641 -                    DEBUG_LOG("FSM event", "sync_fsm.c, state=WaitForGroupKeysSole", "event=Timeout")
  21.642 -                    DEBUG_LOG("FSM action", "sync_fsm.c, state=WaitForGroupKeysSole, event=Timeout", "action=notifyTimeout")
  21.643 -                    status = notifyTimeout(session, state, expected, NULL);
  21.644 -                    if (status == PEP_OUT_OF_MEMORY)
  21.645 -                        return (int) invalid_out_of_memory;
  21.646 -                    if (status != PEP_STATUS_OK)
  21.647 -                        return (int) invalid_action;
  21.648 -                    assert(session->sync_state_payload);
  21.649 -                    if(!session->sync_state_payload) return (DeviceState_state) invalid_state;
  21.650 -                    free_identity(((WaitForGroupKeysSole_state_payload_t*)session->sync_state_payload)->expected);
  21.651 -                    free(session->sync_state_payload);
  21.652 -                    session->sync_state_payload = NULL;
  21.653 -                    DEBUG_LOG("FSM transition", "sync_fsm.c, state=WaitForGroupKeysSole, event=Timeout", "target=Sole")
  21.654 -                    return Sole;
  21.655 -                }
  21.656 -                default:
  21.657 -                    return (DeviceState_state) invalid_event;
  21.658 -            }
  21.659 -            break;
  21.660 -        }
  21.661 -        case WaitForAcceptSole:
  21.662 -        {
  21.663 -            DEBUG_LOG("Entering FSM state", "sync_fsm.c", "state=WaitForAcceptSole")
  21.664 -            assert(session->sync_state_payload);
  21.665 -            if(!session->sync_state_payload) return (DeviceState_state) invalid_state;
  21.666 -            Identity expected = ((WaitForAcceptSole_state_payload_t*)session->sync_state_payload)->expected;
  21.667 -            group_keys_extra_t* groupkeys = ((WaitForAcceptSole_state_payload_t*)session->sync_state_payload)->groupkeys;
  21.668 -            switch (event) {
  21.669 -                case Init: 
  21.670 -                    DEBUG_LOG("FSM event", "sync_fsm.c, state=WaitForAcceptSole", "event=Init") 
  21.671 -                    *timeout = 600;
  21.672 -                    break;
  21.673 -                case HandshakeRejected:
  21.674 -                {
  21.675 -                    DEBUG_LOG("FSM event", "sync_fsm.c, state=WaitForAcceptSole", "event=HandshakeRejected")
  21.676 -                    DEBUG_LOG("FSM action", "sync_fsm.c, state=WaitForAcceptSole, event=HandshakeRejected", "action=rejectHandshake")
  21.677 -                    status = rejectHandshake(session, state, partner, NULL);
  21.678 -                    if (status == PEP_OUT_OF_MEMORY)
  21.679 -                        return (int) invalid_out_of_memory;
  21.680 -                    if (status != PEP_STATUS_OK)
  21.681 -                        return (int) invalid_action;
  21.682 -                    assert(session->sync_state_payload);
  21.683 -                    if(!session->sync_state_payload) return (DeviceState_state) invalid_state;
  21.684 -                    free_identity(((WaitForAcceptSole_state_payload_t*)session->sync_state_payload)->expected);
  21.685 -                    free_group_keys_extra(((WaitForAcceptSole_state_payload_t*)session->sync_state_payload)->groupkeys);
  21.686 -                    free(session->sync_state_payload);
  21.687 -                    session->sync_state_payload = NULL;
  21.688 -                    DEBUG_LOG("FSM transition", "sync_fsm.c, state=WaitForAcceptSole, event=HandshakeRejected", "target=Sole")
  21.689 -                    return Sole;
  21.690 -                }
  21.691 -                case HandshakeAccepted:
  21.692 -                {
  21.693 -                    DEBUG_LOG("FSM event", "sync_fsm.c, state=WaitForAcceptSole", "event=HandshakeAccepted")
  21.694 -                    {
  21.695 -                        int cond_result = sameKeyAndAddress(session, partner, expected);
  21.696 -                        #ifndef NDEBUG
  21.697 -                        char resstr[11] = {0,};
  21.698 -                        snprintf(resstr,10,"result=%d",cond_result);
  21.699 -                        #endif
  21.700 -                        DEBUG_LOG("FSM condition", "sync_fsm.c, state=WaitForAcceptSole, event=HandshakeAccepted, condition=sameKeyAndAddress", resstr)
  21.701 -                        if (cond_result < 0)
  21.702 -                            return cond_result;
  21.703 -                        if (cond_result) {
  21.704 -                        DEBUG_LOG("FSM action", "sync_fsm.c, state=WaitForAcceptSole, event=HandshakeAccepted", "action=acceptHandshake")
  21.705 -                        status = acceptHandshake(session, state, partner, NULL);
  21.706 -                        if (status == PEP_OUT_OF_MEMORY)
  21.707 -                            return (int) invalid_out_of_memory;
  21.708 -                        if (status != PEP_STATUS_OK)
  21.709 -                            return (int) invalid_action;
  21.710 -                        DEBUG_LOG("FSM action", "sync_fsm.c, state=WaitForAcceptSole, event=HandshakeAccepted", "action=storeGroupKeys")
  21.711 -                        status = storeGroupKeys(session, state, partner, groupkeys);
  21.712 -                        if (status == PEP_OUT_OF_MEMORY)
  21.713 -                            return (int) invalid_out_of_memory;
  21.714 -                        if (status != PEP_STATUS_OK)
  21.715 -                            return (int) invalid_action;
  21.716 -                        DEBUG_LOG("FSM action", "sync_fsm.c, state=WaitForAcceptSole, event=HandshakeAccepted", "action=sendGroupUpdate")
  21.717 -                        status = sendGroupUpdate(session, state, NULL, NULL);
  21.718 -                        if (status == PEP_OUT_OF_MEMORY)
  21.719 -                            return (int) invalid_out_of_memory;
  21.720 -                        if (status != PEP_STATUS_OK)
  21.721 -                            return (int) invalid_action;
  21.722 -                        DEBUG_LOG("FSM action", "sync_fsm.c, state=WaitForAcceptSole, event=HandshakeAccepted", "action=renewUUID")
  21.723 -                        status = renewUUID(session, state, NULL, NULL);
  21.724 -                        if (status == PEP_OUT_OF_MEMORY)
  21.725 -                            return (int) invalid_out_of_memory;
  21.726 -                        if (status != PEP_STATUS_OK)
  21.727 -                            return (int) invalid_action;
  21.728 -                        DEBUG_LOG("FSM action", "sync_fsm.c, state=WaitForAcceptSole, event=HandshakeAccepted", "action=notifyAcceptedDeviceAdded")
  21.729 -                        status = notifyAcceptedDeviceAdded(session, state, partner, NULL);
  21.730 -                        if (status == PEP_OUT_OF_MEMORY)
  21.731 -                            return (int) invalid_out_of_memory;
  21.732 -                        if (status != PEP_STATUS_OK)
  21.733 -                            return (int) invalid_action;
  21.734 -                        assert(session->sync_state_payload);
  21.735 -                        if(!session->sync_state_payload) return (DeviceState_state) invalid_state;
  21.736 -                        free_identity(((WaitForAcceptSole_state_payload_t*)session->sync_state_payload)->expected);
  21.737 -                        free_group_keys_extra(((WaitForAcceptSole_state_payload_t*)session->sync_state_payload)->groupkeys);
  21.738 -                        free(session->sync_state_payload);
  21.739 -                        session->sync_state_payload = NULL;
  21.740 -                        DEBUG_LOG("FSM transition", "sync_fsm.c, state=WaitForAcceptSole, event=HandshakeAccepted", "target=Grouped")
  21.741 -                        return Grouped;
  21.742 -                        }
  21.743 -                    }
  21.744 -                    assert(session->sync_state_payload);
  21.745 -                    if(!session->sync_state_payload) return (DeviceState_state) invalid_state;
  21.746 -                    free_identity(((WaitForAcceptSole_state_payload_t*)session->sync_state_payload)->expected);
  21.747 -                    free_group_keys_extra(((WaitForAcceptSole_state_payload_t*)session->sync_state_payload)->groupkeys);
  21.748 -                    free(session->sync_state_payload);
  21.749 -                    session->sync_state_payload = NULL;
  21.750 -                    DEBUG_LOG("FSM transition", "sync_fsm.c, state=WaitForAcceptSole, event=HandshakeAccepted", "target=Sole")
  21.751 -                    return Sole;
  21.752 -                }
  21.753 -                case Cancel:
  21.754 -                {
  21.755 -                    DEBUG_LOG("FSM event", "sync_fsm.c, state=WaitForAcceptSole", "event=Cancel")
  21.756 -                    assert(session->sync_state_payload);
  21.757 -                    if(!session->sync_state_payload) return (DeviceState_state) invalid_state;
  21.758 -                    free_identity(((WaitForAcceptSole_state_payload_t*)session->sync_state_payload)->expected);
  21.759 -                    free_group_keys_extra(((WaitForAcceptSole_state_payload_t*)session->sync_state_payload)->groupkeys);
  21.760 -                    free(session->sync_state_payload);
  21.761 -                    session->sync_state_payload = NULL;
  21.762 -                    DEBUG_LOG("FSM transition", "sync_fsm.c, state=WaitForAcceptSole, event=Cancel", "target=Sole")
  21.763 -                    return Sole;
  21.764 -                }
  21.765 -                case Timeout:
  21.766 -                {
  21.767 -                    DEBUG_LOG("FSM event", "sync_fsm.c, state=WaitForAcceptSole", "event=Timeout")
  21.768 -                    DEBUG_LOG("FSM action", "sync_fsm.c, state=WaitForAcceptSole, event=Timeout", "action=notifyTimeout")
  21.769 -                    status = notifyTimeout(session, state, expected, NULL);
  21.770 -                    if (status == PEP_OUT_OF_MEMORY)
  21.771 -                        return (int) invalid_out_of_memory;
  21.772 -                    if (status != PEP_STATUS_OK)
  21.773 -                        return (int) invalid_action;
  21.774 -                    assert(session->sync_state_payload);
  21.775 -                    if(!session->sync_state_payload) return (DeviceState_state) invalid_state;
  21.776 -                    free_identity(((WaitForAcceptSole_state_payload_t*)session->sync_state_payload)->expected);
  21.777 -                    free_group_keys_extra(((WaitForAcceptSole_state_payload_t*)session->sync_state_payload)->groupkeys);
  21.778 -                    free(session->sync_state_payload);
  21.779 -                    session->sync_state_payload = NULL;
  21.780 -                    DEBUG_LOG("FSM transition", "sync_fsm.c, state=WaitForAcceptSole, event=Timeout", "target=Sole")
  21.781 -                    return Sole;
  21.782 -                }
  21.783 -                default:
  21.784 -                    return (DeviceState_state) invalid_event;
  21.785 -            }
  21.786 -            break;
  21.787 -        }
  21.788 -        case Grouped:
  21.789 -        {
  21.790 -            DEBUG_LOG("Entering FSM state", "sync_fsm.c", "state=Grouped")
  21.791 -            switch (event) {
  21.792 -                case Init: 
  21.793 -                    DEBUG_LOG("FSM event", "sync_fsm.c, state=Grouped", "event=Init") 
  21.794 -                    *timeout = 0;
  21.795 -                    break;
  21.796 -                case KeyGen:
  21.797 -                {
  21.798 -                    DEBUG_LOG("FSM event", "sync_fsm.c, state=Grouped", "event=KeyGen")
  21.799 -                    DEBUG_LOG("FSM action", "sync_fsm.c, state=Grouped, event=KeyGen", "action=sendGroupUpdate")
  21.800 -                    status = sendGroupUpdate(session, state, NULL, NULL);
  21.801 -                    if (status == PEP_OUT_OF_MEMORY)
  21.802 -                        return (int) invalid_out_of_memory;
  21.803 -                    if (status != PEP_STATUS_OK)
  21.804 -                        return (int) invalid_action;
  21.805 -                    break;
  21.806 -                }
  21.807 -                case CannotDecrypt:
  21.808 -                {
  21.809 -                    DEBUG_LOG("FSM event", "sync_fsm.c, state=Grouped", "event=CannotDecrypt")
  21.810 -                    DEBUG_LOG("FSM action", "sync_fsm.c, state=Grouped, event=CannotDecrypt", "action=sendUpdateRequest")
  21.811 -                    status = sendUpdateRequest(session, state, NULL, NULL);
  21.812 -                    if (status == PEP_OUT_OF_MEMORY)
  21.813 -                        return (int) invalid_out_of_memory;
  21.814 -                    if (status != PEP_STATUS_OK)
  21.815 -                        return (int) invalid_action;
  21.816 -                    DEBUG_LOG("FSM action", "sync_fsm.c, state=Grouped, event=CannotDecrypt", "action=sendBeacon")
  21.817 -                    status = sendBeacon(session, state, NULL, NULL);
  21.818 -                    if (status == PEP_OUT_OF_MEMORY)
  21.819 -                        return (int) invalid_out_of_memory;
  21.820 -                    if (status != PEP_STATUS_OK)
  21.821 -                        return (int) invalid_action;
  21.822 -                    DEBUG_LOG("FSM transition", "sync_fsm.c, state=Grouped, event=CannotDecrypt", "target=GroupWaiting")
  21.823 -                    return GroupWaiting;
  21.824 -                }
  21.825 -                case UpdateRequest:
  21.826 -                {
  21.827 -                    DEBUG_LOG("FSM event", "sync_fsm.c, state=Grouped", "event=UpdateRequest")
  21.828 -                    DEBUG_LOG("FSM action", "sync_fsm.c, state=Grouped, event=UpdateRequest", "action=sendGroupUpdate")
  21.829 -                    status = sendGroupUpdate(session, state, NULL, NULL);
  21.830 -                    if (status == PEP_OUT_OF_MEMORY)
  21.831 -                        return (int) invalid_out_of_memory;
  21.832 -                    if (status != PEP_STATUS_OK)
  21.833 -                        return (int) invalid_action;
  21.834 -                    break;
  21.835 -                }
  21.836 -                case Beacon:
  21.837 -                {
  21.838 -                    DEBUG_LOG("FSM event", "sync_fsm.c, state=Grouped", "event=Beacon")
  21.839 -                    DEBUG_LOG("FSM action", "sync_fsm.c, state=Grouped, event=Beacon", "action=sendHandshakeRequest")
  21.840 -                    status = sendHandshakeRequest(session, state, partner, NULL);
  21.841 -                    if (status == PEP_OUT_OF_MEMORY)
  21.842 -                        return (int) invalid_out_of_memory;
  21.843 -                    if (status != PEP_STATUS_OK)
  21.844 -                        return (int) invalid_action;
  21.845 -                    session->sync_state_payload = malloc(sizeof(GroupedBeaconed_state_payload_t));
  21.846 -                    assert(session->sync_state_payload);
  21.847 -                    if(!session->sync_state_payload) return (DeviceState_state) invalid_out_of_memory;
  21.848 -                    ((GroupedBeaconed_state_payload_t*)session->sync_state_payload)->expected =
  21.849 -                        identity_dup(partner);
  21.850 -                    DEBUG_LOG("FSM transition", "sync_fsm.c, state=Grouped, event=Beacon", "target=GroupedBeaconed")
  21.851 -                    return GroupedBeaconed;
  21.852 -                }
  21.853 -                case HandshakeRequest:
  21.854 -                {
  21.855 -                    DEBUG_LOG("FSM event", "sync_fsm.c, state=Grouped", "event=HandshakeRequest")
  21.856 -                    DEBUG_LOG("FSM action", "sync_fsm.c, state=Grouped, event=HandshakeRequest", "action=sendHandshakeRequest")
  21.857 -                    status = sendHandshakeRequest(session, state, partner, NULL);
  21.858 -                    if (status == PEP_OUT_OF_MEMORY)
  21.859 -                        return (int) invalid_out_of_memory;
  21.860 -                    if (status != PEP_STATUS_OK)
  21.861 -                        return (int) invalid_action;
  21.862 -                    session->sync_state_payload = malloc(sizeof(HandshakingGrouped_state_payload_t));
  21.863 -                    assert(session->sync_state_payload);
  21.864 -                    if(!session->sync_state_payload) return (DeviceState_state) invalid_out_of_memory;
  21.865 -                    ((HandshakingGrouped_state_payload_t*)session->sync_state_payload)->expected =
  21.866 -                        identity_dup(partner);
  21.867 -                    DEBUG_LOG("FSM transition", "sync_fsm.c, state=Grouped, event=HandshakeRequest", "target=HandshakingGrouped")
  21.868 -                    return HandshakingGrouped;
  21.869 -                }
  21.870 -                case GroupUpdate:
  21.871 -                {
  21.872 -                    DEBUG_LOG("FSM event", "sync_fsm.c, state=Grouped", "event=GroupUpdate")
  21.873 -                    identity_list* keys = (identity_list*)extra;
  21.874 -                    DEBUG_LOG("FSM action", "sync_fsm.c, state=Grouped, event=GroupUpdate", "action=storeGroupUpdate")
  21.875 -                    status = storeGroupUpdate(session, state, partner, keys);
  21.876 -                    if (status == PEP_OUT_OF_MEMORY)
  21.877 -                        return (int) invalid_out_of_memory;
  21.878 -                    if (status != PEP_STATUS_OK)
  21.879 -                        return (int) invalid_action;
  21.880 -                    break;
  21.881 -                }
  21.882 -                default:
  21.883 -                    return (DeviceState_state) invalid_event;
  21.884 -            }
  21.885 -            break;
  21.886 -        }
  21.887 -        case GroupWaiting:
  21.888 -        {
  21.889 -            DEBUG_LOG("Entering FSM state", "sync_fsm.c", "state=GroupWaiting")
  21.890 -            switch (event) {
  21.891 -                case Init: 
  21.892 -                    DEBUG_LOG("FSM event", "sync_fsm.c, state=GroupWaiting", "event=Init") 
  21.893 -                    *timeout = 60;
  21.894 -                    break;
  21.895 -                case KeyGen:
  21.896 -                {
  21.897 -                    DEBUG_LOG("FSM event", "sync_fsm.c, state=GroupWaiting", "event=KeyGen")
  21.898 -                    DEBUG_LOG("FSM action", "sync_fsm.c, state=GroupWaiting, event=KeyGen", "action=sendGroupUpdate")
  21.899 -                    status = sendGroupUpdate(session, state, NULL, NULL);
  21.900 -                    if (status == PEP_OUT_OF_MEMORY)
  21.901 -                        return (int) invalid_out_of_memory;
  21.902 -                    if (status != PEP_STATUS_OK)
  21.903 -                        return (int) invalid_action;
  21.904 -                    break;
  21.905 -                }
  21.906 -                case CannotDecrypt:
  21.907 -                {
  21.908 -                    DEBUG_LOG("FSM event", "sync_fsm.c, state=GroupWaiting", "event=CannotDecrypt")
  21.909 -                    DEBUG_LOG("FSM action", "sync_fsm.c, state=GroupWaiting, event=CannotDecrypt", "action=sendUpdateRequest")
  21.910 -                    status = sendUpdateRequest(session, state, NULL, NULL);
  21.911 -                    if (status == PEP_OUT_OF_MEMORY)
  21.912 -                        return (int) invalid_out_of_memory;
  21.913 -                    if (status != PEP_STATUS_OK)
  21.914 -                        return (int) invalid_action;
  21.915 -                    DEBUG_LOG("FSM action", "sync_fsm.c, state=GroupWaiting, event=CannotDecrypt", "action=sendBeacon")
  21.916 -                    status = sendBeacon(session, state, NULL, NULL);
  21.917 -                    if (status == PEP_OUT_OF_MEMORY)
  21.918 -                        return (int) invalid_out_of_memory;
  21.919 -                    if (status != PEP_STATUS_OK)
  21.920 -                        return (int) invalid_action;
  21.921 -                    break;
  21.922 -                }
  21.923 -                case UpdateRequest:
  21.924 -                {
  21.925 -                    DEBUG_LOG("FSM event", "sync_fsm.c, state=GroupWaiting", "event=UpdateRequest")
  21.926 -                    DEBUG_LOG("FSM action", "sync_fsm.c, state=GroupWaiting, event=UpdateRequest", "action=sendGroupUpdate")
  21.927 -                    status = sendGroupUpdate(session, state, NULL, NULL);
  21.928 -                    if (status == PEP_OUT_OF_MEMORY)
  21.929 -                        return (int) invalid_out_of_memory;
  21.930 -                    if (status != PEP_STATUS_OK)
  21.931 -                        return (int) invalid_action;
  21.932 -                    break;
  21.933 -                }
  21.934 -                case Beacon:
  21.935 -                {
  21.936 -                    DEBUG_LOG("FSM event", "sync_fsm.c, state=GroupWaiting", "event=Beacon")
  21.937 -                    DEBUG_LOG("FSM action", "sync_fsm.c, state=GroupWaiting, event=Beacon", "action=sendHandshakeRequest")
  21.938 -                    status = sendHandshakeRequest(session, state, partner, NULL);
  21.939 -                    if (status == PEP_OUT_OF_MEMORY)
  21.940 -                        return (int) invalid_out_of_memory;
  21.941 -                    if (status != PEP_STATUS_OK)
  21.942 -                        return (int) invalid_action;
  21.943 -                    session->sync_state_payload = malloc(sizeof(GroupedBeaconed_state_payload_t));
  21.944 -                    assert(session->sync_state_payload);
  21.945 -                    if(!session->sync_state_payload) return (DeviceState_state) invalid_out_of_memory;
  21.946 -                    ((GroupedBeaconed_state_payload_t*)session->sync_state_payload)->expected =
  21.947 -                        identity_dup(partner);
  21.948 -                    DEBUG_LOG("FSM transition", "sync_fsm.c, state=GroupWaiting, event=Beacon", "target=GroupedBeaconed")
  21.949 -                    return GroupedBeaconed;
  21.950 -                }
  21.951 -                case HandshakeRequest:
  21.952 -                {
  21.953 -                    DEBUG_LOG("FSM event", "sync_fsm.c, state=GroupWaiting", "event=HandshakeRequest")
  21.954 -                    DEBUG_LOG("FSM action", "sync_fsm.c, state=GroupWaiting, event=HandshakeRequest", "action=sendHandshakeRequest")
  21.955 -                    status = sendHandshakeRequest(session, state, partner, NULL);
  21.956 -                    if (status == PEP_OUT_OF_MEMORY)
  21.957 -                        return (int) invalid_out_of_memory;
  21.958 -                    if (status != PEP_STATUS_OK)
  21.959 -                        return (int) invalid_action;
  21.960 -                    session->sync_state_payload = malloc(sizeof(HandshakingGrouped_state_payload_t));
  21.961 -                    assert(session->sync_state_payload);
  21.962 -                    if(!session->sync_state_payload) return (DeviceState_state) invalid_out_of_memory;
  21.963 -                    ((HandshakingGrouped_state_payload_t*)session->sync_state_payload)->expected =
  21.964 -                        identity_dup(partner);
  21.965 -                    DEBUG_LOG("FSM transition", "sync_fsm.c, state=GroupWaiting, event=HandshakeRequest", "target=HandshakingGrouped")
  21.966 -                    return HandshakingGrouped;
  21.967 -                }
  21.968 -                case GroupUpdate:
  21.969 -                {
  21.970 -                    DEBUG_LOG("FSM event", "sync_fsm.c, state=GroupWaiting", "event=GroupUpdate")
  21.971 -                    identity_list* keys = (identity_list*)extra;
  21.972 -                    DEBUG_LOG("FSM action", "sync_fsm.c, state=GroupWaiting, event=GroupUpdate", "action=storeGroupUpdate")
  21.973 -                    status = storeGroupUpdate(session, state, partner, keys);
  21.974 -                    if (status == PEP_OUT_OF_MEMORY)
  21.975 -                        return (int) invalid_out_of_memory;
  21.976 -                    if (status != PEP_STATUS_OK)
  21.977 -                        return (int) invalid_action;
  21.978 -                    break;
  21.979 -                }
  21.980 -                case Timeout:
  21.981 -                {
  21.982 -                    DEBUG_LOG("FSM event", "sync_fsm.c, state=GroupWaiting", "event=Timeout")
  21.983 -                    DEBUG_LOG("FSM transition", "sync_fsm.c, state=GroupWaiting, event=Timeout", "target=Grouped")
  21.984 -                    return Grouped;
  21.985 -                }
  21.986 -                default:
  21.987 -                    return (DeviceState_state) invalid_event;
  21.988 -            }
  21.989 -            break;
  21.990 -        }
  21.991 -        case GroupedBeaconed:
  21.992 -        {
  21.993 -            DEBUG_LOG("Entering FSM state", "sync_fsm.c", "state=GroupedBeaconed")
  21.994 -            assert(session->sync_state_payload);
  21.995 -            if(!session->sync_state_payload) return (DeviceState_state) invalid_state;
  21.996 -            Identity expected = ((GroupedBeaconed_state_payload_t*)session->sync_state_payload)->expected;
  21.997 -            switch (event) {
  21.998 -                case Init: 
  21.999 -                    DEBUG_LOG("FSM event", "sync_fsm.c, state=GroupedBeaconed", "event=Init") 
 21.1000 -                    *timeout = 600;
 21.1001 -                    break;
 21.1002 -                case KeyGen:
 21.1003 -                {
 21.1004 -                    DEBUG_LOG("FSM event", "sync_fsm.c, state=GroupedBeaconed", "event=KeyGen")
 21.1005 -                    DEBUG_LOG("FSM action", "sync_fsm.c, state=GroupedBeaconed, event=KeyGen", "action=sendGroupUpdate")
 21.1006 -                    status = sendGroupUpdate(session, state, NULL, NULL);
 21.1007 -                    if (status == PEP_OUT_OF_MEMORY)
 21.1008 -                        return (int) invalid_out_of_memory;
 21.1009 -                    if (status != PEP_STATUS_OK)
 21.1010 -                        return (int) invalid_action;
 21.1011 -                    break;
 21.1012 -                }
 21.1013 -                case CannotDecrypt:
 21.1014 -                {
 21.1015 -                    DEBUG_LOG("FSM event", "sync_fsm.c, state=GroupedBeaconed", "event=CannotDecrypt")
 21.1016 -                    DEBUG_LOG("FSM action", "sync_fsm.c, state=GroupedBeaconed, event=CannotDecrypt", "action=sendUpdateRequest")
 21.1017 -                    status = sendUpdateRequest(session, state, NULL, NULL);
 21.1018 -                    if (status == PEP_OUT_OF_MEMORY)
 21.1019 -                        return (int) invalid_out_of_memory;
 21.1020 -                    if (status != PEP_STATUS_OK)
 21.1021 -                        return (int) invalid_action;
 21.1022 -                    DEBUG_LOG("FSM action", "sync_fsm.c, state=GroupedBeaconed, event=CannotDecrypt", "action=sendBeacon")
 21.1023 -                    status = sendBeacon(session, state, NULL, NULL);
 21.1024 -                    if (status == PEP_OUT_OF_MEMORY)
 21.1025 -                        return (int) invalid_out_of_memory;
 21.1026 -                    if (status != PEP_STATUS_OK)
 21.1027 -                        return (int) invalid_action;
 21.1028 -                    break;
 21.1029 -                }
 21.1030 -                case UpdateRequest:
 21.1031 -                {
 21.1032 -                    DEBUG_LOG("FSM event", "sync_fsm.c, state=GroupedBeaconed", "event=UpdateRequest")
 21.1033 -                    DEBUG_LOG("FSM action", "sync_fsm.c, state=GroupedBeaconed, event=UpdateRequest", "action=sendGroupUpdate")
 21.1034 -                    status = sendGroupUpdate(session, state, NULL, NULL);
 21.1035 -                    if (status == PEP_OUT_OF_MEMORY)
 21.1036 -                        return (int) invalid_out_of_memory;
 21.1037 -                    if (status != PEP_STATUS_OK)
 21.1038 -                        return (int) invalid_action;
 21.1039 -                    break;
 21.1040 -                }
 21.1041 -                case Beacon:
 21.1042 -                {
 21.1043 -                    DEBUG_LOG("FSM event", "sync_fsm.c, state=GroupedBeaconed", "event=Beacon")
 21.1044 -                    DEBUG_LOG("FSM action", "sync_fsm.c, state=GroupedBeaconed, event=Beacon", "action=sendHandshakeRequest")
 21.1045 -                    status = sendHandshakeRequest(session, state, partner, NULL);
 21.1046 -                    if (status == PEP_OUT_OF_MEMORY)
 21.1047 -                        return (int) invalid_out_of_memory;
 21.1048 -                    if (status != PEP_STATUS_OK)
 21.1049 -                        return (int) invalid_action;
 21.1050 -                    assert(session->sync_state_payload);
 21.1051 -                    if(!session->sync_state_payload) return (DeviceState_state) invalid_state;
 21.1052 -                    free_identity(((GroupedBeaconed_state_payload_t*)session->sync_state_payload)->expected);
 21.1053 -                    free(session->sync_state_payload);
 21.1054 -                    session->sync_state_payload = NULL;
 21.1055 -                    session->sync_state_payload = malloc(sizeof(GroupedBeaconed_state_payload_t));
 21.1056 -                    assert(session->sync_state_payload);
 21.1057 -                    if(!session->sync_state_payload) return (DeviceState_state) invalid_out_of_memory;
 21.1058 -                    ((GroupedBeaconed_state_payload_t*)session->sync_state_payload)->expected =
 21.1059 -                        identity_dup(partner);
 21.1060 -                    DEBUG_LOG("FSM transition", "sync_fsm.c, state=GroupedBeaconed, event=Beacon", "target=GroupedBeaconed")
 21.1061 -                    return GroupedBeaconed;
 21.1062 -                }
 21.1063 -                case HandshakeRequest:
 21.1064 -                {
 21.1065 -                    DEBUG_LOG("FSM event", "sync_fsm.c, state=GroupedBeaconed", "event=HandshakeRequest")
 21.1066 -                    {
 21.1067 -                        int cond_result = sameIdentities(session, partner, expected);
 21.1068 -                        #ifndef NDEBUG
 21.1069 -                        char resstr[11] = {0,};
 21.1070 -                        snprintf(resstr,10,"result=%d",cond_result);
 21.1071 -                        #endif
 21.1072 -                        DEBUG_LOG("FSM condition", "sync_fsm.c, state=GroupedBeaconed, event=HandshakeRequest, condition=sameIdentities", resstr)
 21.1073 -                        if (cond_result < 0)
 21.1074 -                            return cond_result;
 21.1075 -                        if (cond_result) {
 21.1076 -                        }
 21.1077 -                        else {
 21.1078 -                        DEBUG_LOG("FSM action", "sync_fsm.c, state=GroupedBeaconed, event=HandshakeRequest", "action=sendHandshakeRequest")
 21.1079 -                        status = sendHandshakeRequest(session, state, partner, NULL);
 21.1080 -                        if (status == PEP_OUT_OF_MEMORY)
 21.1081 -                            return (int) invalid_out_of_memory;
 21.1082 -                        if (status != PEP_STATUS_OK)
 21.1083 -                            return (int) invalid_action;
 21.1084 -                        }
 21.1085 -                    }
 21.1086 -                    assert(session->sync_state_payload);
 21.1087 -                    if(!session->sync_state_payload) return (DeviceState_state) invalid_state;
 21.1088 -                    free_identity(((GroupedBeaconed_state_payload_t*)session->sync_state_payload)->expected);
 21.1089 -                    free(session->sync_state_payload);
 21.1090 -                    session->sync_state_payload = NULL;
 21.1091 -                    session->sync_state_payload = malloc(sizeof(HandshakingGrouped_state_payload_t));
 21.1092 -                    assert(session->sync_state_payload);
 21.1093 -                    if(!session->sync_state_payload) return (DeviceState_state) invalid_out_of_memory;
 21.1094 -                    ((HandshakingGrouped_state_payload_t*)session->sync_state_payload)->expected =
 21.1095 -                        identity_dup(partner);
 21.1096 -                    DEBUG_LOG("FSM transition", "sync_fsm.c, state=GroupedBeaconed, event=HandshakeRequest", "target=HandshakingGrouped")
 21.1097 -                    return HandshakingGrouped;
 21.1098 -                }
 21.1099 -                case GroupUpdate:
 21.1100 -                {
 21.1101 -                    DEBUG_LOG("FSM event", "sync_fsm.c, state=GroupedBeaconed", "event=GroupUpdate")
 21.1102 -                    identity_list* keys = (identity_list*)extra;
 21.1103 -                    DEBUG_LOG("FSM action", "sync_fsm.c, state=GroupedBeaconed, event=GroupUpdate", "action=storeGroupUpdate")
 21.1104 -                    status = storeGroupUpdate(session, state, partner, keys);
 21.1105 -                    if (status == PEP_OUT_OF_MEMORY)
 21.1106 -                        return (int) invalid_out_of_memory;
 21.1107 -                    if (status != PEP_STATUS_OK)
 21.1108 -                        return (int) invalid_action;
 21.1109 -                    break;
 21.1110 -                }
 21.1111 -                case Timeout:
 21.1112 -                {
 21.1113 -                    DEBUG_LOG("FSM event", "sync_fsm.c, state=GroupedBeaconed", "event=Timeout")
 21.1114 -                    assert(session->sync_state_payload);
 21.1115 -                    if(!session->sync_state_payload) return (DeviceState_state) invalid_state;
 21.1116 -                    free_identity(((GroupedBeaconed_state_payload_t*)session->sync_state_payload)->expected);
 21.1117 -                    free(session->sync_state_payload);
 21.1118 -                    session->sync_state_payload = NULL;
 21.1119 -                    DEBUG_LOG("FSM transition", "sync_fsm.c, state=GroupedBeaconed, event=Timeout", "target=Grouped")
 21.1120 -                    return Grouped;
 21.1121 -                }
 21.1122 -                default:
 21.1123 -                    return (DeviceState_state) invalid_event;
 21.1124 -            }
 21.1125 -            break;
 21.1126 -        }
 21.1127 -        case HandshakingGrouped:
 21.1128 -        {
 21.1129 -            DEBUG_LOG("Entering FSM state", "sync_fsm.c", "state=HandshakingGrouped")
 21.1130 -            assert(session->sync_state_payload);
 21.1131 -            if(!session->sync_state_payload) return (DeviceState_state) invalid_state;
 21.1132 -            Identity expected = ((HandshakingGrouped_state_payload_t*)session->sync_state_payload)->expected;
 21.1133 -            switch (event) {
 21.1134 -                case Init:
 21.1135 -                {
 21.1136 -                    DEBUG_LOG("FSM event", "sync_fsm.c, state=HandshakingGrouped", "event=Init")
 21.1137 -                    *timeout = 600;
 21.1138 -                    {
 21.1139 -                        int cond_result = keyElectionWon(session, expected);
 21.1140 -                        #ifndef NDEBUG
 21.1141 -                        char resstr[11] = {0,};
 21.1142 -                        snprintf(resstr,10,"result=%d",cond_result);
 21.1143 -                        #endif
 21.1144 -                        DEBUG_LOG("FSM condition", "sync_fsm.c, state=HandshakingGrouped, event=Init, condition=keyElectionWon", resstr)
 21.1145 -                        if (cond_result < 0)
 21.1146 -                            return cond_result;
 21.1147 -                        if (cond_result) {
 21.1148 -                        DEBUG_LOG("FSM action", "sync_fsm.c, state=HandshakingGrouped, event=Init", "action=notifyInitAddOtherDevice")
 21.1149 -                        status = notifyInitAddOtherDevice(session, state, partner, NULL);
 21.1150 -                        if (status == PEP_OUT_OF_MEMORY)
 21.1151 -                            return (int) invalid_out_of_memory;
 21.1152 -                        if (status != PEP_STATUS_OK)
 21.1153 -                            return (int) invalid_action;
 21.1154 -                        }
 21.1155 -                        else {
 21.1156 -                        DEBUG_LOG("FSM action", "sync_fsm.c, state=HandshakingGrouped, event=Init", "action=notifyInitMoveOurDevice")
 21.1157 -                        status = notifyInitMoveOurDevice(session, state, partner, NULL);
 21.1158 -                        if (status == PEP_OUT_OF_MEMORY)
 21.1159 -                            return (int) invalid_out_of_memory;
 21.1160 -                        if (status != PEP_STATUS_OK)
 21.1161 -                            return (int) invalid_action;
 21.1162 -                        }
 21.1163 -                    }
 21.1164 -                    break;
 21.1165 -                }
 21.1166 -                case HandshakeRejected:
 21.1167 -                {
 21.1168 -                    DEBUG_LOG("FSM event", "sync_fsm.c, state=HandshakingGrouped", "event=HandshakeRejected")
 21.1169 -                    DEBUG_LOG("FSM action", "sync_fsm.c, state=HandshakingGrouped, event=HandshakeRejected", "action=rejectHandshake")
 21.1170 -                    status = rejectHandshake(session, state, partner, NULL);
 21.1171 -                    if (status == PEP_OUT_OF_MEMORY)
 21.1172 -                        return (int) invalid_out_of_memory;
 21.1173 -                    if (status != PEP_STATUS_OK)
 21.1174 -                        return (int) invalid_action;
 21.1175 -                    DEBUG_LOG("FSM action", "sync_fsm.c, state=HandshakingGrouped, event=HandshakeRejected", "action=sendGroupUpdate")
 21.1176 -                    status = sendGroupUpdate(session, state, NULL, NULL);
 21.1177 -                    if (status == PEP_OUT_OF_MEMORY)
 21.1178 -                        return (int) invalid_out_of_memory;
 21.1179 -                    if (status != PEP_STATUS_OK)
 21.1180 -                        return (int) invalid_action;
 21.1181 -                    assert(session->sync_state_payload);
 21.1182 -                    if(!session->sync_state_payload) return (DeviceState_state) invalid_state;
 21.1183 -                    free_identity(((HandshakingGrouped_state_payload_t*)session->sync_state_payload)->expected);
 21.1184 -                    free(session->sync_state_payload);
 21.1185 -                    session->sync_state_payload = NULL;
 21.1186 -                    DEBUG_LOG("FSM transition", "sync_fsm.c, state=HandshakingGrouped, event=HandshakeRejected", "target=Grouped")
 21.1187 -                    return Grouped;
 21.1188 -                }
 21.1189 -                case HandshakeAccepted:
 21.1190 -                {
 21.1191 -                    DEBUG_LOG("FSM event", "sync_fsm.c, state=HandshakingGrouped", "event=HandshakeAccepted")
 21.1192 -                    DEBUG_LOG("FSM action", "sync_fsm.c, state=HandshakingGrouped, event=HandshakeAccepted", "action=acceptHandshake")
 21.1193 -                    status = acceptHandshake(session, state, partner, NULL);
 21.1194 -                    if (status == PEP_OUT_OF_MEMORY)
 21.1195 -                        return (int) invalid_out_of_memory;
 21.1196 -                    if (status != PEP_STATUS_OK)
 21.1197 -                        return (int) invalid_action;
 21.1198 -                    DEBUG_LOG("FSM action", "sync_fsm.c, state=HandshakingGrouped, event=HandshakeAccepted", "action=sendGroupUpdate")
 21.1199 -                    status = sendGroupUpdate(session, state, NULL, NULL);
 21.1200 -                    if (status == PEP_OUT_OF_MEMORY)
 21.1201 -                        return (int) invalid_out_of_memory;
 21.1202 -                    if (status != PEP_STATUS_OK)
 21.1203 -                        return (int) invalid_action;
 21.1204 -                    {
 21.1205 -                        int cond_result = keyElectionWon(session, partner);
 21.1206 -                        #ifndef NDEBUG
 21.1207 -                        char resstr[11] = {0,};
 21.1208 -                        snprintf(resstr,10,"result=%d",cond_result);
 21.1209 -                        #endif
 21.1210 -                        DEBUG_LOG("FSM condition", "sync_fsm.c, state=HandshakingGrouped, event=HandshakeAccepted, condition=keyElectionWon", resstr)
 21.1211 -                        if (cond_result < 0)
 21.1212 -                            return cond_result;
 21.1213 -                        if (cond_result) {
 21.1214 -                        DEBUG_LOG("FSM action", "sync_fsm.c, state=HandshakingGrouped, event=HandshakeAccepted", "action=sendGroupKeys")
 21.1215 -                        status = sendGroupKeys(session, state, partner, NULL);
 21.1216 -                        if (status == PEP_OUT_OF_MEMORY)
 21.1217 -                            return (int) invalid_out_of_memory;
 21.1218 -                        if (status != PEP_STATUS_OK)
 21.1219 -                            return (int) invalid_action;
 21.1220 -                        DEBUG_LOG("FSM action", "sync_fsm.c, state=HandshakingGrouped, event=HandshakeAccepted", "action=notifyAcceptedDeviceAdded")
 21.1221 -                        status = notifyAcceptedDeviceAdded(session, state, partner, NULL);
 21.1222 -                        if (status == PEP_OUT_OF_MEMORY)
 21.1223 -                            return (int) invalid_out_of_memory;
 21.1224 -                        if (status != PEP_STATUS_OK)
 21.1225 -                            return (int) invalid_action;
 21.1226 -                        assert(session->sync_state_payload);
 21.1227 -                        if(!session->sync_state_payload) return (DeviceState_state) invalid_state;
 21.1228 -                        free_identity(((HandshakingGrouped_state_payload_t*)session->sync_state_payload)->expected);
 21.1229 -                        free(session->sync_state_payload);
 21.1230 -                        session->sync_state_payload = NULL;
 21.1231 -                        DEBUG_LOG("FSM transition", "sync_fsm.c, state=HandshakingGrouped, event=HandshakeAccepted", "target=Grouped")
 21.1232 -                        return Grouped;
 21.1233 -                        }
 21.1234 -                    }
 21.1235 -                    assert(session->sync_state_payload);
 21.1236 -                    if(!session->sync_state_payload) return (DeviceState_state) invalid_state;
 21.1237 -                    free_identity(((HandshakingGrouped_state_payload_t*)session->sync_state_payload)->expected);
 21.1238 -                    free(session->sync_state_payload);
 21.1239 -                    session->sync_state_payload = NULL;
 21.1240 -                    session->sync_state_payload = malloc(sizeof(WaitForGroupKeysGrouped_state_payload_t));
 21.1241 -                    assert(session->sync_state_payload);
 21.1242 -                    if(!session->sync_state_payload) return (DeviceState_state) invalid_out_of_memory;
 21.1243 -                    ((WaitForGroupKeysGrouped_state_payload_t*)session->sync_state_payload)->expected =
 21.1244 -                        identity_dup(partner);
 21.1245 -                    DEBUG_LOG("FSM transition", "sync_fsm.c, state=HandshakingGrouped, event=HandshakeAccepted", "target=WaitForGroupKeysGrouped")
 21.1246 -                    return WaitForGroupKeysGrouped;
 21.1247 -                }
 21.1248 -                case Cancel:
 21.1249 -                {
 21.1250 -                    DEBUG_LOG("FSM event", "sync_fsm.c, state=HandshakingGrouped", "event=Cancel")
 21.1251 -                    assert(session->sync_state_payload);
 21.1252 -                    if(!session->sync_state_payload) return (DeviceState_state) invalid_state;
 21.1253 -                    free_identity(((HandshakingGrouped_state_payload_t*)session->sync_state_payload)->expected);
 21.1254 -                    free(session->sync_state_payload);
 21.1255 -                    session->sync_state_payload = NULL;
 21.1256 -                    DEBUG_LOG("FSM transition", "sync_fsm.c, state=HandshakingGrouped, event=Cancel", "target=Grouped")
 21.1257 -                    return Grouped;
 21.1258 -                }
 21.1259 -                case GroupKeys:
 21.1260 -                {
 21.1261 -                    DEBUG_LOG("FSM event", "sync_fsm.c, state=HandshakingGrouped", "event=GroupKeys")
 21.1262 -                    group_keys_extra_t* groupkeys = (group_keys_extra_t*)extra;
 21.1263 -                    {
 21.1264 -                        int cond_result = keyElectionWon(session, expected);
 21.1265 -                        #ifndef NDEBUG
 21.1266 -                        char resstr[11] = {0,};
 21.1267 -                        snprintf(resstr,10,"result=%d",cond_result);
 21.1268 -                        #endif
 21.1269 -                        DEBUG_LOG("FSM condition", "sync_fsm.c, state=HandshakingGrouped, event=GroupKeys, condition=keyElectionWon", resstr)
 21.1270 -                        if (cond_result < 0)
 21.1271 -                            return cond_result;
 21.1272 -                        if (cond_result) {
 21.1273 -                        }
 21.1274 -                        else {
 21.1275 -                        {
 21.1276 -                            int cond_result = sameKeyAndAddress(session, partner, expected);
 21.1277 -                            #ifndef NDEBUG
 21.1278 -                            char resstr[11] = {0,};
 21.1279 -                            snprintf(resstr,10,"result=%d",cond_result);
 21.1280 -                            #endif
 21.1281 -                            DEBUG_LOG("FSM condition", "sync_fsm.c, state=HandshakingGrouped, event=GroupKeys, condition=sameKeyAndAddress", resstr)
 21.1282 -                            if (cond_result < 0)
 21.1283 -                                return cond_result;
 21.1284 -                            if (cond_result) {
 21.1285 -                            assert(session->sync_state_payload);
 21.1286 -                            if(!session->sync_state_payload) return (DeviceState_state) invalid_state;
 21.1287 -                            free_identity(((HandshakingGrouped_state_payload_t*)session->sync_state_payload)->expected);
 21.1288 -                            free(session->sync_state_payload);
 21.1289 -                            session->sync_state_payload = NULL;
 21.1290 -                            session->sync_state_payload = malloc(sizeof(WaitForAcceptGrouped_state_payload_t));
 21.1291 -                            assert(session->sync_state_payload);
 21.1292 -                            if(!session->sync_state_payload) return (DeviceState_state) invalid_out_of_memory;
 21.1293 -                            ((WaitForAcceptGrouped_state_payload_t*)session->sync_state_payload)->expected =
 21.1294 -                                identity_dup(partner);
 21.1295 -                            ((WaitForAcceptGrouped_state_payload_t*)session->sync_state_payload)->groupkeys =
 21.1296 -                                group_keys_extra_dup(groupkeys);
 21.1297 -                            DEBUG_LOG("FSM transition", "sync_fsm.c, state=HandshakingGrouped, event=GroupKeys", "target=WaitForAcceptGrouped")
 21.1298 -                            return WaitForAcceptGrouped;
 21.1299 -                            }
 21.1300 -                        }
 21.1301 -                        }
 21.1302 -                    }
 21.1303 -                    break;
 21.1304 -                }
 21.1305 -                case GroupUpdate:
 21.1306 -                {
 21.1307 -                    DEBUG_LOG("FSM event", "sync_fsm.c, state=HandshakingGrouped", "event=GroupUpdate")
 21.1308 -                    identity_list* keys = (identity_list*)extra;
 21.1309 -                    DEBUG_LOG("FSM action", "sync_fsm.c, state=HandshakingGrouped, event=GroupUpdate", "action=notifyOvertaken")
 21.1310 -                    status = notifyOvertaken(session, state, partner, NULL);
 21.1311 -                    if (status == PEP_OUT_OF_MEMORY)
 21.1312 -                        return (int) invalid_out_of_memory;
 21.1313 -                    if (status != PEP_STATUS_OK)
 21.1314 -                        return (int) invalid_action;
 21.1315 -                    DEBUG_LOG("FSM action", "sync_fsm.c, state=HandshakingGrouped, event=GroupUpdate", "action=storeGroupUpdate")
 21.1316 -                    status = storeGroupUpdate(session, state, partner, keys);
 21.1317 -                    if (status == PEP_OUT_OF_MEMORY)
 21.1318 -                        return (int) invalid_out_of_memory;
 21.1319 -                    if (status != PEP_STATUS_OK)
 21.1320 -                        return (int) invalid_action;
 21.1321 -                    assert(session->sync_state_payload);
 21.1322 -                    if(!session->sync_state_payload) return (DeviceState_state) invalid_state;
 21.1323 -                    free_identity(((HandshakingGrouped_state_payload_t*)session->sync_state_payload)->expected);
 21.1324 -                    free(session->sync_state_payload);
 21.1325 -                    session->sync_state_payload = NULL;
 21.1326 -                    DEBUG_LOG("FSM transition", "sync_fsm.c, state=HandshakingGrouped, event=GroupUpdate", "target=Grouped")
 21.1327 -                    return Grouped;
 21.1328 -                }
 21.1329 -                case Timeout:
 21.1330 -                {
 21.1331 -                    DEBUG_LOG("FSM event", "sync_fsm.c, state=HandshakingGrouped", "event=Timeout")
 21.1332 -                    DEBUG_LOG("FSM action", "sync_fsm.c, state=HandshakingGrouped, event=Timeout", "action=notifyTimeout")
 21.1333 -                    status = notifyTimeout(session, state, expected, NULL);
 21.1334 -                    if (status == PEP_OUT_OF_MEMORY)
 21.1335 -                        return (int) invalid_out_of_memory;
 21.1336 -                    if (status != PEP_STATUS_OK)
 21.1337 -                        return (int) invalid_action;
 21.1338 -                    assert(session->sync_state_payload);
 21.1339 -                    if(!session->sync_state_payload) return (DeviceState_state) invalid_state;
 21.1340 -                    free_identity(((HandshakingGrouped_state_payload_t*)session->sync_state_payload)->expected);
 21.1341 -                    free(session->sync_state_payload);
 21.1342 -                    session->sync_state_payload = NULL;
 21.1343 -                    DEBUG_LOG("FSM transition", "sync_fsm.c, state=HandshakingGrouped, event=Timeout", "target=Grouped")
 21.1344 -                    return Grouped;
 21.1345 -                }
 21.1346 -                default:
 21.1347 -                    return (DeviceState_state) invalid_event;
 21.1348 -            }
 21.1349 -            break;
 21.1350 -        }
 21.1351 -        case WaitForGroupKeysGrouped:
 21.1352 -        {
 21.1353 -            DEBUG_LOG("Entering FSM state", "sync_fsm.c", "state=WaitForGroupKeysGrouped")
 21.1354 -            assert(session->sync_state_payload);
 21.1355 -            if(!session->sync_state_payload) return (DeviceState_state) invalid_state;
 21.1356 -            Identity expected = ((WaitForGroupKeysGrouped_state_payload_t*)session->sync_state_payload)->expected;
 21.1357 -            switch (event) {
 21.1358 -                case Init: 
 21.1359 -                    DEBUG_LOG("FSM event", "sync_fsm.c, state=WaitForGroupKeysGrouped", "event=Init") 
 21.1360 -                    *timeout = 600;
 21.1361 -                    break;
 21.1362 -                case GroupKeys:
 21.1363 -                {
 21.1364 -                    DEBUG_LOG("FSM event", "sync_fsm.c, state=WaitForGroupKeysGrouped", "event=GroupKeys")
 21.1365 -                    group_keys_extra_t* groupkeys = (group_keys_extra_t*)extra;
 21.1366 -                    {
 21.1367 -                        int cond_result = sameIdentities(session, partner, expected);
 21.1368 -                        #ifndef NDEBUG
 21.1369 -                        char resstr[11] = {0,};
 21.1370 -                        snprintf(resstr,10,"result=%d",cond_result);
 21.1371 -                        #endif
 21.1372 -                        DEBUG_LOG("FSM condition", "sync_fsm.c, state=WaitForGroupKeysGrouped, event=GroupKeys, condition=sameIdentities", resstr)
 21.1373 -                        if (cond_result < 0)
 21.1374 -                            return cond_result;
 21.1375 -                        if (cond_result) {
 21.1376 -                        DEBUG_LOG("FSM action", "sync_fsm.c, state=WaitForGroupKeysGrouped, event=GroupKeys", "action=storeGroupKeys")
 21.1377 -                        status = storeGroupKeys(session, state, partner, groupkeys);
 21.1378 -                        if (status == PEP_OUT_OF_MEMORY)
 21.1379 -                            return (int) invalid_out_of_memory;
 21.1380 -                        if (status != PEP_STATUS_OK)
 21.1381 -                            return (int) invalid_action;
 21.1382 -                        DEBUG_LOG("FSM action", "sync_fsm.c, state=WaitForGroupKeysGrouped, event=GroupKeys", "action=sendGroupUpdate")
 21.1383 -                        status = sendGroupUpdate(session, state, NULL, NULL);
 21.1384 -                        if (status == PEP_OUT_OF_MEMORY)
 21.1385 -                            return (int) invalid_out_of_memory;
 21.1386 -                        if (status != PEP_STATUS_OK)
 21.1387 -                            return (int) invalid_action;
 21.1388 -                        DEBUG_LOG("FSM action", "sync_fsm.c, state=WaitForGroupKeysGrouped, event=GroupKeys", "action=renewUUID")
 21.1389 -                        status = renewUUID(session, state, NULL, NULL);
 21.1390 -                        if (status == PEP_OUT_OF_MEMORY)
 21.1391 -                            return (int) invalid_out_of_memory;
 21.1392 -                        if (status != PEP_STATUS_OK)
 21.1393 -                            return (int) invalid_action;
 21.1394 -                        DEBUG_LOG("FSM action", "sync_fsm.c, state=WaitForGroupKeysGrouped, event=GroupKeys", "action=notifyAcceptedDeviceMoved")
 21.1395 -                        status = notifyAcceptedDeviceMoved(session, state, partner, NULL);
 21.1396 -                        if (status == PEP_OUT_OF_MEMORY)
 21.1397 -                            return (int) invalid_out_of_memory;
 21.1398 -                        if (status != PEP_STATUS_OK)
 21.1399 -                            return (int) invalid_action;
 21.1400 -                        assert(session->sync_state_payload);
 21.1401 -                        if(!session->sync_state_payload) return (DeviceState_state) invalid_state;
 21.1402 -                        free_identity(((WaitForGroupKeysGrouped_state_payload_t*)session->sync_state_payload)->expected);
 21.1403 -                        free(session->sync_state_payload);
 21.1404 -                        session->sync_state_payload = NULL;
 21.1405 -                        DEBUG_LOG("FSM transition", "sync_fsm.c, state=WaitForGroupKeysGrouped, event=GroupKeys", "target=Grouped")
 21.1406 -                        return Grouped;
 21.1407 -                        }
 21.1408 -                    }
 21.1409 -                    break;
 21.1410 -                }
 21.1411 -                case GroupUpdate:
 21.1412 -                {
 21.1413 -                    DEBUG_LOG("FSM event", "sync_fsm.c, state=WaitForGroupKeysGrouped", "event=GroupUpdate")
 21.1414 -                    identity_list* keys = (identity_list*)extra;
 21.1415 -                    DEBUG_LOG("FSM action", "sync_fsm.c, state=WaitForGroupKeysGrouped, event=GroupUpdate", "action=notifyOvertaken")
 21.1416 -                    status = notifyOvertaken(session, state, partner, NULL);
 21.1417 -                    if (status == PEP_OUT_OF_MEMORY)
 21.1418 -                        return (int) invalid_out_of_memory;
 21.1419 -                    if (status != PEP_STATUS_OK)
 21.1420 -                        return (int) invalid_action;
 21.1421 -                    DEBUG_LOG("FSM action", "sync_fsm.c, state=WaitForGroupKeysGrouped, event=GroupUpdate", "action=storeGroupUpdate")
 21.1422 -                    status = storeGroupUpdate(session, state, partner, keys);
 21.1423 -                    if (status == PEP_OUT_OF_MEMORY)
 21.1424 -                        return (int) invalid_out_of_memory;
 21.1425 -                    if (status != PEP_STATUS_OK)
 21.1426 -                        return (int) invalid_action;
 21.1427 -                    assert(session->sync_state_payload);
 21.1428 -                    if(!session->sync_state_payload) return (DeviceState_state) invalid_state;
 21.1429 -                    free_identity(((WaitForGroupKeysGrouped_state_payload_t*)session->sync_state_payload)->expected);
 21.1430 -                    free(session->sync_state_payload);
 21.1431 -                    session->sync_state_payload = NULL;
 21.1432 -                    DEBUG_LOG("FSM transition", "sync_fsm.c, state=WaitForGroupKeysGrouped, event=GroupUpdate", "target=Grouped")
 21.1433 -                    return Grouped;
 21.1434 -                }
 21.1435 -                case Timeout:
 21.1436 -                {
 21.1437 -                    DEBUG_LOG("FSM event", "sync_fsm.c, state=WaitForGroupKeysGrouped", "event=Timeout")
 21.1438 -                    DEBUG_LOG("FSM action", "sync_fsm.c, state=WaitForGroupKeysGrouped, event=Timeout", "action=notifyTimeout")
 21.1439 -                    status = notifyTimeout(session, state, expected, NULL);
 21.1440 -                    if (status == PEP_OUT_OF_MEMORY)
 21.1441 -                        return (int) invalid_out_of_memory;
 21.1442 -                    if (status != PEP_STATUS_OK)
 21.1443 -                        return (int) invalid_action;
 21.1444 -                    assert(session->sync_state_payload);
 21.1445 -                    if(!session->sync_state_payload) return (DeviceState_state) invalid_state;
 21.1446 -                    free_identity(((WaitForGroupKeysGrouped_state_payload_t*)session->sync_state_payload)->expected);
 21.1447 -                    free(session->sync_state_payload);
 21.1448 -                    session->sync_state_payload = NULL;
 21.1449 -                    DEBUG_LOG("FSM transition", "sync_fsm.c, state=WaitForGroupKeysGrouped, event=Timeout", "target=Grouped")
 21.1450 -                    return Grouped;
 21.1451 -                }
 21.1452 -                default:
 21.1453 -                    return (DeviceState_state) invalid_event;
 21.1454 -            }
 21.1455 -            break;
 21.1456 -        }
 21.1457 -        case WaitForAcceptGrouped:
 21.1458 -        {
 21.1459 -            DEBUG_LOG("Entering FSM state", "sync_fsm.c", "state=WaitForAcceptGrouped")
 21.1460 -            assert(session->sync_state_payload);
 21.1461 -            if(!session->sync_state_payload) return (DeviceState_state) invalid_state;
 21.1462 -            Identity expected = ((WaitForAcceptGrouped_state_payload_t*)session->sync_state_payload)->expected;
 21.1463 -            group_keys_extra_t* groupkeys = ((WaitForAcceptGrouped_state_payload_t*)session->sync_state_payload)->groupkeys;
 21.1464 -            switch (event) {
 21.1465 -                case Init: 
 21.1466 -                    DEBUG_LOG("FSM event", "sync_fsm.c, state=WaitForAcceptGrouped", "event=Init") 
 21.1467 -                    *timeout = 600;
 21.1468 -                    break;
 21.1469 -                case HandshakeRejected:
 21.1470 -                {
 21.1471 -                    DEBUG_LOG("FSM event", "sync_fsm.c, state=WaitForAcceptGrouped", "event=HandshakeRejected")
 21.1472 -                    DEBUG_LOG("FSM action", "sync_fsm.c, state=WaitForAcceptGrouped, event=HandshakeRejected", "action=rejectHandshake")
 21.1473 -                    status = rejectHandshake(session, state, partner, NULL);
 21.1474 -                    if (status == PEP_OUT_OF_MEMORY)
 21.1475 -                        return (int) invalid_out_of_memory;
 21.1476 -                    if (status != PEP_STATUS_OK)
 21.1477 -                        return (int) invalid_action;
 21.1478 -                    DEBUG_LOG("FSM action", "sync_fsm.c, state=WaitForAcceptGrouped, event=HandshakeRejected", "action=sendGroupUpdate")
 21.1479 -                    status = sendGroupUpdate(session, state, NULL, NULL);
 21.1480 -                    if (status == PEP_OUT_OF_MEMORY)
 21.1481 -                        return (int) invalid_out_of_memory;
 21.1482 -                    if (status != PEP_STATUS_OK)
 21.1483 -                        return (int) invalid_action;
 21.1484 -                    assert(session->sync_state_payload);
 21.1485 -                    if(!session->sync_state_payload) return (DeviceState_state) invalid_state;
 21.1486 -                    free_identity(((WaitForAcceptGrouped_state_payload_t*)session->sync_state_payload)->expected);
 21.1487 -                    free_group_keys_extra(((WaitForAcceptGrouped_state_payload_t*)session->sync_state_payload)->groupkeys);
 21.1488 -                    free(session->sync_state_payload);
 21.1489 -                    session->sync_state_payload = NULL;
 21.1490 -                    DEBUG_LOG("FSM transition", "sync_fsm.c, state=WaitForAcceptGrouped, event=HandshakeRejected", "target=Grouped")
 21.1491 -                    return Grouped;
 21.1492 -                }
 21.1493 -                case HandshakeAccepted:
 21.1494 -                {
 21.1495 -                    DEBUG_LOG("FSM event", "sync_fsm.c, state=WaitForAcceptGrouped", "event=HandshakeAccepted")
 21.1496 -                    {
 21.1497 -                        int cond_result = sameIdentities(session, partner, expected);
 21.1498 -                        #ifndef NDEBUG
 21.1499 -                        char resstr[11] = {0,};
 21.1500 -                        snprintf(resstr,10,"result=%d",cond_result);
 21.1501 -                        #endif
 21.1502 -                        DEBUG_LOG("FSM condition", "sync_fsm.c, state=WaitForAcceptGrouped, event=HandshakeAccepted, condition=sameIdentities", resstr)
 21.1503 -                        if (cond_result < 0)
 21.1504 -                            return cond_result;
 21.1505 -                        if (cond_result) {
 21.1506 -                        DEBUG_LOG("FSM action", "sync_fsm.c, state=WaitForAcceptGrouped, event=HandshakeAccepted", "action=acceptHandshake")
 21.1507 -                        status = acceptHandshake(session, state, partner, NULL);
 21.1508 -                        if (status == PEP_OUT_OF_MEMORY)
 21.1509 -                            return (int) invalid_out_of_memory;
 21.1510 -                        if (status != PEP_STATUS_OK)
 21.1511 -                            return (int) invalid_action;
 21.1512 -                        DEBUG_LOG("FSM action", "sync_fsm.c, state=WaitForAcceptGrouped, event=HandshakeAccepted", "action=storeGroupKeys")
 21.1513 -                        status = storeGroupKeys(session, state, partner, groupkeys);
 21.1514 -                        if (status == PEP_OUT_OF_MEMORY)
 21.1515 -                            return (int) invalid_out_of_memory;
 21.1516 -                        if (status != PEP_STATUS_OK)
 21.1517 -                            return (int) invalid_action;
 21.1518 -                        DEBUG_LOG("FSM action", "sync_fsm.c, state=WaitForAcceptGrouped, event=HandshakeAccepted", "action=sendGroupUpdate")
 21.1519 -                        status = sendGroupUpdate(session, state, NULL, NULL);
 21.1520 -                        if (status == PEP_OUT_OF_MEMORY)
 21.1521 -                            return (int) invalid_out_of_memory;
 21.1522 -                        if (status != PEP_STATUS_OK)
 21.1523 -                            return (int) invalid_action;
 21.1524 -                        DEBUG_LOG("FSM action", "sync_fsm.c, state=WaitForAcceptGrouped, event=HandshakeAccepted", "action=renewUUID")
 21.1525 -                        status = renewUUID(session, state, NULL, NULL);
 21.1526 -                        if (status == PEP_OUT_OF_MEMORY)
 21.1527 -                            return (int) invalid_out_of_memory;
 21.1528 -                        if (status != PEP_STATUS_OK)
 21.1529 -                            return (int) invalid_action;
 21.1530 -                        DEBUG_LOG("FSM action", "sync_fsm.c, state=WaitForAcceptGrouped, event=HandshakeAccepted", "action=notifyAcceptedDeviceMoved")
 21.1531 -                        status = notifyAcceptedDeviceMoved(session, state, partner, NULL);
 21.1532 -                        if (status == PEP_OUT_OF_MEMORY)
 21.1533 -                            return (int) invalid_out_of_memory;
 21.1534 -                        if (status != PEP_STATUS_OK)
 21.1535 -                            return (int) invalid_action;
 21.1536 -                        }
 21.1537 -                    }
 21.1538 -                    assert(session->sync_state_payload);
 21.1539 -                    if(!session->sync_state_payload) return (DeviceState_state) invalid_state;
 21.1540 -                    free_identity(((WaitForAcceptGrouped_state_payload_t*)session->sync_state_payload)->expected);
 21.1541 -                    free_group_keys_extra(((WaitForAcceptGrouped_state_payload_t*)session->sync_state_payload)->groupkeys);
 21.1542 -                    free(session->sync_state_payload);
 21.1543 -                    session->sync_state_payload = NULL;
 21.1544 -                    DEBUG_LOG("FSM transition", "sync_fsm.c, state=WaitForAcceptGrouped, event=HandshakeAccepted", "target=Grouped")
 21.1545 -                    return Grouped;
 21.1546 -                }
 21.1547 -                case Cancel:
 21.1548 -                {
 21.1549 -                    DEBUG_LOG("FSM event", "sync_fsm.c, state=WaitForAcceptGrouped", "event=Cancel")
 21.1550 -                    assert(session->sync_state_payload);
 21.1551 -                    if(!session->sync_state_payload) return (DeviceState_state) invalid_state;
 21.1552 -                    free_identity(((WaitForAcceptGrouped_state_payload_t*)session->sync_state_payload)->expected);
 21.1553 -                    free_group_keys_extra(((WaitForAcceptGrouped_state_payload_t*)session->sync_state_payload)->groupkeys);
 21.1554 -                    free(session->sync_state_payload);
 21.1555 -                    session->sync_state_payload = NULL;
 21.1556 -                    DEBUG_LOG("FSM transition", "sync_fsm.c, state=WaitForAcceptGrouped, event=Cancel", "target=Grouped")
 21.1557 -                    return Grouped;
 21.1558 -                }
 21.1559 -                case GroupUpdate:
 21.1560 -                {
 21.1561 -                    DEBUG_LOG("FSM event", "sync_fsm.c, state=WaitForAcceptGrouped", "event=GroupUpdate")
 21.1562 -                    identity_list* keys = (identity_list*)extra;
 21.1563 -                    DEBUG_LOG("FSM action", "sync_fsm.c, state=WaitForAcceptGrouped, event=GroupUpdate", "action=notifyOvertaken")
 21.1564 -                    status = notifyOvertaken(session, state, partner, NULL);
 21.1565 -                    if (status == PEP_OUT_OF_MEMORY)
 21.1566 -                        return (int) invalid_out_of_memory;
 21.1567 -                    if (status != PEP_STATUS_OK)
 21.1568 -                        return (int) invalid_action;
 21.1569 -                    DEBUG_LOG("FSM action", "sync_fsm.c, state=WaitForAcceptGrouped, event=GroupUpdate", "action=storeGroupUpdate")
 21.1570 -                    status = storeGroupUpdate(session, state, partner, keys);
 21.1571 -                    if (status == PEP_OUT_OF_MEMORY)
 21.1572 -                        return (int) invalid_out_of_memory;
 21.1573 -                    if (status != PEP_STATUS_OK)
 21.1574 -                        return (int) invalid_action;
 21.1575 -                    assert(session->sync_state_payload);
 21.1576 -                    if(!session->sync_state_payload) return (DeviceState_state) invalid_state;
 21.1577 -                    free_identity(((WaitForAcceptGrouped_state_payload_t*)session->sync_state_payload)->expected);
 21.1578 -                    free_group_keys_extra(((WaitForAcceptGrouped_state_payload_t*)session->sync_state_payload)->groupkeys);
 21.1579 -                    free(session->sync_state_payload);
 21.1580 -                    session->sync_state_payload = NULL;
 21.1581 -                    DEBUG_LOG("FSM transition", "sync_fsm.c, state=WaitForAcceptGrouped, event=GroupUpdate", "target=Grouped")
 21.1582 -                    return Grouped;
 21.1583 -                }
 21.1584 -                case Timeout:
 21.1585 -                {
 21.1586 -                    DEBUG_LOG("FSM event", "sync_fsm.c, state=WaitForAcceptGrouped", "event=Timeout")
 21.1587 -                    DEBUG_LOG("FSM action", "sync_fsm.c, state=WaitForAcceptGrouped, event=Timeout", "action=notifyTimeout")
 21.1588 -                    status = notifyTimeout(session, state, expected, NULL);
 21.1589 -                    if (status == PEP_OUT_OF_MEMORY)
 21.1590 -                        return (int) invalid_out_of_memory;
 21.1591 -                    if (status != PEP_STATUS_OK)
 21.1592 -                        return (int) invalid_action;
 21.1593 -                    assert(session->sync_state_payload);
 21.1594 -                    if(!session->sync_state_payload) return (DeviceState_state) invalid_state;
 21.1595 -                    free_identity(((WaitForAcceptGrouped_state_payload_t*)session->sync_state_payload)->expected);
 21.1596 -                    free_group_keys_extra(((WaitForAcceptGrouped_state_payload_t*)session->sync_state_payload)->groupkeys);
 21.1597 -                    free(session->sync_state_payload);
 21.1598 -                    session->sync_state_payload = NULL;
 21.1599 -                    DEBUG_LOG("FSM transition", "sync_fsm.c, state=WaitForAcceptGrouped, event=Timeout", "target=Grouped")
 21.1600 -                    return Grouped;
 21.1601 -                }
 21.1602 -                default:
 21.1603 -                    return (DeviceState_state) invalid_event;
 21.1604 -            }
 21.1605 -            break;
 21.1606 -        }
 21.1607 -        default:
 21.1608 -            return (DeviceState_state) invalid_state;
 21.1609 -    }
 21.1610 -
 21.1611 -    return state;
 21.1612 -}
 21.1613 -
    22.1 --- a/sync/generated/sync_fsm.h	Thu Nov 02 17:02:05 2017 +0100
    22.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
    22.3 @@ -1,145 +0,0 @@
    22.4 -#pragma once
    22.5 -
    22.6 -// state machine for DeviceState
    22.7 -
    22.8 -#include "message_api.h"
    22.9 -
   22.10 -#ifdef __cplusplus
   22.11 -extern "C" {
   22.12 -#endif
   22.13 -
   22.14 -// types
   22.15 -
   22.16 -typedef pEp_identity * Identity;
   22.17 -typedef stringlist_t * Stringlist;
   22.18 -
   22.19 -// error values
   22.20 -
   22.21 -typedef enum _fsm_error {
   22.22 -    // these error values are corresponding to
   22.23 -    // PEP_SYNC_STATEMACHINE_ERROR - value
   22.24 -    invalid_state = -2,
   22.25 -    invalid_event = -3,
   22.26 -    invalid_condition = -4,
   22.27 -    invalid_action = -5,
   22.28 -
   22.29 -    // out of memory condition
   22.30 -    invalid_out_of_memory = -128
   22.31 -} fsm_error;
   22.32 -
   22.33 -// conditions
   22.34 -
   22.35 -int deviceGrouped(PEP_SESSION session);
   22.36 -int keyElectionWon(PEP_SESSION session, Identity partner);
   22.37 -int sameIdentities(PEP_SESSION session, Identity a, Identity b);
   22.38 -int sameKeyAndAddress(PEP_SESSION session, Identity a, Identity b);
   22.39 -
   22.40 -// states
   22.41 -
   22.42 -typedef enum _DeviceState_state {
   22.43 -    // error values also in this namespace
   22.44 -    DeviceState_state_invalid_state = (int) invalid_state,
   22.45 -    DeviceState_state_invalid_event = (int) invalid_event,
   22.46 -    DeviceState_state_invalid_condition = (int) invalid_condition,
   22.47 -    DeviceState_state_invalid_action = (int) invalid_action,
   22.48 -    DeviceState_state_invalid_out_of_memory = (int) invalid_out_of_memory,
   22.49 -
   22.50 -    DeviceState_state_NONE = 0,
   22.51 -    InitState, 
   22.52 -    Sole, 
   22.53 -    SoleWaiting, 
   22.54 -    SoleBeaconed, 
   22.55 -    HandshakingSole, 
   22.56 -    WaitForGroupKeysSole, 
   22.57 -    WaitForAcceptSole, 
   22.58 -    Grouped, 
   22.59 -    GroupWaiting, 
   22.60 -    GroupedBeaconed, 
   22.61 -    HandshakingGrouped, 
   22.62 -    WaitForGroupKeysGrouped, 
   22.63 -    WaitForAcceptGrouped
   22.64 -} DeviceState_state;
   22.65 -
   22.66 -// events
   22.67 -
   22.68 -typedef enum _DeviceState_event {
   22.69 -    DeviceState_event_NONE = 0,
   22.70 -    Init = 1,
   22.71 -    Beacon = 2,
   22.72 -    HandshakeRequest = 3,
   22.73 -    GroupKeys = 4,
   22.74 -    KeyGen, 
   22.75 -    CannotDecrypt, 
   22.76 -    Timeout, 
   22.77 -    HandshakeRejected, 
   22.78 -    HandshakeAccepted, 
   22.79 -    Cancel, 
   22.80 -    UpdateRequest, 
   22.81 -    GroupUpdate
   22.82 -} DeviceState_event;
   22.83 -
   22.84 -// actions
   22.85 -
   22.86 -PEP_STATUS sendBeacon(PEP_SESSION session, DeviceState_state state, Identity partner, void *extra);
   22.87 -PEP_STATUS sendHandshakeRequest(PEP_SESSION session, DeviceState_state state, Identity partner, void *extra);
   22.88 -PEP_STATUS notifyInitFormGroup(PEP_SESSION session, DeviceState_state state, Identity partner, void *extra);
   22.89 -PEP_STATUS notifyInitAddOurDevice(PEP_SESSION session, DeviceState_state state, Identity partner, void *extra);
   22.90 -PEP_STATUS rejectHandshake(PEP_SESSION session, DeviceState_state state, Identity partner, void *extra);
   22.91 -PEP_STATUS acceptHandshake(PEP_SESSION session, DeviceState_state state, Identity partner, void *extra);
   22.92 -PEP_STATUS makeGroup(PEP_SESSION session, DeviceState_state state, Identity partner, void *extra);
   22.93 -PEP_STATUS sendGroupKeys(PEP_SESSION session, DeviceState_state state, Identity partner, void *extra);
   22.94 -PEP_STATUS renewUUID(PEP_SESSION session, DeviceState_state state, Identity partner, void *extra);
   22.95 -PEP_STATUS notifyAcceptedGroupCreated(PEP_SESSION session, DeviceState_state state, Identity partner, void *extra);
   22.96 -PEP_STATUS notifyTimeout(PEP_SESSION session, DeviceState_state state, Identity partner, void *extra);
   22.97 -PEP_STATUS storeGroupKeys(PEP_SESSION session, DeviceState_state state, Identity partner, void *extra);
   22.98 -PEP_STATUS sendGroupUpdate(PEP_SESSION session, DeviceState_state state, Identity partner, void *extra);
   22.99 -PEP_STATUS notifyAcceptedDeviceAdded(PEP_SESSION session, DeviceState_state state, Identity partner, void *extra);
  22.100 -PEP_STATUS sendUpdateRequest(PEP_SESSION session, DeviceState_state state, Identity partner, void *extra);
  22.101 -PEP_STATUS storeGroupUpdate(PEP_SESSION session, DeviceState_state state, Identity partner, void *extra);
  22.102 -PEP_STATUS notifyInitAddOtherDevice(PEP_SESSION session, DeviceState_state state, Identity partner, void *extra);
  22.103 -PEP_STATUS notifyInitMoveOurDevice(PEP_SESSION session, DeviceState_state state, Identity partner, void *extra);
  22.104 -PEP_STATUS notifyOvertaken(PEP_SESSION session, DeviceState_state state, Identity partner, void *extra);
  22.105 -PEP_STATUS notifyAcceptedDeviceMoved(PEP_SESSION session, DeviceState_state state, Identity partner, void *extra);
  22.106 -
  22.107 -// event injector
  22.108 -
  22.109 -PEP_STATUS inject_DeviceState_event(
  22.110 -    PEP_SESSION session, 
  22.111 -    DeviceState_event event,
  22.112 -    Identity partner,
  22.113 -    void *extra);
  22.114 -
  22.115 -// message receiver
  22.116 -
  22.117 -PEP_STATUS receive_DeviceState_msg(
  22.118 -        PEP_SESSION session, 
  22.119 -        message *src, 
  22.120 -        PEP_rating rating, 
  22.121 -        stringlist_t *keylist
  22.122 -    );
  22.123 -
  22.124 -// state machine
  22.125 -
  22.126 -DeviceState_state fsm_DeviceState(
  22.127 -        PEP_SESSION session,
  22.128 -        DeviceState_state state,
  22.129 -        DeviceState_event event,
  22.130 -        Identity partner,
  22.131 -        void *extra,
  22.132 -        time_t *timeout
  22.133 -    );
  22.134 -
  22.135 -// driver
  22.136 -
  22.137 -DYNAMIC_API PEP_STATUS fsm_DeviceState_inject(
  22.138 -        PEP_SESSION session,
  22.139 -        DeviceState_event event,
  22.140 -        Identity partner,
  22.141 -        void *extra,
  22.142 -        time_t *timeout
  22.143 -    );
  22.144 -
  22.145 -#ifdef __cplusplus
  22.146 -}
  22.147 -#endif
  22.148 -
    23.1 --- a/sync/generated/sync_send_actions.c	Thu Nov 02 17:02:05 2017 +0100
    23.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
    23.3 @@ -1,521 +0,0 @@
    23.4 -// Send Actions for DeviceState state machine
    23.5 -
    23.6 -#include <assert.h>
    23.7 -#include "pEp_internal.h"
    23.8 -#include "keymanagement.h"
    23.9 -#include "message.h"
   23.10 -#include "sync_fsm.h"
   23.11 -#include "baseprotocol.h"
   23.12 -#include "map_asn1.h"
   23.13 -#include "../asn.1/DeviceGroup-Protocol.h"
   23.14 -#include "sync_impl.h"
   23.15 -#include "../asn.1/Beacon.h"
   23.16 -#include "../asn.1/HandshakeRequest.h"
   23.17 -#include "../asn.1/GroupKeys.h"
   23.18 -#include "../asn.1/GroupUpdate.h"
   23.19 -#include "../asn.1/UpdateRequest.h"
   23.20 -
   23.21 -
   23.22 -// sendBeacon() - send Beacon message
   23.23 -//
   23.24 -//  params:
   23.25 -//      session (in)        session handle
   23.26 -//      state (in)          state the state machine is in
   23.27 -//      partner (in)        (must be NULL)
   23.28 -//
   23.29 -//  returns:
   23.30 -//      PEP_STATUS_OK or any other value on error
   23.31 -
   23.32 -PEP_STATUS sendBeacon(
   23.33 -        PEP_SESSION session,
   23.34 -        DeviceState_state state,
   23.35 -        Identity partner,
   23.36 -        void *extra
   23.37 -    )
   23.38 -{
   23.39 -    assert(session && state);
   23.40 -    if (!(session && state))
   23.41 -        return PEP_ILLEGAL_VALUE;
   23.42 -
   23.43 -    PEP_STATUS status = PEP_STATUS_OK;
   23.44 -
   23.45 -    DeviceGroup_Protocol_t *msg = new_DeviceGroup_Protocol_msg(DeviceGroup_Protocol__payload_PR_beacon);
   23.46 -    if (!msg)
   23.47 -        goto enomem;
   23.48 -
   23.49 -    bool encrypted = false;
   23.50 -    status = multicast_self_msg(session, state, msg, encrypted);
   23.51 -    if (status != PEP_STATUS_OK)
   23.52 -        goto error;
   23.53 -
   23.54 -    free_DeviceGroup_Protocol_msg(msg);
   23.55 -    return PEP_STATUS_OK;
   23.56 -
   23.57 -enomem:
   23.58 -    status = PEP_OUT_OF_MEMORY;
   23.59 -error:
   23.60 -    free_DeviceGroup_Protocol_msg(msg);
   23.61 -    return status;
   23.62 -}
   23.63 -
   23.64 -
   23.65 -// sendHandshakeRequest() - send HandshakeRequest message
   23.66 -//
   23.67 -//  params:
   23.68 -//      session (in)        session handle
   23.69 -//      state (in)          state the state machine is in
   23.70 -//      partner (in)        partner to communicate with
   23.71 -//
   23.72 -//  returns:
   23.73 -//      PEP_STATUS_OK or any other value on error
   23.74 -
   23.75 -PEP_STATUS sendHandshakeRequest(
   23.76 -        PEP_SESSION session,
   23.77 -        DeviceState_state state,
   23.78 -        Identity partner,
   23.79 -        void *extra
   23.80 -    )
   23.81 -{
   23.82 -    assert(session && state);
   23.83 -    if (!(session && state))
   23.84 -        return PEP_ILLEGAL_VALUE;
   23.85 -
   23.86 -    PEP_STATUS status = PEP_STATUS_OK;
   23.87 -
   23.88 -    DeviceGroup_Protocol_t *msg = new_DeviceGroup_Protocol_msg(DeviceGroup_Protocol__payload_PR_handshakeRequest);
   23.89 -    if (!msg)
   23.90 -        goto enomem;
   23.91 -
   23.92 -    msg->payload.choice.handshakeRequest.partner_id = 
   23.93 -        OCTET_STRING_new_fromBuf(&asn_DEF_UTF8String,
   23.94 -                                 partner->user_id, -1);
   23.95 -    if (partner->user_id && !msg->payload.choice.handshakeRequest.partner_id)
   23.96 -       goto enomem;
   23.97 -
   23.98 -    char *devgrp = NULL;
   23.99 -    status = get_device_group(session, &devgrp);
  23.100 -    if (status == PEP_STATUS_OK && devgrp && devgrp[0])
  23.101 -    msg->payload.choice.handshakeRequest.group_id = 
  23.102 -        OCTET_STRING_new_fromBuf(&asn_DEF_UTF8String,
  23.103 -                                 devgrp, -1);
  23.104 -    free(devgrp);
  23.105 -    if (devgrp && !msg->payload.choice.handshakeRequest.partner_id)
  23.106 -       goto enomem;
  23.107 -
  23.108 -    bool encrypted = true;
  23.109 -    status = unicast_msg(session, partner, state, msg, encrypted);
  23.110 -    if (status != PEP_STATUS_OK)
  23.111 -        goto error;
  23.112 -
  23.113 -    free_DeviceGroup_Protocol_msg(msg);
  23.114 -    return PEP_STATUS_OK;
  23.115 -
  23.116 -enomem:
  23.117 -    status = PEP_OUT_OF_MEMORY;
  23.118 -error:
  23.119 -    free_DeviceGroup_Protocol_msg(msg);
  23.120 -    return status;
  23.121 -}
  23.122 -
  23.123 -
  23.124 -// sendGroupKeys() - send GroupKeys message
  23.125 -//
  23.126 -//  params:
  23.127 -//      session (in)        session handle
  23.128 -//      state (in)          state the state machine is in
  23.129 -//      partner (in)        partner to communicate with
  23.130 -//
  23.131 -//  returns:
  23.132 -//      PEP_STATUS_OK or any other value on error
  23.133 -
  23.134 -PEP_STATUS sendGroupKeys(
  23.135 -        PEP_SESSION session,
  23.136 -        DeviceState_state state,
  23.137 -        Identity partner,
  23.138 -        void *extra
  23.139 -    )
  23.140 -{
  23.141 -    assert(session && state);
  23.142 -    if (!(session && state))
  23.143 -        return PEP_ILLEGAL_VALUE;
  23.144 -
  23.145 -    PEP_STATUS status = PEP_STATUS_OK;
  23.146 -    identity_list *kl = new_identity_list(NULL);
  23.147 -
  23.148 -    DeviceGroup_Protocol_t *msg = new_DeviceGroup_Protocol_msg(DeviceGroup_Protocol__payload_PR_groupKeys);
  23.149 -    if (!msg)
  23.150 -        goto enomem;
  23.151 -
  23.152 -    status = _own_identities_retrieve(session, &kl, PEP_idf_not_for_sync);
  23.153 -    if (status != PEP_STATUS_OK)
  23.154 -        goto error;
  23.155 -    if (IdentityList_from_identity_list(kl, &msg->payload.choice.groupKeys.ownIdentities) == NULL)
  23.156 -        goto enomem;
  23.157 -
  23.158 -    msg->payload.choice.groupKeys.partner_id = 
  23.159 -        OCTET_STRING_new_fromBuf(&asn_DEF_UTF8String,
  23.160 -                                 partner->user_id, -1);
  23.161 -    if (partner->user_id && !msg->payload.choice.groupKeys.partner_id)
  23.162 -       goto enomem;
  23.163 -
  23.164 -    char *devgrp = NULL;
  23.165 -    status = get_device_group(session, &devgrp);
  23.166 -    if (status == PEP_STATUS_OK && devgrp && devgrp[0])
  23.167 -    msg->payload.choice.groupKeys.group_id = 
  23.168 -        OCTET_STRING_new_fromBuf(&asn_DEF_UTF8String,
  23.169 -                                 devgrp, -1);
  23.170 -    free(devgrp);
  23.171 -    if (devgrp && !msg->payload.choice.groupKeys.partner_id)
  23.172 -       goto enomem;
  23.173 -
  23.174 -    bool encrypted = true;
  23.175 -    status = unicast_msg(session, partner, state, msg, encrypted);
  23.176 -    if (status != PEP_STATUS_OK)
  23.177 -        goto error;
  23.178 -
  23.179 -    free_identity_list(kl);
  23.180 -    free_DeviceGroup_Protocol_msg(msg);
  23.181 -    return PEP_STATUS_OK;
  23.182 -
  23.183 -enomem:
  23.184 -    status = PEP_OUT_OF_MEMORY;
  23.185 -error:
  23.186 -    free_DeviceGroup_Protocol_msg(msg);
  23.187 -    free_identity_list(kl);
  23.188 -    return status;
  23.189 -}
  23.190 -
  23.191 -
  23.192 -// sendGroupUpdate() - send GroupUpdate message
  23.193 -//
  23.194 -//  params:
  23.195 -//      session (in)        session handle
  23.196 -//      state (in)          state the state machine is in
  23.197 -//      partner (in)        (must be NULL)
  23.198 -//
  23.199 -//  returns:
  23.200 -//      PEP_STATUS_OK or any other value on error
  23.201 -
  23.202 -PEP_STATUS sendGroupUpdate(
  23.203 -        PEP_SESSION session,
  23.204 -        DeviceState_state state,
  23.205 -        Identity partner,
  23.206 -        void *extra
  23.207 -    )
  23.208 -{
  23.209 -    assert(session && state);
  23.210 -    if (!(session && state))
  23.211 -        return PEP_ILLEGAL_VALUE;
  23.212 -
  23.213 -    PEP_STATUS status = PEP_STATUS_OK;
  23.214 -    identity_list *kl = new_identity_list(NULL);
  23.215 -
  23.216 -    DeviceGroup_Protocol_t *msg = new_DeviceGroup_Protocol_msg(DeviceGroup_Protocol__payload_PR_groupUpdate);
  23.217 -    if (!msg)
  23.218 -        goto enomem;
  23.219 -
  23.220 -    status = _own_identities_retrieve(session, &kl, PEP_idf_not_for_sync);
  23.221 -    if (status != PEP_STATUS_OK)
  23.222 -        goto error;
  23.223 -    if (IdentityList_from_identity_list(kl, &msg->payload.choice.groupUpdate.ownIdentities) == NULL)
  23.224 -        goto enomem;
  23.225 -
  23.226 -    bool encrypted = true;
  23.227 -    status = multicast_self_msg(session, state, msg, encrypted);
  23.228 -    if (status != PEP_STATUS_OK)
  23.229 -        goto error;
  23.230 -
  23.231 -    free_identity_list(kl);
  23.232 -    free_DeviceGroup_Protocol_msg(msg);
  23.233 -    return PEP_STATUS_OK;
  23.234 -
  23.235 -enomem:
  23.236 -    status = PEP_OUT_OF_MEMORY;
  23.237 -error:
  23.238 -    free_DeviceGroup_Protocol_msg(msg);
  23.239 -    return status;
  23.240 -}
  23.241 -
  23.242 -
  23.243 -// sendUpdateRequest() - send UpdateRequest message
  23.244 -//
  23.245 -//  params:
  23.246 -//      session (in)        session handle
  23.247 -//      state (in)          state the state machine is in
  23.248 -//      partner (in)        (must be NULL)
  23.249 -//
  23.250 -//  returns:
  23.251 -//      PEP_STATUS_OK or any other value on error
  23.252 -
  23.253 -PEP_STATUS sendUpdateRequest(
  23.254 -        PEP_SESSION session,
  23.255 -        DeviceState_state state,
  23.256 -        Identity partner,
  23.257 -        void *extra
  23.258 -    )
  23.259 -{
  23.260 -    assert(session && state);
  23.261 -    if (!(session && state))
  23.262 -        return PEP_ILLEGAL_VALUE;
  23.263 -
  23.264 -    PEP_STATUS status = PEP_STATUS_OK;
  23.265 -
  23.266 -    DeviceGroup_Protocol_t *msg = new_DeviceGroup_Protocol_msg(DeviceGroup_Protocol__payload_PR_updateRequest);
  23.267 -    if (!msg)
  23.268 -        goto enomem;
  23.269 -
  23.270 -    bool encrypted = true;
  23.271 -    status = multicast_self_msg(session, state, msg, encrypted);
  23.272 -    if (status != PEP_STATUS_OK)
  23.273 -        goto error;
  23.274 -
  23.275 -    free_DeviceGroup_Protocol_msg(msg);
  23.276 -    return PEP_STATUS_OK;
  23.277 -
  23.278 -enomem:
  23.279 -    status = PEP_OUT_OF_MEMORY;
  23.280 -error:
  23.281 -    free_DeviceGroup_Protocol_msg(msg);
  23.282 -    return status;
  23.283 -}
  23.284 -
  23.285 -
  23.286 -PEP_STATUS _notifyHandshake(
  23.287 -        PEP_SESSION session,
  23.288 -        Identity partner,
  23.289 -        sync_handshake_signal signal
  23.290 -    );
  23.291 -
  23.292 -// notifyInitFormGroup() - notify InitFormGroup to app
  23.293 -//
  23.294 -//  params:
  23.295 -//      session (in)        session handle
  23.296 -//      state (in)          state the state machine is in
  23.297 -//      partner (in)        partner to communicate with
  23.298 -//
  23.299 -//  returns:
  23.300 -//      PEP_STATUS_OK or any other value on error
  23.301 -
  23.302 -PEP_STATUS notifyInitFormGroup(
  23.303 -        PEP_SESSION session,
  23.304 -        DeviceState_state state,
  23.305 -        Identity partner,
  23.306 -        void *extra
  23.307 -    )
  23.308 -{
  23.309 -    assert(session && state);
  23.310 -    assert(extra == NULL);
  23.311 -    if (!(session && state && extra == NULL))
  23.312 -        return PEP_ILLEGAL_VALUE;
  23.313 -
  23.314 -    return _notifyHandshake(session, partner, SYNC_NOTIFY_INIT_FORM_GROUP);
  23.315 -}
  23.316 -
  23.317 -
  23.318 -// notifyInitAddOurDevice() - notify InitAddOurDevice to app
  23.319 -//
  23.320 -//  params:
  23.321 -//      session (in)        session handle
  23.322 -//      state (in)          state the state machine is in
  23.323 -//      partner (in)        partner to communicate with
  23.324 -//
  23.325 -//  returns:
  23.326 -//      PEP_STATUS_OK or any other value on error
  23.327 -
  23.328 -PEP_STATUS notifyInitAddOurDevice(
  23.329 -        PEP_SESSION session,
  23.330 -        DeviceState_state state,
  23.331 -        Identity partner,
  23.332 -        void *extra
  23.333 -    )
  23.334 -{
  23.335 -    assert(session && state);
  23.336 -    assert(extra == NULL);
  23.337 -    if (!(session && state && extra == NULL))
  23.338 -        return PEP_ILLEGAL_VALUE;
  23.339 -
  23.340 -    return _notifyHandshake(session, partner, SYNC_NOTIFY_INIT_ADD_OUR_DEVICE);
  23.341 -}
  23.342 -
  23.343 -
  23.344 -// notifyAcceptedGroupCreated() - notify AcceptedGroupCreated to app
  23.345 -//
  23.346 -//  params:
  23.347 -//      session (in)        session handle
  23.348 -//      state (in)          state the state machine is in
  23.349 -//      partner (in)        partner to communicate with
  23.350 -//
  23.351 -//  returns:
  23.352 -//      PEP_STATUS_OK or any other value on error
  23.353 -
  23.354 -PEP_STATUS notifyAcceptedGroupCreated(
  23.355 -        PEP_SESSION session,
  23.356 -        DeviceState_state state,
  23.357 -        Identity partner,
  23.358 -        void *extra
  23.359 -    )
  23.360 -{
  23.361 -    assert(session && state);
  23.362 -    assert(extra == NULL);
  23.363 -    if (!(session && state && extra == NULL))
  23.364 -        return PEP_ILLEGAL_VALUE;
  23.365 -
  23.366 -    return _notifyHandshake(session, partner, SYNC_NOTIFY_ACCEPTED_GROUP_CREATED);
  23.367 -}
  23.368 -
  23.369 -
  23.370 -// notifyTimeout() - notify Timeout to app
  23.371 -//
  23.372 -//  params:
  23.373 -//      session (in)        session handle
  23.374 -//      state (in)          state the state machine is in
  23.375 -//      partner (in)        partner to communicate with
  23.376 -//
  23.377 -//  returns:
  23.378 -//      PEP_STATUS_OK or any other value on error
  23.379 -
  23.380 -PEP_STATUS notifyTimeout(
  23.381 -        PEP_SESSION session,
  23.382 -        DeviceState_state state,
  23.383 -        Identity partner,
  23.384 -        void *extra
  23.385 -    )
  23.386 -{
  23.387 -    assert(session && state);
  23.388 -    assert(extra == NULL);
  23.389 -    if (!(session && state && extra == NULL))
  23.390 -        return PEP_ILLEGAL_VALUE;
  23.391 -
  23.392 -    return _notifyHandshake(session, partner, SYNC_NOTIFY_TIMEOUT);
  23.393 -}
  23.394 -
  23.395 -
  23.396 -// notifyAcceptedDeviceAdded() - notify AcceptedDeviceAdded to app
  23.397 -//
  23.398 -//  params:
  23.399 -//      session (in)        session handle
  23.400 -//      state (in)          state the state machine is in
  23.401 -//      partner (in)        partner to communicate with
  23.402 -//
  23.403 -//  returns:
  23.404 -//      PEP_STATUS_OK or any other value on error
  23.405 -
  23.406 -PEP_STATUS notifyAcceptedDeviceAdded(
  23.407 -        PEP_SESSION session,
  23.408 -        DeviceState_state state,
  23.409 -        Identity partner,
  23.410 -        void *extra
  23.411 -    )
  23.412 -{
  23.413 -    assert(session && state);
  23.414 -    assert(extra == NULL);
  23.415 -    if (!(session && state && extra == NULL))
  23.416 -        return PEP_ILLEGAL_VALUE;
  23.417 -
  23.418 -    return _notifyHandshake(session, partner, SYNC_NOTIFY_ACCEPTED_DEVICE_ADDED);
  23.419 -}
  23.420 -
  23.421 -
  23.422 -// notifyInitAddOtherDevice() - notify InitAddOtherDevice to app
  23.423 -//
  23.424 -//  params:
  23.425 -//      session (in)        session handle
  23.426 -//      state (in)          state the state machine is in
  23.427 -//      partner (in)        partner to communicate with
  23.428 -//
  23.429 -//  returns:
  23.430 -//      PEP_STATUS_OK or any other value on error
  23.431 -
  23.432 -PEP_STATUS notifyInitAddOtherDevice(
  23.433 -        PEP_SESSION session,
  23.434 -        DeviceState_state state,
  23.435 -        Identity partner,
  23.436 -        void *extra
  23.437 -    )
  23.438 -{
  23.439 -    assert(session && state);
  23.440 -    assert(extra == NULL);
  23.441 -    if (!(session && state && extra == NULL))
  23.442 -        return PEP_ILLEGAL_VALUE;
  23.443 -
  23.444 -    return _notifyHandshake(session, partner, SYNC_NOTIFY_INIT_ADD_OTHER_DEVICE);
  23.445 -}
  23.446 -
  23.447 -
  23.448 -// notifyInitMoveOurDevice() - notify InitMoveOurDevice to app
  23.449 -//
  23.450 -//  params:
  23.451 -//      session (in)        session handle
  23.452 -//      state (in)          state the state machine is in
  23.453 -//      partner (in)        partner to communicate with
  23.454 -//
  23.455 -//  returns:
  23.456 -//      PEP_STATUS_OK or any other value on error
  23.457 -
  23.458 -PEP_STATUS notifyInitMoveOurDevice(
  23.459 -        PEP_SESSION session,
  23.460 -        DeviceState_state state,
  23.461 -        Identity partner,
  23.462 -        void *extra
  23.463 -    )
  23.464 -{
  23.465 -    assert(session && state);
  23.466 -    assert(extra == NULL);
  23.467 -    if (!(session && state && extra == NULL))
  23.468 -        return PEP_ILLEGAL_VALUE;
  23.469 -
  23.470 -    return _notifyHandshake(session, partner, SYNC_NOTIFY_INIT_MOVE_OUR_DEVICE);
  23.471 -}
  23.472 -
  23.473 -
  23.474 -// notifyOvertaken() - notify Overtaken to app
  23.475 -//
  23.476 -//  params:
  23.477 -//      session (in)        session handle
  23.478 -//      state (in)          state the state machine is in
  23.479 -//      partner (in)        partner to communicate with
  23.480 -//
  23.481 -//  returns:
  23.482 -//      PEP_STATUS_OK or any other value on error
  23.483 -
  23.484 -PEP_STATUS notifyOvertaken(
  23.485 -        PEP_SESSION session,
  23.486 -        DeviceState_state state,
  23.487 -        Identity partner,
  23.488 -        void *extra
  23.489 -    )
  23.490 -{
  23.491 -    assert(session && state);
  23.492 -    assert(extra == NULL);
  23.493 -    if (!(session && state && extra == NULL))
  23.494 -        return PEP_ILLEGAL_VALUE;
  23.495 -
  23.496 -    return _notifyHandshake(session, partner, SYNC_NOTIFY_OVERTAKEN);
  23.497 -}
  23.498 -
  23.499 -
  23.500 -// notifyAcceptedDeviceMoved() - notify AcceptedDeviceMoved to app
  23.501 -//
  23.502 -//  params:
  23.503 -//      session (in)        session handle
  23.504 -//      state (in)          state the state machine is in
  23.505 -//      partner (in)        partner to communicate with
  23.506 -//
  23.507 -//  returns:
  23.508 -//      PEP_STATUS_OK or any other value on error
  23.509 -
  23.510 -PEP_STATUS notifyAcceptedDeviceMoved(
  23.511 -        PEP_SESSION session,
  23.512 -        DeviceState_state state,
  23.513 -        Identity partner,
  23.514 -        void *extra
  23.515 -    )
  23.516 -{
  23.517 -    assert(session && state);
  23.518 -    assert(extra == NULL);
  23.519 -    if (!(session && state && extra == NULL))
  23.520 -        return PEP_ILLEGAL_VALUE;
  23.521 -
  23.522 -    return _notifyHandshake(session, partner, SYNC_NOTIFY_ACCEPTED_DEVICE_MOVED);
  23.523 -}
  23.524 -
    24.1 --- a/sync/skeletons/sync_actions.c	Thu Nov 02 17:02:05 2017 +0100
    24.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
    24.3 @@ -1,564 +0,0 @@
    24.4 -// Actions for DeviceState state machine
    24.5 -
    24.6 -#include <assert.h>
    24.7 -#include "pEp_internal.h"
    24.8 -#include "keymanagement.h"
    24.9 -#include "message.h"
   24.10 -#include "sync_fsm.h"
   24.11 -#include "../asn.1/DeviceGroup-Protocol.h"
   24.12 -
   24.13 -
   24.14 -// notifyInitFormGroup() - 
   24.15 -//
   24.16 -//  params:
   24.17 -//      session (in)        session handle
   24.18 -//      state (in)          state the state machine is in
   24.19 -//      partner (in)        partner to communicate with
   24.20 -//
   24.21 -//  returns:
   24.22 -//      PEP_STATUS_OK or any other value on error
   24.23 -
   24.24 -PEP_STATUS notifyInitFormGroup(
   24.25 -        PEP_SESSION session,
   24.26 -        DeviceState_state state,
   24.27 -        Identity partner,
   24.28 -        void *extra
   24.29 -    )
   24.30 -{
   24.31 -    PEP_STATUS status = PEP_STATUS_OK;
   24.32 -
   24.33 -    assert(session);
   24.34 -    assert(!partner);
   24.35 -    if (!(session && !partner))
   24.36 -        return PEP_ILLEGAL_VALUE;
   24.37 -
   24.38 -    // working code
   24.39 -
   24.40 -    // free extra
   24.41 -    return status;
   24.42 -
   24.43 -enomem:
   24.44 -    status = PEP_OUT_OF_MEMORY;
   24.45 -error:
   24.46 -    // free extra
   24.47 -    return status;
   24.48 -}
   24.49 -
   24.50 -
   24.51 -// notifyInitAddOurDevice() - 
   24.52 -//
   24.53 -//  params:
   24.54 -//      session (in)        session handle
   24.55 -//      state (in)          state the state machine is in
   24.56 -//      partner (in)        partner to communicate with
   24.57 -//
   24.58 -//  returns:
   24.59 -//      PEP_STATUS_OK or any other value on error
   24.60 -
   24.61 -PEP_STATUS notifyInitAddOurDevice(
   24.62 -        PEP_SESSION session,
   24.63 -        DeviceState_state state,
   24.64 -        Identity partner,
   24.65 -        void *extra
   24.66 -    )
   24.67 -{
   24.68 -    PEP_STATUS status = PEP_STATUS_OK;
   24.69 -
   24.70 -    assert(session);
   24.71 -    assert(!partner);
   24.72 -    if (!(session && !partner))
   24.73 -        return PEP_ILLEGAL_VALUE;
   24.74 -
   24.75 -    // working code
   24.76 -
   24.77 -    // free extra
   24.78 -    return status;
   24.79 -
   24.80 -enomem:
   24.81 -    status = PEP_OUT_OF_MEMORY;
   24.82 -error:
   24.83 -    // free extra
   24.84 -    return status;
   24.85 -}
   24.86 -
   24.87 -
   24.88 -// rejectHandshake() - 
   24.89 -//
   24.90 -//  params:
   24.91 -//      session (in)        session handle
   24.92 -//      state (in)          state the state machine is in
   24.93 -//      partner (in)        partner to communicate with
   24.94 -//
   24.95 -//  returns:
   24.96 -//      PEP_STATUS_OK or any other value on error
   24.97 -
   24.98 -PEP_STATUS rejectHandshake(
   24.99 -        PEP_SESSION session,
  24.100 -        DeviceState_state state,
  24.101 -        Identity partner,
  24.102 -        void *extra
  24.103 -    )
  24.104 -{
  24.105 -    PEP_STATUS status = PEP_STATUS_OK;
  24.106 -
  24.107 -    assert(session);
  24.108 -    assert(partner);
  24.109 -    if (!(session && partner))
  24.110 -        return PEP_ILLEGAL_VALUE;
  24.111 -
  24.112 -    // working code
  24.113 -
  24.114 -    // free extra
  24.115 -    return status;
  24.116 -
  24.117 -enomem:
  24.118 -    status = PEP_OUT_OF_MEMORY;
  24.119 -error:
  24.120 -    // free extra
  24.121 -    return status;
  24.122 -}
  24.123 -
  24.124 -
  24.125 -// acceptHandshake() - 
  24.126 -//
  24.127 -//  params:
  24.128 -//      session (in)        session handle
  24.129 -//      state (in)          state the state machine is in
  24.130 -//      partner (in)        partner to communicate with
  24.131 -//
  24.132 -//  returns:
  24.133 -//      PEP_STATUS_OK or any other value on error
  24.134 -
  24.135 -PEP_STATUS acceptHandshake(
  24.136 -        PEP_SESSION session,
  24.137 -        DeviceState_state state,
  24.138 -        Identity partner,
  24.139 -        void *extra
  24.140 -    )
  24.141 -{
  24.142 -    PEP_STATUS status = PEP_STATUS_OK;
  24.143 -
  24.144 -    assert(session);
  24.145 -    assert(partner);
  24.146 -    if (!(session && partner))
  24.147 -        return PEP_ILLEGAL_VALUE;
  24.148 -
  24.149 -    // working code
  24.150 -
  24.151 -    // free extra
  24.152 -    return status;
  24.153 -
  24.154 -enomem:
  24.155 -    status = PEP_OUT_OF_MEMORY;
  24.156 -error:
  24.157 -    // free extra
  24.158 -    return status;
  24.159 -}
  24.160 -
  24.161 -
  24.162 -// makeGroup() - 
  24.163 -//
  24.164 -//  params:
  24.165 -//      session (in)        session handle
  24.166 -//      state (in)          state the state machine is in
  24.167 -//      partner (in)        (must be NULL)
  24.168 -//
  24.169 -//  returns:
  24.170 -//      PEP_STATUS_OK or any other value on error
  24.171 -
  24.172 -PEP_STATUS makeGroup(
  24.173 -        PEP_SESSION session,
  24.174 -        DeviceState_state state,
  24.175 -        Identity partner,
  24.176 -        void *extra
  24.177 -    )
  24.178 -{
  24.179 -    PEP_STATUS status = PEP_STATUS_OK;
  24.180 -
  24.181 -    assert(session);
  24.182 -    assert(!partner);
  24.183 -    if (!(session && !partner))
  24.184 -        return PEP_ILLEGAL_VALUE;
  24.185 -
  24.186 -    // working code
  24.187 -
  24.188 -    // free extra
  24.189 -    return status;
  24.190 -
  24.191 -enomem:
  24.192 -    status = PEP_OUT_OF_MEMORY;
  24.193 -error:
  24.194 -    // free extra
  24.195 -    return status;
  24.196 -}
  24.197 -
  24.198 -
  24.199 -// renewUUID() - 
  24.200 -//
  24.201 -//  params:
  24.202 -//      session (in)        session handle
  24.203 -//      state (in)          state the state machine is in
  24.204 -//      partner (in)        (must be NULL)
  24.205 -//
  24.206 -//  returns:
  24.207 -//      PEP_STATUS_OK or any other value on error
  24.208 -
  24.209 -PEP_STATUS renewUUID(
  24.210 -        PEP_SESSION session,
  24.211 -        DeviceState_state state,
  24.212 -        Identity partner,
  24.213 -        void *extra
  24.214 -    )
  24.215 -{
  24.216 -    PEP_STATUS status = PEP_STATUS_OK;
  24.217 -
  24.218 -    assert(session);
  24.219 -    assert(!partner);
  24.220 -    if (!(session && !partner))
  24.221 -        return PEP_ILLEGAL_VALUE;
  24.222 -
  24.223 -    // working code
  24.224 -
  24.225 -    // free extra
  24.226 -    return status;
  24.227 -
  24.228 -enomem:
  24.229 -    status = PEP_OUT_OF_MEMORY;
  24.230 -error:
  24.231 -    // free extra
  24.232 -    return status;
  24.233 -}
  24.234 -
  24.235 -
  24.236 -// notifyAcceptedGroupCreated() - 
  24.237 -//
  24.238 -//  params:
  24.239 -//      session (in)        session handle
  24.240 -//      state (in)          state the state machine is in
  24.241 -//      partner (in)        partner to communicate with
  24.242 -//
  24.243 -//  returns:
  24.244 -//      PEP_STATUS_OK or any other value on error
  24.245 -
  24.246 -PEP_STATUS notifyAcceptedGroupCreated(
  24.247 -        PEP_SESSION session,
  24.248 -        DeviceState_state state,
  24.249 -        Identity partner,
  24.250 -        void *extra
  24.251 -    )
  24.252 -{
  24.253 -    PEP_STATUS status = PEP_STATUS_OK;
  24.254 -
  24.255 -    assert(session);
  24.256 -    assert(partner);
  24.257 -    if (!(session && partner))
  24.258 -        return PEP_ILLEGAL_VALUE;
  24.259 -
  24.260 -    // working code
  24.261 -
  24.262 -    // free extra
  24.263 -    return status;
  24.264 -
  24.265 -enomem:
  24.266 -    status = PEP_OUT_OF_MEMORY;
  24.267 -error:
  24.268 -    // free extra
  24.269 -    return status;
  24.270 -}
  24.271 -
  24.272 -
  24.273 -// notifyTimeout() - 
  24.274 -//
  24.275 -//  params:
  24.276 -//      session (in)        session handle
  24.277 -//      state (in)          state the state machine is in
  24.278 -//      partner (in)        partner to communicate with
  24.279 -//
  24.280 -//  returns:
  24.281 -//      PEP_STATUS_OK or any other value on error
  24.282 -
  24.283 -PEP_STATUS notifyTimeout(
  24.284 -        PEP_SESSION session,
  24.285 -        DeviceState_state state,
  24.286 -        Identity partner,
  24.287 -        void *extra
  24.288 -    )
  24.289 -{
  24.290 -    PEP_STATUS status = PEP_STATUS_OK;
  24.291 -
  24.292 -    assert(session);
  24.293 -    assert(!partner);
  24.294 -    if (!(session && !partner))
  24.295 -        return PEP_ILLEGAL_VALUE;
  24.296 -
  24.297 -    // working code
  24.298 -
  24.299 -    // free extra
  24.300 -    return status;
  24.301 -
  24.302 -enomem:
  24.303 -    status = PEP_OUT_OF_MEMORY;
  24.304 -error:
  24.305 -    // free extra
  24.306 -    return status;
  24.307 -}
  24.308 -
  24.309 -
  24.310 -// storeGroupKeys() - 
  24.311 -//
  24.312 -//  params:
  24.313 -//      session (in)        session handle
  24.314 -//      state (in)          state the state machine is in
  24.315 -//      partner (in)        partner to communicate with
  24.316 -//
  24.317 -//  returns:
  24.318 -//      PEP_STATUS_OK or any other value on error
  24.319 -
  24.320 -PEP_STATUS storeGroupKeys(
  24.321 -        PEP_SESSION session,
  24.322 -        DeviceState_state state,
  24.323 -        Identity partner,
  24.324 -        void *extra
  24.325 -    )
  24.326 -{
  24.327 -    PEP_STATUS status = PEP_STATUS_OK;
  24.328 -
  24.329 -    assert(session);
  24.330 -    assert(partner);
  24.331 -    if (!(session && partner))
  24.332 -        return PEP_ILLEGAL_VALUE;
  24.333 -
  24.334 -    // working code
  24.335 -
  24.336 -    // free extra
  24.337 -    return status;
  24.338 -
  24.339 -enomem:
  24.340 -    status = PEP_OUT_OF_MEMORY;
  24.341 -error:
  24.342 -    // free extra
  24.343 -    return status;
  24.344 -}
  24.345 -
  24.346 -
  24.347 -// notifyAcceptedDeviceAdded() - 
  24.348 -//
  24.349 -//  params:
  24.350 -//      session (in)        session handle
  24.351 -//      state (in)          state the state machine is in
  24.352 -//      partner (in)        partner to communicate with
  24.353 -//
  24.354 -//  returns:
  24.355 -//      PEP_STATUS_OK or any other value on error
  24.356 -
  24.357 -PEP_STATUS notifyAcceptedDeviceAdded(
  24.358 -        PEP_SESSION session,
  24.359 -        DeviceState_state state,
  24.360 -        Identity partner,
  24.361 -        void *extra
  24.362 -    )
  24.363 -{
  24.364 -    PEP_STATUS status = PEP_STATUS_OK;
  24.365 -
  24.366 -    assert(session);
  24.367 -    assert(partner);
  24.368 -    if (!(session && partner))
  24.369 -        return PEP_ILLEGAL_VALUE;
  24.370 -
  24.371 -    // working code
  24.372 -
  24.373 -    // free extra
  24.374 -    return status;
  24.375 -
  24.376 -enomem:
  24.377 -    status = PEP_OUT_OF_MEMORY;
  24.378 -error:
  24.379 -    // free extra
  24.380 -    return status;
  24.381 -}
  24.382 -
  24.383 -
  24.384 -// storeGroupUpdate() - 
  24.385 -//
  24.386 -//  params:
  24.387 -//      session (in)        session handle
  24.388 -//      state (in)          state the state machine is in
  24.389 -//      partner (in)        partner to communicate with
  24.390 -//
  24.391 -//  returns:
  24.392 -//      PEP_STATUS_OK or any other value on error
  24.393 -
  24.394 -PEP_STATUS storeGroupUpdate(
  24.395 -        PEP_SESSION session,
  24.396 -        DeviceState_state state,
  24.397 -        Identity partner,
  24.398 -        void *extra
  24.399 -    )
  24.400 -{
  24.401 -    PEP_STATUS status = PEP_STATUS_OK;
  24.402 -
  24.403 -    assert(session);
  24.404 -    assert(partner);
  24.405 -    if (!(session && partner))
  24.406 -        return PEP_ILLEGAL_VALUE;
  24.407 -
  24.408 -    // working code
  24.409 -
  24.410 -    // free extra
  24.411 -    return status;
  24.412 -
  24.413 -enomem:
  24.414 -    status = PEP_OUT_OF_MEMORY;
  24.415 -error:
  24.416 -    // free extra
  24.417 -    return status;
  24.418 -}
  24.419 -
  24.420 -
  24.421 -// notifyInitAddOtherDevice() - 
  24.422 -//
  24.423 -//  params:
  24.424 -//      session (in)        session handle
  24.425 -//      state (in)          state the state machine is in
  24.426 -//      partner (in)        partner to communicate with
  24.427 -//
  24.428 -//  returns:
  24.429 -//      PEP_STATUS_OK or any other value on error
  24.430 -
  24.431 -PEP_STATUS notifyInitAddOtherDevice(
  24.432 -        PEP_SESSION session,
  24.433 -        DeviceState_state state,
  24.434 -        Identity partner,
  24.435 -        void *extra
  24.436 -    )
  24.437 -{
  24.438 -    PEP_STATUS status = PEP_STATUS_OK;
  24.439 -
  24.440 -    assert(session);
  24.441 -    assert(partner);
  24.442 -    if (!(session && partner))
  24.443 -        return PEP_ILLEGAL_VALUE;
  24.444 -
  24.445 -    // working code
  24.446 -
  24.447 -    // free extra
  24.448 -    return status;
  24.449 -
  24.450 -enomem:
  24.451 -    status = PEP_OUT_OF_MEMORY;
  24.452 -error:
  24.453 -    // free extra
  24.454 -    return status;
  24.455 -}
  24.456 -
  24.457 -
  24.458 -// notifyInitMoveOurDevice() - 
  24.459 -//
  24.460 -//  params:
  24.461 -//      session (in)        session handle
  24.462 -//      state (in)          state the state machine is in
  24.463 -//      partner (in)        partner to communicate with
  24.464 -//
  24.465 -//  returns:
  24.466 -//      PEP_STATUS_OK or any other value on error
  24.467 -
  24.468 -PEP_STATUS notifyInitMoveOurDevice(
  24.469 -        PEP_SESSION session,
  24.470 -        DeviceState_state state,
  24.471 -        Identity partner,
  24.472 -        void *extra
  24.473 -    )
  24.474 -{
  24.475 -    PEP_STATUS status = PEP_STATUS_OK;
  24.476 -
  24.477 -    assert(session);
  24.478 -    assert(partner);
  24.479 -    if (!(session && partner))
  24.480 -        return PEP_ILLEGAL_VALUE;
  24.481 -
  24.482 -    // working code
  24.483 -
  24.484 -    // free extra
  24.485 -    return status;
  24.486 -
  24.487 -enomem:
  24.488 -    status = PEP_OUT_OF_MEMORY;
  24.489 -error:
  24.490 -    // free extra
  24.491 -    return status;
  24.492 -}
  24.493 -
  24.494 -
  24.495 -// notifyOvertaken() - 
  24.496 -//
  24.497 -//  params:
  24.498 -//      session (in)        session handle
  24.499 -//      state (in)          state the state machine is in
  24.500 -//      partner (in)        partner to communicate with
  24.501 -//
  24.502 -//  returns:
  24.503 -//      PEP_STATUS_OK or any other value on error
  24.504 -
  24.505 -PEP_STATUS notifyOvertaken(
  24.506 -        PEP_SESSION session,
  24.507 -        DeviceState_state state,
  24.508 -        Identity partner,
  24.509 -        void *extra
  24.510 -    )
  24.511 -{
  24.512 -    PEP_STATUS status = PEP_STATUS_OK;
  24.513 -
  24.514 -    assert(session);
  24.515 -    assert(partner);
  24.516 -    if (!(session && partner))
  24.517 -        return PEP_ILLEGAL_VALUE;
  24.518 -
  24.519 -    // working code
  24.520 -
  24.521 -    // free extra
  24.522 -    return status;
  24.523 -
  24.524 -enomem:
  24.525 -    status = PEP_OUT_OF_MEMORY;
  24.526 -error:
  24.527 -    // free extra
  24.528 -    return status;
  24.529 -}
  24.530 -
  24.531 -
  24.532 -// notifyAcceptedDeviceMoved() - 
  24.533 -//
  24.534 -//  params:
  24.535 -//      session (in)        session handle
  24.536 -//      state (in)          state the state machine is in
  24.537 -//      partner (in)        partner to communicate with
  24.538 -//
  24.539 -//  returns:
  24.540 -//      PEP_STATUS_OK or any other value on error
  24.541 -
  24.542 -PEP_STATUS notifyAcceptedDeviceMoved(
  24.543 -        PEP_SESSION session,
  24.544 -        DeviceState_state state,
  24.545 -        Identity partner,
  24.546 -        void *extra
  24.547 -    )
  24.548 -{
  24.549 -    PEP_STATUS status = PEP_STATUS_OK;
  24.550 -
  24.551 -    assert(session);
  24.552 -    assert(partner);
  24.553 -    if (!(session && partner))
  24.554 -        return PEP_ILLEGAL_VALUE;
  24.555 -
  24.556 -    // working code
  24.557 -
  24.558 -    // free extra
  24.559 -    return status;
  24.560 -
  24.561 -enomem:
  24.562 -    status = PEP_OUT_OF_MEMORY;
  24.563 -error:
  24.564 -    // free extra
  24.565 -    return status;
  24.566 -}
  24.567 -