Merge master; at least partially.
[libdcp.git] / src / certificates.cc
1 /*
2     Copyright (C) 2012 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 <sstream>
21 #include <vector>
22 #include <boost/algorithm/string.hpp>
23 #include <openssl/x509.h>
24 #include <openssl/ssl.h>
25 #include <openssl/asn1.h>
26 #include <libxml++/nodes/element.h>
27 #include "KM_util.h"
28 #include "certificates.h"
29 #include "exceptions.h"
30
31 using std::list;
32 using std::string;
33 using std::stringstream;
34 using std::vector;
35 using boost::shared_ptr;
36 using namespace libdcp;
37
38 /** @param c X509 certificate, which this object will take ownership of */
39 Certificate::Certificate (X509* c)
40         : _certificate (c)
41 {
42         
43 }
44
45 Certificate::Certificate (string const & filename)
46         : _certificate (0)
47 {
48         FILE* f = fopen (filename.c_str(), "r");
49         if (!f) {
50                 throw FileError ("could not open file", filename);
51         }
52         
53         if (!PEM_read_X509 (f, &_certificate, 0, 0)) {
54                 throw MiscError ("could not read X509 certificate");
55         }
56 }
57
58 Certificate::~Certificate ()
59 {
60         X509_free (_certificate);
61 }
62
63 string
64 Certificate::certificate () const
65 {
66         assert (_certificate);
67         
68         BIO* bio = BIO_new (BIO_s_mem ());
69         if (!bio) {
70                 throw MiscError ("could not create memory BIO");
71         }
72         
73         PEM_write_bio_X509 (bio, _certificate);
74
75         string s;
76         char* data;
77         long int const data_length = BIO_get_mem_data (bio, &data);
78         for (long int i = 0; i < data_length; ++i) {
79                 s += data[i];
80         }
81
82         BIO_free (bio);
83
84         boost::replace_all (s, "-----BEGIN CERTIFICATE-----\n", "");
85         boost::replace_all (s, "\n-----END CERTIFICATE-----\n", "");
86         return s;
87 }
88
89 string
90 Certificate::issuer () const
91 {
92         assert (_certificate);
93         
94         X509_NAME* n = X509_get_issuer_name (_certificate);
95         assert (n);
96
97         char b[256];
98         X509_NAME_oneline (n, b, 256);
99         return b;
100 }
101
102 string
103 Certificate::name_for_xml (string const & n)
104 {
105         stringstream x;
106         
107         vector<string> p;
108         boost::split (p, n, boost::is_any_of ("/"));
109         for (vector<string>::const_reverse_iterator i = p.rbegin(); i != p.rend(); ++i) {
110                 x << *i << ",";
111         }
112
113         string s = x.str();
114         boost::replace_all (s, "+", "\\+");
115
116         return s.substr(0, s.length() - 2);
117 }
118
119 string
120 Certificate::subject () const
121 {
122         assert (_certificate);
123
124         X509_NAME* n = X509_get_subject_name (_certificate);
125         assert (n);
126
127         char b[256];
128         X509_NAME_oneline (n, b, 256);
129         return b;
130 }
131
132 string
133 Certificate::serial () const
134 {
135         assert (_certificate);
136
137         ASN1_INTEGER* s = X509_get_serialNumber (_certificate);
138         assert (s);
139         
140         BIGNUM* b = ASN1_INTEGER_to_BN (s, 0);
141         char* c = BN_bn2dec (b);
142         BN_free (b);
143         
144         string st (c);
145         OPENSSL_free (c);
146
147         return st;
148 }
149
150 string
151 Certificate::thumbprint () const
152 {
153         assert (_certificate);
154         
155         uint8_t buffer[8192];
156         uint8_t* p = buffer;
157         i2d_X509_CINF (_certificate->cert_info, &p);
158         int const length = p - buffer;
159         if (length > 8192) {
160                 throw MiscError ("buffer too small to generate thumbprint");
161         }
162
163         SHA_CTX sha;
164         SHA1_Init (&sha);
165         SHA1_Update (&sha, buffer, length);
166         uint8_t digest[20];
167         SHA1_Final (digest, &sha);
168
169         char digest_base64[64];
170         return Kumu::base64encode (digest, 20, digest_base64, 64);
171 }
172
173 shared_ptr<Certificate>
174 CertificateChain::root () const
175 {
176         assert (!_certificates.empty());
177         return _certificates.front ();
178 }
179
180 shared_ptr<Certificate>
181 CertificateChain::leaf () const
182 {
183         assert (_certificates.size() >= 2);
184         return _certificates.back ();
185 }
186
187 list<shared_ptr<Certificate> >
188 CertificateChain::leaf_to_root () const
189 {
190         list<shared_ptr<Certificate> > c = _certificates;
191         c.reverse ();
192         return c;
193 }
194
195 void
196 CertificateChain::add (shared_ptr<Certificate> c)
197 {
198         _certificates.push_back (c);
199 }