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