Merged keysync
authorEdouard Tisserant
Fri, 02 Sep 2016 01:33:02 +0200
branchkeysync
changeset 1120277b32596365
parent 1119 97f329284202
parent 1117 afcee607d04d
child 1121 a1f6ea9586b9
Merged
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/asn1_helper.c	Fri Sep 02 01:33:02 2016 +0200
     1.3 @@ -0,0 +1,36 @@
     1.4 +#include "asn1_helper.h"
     1.5 +#include <assert.h>
     1.6 +#include <stdlib.h>
     1.7 +
     1.8 +growing_buf_t *new_growing_buf(void)
     1.9 +{
    1.10 +    growing_buf_t *result = calloc(1, sizeof(growing_buf_t));
    1.11 +    assert(result);
    1.12 +    return result;
    1.13 +}
    1.14 +
    1.15 +void free_growing_buf(growing_buf_t *buf)
    1.16 +{
    1.17 +    if (buf) {
    1.18 +        free(buf->data);
    1.19 +        free(buf);
    1.20 +    }
    1.21 +}
    1.22 +
    1.23 +int consume_bytes(const void *src, size_t size, growing_buf_t *dst)
    1.24 +{
    1.25 +    assert(src && dst);
    1.26 +    if (!(src && dst))
    1.27 +        return -1;
    1.28 +
    1.29 +    char *new_data = realloc(dst->data, dst->size + size + 1);
    1.30 +    if (!new_data)
    1.31 +        return -1;
    1.32 +    dst->data = new_data;
    1.33 +    memcpy(dst->data + dst->size, src, size);
    1.34 +    dst->size += size;
    1.35 +    dst->data[dst->size] = 0; // safeguard
    1.36 +
    1.37 +    return 1;
    1.38 +}
    1.39 +
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/src/asn1_helper.h	Fri Sep 02 01:33:02 2016 +0200
     2.3 @@ -0,0 +1,13 @@
     2.4 +#pragma once
     2.5 +
     2.6 +#include <string.h>
     2.7 +
     2.8 +typedef struct growing_buf {
     2.9 +    char *data;
    2.10 +    size_t size;
    2.11 +} growing_buf_t;
    2.12 +
    2.13 +growing_buf_t *new_growing_buf(void);
    2.14 +void free_growing_buf(growing_buf_t *buf);
    2.15 +int consume_bytes(const void *src, size_t size, growing_buf_t *dst);
    2.16 +
     3.1 --- a/src/pEpEngine.c	Fri Sep 02 01:32:22 2016 +0200
     3.2 +++ b/src/pEpEngine.c	Fri Sep 02 01:33:02 2016 +0200
     3.3 @@ -533,8 +533,6 @@
     3.4  
     3.5      // runtime config
     3.6  
     3.7 -    _session->passive_mode = false;
     3.8 -    _session->unencrypted_subject = false;
     3.9  #ifdef ANDROID
    3.10      _session->use_only_own_private_keys = true;
    3.11  #elif TARGET_OS_IPHONE
    3.12 @@ -659,6 +657,12 @@
    3.13      session->use_only_own_private_keys = enable;
    3.14  }
    3.15  
    3.16 +DYNAMIC_API void config_keep_sync_msg(PEP_SESSION session, bool enable)
    3.17 +{
    3.18 +    assert(session);
    3.19 +    session->keep_sync_msg = enable;
    3.20 +}
    3.21 +
    3.22  DYNAMIC_API PEP_STATUS log_event(
    3.23          PEP_SESSION session,
    3.24          const char *title,
     4.1 --- a/src/pEpEngine.h	Fri Sep 02 01:32:22 2016 +0200
     4.2 +++ b/src/pEpEngine.h	Fri Sep 02 01:33:02 2016 +0200
     4.3 @@ -168,6 +168,14 @@
     4.4  DYNAMIC_API void config_use_only_own_private_keys(PEP_SESSION session, bool enable);
     4.5  
     4.6  
     4.7 +// config_keep_sync_msg() - do not remove sync messages (for debugging purposes)
     4.8 +//
     4.9 +//      session (in)    session handle
    4.10 +//      enable (in)     flag if enabled or disabled
    4.11 +
    4.12 +DYNAMIC_API void config_keep_sync_msg(PEP_SESSION session, bool enable);
    4.13 +
    4.14 +
    4.15  // decrypt_and_verify() - decrypt and/or verify a message
    4.16  //
    4.17  //    parameters:
     5.1 --- a/src/pEp_internal.h	Fri Sep 02 01:32:22 2016 +0200
     5.2 +++ b/src/pEp_internal.h	Fri Sep 02 01:33:02 2016 +0200
     5.3 @@ -145,6 +145,7 @@
     5.4      bool passive_mode;
     5.5      bool unencrypted_subject;
     5.6      bool use_only_own_private_keys;
     5.7 +    bool keep_sync_msg;
     5.8      
     5.9  } pEpSession;
    5.10  
     6.1 --- a/src/sync.c	Fri Sep 02 01:32:22 2016 +0200
     6.2 +++ b/src/sync.c	Fri Sep 02 01:33:02 2016 +0200
     6.3 @@ -3,6 +3,7 @@
     6.4  #include <memory.h>
     6.5  #include <assert.h>
     6.6  
     6.7 +#include "asn1_helper.h"
     6.8  #include "../asn.1/DeviceGroup-Protocol.h"
     6.9  
    6.10  // receive_sync_msg is defined in the sync_actions
    6.11 @@ -110,3 +111,45 @@
    6.12      return PEP_STATUS_OK;
    6.13  }
    6.14  
    6.15 +DYNAMIC_API PEP_STATUS decode_sync_msg(
    6.16 +        const char *data,
    6.17 +        size_t size,
    6.18 +        char **text
    6.19 +    )
    6.20 +{
    6.21 +    PEP_STATUS status = PEP_STATUS_OK;
    6.22 +
    6.23 +    assert(data && text);
    6.24 +    if (!(data && text))
    6.25 +        return PEP_ILLEGAL_VALUE;
    6.26 +
    6.27 +    *text = NULL;
    6.28 +
    6.29 +    DeviceGroup_Protocol_t *msg = NULL;
    6.30 +    uper_decode_complete(NULL, &asn_DEF_DeviceGroup_Protocol, (void **) &msg,
    6.31 +            data, size);
    6.32 +    if (!msg)
    6.33 +        return PEP_SYNC_ILLEGAL_MESSAGE;
    6.34 +
    6.35 +    growing_buf_t *dst = new_growing_buf();
    6.36 +    if (!dst) {
    6.37 +        status = PEP_OUT_OF_MEMORY;
    6.38 +        goto the_end;
    6.39 +    }
    6.40 +
    6.41 +    asn_enc_rval_t er = xer_encode(&asn_DEF_DeviceGroup_Protocol, msg,
    6.42 +            XER_F_BASIC, (asn_app_consume_bytes_f *) consume_bytes, (void *) dst);
    6.43 +    if (er.encoded == -1) {
    6.44 +        status = PEP_SYNC_ILLEGAL_MESSAGE;
    6.45 +        goto the_end;
    6.46 +    }
    6.47 +
    6.48 +    *text = dst->data;
    6.49 +    dst->data = NULL;
    6.50 +
    6.51 +the_end:
    6.52 +    free_growing_buf(dst);
    6.53 +    ASN_STRUCT_FREE(asn_DEF_DeviceGroup_Protocol, msg);
    6.54 +    return status;
    6.55 +}
    6.56 +
     7.1 --- a/src/sync.h	Fri Sep 02 01:32:22 2016 +0200
     7.2 +++ b/src/sync.h	Fri Sep 02 01:33:02 2016 +0200
     7.3 @@ -121,7 +121,7 @@
     7.4  DYNAMIC_API void unregister_sync_callbacks(PEP_SESSION session);
     7.5  
     7.6  
     7.7 -// do_sync_protocol - function to be run on an extra thread
     7.8 +// do_sync_protocol() - function to be run on an extra thread
     7.9  //
    7.10  //  parameters:
    7.11  //      session                 pEp session to use
    7.12 @@ -146,6 +146,20 @@
    7.13      );
    7.14  
    7.15  
    7.16 +// decode_sync_msg() - decode sync message from PER into XER
    7.17 +//
    7.18 +//  parameters:
    7.19 +//      data (in)               PER encoded data
    7.20 +//      size (in)               size of PER encoded data
    7.21 +//      text (out)              XER text of the same sync message
    7.22 +
    7.23 +DYNAMIC_API PEP_STATUS decode_sync_msg(
    7.24 +        const char *data,
    7.25 +        size_t size,
    7.26 +        char **text
    7.27 +    );
    7.28 +
    7.29 +
    7.30  #ifdef __cplusplus
    7.31  }
    7.32  #endif
     8.1 --- a/src/sync_impl.c	Fri Sep 02 01:32:22 2016 +0200
     8.2 +++ b/src/sync_impl.c	Fri Sep 02 01:33:02 2016 +0200
     8.3 @@ -117,14 +117,19 @@
     8.4                  }
     8.5              }
     8.6  
     8.7 -            bloblist_t *blob = bl;
     8.8 -            if (last)
     8.9 -                last->next = bl->next;
    8.10 -            else
    8.11 -                src->attachments = bl->next;
    8.12 +            if (!session->keep_sync_msg) {
    8.13 +                bloblist_t *blob = bl;
    8.14 +                if (last)
    8.15 +                    last->next = bl->next;
    8.16 +                else
    8.17 +                    src->attachments = bl->next;
    8.18  
    8.19 -            blob->next = NULL;
    8.20 -            free_bloblist(blob);
    8.21 +                blob->next = NULL;
    8.22 +                free_bloblist(blob);
    8.23 +            }
    8.24 +            else {
    8.25 +                last = bl;
    8.26 +            }
    8.27          }
    8.28          else {
    8.29  skip:
    8.30 @@ -132,7 +137,7 @@
    8.31          }
    8.32      }
    8.33  
    8.34 -    if (found) {
    8.35 +    if (found && !session->keep_sync_msg) {
    8.36          for (stringpair_list_t *spl = src->opt_fields ; spl && spl->value ;
    8.37                  spl = spl->next) {
    8.38              if (spl->value->key &&