CpEpEngine.h
author Markus Schaber <markus@pep-security.net>
Tue, 04 Oct 2016 21:50:26 +0200
branchkeysync
changeset 175 9aabb2c9df08
parent 172 112b0fac353d
child 177 4d197f1c3abb
permissions -rw-r--r--
COM-29: Adapt to new interface changes in the engine.
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 "locked_queue.hh"
vb@0
     8
#include "utf8_helper.h"
vb@33
     9
#include "pEp_utility.h"
markus@165
    10
#include <queue>
vb@0
    11
vb@0
    12
#if defined(_WIN32_WCE) && !defined(_CE_DCOM) && !defined(_CE_ALLOW_SINGLE_THREADED_OBJECTS_IN_MTA)
vb@0
    13
#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
    14
#endif
vb@0
    15
vb@0
    16
using namespace ATL;
vb@0
    17
using namespace utility;
vb@10
    18
using namespace pEp::utility;
vb@0
    19
markus@172
    20
markus@172
    21
// TODO: Remove this enum, it is not needed in the interface, but it currently is still used in the code which is
markus@172
    22
// not removed / reworked yet...
markus@172
    23
typedef enum pEpStatus {
markus@172
    24
     pEpStatusOK = 0,
markus@172
    25
markus@172
    26
     pEp_INIT_CANNOT_LOAD_GPGME = 0x0110,
markus@172
    27
     pEp_INIT_GPGME_INIT_FAILED = 0x0111,
markus@172
    28
     pEp_INIT_NO_GPG_HOME = 0x0112,
markus@172
    29
     pEp_INIT_NETPGP_INIT_FAILED = 0x0113,
markus@172
    30
markus@172
    31
     pEp_INIT_SQLITE3_WITHOUT_MUTEX = 0x0120,
markus@172
    32
     pEp_INIT_CANNOT_OPEN_DB = 0x0121,
markus@172
    33
     pEp_INIT_CANNOT_OPEN_SYSTEM_DB = 0x0122,
markus@172
    34
markus@172
    35
     pEp_KEY_NOT_FOUND = 0x0201,
markus@172
    36
     pEp_KEY_HAS_AMBIG_NAME = 0x0202,
markus@172
    37
     pEp_GET_KEY_FAILED = 0x0203,
markus@172
    38
markus@172
    39
     pEp_CANNOT_FIND_IDENTITY = 0x0301,
markus@172
    40
     pEp_CANNOT_SET_PERSON = 0x0381,
markus@172
    41
     pEp_CANNOT_SET_PGP_KEYPAIR = 0x0382,
markus@172
    42
     pEp_CANNOT_SET_IDENTITY = 0x0383,
markus@172
    43
     pEp_CANNOT_SET_TRUST = 0x0384,
markus@172
    44
markus@172
    45
     pEp_UNENCRYPTED = 0x0400,
markus@172
    46
     pEp_VERIFIED = 0x0401,
markus@172
    47
     pEp_DECRYPTED = 0x0402,
markus@172
    48
     pEp_DECRYPTED_AND_VERIFIED = 0x0403,
markus@172
    49
     pEp_DECRYPT_WRONG_FORMAT = 0x0404,
markus@172
    50
     pEp_DECRYPT_NO_KEY = 0x0405,
markus@172
    51
     pEp_DECRYPT_SIGNATURE_DOES_NOT_MATCH = 0x0406,
markus@172
    52
     pEp_VERIFY_NO_KEY = 0x0407,
markus@172
    53
     pEp_VERIFIED_AND_TRUSTED = 0x0408,
markus@172
    54
     pEp_CANNOT_DECRYPT_UNKNOWN = 0x04ff,
markus@172
    55
markus@172
    56
     pEp_TRUSTWORD_NOT_FOUND = 0x0501,
markus@172
    57
markus@172
    58
     pEp_CANNOT_CREATE_KEY = 0x0601,
markus@172
    59
     pEp_CANNOT_SEND_KEY = 0x0602,
markus@172
    60
markus@172
    61
     pEp_PHRASE_NOT_FOUND = 0x0701,
markus@172
    62
markus@172
    63
     pEp_COMMIT_FAILED = 0xff01,
markus@172
    64
markus@172
    65
     pEp_CANNOT_CREATE_TEMP_FILE = -5,
markus@172
    66
     pEp_ILLEGAL_VALUE = -4,
markus@172
    67
     pEp_BUFFER_TOO_SMALL = -3,
markus@172
    68
     pEp_OUT_OF_MEMORY = -2,
markus@172
    69
     pEp_UNKNOWN_ERROR = -1
markus@172
    70
 } pEpStatus;
markus@172
    71
vb@0
    72
// CpEpEngine
vb@0
    73
vb@0
    74
class ATL_NO_VTABLE CpEpEngine :
vb@3
    75
    public CComObjectRootEx<CComObjectThreadModel>,
vb@0
    76
	public CComCoClass<CpEpEngine, &CLSID_pEpEngine>,
vb@0
    77
	public ISupportErrorInfo,
vb@15
    78
	public IpEpEngine
vb@0
    79
{
vb@28
    80
protected:
vb@28
    81
    static int examine_identity(pEp_identity *ident, void *management);
vb@28
    82
vb@0
    83
public:
vb@51
    84
    CpEpEngine() : keymanagement_thread(NULL), identity_queue(NULL), verbose_mode(false)
vb@0
    85
	{
vb@0
    86
        PEP_STATUS status = ::init(&m_session);
vb@0
    87
        assert(status == PEP_STATUS_OK);
vb@28
    88
        ::register_examine_function(m_session, CpEpEngine::examine_identity, (void *)this);
vb@0
    89
        ::log_event(m_session, "Startup", "pEp COM Adapter", NULL, NULL);
vb@0
    90
    }
vb@24
    91
vb@0
    92
    ~CpEpEngine()
vb@0
    93
    {
markus@169
    94
        stop_keysync();
markus@172
    95
        StopKeyserverLookup();
vb@0
    96
        ::log_event(m_session, "Shutdown", "pEp COM Adapter", NULL, NULL);
vb@0
    97
        ::release(m_session);
vb@0
    98
    }
vb@0
    99
vb@0
   100
DECLARE_REGISTRY_RESOURCEID(IDR_PEPENGINE)
vb@0
   101
vb@0
   102
DECLARE_NOT_AGGREGATABLE(CpEpEngine)
vb@0
   103
vb@0
   104
BEGIN_COM_MAP(CpEpEngine)
vb@5
   105
    COM_INTERFACE_ENTRY(IpEpEngine)
vb@5
   106
    COM_INTERFACE_ENTRY(ISupportErrorInfo)
vb@0
   107
END_COM_MAP()
vb@0
   108
vb@0
   109
// ISupportsErrorInfo
vb@0
   110
	STDMETHOD(InterfaceSupportsErrorInfo)(REFIID riid);
vb@0
   111
vb@0
   112
vb@0
   113
	DECLARE_PROTECT_FINAL_CONSTRUCT()
vb@0
   114
vb@0
   115
	HRESULT FinalConstruct()
vb@0
   116
	{
vb@0
   117
		return S_OK;
vb@0
   118
	}
vb@0
   119
vb@0
   120
	void FinalRelease()
vb@0
   121
	{
vb@0
   122
	}
vb@0
   123
vb@0
   124
vb@0
   125
protected:
vb@0
   126
    class session
vb@0
   127
    {
vb@0
   128
    private:
vb@0
   129
        CpEpEngine *me;
vb@0
   130
vb@0
   131
    public:
vb@0
   132
        session(CpEpEngine *myself)
vb@0
   133
        {
vb@0
   134
            me = myself;
vb@0
   135
            me->session_mutex.lock();
vb@0
   136
        }
vb@0
   137
vb@0
   138
        ~session()
vb@0
   139
        {
vb@0
   140
            me->session_mutex.unlock();
vb@0
   141
        }
vb@0
   142
markus@169
   143
        operator PEP_SESSION const ()
vb@0
   144
        {
vb@0
   145
            return me->m_session;
vb@0
   146
        }
vb@0
   147
    };
vb@0
   148
vb@0
   149
    session get_session()
vb@0
   150
    {
vb@0
   151
        return session(this);
vb@0
   152
    }
vb@0
   153
markus@84
   154
	class callbacks
markus@84
   155
	{
markus@84
   156
	private:
markus@84
   157
		CpEpEngine *me;
markus@84
   158
markus@84
   159
	public:
markus@84
   160
		callbacks(CpEpEngine *myself)
markus@84
   161
		{
markus@84
   162
			me = myself;
markus@84
   163
			me->callback_mutex.lock();
markus@84
   164
		}
markus@84
   165
markus@84
   166
		~callbacks()
markus@84
   167
		{
markus@84
   168
			me->callback_mutex.unlock();
markus@84
   169
		}
markus@84
   170
markus@84
   171
		operator vector<IpEpEngineCallbacks *>& ()
markus@84
   172
		{
markus@84
   173
			return me->callback_vector;
markus@84
   174
		}
markus@84
   175
	};
markus@84
   176
markus@84
   177
	callbacks get_callbacks()
markus@84
   178
	{
markus@84
   179
		return callbacks(this);
markus@84
   180
	}
markus@84
   181
vb@0
   182
    typedef locked_queue<pEp_identity_cpp> identity_queue_t;
vb@0
   183
    static ::pEp_identity * retrieve_next_identity(void *management);
markus@165
   184
    static PEP_STATUS messageToSend(void * obj, message *msg);
markus@164
   185
    static PEP_STATUS showHandshake(void * obj, pEp_identity *self, pEp_identity *partner);
vb@74
   186
markus@165
   187
vb@0
   188
    HRESULT error(_bstr_t msg);
vb@0
   189
vb@51
   190
    void verbose(string text)
vb@51
   191
    {
vb@51
   192
        if (verbose_mode) {
vb@51
   193
            stringstream ss;
vb@51
   194
            ss << __FILE__ << ":" << __LINE__ << " " << text;
vb@51
   195
            ::log_event(get_session(), "verbose", "pEp COM Server Adapter", ss.str().c_str(), NULL);
vb@51
   196
        }
vb@51
   197
    }
vb@51
   198
vb@0
   199
private:
vb@0
   200
    PEP_SESSION m_session;
vb@0
   201
    mutex session_mutex;
vb@25
   202
    atomic< identity_queue_t * > identity_queue;
vb@0
   203
    thread *keymanagement_thread;
vb@51
   204
    bool verbose_mode;
vb@0
   205
markus@84
   206
	mutex callback_mutex;
markus@84
   207
	vector<IpEpEngineCallbacks*> callback_vector;
markus@84
   208
markus@165
   209
	// Keysync members
markus@169
   210
    static int inject_sync_msg(void *msg, void* management);
markus@169
   211
    static void* retreive_next_sync_msg(void* management);
markus@169
   212
    void start_keysync();
markus@169
   213
    void stop_keysync();
markus@169
   214
markus@169
   215
    std::mutex keysync_mutex;
markus@169
   216
    std::condition_variable keysync_condition;
markus@169
   217
    std::thread *keysync_thread = NULL;
markus@169
   218
    std::queue<void*> keysync_queue;
markus@169
   219
    bool keysync_thread_running = false;
markus@169
   220
    bool keysync_abort_requested = false;
markus@169
   221
    PEP_SESSION keysync_session;
markus@165
   222
vb@0
   223
public:
vb@60
   224
    // runtime config of the adapter
vb@60
   225
markus@172
   226
    STDMETHOD(VerboseLogging)(VARIANT_BOOL enable);
markus@169
   227
vb@60
   228
    // runtime config of the engine
vb@60
   229
markus@172
   230
    STDMETHOD(PassiveMode)(VARIANT_BOOL enable);
markus@172
   231
    STDMETHOD(UnencryptedSubject)(VARIANT_BOOL enable);
vb@60
   232
vb@60
   233
    // basic API
vb@51
   234
vb@0
   235
    STDMETHOD(log)(BSTR title, BSTR entity, BSTR description, BSTR comment);
markus@172
   236
    STDMETHOD(decrypt)(BSTR ctext, BSTR * ptext, LPSAFEARRAY * key_list, pEpStatus * decrypt_status);
markus@172
   237
    STDMETHOD(decrypt_b)(BSTR ctext, LPSAFEARRAY * ptext, LPSAFEARRAY * key_list, pEpStatus * decrypt_status);
markus@172
   238
    STDMETHOD(encrypt)(SAFEARRAY * key_list, BSTR ptext, BSTR * ctext, pEpStatus * status);
markus@172
   239
    STDMETHOD(encrypt_b)(SAFEARRAY * key_list, SAFEARRAY * ptext, BSTR * ctext, pEpStatus * status);
vb@17
   240
    STDMETHOD(trustword)(LONG value, BSTR lang, BSTR * word);
markus@172
   241
    STDMETHOD(TrustWords)(BSTR fpr, BSTR lang, LONG max_words, BSTR * words);
markus@172
   242
    STDMETHOD(get_identity)(BSTR address, BSTR user_id, pEpIdentity * ident);
markus@172
   243
    STDMETHOD(set_identity)(pEpIdentity * ident);
markus@172
   244
    STDMETHOD(generate_keypair)(pEpIdentity * ident, BSTR * fpr);
vb@0
   245
    STDMETHOD(delete_keypair)(BSTR fpr);
vb@0
   246
    STDMETHOD(import_key)(BSTR key_data);
vb@0
   247
    STDMETHOD(import_key_b)(SAFEARRAY * key_data);
vb@0
   248
    STDMETHOD(export_key)(BSTR fpr, BSTR * key_data);
vb@0
   249
    STDMETHOD(recv_key)(BSTR pattern);
vb@0
   250
    STDMETHOD(find_keys)(BSTR pattern, LPSAFEARRAY * key_list);
vb@0
   251
    STDMETHOD(send_key)(BSTR pattern);
markus@172
   252
    STDMETHOD(GetCrashdumpLog)(LONG maxlines, BSTR * log);
markus@172
   253
    STDMETHOD(GetEngineVersion)(BSTR * engine_version);
markus@172
   254
    STDMETHOD(GetLanguagelist)(BSTR * languages);
vb@59
   255
    STDMETHOD(get_phrase)(BSTR lang, LONG phrase_id, BSTR * phrase);
vb@11
   256
vb@11
   257
    // keymanagement API
vb@11
   258
markus@172
   259
    STDMETHOD(StartKeyserverLookup)();
markus@172
   260
    STDMETHOD(StopKeyserverLookup)();
vb@24
   261
markus@172
   262
    STDMETHOD(examine_identity)(pEpIdentity * ident);
markus@172
   263
    STDMETHOD(verify)(BSTR text, BSTR signature, LPSAFEARRAY * key_list, pEpStatus * verify_status);
markus@172
   264
    STDMETHOD(Myself)(struct pEpIdentity *ident, struct pEpIdentity *result);
markus@172
   265
    STDMETHOD(UpdateIdentity)(struct pEpIdentity *ident, struct pEpIdentity *result);
markus@172
   266
    STDMETHOD(KeyMistrusted)(struct pEpIdentity *ident);
markus@172
   267
    STDMETHOD(KeyResetTrust)(struct pEpIdentity *ident);
markus@172
   268
    STDMETHOD(TrustPersonalKey)(struct pEpIdentity *ident, struct pEpIdentity *result);
vb@48
   269
markus@165
   270
vb@65
   271
    // Blacklist API
vb@65
   272
markus@172
   273
    STDMETHOD(BlacklistAdd)(BSTR fpr);
markus@172
   274
    STDMETHOD(BlacklistDelete)(BSTR fpr);
markus@172
   275
    STDMETHOD(BlacklistIsListed)(BSTR fpr, VARIANT_BOOL *listed);
markus@172
   276
    STDMETHOD(BlacklistRetreive)(SAFEARRAY **blacklist);
vb@65
   277
vb@12
   278
    // Message API
vb@12
   279
markus@172
   280
    STDMETHOD(EncryptMessage)(TextMessage * src, TextMessage * dst, SAFEARRAY * extra, pEpEncryptFlags flags);
markus@172
   281
    STDMETHOD(DecryptMessage)(TextMessage * src, TextMessage * dst, SAFEARRAY ** keylist, pEpDecryptFlags* flags, pEpRating *rating);
markus@172
   282
    STDMETHOD(OutgoingMessageRating)(TextMessage *msg, pEpRating * pVal);
markus@172
   283
    STDMETHOD(IdentityRating)(pEpIdentity * ident, pEpRating * pVal);
markus@172
   284
	STDMETHOD(ColorFromRating)(pEpRating rating, pEpColor * pVal);
vb@18
   285
markus@84
   286
	// Event callbacks
markus@84
   287
markus@172
   288
	STDMETHOD(RegisterCallbacks)(IpEpEngineCallbacks *new_callback);
markus@172
   289
	STDMETHOD(UnregisterCallbacks)(IpEpEngineCallbacks *obsolete_callback);
markus@169
   290
krista@154
   291
    // PGP compatibility functions
krista@157
   292
    STDMETHOD(OpenPGP_list_keyinfo)(BSTR search_pattern, LPSAFEARRAY* keyinfo_list);
markus@84
   293
markus@84
   294
protected:
markus@84
   295
	HRESULT Fire_MessageToSend(
markus@172
   296
		/* [in] */ struct TextMessage *msg);
markus@84
   297
markus@84
   298
	HRESULT Fire_ShowHandshake(
markus@172
   299
		/* [in] */ struct pEpIdentity *self,
markus@172
   300
		/* [in] */ struct pEpIdentity *partner,
markus@172
   301
		/* [retval][out] */ SyncHandshakeResult *result);
vb@0
   302
};
vb@0
   303
vb@0
   304
OBJECT_ENTRY_AUTO(__uuidof(pEpEngine), CpEpEngine)