Fix the boiler plate codes
[platform/framework/native/appfw.git] / src / security / FSecRsaKeyConverter.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                FSecRsaKeyConverter.cpp
19  * @brief               This is the implementation file for RsaKeyConverter class.
20  */
21
22 #include <unique_ptr.h>
23 #include <openssl/bn.h>
24 #include <openssl/evp.h>
25 #include <openssl/dh.h>
26 #include <openssl/rsa.h>
27 #include <openssl/x509.h>
28 #include <openssl/pem.h>
29 #include <FBaseResult.h>
30 #include <FBaseErrors.h>
31 #include <FSecIPublicKey.h>
32 #include <FSecIPrivateKey.h>
33 #include <FSecPrivateKey.h>
34 #include <FSecPublicKey.h>
35 #include <FBaseSysLog.h>
36 #include <FSecRsaKeyConverter.h>
37
38 using namespace Tizen::Base;
39 using namespace Tizen::Security;
40
41
42 namespace Tizen { namespace Security
43 {
44
45 RsaKeyConverter::RsaKeyConverter(void)
46 {
47
48 }
49
50 RsaKeyConverter::~RsaKeyConverter(void)
51 {
52 }
53
54 ByteBuffer*
55 RsaKeyConverter::ConvertPrivateKeyFormatN(RsaKeyFormat format, const IPrivateKey& key)
56 {
57         result r = E_SUCCESS;
58         byte* pBuffer = null;
59         const byte* pKey = null;
60         std::unique_ptr< ByteBuffer > pOutBuffer;
61         BIO* pBio = null;
62         EVP_PKEY* pEvpKey = null;
63         int ret = 0;
64         int keyLen = 0;
65         bool isPemFormat = false;
66
67         SysTryReturn(NID_SEC, format != RSA_KEY_FORMAT_UNKNOWN, null, E_INVALID_ARG, " [E_INVALID_ARG] The specified input parameter is invalid.");
68
69         std::unique_ptr< ByteBuffer > pPrivateKey(key.GetEncodedN());
70         SysTryReturn(NID_SEC, pPrivateKey != null, null, r, "[%s] Failed to get the public key", GetErrorMessage(r));
71
72         pKey = pPrivateKey->GetPointer();
73         SysTryReturn(NID_SEC, pKey != null, null, E_INVALID_ARG, "[E_INVALID_ARG] The specified input parameter is invalid.");
74
75         keyLen = pPrivateKey->GetRemaining();
76         SysTryReturn(NID_SEC, keyLen > 0, null, E_INVALID_ARG, "[E_INVALID_ARG] The specified input parameter is invalid.");
77
78         pEvpKey = d2i_PrivateKey(EVP_PKEY_RSA, null, &pKey, keyLen);
79
80         if (pEvpKey == null)
81         {
82                 PKCS8_PRIV_KEY_INFO* pPrivKeyInfo = d2i_PKCS8_PRIV_KEY_INFO(null, &pKey, keyLen);
83                 if (pPrivKeyInfo != null)
84                 {
85                         pEvpKey = EVP_PKCS82PKEY(pPrivKeyInfo);
86                         PKCS8_PRIV_KEY_INFO_free(pPrivKeyInfo);
87                 }
88
89         }
90         if (pEvpKey == null)
91         {
92                 pBio = BIO_new(BIO_s_mem());
93                 SysTryReturn(NID_SEC, pBio != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
94
95                 int readCount = BIO_write(pBio, (const void*) pKey, keyLen);
96                 SysTryCatch(NID_SEC, readCount > 0, r = E_INVALID_ARG, E_INVALID_ARG, "[E_INVALID_ARG] The specified input parameter is invalid.");
97
98                 pEvpKey = PEM_read_bio_PrivateKey(pBio, null, 0, null);
99                 if (pEvpKey != null)
100                 {
101                         isPemFormat = true;
102                 }
103         }
104
105         SysTryCatch(NID_SEC, pEvpKey != null, r = E_INVALID_ARG, E_INVALID_ARG, "[E_INVALID_ARG] The specified input parameter is invalid.");
106
107         switch (format)
108         {
109         case RSA_KEY_FORMAT_PKCS01_PRIVATE_KEY:
110         {
111                 ret = i2d_PrivateKey(pEvpKey, &pBuffer);
112                 SysTryCatch(NID_SEC, ret != -1, r = E_INVALID_ARG, E_INVALID_ARG, "[E_INVALID_ARG] The specified input parameter is invalid.");
113         }
114         break;
115
116         case RSA_KEY_FORMAT_PKCS08_PRIVATE_KEY:
117         {
118                 PKCS8_PRIV_KEY_INFO* pPrivKeyInfo = EVP_PKEY2PKCS8(pEvpKey);
119                 SysTryCatch(NID_SEC, pPrivKeyInfo != null, r = E_INVALID_ARG, E_INVALID_ARG, "[E_INVALID_ARG] The specified input parameter is invalid.");
120
121                 ret = i2d_PKCS8_PRIV_KEY_INFO(pPrivKeyInfo, &pBuffer);
122                 SysTryCatch(NID_SEC, ret != -1, r = E_INVALID_ARG, E_INVALID_ARG, "[E_INVALID_ARG] The specified input parameter is invalid.");
123         }
124         break;
125
126         default:
127                 SysTryCatch(NID_SEC, false, r = E_INVALID_ARG, E_INVALID_ARG, " [E_INVALID_ARG] The specified input parameter is invalid.");
128
129         }
130
131         pOutBuffer = std::unique_ptr< ByteBuffer >(new (std::nothrow) ByteBuffer());
132         SysTryCatch(NID_SEC, pOutBuffer != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
133
134         r = pOutBuffer->Construct(ret);
135         SysTryCatch(NID_SEC, !IsFailed(r), r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
136
137         r = pOutBuffer->SetArray(pBuffer, 0, ret);
138         SysTryCatch(NID_SEC, !IsFailed(r), r = E_INVALID_ARG, E_INVALID_ARG, "[E_INVALID_ARG] The specified input parameter is invalid.");
139         pOutBuffer->Flip();
140
141         if (isPemFormat == true)
142         {
143                 PrivateKey key;
144                 r = key.SetKey(*pOutBuffer.get());
145                 SysTryCatch(NID_SEC, !IsFailed(r), r = E_INVALID_ARG, E_INVALID_ARG, "[E_INVALID_ARG] The specified input parameter is invalid.");
146                 pOutBuffer = std::unique_ptr< ByteBuffer >(ConvertDerToPemN(format, key));
147                 SysTryCatch(NID_SEC, pOutBuffer != null, r = GetLastResult(), GetLastResult(), "[%s] Failed to convert der to pem encoded byte buffer");
148         }
149
150         OPENSSL_free(pBuffer);
151         pBuffer = null;
152
153 CATCH:
154
155         if (pEvpKey != null)
156         {
157                 EVP_PKEY_free(pEvpKey);
158         }
159
160         if (pBio != null)
161         {
162                 BIO_free(pBio);
163         }
164
165         return pOutBuffer.release();
166 }
167
168 ByteBuffer*
169 RsaKeyConverter::ConvertPublicKeyFormatN(RsaKeyFormat format, const IPublicKey& key)
170 {
171         result r = E_SUCCESS;
172         byte* pBuffer = null;
173         const byte* pKey = null;
174         std::unique_ptr< ByteBuffer > pOutBuffer;
175         BIO* pBio = null;
176         EVP_PKEY* pEvpKey = null;
177         int ret = 0;
178         int keyLen = 0;
179         bool isPemFormat = false;
180
181         SysTryReturn(NID_SEC, format != RSA_KEY_FORMAT_UNKNOWN, null, E_INVALID_ARG, " [E_INVALID_ARG] The specified input parameter is invalid.");
182
183         std::unique_ptr< ByteBuffer > pPublicKey(key.GetEncodedN());
184         SysTryReturn(NID_SEC, pPublicKey, null, r, "[%s] Failed to get the public key", GetErrorMessage(r));
185
186         pKey = pPublicKey->GetPointer();
187         SysTryReturn(NID_SEC, pKey != null, null, E_INVALID_ARG, "[E_INVALID_ARG] The specified input parameter is invalid.");
188
189         keyLen = pPublicKey->GetRemaining();
190         SysTryReturn(NID_SEC, keyLen > 0, null, E_INVALID_ARG, "[E_INVALID_ARG] The specified input parameter is invalid.");
191
192         pEvpKey = d2i_PUBKEY(null, &pKey, keyLen);
193
194         if (pEvpKey == null)
195         {
196                 pEvpKey = d2i_PublicKey(EVP_PKEY_RSA, null, &pKey, keyLen);
197
198         }
199         if (pEvpKey == null)
200         {
201                 pBio = BIO_new(BIO_s_mem());
202                 SysTryReturn(NID_SEC, pBio != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
203
204                 int readCount = BIO_write(pBio, (const void*) pKey, keyLen);
205                 if (readCount > 0)
206                 {
207                         pEvpKey = PEM_read_bio_PUBKEY(pBio, null, 0, null);
208                 }
209                 if (pEvpKey != null)
210                 {
211                         isPemFormat = true;
212                 }
213         }
214
215         SysTryCatch(NID_SEC, pEvpKey != null, r = E_INVALID_ARG, E_INVALID_ARG, "[E_INVALID_ARG] The specified input parameter is invalid.");
216         switch (format)
217         {
218         case RSA_KEY_FORMAT_PKCS01_PUBLIC_KEY:
219         {
220                 ret = i2d_PublicKey(pEvpKey, &pBuffer);
221                 SysTryCatch(NID_SEC, ret != -1, r = E_INVALID_ARG, E_INVALID_ARG, "[E_INVALID_ARG] The specified input parameter is invalid.");
222         }
223         break;
224
225         case RSA_KEY_FORMAT_X509_PUBLIC_KEY:
226         {
227                 ret = i2d_PUBKEY(pEvpKey, &pBuffer);
228                 SysTryCatch(NID_SEC, ret != -1, r = E_INVALID_ARG, E_INVALID_ARG, "[E_INVALID_ARG] The specified input parameter is invalid.");
229         }
230         break;
231
232         default:
233                 SysTryCatch(NID_SEC, false, r = E_INVALID_ARG, E_INVALID_ARG, " [E_INVALID_ARG] The specified input parameter is invalid.");
234
235         }
236
237         pOutBuffer = std::unique_ptr< ByteBuffer >(new (std::nothrow) ByteBuffer());
238         SysTryCatch(NID_SEC, pOutBuffer != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
239
240         r = pOutBuffer->Construct(ret);
241         SysTryCatch(NID_SEC, !IsFailed(r), r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
242
243         r = pOutBuffer->SetArray(pBuffer, 0, ret);
244         SysTryCatch(NID_SEC, !IsFailed(r), r = E_INVALID_ARG, E_INVALID_ARG, "[E_INVALID_ARG] The specified input parameter is invalid.");
245
246         pOutBuffer->Flip();
247
248         if (isPemFormat == true)
249         {
250                 PublicKey key;
251                 r = key.SetKey(*pOutBuffer);
252                 SysTryCatch(NID_SEC, !IsFailed(r), r = E_INVALID_ARG, E_INVALID_ARG, "[E_INVALID_ARG] The specified input parameter is invalid.");
253                 pOutBuffer.reset(ConvertDerToPemN(format, key));
254                 //pOutBuffer = std::unique_ptr< ByteBuffer >(ConvertDerToPemN(format, key));
255                 SysTryCatch(NID_SEC, pOutBuffer != null, r = GetLastResult(), GetLastResult(), "[%s] Failed to convert der to pem encoded byte buffer");
256         }
257
258         OPENSSL_free(pBuffer);
259         pBuffer = null;
260
261 CATCH:
262
263         if (pEvpKey != null)
264         {
265                 EVP_PKEY_free(pEvpKey);
266         }
267
268         if (pBio != null)
269         {
270                 BIO_free(pBio);
271         }
272
273         if (IsFailed(r))
274         {
275                 pOutBuffer.reset(null);
276         }
277
278         return pOutBuffer.release();
279 }
280
281
282 ByteBuffer*
283 RsaKeyConverter::ConvertDerToPemN(RsaKeyFormat format, const IKey& key)
284 {
285         result r = E_SUCCESS;
286         const byte* pKey = null;
287         BIO* pBio = null;
288         EVP_PKEY* pEvpKey = null;
289         std::unique_ptr< byte[] > pData;
290         std::unique_ptr< ByteBuffer > pOutBuffer;
291         std::unique_ptr< ByteBuffer > pEncodedInputBuffer;
292         int ret = 0;
293         int keyLen = 0;
294         int dataLen = 0;
295
296         pEncodedInputBuffer = std::unique_ptr< ByteBuffer >(key.GetEncodedN());
297         SysTryReturn(NID_SEC, pEncodedInputBuffer != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
298
299         pKey = pEncodedInputBuffer->GetPointer();
300         SysTryReturn(NID_SEC, pKey != null, null, E_INVALID_ARG, "[E_INVALID_ARG] The specified input parameter is invalid.");
301
302         keyLen = pEncodedInputBuffer->GetRemaining();
303         SysTryReturn(NID_SEC, keyLen > 0, null, E_INVALID_ARG, "[E_INVALID_ARG] The specified input parameter is invalid.");
304
305         switch (format)
306         {
307         case RSA_KEY_FORMAT_PKCS01_PRIVATE_KEY:
308         {
309                 pEvpKey = d2i_PrivateKey(EVP_PKEY_RSA, null, &pKey, keyLen);
310                 SysTryCatch(NID_SEC, pEvpKey != null, r = E_INVALID_ARG, E_INVALID_ARG, "[E_INVALID_ARG] The specified input parameter is invalid.");
311
312                 pBio = BIO_new(BIO_s_mem());
313                 SysTryCatch(NID_SEC, pBio != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
314
315                 ret = PEM_write_bio_PrivateKey(pBio, pEvpKey, null, null, 0, null, null);
316                 SysTryCatch(NID_SEC, ret != -1, r = E_INVALID_ARG, E_INVALID_ARG, "[E_INVALID_ARG] The specified input parameter is invalid.");
317
318         }
319         break;
320
321         case RSA_KEY_FORMAT_PKCS01_PUBLIC_KEY:
322         {
323                 pEvpKey = d2i_PublicKey(EVP_PKEY_RSA, null, &pKey, keyLen);
324                 SysTryCatch(NID_SEC, pEvpKey != null, r = E_INVALID_ARG, E_INVALID_ARG, "[E_INVALID_ARG] The specified input parameter is invalid.");
325
326                 pBio = BIO_new(BIO_s_mem());
327                 SysTryCatch(NID_SEC, pBio != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
328
329                 ret = PEM_write_bio_PUBKEY(pBio, pEvpKey);
330                 SysTryCatch(NID_SEC, ret != -1, r = E_INVALID_ARG, E_INVALID_ARG, "[E_INVALID_ARG] The specified input parameter is invalid.");
331
332         }
333         break;
334
335         case RSA_KEY_FORMAT_X509_PUBLIC_KEY:
336         {
337                 pEvpKey = d2i_PUBKEY(null, &pKey, keyLen);
338                 SysTryCatch(NID_SEC, pEvpKey != null, r = E_INVALID_ARG, E_INVALID_ARG, "[E_INVALID_ARG] The specified input parameter is invalid.");
339
340                 pBio = BIO_new(BIO_s_mem());
341                 SysTryCatch(NID_SEC, pBio != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
342
343                 ret = PEM_write_bio_PUBKEY(pBio, pEvpKey);
344                 SysTryCatch(NID_SEC, ret != -1, r = E_INVALID_ARG, E_INVALID_ARG, "[E_INVALID_ARG] The specified input parameter is invalid.");
345         }
346         break;
347
348         case RSA_KEY_FORMAT_PKCS08_PRIVATE_KEY:
349         {
350                 PKCS8_PRIV_KEY_INFO* pPrivKeyInfo = d2i_PKCS8_PRIV_KEY_INFO(null, &pKey, keyLen);
351                 if (pPrivKeyInfo != null)
352                 {
353                         pEvpKey = EVP_PKCS82PKEY(pPrivKeyInfo);
354                         PKCS8_PRIV_KEY_INFO_free(pPrivKeyInfo);
355                 }
356                 pBio = BIO_new(BIO_s_mem());
357                 SysTryCatch(NID_SEC, pBio != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
358
359                 ret = PEM_write_bio_PrivateKey(pBio, pEvpKey, null, null, 0, null, null);
360                 SysTryCatch(NID_SEC, ret != -1, r = E_INVALID_ARG, E_INVALID_ARG, "[E_INVALID_ARG] The specified input parameter is invalid.");
361         }
362
363         break;
364
365         default:
366
367                 SysTryCatch(NID_SEC, false, r = E_INVALID_ARG, E_INVALID_ARG, " [E_INVALID_ARG] The specified input parameter is invalid.");
368         }
369
370         dataLen = BIO_get_mem_data(pBio, null);
371         SysTryCatch(NID_SEC, dataLen > 0, r = E_INVALID_ARG, E_INVALID_ARG, "[E_INVALID_ARG] The specified input parameter is invalid.");
372
373         pData = std::unique_ptr< byte[] >(new (std::nothrow) byte[dataLen]);
374         SysTryCatch(NID_SEC, pData != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
375
376         ret = BIO_read(pBio, pData.get(), dataLen);
377         SysTryCatch(NID_SEC, ret > 0, r = E_INVALID_ARG, E_INVALID_ARG, "[E_INVALID_ARG] The specified input parameter is invalid.");
378
379         pOutBuffer = std::unique_ptr< ByteBuffer >(new (std::nothrow) ByteBuffer());
380         SysTryCatch(NID_SEC, pOutBuffer != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
381
382         r = pOutBuffer->Construct(ret);
383         SysTryCatch(NID_SEC, !IsFailed(r), r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
384
385         r = pOutBuffer->SetArray(pData.get(), 0, ret);
386         SysTryCatch(NID_SEC, !IsFailed(r), r = E_INVALID_ARG, E_INVALID_ARG, "[E_INVALID_ARG] The specified input parameter is invalid.");
387
388         pOutBuffer->Flip();
389
390 CATCH:
391         if (pEvpKey != null)
392         {
393
394                 EVP_PKEY_free(pEvpKey);
395         }
396
397         if (pBio != null)
398         {
399                 BIO_free(pBio);
400         }
401
402         return pOutBuffer.release();
403 }
404
405 ByteBuffer*
406 RsaKeyConverter::ConvertPemToDerN(RsaKeyFormat format, const IKey& key)
407 {
408         result r = E_SUCCESS;
409         int keyLen = 0;
410         int ret = 0;
411         const byte* pKey = null;
412         byte* pBuffer = null;
413         BIO* pBio = null;
414         EVP_PKEY* pEvpKey = null;
415         std::unique_ptr< ByteBuffer > pOutBuffer;
416         std::unique_ptr< ByteBuffer > pEncodedInputBuffer;
417
418         pEncodedInputBuffer = std::unique_ptr< ByteBuffer >(key.GetEncodedN());
419         SysTryReturn(NID_SEC, pEncodedInputBuffer != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
420
421         pKey = pEncodedInputBuffer->GetPointer();
422         SysTryReturn(NID_SEC, pKey != null, null, E_INVALID_ARG, "[E_INVALID_ARG] The specified input parameter is invalid.");
423
424         keyLen = pEncodedInputBuffer->GetRemaining();
425         SysTryReturn(NID_SEC, keyLen > 0, null, E_INVALID_ARG, "[E_INVALID_ARG] The specified input parameter is invalid.");
426
427         pBio = BIO_new(BIO_s_mem());
428         SysTryReturn(NID_SEC, pBio != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
429
430         int readCount = BIO_write(pBio, (const void*) pKey, keyLen);
431         SysTryCatch(NID_SEC, readCount > 0, r = E_INVALID_ARG, E_INVALID_ARG, "[E_INVALID_ARG] The specified input parameter is invalid.");
432
433         switch (format)
434         {
435         case RSA_KEY_FORMAT_PKCS01_PRIVATE_KEY:
436         {
437                 pEvpKey = PEM_read_bio_PrivateKey(pBio, null, 0, null);
438                 SysTryCatch(NID_SEC, pEvpKey != null, r = E_INVALID_ARG, E_INVALID_ARG, "[E_INVALID_ARG] The specified input parameter is invalid.");
439
440                 ret = i2d_PrivateKey(pEvpKey, &pBuffer);
441                 SysTryCatch(NID_SEC, ret != -1, r = E_INVALID_ARG, E_INVALID_ARG, "[E_INVALID_ARG] The specified input parameter is invalid.");
442         }
443         break;
444
445         case RSA_KEY_FORMAT_PKCS01_PUBLIC_KEY:
446         {
447                 pEvpKey = PEM_read_bio_PUBKEY(pBio, null, 0, null);
448                 SysTryCatch(NID_SEC, pEvpKey != null, r = E_INVALID_ARG, E_INVALID_ARG, "[E_INVALID_ARG] The specified input parameter is invalid.");
449
450                 ret = i2d_PublicKey(pEvpKey, &pBuffer);
451                 SysTryCatch(NID_SEC, ret != -1, r = E_INVALID_ARG, E_INVALID_ARG, "[E_INVALID_ARG] The specified input parameter is invalid.");
452         }
453         break;
454
455         case RSA_KEY_FORMAT_X509_PUBLIC_KEY:
456         {
457                 pEvpKey = PEM_read_bio_PUBKEY(pBio, null, 0, null);
458                 SysTryCatch(NID_SEC, pEvpKey != null, r = E_INVALID_ARG, E_INVALID_ARG, "[E_INVALID_ARG] The specified input parameter is invalid.");
459
460                 ret = i2d_PUBKEY(pEvpKey, &pBuffer);
461                 SysTryCatch(NID_SEC, ret != -1, r = E_INVALID_ARG, E_INVALID_ARG, "[E_INVALID_ARG] The specified input parameter is invalid.");
462         }
463         break;
464
465         case RSA_KEY_FORMAT_PKCS08_PRIVATE_KEY:
466         {
467                 pEvpKey = PEM_read_bio_PrivateKey(pBio, null, 0, null);
468                 SysTryCatch(NID_SEC, pEvpKey != null, r = E_INVALID_ARG, E_INVALID_ARG, "[E_INVALID_ARG] The specified input parameter is invalid.");
469
470                 PKCS8_PRIV_KEY_INFO* pPrivKeyInfo = EVP_PKEY2PKCS8(pEvpKey);
471                 SysTryCatch(NID_SEC, pPrivKeyInfo != null, r = E_INVALID_ARG, E_INVALID_ARG, "[E_INVALID_ARG] The specified input parameter is invalid.");
472
473                 ret = i2d_PKCS8_PRIV_KEY_INFO(pPrivKeyInfo, &pBuffer);
474                 SysTryCatch(NID_SEC, ret != -1, r = E_INVALID_ARG, E_INVALID_ARG, "[E_INVALID_ARG] The specified input parameter is invalid.");
475         }
476         break;
477
478         default:
479                 SysTryCatch(NID_SEC, false, r = E_INVALID_ARG, E_INVALID_ARG, " [E_INVALID_ARG] The specified input parameter is invalid.");
480
481         }
482
483         pOutBuffer = std::unique_ptr< ByteBuffer >(new (std::nothrow) ByteBuffer());
484         SysTryCatch(NID_SEC, pOutBuffer != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
485
486         r = pOutBuffer->Construct(ret);
487         SysTryCatch(NID_SEC, !IsFailed(r), r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
488
489         r = pOutBuffer->SetArray(pBuffer, 0, ret);
490         SysTryCatch(NID_SEC, !IsFailed(r), r = E_INVALID_ARG, E_INVALID_ARG, "[E_INVALID_ARG] The specified input parameter is invalid.");
491
492         pOutBuffer->Flip();
493
494         OPENSSL_free(pBuffer);
495         pBuffer = null;
496
497 CATCH:
498
499         if (pEvpKey != null)
500         {
501                 EVP_PKEY_free(pEvpKey);
502         }
503
504         if (pBio != null)
505         {
506                 BIO_free(pBio);
507         }
508
509         return pOutBuffer.release();
510
511 }
512
513 } } //Tizen::Security