Fix code and tests so that SubtitleString::v_position is between 0 and 1 (not a perce...
[libdcp.git] / src / signer.cc
1 /*
2     Copyright (C) 2013-2014 Carl Hetherington <cth@carlh.net>
3
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.
8
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.
13
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.
17
18 */
19
20 /** @file  src/signer.cc
21  *  @brief Signer class.
22  */
23
24 #include "signer.h"
25 #include "exceptions.h"
26 #include "certificate_chain.h"
27 #include "util.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"
36
37 using std::string;
38 using std::list;
39 using std::cout;
40 using boost::shared_ptr;
41 using namespace dcp;
42
43 Signer::Signer (boost::filesystem::path openssl)
44 {
45         create (make_certificate_chain (openssl));
46 }
47
48 Signer::Signer (boost::filesystem::path openssl,
49                 string organisation,
50                 string organisational_unit,
51                 string root_common_name,
52                 string intermediate_common_name,
53                 string leaf_common_name
54         )
55 {
56         create (
57                 make_certificate_chain (
58                         openssl,
59                         organisation,
60                         organisational_unit,
61                         root_common_name,
62                         intermediate_common_name,
63                         leaf_common_name
64                         )
65                 );
66 }
67
68 void
69 Signer::create (boost::filesystem::path directory)
70 {
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")));
74
75         _key = dcp::file_to_string (directory / "leaf.key");
76
77         boost::filesystem::remove_all (directory);
78 }
79
80 /** Add a &lt;Signer&gt; and &lt;ds:Signature&gt; nodes to an XML node.
81  *  @param parent XML node to add to.
82  *  @param standard INTEROP or SMPTE.
83  */
84 void
85 Signer::sign (xmlpp::Element* parent, Standard standard) const
86 {
87         /* <Signer> */
88         
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());
95
96         /* <Signature> */
97         
98         xmlpp::Element* signature = parent->add_child("Signature", "dsig");
99         
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");
102         
103         if (standard == INTEROP) {
104                 signed_info->add_child("SignatureMethod", "dsig")->set_attribute("Algorithm", "http://www.w3.org/2000/09/xmldsig#rsa-sha1");
105         } else {
106                 signed_info->add_child("SignatureMethod", "dsig")->set_attribute("Algorithm", "http://www.w3.org/2001/04/xmldsig-more#rsa-sha256");
107         }
108         
109         xmlpp::Element* reference = signed_info->add_child("Reference", "dsig");
110         reference->set_attribute ("URI", "");
111
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"
115                 );
116
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");
120
121         signature->add_child("SignatureValue", "dsig");
122         signature->add_child("KeyInfo", "dsig");
123         add_signature_value (signature, "dsig");
124 }
125
126
127 /** Sign an XML node.
128  *
129  *  @param parent Node to sign.
130  *  @param ns Namespace to use for the signature XML nodes.
131  */
132 void
133 Signer::add_signature_value (xmlpp::Node* parent, string ns) const
134 {
135         cxml::Node cp (parent);
136         xmlpp::Node* key_info = cp.node_child("KeyInfo")->node ();
137
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);
142                 
143                 {
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 ());
147                 }
148                 
149                 data->add_child("X509Certificate", ns)->add_child_text (i->certificate());
150         }
151
152         xmlSecDSigCtxPtr signature_context = xmlSecDSigCtxCreate (0);
153         if (signature_context == 0) {
154                 throw MiscError ("could not create signature context");
155         }
156
157         signature_context->signKey = xmlSecCryptoAppKeyLoadMemory (
158                 reinterpret_cast<const unsigned char *> (_key.c_str()), _key.size(), xmlSecKeyDataFormatPem, 0, 0, 0
159                 );
160         
161         if (signature_context->signKey == 0) {
162                 throw FileError ("could not load private key file", _key, 0);
163         }
164
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");
168         }
169
170         int const r = xmlSecDSigCtxSign (signature_context, parent->cobj ());
171         if (r < 0) {
172                 throw MiscError (String::compose ("could not sign (%1)", r));
173         }
174
175         xmlSecDSigCtxDestroy (signature_context);
176 }
177
178 bool
179 Signer::valid () const
180 {
181         if (!_certificates.valid ()) {
182                 return false;
183         }
184
185         BIO* bio = BIO_new_mem_buf (const_cast<char *> (_key.c_str ()), -1);
186         if (!bio) {
187                 throw MiscError ("could not create memory BIO");
188         }
189         
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);
193         BIO_free (bio);
194
195         return valid;
196 }