store group keys keysync
authorVolker Birk <vb@pep.foundation>
Tue, 02 Aug 2016 12:06:45 +0200
branchkeysync
changeset 964ef02296c41ca
parent 959 c1c463ecf30c
child 965 2c833e0a883b
store group keys
src/pEpEngine.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/pEpEngine.h	Mon Aug 01 12:32:04 2016 +0200
     1.2 +++ b/src/pEpEngine.h	Tue Aug 02 12:06:45 2016 +0200
     1.3 @@ -74,6 +74,12 @@
     1.4      PEP_SYNC_NO_TRUSTWORDS_CALLBACK                 = 0x0901,
     1.5      PEP_SYNC_ILLEGAL_MESSAGE                        = 0x0902,
     1.6  
     1.7 +    PEP_SYNC_STATEMACHINE_ERROR                     = 0x0980,
     1.8 +    PEP_STATEMACHINE_INVALID_STATE                  = 0x0982,
     1.9 +    PEP_STATEMACHINE_INVALID_EVENT                  = 0x0983,
    1.10 +    PEP_STATEMACHINE_INVALID_CONDITION              = 0x0984,
    1.11 +    PEP_STATEMACHINE_INVALID_ACTION                 = 0x0985,
    1.12 +
    1.13      PEP_COMMIT_FAILED                               = 0xff01,
    1.14      PEP_MESSAGE_CONSUMED                            = 0xff02,
    1.15  
     2.1 --- a/src/sync_actions.c	Mon Aug 01 12:32:04 2016 +0200
     2.2 +++ b/src/sync_actions.c	Tue Aug 02 12:06:45 2016 +0200
     2.3 @@ -28,7 +28,7 @@
     2.4  {
     2.5      assert(session);
     2.6      if (!session)
     2.7 -        return -1; // error
     2.8 +        return invalid_condition; // error
     2.9  
    2.10      bool gc = false;
    2.11      int int_result = sqlite3_exec(
    2.12 @@ -40,7 +40,7 @@
    2.13      );
    2.14      assert(int_result == SQLITE_OK);
    2.15      if (int_result != SQLITE_OK)
    2.16 -        return -1; // error
    2.17 +        return invalid_condition; // error
    2.18  
    2.19      if (gc)
    2.20          return 1;
    2.21 @@ -53,7 +53,7 @@
    2.22      assert(session);
    2.23      assert(partner);
    2.24      if (!(session && partner))
    2.25 -        return -1; // error
    2.26 +        return invalid_condition; // error
    2.27  
    2.28      // an already existing group always wins
    2.29  
    2.30 @@ -68,10 +68,12 @@
    2.31      Identity me = NULL;
    2.32      PEP_STATUS status = get_identity(session, partner->address, PEP_OWN_USERID,
    2.33              &me);
    2.34 +    if (status == PEP_OUT_OF_MEMORY)
    2.35 +        return invalid_out_of_memory;
    2.36      if (status != PEP_STATUS_OK)
    2.37 -        return -1; // error
    2.38 +        return invalid_condition; // error
    2.39  
    2.40 -    int result = -1; // error state has to be overwritten
    2.41 +    int result = invalid_condition; // error state has to be overwritten
    2.42  
    2.43      time_t own_created;
    2.44      time_t partners_created;
    2.45 @@ -137,8 +139,6 @@
    2.46      free_identity(partner);
    2.47      return status;
    2.48  
    2.49 -enomem:
    2.50 -    status = PEP_OUT_OF_MEMORY;
    2.51  error:
    2.52      free_identity(me);
    2.53      free_identity(partner);
    2.54 @@ -176,13 +176,6 @@
    2.55  
    2.56      free_identity(partner);
    2.57      return status;
    2.58 -
    2.59 -enomem:
    2.60 -    status = PEP_OUT_OF_MEMORY;
    2.61 -error:
    2.62 -    free_identity(partner);
    2.63 -    // free...
    2.64 -    return status;
    2.65  }
    2.66  
    2.67  
    2.68 @@ -216,14 +209,23 @@
    2.69      if (!group_keys)
    2.70          goto enomem;
    2.71  
    2.72 +    for (identity_list *il = group_keys; il && il->ident; il = il->next) {
    2.73 +        free(il->ident->user_id);
    2.74 +        il->ident->user_id = strdup(PEP_OWN_USERID);
    2.75 +        assert(il->ident->user_id);
    2.76 +        if (!il->ident->user_id)
    2.77 +            goto enomem;
    2.78 +        status = set_identity(session, il->ident);
    2.79 +        if (status != PEP_STATUS_OK)
    2.80 +            break;
    2.81 +    }
    2.82 +
    2.83      free_identity(partner);
    2.84      free_identity_list(group_keys);
    2.85      return status;
    2.86  
    2.87  enomem:
    2.88      status = PEP_OUT_OF_MEMORY;
    2.89 -error:
    2.90 -    // free...
    2.91      free_identity(partner);
    2.92      free_identity_list(group_keys);
    2.93      return status;
     3.1 --- a/src/sync_driver.c	Mon Aug 01 12:32:04 2016 +0200
     3.2 +++ b/src/sync_driver.c	Tue Aug 02 12:06:45 2016 +0200
     3.3 @@ -15,9 +15,14 @@
     3.4      if (!session)
     3.5          return PEP_ILLEGAL_VALUE;
     3.6  
     3.7 -    session->sync_state = fsm_DeviceState(session, session->sync_state,
     3.8 -            event, partner, extra);
     3.9 +    DeviceState_state state = fsm_DeviceState(session,
    3.10 +            session->sync_state, event, partner, extra);
    3.11 +    if (state == invalid_out_of_memory)
    3.12 +        return PEP_OUT_OF_MEMORY;
    3.13 +    if (state < 0)
    3.14 +        return PEP_SYNC_STATEMACHINE_ERROR - state;
    3.15  
    3.16 +    session->sync_state = state;
    3.17      return PEP_STATUS_OK;
    3.18  }
    3.19  
     4.1 --- a/src/sync_fsm.c	Mon Aug 01 12:32:04 2016 +0200
     4.2 +++ b/src/sync_fsm.c	Tue Aug 02 12:06:45 2016 +0200
     4.3 @@ -10,7 +10,7 @@
     4.4          void *extra
     4.5      )
     4.6  {
     4.7 -    int cond_result = -1;
     4.8 +    int cond_result;
     4.9      PEP_STATUS status = PEP_STATUS_OK;
    4.10  
    4.11      switch (state) {
    4.12 @@ -18,9 +18,8 @@
    4.13              switch (event) {
    4.14                  case Init:
    4.15                      cond_result = storedGroupKeys(session);
    4.16 -                    assert(cond_result == 0 || cond_result == 1);
    4.17 -                    if (cond_result == -1)
    4.18 -                        return invalid_condition;
    4.19 +                    if (cond_result < 0)
    4.20 +                        return cond_result;
    4.21                      if (cond_result) {
    4.22                          return Grouped;
    4.23                      }
    4.24 @@ -34,21 +33,29 @@
    4.25              switch (event) {
    4.26                  case KeyGen:
    4.27                      status = sendBeacon(session, state, NULL, NULL);
    4.28 +                    if (status == PEP_OUT_OF_MEMORY)
    4.29 +                        return invalid_out_of_memory;
    4.30                      if (status != PEP_STATUS_OK)
    4.31                          return invalid_action;
    4.32                      break;
    4.33                  case CannotDecrypt:
    4.34                      status = sendBeacon(session, state, NULL, NULL);
    4.35 +                    if (status == PEP_OUT_OF_MEMORY)
    4.36 +                        return invalid_out_of_memory;
    4.37                      if (status != PEP_STATUS_OK)
    4.38                          return invalid_action;
    4.39                      break;
    4.40                  case Beacon:
    4.41                      status = sendHandshakeRequest(session, state, partner, NULL);
    4.42 +                    if (status == PEP_OUT_OF_MEMORY)
    4.43 +                        return invalid_out_of_memory;
    4.44                      if (status != PEP_STATUS_OK)
    4.45                          return invalid_action;
    4.46                      break;
    4.47                  case HandshakeRequest:
    4.48                      status = sendHandshakeRequest(session, state, partner, NULL);
    4.49 +                    if (status == PEP_OUT_OF_MEMORY)
    4.50 +                        return invalid_out_of_memory;
    4.51                      if (status != PEP_STATUS_OK)
    4.52                          return invalid_action;
    4.53                      return HandshakingSole;
    4.54 @@ -61,19 +68,22 @@
    4.55              switch (event) {
    4.56                  case Init:
    4.57                      status = showHandshake(session, state, partner, NULL);
    4.58 +                    if (status == PEP_OUT_OF_MEMORY)
    4.59 +                        return invalid_out_of_memory;
    4.60                      if (status != PEP_STATUS_OK)
    4.61                          return invalid_action;
    4.62                      break;
    4.63                  case HandshakeRejected:
    4.64                      status = reject(session, state, partner, NULL);
    4.65 +                    if (status == PEP_OUT_OF_MEMORY)
    4.66 +                        return invalid_out_of_memory;
    4.67                      if (status != PEP_STATUS_OK)
    4.68                          return invalid_action;
    4.69                      return Sole;
    4.70                  case HandshakeAccepted:
    4.71                      cond_result = keyElectionWon(session, partner);
    4.72 -                    assert(cond_result == 0 || cond_result == 1);
    4.73 -                    if (cond_result == -1)
    4.74 -                        return invalid_condition;
    4.75 +                    if (cond_result < 0)
    4.76 +                        return cond_result;
    4.77                      if (cond_result) {
    4.78                          return Grouped;
    4.79                      }
    4.80 @@ -87,6 +97,8 @@
    4.81              switch (event) {
    4.82                  case GroupKeys:
    4.83                      status = storeGroupKeys(session, state, partner, NULL);
    4.84 +                    if (status == PEP_OUT_OF_MEMORY)
    4.85 +                        return invalid_out_of_memory;
    4.86                      if (status != PEP_STATUS_OK)
    4.87                          return invalid_action;
    4.88                      return Grouped;
    4.89 @@ -94,6 +106,8 @@
    4.90                      return Sole;
    4.91                  case Reject:
    4.92                      status = reject(session, state, partner, NULL);
    4.93 +                    if (status == PEP_OUT_OF_MEMORY)
    4.94 +                        return invalid_out_of_memory;
    4.95                      if (status != PEP_STATUS_OK)
    4.96                          return invalid_action;
    4.97                      return Sole;
    4.98 @@ -106,19 +120,27 @@
    4.99              switch (event) {
   4.100                  case KeyGen:
   4.101                      status = sendGroupKeys(session, state, NULL, NULL);
   4.102 +                    if (status == PEP_OUT_OF_MEMORY)
   4.103 +                        return invalid_out_of_memory;
   4.104                      if (status != PEP_STATUS_OK)
   4.105                          return invalid_action;
   4.106                      break;
   4.107                  case HandshakeRequest:
   4.108                      status = sendHandshakeRequest(session, state, partner, NULL);
   4.109 +                    if (status == PEP_OUT_OF_MEMORY)
   4.110 +                        return invalid_out_of_memory;
   4.111                      if (status != PEP_STATUS_OK)
   4.112                          return invalid_action;
   4.113                      status = showHandshake(session, state, partner, NULL);
   4.114 +                    if (status == PEP_OUT_OF_MEMORY)
   4.115 +                        return invalid_out_of_memory;
   4.116                      if (status != PEP_STATUS_OK)
   4.117                          return invalid_action;
   4.118                      break;
   4.119                  case HandshakeRejected:
   4.120                      status = reject(session, state, partner, NULL);
   4.121 +                    if (status == PEP_OUT_OF_MEMORY)
   4.122 +                        return invalid_out_of_memory;
   4.123                      if (status != PEP_STATUS_OK)
   4.124                          return invalid_action;
   4.125                      break;
   4.126 @@ -126,6 +148,8 @@
   4.127                      break;
   4.128                  case Reject:
   4.129                      status = reject(session, state, NULL, NULL);
   4.130 +                    if (status == PEP_OUT_OF_MEMORY)
   4.131 +                        return invalid_out_of_memory;
   4.132                      if (status != PEP_STATUS_OK)
   4.133                          return invalid_action;
   4.134                      break;
     5.1 --- a/src/sync_fsm.h	Mon Aug 01 12:32:04 2016 +0200
     5.2 +++ b/src/sync_fsm.h	Tue Aug 02 12:06:45 2016 +0200
     5.3 @@ -19,10 +19,15 @@
     5.4  // error values
     5.5  
     5.6  typedef enum _fsm_error {
     5.7 +    // these error values are corresponding to
     5.8 +    // PEP_SYNC_STATEMACHINE_ERROR - value
     5.9      invalid_state = -2,
    5.10      invalid_event = -3,
    5.11      invalid_condition = -4,
    5.12 -    invalid_action = -5
    5.13 +    invalid_action = -5,
    5.14 +
    5.15 +    // out of memory condition
    5.16 +    invalid_out_of_memory = -128
    5.17  } fsm_error;
    5.18  
    5.19  // conditions
     6.1 --- a/sync/gen_statemachine.ysl2	Mon Aug 01 12:32:04 2016 +0200
     6.2 +++ b/sync/gen_statemachine.ysl2	Tue Aug 02 12:06:45 2016 +0200
     6.3 @@ -45,10 +45,15 @@
     6.4          // error values
     6.5  
     6.6          typedef enum _fsm_error {
     6.7 +            // these error values are corresponding to
     6.8 +            // PEP_SYNC_STATEMACHINE_ERROR - value
     6.9              invalid_state = -2,
    6.10              invalid_event = -3,
    6.11              invalid_condition = -4,
    6.12 -            invalid_action = -5
    6.13 +            invalid_action = -5,
    6.14 +
    6.15 +            // out of memory condition
    6.16 +            invalid_out_of_memory = -128
    6.17          } fsm_error;
    6.18  
    6.19          // conditions
    6.20 @@ -129,9 +134,14 @@
    6.21              if (!session)
    6.22                  return PEP_ILLEGAL_VALUE;
    6.23  
    6.24 -            session->«@filename»_state = fsm_«@name»(session, session->«@filename»_state,
    6.25 -                    event, partner, extra);
    6.26 +            «@name»_state state = fsm_«@name»(session,
    6.27 +                    session->«@filename»_state, event, partner, extra);
    6.28 +            if (state == invalid_out_of_memory)
    6.29 +                return PEP_OUT_OF_MEMORY;
    6.30 +            if (state < 0)
    6.31 +                return PEP_SYNC_STATEMACHINE_ERROR - state;
    6.32  
    6.33 +            session->«@filename»_state = state;
    6.34              return PEP_STATUS_OK;
    6.35          }
    6.36  
    6.37 @@ -150,7 +160,7 @@
    6.38                  void *extra
    6.39              )
    6.40          {
    6.41 -            int cond_result = -1;
    6.42 +            int cond_result;
    6.43              PEP_STATUS status = PEP_STATUS_OK;
    6.44  
    6.45              switch (state) {
    6.46 @@ -192,15 +202,16 @@
    6.47              otherwise > NULL
    6.48          }
    6.49          > , NULL);\n
    6.50 +        | if (status == PEP_OUT_OF_MEMORY)
    6.51 +        |> return invalid_out_of_memory;
    6.52          | if (status != PEP_STATUS_OK)
    6.53          |> return invalid_action;
    6.54      }
    6.55  
    6.56      template "condition" {
    6.57          | cond_result = «@name»(session`apply "parm", 0`);
    6.58 -        | assert(cond_result == 0 || cond_result == 1);
    6.59 -        | if (cond_result == -1)
    6.60 -        |> return invalid_condition;
    6.61 +        | if (cond_result < 0)
    6.62 +        |> return cond_result;
    6.63          | if (cond_result) {
    6.64          apply "action|transition|condition";
    6.65          | }