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