diff --git a/Makefile b/Makefile index d713494b..0741b873 100644 --- a/Makefile +++ b/Makefile @@ -19,7 +19,7 @@ test: all $(MAKE) -C test test package: clean - cd .. ; COPYFILE_DISABLE=true tar cjf pEpEngine.tar.bz2 pEpEngine + cd .. ; COPYFILE_DISABLE=true tar cjf pEpEngine.tar.bz2 pEpEngine windist: ifneq ($(BUILD_FOR),Windoze) diff --git a/Makefile.conf b/Makefile.conf index 07605422..20f53c36 100644 --- a/Makefile.conf +++ b/Makefile.conf @@ -1,7 +1,7 @@ BUILD_ON=$(shell uname) BUILD_FOR=$(BUILD_ON) -OPTIMIZE=-g -O0 -#OPTIMIZE=-O3 -DNDEBUG +#OPTIMIZE=-g -O0 +OPTIMIZE=-O3 -DNDEBUG # the next two lines are ignored on Windoze SYSTEM_DB=/usr/local/share/pEp/system.db PREFIX=$(HOME) diff --git a/README.txt b/README.txt index 90435add..989cda12 100644 --- a/README.txt +++ b/README.txt @@ -7,6 +7,8 @@ pEp Engine The pEp Engine encapsulates all real functionality of pEp. It has an old style Makefile for building it. +The build is configured in Makefile.conf + It supports the common targets $ make all diff --git a/src/message_api.c b/src/message_api.c new file mode 100644 index 00000000..307ad898 --- /dev/null +++ b/src/message_api.c @@ -0,0 +1,106 @@ +#include "message_api.h" +#include "keymanagement.h" + +#include +#include +#include + +PEP_STATUS encrypt_message( + PEP_SESSION session, + const message *src, + stringlist_t * extra, + message **dst + ) +{ + PEP_STATUS status = PEP_STATUS_OK; + + assert(session); + assert(src); + assert(dst); + *dst = NULL; + + message *msg = new_message(src->dir, src->from, src->to, NULL); + if (msg == NULL) + return PEP_OUT_OF_MEMORY; + + src->from->me = true; + + status = myself(session, src->from); + if (status != PEP_STATUS_OK) { + free_message(msg); + return status; + } + + stringlist_t * keys = new_stringlist(src->from->fpr); + if (keys == NULL) { + free_message(msg); + return PEP_OUT_OF_MEMORY; + } + + stringlist_t *_x; + for (_x = extra; _x && _x->value; _x = _x->next) { + if (stringlist_add(keys, _x->value) == NULL) { + free_message(msg); + free_stringlist(keys); + return PEP_OUT_OF_MEMORY; + } + } + + identity_list * _il; + for (_il = src->to; _il && _il->ident; _il = _il->next) { + status = update_identity(session, _il->ident); + if (status != PEP_STATUS_OK) { + free_message(msg); + free_stringlist(keys); + return status; + } + if (_il->ident->fpr) { + if (stringlist_add(keys, _il->ident->fpr) == NULL) { + free_message(msg); + free_stringlist(keys); + return PEP_OUT_OF_MEMORY; + } + } + else + status = PEP_KEY_NOT_FOUND; + } + + int _own_keys = 1; + if (extra) + _own_keys += stringlist_length(extra); + + if (stringlist_length(keys) > _own_keys) { + char *ptext = NULL; + char *ctext = NULL; + size_t csize = 0; + + // TODO: set ptext to MIME text + + status = encrypt_and_sign(session, keys, ptext, strlen(ptext), &ctext, &csize); + if (ctext) { + msg->longmsg = ctext; + msg->longmsg_size = csize; + *dst = msg; + } + else + free_message(msg); + free(ptext); + } + else + free_message(msg); + + free_stringlist(keys); + return status; +} + +PEP_STATUS decrypt_message( + PEP_SESSION session, + const message *src, + message **dst + ) +{ + PEP_STATUS status = PEP_STATUS_OK; + + return status; +} + diff --git a/src/message_api.h b/src/message_api.h new file mode 100644 index 00000000..c4752b41 --- /dev/null +++ b/src/message_api.h @@ -0,0 +1,23 @@ +#ifdef __cplusplus +extern "C" { +#endif + +#include "transport.h" + +PEP_STATUS encrypt_message( + PEP_SESSION session, + const message *src, + stringlist_t *extra, + message **dst + ); + +PEP_STATUS decrypt_message( + PEP_SESSION session, + const message *src, + message **dst + ); + +#ifdef __cplusplus +} +#endif + diff --git a/src/pEpEngine.c b/src/pEpEngine.c index fc1b4a84..5f0e7990 100644 --- a/src/pEpEngine.c +++ b/src/pEpEngine.c @@ -534,6 +534,24 @@ pEp_identity *new_identity( return result; } +pEp_identity *identity_dup(const pEp_identity *src) +{ + assert(src); + + pEp_identity *dup = new_identity(src->address, src->fpr, src->user_id, src->username); + assert(dup); + if (dup == NULL) + return NULL; + + dup->comm_type = src->comm_type; + dup->lang[0] = src->lang[0]; + dup->lang[1] = src->lang[1]; + dup->lang[2] = 0; + dup->me = src->me; + + return dup; +} + void free_identity(pEp_identity *identity) { if (identity) { diff --git a/src/pEpEngine.h b/src/pEpEngine.h index dd8c8de7..390e1280 100644 --- a/src/pEpEngine.h +++ b/src/pEpEngine.h @@ -415,6 +415,21 @@ DYNAMIC_API pEp_identity *new_identity( ); +// identity_dup() - allocate memory and set the string and size fields +// +// parameters: +// src (in) identity to duplicate +// +// return value: +// pEp_identity struct with correct size values or NULL if out of memory +// +// caveat: +// the strings are copied; the original strings are still being owned by +// the caller + +DYNAMIC_API pEp_identity *identity_dup(const pEp_identity *src); + + // free_identity() - free all memory being occupied by a pEp_identity struct // // parameters: diff --git a/src/transport.c b/src/transport.c index b8c53a14..83b938da 100644 --- a/src/transport.c +++ b/src/transport.c @@ -24,28 +24,6 @@ void release_transport_system(PEP_SESSION session) // nothing yet } -pEp_identity *identity_dup(const pEp_identity *src) -{ - assert(src); - - pEp_identity *dup = new_identity(src->address, src->fpr, src->user_id, src->username); - assert(dup); - if (dup == NULL) - return NULL; - - dup->address_size = strlen(dup->address); - dup->fpr_size = strlen(dup->fpr); - dup->user_id_size = strlen(dup->user_id); - dup->username_size = strlen(dup->username); - dup->comm_type = src->comm_type; - dup->lang[0] = src->lang[0]; - dup->lang[1] = src->lang[1]; - dup->lang[2] = 0; - dup->me = src->me; - - return dup; -} - identity_list *new_identity_list(const pEp_identity *ident) { identity_list *id_list = calloc(1, sizeof(identity_list)); @@ -65,6 +43,26 @@ identity_list *new_identity_list(const pEp_identity *ident) return id_list; } +identity_list *identity_list_dup(const identity_list *src) +{ + assert(src); + + identity_list *id_list = new_identity_list(src->ident); + assert(id_list); + if (id_list == NULL) + return NULL; + + if (src->next) { + id_list->next = identity_list_dup(src->next); + if (id_list->next == NULL) { + free_identity_list(id_list); + return NULL; + } + } + + return id_list; +} + void free_identity_list(identity_list *id_list) { if (id_list) { @@ -100,7 +98,7 @@ identity_list *identity_list_add(identity_list *id_list, const pEp_identity *ide message *new_message( msg_direction dir, const pEp_identity *from, - const pEp_identity *to, + const identity_list *to, const char *shortmsg ) { @@ -109,13 +107,15 @@ message *new_message( if (msg == NULL) return NULL; - msg->shortmsg = strdup(shortmsg); - assert(msg->shortmsg); - if (msg->shortmsg == NULL) { - free(msg); - return NULL; + if (msg->shortmsg) { + msg->shortmsg = strdup(shortmsg); + assert(msg->shortmsg); + if (msg->shortmsg == NULL) { + free(msg); + return NULL; + } + msg->shortmsg_size = strlen(msg->shortmsg); } - msg->shortmsg_size = strlen(msg->shortmsg); msg->dir = dir; @@ -126,16 +126,7 @@ message *new_message( return NULL; } - if (dir == dir_incoming) { - msg->recv_by = identity_dup(to); - assert(msg->recv_by); - if (msg->recv_by == NULL) { - free_message(msg); - return NULL; - } - } - - msg->to = new_identity_list(to); + msg->to = identity_list_dup(to); assert(msg->to); if (msg->to == NULL) { free_message(msg); diff --git a/src/transport.h b/src/transport.h index a69d87bd..07aeb6fc 100644 --- a/src/transport.h +++ b/src/transport.h @@ -4,9 +4,12 @@ #include #include +// all functions are using POSIX struct tm + typedef struct tm timestamp; typedef enum _PEP_transports { + // auto transport chooses transport per message automatically PEP_trans_auto = 0, // PEP_trans_email, // PEP_trans_whatsapp, @@ -16,14 +19,13 @@ typedef enum _PEP_transports { typedef struct _PEP_transport_t PEP_transport_t; -pEp_identity *identity_dup(const pEp_identity *src); - typedef struct _identity_list { pEp_identity *ident; struct _identity_list *next; } identity_list; identity_list *new_identity_list(const pEp_identity *ident); +identity_list *identity_list_dup(const identity_list *src); void free_identity_list(identity_list *id_list); identity_list *identity_list_add(identity_list *id_list, const pEp_identity *ident); @@ -73,7 +75,7 @@ typedef struct _message_ref_list { message *new_message( msg_direction dir, const pEp_identity *from, - const pEp_identity *to, + const identity_list *to, const char *shortmsg ); @@ -98,3 +100,4 @@ typedef uint64_t transports_mask; PEP_STATUS init_transport_system(PEP_SESSION session); void release_transport_system(PEP_SESSION session); +