diff --git a/CpEpEngine.cpp b/CpEpEngine.cpp index 27fa205..0e91a25 100644 --- a/CpEpEngine.cpp +++ b/CpEpEngine.cpp @@ -2250,3 +2250,15 @@ STDMETHODIMP CpEpEngine::EnableEchoProtocolInOutgoingMessageRatingPreview(VARIAN return S_OK; } +STDMETHODIMP CpEpEngine::ConfigMediaKey(BSTR pattern, BSTR fpr) noexcept +{ + PEP_STATUS status = PEP_STATUS_OK; + string _pattern = utf8_string(pattern); + string _fpr = utf8_string(fpr); + stringpair_list_t* media_key_map = new_stringpair_list(new_stringpair(_pattern.c_str(), _fpr.c_str())); + status = config_media_keys(session(), media_key_map); + free_stringpair_list(media_key_map); + return status; +} + + diff --git a/CpEpEngine.h b/CpEpEngine.h index 49dc795..a97ca49 100644 --- a/CpEpEngine.h +++ b/CpEpEngine.h @@ -301,6 +301,7 @@ public: // Echo protocol & Media key STDMETHOD(EnableEchoProtocol)(VARIANT_BOOL enable); STDMETHOD(EnableEchoProtocolInOutgoingMessageRatingPreview)(VARIANT_BOOL enable); + STDMETHOD(ConfigMediaKey)(BSTR pattern, BSTR fpr); }; diff --git a/LocalProvisioning.cpp b/LocalProvisioning.cpp index 10411cc..069ede3 100644 --- a/LocalProvisioning.cpp +++ b/LocalProvisioning.cpp @@ -16,7 +16,7 @@ void LocalProvisioning::Run() std::wstring isProvisioned = provisionRegKey.GetValue(ProvisioningIsProvisionedRegKey, L"False"); std::wstring localFolder = provisionRegKey.GetValue(ProvisioningLocalFolderRegKey, - defaultProvisioningPath()); + LocalProvisioning::defaultProvisioningPath()); std::wstring provisioning_file_name = provisionRegKey.GetValue(ProvisioningFileNameRegKey, DefaultProvisionPackage); @@ -30,7 +30,7 @@ void LocalProvisioning::Run() std::filesystem::path target_path = provisioning_path / L"package"; if (!std::filesystem::exists(provisioning_path)) - create_dir_if_not_exists(provisioning_path); + LocalProvisioning::create_dir_if_not_exists(provisioning_path); if (!std::filesystem::exists(pkg_path)) // there is no package to provision return; diff --git a/LocalProvisioning.h b/LocalProvisioning.h index b0830fb..6957408 100644 --- a/LocalProvisioning.h +++ b/LocalProvisioning.h @@ -27,17 +27,26 @@ namespace pEp /// /// bool convert_bool(const std::wstring& in); + + public: + + /// + /// Run provisioning procedure + /// + void Run(); + /// /// Get default provisioning directory /// /// - std::wstring defaultProvisioningPath(); - bool create_dir_if_not_exists(const std::filesystem::path& path); - public: + static std::wstring defaultProvisioningPath(); + /// - /// Run provisioning procedure + /// Creates a directory /// - void Run(); + /// + /// + static bool create_dir_if_not_exists(const std::filesystem::path& path); }; diff --git a/MediaKeyManager.cpp b/MediaKeyManager.cpp new file mode 100644 index 0000000..10aab91 --- /dev/null +++ b/MediaKeyManager.cpp @@ -0,0 +1,122 @@ +#include "stdafx.h" +#include "MediaKeyManager.h" +#include +#include + +namespace pEp +{ + +namespace fs = std::filesystem; + +std::string MediaKeyManager::trim_chars(const std::string& in, const std::string& chars) +{ + std::string part; + size_t start = in.find_first_not_of(chars); + part = (start == std::string::npos) ? "" : in.substr(start); + size_t end = part.find_last_not_of(chars); + return (end == std::string::npos) ? "" : part.substr(0, end + 1); +} + + +std::string MediaKeyManager::load_text_file_contents(const std::filesystem::path& p) +{ + std::ifstream t(p); + std::stringstream buffer; + buffer << t.rdbuf(); + return buffer.str(); +} + +void MediaKeyManager::save_fpr_stamp(const std::filesystem::path& p, const std::string& fpr) +{ + std::ofstream outfile; + outfile.open(p / stamp_filename, std::ios_base::trunc); + outfile << fpr; +} + +void MediaKeyManager::add_registry_pattern(const std::string& pattern, const std::string& fpr) +{ + +} + + +PEP_STATUS MediaKeyManager::config_media_key(const std::string& pattern, const std::string& fpr) +{ + PEP_STATUS status = PEP_STATUS_OK; + stringpair_list_t* media_key_map = new_stringpair_list(new_stringpair(pattern.c_str(), fpr.c_str())); + status = config_media_keys(session, media_key_map); + free_stringpair_list(media_key_map); + return status; +} + + + +std::string MediaKeyManager::import_media_key(const std::filesystem::path& p) +{ + std::string k = load_text_file_contents(p); + identity_list *l; + import_key(session, k.c_str(), k.size(), &l); + return l->ident->fpr; +} + + + + +void MediaKeyManager::load_keys_in_dir(const std::filesystem::path& p) +{ + fs::path privkey_path = p / privkey_filename; + fs::path pubkey_path = p / pubkey_filename; + fs::path pattern_path = p / pattern_filename; + + if (fs::exists(privkey_path) && fs::exists(pubkey_path) && fs::exists(pattern_path)) + { + std::string fpr_pri = import_media_key(privkey_path); + std::string fpr_pub = import_media_key(pubkey_path); + if (fpr_pri.compare(fpr_pub) != 0) + { + // TODO log + std::cout << "FPRs do not match\n"; + delete_keypair(session, fpr_pri.c_str()); + delete_keypair(session, fpr_pub.c_str()); + } + else + { + std::string pattern = trim_chars(load_text_file_contents(pattern_path)); + PEP_STATUS status = config_media_key(pattern, fpr_pri); + if (status != PEP_STATUS_OK) + { + // TODO log + std::cout << "Error in config_media_key: " << status << "\n"; + } + else + { + save_fpr_stamp(p, fpr_pri); + } + } + } + +} + +void MediaKeyManager::ImportKeys() +{ + fs::path provisioning_path(LocalProvisioning::defaultProvisioningPath()); + fs::path media_key_path = provisioning_path / MediaKeyDir; + + LocalProvisioning::create_dir_if_not_exists(media_key_path); + + for (const fs::directory_entry& dir_entry : fs::directory_iterator(media_key_path)) + { + std::cerr << dir_entry << '\n'; + if (dir_entry.is_directory()) + { + load_keys_in_dir(dir_entry); + } + } + +} + +void MediaKeyManager::ConfigureMediaKeyMap() +{ +} + + +} // namespace pEp \ No newline at end of file diff --git a/MediaKeyManager.h b/MediaKeyManager.h new file mode 100644 index 0000000..ab1cf95 --- /dev/null +++ b/MediaKeyManager.h @@ -0,0 +1,46 @@ +#pragma once +#include "LocalProvisioning.h" +#include + +namespace pEp +{ + #define MediaKeyRegKey _T("Software\\pEp\\Provisioning\\Mediakeys") + #define MediaKeyDir _T("Mediakeys") + + class MediaKeyManager + { + + inline static const std::wstring pubkey_filename = L"public_key.asc"; + inline static const std::wstring privkey_filename = L"private_key.asc"; + inline static const std::wstring pattern_filename = L"pattern.txt"; + inline static const std::wstring stamp_filename = L"stamp.txt"; + + PEP_SESSION session; + + void load_keys_in_dir(const std::filesystem::path& p); + std::string import_media_key(const std::filesystem::path& p); + + std::string trim_chars(const std::string& in, const std::string& chars = " \n\r\t\f\v"); + std::string load_text_file_contents(const std::filesystem::path& p); + void add_registry_pattern(const std::string& pattern, const std::string& fpr); + PEP_STATUS config_media_key(const std::string& pattern, const std::string& fpr); + void save_fpr_stamp(const std::filesystem::path& p, const std::string& fpr); + + public: + + MediaKeyManager(PEP_SESSION session) noexcept : session(session) + { + } + + void ImportKeys(); + + void ConfigureMediaKeyMap(); + + + + + + + }; + +} \ No newline at end of file diff --git a/pEpCOMServerAdapter.cpp b/pEpCOMServerAdapter.cpp index 42032d4..b8ce2ef 100644 --- a/pEpCOMServerAdapter.cpp +++ b/pEpCOMServerAdapter.cpp @@ -10,7 +10,7 @@ #include "LocalJSONAdapter.h" #include "CMainWindow.h" #include "LocalProvisioning.h" - +#include "MediaKeyManager.h" #include @@ -107,6 +107,10 @@ extern "C" int WINAPI _tWinMain(HINSTANCE hInstance, HINSTANCE /*hPrevInstance*/ assert(mw); } + pEp::MediaKeyManager media_key_manager(first_session); + media_key_manager.ImportKeys(); + media_key_manager.ConfigureMediaKeyMap(); + auto rv = _AtlModule.WinMain(nShowCmd); if (ljs) { diff --git a/pEpCOMServerAdapter.idl b/pEpCOMServerAdapter.idl index 837cb54..e756b06 100644 --- a/pEpCOMServerAdapter.idl +++ b/pEpCOMServerAdapter.idl @@ -566,7 +566,7 @@ interface IpEpEngine : IUnknown { // Enable Echo protocol [id(72)] HRESULT EnableEchoProtocol([in] VARIANT_BOOL enable); [id(73)] HRESULT EnableEchoProtocolInOutgoingMessageRatingPreview([in] VARIANT_BOOL enable); - + [id(74)] HRESULT ConfigMediaKey([in] BSTR pattern, [in] BSTR fpr); }; [ diff --git a/pEpCOMServerAdapter.vcxproj b/pEpCOMServerAdapter.vcxproj index 87d78f8..7632e90 100644 --- a/pEpCOMServerAdapter.vcxproj +++ b/pEpCOMServerAdapter.vcxproj @@ -144,6 +144,7 @@ + false @@ -177,6 +178,7 @@ + diff --git a/pEpCOMServerAdapter.vcxproj.filters b/pEpCOMServerAdapter.vcxproj.filters index eab4f65..02b6d3b 100644 --- a/pEpCOMServerAdapter.vcxproj.filters +++ b/pEpCOMServerAdapter.vcxproj.filters @@ -55,6 +55,9 @@ Source Files + + Source Files + @@ -102,6 +105,9 @@ Header Files + + Header Files + diff --git a/stdafx.h b/stdafx.h index c152662..c3e15c2 100644 --- a/stdafx.h +++ b/stdafx.h @@ -67,6 +67,7 @@ #include #include #include +#include #include "utf8_helper.h" #include "pEp_utility.h"