diff options
| author | Carl Hetherington <cth@carlh.net> | 2014-07-17 16:52:45 +0100 |
|---|---|---|
| committer | Carl Hetherington <cth@carlh.net> | 2014-07-17 16:52:45 +0100 |
| commit | 9026320cc48e9c200d33aae9e8f601f29542e186 (patch) | |
| tree | ab1f49dd88bf2b1006ca42fd7f753f46aca03fa6 | |
| parent | 3d77daab7639c06d1cdbeb852559fc4be5671819 (diff) | |
Get Signer to take a PEM string rather than a filename.
| -rw-r--r-- | src/signer.cc | 5 | ||||
| -rw-r--r-- | src/signer.h | 8 | ||||
| -rw-r--r-- | src/util.cc | 20 | ||||
| -rw-r--r-- | src/util.h | 1 | ||||
| -rw-r--r-- | test/encryption_test.cc | 2 | ||||
| -rw-r--r-- | test/round_trip_test.cc | 2 |
6 files changed, 31 insertions, 7 deletions
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<const unsigned char *> (_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; +} @@ -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<boost::filesystem::path> 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 <class F, class T> std::list<boost::shared_ptr<T> > diff --git a/test/encryption_test.cc b/test/encryption_test.cc index 5abe53aa..600a4eae 100644 --- a/test/encryption_test.cc +++ b/test/encryption_test.cc @@ -76,7 +76,7 @@ BOOST_AUTO_TEST_CASE (encryption_test) shared_ptr<dcp::Signer> signer ( new dcp::Signer ( chain, - "test/ref/crypt/leaf.key" + dcp::file_to_string ("test/ref/crypt/leaf.key") ) ); diff --git a/test/round_trip_test.cc b/test/round_trip_test.cc index 311fe6d5..899734f4 100644 --- a/test/round_trip_test.cc +++ b/test/round_trip_test.cc @@ -54,7 +54,7 @@ BOOST_AUTO_TEST_CASE (round_trip_test) shared_ptr<dcp::Signer> signer ( new dcp::Signer ( chain, - "test/data/signer.key" + dcp::file_to_string ("test/data/signer.key") ) ); |
