diff --git a/src/cryptotech.c b/src/cryptotech.c index 4a3783e3..4104ca8e 100644 --- a/src/cryptotech.c +++ b/src/cryptotech.c @@ -10,9 +10,10 @@ #include #include +PEP_cryptotech_t cryptotech[PEP_crypt__count]; + PEP_STATUS init_cryptotech(PEP_SESSION session, bool in_first) { - static PEP_cryptotech_t cryptotech[PEP_crypt__count]; PEP_STATUS status = PEP_STATUS_OK; assert(PEP_crypt__count == 2); diff --git a/src/cryptotech.h b/src/cryptotech.h index c3b88a65..01049a56 100644 --- a/src/cryptotech.h +++ b/src/cryptotech.h @@ -84,6 +84,8 @@ typedef struct _PEP_cryptotech_t { key_expired_t key_expired; } PEP_cryptotech_t; +extern PEP_cryptotech_t cryptotech[PEP_crypt__count]; + typedef uint64_t cryptotech_mask; PEP_STATUS init_cryptotech(PEP_SESSION session, bool in_first); diff --git a/src/message_api.c b/src/message_api.c index 67ae7b8b..5bb5c29f 100644 --- a/src/message_api.c +++ b/src/message_api.c @@ -363,37 +363,10 @@ DYNAMIC_API PEP_STATUS encrypt_message( if (!(session && src && dst && (enc_format >= PEP_enc_pieces))) return PEP_ILLEGAL_VALUE; - *dst = NULL; - - determine_encryption_format(src); import_attached_keys(session, src); + determine_encryption_format(src); - if (src->enc_format >= PEP_enc_pieces) { - if (src->enc_format == enc_format) { - assert(0); // the message is encrypted this way already - msg = message_dup(src); - if (msg == NULL) - goto enomem; - *dst = msg; - return PEP_STATUS_OK; - } - else { - // decrypt and re-encrypt again - message * _dst = NULL; - stringlist_t *_keylist = NULL; - PEP_MIME_format mime = (enc_format == PEP_enc_PEP) ? PEP_MIME : - PEP_MIME_fields_omitted; - - PEP_color color; - status = decrypt_message(session, src, mime, &_dst, &_keylist, &color); - if (status != PEP_STATUS_OK) - goto pep_error; - free_stringlist(_keylist); - - src = _dst; - free_src = true; - } - } + *dst = NULL; status = myself(session, src->from); if (status != PEP_STATUS_OK) @@ -792,13 +765,13 @@ DYNAMIC_API PEP_STATUS decrypt_message( if (!(session && src && dst && keylist && color)) return PEP_ILLEGAL_VALUE; + import_attached_keys(session, src); + PEP_cryptotech crypto = determine_encryption_format(src); + *dst = NULL; *keylist = NULL; *color = PEP_rating_undefined; - determine_encryption_format(src); - import_attached_keys(session, src); - if (src->mime == PEP_MIME_fields_omitted || src->mime == PEP_MIME) { message *_src = NULL; status = mime_decode_message(src->longmsg, &_src); @@ -820,26 +793,40 @@ DYNAMIC_API PEP_STATUS decrypt_message( // src message is not MIME encoded (any more) assert(src->mime == PEP_MIME_none); - if (!is_PGP_message_text(src->longmsg)) { - status = PEP_UNENCRYPTED; - } - else { + if (crypto) { ctext = src->longmsg; csize = strlen(src->longmsg); - status = decrypt_and_verify(session, ctext, csize, &ptext, &psize, - &_keylist); + status = cryptotech[crypto].decrypt_and_verify(session, ctext, csize, + &ptext, &psize, &_keylist); if (status > PEP_CANNOT_DECRYPT_UNKNOWN) goto pep_error; } + else { + status = PEP_UNENCRYPTED; + } *color = decrypt_color(status); - if (*color != PEP_rating_under_attack && _keylist) { - PEP_color _color = keylist_color(session, _keylist); - if (_color == PEP_rating_under_attack) + + if (*color != PEP_rating_under_attack) { + PEP_color kl_color = PEP_rating_undefined; + + if (_keylist) + kl_color = keylist_color(session, _keylist); + + if (kl_color == PEP_rating_under_attack) *color = PEP_rating_under_attack; + + else if (*color == PEP_rating_reliable && + kl_color >= PEP_rating_trusted) + *color = kl_color; + + else if (*color == PEP_rating_reliable && + kl_color < PEP_rating_reliable) + *color = PEP_rating_unreliable; + else - *color = MIN(*color, _color); + *color = MIN(*color, kl_color); } if (ptext) { diff --git a/src/message_api.h b/src/message_api.h index f79110d7..bfe0fc02 100644 --- a/src/message_api.h +++ b/src/message_api.h @@ -3,6 +3,7 @@ #include "pEpEngine.h" #include "keymanagement.h" #include "message.h" +#include "cryptotech.h" #ifdef __cplusplus extern "C" { diff --git a/src/pEpEngine.c b/src/pEpEngine.c index 5114097d..e92efb56 100644 --- a/src/pEpEngine.c +++ b/src/pEpEngine.c @@ -725,8 +725,8 @@ DYNAMIC_API PEP_STATUS get_trust(PEP_SESSION session, pEp_identity *identity) result = sqlite3_step(session->get_trust); switch (result) { case SQLITE_ROW: { - const char * user_id = (const char *) sqlite3_column_text(session->get_trust, 1); - int comm_type = (PEP_comm_type) sqlite3_column_int(session->get_trust, 2); + const char * user_id = (const char *) sqlite3_column_text(session->get_trust, 0); + int comm_type = (PEP_comm_type) sqlite3_column_int(session->get_trust, 1); if (strcmp(user_id, identity->user_id) != 0) { free(identity->user_id); @@ -755,12 +755,13 @@ DYNAMIC_API PEP_STATUS least_trust( { PEP_STATUS status = PEP_STATUS_OK; int result; - PEP_comm_type _comm_type = PEP_ct_unknown; assert(session); assert(fpr); assert(comm_type); + *comm_type = PEP_ct_unknown; + if (!(session && fpr && comm_type)) return PEP_ILLEGAL_VALUE; @@ -770,8 +771,8 @@ DYNAMIC_API PEP_STATUS least_trust( result = sqlite3_step(session->least_trust); switch (result) { case SQLITE_ROW: { - *comm_type = (PEP_comm_type) - sqlite3_column_int(session->get_identity, 1); + int _comm_type = sqlite3_column_int(session->least_trust, 0); + *comm_type = (PEP_comm_type) _comm_type; break; } default: diff --git a/src/pgp_gpg_internal.h b/src/pgp_gpg_internal.h index 16107e6d..4059dd7c 100644 --- a/src/pgp_gpg_internal.h +++ b/src/pgp_gpg_internal.h @@ -1,118 +1,119 @@ -#pragma once - -#include - -// init - -typedef const char * (*gpgme_check_version_t)(const char*); -typedef gpgme_error_t(*gpgme_set_locale_t)(gpgme_ctx_t CTX, int CATEGORY, - const char *VALUE); -typedef gpgme_error_t(*gpgme_new_t)(gpgme_ctx_t *CTX); -typedef void(*gpgme_release_t)(gpgme_ctx_t CTX); -typedef gpgme_error_t(*gpgme_set_protocol_t)(gpgme_ctx_t CTX, - gpgme_protocol_t PROTO); -typedef void(*gpgme_set_armor_t)(gpgme_ctx_t CTX, int YES); - -// data - -typedef gpgme_error_t(*gpgme_data_new_t)(gpgme_data_t *DH); -typedef gpgme_error_t(*gpgme_data_new_from_mem_t)(gpgme_data_t *DH, - const char *BUFFER, size_t SIZE, int COPY); -typedef gpgme_error_t (*gpgme_data_new_from_cbs_t)(gpgme_data_t *DH, - gpgme_data_cbs_t CBS, void *HANDLE); -typedef void(*gpgme_data_release_t)(gpgme_data_t DH); -typedef gpgme_data_type_t(*gpgme_data_identify_t)(gpgme_data_t DH); -typedef size_t(*gpgme_data_seek_t)(gpgme_data_t DH, size_t OFFSET, - int WHENCE); -typedef size_t(*gpgme_data_read_t)(gpgme_data_t DH, void *BUFFER, - size_t LENGTH); - -// encrypt and decrypt - -typedef gpgme_error_t(*gpgme_op_decrypt_t)(gpgme_ctx_t CTX, - gpgme_data_t CIPHER, gpgme_data_t PLAIN); -typedef gpgme_error_t(*gpgme_op_verify_t)(gpgme_ctx_t CTX, gpgme_data_t SIG, - gpgme_data_t SIGNED_TEXT, gpgme_data_t PLAIN); -typedef gpgme_error_t(*gpgme_op_decrypt_verify_t)(gpgme_ctx_t CTX, - gpgme_data_t CIPHER, gpgme_data_t PLAIN); -typedef gpgme_decrypt_result_t(*gpgme_op_decrypt_result_t)(gpgme_ctx_t CTX); -typedef gpgme_error_t(*gpgme_op_encrypt_sign_t)(gpgme_ctx_t CTX, - gpgme_key_t RECP[], gpgme_encrypt_flags_t FLAGS, gpgme_data_t PLAIN, - gpgme_data_t CIPHER); -typedef gpgme_verify_result_t(*gpgme_op_verify_result_t)(gpgme_ctx_t CTX); -typedef void(*gpgme_signers_clear_t)(gpgme_ctx_t CTX); -typedef gpgme_error_t(*gpgme_signers_add_t)(gpgme_ctx_t CTX, const gpgme_key_t KEY); - -// keys - -typedef gpgme_error_t(*gpgme_get_key_t)(gpgme_ctx_t CTX, const char *FPR, - gpgme_key_t *R_KEY, int SECRET); -typedef gpgme_error_t(*gpgme_op_genkey_t)(gpgme_ctx_t CTX, const char *PARMS, - gpgme_data_t PUBLIC, gpgme_data_t SECRET); -typedef gpgme_genkey_result_t(*gpgme_op_genkey_result_t)(gpgme_ctx_t CTX); -typedef gpgme_error_t(*gpgme_op_delete_t)(gpgme_ctx_t CTX, - const gpgme_key_t KEY, int ALLOW_SECRET); -typedef gpgme_error_t(*gpgme_op_import_t)(gpgme_ctx_t CTX, - gpgme_data_t KEYDATA); -typedef gpgme_error_t(*gpgme_op_export_t)(gpgme_ctx_t CTX, - const char *PATTERN, gpgme_export_mode_t MODE, gpgme_data_t KEYDATA); -typedef gpgme_error_t(*gpgme_set_keylist_mode_t)(gpgme_ctx_t CTX, - gpgme_keylist_mode_t MODE); -typedef gpgme_keylist_mode_t(*gpgme_get_keylist_mode_t)(gpgme_ctx_t CTX); -typedef gpgme_error_t(*gpgme_op_keylist_start_t)(gpgme_ctx_t CTX, - const char *PATTERN, int SECRET_ONLY); -typedef gpgme_error_t(*gpgme_op_keylist_next_t)(gpgme_ctx_t CTX, - gpgme_key_t *R_KEY); -typedef gpgme_error_t(*gpgme_op_keylist_end_t)(gpgme_ctx_t CTX); -typedef gpgme_error_t(*gpgme_op_import_keys_t)(gpgme_ctx_t CTX, - gpgme_key_t *KEYS); -typedef void(*gpgme_key_ref_t)(gpgme_key_t KEY); -typedef void(*gpgme_key_unref_t)(gpgme_key_t KEY); -typedef gpgme_error_t (*gpgme_op_edit_t)(gpgme_ctx_t CTX, gpgme_key_t KEY, - gpgme_edit_cb_t FNC, void *HANDLE, gpgme_data_t OUT); +#pragma once + +#include + +// init + +typedef const char * (*gpgme_check_version_t)(const char*); +typedef gpgme_error_t(*gpgme_set_locale_t)(gpgme_ctx_t CTX, int CATEGORY, + const char *VALUE); +typedef gpgme_error_t(*gpgme_new_t)(gpgme_ctx_t *CTX); +typedef void(*gpgme_release_t)(gpgme_ctx_t CTX); +typedef gpgme_error_t(*gpgme_set_protocol_t)(gpgme_ctx_t CTX, + gpgme_protocol_t PROTO); +typedef void(*gpgme_set_armor_t)(gpgme_ctx_t CTX, int YES); + +// data + +typedef gpgme_error_t(*gpgme_data_new_t)(gpgme_data_t *DH); +typedef gpgme_error_t(*gpgme_data_new_from_mem_t)(gpgme_data_t *DH, + const char *BUFFER, size_t SIZE, int COPY); +typedef gpgme_error_t (*gpgme_data_new_from_cbs_t)(gpgme_data_t *DH, + gpgme_data_cbs_t CBS, void *HANDLE); +typedef void(*gpgme_data_release_t)(gpgme_data_t DH); +typedef gpgme_data_type_t(*gpgme_data_identify_t)(gpgme_data_t DH); +typedef size_t(*gpgme_data_seek_t)(gpgme_data_t DH, size_t OFFSET, + int WHENCE); +typedef size_t(*gpgme_data_read_t)(gpgme_data_t DH, void *BUFFER, + size_t LENGTH); + +// encrypt and decrypt + +typedef gpgme_error_t(*gpgme_op_decrypt_t)(gpgme_ctx_t CTX, + gpgme_data_t CIPHER, gpgme_data_t PLAIN); +typedef gpgme_error_t(*gpgme_op_verify_t)(gpgme_ctx_t CTX, gpgme_data_t SIG, + gpgme_data_t SIGNED_TEXT, gpgme_data_t PLAIN); +typedef gpgme_error_t(*gpgme_op_decrypt_verify_t)(gpgme_ctx_t CTX, + gpgme_data_t CIPHER, gpgme_data_t PLAIN); +typedef gpgme_decrypt_result_t(*gpgme_op_decrypt_result_t)(gpgme_ctx_t CTX); +typedef gpgme_error_t(*gpgme_op_encrypt_sign_t)(gpgme_ctx_t CTX, + gpgme_key_t RECP[], gpgme_encrypt_flags_t FLAGS, gpgme_data_t PLAIN, + gpgme_data_t CIPHER); +typedef gpgme_verify_result_t(*gpgme_op_verify_result_t)(gpgme_ctx_t CTX); +typedef void(*gpgme_signers_clear_t)(gpgme_ctx_t CTX); +typedef gpgme_error_t(*gpgme_signers_add_t)(gpgme_ctx_t CTX, const gpgme_key_t KEY); + +// keys + +typedef gpgme_error_t(*gpgme_get_key_t)(gpgme_ctx_t CTX, const char *FPR, + gpgme_key_t *R_KEY, int SECRET); +typedef gpgme_error_t(*gpgme_op_genkey_t)(gpgme_ctx_t CTX, const char *PARMS, + gpgme_data_t PUBLIC, gpgme_data_t SECRET); +typedef gpgme_genkey_result_t(*gpgme_op_genkey_result_t)(gpgme_ctx_t CTX); +typedef gpgme_error_t(*gpgme_op_delete_t)(gpgme_ctx_t CTX, + const gpgme_key_t KEY, int ALLOW_SECRET); +typedef gpgme_error_t(*gpgme_op_import_t)(gpgme_ctx_t CTX, + gpgme_data_t KEYDATA); +typedef gpgme_error_t(*gpgme_op_export_t)(gpgme_ctx_t CTX, + const char *PATTERN, gpgme_export_mode_t MODE, gpgme_data_t KEYDATA); +typedef gpgme_error_t(*gpgme_set_keylist_mode_t)(gpgme_ctx_t CTX, + gpgme_keylist_mode_t MODE); +typedef gpgme_keylist_mode_t(*gpgme_get_keylist_mode_t)(gpgme_ctx_t CTX); +typedef gpgme_error_t(*gpgme_op_keylist_start_t)(gpgme_ctx_t CTX, + const char *PATTERN, int SECRET_ONLY); +typedef gpgme_error_t(*gpgme_op_keylist_next_t)(gpgme_ctx_t CTX, + gpgme_key_t *R_KEY); +typedef gpgme_error_t(*gpgme_op_keylist_end_t)(gpgme_ctx_t CTX); +typedef gpgme_error_t(*gpgme_op_import_keys_t)(gpgme_ctx_t CTX, + gpgme_key_t *KEYS); +typedef void(*gpgme_key_ref_t)(gpgme_key_t KEY); +typedef void(*gpgme_key_unref_t)(gpgme_key_t KEY); +typedef gpgme_error_t (*gpgme_op_edit_t)(gpgme_ctx_t CTX, gpgme_key_t KEY, + gpgme_edit_cb_t FNC, void *HANDLE, gpgme_data_t OUT); typedef gpgme_ssize_t (*gpgme_io_write_t)(int fd, const void *buffer, size_t count); - -struct gpg_s { - const char * version; - gpgme_check_version_t gpgme_check; - gpgme_set_locale_t gpgme_set_locale; - gpgme_new_t gpgme_new; - gpgme_release_t gpgme_release; - gpgme_set_protocol_t gpgme_set_protocol; - gpgme_set_armor_t gpgme_set_armor; - - gpgme_data_new_t gpgme_data_new; - gpgme_data_new_from_mem_t gpgme_data_new_from_mem; - gpgme_data_new_from_cbs_t gpgme_data_new_from_cbs; - gpgme_data_release_t gpgme_data_release; - gpgme_data_identify_t gpgme_data_identify; - gpgme_data_seek_t gpgme_data_seek; - gpgme_data_read_t gpgme_data_read; - - gpgme_op_decrypt_t gpgme_op_decrypt; - gpgme_op_verify_t gpgme_op_verify; - gpgme_op_decrypt_verify_t gpgme_op_decrypt_verify; - gpgme_op_decrypt_result_t gpgme_op_decrypt_result; - gpgme_op_encrypt_sign_t gpgme_op_encrypt_sign; - gpgme_op_verify_result_t gpgme_op_verify_result; - gpgme_signers_clear_t gpgme_signers_clear; - gpgme_signers_add_t gpgme_signers_add; - - gpgme_get_key_t gpgme_get_key; - gpgme_op_genkey_t gpgme_op_genkey; - gpgme_op_genkey_result_t gpgme_op_genkey_result; - gpgme_op_delete_t gpgme_op_delete; - gpgme_op_import_t gpgme_op_import; - gpgme_op_export_t gpgme_op_export; - gpgme_set_keylist_mode_t gpgme_set_keylist_mode; - gpgme_get_keylist_mode_t gpgme_get_keylist_mode; - gpgme_op_keylist_start_t gpgme_op_keylist_start; - gpgme_op_keylist_next_t gpgme_op_keylist_next; - gpgme_op_keylist_end_t gpgme_op_keylist_end; - gpgme_op_import_keys_t gpgme_op_import_keys; - gpgme_key_ref_t gpgme_key_ref; - gpgme_key_unref_t gpgme_key_unref; - gpgme_op_edit_t gpgme_op_edit; - gpgme_io_write_t gpgme_io_write; -}; + +struct gpg_s { + const char * version; + gpgme_check_version_t gpgme_check; + gpgme_set_locale_t gpgme_set_locale; + gpgme_new_t gpgme_new; + gpgme_release_t gpgme_release; + gpgme_set_protocol_t gpgme_set_protocol; + gpgme_set_armor_t gpgme_set_armor; + + gpgme_data_new_t gpgme_data_new; + gpgme_data_new_from_mem_t gpgme_data_new_from_mem; + gpgme_data_new_from_cbs_t gpgme_data_new_from_cbs; + gpgme_data_release_t gpgme_data_release; + gpgme_data_identify_t gpgme_data_identify; + gpgme_data_seek_t gpgme_data_seek; + gpgme_data_read_t gpgme_data_read; + + gpgme_op_decrypt_t gpgme_op_decrypt; + gpgme_op_verify_t gpgme_op_verify; + gpgme_op_decrypt_verify_t gpgme_op_decrypt_verify; + gpgme_op_decrypt_result_t gpgme_op_decrypt_result; + gpgme_op_encrypt_sign_t gpgme_op_encrypt_sign; + gpgme_op_verify_result_t gpgme_op_verify_result; + gpgme_signers_clear_t gpgme_signers_clear; + gpgme_signers_add_t gpgme_signers_add; + + gpgme_get_key_t gpgme_get_key; + gpgme_op_genkey_t gpgme_op_genkey; + gpgme_op_genkey_result_t gpgme_op_genkey_result; + gpgme_op_delete_t gpgme_op_delete; + gpgme_op_import_t gpgme_op_import; + gpgme_op_export_t gpgme_op_export; + gpgme_set_keylist_mode_t gpgme_set_keylist_mode; + gpgme_get_keylist_mode_t gpgme_get_keylist_mode; + gpgme_op_keylist_start_t gpgme_op_keylist_start; + gpgme_op_keylist_next_t gpgme_op_keylist_next; + gpgme_op_keylist_end_t gpgme_op_keylist_end; + gpgme_op_import_keys_t gpgme_op_import_keys; + gpgme_key_ref_t gpgme_key_ref; + gpgme_key_unref_t gpgme_key_unref; + gpgme_op_edit_t gpgme_op_edit; + gpgme_io_write_t gpgme_io_write; +}; + diff --git a/src/transport.c b/src/transport.c index 8ab7115b..b8b609f9 100644 --- a/src/transport.c +++ b/src/transport.c @@ -4,10 +4,10 @@ #include #include +PEP_transport_t transports[PEP_trans__count]; + PEP_STATUS init_transport_system(PEP_SESSION session, bool in_first) { - static PEP_transport_t transports[PEP_trans__count]; - assert(session); session->transports = transports; diff --git a/src/transport.h b/src/transport.h index 9b5def0a..8af8839c 100644 --- a/src/transport.h +++ b/src/transport.h @@ -33,6 +33,8 @@ struct _PEP_transport_t { PEP_text_format native_text_format; // native format of the transport }; +extern PEP_transport_t transports[PEP_trans__count]; + typedef uint64_t transports_mask; #ifdef __cplusplus diff --git a/test/message_api_test.cc b/test/message_api_test.cc index ef3ea4e5..9b73617f 100644 --- a/test/message_api_test.cc +++ b/test/message_api_test.cc @@ -68,7 +68,7 @@ int main() { stringlist_t *keylist4; PEP_color color; PEP_STATUS status4 = decrypt_message(session, enc_msg2, PEP_MIME_none, &msg4, &keylist4, &color); - assert(status4 == PEP_STATUS_OK); + assert(status4 == PEP_DECRYPTED_AND_VERIFIED); assert(msg4); assert(keylist4);