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