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