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