Get Signer to take a PEM string rather than a filename.
authorCarl Hetherington <cth@carlh.net>
Thu, 17 Jul 2014 15:52:45 +0000 (16:52 +0100)
committerCarl Hetherington <cth@carlh.net>
Thu, 17 Jul 2014 15:52:45 +0000 (16:52 +0100)
src/signer.cc
src/signer.h
src/util.cc
src/util.h
test/encryption_test.cc
test/round_trip_test.cc

index 8f0114a2b0e0a9c6c944345b60e707e002130890..a0d9912ad7974cef0c0a21a3e1a158a2bd38b0a0 100644 (file)
@@ -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);
        }
index 6e258f8d5dba8bd87aef6f4e2742f22b3da10a0d..92745ff2b105f5c7f5c241112d28c6be63613dc5 100644 (file)
@@ -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;
 };
 
 }
index 3d37454f58a8b0061d7c69972ae06fef4ac4c450..9758db9cf030fae331ffbb4d1b9072cb6131774f 100644 (file)
@@ -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;
+}
index d3f212c7531fbd51785e1856e40eaa309a5dfc96..33fd79a327ca230d4b80e5500a546dac12889199 100644 (file)
@@ -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> >
index 5abe53aaa515e7d7ed3bf9db1f6517ca52c98dae..600a4eaeb129d0505dce90e9731073706d54de26 100644 (file)
@@ -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")
                        )
                );
 
index 311fe6d5b917ac4e02020c94fff969110ce5fb71..899734f48ac1893c7fe96c8884450f43e1e461f8 100644 (file)
@@ -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")
                        )
                );