d048ca4c9a93518992b9c0de64abe3984b5ee869
[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         boost::filesystem::path directory = make_certificate_chain (openssl);
46
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")));
50
51         _key = dcp::file_to_string (directory / "leaf.key");
52
53         boost::filesystem::remove_all (directory);
54 }
55         
56
57 /** Add a &lt;Signer&gt; and &lt;ds:Signature&gt; nodes to an XML node.
58  *  @param parent XML node to add to.
59  *  @param standard INTEROP or SMPTE.
60  */
61 void
62 Signer::sign (xmlpp::Element* parent, Standard standard) const
63 {
64         /* <Signer> */
65         
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());
72
73         /* <Signature> */
74         
75         xmlpp::Element* signature = parent->add_child("Signature", "dsig");
76         
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");
79         
80         if (standard == INTEROP) {
81                 signed_info->add_child("SignatureMethod", "dsig")->set_attribute("Algorithm", "http://www.w3.org/2000/09/xmldsig#rsa-sha1");
82         } else {
83                 signed_info->add_child("SignatureMethod", "dsig")->set_attribute("Algorithm", "http://www.w3.org/2001/04/xmldsig-more#rsa-sha256");
84         }
85         
86         xmlpp::Element* reference = signed_info->add_child("Reference", "dsig");
87         reference->set_attribute ("URI", "");
88
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"
92                 );
93
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");
97
98         signature->add_child("SignatureValue", "dsig");
99         signature->add_child("KeyInfo", "dsig");
100         add_signature_value (signature, "dsig");
101 }
102
103
104 /** Sign an XML node.
105  *
106  *  @param parent Node to sign.
107  *  @param ns Namespace to use for the signature XML nodes.
108  */
109 void
110 Signer::add_signature_value (xmlpp::Node* parent, string ns) const
111 {
112         cxml::Node cp (parent);
113         xmlpp::Node* key_info = cp.node_child("KeyInfo")->node ();
114
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);
119                 
120                 {
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 ());
124                 }
125                 
126                 data->add_child("X509Certificate", ns)->add_child_text (i->certificate());
127         }
128
129         xmlSecDSigCtxPtr signature_context = xmlSecDSigCtxCreate (0);
130         if (signature_context == 0) {
131                 throw MiscError ("could not create signature context");
132         }
133
134         signature_context->signKey = xmlSecCryptoAppKeyLoadMemory (
135                 reinterpret_cast<const unsigned char *> (_key.c_str()), _key.size(), xmlSecKeyDataFormatPem, 0, 0, 0
136                 );
137         
138         if (signature_context->signKey == 0) {
139                 throw FileError ("could not load private key file", _key, 0);
140         }
141
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");
145         }
146
147         int const r = xmlSecDSigCtxSign (signature_context, parent->cobj ());
148         if (r < 0) {
149                 throw MiscError (String::compose ("could not sign (%1)", r));
150         }
151
152         xmlSecDSigCtxDestroy (signature_context);
153 }
154
155 bool
156 Signer::valid () const
157 {
158         if (!_certificates.valid ()) {
159                 return false;
160         }
161
162         BIO* bio = BIO_new_mem_buf (const_cast<char *> (_key.c_str ()), -1);
163         if (!bio) {
164                 throw MiscError ("could not create memory BIO");
165         }
166         
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);
170         BIO_free (bio);
171
172         return valid;
173 }