CpEpEngine.h
author Markus Schaber <markus@pep-security.net>
Thu, 01 Sep 2016 19:09:52 +0200
branchoutlook_mime_support
changeset 155 0264d4b637a4
parent 150 316269fc987e
parent 151 432c35b5903c
child 156 8c9f8ce2c3a8
permissions -rw-r--r--
Merge with default
     1 // CpEpEngine.h : Declaration of the CpEpEngine
     2 
     3 #pragma once
     4 #include "resource.h"       // main symbols
     5 
     6 #include "pEpComServerAdapter_i.h"
     7 #include "_IpEpEngineEvents_CP.h"
     8 #include "locked_queue.hh"
     9 #include "utf8_helper.h"
    10 #include "pEp_utility.h"
    11 
    12 
    13 #if defined(_WIN32_WCE) && !defined(_CE_DCOM) && !defined(_CE_ALLOW_SINGLE_THREADED_OBJECTS_IN_MTA)
    14 #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."
    15 #endif
    16 
    17 using namespace ATL;
    18 using namespace utility;
    19 using namespace pEp::utility;
    20 
    21 // CpEpEngine
    22 
    23 class ATL_NO_VTABLE CpEpEngine :
    24     public CComObjectRootEx<CComObjectThreadModel>,
    25 	public CComCoClass<CpEpEngine, &CLSID_pEpEngine>,
    26 	public ISupportErrorInfo,
    27 	public IConnectionPointContainerImpl<CpEpEngine>,
    28 	public CProxy_IpEpEngineEvents<CpEpEngine>,
    29 	public IpEpEngine
    30 {
    31 protected:
    32     static int examine_identity(pEp_identity *ident, void *management);
    33 
    34 public:
    35     CpEpEngine() : keymanagement_thread(NULL), identity_queue(NULL), verbose_mode(false)
    36 	{
    37         PEP_STATUS status = ::init(&m_session);
    38         assert(status == PEP_STATUS_OK);
    39         ::register_examine_function(m_session, CpEpEngine::examine_identity, (void *)this);
    40         ::register_sync_callbacks(m_session, (void*)this, messageToSend, showHandshake);
    41         ::log_event(m_session, "Startup", "pEp COM Adapter", NULL, NULL);
    42     }
    43 
    44     ~CpEpEngine()
    45     {
    46         stop_keyserver_lookup();
    47         ::unregister_sync_callbacks(m_session);
    48         ::log_event(m_session, "Shutdown", "pEp COM Adapter", NULL, NULL);
    49         ::release(m_session);
    50     }
    51 
    52 DECLARE_REGISTRY_RESOURCEID(IDR_PEPENGINE)
    53 
    54 DECLARE_NOT_AGGREGATABLE(CpEpEngine)
    55 
    56 BEGIN_COM_MAP(CpEpEngine)
    57     COM_INTERFACE_ENTRY(IpEpEngine)
    58     COM_INTERFACE_ENTRY(ISupportErrorInfo)
    59     COM_INTERFACE_ENTRY(IConnectionPointContainer)
    60 END_COM_MAP()
    61 
    62 BEGIN_CONNECTION_POINT_MAP(CpEpEngine)
    63 	CONNECTION_POINT_ENTRY(__uuidof(_IpEpEngineEvents))
    64 END_CONNECTION_POINT_MAP()
    65 // ISupportsErrorInfo
    66 	STDMETHOD(InterfaceSupportsErrorInfo)(REFIID riid);
    67 
    68 
    69 	DECLARE_PROTECT_FINAL_CONSTRUCT()
    70 
    71 	HRESULT FinalConstruct()
    72 	{
    73 		return S_OK;
    74 	}
    75 
    76 	void FinalRelease()
    77 	{
    78 	}
    79 
    80 
    81 protected:
    82     class session
    83     {
    84     private:
    85         CpEpEngine *me;
    86 
    87     public:
    88         session(CpEpEngine *myself)
    89         {
    90             me = myself;
    91             me->session_mutex.lock();
    92         }
    93 
    94         ~session()
    95         {
    96             me->session_mutex.unlock();
    97         }
    98 
    99         operator PEP_SESSION const () 
   100         {
   101             return me->m_session;
   102         }
   103     };
   104 
   105     session get_session()
   106     {
   107         return session(this);
   108     }
   109 
   110 	class callbacks
   111 	{
   112 	private:
   113 		CpEpEngine *me;
   114 
   115 	public:
   116 		callbacks(CpEpEngine *myself)
   117 		{
   118 			me = myself;
   119 			me->callback_mutex.lock();
   120 		}
   121 
   122 		~callbacks()
   123 		{
   124 			me->callback_mutex.unlock();
   125 		}
   126 
   127 		operator vector<IpEpEngineCallbacks *>& ()
   128 		{
   129 			return me->callback_vector;
   130 		}
   131 	};
   132 
   133 	callbacks get_callbacks()
   134 	{
   135 		return callbacks(this);
   136 	}
   137 
   138     typedef locked_queue<pEp_identity_cpp> identity_queue_t;
   139     static ::pEp_identity * retrieve_next_identity(void *management);
   140     static PEP_STATUS messageToSend(void *obj, const message *msg);
   141     static PEP_STATUS showHandshake(void * obj, const pEp_identity *self, const pEp_identity *partner);
   142 
   143     HRESULT error(_bstr_t msg);
   144 
   145     void verbose(string text)
   146     {
   147         if (verbose_mode) {
   148             stringstream ss;
   149             ss << __FILE__ << ":" << __LINE__ << " " << text;
   150             ::log_event(get_session(), "verbose", "pEp COM Server Adapter", ss.str().c_str(), NULL);
   151         }
   152     }
   153 
   154 private:
   155     PEP_SESSION m_session;
   156     mutex session_mutex;
   157     atomic< identity_queue_t * > identity_queue;
   158     thread *keymanagement_thread;
   159     bool verbose_mode;
   160 
   161 	mutex callback_mutex;
   162 	vector<IpEpEngineCallbacks*> callback_vector;
   163 
   164 public:
   165     // runtime config of the adapter
   166 
   167     STDMETHOD(verbose_logging)(VARIANT_BOOL enable);
   168     
   169     // runtime config of the engine
   170 
   171     STDMETHOD(passive_mode)(VARIANT_BOOL enable);
   172     STDMETHOD(unencrypted_subject)(VARIANT_BOOL enable);
   173 
   174     // basic API
   175 
   176     STDMETHOD(log)(BSTR title, BSTR entity, BSTR description, BSTR comment);
   177     STDMETHOD(decrypt)(BSTR ctext, BSTR * ptext, LPSAFEARRAY * key_list, pEp_STATUS * decrypt_status);
   178     STDMETHOD(decrypt_b)(BSTR ctext, LPSAFEARRAY * ptext, LPSAFEARRAY * key_list, pEp_STATUS * decrypt_status);
   179     STDMETHOD(encrypt)(SAFEARRAY * key_list, BSTR ptext, BSTR * ctext, pEp_STATUS * status);
   180     STDMETHOD(encrypt_b)(SAFEARRAY * key_list, SAFEARRAY * ptext, BSTR * ctext, pEp_STATUS * status);
   181     STDMETHOD(trustword)(LONG value, BSTR lang, BSTR * word);
   182     STDMETHOD(trustwords)(BSTR fpr, BSTR lang, LONG max_words, BSTR * words);
   183     STDMETHOD(get_identity)(BSTR address, BSTR user_id, pEp_identity_s * ident);
   184     STDMETHOD(set_identity)(pEp_identity_s * ident);
   185     STDMETHOD(generate_keypair)(pEp_identity_s * ident, BSTR * fpr);
   186     STDMETHOD(delete_keypair)(BSTR fpr);
   187     STDMETHOD(import_key)(BSTR key_data);
   188     STDMETHOD(import_key_b)(SAFEARRAY * key_data);
   189     STDMETHOD(export_key)(BSTR fpr, BSTR * key_data);
   190     STDMETHOD(recv_key)(BSTR pattern);
   191     STDMETHOD(find_keys)(BSTR pattern, LPSAFEARRAY * key_list);
   192     STDMETHOD(send_key)(BSTR pattern);
   193     STDMETHOD(get_crashdump_log)(LONG maxlines, BSTR * log);
   194     STDMETHOD(get_engine_version)(BSTR * engine_version);
   195     STDMETHOD(get_languagelist)(BSTR * languages);
   196     STDMETHOD(get_phrase)(BSTR lang, LONG phrase_id, BSTR * phrase);
   197 
   198     // keymanagement API
   199 
   200     STDMETHOD(start_keyserver_lookup)();
   201     STDMETHOD(stop_keyserver_lookup)();
   202 
   203     STDMETHOD(examine_identity)(pEp_identity_s * ident);
   204     STDMETHOD(verify)(BSTR text, BSTR signature, LPSAFEARRAY * key_list, pEp_STATUS * verify_status);
   205     STDMETHOD(myself)(struct pEp_identity_s *ident, struct pEp_identity_s *result);
   206     STDMETHOD(update_identity)(struct pEp_identity_s *ident, struct pEp_identity_s *result);
   207     STDMETHOD(key_compromized)(struct pEp_identity_s *ident);
   208     STDMETHOD(key_reset_trust)(struct pEp_identity_s *ident);
   209     STDMETHOD(trust_personal_key)(struct pEp_identity_s *ident, struct pEp_identity_s *result);
   210 
   211     // Blacklist API
   212 
   213     STDMETHOD(blacklist_add)(BSTR fpr);
   214     STDMETHOD(blacklist_delete)(BSTR fpr);
   215     STDMETHOD(blacklist_is_listed)(BSTR fpr, VARIANT_BOOL *listed);
   216     STDMETHOD(blacklist_retrieve)(SAFEARRAY **blacklist);
   217 
   218     // Message API
   219 
   220     STDMETHOD(encrypt_message)(text_message * src, text_message * dst, SAFEARRAY * extra);
   221 	STDMETHOD(encrypt_message_ex)(text_message * src, text_message * dst, SAFEARRAY * extra, pEp_enc_format enc_format);
   222 	STDMETHOD(decrypt_message)(text_message * src, text_message * dst, SAFEARRAY ** keylist, pEp_color *rating);
   223     STDMETHOD(outgoing_message_color)(text_message *msg, pEp_color * pVal);
   224     STDMETHOD(identity_color)(pEp_identity_s * ident, pEp_color * pVal);
   225 
   226 	// Event callbacks
   227 
   228 	STDMETHOD(register_callbacks)(IpEpEngineCallbacks *new_callback);
   229 	STDMETHOD(unregister_callbacks)(IpEpEngineCallbacks *obsolete_callback);
   230 
   231 protected:
   232 	HRESULT Fire_MessageToSend(
   233 		/* [in] */ struct text_message *msg);
   234 
   235 	HRESULT Fire_ShowHandshake(
   236 		/* [in] */ struct pEp_identity_s *self,
   237 		/* [in] */ struct pEp_identity_s *partner,
   238 		/* [retval][out] */ sync_handshake_result_s *result);
   239 };
   240 
   241 OBJECT_ENTRY_AUTO(__uuidof(pEpEngine), CpEpEngine)