diff --git a/UpdateStream.cc b/UpdateStream.cc index ddab026..e30b334 100644 --- a/UpdateStream.cc +++ b/UpdateStream.cc @@ -13,17 +13,6 @@ namespace pEp { namespace UpdateClient { using namespace CryptoPP; - PublicKey load_key(string filename) - { - ByteQueue queue; - FileSource file("public.der", true); - file.TransferTo(queue); - queue.MessageEnd(); - CryptoPP::RSA::PublicKey key; - key.Load(queue); - return key; - } - UpdateDevice::UpdateDevice(const product p) : _https({HTTPSDevice()}, ios::binary), _p(p), _key(32), _src(nullptr), _str(nullptr) diff --git a/UpdateStream.hh b/UpdateStream.hh index f79ed8c..5399912 100644 --- a/UpdateStream.hh +++ b/UpdateStream.hh @@ -25,7 +25,6 @@ namespace pEp { }; typedef CryptoPP::RSA::PublicKey PublicKey; - PublicKey load_key(string filename); class UpdateDevice : public io::source { HTTPSStream _https; diff --git a/downloadclient.cc b/downloadclient.cc index 97a3c31..208762e 100644 --- a/downloadclient.cc +++ b/downloadclient.cc @@ -8,6 +8,24 @@ namespace pEp { namespace UpdateClient { + PublicKey load_key(string filename) + { + CryptoPP::RSA::PublicKey key; + + try { + CryptoPP::ByteQueue queue; + CryptoPP::FileSource file("public.der", true); + file.TransferTo(queue); + queue.MessageEnd(); + key.Load(queue); + } + catch (exception&) { + throw CannotLoadKey(filename); + } + + return key; + } + string update(product p, PublicKey update_key) { UpdateStream us { UpdateDevice(p) }; diff --git a/downloadclient.hh b/downloadclient.hh index 2440b1c..8fd5646 100644 --- a/downloadclient.hh +++ b/downloadclient.hh @@ -14,7 +14,48 @@ namespace pEp { DownloadError() : runtime_error("download not possible") { } }; + struct CannotLoadKey : runtime_error { + CannotLoadKey(string filename) + : runtime_error("cannot load key from " + filename) + { + } + }; + + // load_key() - load BER encoded RSA public key + // + // params: + // filename (in) keyfile to load from + // + // returns: + // CryptoPP::RSA::PublicKey + // + // throws: + // CannotLoadKey if key cannot be loaded + + PublicKey load_key(string filename); + + // update() - download update for a product if available + // + // params: + // p (in) product to update with: + // name (in) product name + // url (in) download URL with GET param: + // hash serial number + // update_key (in) Update Key as CryptoPP::RSA::PublicKey + // + // returns: + // filename of downloaded file + // + // caveat: + // this function is using the current directory to download + // therefore, the current directory has to be writable + // + // see sample in test_updater.cc + string update(product p, PublicKey update_key); + + // update() - convenience function doing load_key() and update() + string update(product p, string keyfile); } } diff --git a/test_updater.cc b/test_updater.cc index 6f5f00a..7b80deb 100644 --- a/test_updater.cc +++ b/test_updater.cc @@ -10,28 +10,6 @@ using namespace pEp::UpdateClient; int main() { - /* - product p { "pEp for Something", "https://fdik.org/cgidownload?hash=23232323234242" }; - UpdateStream us { UpdateDevice(p) }; - - cout << "downloading...\n"; - - PublicKey update_key = load_key("public.der"); - try { - us->open(update_key); - } - catch (exception&) { } - - if (us->filename() != "") { - cout << "downloaded " << us->filename() << "\n"; - } - else { - stringstream str; - str << us.rdbuf(); - cout << str.str() << "\n"; - } - */ - cout << "downloading...\n"; product p { "pEp for Something", "https://fdik.org/cgidownload?hash=23232323234242" };