Rename certificates.{cc,h} -> certificate.{cc,h}.
[libdcp.git] / src / certificate.h
1 /*
2     Copyright (C) 2012-2014 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 /** @file  src/certificates.h
21  *  @brief Certificate and CertificateChain classes.
22  */
23
24 #ifndef LIBDCP_CERTIFICATES_H
25 #define LIBDCP_CERTIFICATES_H
26
27 #undef X509_NAME
28 #include <openssl/x509.h>
29 #include <boost/filesystem.hpp>
30 #include <string>
31 #include <list>
32
33 class certificates;
34
35 namespace xmlpp {
36         class Element;
37 }
38
39 namespace dcp {
40
41 /** @class Certificate
42  *  @brief A wrapper for an X509 certificate.
43  *
44  *  This class can take a Certificate from a string or an OpenSSL X509 object.
45  */
46 class Certificate
47 {
48 public:
49         Certificate ()
50                 : _certificate (0)
51                 , _public_key (0)
52         {}
53
54         Certificate (std::string);
55         Certificate (X509 *);
56         Certificate (Certificate const &);
57         ~Certificate ();
58
59         Certificate& operator= (Certificate const &);
60
61         std::string certificate (bool with_begin_end = false) const;
62         std::string serial () const;
63
64         std::string issuer () const;
65
66         std::string subject () const;
67         std::string subject_common_name () const;
68         std::string subject_organization_name () const;
69         std::string subject_organizational_unit_name () const;
70
71         X509* x509 () const {
72                 return _certificate;
73         }
74
75         RSA* public_key () const;
76
77         std::string thumbprint () const;
78
79 private:
80         void read_string (std::string);
81
82         static std::string name_for_xml (X509_NAME *);
83         static std::string asn_to_utf8 (ASN1_STRING *);
84         static std::string get_name_part (X509_NAME *, int);
85
86         X509* _certificate;
87         mutable RSA* _public_key;
88 };
89
90 bool operator== (Certificate const & a, Certificate const & b);
91 bool operator< (Certificate const & a, Certificate const & b);
92 std::ostream& operator<< (std::ostream&s, Certificate const & c);
93
94 }
95
96 #endif