2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * @author Bartlomiej Grzelewski (b.grzelewski@samsung.com)
22 #ifndef _VALIDATION_CORE_CERTIFICATE_H_
23 #define _VALIDATION_CORE_CERTIFICATE_H_
32 #include <openssl/x509.h>
34 #include <vcore/exception.h>
36 #include <cert-service.h>
40 typedef struct x509_st X509;
42 typedef struct X509_name_st X509_NAME;
45 namespace ValidationCore {
49 typedef std::shared_ptr<Certificate> CertificatePtr;
50 typedef std::list<CertificatePtr> CertificateList;
52 class Certificate : public std::enable_shared_from_this<Certificate> {
56 VCORE_DECLARE_EXCEPTION_TYPE(ValidationCore::Exception, Base);
57 VCORE_DECLARE_EXCEPTION_TYPE(Base, OpensslInternalError);
58 VCORE_DECLARE_EXCEPTION_TYPE(Base, Base64Error);
61 typedef std::vector<unsigned char> Fingerprint;
64 typedef std::string AltName;
65 typedef std::set<AltName> AltNameSet;
84 explicit Certificate(X509 *cert);
86 explicit Certificate(cert_svc_mem_buff &buffer);
88 explicit Certificate(const std::string &data,
89 FormType form = FORM_DER);
93 // It returns pointer to internal structure!
94 // Do not free this pointer!
95 X509 *getX509(void) const;
97 std::string getDER(void) const;
99 std::string getBase64(void) const;
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;
106 Fingerprint getFingerprint(FingerprintType type) const;
108 // getName uses deprecated functions. Usage is strongly discouraged.
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;
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;
126 time_t getNotAfter() const;
128 time_t getNotBefore() const;
130 ASN1_TIME* getNotAfterTime() const;
132 ASN1_TIME* getNotBeforeTime() const;
135 * @brief This is convenient function.
137 * @details It can't be const function (however it doesn't change internal
138 * object). For details see #isSignedBy() function description.
143 * @brief Gets list of CRL distribution's points URIs
145 std::list<std::string> getCrlUris() const;
147 long getVersion() const;
150 std::string getSerialNumberString() const;
151 std::string getKeyUsageString() const;
152 std::string getSignatureAlgorithmString() const;
153 std::string getPublicKeyString() const;
158 * 2 - deprecated and not used
159 * 3 - older version of CA
160 * 4 - older version of CA
165 static std::string FingerprintToColonHex(
166 const Fingerprint &fingerprint);
169 X509_NAME *getX509Name(FieldType type) const;
172 std::string getField(FieldType type, int fieldNid) const;
176 } // namespace ValidationCore
178 #endif // _VALIDATION_CORE_CERTIFICATE_H_