Fix internal tests
[platform/core/security/cert-svc.git] / vcore / 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 all certificates from
68      * collection encoded in base64 format.
69      */
70     std::string toBase64String() const;
71
72     /*
73      * This will return all certificate from collection.
74      */
75     CertificateList getCertificateList() const;
76
77     /*
78      * This function will return true if certificates
79      * in in this structure were sorted and create
80      * certificate chain.
81
82      * Note: You MUST sort certificates first.
83      */
84     bool isChain() const;
85
86     /*
87      * This function will return true if all certificate are
88      * able to create certificate chain.
89      *
90      * This function will sort certificates if collection
91      * is not sorted.
92      *
93      * Note: This function will make all iterators invalid.
94      */
95     bool sort();
96
97     /*
98      * Precondition : cert list sorted and has more than on cert.
99      * This function add root cert in cert list to complete cert chain
100      */
101     bool completeCertificateChain();
102
103     /*
104      * This function will return Certificate chain.
105      *
106      * First certificate on the list is EndEntity certificate.
107      *
108      * Last certificate on the list is RootCA certificate or
109      * CA certificate if RootCA is not present.
110      *
111      * Note: You MUST sort certificates first and
112      * check if certificates creates proper chain.
113      */
114     CertificateList getChain() const;
115
116     /*
117      * It returns size of certificate collection.
118      */
119     size_t size() const;
120
121     /*
122      * Return true if collection is empty.
123      */
124     bool empty() const;
125
126     /*
127      * This will return end iterator to internal collection.
128      *
129      * Note: this iterator will lose validity if you call non const
130      * method on CertificateCollection class.
131      */
132     const_iterator begin() const;
133
134     /*
135      * This will return end iterator to internal collection.
136      *
137      * Note: this iterator will lose validity if you call non const
138      * method on CertificateCollection class.
139      */
140     const_iterator end() const;
141
142     /*
143      * This function will return the last certificate from collection.
144      *
145      * Note: There is no point to call this function if certificate
146      * collection is not sorted!
147      */
148     CertificatePtr back() const;
149
150 protected:
151     void sortCollection(void);
152
153     enum CollectionStatus
154     {
155         // Certificate collection are not sorted in any way
156         COLLECTION_UNSORTED,
157         // Certificate collection creates certificate chain
158         COLLECTION_SORTED,
159         // Cerfificate collection is not able to create certificate chain
160         COLLECTION_CHAIN_BROKEN,
161     };
162
163     CollectionStatus m_collectionStatus;
164     CertificateList m_certList;
165 };
166
167 typedef std::list<CertificateCollection> CertificateCollectionList;
168
169 } // namespace ValidationCore
170
171 #endif // _VALIDATION_CORE_CERTIFICATECHAIN_H_