2 Copyright (C) 2013-2014 Carl Hetherington <cth@carlh.net>
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 /** @file src/signer.cc
21 * @brief Signer class.
25 #include "exceptions.h"
26 #include "certificate_chain.h"
28 #include <libcxml/cxml.h>
29 #include <libxml++/libxml++.h>
30 #include <xmlsec/xmldsig.h>
31 #include <xmlsec/dl.h>
32 #include <xmlsec/app.h>
33 #include <xmlsec/crypto.h>
34 #include <openssl/pem.h>
35 #include "compose.hpp"
40 using boost::shared_ptr;
43 Signer::Signer (boost::filesystem::path openssl)
45 boost::filesystem::path directory = make_certificate_chain (openssl);
47 _certificates.add (dcp::Certificate (dcp::file_to_string (directory / "ca.self-signed.pem")));
48 _certificates.add (dcp::Certificate (dcp::file_to_string (directory / "intermediate.signed.pem")));
49 _certificates.add (dcp::Certificate (dcp::file_to_string (directory / "leaf.signed.pem")));
51 _key = dcp::file_to_string (directory / "leaf.key");
53 boost::filesystem::remove_all (directory);
57 /** Add a <Signer> and <ds:Signature> nodes to an XML node.
58 * @param parent XML node to add to.
59 * @param standard INTEROP or SMPTE.
62 Signer::sign (xmlpp::Element* parent, Standard standard) const
66 xmlpp::Element* signer = parent->add_child("Signer");
67 xmlpp::Element* data = signer->add_child("X509Data", "dsig");
68 xmlpp::Element* serial_element = data->add_child("X509IssuerSerial", "dsig");
69 serial_element->add_child("X509IssuerName", "dsig")->add_child_text (_certificates.leaf().issuer());
70 serial_element->add_child("X509SerialNumber", "dsig")->add_child_text (_certificates.leaf().serial());
71 data->add_child("X509SubjectName", "dsig")->add_child_text (_certificates.leaf().subject());
75 xmlpp::Element* signature = parent->add_child("Signature", "dsig");
77 xmlpp::Element* signed_info = signature->add_child ("SignedInfo", "dsig");
78 signed_info->add_child("CanonicalizationMethod", "dsig")->set_attribute ("Algorithm", "http://www.w3.org/TR/2001/REC-xml-c14n-20010315");
80 if (standard == INTEROP) {
81 signed_info->add_child("SignatureMethod", "dsig")->set_attribute("Algorithm", "http://www.w3.org/2000/09/xmldsig#rsa-sha1");
83 signed_info->add_child("SignatureMethod", "dsig")->set_attribute("Algorithm", "http://www.w3.org/2001/04/xmldsig-more#rsa-sha256");
86 xmlpp::Element* reference = signed_info->add_child("Reference", "dsig");
87 reference->set_attribute ("URI", "");
89 xmlpp::Element* transforms = reference->add_child("Transforms", "dsig");
90 transforms->add_child("Transform", "dsig")->set_attribute (
91 "Algorithm", "http://www.w3.org/2000/09/xmldsig#enveloped-signature"
94 reference->add_child("DigestMethod", "dsig")->set_attribute("Algorithm", "http://www.w3.org/2000/09/xmldsig#sha1");
95 /* This will be filled in by the signing later */
96 reference->add_child("DigestValue", "dsig");
98 signature->add_child("SignatureValue", "dsig");
99 signature->add_child("KeyInfo", "dsig");
100 add_signature_value (signature, "dsig");
104 /** Sign an XML node.
106 * @param parent Node to sign.
107 * @param ns Namespace to use for the signature XML nodes.
110 Signer::add_signature_value (xmlpp::Node* parent, string ns) const
112 cxml::Node cp (parent);
113 xmlpp::Node* key_info = cp.node_child("KeyInfo")->node ();
115 /* Add the certificate chain to the KeyInfo child node of parent */
116 CertificateChain::List c = _certificates.leaf_to_root ();
117 for (CertificateChain::List::iterator i = c.begin(); i != c.end(); ++i) {
118 xmlpp::Element* data = key_info->add_child("X509Data", ns);
121 xmlpp::Element* serial = data->add_child("X509IssuerSerial", ns);
122 serial->add_child("X509IssuerName", ns)->add_child_text (i->issuer ());
123 serial->add_child("X509SerialNumber", ns)->add_child_text (i->serial ());
126 data->add_child("X509Certificate", ns)->add_child_text (i->certificate());
129 xmlSecDSigCtxPtr signature_context = xmlSecDSigCtxCreate (0);
130 if (signature_context == 0) {
131 throw MiscError ("could not create signature context");
134 signature_context->signKey = xmlSecCryptoAppKeyLoadMemory (
135 reinterpret_cast<const unsigned char *> (_key.c_str()), _key.size(), xmlSecKeyDataFormatPem, 0, 0, 0
138 if (signature_context->signKey == 0) {
139 throw FileError ("could not load private key file", _key, 0);
142 /* XXX: set key name to the file name: is this right? */
143 if (xmlSecKeySetName (signature_context->signKey, reinterpret_cast<const xmlChar *> (_key.c_str())) < 0) {
144 throw MiscError ("could not set key name");
147 int const r = xmlSecDSigCtxSign (signature_context, parent->cobj ());
149 throw MiscError (String::compose ("could not sign (%1)", r));
152 xmlSecDSigCtxDestroy (signature_context);
156 Signer::valid () const
158 if (!_certificates.valid ()) {
162 BIO* bio = BIO_new_mem_buf (const_cast<char *> (_key.c_str ()), -1);
164 throw MiscError ("could not create memory BIO");
167 RSA* private_key = PEM_read_bio_RSAPrivateKey (bio, 0, 0, 0);
168 RSA* public_key = _certificates.leaf().public_key ();
169 bool const valid = !BN_cmp (private_key->n, public_key->n);