1bde60ac8a8e97f89171caad21bbf300ba79119e
[platform/core/security/cert-svc.git] / vcore / src / vcore / Certificate.h
1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  *    Licensed under the Apache License, Version 2.0 (the "License");
5  *    you may not use this file except in compliance with the License.
6  *    You may obtain a copy of the License at
7  *
8  *        http://www.apache.org/licenses/LICENSE-2.0
9  *
10  *    Unless required by applicable law or agreed to in writing, software
11  *    distributed under the License is distributed on an "AS IS" BASIS,
12  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *    See the License for the specific language governing permissions and
14  *    limitations under the License.
15  */
16 /*
17  * @file        Certificate.h
18  * @author      Bartlomiej Grzelewski (b.grzelewski@samsung.com)
19  * @version     1.1
20  * @brief
21  */
22 #ifndef _VALIDATION_CORE_CERTIFICATE_H_
23 #define _VALIDATION_CORE_CERTIFICATE_H_
24
25 #include <list>
26 #include <set>
27 #include <string>
28 #include <vector>
29 #include <ctime>
30 #include <memory>
31
32 #include <openssl/x509.h>
33
34 #include <vcore/exception.h>
35
36 #include <cert-service.h>
37
38 extern "C" {
39 struct x509_st;
40 typedef struct x509_st X509;
41 struct X509_name_st;
42 typedef struct X509_name_st X509_NAME;
43 }
44
45 namespace ValidationCore {
46
47 class Certificate;
48
49 typedef std::shared_ptr<Certificate> CertificatePtr;
50 typedef std::list<CertificatePtr> CertificateList;
51
52 class Certificate : public std::enable_shared_from_this<Certificate> {
53 public:
54     class Exception {
55     public:
56         VCORE_DECLARE_EXCEPTION_TYPE(ValidationCore::Exception, Base);
57         VCORE_DECLARE_EXCEPTION_TYPE(Base, OpensslInternalError);
58         VCORE_DECLARE_EXCEPTION_TYPE(Base, Base64Error);
59     };
60
61     typedef std::vector<unsigned char> Fingerprint;
62
63     // ascii string
64     typedef std::string AltName;
65     typedef std::set<AltName> AltNameSet;
66
67     enum FingerprintType
68     {
69         FINGERPRINT_MD5,
70         FINGERPRINT_SHA1
71     };
72     enum FieldType
73     {
74         FIELD_ISSUER,
75         FIELD_SUBJECT
76     };
77
78     enum FormType
79     {
80         FORM_DER,
81         FORM_BASE64
82     };
83
84     explicit Certificate(X509 *cert);
85
86     explicit Certificate(cert_svc_mem_buff &buffer);
87
88     explicit Certificate(const std::string &data,
89                          FormType form = FORM_DER);
90
91     ~Certificate();
92
93     // It returns pointer to internal structure!
94     // Do not free this pointer!
95     X509 *getX509(void) const;
96
97     std::string getDER(void) const;
98
99     std::string getBase64(void) const;
100
101     // This const is cheating here because you have no
102     // guarantee that X509_get_subject_name will not
103     // change X509 object.
104     bool isSignedBy(const CertificatePtr &parent) const;
105
106     Fingerprint getFingerprint(FingerprintType type) const;
107
108     // getName uses deprecated functions. Usage is strongly discouraged.
109     // utf8 string
110     std::string getOneLine(FieldType type = FIELD_SUBJECT) const;
111     std::string getCommonName(FieldType type = FIELD_SUBJECT) const;
112     std::string getCountryName(FieldType type = FIELD_SUBJECT) const;
113     std::string getStateOrProvinceName(FieldType type = FIELD_SUBJECT) const;
114     std::string getLocalityName(FieldType type = FIELD_SUBJECT) const;
115     std::string getOrganizationName(FieldType type = FIELD_SUBJECT) const;
116     std::string getOrganizationalUnitName(FieldType type = FIELD_SUBJECT) const;
117     std::string getEmailAddres(FieldType type = FIELD_SUBJECT) const;
118     std::string getOCSPURL() const;
119
120
121     // Openssl supports 9 types of alternative name filed.
122     // 4 of them are "string similar" types so it is possible
123     // to create more generic function.
124     AltNameSet getAlternativeNameDNS() const;
125
126     time_t getNotAfter() const;
127
128     time_t getNotBefore() const;
129
130     ASN1_TIME* getNotAfterTime() const;
131
132     ASN1_TIME* getNotBeforeTime() const;
133
134     /**
135      * @brief This is convenient function.
136      *
137      * @details It can't be const function (however it doesn't change internal
138      * object). For details see #isSignedBy() function description.
139      */
140     bool isRootCert();
141
142     /**
143      * @brief Gets list of CRL distribution's points URIs
144      */
145     std::list<std::string> getCrlUris() const;
146
147     long getVersion() const;
148
149     // utf8 string
150     std::string getSerialNumberString() const;
151     std::string getKeyUsageString() const;
152     std::string getSignatureAlgorithmString() const;
153     std::string getPublicKeyString() const;
154
155     /*
156      * 0 - not CA
157      * 1 - CA
158      * 2 - deprecated and not used
159      * 3 - older version of CA
160      * 4 - older version of CA
161      * 5 - netscape CA
162      */
163     int isCA() const;
164
165     static std::string FingerprintToColonHex(
166             const Fingerprint &fingerprint);
167
168 protected:
169     X509_NAME *getX509Name(FieldType type) const;
170
171     // utf8 string
172     std::string getField(FieldType type, int fieldNid) const;
173
174     X509 *m_x509;
175 };
176 } // namespace ValidationCore
177
178 #endif // _VALIDATION_CORE_CERTIFICATE_H_