From 9026320cc48e9c200d33aae9e8f601f29542e186 Mon Sep 17 00:00:00 2001 From: Carl Hetherington Date: Thu, 17 Jul 2014 16:52:45 +0100 Subject: Get Signer to take a PEM string rather than a filename. --- src/signer.cc | 5 ++++- src/signer.h | 8 ++++---- src/util.cc | 20 ++++++++++++++++++++ src/util.h | 1 + 4 files changed, 29 insertions(+), 5 deletions(-) (limited to 'src') diff --git a/src/signer.cc b/src/signer.cc index 8f0114a2..a0d9912a 100644 --- a/src/signer.cc +++ b/src/signer.cc @@ -114,7 +114,10 @@ Signer::add_signature_value (xmlpp::Node* parent, string ns) const throw MiscError ("could not create signature context"); } - signature_context->signKey = xmlSecCryptoAppKeyLoad (_key.string().c_str(), xmlSecKeyDataFormatPem, 0, 0, 0); + signature_context->signKey = xmlSecCryptoAppKeyLoadMemory ( + reinterpret_cast (_key.c_str()), _key.size(), xmlSecKeyDataFormatPem, 0, 0, 0 + ); + if (signature_context->signKey == 0) { throw FileError ("could not load private key file", _key, 0); } diff --git a/src/signer.h b/src/signer.h index 6e258f8d..92745ff2 100644 --- a/src/signer.h +++ b/src/signer.h @@ -42,9 +42,9 @@ class Signer : public boost::noncopyable { public: /** @param c Certificate chain to sign with. - * @param k Key to sign with. + * @param k Key to sign with as a PEM-format string. */ - Signer (CertificateChain c, boost::filesystem::path k) + Signer (CertificateChain c, std::string k) : _certificates (c) , _key (k) {} @@ -60,8 +60,8 @@ private: /** Certificate chain to sign with */ CertificateChain _certificates; - /** Filename of signer key */ - boost::filesystem::path _key; + /** Key to sign with as a PEM-format string */ + std::string _key; }; } diff --git a/src/util.cc b/src/util.cc index 3d37454f..9758db9c 100644 --- a/src/util.cc +++ b/src/util.cc @@ -368,3 +368,23 @@ dcp::ids_equal (string a, string b) trim (b); return a == b; } + +string +dcp::file_to_string (boost::filesystem::path p) +{ + uintmax_t len = boost::filesystem::file_size (p); + char* c = new char[len]; + + FILE* f = fopen_boost (p, "r"); + if (!f) { + return ""; + } + + fread (c, 1, len, f); + fclose (f); + + string s (c); + delete[] c; + + return s; +} diff --git a/src/util.h b/src/util.h index d3f212c7..33fd79a3 100644 --- a/src/util.h +++ b/src/util.h @@ -88,6 +88,7 @@ extern void add_signer (xmlpp::Element* parent, CertificateChain const & certifi extern int base64_decode (std::string const & in, unsigned char* out, int out_length); extern boost::optional relative_to_root (boost::filesystem::path root, boost::filesystem::path file); extern FILE * fopen_boost (boost::filesystem::path, std::string); +extern std::string file_to_string (boost::filesystem::path); template std::list > -- cgit v1.2.3