Add a somewhat dubious constructor.
[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 ()
46 {
47         X509_free (_certificate);
48 }
49
50 string
51 Certificate::certificate () const
52 {
53         BIO* bio = BIO_new (BIO_s_mem ());
54         if (!bio) {
55                 throw MiscError ("could not create memory BIO");
56         }
57         
58         PEM_write_bio_X509 (bio, _certificate);
59
60         string s;
61         char* data;
62         long int const data_length = BIO_get_mem_data (bio, &data);
63         for (long int i = 0; i < data_length; ++i) {
64                 s += data[i];
65         }
66
67         BIO_free (bio);
68
69         boost::replace_all (s, "-----BEGIN CERTIFICATE-----\n", "");
70         boost::replace_all (s, "\n-----END CERTIFICATE-----\n", "");
71         return s;
72 }
73
74 string
75 Certificate::issuer () const
76 {
77         X509_NAME* n = X509_get_issuer_name (_certificate);
78         assert (n);
79
80         char b[256];
81         X509_NAME_oneline (n, b, 256);
82         return b;
83 }
84
85 string
86 Certificate::name_for_xml (string const & n)
87 {
88         stringstream x;
89         
90         vector<string> p;
91         boost::split (p, n, boost::is_any_of ("/"));
92         for (vector<string>::const_reverse_iterator i = p.rbegin(); i != p.rend(); ++i) {
93                 x << *i << ",";
94         }
95
96         string s = x.str();
97         boost::replace_all (s, "+", "\\+");
98
99         return s.substr(0, s.length() - 2);
100 }
101
102 string
103 Certificate::subject () const
104 {
105         X509_NAME* n = X509_get_subject_name (_certificate);
106         assert (n);
107
108         char b[256];
109         X509_NAME_oneline (n, b, 256);
110         return b;
111 }
112
113 string
114 Certificate::serial () const
115 {
116         ASN1_INTEGER* s = X509_get_serialNumber (_certificate);
117         assert (s);
118         
119         BIGNUM* b = ASN1_INTEGER_to_BN (s, 0);
120         char* c = BN_bn2dec (b);
121         BN_free (b);
122         
123         string st (c);
124         OPENSSL_free (c);
125
126         return st;
127 }
128
129 string
130 Certificate::thumbprint () const
131 {
132         uint8_t buffer[8192];
133         uint8_t* p = buffer;
134         i2d_X509_CINF (_certificate->cert_info, &p);
135         int const length = p - buffer;
136         if (length > 8192) {
137                 throw MiscError ("buffer too small to generate thumbprint");
138         }
139
140         SHA_CTX sha;
141         SHA1_Init (&sha);
142         SHA1_Update (&sha, buffer, length);
143         uint8_t digest[20];
144         SHA1_Final (digest, &sha);
145
146         char digest_base64[64];
147         return Kumu::base64encode (digest, 20, digest_base64, 64);
148 }
149
150 /** @param filename Text file of PEM-format certificates,
151  *  in the order:
152  *
153  *  1. self-signed root certificate
154  *  2. intermediate certificate signed by root certificate
155  *  ...
156  *  n. leaf certificate signed by previous intermediate.
157  */
158
159 CertificateChain::CertificateChain (string const & filename)
160 {
161         FILE* f = fopen (filename.c_str(), "r");
162         if (!f) {
163                 throw FileError ("could not open file", filename);
164         }
165         
166         while (1) {
167                 X509* c = 0;
168                 if (!PEM_read_X509 (f, &c, 0, 0)) {
169                         break;
170                 }
171
172                 _certificates.push_back (shared_ptr<Certificate> (new Certificate (c)));
173         }
174 }
175
176 shared_ptr<Certificate>
177 CertificateChain::root () const
178 {
179         assert (!_certificates.empty());
180         return _certificates.front ();
181 }
182
183 shared_ptr<Certificate>
184 CertificateChain::leaf () const
185 {
186         assert (_certificates.size() >= 2);
187         return _certificates.back ();
188 }
189
190 list<shared_ptr<Certificate> >
191 CertificateChain::leaf_to_root () const
192 {
193         list<shared_ptr<Certificate> > c = _certificates;
194         c.reverse ();
195         return c;
196 }
197