empty address allowed for myself() keysync
authorVolker Birk <vb@pep.foundation>
Sat, 20 Aug 2016 14:45:19 +0200
branchkeysync
changeset 1043f786ee22e281
parent 1026 ed301012d546
child 1044 10a56596be7d
empty address allowed for myself()
src/keymanagement.c
src/pEp_internal.h
src/sync.c
src/sync.h
src/sync_actions.c
src/sync_driver.c
src/sync_fsm.c
src/sync_fsm.h
sync/gen_statemachine.ysl2
     1.1 --- a/src/keymanagement.c	Tue Aug 16 19:20:20 2016 +0200
     1.2 +++ b/src/keymanagement.c	Sat Aug 20 14:45:19 2016 +0200
     1.3 @@ -356,31 +356,37 @@
     1.4  
     1.5      assert(session);
     1.6      assert(identity);
     1.7 -    assert(identity->address);
     1.8 -    assert(identity->username);
     1.9 +
    1.10      assert(EMPTYSTR(identity->user_id) ||
    1.11             strcmp(identity->user_id, PEP_OWN_USERID) == 0);
    1.12  
    1.13 -    if (!(session && identity && identity->address && identity->username &&
    1.14 -          (EMPTYSTR(identity->user_id) ||
    1.15 -           strcmp(identity->user_id, PEP_OWN_USERID) == 0)))
    1.16 +    if (!(session && identity &&
    1.17 +            (EMPTYSTR(identity->user_id) ||
    1.18 +            strcmp(identity->user_id, PEP_OWN_USERID) == 0)))
    1.19          return PEP_ILLEGAL_VALUE;
    1.20  
    1.21      identity->comm_type = PEP_ct_pEp;
    1.22      identity->me = true;
    1.23      
    1.24 -    if(EMPTYSTR(identity->user_id))
    1.25 +    if (EMPTYSTR(identity->user_id))
    1.26      {
    1.27          free(identity->user_id);
    1.28          identity->user_id = strdup(PEP_OWN_USERID);
    1.29          assert(identity->user_id);
    1.30          if (identity->user_id == NULL)
    1.31 -        {
    1.32              return PEP_OUT_OF_MEMORY;
    1.33 -        }
    1.34      }
    1.35  
    1.36 -    DEBUG_LOG("myself", "debug", identity->address);
    1.37 +    if (!identity->address)
    1.38 +    {
    1.39 +        identity->address = strdup("");
    1.40 +        assert(identity->address);
    1.41 +        if (!identity->address)
    1.42 +            return PEP_OUT_OF_MEMORY;
    1.43 +    }
    1.44 +
    1.45 +    DEBUG_LOG("myself", "debug", EMPTYSTR(identity->address) ?
    1.46 +            "<default address>" : identity->address);
    1.47      
    1.48      status = get_identity(session,
    1.49                            identity->address,
     2.1 --- a/src/pEp_internal.h	Tue Aug 16 19:20:20 2016 +0200
     2.2 +++ b/src/pEp_internal.h	Sat Aug 20 14:45:19 2016 +0200
     2.3 @@ -131,6 +131,8 @@
     2.4      void *sync_obj;
     2.5      messageToSend_t messageToSend;
     2.6      showHandshake_t showHandshake;
     2.7 +    inject_sync_msg_t inject_sync_msg;
     2.8 +    retrieve_next_sync_msg_t retrieve_next_sync_msg;
     2.9  
    2.10      // state machines
    2.11      DeviceState_state sync_state;
     3.1 --- a/src/sync.c	Tue Aug 16 19:20:20 2016 +0200
     3.2 +++ b/src/sync.c	Sat Aug 20 14:45:19 2016 +0200
     3.3 @@ -5,17 +5,27 @@
     3.4  
     3.5  #include "../asn.1/DeviceGroup-Protocol.h"
     3.6  
     3.7 +// receive_sync_msg is defined in the sync_actions
     3.8 +
     3.9 +PEP_STATUS receive_sync_msg(
    3.10 +        PEP_SESSION session,
    3.11 +        DeviceGroup_Protocol_t *msg
    3.12 +    );
    3.13  
    3.14  DYNAMIC_API PEP_STATUS register_sync_callbacks(
    3.15          PEP_SESSION session,
    3.16          void *obj,
    3.17          messageToSend_t messageToSend,
    3.18 -        showHandshake_t showHandshake
    3.19 +        showHandshake_t showHandshake,
    3.20 +        inject_sync_msg_t inject_sync_msg,
    3.21 +        retrieve_next_sync_msg_t retrieve_next_sync_msg
    3.22      )
    3.23  {
    3.24      session->sync_obj = obj;
    3.25      session->messageToSend = messageToSend;
    3.26      session->showHandshake = showHandshake;
    3.27 +    session->inject_sync_msg = inject_sync_msg;
    3.28 +    session->retrieve_next_sync_msg = retrieve_next_sync_msg;
    3.29  
    3.30      // start state machine
    3.31      session->sync_state = InitState;
    3.32 @@ -34,6 +44,7 @@
    3.33      session->sync_obj = NULL;
    3.34      session->messageToSend = NULL;
    3.35      session->showHandshake = NULL;
    3.36 +    session->retrieve_next_sync_msg = NULL;
    3.37  }
    3.38  
    3.39  DYNAMIC_API PEP_STATUS deliverHandshakeResult(
    3.40 @@ -45,20 +56,53 @@
    3.41      if (!session)
    3.42          return PEP_ILLEGAL_VALUE;
    3.43  
    3.44 +    PEP_STATUS status = PEP_STATUS_OK;
    3.45 +
    3.46      switch (result) {
    3.47          case SYNC_HANDSHAKE_CANCEL:
    3.48 -            fsm_DeviceState_inject(session, Cancel, NULL, 0);
    3.49 +            status = fsm_DeviceState_inject(session, Cancel, NULL, 0);
    3.50              break;
    3.51          case SYNC_HANDSHAKE_ACCEPTED:
    3.52 -            fsm_DeviceState_inject(session, HandshakeAccepted, NULL, 0);
    3.53 +            status = fsm_DeviceState_inject(session, HandshakeAccepted, NULL, 0);
    3.54              break;
    3.55          case SYNC_HANDSHAKE_REJECTED:
    3.56 -            fsm_DeviceState_inject(session, HandshakeRejected, NULL, 0);
    3.57 +            status = fsm_DeviceState_inject(session, HandshakeRejected, NULL, 0);
    3.58              break;
    3.59          default:
    3.60              return PEP_ILLEGAL_VALUE;
    3.61      }
    3.62  
    3.63 +    return status;
    3.64 +}
    3.65 +
    3.66 +DYNAMIC_API PEP_STATUS do_sync_protocol(
    3.67 +        PEP_SESSION session,
    3.68 +        void *management
    3.69 +    )
    3.70 +{
    3.71 +    DeviceGroup_Protocol_t *msg;
    3.72 +    PEP_STATUS status = PEP_STATUS_OK;
    3.73 +
    3.74 +    assert(session && session->retrieve_next_sync_msg);
    3.75 +    assert(management);
    3.76 +
    3.77 +    if (!(session && session->retrieve_next_sync_msg) || !management)
    3.78 +        return PEP_ILLEGAL_VALUE;
    3.79 +
    3.80 +    log_event(session, "sync_protocol thread started", "pEp sync protocol", NULL, NULL);
    3.81 +
    3.82 +    while ((msg = (DeviceGroup_Protocol_t *) session->retrieve_next_sync_msg(management))) 
    3.83 +    {
    3.84 +        if ((status = receive_sync_msg(session, msg) != PEP_STATUS_OK)) {
    3.85 +            char buffer[MAX_LINELENGTH];
    3.86 +            memset(buffer, 0, MAX_LINELENGTH);
    3.87 +            snprintf(buffer, MAX_LINELENGTH, "problem with msg received: %d\n", (int) status);
    3.88 +            log_event(session, buffer, "pEp sync protocol", NULL, NULL);
    3.89 +        }
    3.90 +    }
    3.91 +
    3.92 +    log_event(session, "sync_protocol thread shutdown", "pEp sync protocol", NULL, NULL);
    3.93 +
    3.94      return PEP_STATUS_OK;
    3.95  }
    3.96  
     4.1 --- a/src/sync.h	Tue Aug 16 19:20:20 2016 +0200
     4.2 +++ b/src/sync.h	Sat Aug 20 14:45:19 2016 +0200
     4.3 @@ -12,7 +12,7 @@
     4.4  extern "C" {
     4.5  #endif
     4.6  
     4.7 -// messageToSend() - send a beacon message
     4.8 +// messageToSend() - send a message
     4.9  //
    4.10  //  parameters:
    4.11  //      obj (in)        object handle (implementation defined)
    4.12 @@ -56,8 +56,8 @@
    4.13  // deliverHandshakeResult() - give the result of the handshake dialog
    4.14  //
    4.15  //  parameters:
    4.16 -//      session (in)    session handle
    4.17 -//      result (in)     handshake result
    4.18 +//      session (in)        session handle
    4.19 +//      result (in)         handshake result
    4.20  
    4.21  DYNAMIC_API PEP_STATUS deliverHandshakeResult(
    4.22          PEP_SESSION session,
    4.23 @@ -65,6 +65,29 @@
    4.24      );
    4.25  
    4.26  
    4.27 +// inject_sync_msg - inject sync protocol message
    4.28 +//
    4.29 +//  parameters:
    4.30 +//      msg (in)            message to inject
    4.31 +//      management (in)     application defined
    4.32 +//
    4.33 +//  return value:
    4.34 +//      0 if msg could be stored successfully or nonzero otherwise
    4.35 +
    4.36 +typedef int (*inject_sync_msg_t)(void *msg, void *management);
    4.37 +
    4.38 +
    4.39 +// retrieve_next_sync_msg - receive next sync message
    4.40 +//
    4.41 +//  parameters:
    4.42 +//      management (in)     application defined
    4.43 +//
    4.44 +//  return value:
    4.45 +//      next message or NULL for termination
    4.46 +
    4.47 +typedef void *(*retrieve_next_sync_msg_t)(void *management);
    4.48 +
    4.49 +
    4.50  // register_sync_callbacks() - register adapter's callbacks
    4.51  //
    4.52  //  parameters:
    4.53 @@ -72,6 +95,7 @@
    4.54  //      obj (in)                    object handle (implementation defined)
    4.55  //      messageToSend (in)          callback for sending message
    4.56  //      showHandshake (in)          callback for doing the handshake
    4.57 +//      retrieve_next_sync_msg (in) callback for receiving sync messages
    4.58  //
    4.59  //  return value:
    4.60  //      PEP_STATUS_OK or any other value on errror
    4.61 @@ -83,7 +107,9 @@
    4.62          PEP_SESSION session,
    4.63          void *obj,
    4.64          messageToSend_t messageToSend,
    4.65 -        showHandshake_t showHandshake
    4.66 +        showHandshake_t showHandshake,
    4.67 +        inject_sync_msg_t inject_sync_msg,
    4.68 +        retrieve_next_sync_msg_t retrieve_next_sync_msg
    4.69      );
    4.70  
    4.71  
    4.72 @@ -95,6 +121,31 @@
    4.73  DYNAMIC_API void unregister_sync_callbacks(PEP_SESSION session);
    4.74  
    4.75  
    4.76 +// do_sync_protocol - function to be run on an extra thread
    4.77 +//
    4.78 +//  parameters:
    4.79 +//      session                 pEp session to use
    4.80 +//      retrieve_next_sync_msg  pointer to retrieve_next_identity() callback
    4.81 +//                              which returns at least a valid address field in
    4.82 +//                              the identity struct
    4.83 +//      management              management data to give to keymanagement
    4.84 +//                              (implementation defined)
    4.85 +//
    4.86 +//  return value:
    4.87 +//      PEP_STATUS_OK if thread has to terminate successfully or any other
    4.88 +//      value on failure
    4.89 +//
    4.90 +//  caveat:
    4.91 +//      to ensure proper working of this library, a thread has to be started
    4.92 +//      with this function immediately after initialization
    4.93 +//      do_keymanagement() calls retrieve_next_identity(management)
    4.94 +
    4.95 +DYNAMIC_API PEP_STATUS do_sync_protocol(
    4.96 +        PEP_SESSION session,
    4.97 +        void *management
    4.98 +    );
    4.99 +
   4.100 +
   4.101  #ifdef __cplusplus
   4.102  }
   4.103  #endif
     5.1 --- a/src/sync_actions.c	Tue Aug 16 19:20:20 2016 +0200
     5.2 +++ b/src/sync_actions.c	Sat Aug 20 14:45:19 2016 +0200
     5.3 @@ -228,7 +228,7 @@
     5.4      return status;
     5.5  }
     5.6  
     5.7 -static PEP_STATUS receive_sync_msg(
     5.8 +PEP_STATUS receive_sync_msg(
     5.9          PEP_SESSION session,
    5.10          DeviceGroup_Protocol_t *msg
    5.11      )
    5.12 @@ -295,7 +295,7 @@
    5.13                      (void **) &msg, bl->value, bl->size);
    5.14              if (msg) {
    5.15                  found = true;
    5.16 -                PEP_STATUS status = receive_sync_msg(session, msg);
    5.17 +                PEP_STATUS status = session->inject_sync_msg(msg, session->sync_obj);
    5.18                  ASN_STRUCT_FREE(asn_DEF_DeviceGroup_Protocol, msg);
    5.19                  if (status != PEP_STATUS_OK)
    5.20                      return status;
     6.1 --- a/src/sync_driver.c	Tue Aug 16 19:20:20 2016 +0200
     6.2 +++ b/src/sync_driver.c	Sat Aug 20 14:45:19 2016 +0200
     6.3 @@ -17,7 +17,7 @@
     6.4  
     6.5      DeviceState_state state = fsm_DeviceState(session,
     6.6              session->sync_state, event, partner, extra);
     6.7 -    if (state == invalid_out_of_memory)
     6.8 +    if (state == DeviceState_state_invalid_out_of_memory)
     6.9          return PEP_OUT_OF_MEMORY;
    6.10      if (state < 0)
    6.11          return PEP_SYNC_STATEMACHINE_ERROR - state;
     7.1 --- a/src/sync_fsm.c	Tue Aug 16 19:20:20 2016 +0200
     7.2 +++ b/src/sync_fsm.c	Sat Aug 20 14:45:19 2016 +0200
     7.3 @@ -34,30 +34,30 @@
     7.4                  case KeyGen:
     7.5                      status = sendBeacon(session, state, NULL, NULL);
     7.6                      if (status == PEP_OUT_OF_MEMORY)
     7.7 -                        return invalid_out_of_memory;
     7.8 +                        return (int) invalid_out_of_memory;
     7.9                      if (status != PEP_STATUS_OK)
    7.10 -                        return invalid_action;
    7.11 +                        return (int) invalid_action;
    7.12                      break;
    7.13                  case CannotDecrypt:
    7.14                      status = sendBeacon(session, state, NULL, NULL);
    7.15                      if (status == PEP_OUT_OF_MEMORY)
    7.16 -                        return invalid_out_of_memory;
    7.17 +                        return (int) invalid_out_of_memory;
    7.18                      if (status != PEP_STATUS_OK)
    7.19 -                        return invalid_action;
    7.20 +                        return (int) invalid_action;
    7.21                      break;
    7.22                  case Beacon:
    7.23                      status = sendHandshakeRequest(session, state, partner, NULL);
    7.24                      if (status == PEP_OUT_OF_MEMORY)
    7.25 -                        return invalid_out_of_memory;
    7.26 +                        return (int) invalid_out_of_memory;
    7.27                      if (status != PEP_STATUS_OK)
    7.28 -                        return invalid_action;
    7.29 +                        return (int) invalid_action;
    7.30                      break;
    7.31                  case HandshakeRequest:
    7.32                      status = sendHandshakeRequest(session, state, partner, NULL);
    7.33                      if (status == PEP_OUT_OF_MEMORY)
    7.34 -                        return invalid_out_of_memory;
    7.35 +                        return (int) invalid_out_of_memory;
    7.36                      if (status != PEP_STATUS_OK)
    7.37 -                        return invalid_action;
    7.38 +                        return (int) invalid_action;
    7.39                      return HandshakingSole;
    7.40                  default:
    7.41                      return (DeviceState_state) invalid_event;
    7.42 @@ -69,16 +69,16 @@
    7.43                  case Init:
    7.44                      status = showHandshake(session, state, partner, NULL);
    7.45                      if (status == PEP_OUT_OF_MEMORY)
    7.46 -                        return invalid_out_of_memory;
    7.47 +                        return (int) invalid_out_of_memory;
    7.48                      if (status != PEP_STATUS_OK)
    7.49 -                        return invalid_action;
    7.50 +                        return (int) invalid_action;
    7.51                      break;
    7.52                  case HandshakeRejected:
    7.53                      status = reject(session, state, partner, NULL);
    7.54                      if (status == PEP_OUT_OF_MEMORY)
    7.55 -                        return invalid_out_of_memory;
    7.56 +                        return (int) invalid_out_of_memory;
    7.57                      if (status != PEP_STATUS_OK)
    7.58 -                        return invalid_action;
    7.59 +                        return (int) invalid_action;
    7.60                      return Sole;
    7.61                  case HandshakeAccepted:
    7.62                      cond_result = keyElectionWon(session, partner);
    7.63 @@ -98,18 +98,18 @@
    7.64                  case GroupKeys:
    7.65                      status = storeGroupKeys(session, state, partner, NULL);
    7.66                      if (status == PEP_OUT_OF_MEMORY)
    7.67 -                        return invalid_out_of_memory;
    7.68 +                        return (int) invalid_out_of_memory;
    7.69                      if (status != PEP_STATUS_OK)
    7.70 -                        return invalid_action;
    7.71 +                        return (int) invalid_action;
    7.72                      return Grouped;
    7.73                  case Cancel:
    7.74                      return Sole;
    7.75                  case Reject:
    7.76                      status = reject(session, state, partner, NULL);
    7.77                      if (status == PEP_OUT_OF_MEMORY)
    7.78 -                        return invalid_out_of_memory;
    7.79 +                        return (int) invalid_out_of_memory;
    7.80                      if (status != PEP_STATUS_OK)
    7.81 -                        return invalid_action;
    7.82 +                        return (int) invalid_action;
    7.83                      return Sole;
    7.84                  default:
    7.85                      return (DeviceState_state) invalid_event;
    7.86 @@ -121,37 +121,37 @@
    7.87                  case KeyGen:
    7.88                      status = sendGroupKeys(session, state, NULL, NULL);
    7.89                      if (status == PEP_OUT_OF_MEMORY)
    7.90 -                        return invalid_out_of_memory;
    7.91 +                        return (int) invalid_out_of_memory;
    7.92                      if (status != PEP_STATUS_OK)
    7.93 -                        return invalid_action;
    7.94 +                        return (int) invalid_action;
    7.95                      break;
    7.96                  case HandshakeRequest:
    7.97                      status = sendHandshakeRequest(session, state, partner, NULL);
    7.98                      if (status == PEP_OUT_OF_MEMORY)
    7.99 -                        return invalid_out_of_memory;
   7.100 +                        return (int) invalid_out_of_memory;
   7.101                      if (status != PEP_STATUS_OK)
   7.102 -                        return invalid_action;
   7.103 +                        return (int) invalid_action;
   7.104                      status = showHandshake(session, state, partner, NULL);
   7.105                      if (status == PEP_OUT_OF_MEMORY)
   7.106 -                        return invalid_out_of_memory;
   7.107 +                        return (int) invalid_out_of_memory;
   7.108                      if (status != PEP_STATUS_OK)
   7.109 -                        return invalid_action;
   7.110 +                        return (int) invalid_action;
   7.111                      break;
   7.112                  case HandshakeRejected:
   7.113                      status = reject(session, state, partner, NULL);
   7.114                      if (status == PEP_OUT_OF_MEMORY)
   7.115 -                        return invalid_out_of_memory;
   7.116 +                        return (int) invalid_out_of_memory;
   7.117                      if (status != PEP_STATUS_OK)
   7.118 -                        return invalid_action;
   7.119 +                        return (int) invalid_action;
   7.120                      break;
   7.121                  case Hand:
   7.122                      break;
   7.123                  case Reject:
   7.124                      status = reject(session, state, NULL, NULL);
   7.125                      if (status == PEP_OUT_OF_MEMORY)
   7.126 -                        return invalid_out_of_memory;
   7.127 +                        return (int) invalid_out_of_memory;
   7.128                      if (status != PEP_STATUS_OK)
   7.129 -                        return invalid_action;
   7.130 +                        return (int) invalid_action;
   7.131                      break;
   7.132                  default:
   7.133                      return (DeviceState_state) invalid_event;
     8.1 --- a/src/sync_fsm.h	Tue Aug 16 19:20:20 2016 +0200
     8.2 +++ b/src/sync_fsm.h	Sat Aug 20 14:45:19 2016 +0200
     8.3 @@ -37,6 +37,13 @@
     8.4  // states
     8.5  
     8.6  typedef enum _DeviceState_state {
     8.7 +    // error values also in this namespace
     8.8 +    DeviceState_state_invalid_state = (int) invalid_state,
     8.9 +    DeviceState_state_invalid_event = (int) invalid_event,
    8.10 +    DeviceState_state_invalid_condition = (int) invalid_condition,
    8.11 +    DeviceState_state_invalid_action = (int) invalid_action,
    8.12 +    DeviceState_state_invalid_out_of_memory = (int) invalid_out_of_memory,
    8.13 +
    8.14      DeviceState_state_NONE = 0,
    8.15      InitState, 
    8.16      Sole, 
     9.1 --- a/sync/gen_statemachine.ysl2	Tue Aug 16 19:20:20 2016 +0200
     9.2 +++ b/sync/gen_statemachine.ysl2	Sat Aug 20 14:45:19 2016 +0200
     9.3 @@ -62,6 +62,13 @@
     9.4          // states
     9.5  
     9.6          typedef enum _«@name»_state {
     9.7 +            // error values also in this namespace
     9.8 +            «@name»_state_invalid_state = (int) invalid_state,
     9.9 +            «@name»_state_invalid_event = (int) invalid_event,
    9.10 +            «@name»_state_invalid_condition = (int) invalid_condition,
    9.11 +            «@name»_state_invalid_action = (int) invalid_action,
    9.12 +            «@name»_state_invalid_out_of_memory = (int) invalid_out_of_memory,
    9.13 +
    9.14              «@name»_state_NONE = 0,
    9.15          `` for "func:distinctName(state)" |> «@name»`if "position()!=last()" > , `
    9.16          } «@name»_state;
    9.17 @@ -135,7 +142,7 @@
    9.18  
    9.19              «@name»_state state = fsm_«@name»(session,
    9.20                      session->«@filename»_state, event, partner, extra);
    9.21 -            if (state == invalid_out_of_memory)
    9.22 +            if (state == «@name»_state_invalid_out_of_memory)
    9.23                  return PEP_OUT_OF_MEMORY;
    9.24              if (state < 0)
    9.25                  return PEP_SYNC_STATEMACHINE_ERROR - state;
    9.26 @@ -202,9 +209,9 @@
    9.27          }
    9.28          > , NULL);\n
    9.29          | if (status == PEP_OUT_OF_MEMORY)
    9.30 -        |> return invalid_out_of_memory;
    9.31 +        |> return (int) invalid_out_of_memory;
    9.32          | if (status != PEP_STATUS_OK)
    9.33 -        |> return invalid_action;
    9.34 +        |> return (int) invalid_action;
    9.35      }
    9.36  
    9.37      template "condition" {