Get Signer to take a PEM string rather than a filename.
[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 <libcxml/cxml.h>
27 #include <libxml++/libxml++.h>
28 #include <xmlsec/xmldsig.h>
29 #include <xmlsec/dl.h>
30 #include <xmlsec/app.h>
31 #include <xmlsec/crypto.h>
32 #include "compose.hpp"
33
34 using std::string;
35 using std::list;
36 using std::cout;
37 using boost::shared_ptr;
38 using namespace dcp;
39
40 /** Add a &lt;Signer&gt; and &lt;ds:Signature&gt; nodes to an XML node.
41  *  @param parent XML node to add to.
42  *  @param standard INTEROP or SMPTE.
43  */
44 void
45 Signer::sign (xmlpp::Element* parent, Standard standard) const
46 {
47         /* <Signer> */
48         
49         xmlpp::Element* signer = parent->add_child("Signer");
50         xmlpp::Element* data = signer->add_child("X509Data", "dsig");
51         xmlpp::Element* serial_element = data->add_child("X509IssuerSerial", "dsig");
52         serial_element->add_child("X509IssuerName", "dsig")->add_child_text (_certificates.leaf()->issuer());
53         serial_element->add_child("X509SerialNumber", "dsig")->add_child_text (_certificates.leaf()->serial());
54         data->add_child("X509SubjectName", "dsig")->add_child_text (_certificates.leaf()->subject());
55
56         /* <Signature> */
57         
58         xmlpp::Element* signature = parent->add_child("Signature", "dsig");
59         
60         xmlpp::Element* signed_info = signature->add_child ("SignedInfo", "dsig");
61         signed_info->add_child("CanonicalizationMethod", "dsig")->set_attribute ("Algorithm", "http://www.w3.org/TR/2001/REC-xml-c14n-20010315");
62         
63         if (standard == INTEROP) {
64                 signed_info->add_child("SignatureMethod", "dsig")->set_attribute("Algorithm", "http://www.w3.org/2000/09/xmldsig#rsa-sha1");
65         } else {
66                 signed_info->add_child("SignatureMethod", "dsig")->set_attribute("Algorithm", "http://www.w3.org/2001/04/xmldsig-more#rsa-sha256");
67         }
68         
69         xmlpp::Element* reference = signed_info->add_child("Reference", "dsig");
70         reference->set_attribute ("URI", "");
71
72         xmlpp::Element* transforms = reference->add_child("Transforms", "dsig");
73         transforms->add_child("Transform", "dsig")->set_attribute (
74                 "Algorithm", "http://www.w3.org/2000/09/xmldsig#enveloped-signature"
75                 );
76
77         reference->add_child("DigestMethod", "dsig")->set_attribute("Algorithm", "http://www.w3.org/2000/09/xmldsig#sha1");
78         /* This will be filled in by the signing later */
79         reference->add_child("DigestValue", "dsig");
80
81         signature->add_child("SignatureValue", "dsig");
82         signature->add_child("KeyInfo", "dsig");
83         add_signature_value (signature, "dsig");
84 }
85
86
87 /** Sign an XML node.
88  *
89  *  @param parent Node to sign.
90  *  @param ns Namespace to use for the signature XML nodes.
91  */
92 void
93 Signer::add_signature_value (xmlpp::Node* parent, string ns) const
94 {
95         cxml::Node cp (parent);
96         xmlpp::Node* key_info = cp.node_child("KeyInfo")->node ();
97
98         /* Add the certificate chain to the KeyInfo child node of parent */
99         list<shared_ptr<Certificate> > c = _certificates.leaf_to_root ();
100         for (list<shared_ptr<Certificate> >::iterator i = c.begin(); i != c.end(); ++i) {
101                 xmlpp::Element* data = key_info->add_child("X509Data", ns);
102                 
103                 {
104                         xmlpp::Element* serial = data->add_child("X509IssuerSerial", ns);
105                         serial->add_child("X509IssuerName", ns)->add_child_text((*i)->issuer ());
106                         serial->add_child("X509SerialNumber", ns)->add_child_text((*i)->serial ());
107                 }
108                 
109                 data->add_child("X509Certificate", ns)->add_child_text((*i)->certificate());
110         }
111
112         xmlSecDSigCtxPtr signature_context = xmlSecDSigCtxCreate (0);
113         if (signature_context == 0) {
114                 throw MiscError ("could not create signature context");
115         }
116
117         signature_context->signKey = xmlSecCryptoAppKeyLoadMemory (
118                 reinterpret_cast<const unsigned char *> (_key.c_str()), _key.size(), xmlSecKeyDataFormatPem, 0, 0, 0
119                 );
120         
121         if (signature_context->signKey == 0) {
122                 throw FileError ("could not load private key file", _key, 0);
123         }
124
125         /* XXX: set key name to the file name: is this right? */
126         if (xmlSecKeySetName (signature_context->signKey, reinterpret_cast<const xmlChar *> (_key.c_str())) < 0) {
127                 throw MiscError ("could not set key name");
128         }
129
130         int const r = xmlSecDSigCtxSign (signature_context, parent->cobj ());
131         if (r < 0) {
132                 throw MiscError (String::compose ("could not sign (%1)", r));
133         }
134
135         xmlSecDSigCtxDestroy (signature_context);
136 }