Fix bad frees if exceptions are thrown by constructors.
[libdcp.git] / src / signer.cc
1 /*
2     Copyright (C) 2013 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 #include <libxml++/libxml++.h>
21 #include <xmlsec/xmldsig.h>
22 #include <xmlsec/dl.h>
23 #include <xmlsec/app.h>
24 #include "signer.h"
25 #include "exceptions.h"
26
27 using std::string;
28 using std::list;
29 using boost::shared_ptr;
30 using namespace libdcp;
31
32 /** @param signer_key Filename of private key to sign with */
33 void
34 Signer::sign (xmlpp::Element* parent, bool interop) const
35 {
36         add_signer (parent, "dsig");
37
38         xmlpp::Element* signature = parent->add_child("Signature", "dsig");
39         
40         {
41                 xmlpp::Element* signed_info = signature->add_child ("SignedInfo", "dsig");
42                 signed_info->add_child("CanonicalizationMethod", "dsig")->set_attribute ("Algorithm", "http://www.w3.org/TR/2001/REC-xml-c14n-20010315");
43
44                 if (interop) {
45                         signed_info->add_child("SignatureMethod", "dsig")->set_attribute("Algorithm", "http://www.w3.org/2000/09/xmldsig#rsa-sha1");
46                 } else {
47                         signed_info->add_child("SignatureMethod", "dsig")->set_attribute("Algorithm", "http://www.w3.org/2001/04/xmldsig-more#rsa-sha256");
48                 }
49                 
50                 {
51                         xmlpp::Element* reference = signed_info->add_child("Reference", "dsig");
52                         reference->set_attribute ("URI", "");
53                         {
54                                 xmlpp::Element* transforms = reference->add_child("Transforms", "dsig");
55                                 transforms->add_child("Transform", "dsig")->set_attribute (
56                                         "Algorithm", "http://www.w3.org/2000/09/xmldsig#enveloped-signature"
57                                         );
58                         }
59                         reference->add_child("DigestMethod", "dsig")->set_attribute("Algorithm", "http://www.w3.org/2000/09/xmldsig#sha1");
60                         /* This will be filled in by the signing later */
61                         reference->add_child("DigestValue", "dsig");
62                 }
63         }
64         
65         add_signature_value (signature, "dsig");
66 }
67
68
69 /** Sign an XML node.  This function takes a certificate chain (to prove that the sender is bona fide) and
70  *  a private key with which to sign the node.
71  *
72  *  @param parent Node to sign.
73  *  @param certificates Certificate chain for the signer.
74  *  @param signer_key Filename of the private key of the signer.
75  *  @param ns Namespace to use for the signature XML nodes.
76  */
77 void
78 Signer::add_signature_value (xmlpp::Element* parent, string ns) const
79 {
80         parent->add_child("SignatureValue", ns);
81
82         /* Add the certificate chain to a KeyInfo child node of parent */
83         xmlpp::Element* key_info = parent->add_child("KeyInfo", ns);
84         list<shared_ptr<Certificate> > c = _certificates.leaf_to_root ();
85         for (list<shared_ptr<Certificate> >::iterator i = c.begin(); i != c.end(); ++i) {
86                 xmlpp::Element* data = key_info->add_child("X509Data", ns);
87                 
88                 {
89                         xmlpp::Element* serial = data->add_child("X509IssuerSerial", ns);
90                         serial->add_child("X509IssuerName", ns)->add_child_text((*i)->issuer ());
91                         serial->add_child("X509SerialNumber", ns)->add_child_text((*i)->serial ());
92                 }
93                 
94                 data->add_child("X509Certificate", ns)->add_child_text((*i)->certificate());
95         }
96
97         xmlSecDSigCtxPtr signature_context = xmlSecDSigCtxCreate (0);
98         if (signature_context == 0) {
99                 throw MiscError ("could not create signature context");
100         }
101
102         signature_context->signKey = xmlSecCryptoAppKeyLoad (_key.c_str(), xmlSecKeyDataFormatPem, 0, 0, 0);
103         if (signature_context->signKey == 0) {
104                 throw FileError ("could not load private key file", _key);
105         }
106
107         /* XXX: set key name to the file name: is this right? */
108         if (xmlSecKeySetName (signature_context->signKey, reinterpret_cast<const xmlChar *> (_key.c_str())) < 0) {
109                 throw MiscError ("could not set key name");
110         }
111
112         if (xmlSecDSigCtxSign (signature_context, parent->cobj ()) < 0) {
113                 throw MiscError ("could not sign");
114         }
115
116         xmlSecDSigCtxDestroy (signature_context);
117 }
118
119 void
120 Signer::add_signer (xmlpp::Element* parent, string ns) const
121 {
122         xmlpp::Element* signer = parent->add_child("Signer");
123
124         {
125                 xmlpp::Element* data = signer->add_child("X509Data", ns);
126                 
127                 {
128                         xmlpp::Element* serial_element = data->add_child("X509IssuerSerial", ns);
129                         serial_element->add_child("X509IssuerName", ns)->add_child_text (_certificates.leaf()->issuer());
130                         serial_element->add_child("X509SerialNumber", ns)->add_child_text (_certificates.leaf()->serial());
131                 }
132                 
133                 data->add_child("X509SubjectName", ns)->add_child_text (_certificates.leaf()->subject());
134         }
135 }