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