Refactor SignatureValidator and reduce interface headers
[platform/core/security/cert-svc.git] / vcore / src / vcore / SSLContainers.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 _SSLCONTAINERS_H
17 #define _SSLCONTAINERS_H
18
19 #include <openssl/x509v3.h>
20 #include <openssl/ocsp.h>
21
22 /*
23  * default deleter functor with no overloaded operator()
24  */
25 template <typename T>
26 struct MySSLFree {};
27
28 /*
29  * macro for defining custom deleters for openssl structs
30  * usage DECLARE_DELETER(OpenSSLType)
31  */
32 #define DECLARE_DELETER(Type) template<> \
33     struct MySSLFree <Type>                  \
34     {                                        \
35         void operator() (Type* p)            \
36         {                                    \
37             Type ## _free(p);                  \
38         }                                    \
39                                          \
40     };
41
42 /*
43  * declare custom deleter for X509 structs
44  */
45 DECLARE_DELETER(X509)
46 /*
47  * declare custom deleter for OCSP_REQUEST structs
48  */
49 DECLARE_DELETER(OCSP_REQUEST)
50 /*
51  * declare custom deleter for OCSP_RESPONSE structs
52  */
53 DECLARE_DELETER(OCSP_RESPONSE)
54 /*
55  * declare custom deleter for OCSP_CERTID structs
56  */
57 DECLARE_DELETER(OCSP_CERTID)
58 /*
59  * declare custom deleter for OCSP_BASICRESP structs
60  */
61 DECLARE_DELETER(OCSP_BASICRESP)
62 /*
63  * declare custom deleter for X509_STORE structs
64  */
65 DECLARE_DELETER(X509_STORE)
66
67 /*
68  * undef it, so anyone could use that macro name
69  */
70 #undef DECLARE_DELETER
71
72 /*
73  * OpenSSL smart container
74  * usage SSLSmartContainer <OpenSSLType> smartptr = ptrToOpenSSLType
75  * remember to add OpenSSLType to macro list few lines above
76  */
77 template <typename T, typename deleter = MySSLFree<T> >
78 class SSLSmartContainer
79 {
80   public:
81     SSLSmartContainer() : m_pData(NULL)
82     {
83     }
84
85     /*
86      * explicit constructor, we don't want any auto casting
87      */
88     explicit SSLSmartContainer(T* pData)
89     {
90         m_pData = pData;
91     }
92
93     /*
94      * overloaded assignment operator
95      */
96     SSLSmartContainer & operator=(SSLSmartContainer& pContainer)
97     {
98         /*
99          * check if no assignment was done before
100          */
101         if (this != &pContainer) {
102             // if so, free internal data
103             deleter ssl_free;
104             ssl_free(m_pData);
105
106             // and assign new
107             m_pData = pContainer.m_pData;
108
109             pContainer.m_pData = NULL;
110         }
111
112         return *this;
113     }
114
115     SSLSmartContainer & operator=(T* pData)
116     {
117         /*
118          * check if no assignment was done before
119          */
120         if (m_pData != pData) {
121             // if so, free internal data
122             deleter ssl_free;
123             ssl_free(m_pData);
124
125             // and assign new
126             m_pData = pData;
127         }
128
129         return *this;
130     }
131
132     ~SSLSmartContainer()
133     {
134         deleter ssl_free;
135         ssl_free(m_pData);
136     }
137
138     /*
139      * overloaded operators for standardptr - like usage
140      */
141     SSLSmartContainer & operator*()
142     {
143         return *m_pData;
144     }
145     SSLSmartContainer* operator->()
146     {
147         return m_pData;
148     }
149
150     /*
151      * auto cast to T operator
152      */
153     operator T *() const { return m_pData;
154     }
155
156     /*
157      * detachs internal pointer from smart pointer
158      */
159     T* DetachPtr()
160     {
161         T* pData = m_pData;
162         m_pData = NULL;
163         return pData;
164     }
165
166   private:
167     /*
168      * blocked assignment from another types operator
169      */
170     template <typename S>
171     T & operator = (S& pContainer)
172     {
173         return *this;
174     }
175
176     /*
177      * internal data
178      */
179     T* m_pData;
180 };
181
182 #endif /* _SSLCONTAINERS_H */
183