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 create (make_certificate_chain (openssl));
48 Signer::Signer (boost::filesystem::path openssl,
50 string organisational_unit,
51 string root_common_name,
52 string intermediate_common_name,
53 string leaf_common_name
57 make_certificate_chain (
62 intermediate_common_name,
69 Signer::create (boost::filesystem::path directory)
71 _certificates.add (dcp::Certificate (dcp::file_to_string (directory / "ca.self-signed.pem")));
72 _certificates.add (dcp::Certificate (dcp::file_to_string (directory / "intermediate.signed.pem")));
73 _certificates.add (dcp::Certificate (dcp::file_to_string (directory / "leaf.signed.pem")));
75 _key = dcp::file_to_string (directory / "leaf.key");
77 boost::filesystem::remove_all (directory);
80 /** Add a <Signer> and <ds:Signature> nodes to an XML node.
81 * @param parent XML node to add to.
82 * @param standard INTEROP or SMPTE.
85 Signer::sign (xmlpp::Element* parent, Standard standard) const
89 xmlpp::Element* signer = parent->add_child("Signer");
90 xmlpp::Element* data = signer->add_child("X509Data", "dsig");
91 xmlpp::Element* serial_element = data->add_child("X509IssuerSerial", "dsig");
92 serial_element->add_child("X509IssuerName", "dsig")->add_child_text (_certificates.leaf().issuer());
93 serial_element->add_child("X509SerialNumber", "dsig")->add_child_text (_certificates.leaf().serial());
94 data->add_child("X509SubjectName", "dsig")->add_child_text (_certificates.leaf().subject());
98 xmlpp::Element* signature = parent->add_child("Signature", "dsig");
100 xmlpp::Element* signed_info = signature->add_child ("SignedInfo", "dsig");
101 signed_info->add_child("CanonicalizationMethod", "dsig")->set_attribute ("Algorithm", "http://www.w3.org/TR/2001/REC-xml-c14n-20010315");
103 if (standard == INTEROP) {
104 signed_info->add_child("SignatureMethod", "dsig")->set_attribute("Algorithm", "http://www.w3.org/2000/09/xmldsig#rsa-sha1");
106 signed_info->add_child("SignatureMethod", "dsig")->set_attribute("Algorithm", "http://www.w3.org/2001/04/xmldsig-more#rsa-sha256");
109 xmlpp::Element* reference = signed_info->add_child("Reference", "dsig");
110 reference->set_attribute ("URI", "");
112 xmlpp::Element* transforms = reference->add_child("Transforms", "dsig");
113 transforms->add_child("Transform", "dsig")->set_attribute (
114 "Algorithm", "http://www.w3.org/2000/09/xmldsig#enveloped-signature"
117 reference->add_child("DigestMethod", "dsig")->set_attribute("Algorithm", "http://www.w3.org/2000/09/xmldsig#sha1");
118 /* This will be filled in by the signing later */
119 reference->add_child("DigestValue", "dsig");
121 signature->add_child("SignatureValue", "dsig");
122 signature->add_child("KeyInfo", "dsig");
123 add_signature_value (signature, "dsig");
127 /** Sign an XML node.
129 * @param parent Node to sign.
130 * @param ns Namespace to use for the signature XML nodes.
133 Signer::add_signature_value (xmlpp::Node* parent, string ns) const
135 cxml::Node cp (parent);
136 xmlpp::Node* key_info = cp.node_child("KeyInfo")->node ();
138 /* Add the certificate chain to the KeyInfo child node of parent */
139 CertificateChain::List c = _certificates.leaf_to_root ();
140 for (CertificateChain::List::iterator i = c.begin(); i != c.end(); ++i) {
141 xmlpp::Element* data = key_info->add_child("X509Data", ns);
144 xmlpp::Element* serial = data->add_child("X509IssuerSerial", ns);
145 serial->add_child("X509IssuerName", ns)->add_child_text (i->issuer ());
146 serial->add_child("X509SerialNumber", ns)->add_child_text (i->serial ());
149 data->add_child("X509Certificate", ns)->add_child_text (i->certificate());
152 xmlSecDSigCtxPtr signature_context = xmlSecDSigCtxCreate (0);
153 if (signature_context == 0) {
154 throw MiscError ("could not create signature context");
157 signature_context->signKey = xmlSecCryptoAppKeyLoadMemory (
158 reinterpret_cast<const unsigned char *> (_key.c_str()), _key.size(), xmlSecKeyDataFormatPem, 0, 0, 0
161 if (signature_context->signKey == 0) {
162 throw FileError ("could not load private key file", _key, 0);
165 /* XXX: set key name to the file name: is this right? */
166 if (xmlSecKeySetName (signature_context->signKey, reinterpret_cast<const xmlChar *> (_key.c_str())) < 0) {
167 throw MiscError ("could not set key name");
170 int const r = xmlSecDSigCtxSign (signature_context, parent->cobj ());
172 throw MiscError (String::compose ("could not sign (%1)", r));
175 xmlSecDSigCtxDestroy (signature_context);
179 Signer::valid () const
181 if (!_certificates.valid ()) {
185 BIO* bio = BIO_new_mem_buf (const_cast<char *> (_key.c_str ()), -1);
187 throw MiscError ("could not create memory BIO");
190 RSA* private_key = PEM_read_bio_RSAPrivateKey (bio, 0, 0, 0);
191 RSA* public_key = _certificates.leaf().public_key ();
192 bool const valid = !BN_cmp (private_key->n, public_key->n);