encrypt in pieces
authorvb
Mon, 29 Dec 2014 20:48:30 +0100
changeset 38271bea5552dc
parent 37 7928e0e6eee9
child 39 66b5cc6cb987
encrypt in pieces
Makefile.conf
src/Makefile
src/message_api.c
src/message_api.h
src/transport.c
src/transport.h
     1.1 --- a/Makefile.conf	Sun Dec 28 23:07:55 2014 +0100
     1.2 +++ b/Makefile.conf	Mon Dec 29 20:48:30 2014 +0100
     1.3 @@ -1,7 +1,7 @@
     1.4  BUILD_ON=$(shell uname)
     1.5  BUILD_FOR=$(BUILD_ON)
     1.6 -#OPTIMIZE=-g -O0
     1.7 -OPTIMIZE=-O3 -DNDEBUG
     1.8 +OPTIMIZE=-g -O0
     1.9 +#OPTIMIZE=-O3 -DNDEBUG
    1.10  # the next two lines are ignored on Windoze
    1.11  SYSTEM_DB=/usr/local/share/pEp/system.db
    1.12  PREFIX=$(HOME)
     2.1 --- a/src/Makefile	Sun Dec 28 23:07:55 2014 +0100
     2.2 +++ b/src/Makefile	Mon Dec 29 20:48:30 2014 +0100
     2.3 @@ -10,7 +10,8 @@
     2.4  CC=gcc -std=c99
     2.5  CFLAGS=-I$(GPGME_IN)/include -I/opt/local/include $(OPTIMIZE) -pedantic \
     2.6  	-DSYSTEM_DB=\"$(SYSTEM_DB)\" -DLIBGPGME=\"$(LIBGPGME)\"  -DSQLITE_THREADSAFE=1
     2.7 -LDFLAGS=-lc -macosx_version_min $(MACOSX_VERSION_MIN) -dylib -arch x86_64
     2.8 +LDFLAGS=-lc -macosx_version_min $(MACOSX_VERSION_MIN) -dylib -arch x86_64 \
     2.9 +	-L/opt/local/lib -letpan
    2.10  
    2.11  else ifeq ($(BUILD_FOR),Windoze)
    2.12  
     3.1 --- a/src/message_api.c	Sun Dec 28 23:07:55 2014 +0100
     3.2 +++ b/src/message_api.c	Mon Dec 29 20:48:30 2014 +0100
     3.3 @@ -9,29 +9,32 @@
     3.4          PEP_SESSION session,
     3.5          const message *src,
     3.6          stringlist_t * extra,
     3.7 -        message **dst
     3.8 +        message **dst,
     3.9 +        PEP_enc_format format
    3.10      )
    3.11  {
    3.12      PEP_STATUS status = PEP_STATUS_OK;
    3.13  
    3.14      assert(session);
    3.15      assert(src);
    3.16 +    assert(src->shortmsg || src->longmsg);
    3.17      assert(dst);
    3.18      *dst = NULL;
    3.19 +    assert(format != PEP_enc_none);
    3.20  
    3.21      message *msg = new_message(src->dir, src->from, src->to, NULL);
    3.22      if (msg == NULL)
    3.23          return PEP_OUT_OF_MEMORY;
    3.24  
    3.25 -    src->from->me = true;
    3.26 +    msg->from->me = true;
    3.27  
    3.28 -    status = myself(session, src->from);
    3.29 +    status = myself(session, msg->from);
    3.30      if (status != PEP_STATUS_OK) {
    3.31          free_message(msg);
    3.32          return status;
    3.33      }
    3.34  
    3.35 -    stringlist_t * keys = new_stringlist(src->from->fpr);
    3.36 +    stringlist_t * keys = new_stringlist(msg->from->fpr);
    3.37      if (keys == NULL) {
    3.38          free_message(msg);
    3.39          return PEP_OUT_OF_MEMORY;
    3.40 @@ -47,7 +50,7 @@
    3.41      }
    3.42      
    3.43      identity_list * _il;
    3.44 -    for (_il = src->to; _il && _il->ident; _il = _il->next) {
    3.45 +    for (_il = msg->to; _il && _il->ident; _il = _il->next) {
    3.46          status = update_identity(session, _il->ident);
    3.47          if (status != PEP_STATUS_OK) {
    3.48              free_message(msg);
    3.49 @@ -70,21 +73,103 @@
    3.50          _own_keys += stringlist_length(extra);
    3.51      
    3.52      if (stringlist_length(keys) > _own_keys) {
    3.53 -        char *ptext = NULL;
    3.54 +        char *ptext;
    3.55          char *ctext = NULL;
    3.56          size_t csize = 0;
    3.57  
    3.58 -        // TODO: set ptext to MIME text
    3.59 +        switch (format) {
    3.60 +        case PEP_enc_MIME_multipart:
    3.61 +            break;
    3.62  
    3.63 -        status = encrypt_and_sign(session, keys, ptext, strlen(ptext), &ctext, &csize);
    3.64 -        if (ctext) {
    3.65 -            msg->longmsg = ctext;
    3.66 -            msg->longmsg_size = csize;
    3.67 -            *dst = msg;
    3.68 +        case PEP_enc_pieces:
    3.69 +            if (src->shortmsg && src->longmsg) {
    3.70 +                ptext = calloc(1, strlen(src->shortmsg) + strlen(src->longmsg) + 12);
    3.71 +                if (ptext == NULL) {
    3.72 +                    free_message(msg);
    3.73 +                    free_stringlist(keys);
    3.74 +                    return PEP_OUT_OF_MEMORY;
    3.75 +                }
    3.76 +                strcpy(ptext, "subject: ");
    3.77 +                strcat(ptext, src->shortmsg);
    3.78 +                strcat(ptext, "\n\n");
    3.79 +                strcat(ptext, src->longmsg);
    3.80 +                status = encrypt_and_sign(session, keys, ptext, strlen(ptext), &ctext, &csize);
    3.81 +                if (ctext) {
    3.82 +                    msg->longmsg = ctext;
    3.83 +                    msg->longmsg_size = csize;
    3.84 +                    msg->shortmsg = strdup("pEp");
    3.85 +                }
    3.86 +                else {
    3.87 +                    free_message(msg);
    3.88 +                    msg = NULL;
    3.89 +                }
    3.90 +            }
    3.91 +            else if (src->shortmsg) {
    3.92 +                ptext = src->shortmsg;
    3.93 +                status = encrypt_and_sign(session, keys, ptext, strlen(ptext), &ctext, &csize);
    3.94 +                if (ctext) {
    3.95 +                    msg->shortmsg = ctext;
    3.96 +                    msg->shortmsg_size = csize;
    3.97 +                }
    3.98 +                else {
    3.99 +                    free_message(msg);
   3.100 +                    msg = NULL;
   3.101 +                }
   3.102 +            }
   3.103 +            else if (src->longmsg) {
   3.104 +                ptext = src->longmsg;
   3.105 +                status = encrypt_and_sign(session, keys, ptext, strlen(ptext), &ctext, &csize);
   3.106 +                if (ctext) {
   3.107 +                    msg->longmsg = ctext;
   3.108 +                    msg->longmsg_size = csize;
   3.109 +                    msg->shortmsg = strdup("pEp");
   3.110 +                }
   3.111 +                else {
   3.112 +                    free_message(msg);
   3.113 +                    msg = NULL;
   3.114 +                }
   3.115 +            }
   3.116 +            if (msg && msg->longmsg_formatted) {
   3.117 +                ptext = src->longmsg_formatted;
   3.118 +                status = encrypt_and_sign(session, keys, ptext, strlen(ptext), &ctext, &csize);
   3.119 +                if (ctext) {
   3.120 +                    msg->longmsg_formatted = ctext;
   3.121 +                    msg->longmsg_formatted_size = csize;
   3.122 +                }
   3.123 +                else {
   3.124 +                    free_message(msg);
   3.125 +                    msg = NULL;
   3.126 +                }
   3.127 +            }
   3.128 +            if (msg) {
   3.129 +                bloblist_t *_s;
   3.130 +                bloblist_t *_d = new_bloblist(NULL, 0);
   3.131 +                if (_d == NULL) {
   3.132 +                    free_message(msg);
   3.133 +                    free_stringlist(keys);
   3.134 +                    return PEP_OUT_OF_MEMORY;
   3.135 +                }
   3.136 +                msg->attachments = _d;
   3.137 +                for (_s = src->attachments; _s && _s->data_ref; _s = _s->next) {
   3.138 +                    int psize = _s->size;
   3.139 +                    ptext = _s->data_ref;
   3.140 +                    status = encrypt_and_sign(session, keys, ptext, psize, &ctext, &csize);
   3.141 +                    if (ctext) {
   3.142 +                        _d = bloblist_add(_d, ctext, csize);
   3.143 +                    }
   3.144 +                    else {
   3.145 +                        free_message(msg);
   3.146 +                        msg = NULL;
   3.147 +                        break;
   3.148 +                    }
   3.149 +                }
   3.150 +                *dst = msg;
   3.151 +            }
   3.152 +            break;
   3.153 +
   3.154 +        default:
   3.155 +            assert(0);
   3.156          }
   3.157 -        else
   3.158 -            free_message(msg);
   3.159 -        free(ptext);
   3.160      }
   3.161      else
   3.162          free_message(msg);
     4.1 --- a/src/message_api.h	Sun Dec 28 23:07:55 2014 +0100
     4.2 +++ b/src/message_api.h	Mon Dec 29 20:48:30 2014 +0100
     4.3 @@ -4,11 +4,18 @@
     4.4  
     4.5  #include "transport.h"
     4.6  
     4.7 +typedef enum _PEP_enc_format {
     4.8 +    PEP_enc_none = 0,
     4.9 +    PEP_enc_MIME_multipart,
    4.10 +    PEP_enc_pieces
    4.11 +} PEP_enc_format;
    4.12 +
    4.13  PEP_STATUS encrypt_message(
    4.14          PEP_SESSION session,
    4.15          const message *src,
    4.16          stringlist_t *extra,
    4.17 -        message **dst
    4.18 +        message **dst,
    4.19 +        PEP_enc_format format
    4.20      );
    4.21  
    4.22  PEP_STATUS decrypt_message(
     5.1 --- a/src/transport.c	Sun Dec 28 23:07:55 2014 +0100
     5.2 +++ b/src/transport.c	Mon Dec 29 20:48:30 2014 +0100
     5.3 @@ -74,9 +74,11 @@
     5.4  
     5.5  identity_list *identity_list_add(identity_list *id_list, const pEp_identity *ident)
     5.6  {
     5.7 -    assert(id_list);
     5.8      assert(ident);
     5.9  
    5.10 +    if (id_list == NULL)
    5.11 +        return new_identity_list(ident);
    5.12 +
    5.13      if (id_list->ident == NULL) {
    5.14          id_list->ident = identity_dup(ident);
    5.15          assert(id_list->ident);
    5.16 @@ -95,8 +97,61 @@
    5.17      }
    5.18  }
    5.19  
    5.20 +bloblist_t *new_bloblist(char *blob, size_t size)
    5.21 +{
    5.22 +    bloblist_t * bloblist = calloc(1, sizeof(bloblist_t));
    5.23 +    if (bloblist == NULL)
    5.24 +        return NULL;
    5.25 +    bloblist->data_ref = blob;
    5.26 +    bloblist->size = size;
    5.27 +    return bloblist;
    5.28 +}
    5.29 +
    5.30 +bloblist_t *bloblist_dup(const bloblist_t *src)
    5.31 +{
    5.32 +    assert(src);
    5.33 +
    5.34 +    if (src) {
    5.35 +        bloblist_t * dst = new_bloblist(src->data_ref, src->size);
    5.36 +        if (dst == NULL)
    5.37 +            return NULL;
    5.38 +        dst->next = bloblist_dup(src->next);
    5.39 +        return dst;
    5.40 +    }
    5.41 +    else
    5.42 +        return NULL;
    5.43 +}
    5.44 +
    5.45 +void free_bloblist(bloblist_t *bloblist)
    5.46 +{
    5.47 +    if (bloblist && bloblist->next)
    5.48 +        free_bloblist(bloblist->next);
    5.49 +    free(bloblist);
    5.50 +}
    5.51 +
    5.52 +bloblist_t *bloblist_add(bloblist_t *bloblist, char *blob, size_t size)
    5.53 +{
    5.54 +    assert(blob);
    5.55 +
    5.56 +    if (bloblist == NULL)
    5.57 +        return new_bloblist(blob, size);
    5.58 +
    5.59 +    if (bloblist->data_ref == NULL) {
    5.60 +        bloblist->data_ref = blob;
    5.61 +        bloblist->size = size;
    5.62 +        return bloblist;
    5.63 +    }
    5.64 +
    5.65 +    if (bloblist->next == NULL) {
    5.66 +        bloblist->next = new_bloblist(blob, size);
    5.67 +        return bloblist->next;
    5.68 +    }
    5.69 +
    5.70 +    return bloblist_add(bloblist->next, blob, size);
    5.71 +}
    5.72 +
    5.73  message *new_message(
    5.74 -        msg_direction dir,
    5.75 +        PEP_msg_direction dir,
    5.76          const pEp_identity *from,
    5.77          const identity_list *to,
    5.78          const char *shortmsg
    5.79 @@ -142,6 +197,7 @@
    5.80      free(msg->shortmsg);
    5.81      free(msg->longmsg);
    5.82      free(msg->longmsg_formatted);
    5.83 +    free_bloblist(msg->attachments);
    5.84      free(msg->rawmsg);
    5.85      free_identity_list(msg->to);
    5.86      free_identity_list(msg->cc);
    5.87 @@ -173,9 +229,11 @@
    5.88  
    5.89  message_ref_list *message_ref_list_add(message_ref_list *msg_list, message *msg)
    5.90  {
    5.91 -    assert(msg_list);
    5.92      assert(msg);
    5.93  
    5.94 +    if (msg_list == NULL)
    5.95 +        return new_message_ref_list(msg);
    5.96 +
    5.97      if (msg_list->msg_ref == NULL) {
    5.98          msg_list->msg_ref = msg;
    5.99          return msg_list;
     6.1 --- a/src/transport.h	Sun Dec 28 23:07:55 2014 +0100
     6.2 +++ b/src/transport.h	Mon Dec 29 20:48:30 2014 +0100
     6.3 @@ -29,20 +29,31 @@
     6.4  void free_identity_list(identity_list *id_list);
     6.5  identity_list *identity_list_add(identity_list *id_list, const pEp_identity *ident);
     6.6  
     6.7 -typedef enum _msg_format {
     6.8 -    format_plain = 0,
     6.9 -    format_html
    6.10 -} msg_format;
    6.11 +typedef enum _PEP_msg_format {
    6.12 +    PEP_format_plain = 0,
    6.13 +    PEP_format_html
    6.14 +} PEP_msg_format;
    6.15  
    6.16 -typedef enum _msg_direction {
    6.17 -    dir_incoming = 0,
    6.18 -    dir_outgoing
    6.19 -} msg_direction;
    6.20 +typedef enum _PEP_msg_direction {
    6.21 +    PEP_dir_incoming = 0,
    6.22 +    PEP_dir_outgoing
    6.23 +} PEP_msg_direction;
    6.24 +
    6.25 +typedef struct _bloblist_t {
    6.26 +    char *data_ref;
    6.27 +    size_t size;
    6.28 +    struct _bloblist_t *next;
    6.29 +} bloblist_t;
    6.30 +
    6.31 +bloblist_t *new_bloblist(char *blob, size_t size);
    6.32 +bloblist_t *bloblist_dup(const bloblist_t *src);
    6.33 +void free_bloblist(bloblist_t *bloblist);
    6.34 +bloblist_t *bloblist_add(bloblist_t *bloblist, char *blob, size_t size);
    6.35  
    6.36  struct _message_ref_list;
    6.37  
    6.38  typedef struct _message {
    6.39 -    msg_direction dir;
    6.40 +    PEP_msg_direction dir;
    6.41      char * id;
    6.42      size_t id_size;
    6.43      char * shortmsg;
    6.44 @@ -51,7 +62,8 @@
    6.45      size_t longmsg_size;
    6.46      char * longmsg_formatted;
    6.47      size_t longmsg_formatted_size;
    6.48 -    msg_format format;
    6.49 +    PEP_msg_format format;
    6.50 +    bloblist_t * attachments;
    6.51      char * rawmsg;
    6.52      size_t rawmsg_size;
    6.53      timestamp sent;
    6.54 @@ -65,6 +77,7 @@
    6.55      size_t refering_id_size;
    6.56      struct _message *refering_msg;
    6.57      struct _message_ref_list *refered_by;
    6.58 +    bool encrypted;
    6.59  } message;
    6.60  
    6.61  typedef struct _message_ref_list {
    6.62 @@ -73,7 +86,7 @@
    6.63  } message_ref_list;
    6.64  
    6.65  message *new_message(
    6.66 -        msg_direction dir,
    6.67 +        PEP_msg_direction dir,
    6.68          const pEp_identity *from,
    6.69          const identity_list *to,
    6.70          const char *shortmsg
    6.71 @@ -93,7 +106,7 @@
    6.72      sendto_t sendto;
    6.73      readnext_t readnext;
    6.74      bool long_message_supported;
    6.75 -    msg_format native_format;
    6.76 +    PEP_msg_format native_format;
    6.77  };
    6.78  
    6.79  typedef uint64_t transports_mask;