CpEpEngine.h
author Markus Schaber <markus@pep-security.net>
Sun, 02 Oct 2016 15:38:37 +0200
branchkeysync
changeset 167 9bdd481dd300
parent 165 77dfd1defafa
child 169 d776268c12a7
permissions -rw-r--r--
Expose flags for en- and decryption
vb@0
     1
// CpEpEngine.h : Declaration of the CpEpEngine
vb@0
     2
vb@0
     3
#pragma once
vb@0
     4
#include "resource.h"       // main symbols
vb@0
     5
vb@0
     6
#include "pEpComServerAdapter_i.h"
vb@0
     7
#include "_IpEpEngineEvents_CP.h"
vb@0
     8
#include "locked_queue.hh"
vb@0
     9
#include "utf8_helper.h"
vb@33
    10
#include "pEp_utility.h"
markus@165
    11
#include <queue>
vb@0
    12
vb@0
    13
vb@0
    14
#if defined(_WIN32_WCE) && !defined(_CE_DCOM) && !defined(_CE_ALLOW_SINGLE_THREADED_OBJECTS_IN_MTA)
vb@0
    15
#error "Single-threaded COM objects are not properly supported on Windows CE platform, such as the Windows Mobile platforms that do not include full DCOM support. Define _CE_ALLOW_SINGLE_THREADED_OBJECTS_IN_MTA to force ATL to support creating single-thread COM object's and allow use of it's single-threaded COM object implementations. The threading model in your rgs file was set to 'Free' as that is the only threading model supported in non DCOM Windows CE platforms."
vb@0
    16
#endif
vb@0
    17
vb@0
    18
using namespace ATL;
vb@0
    19
using namespace utility;
vb@10
    20
using namespace pEp::utility;
vb@0
    21
vb@0
    22
// CpEpEngine
vb@0
    23
vb@0
    24
class ATL_NO_VTABLE CpEpEngine :
vb@3
    25
    public CComObjectRootEx<CComObjectThreadModel>,
vb@0
    26
	public CComCoClass<CpEpEngine, &CLSID_pEpEngine>,
vb@0
    27
	public ISupportErrorInfo,
vb@0
    28
	public IConnectionPointContainerImpl<CpEpEngine>,
vb@0
    29
	public CProxy_IpEpEngineEvents<CpEpEngine>,
vb@15
    30
	public IpEpEngine
vb@0
    31
{
vb@28
    32
protected:
vb@28
    33
    static int examine_identity(pEp_identity *ident, void *management);
vb@28
    34
vb@0
    35
public:
vb@51
    36
    CpEpEngine() : keymanagement_thread(NULL), identity_queue(NULL), verbose_mode(false)
vb@0
    37
	{
vb@0
    38
        PEP_STATUS status = ::init(&m_session);
vb@0
    39
        assert(status == PEP_STATUS_OK);
vb@28
    40
        ::register_examine_function(m_session, CpEpEngine::examine_identity, (void *)this);
markus@165
    41
        ::register_sync_callbacks(m_session, (void*)this, messageToSend, showHandshake, inject_sync_msg, retreive_next_sync_msg);
vb@0
    42
        ::log_event(m_session, "Startup", "pEp COM Adapter", NULL, NULL);
vb@0
    43
    }
vb@24
    44
vb@0
    45
    ~CpEpEngine()
vb@0
    46
    {
vb@26
    47
        stop_keyserver_lookup();
vb@77
    48
        ::unregister_sync_callbacks(m_session);
vb@0
    49
        ::log_event(m_session, "Shutdown", "pEp COM Adapter", NULL, NULL);
vb@0
    50
        ::release(m_session);
vb@0
    51
    }
vb@0
    52
vb@0
    53
DECLARE_REGISTRY_RESOURCEID(IDR_PEPENGINE)
vb@0
    54
vb@0
    55
DECLARE_NOT_AGGREGATABLE(CpEpEngine)
vb@0
    56
vb@0
    57
BEGIN_COM_MAP(CpEpEngine)
vb@5
    58
    COM_INTERFACE_ENTRY(IpEpEngine)
vb@5
    59
    COM_INTERFACE_ENTRY(ISupportErrorInfo)
vb@5
    60
    COM_INTERFACE_ENTRY(IConnectionPointContainer)
vb@0
    61
END_COM_MAP()
vb@0
    62
vb@0
    63
BEGIN_CONNECTION_POINT_MAP(CpEpEngine)
vb@0
    64
	CONNECTION_POINT_ENTRY(__uuidof(_IpEpEngineEvents))
vb@0
    65
END_CONNECTION_POINT_MAP()
vb@0
    66
// ISupportsErrorInfo
vb@0
    67
	STDMETHOD(InterfaceSupportsErrorInfo)(REFIID riid);
vb@0
    68
vb@0
    69
vb@0
    70
	DECLARE_PROTECT_FINAL_CONSTRUCT()
vb@0
    71
vb@0
    72
	HRESULT FinalConstruct()
vb@0
    73
	{
vb@0
    74
		return S_OK;
vb@0
    75
	}
vb@0
    76
vb@0
    77
	void FinalRelease()
vb@0
    78
	{
vb@0
    79
	}
vb@0
    80
vb@0
    81
vb@0
    82
protected:
vb@0
    83
    class session
vb@0
    84
    {
vb@0
    85
    private:
vb@0
    86
        CpEpEngine *me;
vb@0
    87
vb@0
    88
    public:
vb@0
    89
        session(CpEpEngine *myself)
vb@0
    90
        {
vb@0
    91
            me = myself;
vb@0
    92
            me->session_mutex.lock();
vb@0
    93
        }
vb@0
    94
vb@0
    95
        ~session()
vb@0
    96
        {
vb@0
    97
            me->session_mutex.unlock();
vb@0
    98
        }
vb@0
    99
vb@0
   100
        operator PEP_SESSION const () 
vb@0
   101
        {
vb@0
   102
            return me->m_session;
vb@0
   103
        }
vb@0
   104
    };
vb@0
   105
vb@0
   106
    session get_session()
vb@0
   107
    {
vb@0
   108
        return session(this);
vb@0
   109
    }
vb@0
   110
markus@84
   111
	class callbacks
markus@84
   112
	{
markus@84
   113
	private:
markus@84
   114
		CpEpEngine *me;
markus@84
   115
markus@84
   116
	public:
markus@84
   117
		callbacks(CpEpEngine *myself)
markus@84
   118
		{
markus@84
   119
			me = myself;
markus@84
   120
			me->callback_mutex.lock();
markus@84
   121
		}
markus@84
   122
markus@84
   123
		~callbacks()
markus@84
   124
		{
markus@84
   125
			me->callback_mutex.unlock();
markus@84
   126
		}
markus@84
   127
markus@84
   128
		operator vector<IpEpEngineCallbacks *>& ()
markus@84
   129
		{
markus@84
   130
			return me->callback_vector;
markus@84
   131
		}
markus@84
   132
	};
markus@84
   133
markus@84
   134
	callbacks get_callbacks()
markus@84
   135
	{
markus@84
   136
		return callbacks(this);
markus@84
   137
	}
markus@84
   138
vb@0
   139
    typedef locked_queue<pEp_identity_cpp> identity_queue_t;
vb@0
   140
    static ::pEp_identity * retrieve_next_identity(void *management);
markus@165
   141
    static PEP_STATUS messageToSend(void * obj, message *msg);
markus@164
   142
    static PEP_STATUS showHandshake(void * obj, pEp_identity *self, pEp_identity *partner);
vb@74
   143
markus@165
   144
	static int inject_sync_msg(void *msg, void* management);
markus@165
   145
	static void* retreive_next_sync_msg(void* management);
markus@165
   146
vb@0
   147
    HRESULT error(_bstr_t msg);
vb@0
   148
vb@51
   149
    void verbose(string text)
vb@51
   150
    {
vb@51
   151
        if (verbose_mode) {
vb@51
   152
            stringstream ss;
vb@51
   153
            ss << __FILE__ << ":" << __LINE__ << " " << text;
vb@51
   154
            ::log_event(get_session(), "verbose", "pEp COM Server Adapter", ss.str().c_str(), NULL);
vb@51
   155
        }
vb@51
   156
    }
vb@51
   157
vb@0
   158
private:
vb@0
   159
    PEP_SESSION m_session;
vb@0
   160
    mutex session_mutex;
vb@25
   161
    atomic< identity_queue_t * > identity_queue;
vb@0
   162
    thread *keymanagement_thread;
vb@51
   163
    bool verbose_mode;
vb@0
   164
markus@84
   165
	mutex callback_mutex;
markus@84
   166
	vector<IpEpEngineCallbacks*> callback_vector;
markus@84
   167
markus@165
   168
	// Keysync members
markus@165
   169
	static std::mutex keysync_mutex;
markus@165
   170
	static std::condition_variable keysync_condition;
markus@165
   171
	static std::thread *keysync_thread;
markus@165
   172
	static std::queue<void*> keysync_queue;
markus@165
   173
	static bool keysync_thread_running;
markus@165
   174
	static bool keysync_abort_requested;
markus@165
   175
	static PEP_SESSION keysync_session;
markus@165
   176
vb@0
   177
public:
vb@60
   178
    // runtime config of the adapter
vb@60
   179
vb@61
   180
    STDMETHOD(verbose_logging)(VARIANT_BOOL enable);
vb@60
   181
    
vb@60
   182
    // runtime config of the engine
vb@60
   183
vb@61
   184
    STDMETHOD(passive_mode)(VARIANT_BOOL enable);
vb@61
   185
    STDMETHOD(unencrypted_subject)(VARIANT_BOOL enable);
vb@60
   186
vb@60
   187
    // basic API
vb@51
   188
vb@0
   189
    STDMETHOD(log)(BSTR title, BSTR entity, BSTR description, BSTR comment);
vb@0
   190
    STDMETHOD(decrypt)(BSTR ctext, BSTR * ptext, LPSAFEARRAY * key_list, pEp_STATUS * decrypt_status);
vb@0
   191
    STDMETHOD(decrypt_b)(BSTR ctext, LPSAFEARRAY * ptext, LPSAFEARRAY * key_list, pEp_STATUS * decrypt_status);
vb@0
   192
    STDMETHOD(encrypt)(SAFEARRAY * key_list, BSTR ptext, BSTR * ctext, pEp_STATUS * status);
vb@0
   193
    STDMETHOD(encrypt_b)(SAFEARRAY * key_list, SAFEARRAY * ptext, BSTR * ctext, pEp_STATUS * status);
vb@17
   194
    STDMETHOD(trustword)(LONG value, BSTR lang, BSTR * word);
vb@17
   195
    STDMETHOD(trustwords)(BSTR fpr, BSTR lang, LONG max_words, BSTR * words);
vb@70
   196
    STDMETHOD(get_identity)(BSTR address, BSTR user_id, pEp_identity_s * ident);
vb@0
   197
    STDMETHOD(set_identity)(pEp_identity_s * ident);
vb@0
   198
    STDMETHOD(generate_keypair)(pEp_identity_s * ident, BSTR * fpr);
vb@0
   199
    STDMETHOD(delete_keypair)(BSTR fpr);
vb@0
   200
    STDMETHOD(import_key)(BSTR key_data);
vb@0
   201
    STDMETHOD(import_key_b)(SAFEARRAY * key_data);
vb@0
   202
    STDMETHOD(export_key)(BSTR fpr, BSTR * key_data);
vb@0
   203
    STDMETHOD(recv_key)(BSTR pattern);
vb@0
   204
    STDMETHOD(find_keys)(BSTR pattern, LPSAFEARRAY * key_list);
vb@0
   205
    STDMETHOD(send_key)(BSTR pattern);
vb@57
   206
    STDMETHOD(get_crashdump_log)(LONG maxlines, BSTR * log);
Dean@151
   207
    STDMETHOD(get_engine_version)(BSTR * engine_version);
vb@59
   208
    STDMETHOD(get_languagelist)(BSTR * languages);
vb@59
   209
    STDMETHOD(get_phrase)(BSTR lang, LONG phrase_id, BSTR * phrase);
vb@11
   210
vb@11
   211
    // keymanagement API
vb@11
   212
vb@24
   213
    STDMETHOD(start_keyserver_lookup)();
vb@24
   214
    STDMETHOD(stop_keyserver_lookup)();
vb@24
   215
vb@0
   216
    STDMETHOD(examine_identity)(pEp_identity_s * ident);
vb@0
   217
    STDMETHOD(verify)(BSTR text, BSTR signature, LPSAFEARRAY * key_list, pEp_STATUS * verify_status);
vb@0
   218
    STDMETHOD(myself)(struct pEp_identity_s *ident, struct pEp_identity_s *result);
vb@0
   219
    STDMETHOD(update_identity)(struct pEp_identity_s *ident, struct pEp_identity_s *result);
markus@164
   220
    STDMETHOD(key_mistrusted)(struct pEp_identity_s *ident);
Edouard@55
   221
    STDMETHOD(key_reset_trust)(struct pEp_identity_s *ident);
vb@50
   222
    STDMETHOD(trust_personal_key)(struct pEp_identity_s *ident, struct pEp_identity_s *result);
vb@48
   223
markus@165
   224
	// keysync API
markus@165
   225
	STDMETHOD(start_keysync)();
markus@165
   226
	STDMETHOD(stop_keysync)();
markus@165
   227
vb@65
   228
    // Blacklist API
vb@65
   229
vb@65
   230
    STDMETHOD(blacklist_add)(BSTR fpr);
vb@65
   231
    STDMETHOD(blacklist_delete)(BSTR fpr);
vb@65
   232
    STDMETHOD(blacklist_is_listed)(BSTR fpr, VARIANT_BOOL *listed);
vb@65
   233
    STDMETHOD(blacklist_retrieve)(SAFEARRAY **blacklist);
vb@65
   234
vb@12
   235
    // Message API
vb@12
   236
markus@167
   237
    STDMETHOD(encrypt_message)(text_message * src, text_message * dst, SAFEARRAY * extra, pEpEncryptFlags flags);
markus@167
   238
    STDMETHOD(decrypt_message)(text_message * src, text_message * dst, SAFEARRAY ** keylist, pEpDecryptFlags* flags, pEp_rating *rating);
markus@164
   239
    STDMETHOD(outgoing_message_rating)(text_message *msg, pEp_rating * pVal);
markus@164
   240
    STDMETHOD(identity_rating)(pEp_identity_s * ident, pEp_rating * pVal);
markus@164
   241
	STDMETHOD(color_from_rating)(pEp_rating rating, pEp_color * pVal);
vb@18
   242
markus@84
   243
	// Event callbacks
markus@84
   244
markus@84
   245
	STDMETHOD(register_callbacks)(IpEpEngineCallbacks *new_callback);
markus@84
   246
	STDMETHOD(unregister_callbacks)(IpEpEngineCallbacks *obsolete_callback);
krista@154
   247
    
krista@154
   248
    // PGP compatibility functions
krista@157
   249
    STDMETHOD(OpenPGP_list_keyinfo)(BSTR search_pattern, LPSAFEARRAY* keyinfo_list);
markus@84
   250
markus@84
   251
protected:
markus@84
   252
	HRESULT Fire_MessageToSend(
markus@84
   253
		/* [in] */ struct text_message *msg);
markus@84
   254
markus@84
   255
	HRESULT Fire_ShowHandshake(
markus@84
   256
		/* [in] */ struct pEp_identity_s *self,
markus@84
   257
		/* [in] */ struct pEp_identity_s *partner,
markus@84
   258
		/* [retval][out] */ sync_handshake_result_s *result);
vb@0
   259
};
vb@0
   260
vb@0
   261
OBJECT_ENTRY_AUTO(__uuidof(pEpEngine), CpEpEngine)