Merge "Revert "Fix prevent defect for locales"" into devel_3.0_main
[platform/framework/native/appfw.git] / src / security / pkcs / FSecPkcs_Pkcs08AttributeImpl.cpp
1 ///
2 // Copyright (c) 2013 Samsung Electronics Co., Ltd.
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 /**
18  * @file                FSecPkcs_Pkcs08AttributeValueImpl.cpp
19  * @brief               This is the implementation file for _Pkcs08AttributeValueImpl class.
20  *
21  * This header file contains the implementation of _Pkcs08AttributeValueImpl class.
22  *
23  */
24
25 #include <new>
26 #include <openssl/x509.h>
27 #include <openssl/objects.h>
28 #include <openssl/obj_mac.h>
29 #include <openssl/evp.h>
30 #include <unique_ptr.h>
31 #include <FBaseByteBuffer.h>
32 #include <FBaseResult.h>
33 #include <FBaseSysLog.h>
34 #include <FSecPkcsPkcs08Attribute.h>
35 #include <FSecPkcsPkcs08AttributeValue.h>
36
37 #include "FSecPkcs_PkcsUtility.h"
38 #include "FSecPkcs_Pkcs08AttributeImpl.h"
39
40
41 using namespace Tizen::Base;
42
43 namespace Tizen { namespace Security { namespace Pkcs
44 {
45
46 _Pkcs08AttributeImpl::_Pkcs08AttributeImpl(void)
47 {
48         //do nothing
49 }
50
51 _Pkcs08AttributeImpl::~_Pkcs08AttributeImpl(void)
52 {
53         if (__attributeValues.GetCount() > 0)
54                 __attributeValues.RemoveAll();
55         //do nothing
56 }
57
58 result
59 _Pkcs08AttributeImpl::Construct(const Tizen::Base::ByteBuffer& encodedData)
60 {
61         result r = E_SUCCESS;
62         int bufferLen = 0;
63         int value = 0;
64         int attrNid = 0;
65         byte* pTemp = {0, };
66         const byte* pBuffer = null;
67         ByteBuffer asn1TypeBuffer;
68         Tizen::Base::String attrOid = null;
69         X509_ATTRIBUTE* pAttribute = null;
70         ASN1_OBJECT* pObject = null;
71         ASN1_TYPE* pAsnType = null;
72
73         SysAssertf(__attributeType.GetLength() <= 0, "Already constructed. Calling Construct() twice or more on a same instance is not allowed for this class.");
74
75         pBuffer = encodedData.GetPointer();
76         SysTryReturnResult(NID_SEC_CRYPTO, pBuffer != null, E_INVALID_ARG, "The specified input parameter is invalid.");
77
78         bufferLen = encodedData.GetRemaining();
79         SysTryReturnResult(NID_SEC_CRYPTO, bufferLen > 0, E_INVALID_ARG, "The specified input parameter is invalid.");
80
81         pAttribute = d2i_X509_ATTRIBUTE(null, reinterpret_cast< const unsigned char** >(&pBuffer), bufferLen);
82         SysTryReturnResult(NID_SEC_CRYPTO, pAttribute != null, E_INVALID_ARG, "The specified input parameter is invalid.");
83
84         pObject = pAttribute->object;
85         SysTryCatch(NID_SEC_CRYPTO, pObject != null, r = E_INVALID_ARG, E_INVALID_ARG, "[E_INVALID_ARG] The specified input parameter is invalid.");
86
87         attrNid = OBJ_obj2nid(pObject);
88         attrOid = _PkcsUtility::ConvertToOid(attrNid);
89
90         r = GetLastResult();
91         SysTryCatch(NID_SEC_CRYPTO, !IsFailed(r), , r, "[%s] Failed to get the object id of an attribute.", GetErrorMessage(r));
92
93         __attributeType = attrOid;
94
95         for (int i = 0; i < X509_ATTRIBUTE_count(pAttribute); i++)
96         {
97
98                 pAsnType = sk_ASN1_TYPE_value(pAttribute->value.set, i);
99                 SysTryCatch(NID_SEC_CRYPTO, pAsnType != null, r = E_INVALID_ARG, E_INVALID_ARG, "[E_INVALID_ARG] The specified input parameter is invalid.");
100
101                 std::unique_ptr< Pkcs08AttributeValue > pNewObj(new (std::nothrow) Pkcs08AttributeValue());
102                 SysTryCatch(NID_SEC_CRYPTO, pNewObj != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
103
104                 value = i2d_ASN1_TYPE(pAsnType, &pTemp);
105                 SysTryCatch(NID_SEC_CRYPTO, value > 0, r = E_INVALID_ARG, E_INVALID_ARG, "[E_INVALID_ARG] The specified input parameter is invalid.");
106
107                 r = asn1TypeBuffer.Construct(value);
108                 SysTryCatch(NID_SEC_CRYPTO, !IsFailed(r), r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
109
110                 r = asn1TypeBuffer.SetArray(pTemp, 0, value);
111                 SysTryCatch(NID_SEC_CRYPTO, !IsFailed(r), r = E_INVALID_ARG, E_INVALID_ARG, "[E_INVALID_ARG] The specified input parameter is invalid.");
112
113                 asn1TypeBuffer.Flip();
114
115                 r = pNewObj->Construct(asn1TypeBuffer);
116                 SysTryCatch(NID_SEC_CRYPTO, !IsFailed(r), r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
117
118                 r = AddAttributeValue(*pNewObj.release());
119                 SysTryCatch(NID_SEC_CRYPTO, !IsFailed(r), , r, "[%s] Failed to add attribute", GetErrorMessage(r));
120
121         }
122
123 CATCH:
124
125         X509_ATTRIBUTE_free(pAttribute);
126         return r;
127 }
128
129 result
130 _Pkcs08AttributeImpl::Construct(const Tizen::Base::String& attrType)
131 {
132         //Variables local to function
133
134         SysAssertf(__attributeType.GetLength() <= 0, "Already constructed. Calling Construct() twice or more on a same instance is not allowed for this class.");
135
136         SysTryReturnResult(NID_SEC_CRYPTO, attrType.GetLength() > 0, E_INVALID_ARG, "[E_INVALID_ARG] The specified input parameter is invalid.");
137
138         //Initialize the Attribute instance with Attribute type
139         __attributeType = attrType;
140
141         //Return SUCCESS
142         return E_SUCCESS;
143 }
144
145
146 result
147 _Pkcs08AttributeImpl::AddAttributeValue(const Pkcs08AttributeValue& value)
148 {
149         SysAssertf(__attributeType.GetLength() > 0, "Not yet constructed. Construct () should be called before use.");
150
151         return __attributeValues.Add(value);
152 }
153
154 result
155 _Pkcs08AttributeImpl::RemoveAttributeValue(const Pkcs08AttributeValue& value)
156 {
157         SysAssertf(__attributeType.GetLength() > 0, "Not yet constructed. Construct () should be called before use.");
158
159         return __attributeValues.Remove(value);
160 }
161
162 Tizen::Base::String
163 _Pkcs08AttributeImpl::GetAttributeType(void) const
164 {
165         SysAssertf(__attributeType.GetLength() > 0, "Not yet constructed. Construct () should be called before use.");
166
167         return __attributeType;
168
169 }
170
171
172 const Tizen::Base::Collection::ArrayList&
173 _Pkcs08AttributeImpl::GetAttributeValues(void) const
174 {
175         SysAssertf(__attributeType.GetLength() > 0, "Not yet constructed. Construct () should be called before use.");
176
177         return __attributeValues;
178 }
179
180 ByteBuffer*
181 _Pkcs08AttributeImpl::GetEncodedDataN(void) const
182 {
183         result r = E_SUCCESS;
184         int attrNid = 0;
185         int count = 0;
186         int value = 0;
187         byte* pTemp = {0, };
188         X509_ATTRIBUTE* pAttribute;
189         std::unique_ptr< ByteBuffer > pAttrVal;
190         std::unique_ptr< ByteBuffer > pAttributeBuffer;
191         Pkcs08TagValue tagType;
192         ASN1_TYPE* pAsn1Type = null;
193         ASN1_STRING* pString = null;
194
195         ClearLastResult();
196
197         SysAssertf(__attributeType.GetLength() > 0, "Not yet constructed. Construct () should be called before use.");
198
199         pAttribute = X509_ATTRIBUTE_new();
200         SysTryReturn(NID_SEC_CRYPTO, pAttribute != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
201
202         attrNid = _PkcsUtility::ConvertToNid(__attributeType);
203
204         value = X509_ATTRIBUTE_set1_object(pAttribute, OBJ_nid2obj(attrNid));
205         SysTryCatch(NID_SEC_CRYPTO, value == 1, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] The method cannot proceed due to a severe system error.");
206
207         pAttribute->value.set = sk_ASN1_TYPE_new_null();
208         SysTryCatch(NID_SEC_CRYPTO, pAttribute != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
209
210         pAttribute->single = 0;
211
212         count = __attributeValues.GetCount();
213         SysTryCatch(NID_SEC_CRYPTO, count > 0, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] The method cannot proceed due to a severe system error.");
214
215         for (int i = 0; i < count; i++)
216         {
217                 Pkcs08AttributeValue* pObj = const_cast< Pkcs08AttributeValue* >(static_cast< const Pkcs08AttributeValue* >(__attributeValues.GetAt(i)));
218                 SysTryCatch(NID_SEC_CRYPTO, pObj != null, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] The method cannot proceed due to a severe system error.");
219
220                 tagType = pObj->GetTag();
221
222                 pAttrVal = std::unique_ptr< ByteBuffer >(new (std::nothrow) ByteBuffer());
223                 SysTryCatch(NID_SEC_CRYPTO, pAttrVal != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
224
225                 SysTryCatch(NID_SEC_CRYPTO, pObj->GetValue().GetRemaining() > 0, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
226
227                 r = pAttrVal->Construct(pObj->GetValue());
228                 SysTryCatch(NID_SEC_CRYPTO, !IsFailed(r), r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
229
230                 pString = ASN1_STRING_type_new(tagType);
231                 SysTryCatch(NID_SEC_CRYPTO, pString != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
232
233                 ASN1_STRING_set(pString, pAttrVal->GetPointer(), pAttrVal->GetRemaining());
234
235                 pAsn1Type = ASN1_TYPE_new();
236                 SysTryCatch(NID_SEC_CRYPTO, pString != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
237
238                 ASN1_TYPE_set(pAsn1Type, tagType, pString);
239
240                 value = sk_ASN1_TYPE_push(pAttribute->value.set, pAsn1Type);
241                 SysTryCatch(NID_SEC_CRYPTO, value > 0, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] The method cannot proceed due to a severe system error.");
242         }
243
244         count = X509_ATTRIBUTE_count(pAttribute);
245
246         value = i2d_X509_ATTRIBUTE(pAttribute, &pTemp);
247         SysTryCatch(NID_SEC_CRYPTO, value > 0, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] The method cannot proceed due to a severe system error.");
248
249         pAttributeBuffer = std::unique_ptr< ByteBuffer >(new (std::nothrow) ByteBuffer());
250         SysTryCatch(NID_SEC_CRYPTO, pAttributeBuffer, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
251
252         r = pAttributeBuffer->Construct(value);
253         SysTryCatch(NID_SEC_CRYPTO, !IsFailed(r), r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
254
255         r = pAttributeBuffer->SetArray(pTemp, 0, value);
256         SysTryCatch(NID_SEC_CRYPTO, !IsFailed(r), r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] The method cannot proceed due to a severe system error.");
257
258         pAttributeBuffer->Flip();
259
260 CATCH:
261
262         if (IsFailed(r))
263         {
264                 pAttributeBuffer.reset(null);
265         }
266         X509_ATTRIBUTE_free(pAttribute);
267         OPENSSL_free(pTemp);
268
269         return pAttributeBuffer.release();
270
271 }
272
273 _Pkcs08AttributeImpl*
274 _Pkcs08AttributeImpl::GetInstance(Pkcs08Attribute& pkcs08Attribute)
275 {
276         return pkcs08Attribute.__pPkcs08AttributeImpl;
277 }
278
279 const _Pkcs08AttributeImpl*
280 _Pkcs08AttributeImpl::GetInstance(const Pkcs08Attribute& pkcs08Attribute)
281 {
282         return pkcs08Attribute.__pPkcs08AttributeImpl;
283 }
284
285 } } }