Refactor SignatureValidator and reduce interface headers
[platform/core/security/cert-svc.git] / vcore / src / vcore / CertificateCollection.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        CertificateCollection.h
18  * @author      Bartlomiej Grzelewski (b.grzelewski@samsung.com)
19  * @version     0.1
20  * @brief
21  */
22 #ifndef _VALIDATION_CORE_CERTIFICATECOLLECTION_H_
23 #define _VALIDATION_CORE_CERTIFICATECOLLECTION_H_
24
25 #include <list>
26 #include <string>
27 #include <map>
28
29 #include <vcore/exception.h>
30
31 #include <vcore/Certificate.h>
32
33 namespace ValidationCore {
34 /*
35  * This class is used to store Certificate Chain.
36  * It could serialize chain to std::string in base64 form.
37  * It could read chain written in base64 form.
38  * It could check if collection creates certificate chain.
39  */
40
41 class CertificateCollection {
42 public:
43     class Exception {
44     public:
45         VCORE_DECLARE_EXCEPTION_TYPE(ValidationCore::Exception, Base);
46         VCORE_DECLARE_EXCEPTION_TYPE(Base, WrongUsage);
47     };
48
49     CertificateCollection();
50
51     typedef CertificateList::const_iterator const_iterator;
52
53     /*
54      * Remove all certificates from collection.
55      */
56     void clear();
57
58     /*
59      * In current implemenation this function MUST success.
60      *
61      * This function will add new certificate to collection.
62      * This function DOES NOT clean collection.
63      */
64     void load(const CertificateList &certList);
65
66     /*
67      * This function will return false if base64 string is broken
68      * (is not encoded in base64 format) or one from certificate
69      * is broken (is not encoded in der format).
70      *
71      * This function will add new certificate to collection.
72      * This function DOES NOT clean collection.
73      */
74     bool load(const std::string &base64);
75
76     /*
77      * This function will return all certificates from
78      * collection encoded in base64 format.
79      */
80     std::string toBase64String() const;
81
82     /*
83      * This will return all certificate from collection.
84      */
85     CertificateList getCertificateList() const;
86
87     /*
88      * This function will return true if certificates
89      * in in this structure were sorted and create
90      * certificate chain.
91
92      * Note: You MUST sort certificates first.
93      */
94     bool isChain() const;
95
96     /*
97      * This function will return true if all certificate are
98      * able to create certificate chain.
99      *
100      * This function will sort certificates if collection
101      * is not sorted.
102      *
103      * Note: This function will make all iterators invalid.
104      */
105     bool sort();
106
107     /*
108      * Precondition : cert list sorted and has more than on cert.
109      * This function add root cert in cert list to complete cert chain
110      */
111     bool completeCertificateChain();
112
113     /*
114      * This function will return Certificate chain.
115      *
116      * First certificate on the list is EndEntity certificate.
117      *
118      * Last certificate on the list is RootCA certificate or
119      * CA certificate if RootCA is not present.
120      *
121      * Note: You MUST sort certificates first and
122      * check if certificates creates proper chain.
123      */
124     CertificateList getChain() const;
125
126     /*
127      * It returns size of certificate collection.
128      */
129     size_t size() const;
130
131     /*
132      * Return true if collection is empty.
133      */
134     bool empty() const;
135
136     /*
137      * This will return end iterator to internal collection.
138      *
139      * Note: this iterator will lose validity if you call non const
140      * method on CertificateCollection class.
141      */
142     const_iterator begin() const;
143
144     /*
145      * This will return end iterator to internal collection.
146      *
147      * Note: this iterator will lose validity if you call non const
148      * method on CertificateCollection class.
149      */
150     const_iterator end() const;
151
152     /*
153      * This function will return the last certificate from collection.
154      *
155      * Note: There is no point to call this function if certificate
156      * collection is not sorted!
157      */
158     CertificatePtr back() const;
159
160 protected:
161     void sortCollection(void);
162
163     enum CollectionStatus
164     {
165         // Certificate collection are not sorted in any way
166         COLLECTION_UNSORTED,
167         // Certificate collection creates certificate chain
168         COLLECTION_SORTED,
169         // Cerfificate collection is not able to create certificate chain
170         COLLECTION_CHAIN_BROKEN,
171     };
172
173     CollectionStatus m_collectionStatus;
174     CertificateList m_certList;
175 };
176
177 typedef std::list<CertificateCollection> CertificateCollectionList;
178
179 } // namespace ValidationCore
180
181 #endif // _VALIDATION_CORE_CERTIFICATECHAIN_H_