Tweaks to management of crypto information.
[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         BIO* bio = BIO_new (BIO_s_mem ());
67         if (!bio) {
68                 throw MiscError ("could not create memory BIO");
69         }
70         
71         PEM_write_bio_X509 (bio, _certificate);
72
73         string s;
74         char* data;
75         long int const data_length = BIO_get_mem_data (bio, &data);
76         for (long int i = 0; i < data_length; ++i) {
77                 s += data[i];
78         }
79
80         BIO_free (bio);
81
82         boost::replace_all (s, "-----BEGIN CERTIFICATE-----\n", "");
83         boost::replace_all (s, "\n-----END CERTIFICATE-----\n", "");
84         return s;
85 }
86
87 string
88 Certificate::issuer () const
89 {
90         X509_NAME* n = X509_get_issuer_name (_certificate);
91         assert (n);
92
93         char b[256];
94         X509_NAME_oneline (n, b, 256);
95         return b;
96 }
97
98 string
99 Certificate::name_for_xml (string const & n)
100 {
101         stringstream x;
102         
103         vector<string> p;
104         boost::split (p, n, boost::is_any_of ("/"));
105         for (vector<string>::const_reverse_iterator i = p.rbegin(); i != p.rend(); ++i) {
106                 x << *i << ",";
107         }
108
109         string s = x.str();
110         boost::replace_all (s, "+", "\\+");
111
112         return s.substr(0, s.length() - 2);
113 }
114
115 string
116 Certificate::subject () const
117 {
118         X509_NAME* n = X509_get_subject_name (_certificate);
119         assert (n);
120
121         char b[256];
122         X509_NAME_oneline (n, b, 256);
123         return b;
124 }
125
126 string
127 Certificate::serial () const
128 {
129         ASN1_INTEGER* s = X509_get_serialNumber (_certificate);
130         assert (s);
131         
132         BIGNUM* b = ASN1_INTEGER_to_BN (s, 0);
133         char* c = BN_bn2dec (b);
134         BN_free (b);
135         
136         string st (c);
137         OPENSSL_free (c);
138
139         return st;
140 }
141
142 string
143 Certificate::thumbprint () const
144 {
145         uint8_t buffer[8192];
146         uint8_t* p = buffer;
147         i2d_X509_CINF (_certificate->cert_info, &p);
148         int const length = p - buffer;
149         if (length > 8192) {
150                 throw MiscError ("buffer too small to generate thumbprint");
151         }
152
153         SHA_CTX sha;
154         SHA1_Init (&sha);
155         SHA1_Update (&sha, buffer, length);
156         uint8_t digest[20];
157         SHA1_Final (digest, &sha);
158
159         char digest_base64[64];
160         return Kumu::base64encode (digest, 20, digest_base64, 64);
161 }
162
163 shared_ptr<Certificate>
164 CertificateChain::root () const
165 {
166         assert (!_certificates.empty());
167         return _certificates.front ();
168 }
169
170 shared_ptr<Certificate>
171 CertificateChain::leaf () const
172 {
173         assert (_certificates.size() >= 2);
174         return _certificates.back ();
175 }
176
177 list<shared_ptr<Certificate> >
178 CertificateChain::leaf_to_root () const
179 {
180         list<shared_ptr<Certificate> > c = _certificates;
181         c.reverse ();
182         return c;
183 }
184
185 void
186 CertificateChain::add (shared_ptr<Certificate> c)
187 {
188         _certificates.push_back (c);
189 }