9b9a6c4731da1db30778460f9e8f87acf2f0beb7
[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      * This function will return Certificate chain.
109      *
110      * First certificate on the list is EndEntity certificate.
111      *
112      * Last certificate on the list is RootCA certificate or
113      * CA certificate if RootCA is not present.
114      *
115      * Note: You MUST sort certificates first and
116      * check if certificates creates proper chain.
117      */
118     CertificateList getChain() const;
119
120     /*
121      * It returns size of certificate collection.
122      */
123     size_t size() const;
124
125     /*
126      * Return true if collection is empty.
127      */
128     bool empty() const;
129
130     /*
131      * This will return end iterator to internal collection.
132      *
133      * Note: this iterator will lose validity if you call non const
134      * method on CertificateCollection class.
135      */
136     const_iterator begin() const;
137
138     /*
139      * This will return end iterator to internal collection.
140      *
141      * Note: this iterator will lose validity if you call non const
142      * method on CertificateCollection class.
143      */
144     const_iterator end() const;
145
146     /*
147      * This function will return the last certificate from collection.
148      *
149      * Note: There is no point to call this function if certificate
150      * collection is not sorted!
151      */
152     CertificatePtr back() const;
153
154 protected:
155     void sortCollection(void);
156
157     enum CollectionStatus
158     {
159         // Certificate collection are not sorted in any way
160         COLLECTION_UNSORTED,
161         // Certificate collection creates certificate chain
162         COLLECTION_SORTED,
163         // Cerfificate collection is not able to create certificate chain
164         COLLECTION_CHAIN_BROKEN,
165     };
166
167     CollectionStatus m_collectionStatus;
168     CertificateList m_certList;
169 };
170
171 typedef std::list<CertificateCollection> CertificateCollectionList;
172
173 } // namespace ValidationCore
174
175 #endif // _VALIDATION_CORE_CERTIFICATECHAIN_H_