adding distribution_codec sync
authorVolker Birk <vb@pep-project.org>
Wed, 11 Dec 2019 16:21:45 +0100
branchsync
changeset 42577b2a471c9dff
parent 4256 470fa7394f5f
child 4258 31cb55240542
adding distribution_codec
src/distribution_codec.c
src/distribution_codec.h
src/key_reset.c
src/key_reset.h
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/distribution_codec.c	Wed Dec 11 16:21:45 2019 +0100
     1.3 @@ -0,0 +1,134 @@
     1.4 +// This file is under GNU General Public License 3.0
     1.5 +// see LICENSE.txt
     1.6 +
     1.7 +#include "platform.h"
     1.8 +
     1.9 +#include "distribution_codec.h"
    1.10 +#include "../asn.1/Distribution.h"
    1.11 +#include "pEp_internal.h"
    1.12 +#include "growing_buf.h"
    1.13 +
    1.14 +DYNAMIC_API PEP_STATUS decode_Distribution_message(
    1.15 +        const char *data,
    1.16 +        size_t size,
    1.17 +        Distribution_t **msg
    1.18 +    )
    1.19 +{
    1.20 +    assert(data && msg);
    1.21 +    if (!(data && msg))
    1.22 +        return PEP_ILLEGAL_VALUE;
    1.23 +
    1.24 +    *msg = NULL;
    1.25 +    Distribution_t *_msg = NULL;
    1.26 +    uper_decode_complete(NULL, &asn_DEF_Distribution, (void **) &_msg, data, size);
    1.27 +    if (!_msg)
    1.28 +        return PEP_DISTRIBUTION_ILLEGAL_MESSAGE;
    1.29 +
    1.30 +    *msg = _msg;
    1.31 +    return PEP_STATUS_OK;
    1.32 +}
    1.33 +
    1.34 +PEP_STATUS encode_Distribution_message(
    1.35 +        Distribution_t *msg,
    1.36 +        char **data,
    1.37 +        size_t *size
    1.38 +    )
    1.39 +{
    1.40 +    assert(data && msg);
    1.41 +    if (!(data && msg))
    1.42 +        return PEP_ILLEGAL_VALUE;
    1.43 +
    1.44 +    *data = NULL;
    1.45 +    *size = 0;
    1.46 +
    1.47 +    char *_data = NULL;
    1.48 +    ssize_t _size = uper_encode_to_new_buffer(&asn_DEF_Distribution, NULL, msg,
    1.49 +            (void **) &_data);
    1.50 +    if (_size == -1)
    1.51 +        return PEP_CANNOT_ENCODE;
    1.52 +
    1.53 +    *data = _data;
    1.54 +    *size = (size_t) _size;
    1.55 +
    1.56 +    return PEP_STATUS_OK;
    1.57 +}
    1.58 +
    1.59 +PEP_STATUS PER_to_XER_Distribution_msg(
    1.60 +        const char *data,
    1.61 +        size_t size,
    1.62 +        char **text
    1.63 +    )
    1.64 +{
    1.65 +    PEP_STATUS status = PEP_STATUS_OK;
    1.66 +    growing_buf_t *dst = NULL;
    1.67 +
    1.68 +    assert(data && text);
    1.69 +    if (!(data && text))
    1.70 +        return PEP_ILLEGAL_VALUE;
    1.71 +
    1.72 +    *text = NULL;
    1.73 +
    1.74 +    Distribution_t *msg = NULL;
    1.75 +    status = decode_Distribution_message(data, size, &msg);
    1.76 +    if (status)
    1.77 +        goto the_end;
    1.78 +
    1.79 +    dst = new_growing_buf();
    1.80 +    if (!dst) {
    1.81 +        status = PEP_OUT_OF_MEMORY;
    1.82 +        goto the_end;
    1.83 +    }
    1.84 +
    1.85 +    asn_enc_rval_t er = xer_encode(&asn_DEF_Distribution, msg, XER_F_BASIC,
    1.86 +            (asn_app_consume_bytes_f *) growing_buf_consume, (void *) dst);
    1.87 +    if (er.encoded == -1) {
    1.88 +        status = PEP_CANNOT_ENCODE;
    1.89 +        goto the_end;
    1.90 +    }
    1.91 +
    1.92 +    *text = dst->data;
    1.93 +    dst->data = NULL;
    1.94 +
    1.95 +the_end:
    1.96 +    free_growing_buf(dst);
    1.97 +    ASN_STRUCT_FREE(asn_DEF_Distribution, msg);
    1.98 +    return status;
    1.99 +}
   1.100 +
   1.101 +PEP_STATUS XER_to_PER_Distribution_msg(
   1.102 +        const char *text,
   1.103 +        char **data,
   1.104 +        size_t *size
   1.105 +    )
   1.106 +{
   1.107 +    PEP_STATUS status = PEP_STATUS_OK;
   1.108 +
   1.109 +    assert(text && data && size);
   1.110 +    if (!(text && data && size))
   1.111 +        return PEP_ILLEGAL_VALUE;
   1.112 +
   1.113 +    *data = NULL;
   1.114 +    *size = 0;
   1.115 +
   1.116 +    Distribution_t *msg = NULL;
   1.117 +    asn_dec_rval_t dr = xer_decode(NULL, &asn_DEF_Distribution, (void **) &msg,
   1.118 +            (const void *) text, strlen(text));
   1.119 +    if (dr.code != RC_OK) {
   1.120 +        status = PEP_DISTRIBUTION_ILLEGAL_MESSAGE;
   1.121 +        goto the_end;
   1.122 +    }
   1.123 +
   1.124 +    char *_data = NULL;
   1.125 +    size_t _size = 0;
   1.126 +    status = encode_Distribution_message(msg, &_data, &_size);
   1.127 +    if (status)
   1.128 +        goto the_end;
   1.129 +
   1.130 +    *data = _data;
   1.131 +    *size = (size_t) _size;
   1.132 +
   1.133 +the_end:
   1.134 +    ASN_STRUCT_FREE(asn_DEF_Distribution, msg);
   1.135 +    return status;
   1.136 +}
   1.137 +
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/src/distribution_codec.h	Wed Dec 11 16:21:45 2019 +0100
     2.3 @@ -0,0 +1,82 @@
     2.4 +// This file is under GNU General Public License 3.0
     2.5 +// see LICENSE.txt
     2.6 +
     2.7 +#pragma once
     2.8 +
     2.9 +
    2.10 +#include "pEpEngine.h"
    2.11 +
    2.12 +
    2.13 +#ifdef __cplusplus
    2.14 +extern "C" {
    2.15 +#endif
    2.16 +
    2.17 +
    2.18 +struct Distribution;
    2.19 +
    2.20 +// decode_Distribution_message() - decode PER encoded Distribution message
    2.21 +//
    2.22 +//  parameters:
    2.23 +//      data (in)               PER encoded data
    2.24 +//      size (in)               size of PER encoded data
    2.25 +//      msg (out)               Distribution message
    2.26 +//
    2.27 +//  caveat:
    2.28 +//      msg goes into the ownership of the caller
    2.29 +
    2.30 +DYNAMIC_API PEP_STATUS decode_Distribution_message(
    2.31 +        const char *data,
    2.32 +        size_t size,
    2.33 +        struct Distribution **msg
    2.34 +    );
    2.35 +
    2.36 +
    2.37 +// encode_Distribution_message() - encode Distribution message into PER encoded data
    2.38 +//
    2.39 +//  parameters:
    2.40 +//      msg (in)                Distribution message
    2.41 +//      data (out)              PER encoded data
    2.42 +//      size (out)              size of PER encoded data
    2.43 +//
    2.44 +//  caveat:
    2.45 +//      data goes to the ownership of the caller
    2.46 +
    2.47 +DYNAMIC_API PEP_STATUS encode_Distribution_message(
    2.48 +        struct Distribution *msg,
    2.49 +        char **data,
    2.50 +        size_t *size
    2.51 +    );
    2.52 +
    2.53 +
    2.54 +// PER_to_XER_Distribution_msg() - decode Distribution message from PER into XER
    2.55 +//
    2.56 +//  parameters:
    2.57 +//      data (in)               PER encoded data
    2.58 +//      size (in)               size of PER encoded data
    2.59 +//      text (out)              XER text of the same Distribution message
    2.60 +
    2.61 +DYNAMIC_API PEP_STATUS PER_to_XER_Distribution_msg(
    2.62 +        const char *data,
    2.63 +        size_t size,
    2.64 +        char **text
    2.65 +    );
    2.66 +
    2.67 +
    2.68 +// XER_to_PER_Distribution_msg() - encode Distribution message from XER into PER
    2.69 +//
    2.70 +//  parameters:
    2.71 +//      text (in)               string with XER text of the Distribution message
    2.72 +//      data (out)              PER encoded data
    2.73 +//      size (out)              size of PER encoded data
    2.74 +
    2.75 +DYNAMIC_API PEP_STATUS XER_to_PER_Distribution_msg(
    2.76 +        const char *text,
    2.77 +        char **data,
    2.78 +        size_t *size
    2.79 +    );
    2.80 +
    2.81 +
    2.82 +#ifdef __cplusplus
    2.83 +}
    2.84 +#endif
    2.85 +
     3.1 --- a/src/key_reset.c	Wed Dec 11 15:55:48 2019 +0100
     3.2 +++ b/src/key_reset.c	Wed Dec 11 16:21:45 2019 +0100
     3.3 @@ -5,7 +5,9 @@
     3.4  #include "dynamic_api.h"
     3.5  #include "message_api.h"
     3.6  #include "key_reset.h"
     3.7 -
     3.8 +#include "distribution_codec.h"
     3.9 +#include "map_asn1.h"
    3.10 +#include "../asn.1/Distribution.h"
    3.11  
    3.12  #include <string.h>
    3.13  #include <stdlib.h>
    3.14 @@ -707,21 +709,45 @@
    3.15      return PEP_STATUS_OK;
    3.16  }
    3.17  
    3.18 -PEP_STATUS key_reset_commands_to_binary(const keyreset_command_list *kcl, char **cmds)
    3.19 +PEP_STATUS key_reset_commands_to_PER(const keyreset_command_list *command_list, char **cmds)
    3.20  {
    3.21 -    assert(kcl && cmds);
    3.22 -    if (!(kcl && cmds))
    3.23 +    PEP_STATUS status = PEP_STATUS_OK;
    3.24 +
    3.25 +    assert(command_list && cmds);
    3.26 +    if (!(command_list && cmds))
    3.27 +        return PEP_ILLEGAL_VALUE;
    3.28 +
    3.29 +    for (const keyreset_command_list *cl = command_list; cl && cl->command; cl = cl->next) {
    3.30 +        Commands_t *c = calloc(1, sizeof(Commands_t));
    3.31 +        assert(c);
    3.32 +        if (!c)
    3.33 +            goto enomem;
    3.34 +
    3.35 +        if (!Identity_from_Struct(cl->command->ident, &c->ident)) {
    3.36 +            free(c);
    3.37 +            goto enomem;
    3.38 +        }
    3.39 +
    3.40 +        if (OCTET_STRING_fromString(&c->newkey, cl->command->new_key)) {
    3.41 +            ASN_STRUCT_FREE(asn_DEF_Identity, &c->ident);
    3.42 +            free(c);
    3.43 +            goto enomem;
    3.44 +        }
    3.45 +    }
    3.46 +
    3.47 +enomem:
    3.48 +    status = PEP_OUT_OF_MEMORY;
    3.49 +
    3.50 +the_end:
    3.51 +    return status;
    3.52 +}
    3.53 +
    3.54 +PEP_STATUS PER_to_key_reset_commands(const char **cmds, keyreset_command_list **command_list)
    3.55 +{
    3.56 +    assert(command_list && cmds);
    3.57 +    if (!(command_list && cmds))
    3.58          return PEP_ILLEGAL_VALUE;
    3.59  
    3.60      return PEP_STATUS_OK;
    3.61  }
    3.62  
    3.63 -PEP_STATUS binary_to_key_reset_commands(const char **cmds, keyreset_command_list **kcl)
    3.64 -{
    3.65 -    assert(kcl && cmds);
    3.66 -    if (!(kcl && cmds))
    3.67 -        return PEP_ILLEGAL_VALUE;
    3.68 -
    3.69 -    return PEP_STATUS_OK;
    3.70 -}
    3.71 -
     4.1 --- a/src/key_reset.h	Wed Dec 11 15:55:48 2019 +0100
     4.2 +++ b/src/key_reset.h	Wed Dec 11 16:21:45 2019 +0100
     4.3 @@ -165,8 +165,8 @@
     4.4                                       const char* old_fpr, 
     4.5                                       const char* new_fpr);
     4.6   
     4.7 -PEP_STATUS key_reset_commands_to_binary(const keyreset_command_list *kcl, char **cmds);
     4.8 -PEP_STATUS binary_to_key_reset_commands(const char **cmds, keyreset_command_list **kcl);
     4.9 +PEP_STATUS key_reset_commands_to_PER(const keyreset_command_list *command_list, char **cmds);
    4.10 +PEP_STATUS PER_to_key_reset_commands(const char **cmds, keyreset_command_list **command_list);
    4.11  
    4.12  #ifdef __cplusplus
    4.13  }