Merge "Update GetPackageAppInfoN() API" into tizen_2.1
[platform/framework/native/appfw.git] / src / security / pkcs / FSecPkcs_PkcsUtility.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                FSecPkcs_PkcsUtility.cpp
20  * @brief               This is the implementation file for _PkcsUtility class.
21  *
22  * This header file contains the implementation of _PkcsUtility class.
23  *
24  */
25
26
27 #include <openssl/x509.h>
28 #include <openssl/objects.h>
29 #include <openssl/obj_mac.h>
30 #include <openssl/evp.h>
31 #include <unique_ptr.h>
32 #include <FBaseResult.h>
33 #include <FBaseSysLog.h>
34 #include <FBaseErrors.h>
35 #include <FSecCryptoRsaCipher.h>
36 #include <FSecPrivateKey.h>
37 #include <FSecPublicKey.h>
38 #include <FSecPkcsPkcs05Schemes.h>
39 #include <FSecPkcsInitialVector.h>
40 #include <FSecPkcsAlgorithmIdentifier.h>
41 #include <FSecPkcsPkcs05PbKdf2Parameters.h>
42 #include <FSecPkcsPkcs05PbEs2Parameters.h>
43 #include <FSecPkcsPkcs05PbMacParameters.h>
44 #include <FSecPkcsRc2CbcParameters.h>
45 #include <FSecPkcsTypes.h>
46 #include <FSecPkcsIAlgorithmParameters.h>
47
48 #include "FSecPkcs_PkcsUtility.h"
49
50 using namespace Tizen::Base;
51 using namespace Tizen::Security::Crypto;
52
53 namespace Tizen { namespace Security { namespace Pkcs
54 {
55 static const int _PKCS05_MAX_KEY_SIZE = 32;
56
57 bool
58 _PkcsUtility::IsParameterSupported(Tizen::Base::String algorithm)
59 {
60         if (algorithm == OID_PBKDF2 || algorithm == OID_PBES2 || algorithm == OID_PBMAC1 || algorithm == OID_DES_CBC || algorithm == OID_DES_CBC_EDE3
61                 || algorithm == OID_AES_128_CBC || algorithm == OID_AES_192_CBC || algorithm == OID_AES_256_CBC || algorithm == OID_RC2_CBC)
62         {
63                 return true;
64         }
65         else
66         {
67                 return false;
68         }
69
70 }
71
72
73 _OidType
74 _PkcsUtility::ConvertOidToEnum(Tizen::Base::String algorithm)
75 {
76
77         result r = E_SUCCESS;
78         ClearLastResult();
79
80         _OidType oidValue = _OID_TYPE_UNKNOWN;
81
82         if (algorithm == OID_PBKDF2)
83         {
84                 oidValue = _OID_TYPE_PBKDF2;
85         }
86         else if (algorithm == OID_PBES2)
87         {
88                 oidValue = _OID_TYPE_PBES2;
89         }
90         else if (algorithm == OID_PBMAC1)
91         {
92                 oidValue = _OID_TYPE_PBMAC1;
93         }
94         else if (algorithm == OID_HMAC_SHA1)
95         {
96                 oidValue = _OID_TYPE_HMAC_SHA1;
97         }
98         else if (algorithm == OID_HMAC_SHA2_224)
99         {
100                 oidValue = _OID_TYPE_HMAC_SHA2_224;
101         }
102         else if (algorithm == OID_HMAC_SHA2_256)
103         {
104                 oidValue = _OID_TYPE_HMAC_SHA2_256;
105         }
106         else if (algorithm == OID_HMAC_SHA2_384)
107         {
108                 oidValue = _OID_TYPE_HMAC_SHA2_384;
109         }
110         else if (algorithm == OID_HMAC_SHA2_512)
111         {
112                 oidValue = _OID_TYPE_HMAC_SHA2_512;
113         }
114         else if (algorithm == OID_DES_CBC)
115         {
116                 oidValue = _OID_TYPE_DES_CBC;
117         }
118         else if (algorithm == OID_DES_CBC_EDE3)
119         {
120                 oidValue = _OID_TYPE_DES_CBC_EDE3;
121         }
122         else if (algorithm == OID_AES_128_CBC)
123         {
124                 oidValue = _OID_TYPE_AES_128_CBC;
125         }
126         else if (algorithm == OID_AES_192_CBC)
127         {
128                 oidValue = _OID_TYPE_AES_192_CBC;
129         }
130         else if (algorithm == OID_AES_256_CBC)
131         {
132                 oidValue = _OID_TYPE_AES_256_CBC;
133         }
134         else if (algorithm == OID_RC2_CBC)
135         {
136                 oidValue = _OID_TYPE_RC2_CBC;
137         }
138         else if (algorithm == OID_RSA_ENCRYPTION)
139         {
140                 oidValue = _OID_TYPE_RSA_ENCRYPTION;
141         }
142         else
143         {
144                 r = E_UNSUPPORTED_ALGORITHM;
145         }
146
147         SetLastResult(r);
148         return oidValue;
149 }
150
151 Tizen::Base::String
152 _PkcsUtility::ConvertToOid(int nidValue)
153 {
154         result r = E_SUCCESS;
155         Tizen::Base::String oidValue = null;
156
157         ClearLastResult();
158
159         if (nidValue == NID_id_pbkdf2)
160         {
161                 oidValue = OID_PBKDF2;
162         }
163         else if (nidValue == NID_pbes2)
164         {
165                 oidValue = OID_PBES2;
166         }
167         else if (nidValue == NID_pbmac1)
168         {
169                 oidValue = OID_PBMAC1;
170         }
171         else if (nidValue == NID_hmacWithSHA1)
172         {
173                 oidValue = OID_HMAC_SHA1;
174         }
175         else if (nidValue == NID_hmacWithSHA224)
176         {
177                 oidValue = OID_HMAC_SHA2_224;
178         }
179         else if (nidValue == NID_hmacWithSHA256)
180         {
181                 oidValue = OID_HMAC_SHA2_256;
182         }
183         else if (nidValue == NID_hmacWithSHA384)
184         {
185                 oidValue = OID_HMAC_SHA2_384;
186         }
187         else if (nidValue == NID_hmacWithSHA512)
188         {
189                 oidValue = OID_HMAC_SHA2_512;
190         }
191         else if (nidValue == NID_des_cbc)
192         {
193                 oidValue = OID_DES_CBC;
194         }
195         else if (nidValue == NID_des_ede3_cbc)
196         {
197                 oidValue = OID_DES_CBC_EDE3;
198         }
199         else if (nidValue == NID_aes_128_cbc)
200         {
201                 oidValue = OID_AES_128_CBC;
202         }
203         else if (nidValue == NID_aes_192_cbc)
204         {
205                 oidValue = OID_AES_192_CBC;
206         }
207         else if (nidValue == NID_aes_256_cbc)
208         {
209                 oidValue = OID_AES_256_CBC;
210         }
211         else if (nidValue == NID_rc2_cbc)
212         {
213                 oidValue = OID_RC2_CBC;
214         }
215         else if (nidValue == NID_rsaEncryption)
216         {
217                 oidValue = OID_RSA_ENCRYPTION;
218         }
219         else if (nidValue == NID_commonName)
220         {
221                 oidValue = OID_ATTR_COMMON_NAME;
222         }
223         else if (nidValue == NID_countryName)
224         {
225                 oidValue = OID_ATTR_COUNTRY_NAME;
226         }
227         else if (nidValue == NID_localityName)
228         {
229                 oidValue = OID_ATTR_LOCALITY_NAME;
230         }
231         else if (nidValue == NID_stateOrProvinceName)
232         {
233                 oidValue = OID_ATTR_STATE_OR_PROV_NAME;
234         }
235         else if (nidValue == NID_organizationName)
236         {
237                 oidValue = OID_ATTR_ORG_NAME;
238         }
239         else if (nidValue == NID_organizationalUnitName)
240         {
241                 oidValue = OID_ATTR_ORG_UNIT_NAME;
242         }
243         else if (nidValue == NID_givenName)
244         {
245                 oidValue = OID_ATTR_GIVEN_NAME;
246         }
247         else if (nidValue == NID_surname)
248         {
249                 oidValue = OID_ATTR_SURNAME;
250         }
251         else if (nidValue == NID_initials)
252         {
253                 oidValue = OID_ATTR_INITIAL;
254         }
255         else if (nidValue == NID_serialNumber)
256         {
257                 oidValue = OID_ATTR_SERIAL_NUMBER;
258         }
259         else if (nidValue == NID_title)
260         {
261                 oidValue = OID_ATTR_TITLE;
262         }
263         else if (nidValue == NID_pkcs9_emailAddress)
264         {
265                 oidValue = OID_ATTR_EMAIL_ADDRESS;
266         }
267         else if (nidValue == NID_generationQualifier)
268         {
269                 oidValue = OID_ATTR_GEN_QUALIFIER;
270         }
271         else if (nidValue == NID_pseudonym)
272         {
273                 oidValue = OID_ATTR_PSEUDONYM;
274         }
275         else if (nidValue == NID_domainComponent)
276         {
277                 oidValue = OID_ATTR_DOMAIN_COMPONENT;
278         }
279         else if (nidValue == NID_dnQualifier)
280         {
281                 oidValue = OID_ATTR_DN_QUALIFIER;
282         }
283         else if (nidValue == NID_name)
284         {
285                 oidValue = OID_ATTR_NAME;
286         }
287         else
288         {
289                 r = E_UNSUPPORTED_ALGORITHM;
290         }
291
292         SetLastResult(r);
293         return oidValue;
294 }
295
296 Pkcs08TagValue
297 _PkcsUtility::ConvertToTagValue(int ans1Type)
298 {
299         Pkcs08TagValue tagValue;
300
301         switch (ans1Type)
302         {
303         case V_ASN1_PRINTABLESTRING:
304                 tagValue = PKCS08_TAG_PRINTABLE_STRING;
305                 break;
306
307         case V_ASN1_EOC:
308                 tagValue = PKCS08_TAG_RESERVED;
309                 break;
310
311         case V_ASN1_BOOLEAN:
312                 tagValue = PKCS08_TAG_BOOLEAN;
313                 break;
314
315         case V_ASN1_INTEGER:
316                 tagValue = PKCS08_TAG_INTEGER;
317                 break;
318
319         case V_ASN1_BIT_STRING:
320                 tagValue = PKCS08_TAG_BITSTRING;
321                 break;
322
323         case V_ASN1_OCTET_STRING:
324                 tagValue = PKCS08_TAG_OCTETSTRING;
325                 break;
326
327         case V_ASN1_NULL:
328                 tagValue = PKCS08_TAG_NULL;
329                 break;
330
331         case V_ASN1_OBJECT:
332                 tagValue = PKCS08_TAG_OBJECT_ID;
333                 break;
334
335         case V_ASN1_OBJECT_DESCRIPTOR:
336                 tagValue = PKCS08_TAG_OBJECT_DES;
337                 break;
338
339         case V_ASN1_EXTERNAL:
340                 tagValue = PKCS08_TAG_EXTERNAL;
341                 break;
342
343         case V_ASN1_REAL:
344                 tagValue = PKCS08_TAG_REAL;
345                 break;
346
347         case V_ASN1_ENUMERATED:
348                 tagValue = PKCS08_TAG_ENUM;
349                 break;
350
351         case 11:
352                 tagValue = PKCS08_TAG_EMBEDDED;
353                 break;
354
355         case V_ASN1_UTF8STRING:
356                 tagValue = PKCS08_TAG_UTF8STRING;
357                 break;
358
359         case 13:
360                 tagValue = PKCS08_TAG_REL_OBJ_ID;
361                 break;
362
363         case V_ASN1_SEQUENCE:
364                 tagValue = PKCS08_TAG_SEQUENCE;
365                 break;
366
367         case V_ASN1_SET:
368                 tagValue = PKCS08_TAG_SET;
369                 break;
370
371         case V_ASN1_NUMERICSTRING:
372                 tagValue = PKCS08_TAG_CHAR_STRING;
373                 break;
374
375         case V_ASN1_TELETEXSTRING:
376                 tagValue = PKCS08_TAG_TELETEXT_STRING;
377                 break;
378
379         case V_ASN1_VIDEOTEXSTRING:
380                 tagValue = PKCS08_TAG_VIDEOTEXT_STRING;
381                 break;
382
383         case V_ASN1_IA5STRING:
384                 tagValue = PKCS08_TAG_IA5STRING;
385                 break;
386
387         case V_ASN1_UTCTIME:
388                 tagValue = PKCS08_TAG_UTC_TIME;
389                 break;
390
391         case V_ASN1_GENERALIZEDTIME:
392                 tagValue = PKCS08_TAG_GEN_TIME;
393                 break;
394
395         case V_ASN1_GRAPHICSTRING:
396                 tagValue = PKCS08_TAG_GRAPHICS_STRING;
397                 break;
398
399         case V_ASN1_VISIBLESTRING:
400                 tagValue = PKCS08_TAG_VISIBLE_STRING;
401                 break;
402
403         case V_ASN1_GENERALSTRING:
404                 tagValue = PKCS08_TAG_GENERAL_STRING;
405                 break;
406
407         case V_ASN1_UNIVERSALSTRING:
408                 tagValue = PKCS08_TAG_UNIVERSAL_STRING;
409                 break;
410
411         case 29:
412                 tagValue = PKCS08_TAG_CHARACTER_STRING;
413                 break;
414
415         case V_ASN1_BMPSTRING:
416                 tagValue = PKCS08_TAG_BMP_STRING;
417                 break;
418
419         default:
420                 tagValue = PKCS08_TAG_UNKNOWN;
421                 SetLastResult(E_INVALID_ARG);
422                 break;
423
424         }
425
426         SetLastResult(E_SUCCESS);
427         return tagValue;
428 }
429
430 int
431 _PkcsUtility::ConvertToNid(Tizen::Base::String algorithm)
432 {
433         result r = E_SUCCESS;
434         int nid = 0;
435
436         ClearLastResult();
437
438         if (algorithm == OID_PBKDF2)
439         {
440                 nid = NID_id_pbkdf2;
441         }
442         else if (algorithm == OID_PBES2)
443         {
444                 nid = NID_pbes2;
445         }
446         else if (algorithm == OID_PBMAC1)
447         {
448                 nid = NID_pbmac1;
449         }
450         else if (algorithm == OID_HMAC_SHA1)
451         {
452                 nid = NID_hmacWithSHA1;
453         }
454         else if (algorithm == OID_HMAC_SHA2_224)
455         {
456                 nid = NID_hmacWithSHA224;
457         }
458         else if (algorithm == OID_HMAC_SHA2_256)
459         {
460                 nid = NID_hmacWithSHA256;
461         }
462         else if (algorithm == OID_HMAC_SHA2_384)
463         {
464                 nid = NID_hmacWithSHA384;
465         }
466         else if (algorithm == OID_HMAC_SHA2_512)
467         {
468                 nid = NID_hmacWithSHA512;
469         }
470         else if (algorithm == OID_DES_CBC)
471         {
472                 nid = NID_des_cbc;
473         }
474         else if (algorithm == OID_DES_CBC_EDE3)
475         {
476                 nid = NID_des_ede3_cbc;
477         }
478         else if (algorithm == OID_AES_128_CBC)
479         {
480                 nid = NID_aes_128_cbc;
481         }
482         else if (algorithm == OID_AES_192_CBC)
483         {
484                 nid = NID_aes_192_cbc;
485         }
486         else if (algorithm == OID_AES_256_CBC)
487         {
488                 nid = NID_aes_256_cbc;
489         }
490         else if (algorithm == OID_RC2_CBC)
491         {
492                 nid = NID_rc2_cbc;
493         }
494         else if (algorithm == OID_RSA_ENCRYPTION)
495         {
496                 nid = NID_rsaEncryption;
497         }
498         else if (algorithm == OID_ATTR_COUNTRY_NAME)
499         {
500                 nid = NID_countryName;
501         }
502         else if (algorithm == OID_ATTR_NAME)
503         {
504                 nid = NID_name;
505         }
506         else if (algorithm == OID_ATTR_COMMON_NAME)
507         {
508                 nid = NID_commonName;
509         }
510         else if (algorithm == OID_ATTR_LOCALITY_NAME)
511         {
512                 nid = NID_localityName;
513         }
514         else if (algorithm == OID_ATTR_STATE_OR_PROV_NAME)
515         {
516                 nid = NID_stateOrProvinceName;
517         }
518         else if (algorithm == OID_ATTR_ORG_NAME)
519         {
520                 nid = NID_organizationName;
521         }
522         else if (algorithm == OID_ATTR_ORG_UNIT_NAME)
523         {
524                 nid = NID_organizationalUnitName;
525         }
526         else if (algorithm == OID_ATTR_GIVEN_NAME)
527         {
528                 nid = NID_givenName;
529         }
530         else if (algorithm == OID_ATTR_SURNAME)
531         {
532                 nid = NID_surname;
533         }
534         else if (algorithm == OID_ATTR_INITIAL)
535         {
536                 nid = NID_initials;
537         }
538         else if (algorithm == OID_ATTR_SERIAL_NUMBER)
539         {
540                 nid = NID_serialNumber;
541         }
542         else if (algorithm == OID_ATTR_TITLE)
543         {
544                 nid = NID_title;
545         }
546         else if (algorithm == OID_ATTR_EMAIL_ADDRESS)
547         {
548                 nid = NID_pkcs9_emailAddress;
549         }
550         else if (algorithm == OID_ATTR_GEN_QUALIFIER)
551         {
552                 nid = NID_generationQualifier;
553         }
554         else if (algorithm == OID_ATTR_PSEUDONYM)
555         {
556                 nid = NID_pseudonym;
557         }
558         else if (algorithm == OID_ATTR_DOMAIN_COMPONENT)
559         {
560                 nid = NID_domainComponent;
561         }
562         else if (algorithm == OID_ATTR_DN_QUALIFIER)
563         {
564                 nid = NID_dnQualifier;
565         }
566         else
567         {
568                 r = E_UNSUPPORTED_ALGORITHM;
569         }
570         SetLastResult(r);
571         return nid;
572 }
573
574 //FSecurity PKCS Utility Operations
575 Tizen::Base::ByteBuffer*
576 _PkcsUtility::EncryptDecryptN(const AlgorithmIdentifier& algo, const Tizen::Base::ByteBuffer& derivedKey, const Tizen::Base::ByteBuffer& input, int modeValue)
577 {
578         result r = E_SUCCESS;
579         EVP_CIPHER_CTX cipherCtx;
580         std::unique_ptr< byte[] > pOut;
581         byte* pData = null;
582         int outLen = 0;
583         int dataLen = 0;
584         int finalLen = 0;
585         int tempLen = 0;
586         int ret = 0;
587         std::unique_ptr< ByteBuffer > pOutBuffer;
588         ByteBuffer ivValue;
589         Tizen::Base::String encOid = null;
590         const evp_cipher_st* pCipherAlgorithm = null;
591         _OidType oidValue = _OID_TYPE_UNKNOWN;
592
593         ClearLastResult();
594
595         EVP_CIPHER_CTX_init(&cipherCtx);
596         encOid = algo.GetAlgorithmObjectId();
597         oidValue = _PkcsUtility::ConvertOidToEnum(encOid);
598
599         switch (oidValue)
600         {
601         case _OID_TYPE_DES_CBC:
602         {
603                 std::unique_ptr< InitialVector > pIvObj(dynamic_cast< InitialVector* >(algo.GetParametersN()));
604                 if (pIvObj == null)
605                 {
606                         r = GetLastResult();
607                         if (r == E_OUT_OF_MEMORY)
608                         {
609                                 SysTryCatch(NID_SEC_CRYPTO, pIvObj, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
610
611                         }
612                         else
613                         {
614                                 SysTryCatch(NID_SEC_CRYPTO, pIvObj, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] The method cannot proceed due to a severe system error.");
615                         }
616
617                 }
618
619                 ivValue.Construct(pIvObj->GetInitialVector());
620                 SysTryCatch(NID_SEC_CRYPTO, ivValue.GetRemaining() > 0, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
621
622                 pCipherAlgorithm = EVP_des_cbc();
623                 ret = EVP_CipherInit(&cipherCtx, pCipherAlgorithm, const_cast< byte* >(derivedKey.GetPointer()), const_cast< byte* >(ivValue.GetPointer()), modeValue);
624                 SysTryCatch(NID_SEC_CRYPTO, ret == 1, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] The method cannot proceed due to a severe system error.");
625         }
626         break;
627
628         case _OID_TYPE_DES_CBC_EDE3:
629         {
630                 std::unique_ptr< InitialVector > pIvObj(dynamic_cast< InitialVector* >(algo.GetParametersN()));
631                 if (pIvObj == null)
632                 {
633                         r = GetLastResult();
634                         if (r == E_OUT_OF_MEMORY)
635                         {
636                                 SysTryCatch(NID_SEC_CRYPTO, pIvObj, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
637
638                         }
639                         else
640                         {
641                                 SysTryCatch(NID_SEC_CRYPTO, pIvObj, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] The method cannot proceed due to a severe system error.");
642                         }
643
644                 }
645
646                 ivValue.Construct(pIvObj->GetInitialVector());
647                 SysTryCatch(NID_SEC_CRYPTO, ivValue.GetRemaining() > 0, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
648
649
650                 pCipherAlgorithm = EVP_des_ede3();
651                 ret = EVP_CipherInit(&cipherCtx, pCipherAlgorithm, const_cast< byte* >(derivedKey.GetPointer()), const_cast< byte* >(ivValue.GetPointer()), modeValue);
652                 SysTryCatch(NID_SEC_CRYPTO, ret == 1, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] The method cannot proceed due to a severe system error.");
653         }
654         break;
655
656         case _OID_TYPE_AES_128_CBC:
657         {
658                 std::unique_ptr< InitialVector > pIvObj(dynamic_cast< InitialVector* >(algo.GetParametersN()));
659                 if (pIvObj == null)
660                 {
661                         r = GetLastResult();
662                         if (r == E_OUT_OF_MEMORY)
663                         {
664                                 SysTryCatch(NID_SEC_CRYPTO, pIvObj, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
665
666                         }
667                         else
668                         {
669                                 SysTryCatch(NID_SEC_CRYPTO, pIvObj, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] The method cannot proceed due to a severe system error.");
670                         }
671
672                 }
673
674                 ivValue.Construct(pIvObj->GetInitialVector());
675                 SysTryCatch(NID_SEC_CRYPTO, ivValue.GetRemaining() > 0, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
676
677
678                 pCipherAlgorithm = EVP_aes_128_cbc();
679
680                 ret = EVP_CipherInit(&cipherCtx, pCipherAlgorithm, const_cast< byte* >(derivedKey.GetPointer()), const_cast< byte* >(ivValue.GetPointer()), modeValue);
681                 SysTryCatch(NID_SEC_CRYPTO, ret == 1, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] The method cannot proceed due to a severe system error.");
682         }
683         break;
684
685         case _OID_TYPE_AES_192_CBC:
686         {
687                 std::unique_ptr< InitialVector > pIvObj(dynamic_cast< InitialVector* >(algo.GetParametersN()));
688                 if (pIvObj == null)
689                 {
690                         r = GetLastResult();
691                         if (r == E_OUT_OF_MEMORY)
692                         {
693                                 SysTryCatch(NID_SEC_CRYPTO, pIvObj, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
694
695                         }
696                         else
697                         {
698                                 SysTryCatch(NID_SEC_CRYPTO, pIvObj, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] The method cannot proceed due to a severe system error.");
699                         }
700
701                 }
702
703                 ivValue.Construct(pIvObj->GetInitialVector());
704                 SysTryCatch(NID_SEC_CRYPTO, ivValue.GetRemaining() > 0, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
705
706                 pCipherAlgorithm = EVP_aes_192_cbc();
707                 ret = EVP_CipherInit(&cipherCtx, pCipherAlgorithm, const_cast< byte* >(derivedKey.GetPointer()), const_cast< byte* >(ivValue.GetPointer()), modeValue);
708                 SysTryCatch(NID_SEC_CRYPTO, ret == 1, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] The method cannot proceed due to a severe system error.");
709
710         }
711         break;
712
713         case _OID_TYPE_AES_256_CBC:
714         {
715                 std::unique_ptr< InitialVector > pIvObj(dynamic_cast< InitialVector* >(algo.GetParametersN()));
716                 if (pIvObj == null)
717                 {
718                         r = GetLastResult();
719                         if (r == E_OUT_OF_MEMORY)
720                         {
721                                 SysTryCatch(NID_SEC_CRYPTO, pIvObj, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
722
723                         }
724                         else
725                         {
726                                 SysTryCatch(NID_SEC_CRYPTO, pIvObj, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] The method cannot proceed due to a severe system error.");
727                         }
728
729                 }
730
731                 ivValue.Construct(pIvObj->GetInitialVector());
732                 SysTryCatch(NID_SEC_CRYPTO, ivValue.GetRemaining() > 0, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
733
734
735                 pCipherAlgorithm = EVP_aes_256_cbc();
736                 ret = EVP_CipherInit(&cipherCtx, pCipherAlgorithm, const_cast< byte* >(derivedKey.GetPointer()), const_cast< byte* >(ivValue.GetPointer()), modeValue);
737                 SysTryCatch(NID_SEC_CRYPTO, ret == 1, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] The method cannot proceed due to a severe system error.");
738
739         }
740         break;
741
742         case _OID_TYPE_RC2_CBC:
743         {
744                 std::unique_ptr< Rc2CbcParameters > pRcObj(dynamic_cast< Rc2CbcParameters* >(algo.GetParametersN()));
745                 if (pRcObj == null)
746                 {
747                         r = GetLastResult();
748                         if (r == E_OUT_OF_MEMORY)
749                         {
750                                 SysTryCatch(NID_SEC_CRYPTO, pRcObj, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
751
752                         }
753                         else
754                         {
755                                 SysTryCatch(NID_SEC_CRYPTO, pRcObj, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] The method cannot proceed due to a severe system error.");
756                         }
757
758                 }
759
760                 ivValue.Construct(pRcObj->GetInitialVector());
761                 SysTryCatch(NID_SEC_CRYPTO, ivValue.GetRemaining() > 0, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
762
763
764                 pCipherAlgorithm = EVP_rc2_cbc();
765                 ret = EVP_CipherInit(&cipherCtx, pCipherAlgorithm, null, null, modeValue);
766                 SysTryCatch(NID_SEC_CRYPTO, ret == 1, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] The method cannot proceed due to a severe system error.");
767
768                 EVP_CIPHER_CTX_set_key_length(&cipherCtx, derivedKey.GetRemaining());
769
770                 ret = EVP_CipherInit(&cipherCtx, null, const_cast< byte* >(derivedKey.GetPointer()), const_cast< byte* >(ivValue.GetPointer()), modeValue);
771                 SysTryCatch(NID_SEC_CRYPTO, ret == 1, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] The method cannot proceed due to a severe system error.");
772
773
774         }
775         break;
776
777         case _OID_TYPE_PBES2:
778         {
779                 Pkcs05Schemes pkcs05Scheme;
780                 std::unique_ptr< Pkcs05PbEs2Parameters > pPbEs2(dynamic_cast< Pkcs05PbEs2Parameters* >(algo.GetParametersN()));
781                 if (pPbEs2 == null)
782                 {
783                         r = GetLastResult();
784                         if (r == E_OUT_OF_MEMORY)
785                         {
786                                 SysTryCatch(NID_SEC_CRYPTO, pPbEs2, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
787
788                         }
789                         else
790                         {
791                                 SysTryCatch(NID_SEC_CRYPTO, pPbEs2, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] The method cannot proceed due to a severe system error.");
792                         }
793
794                 }
795
796                 SysTryCatch(NID_SEC_CRYPTO, pPbEs2->GetKeyDerivationAlgorithm().GetAlgorithmObjectId().GetLength() > 0, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] The method cannot proceed due to a severe system error.");
797
798                 std::unique_ptr< Pkcs05PbKdf2Parameters > pKdfParams(dynamic_cast< Pkcs05PbKdf2Parameters* >(pPbEs2->GetKeyDerivationAlgorithm().GetParametersN()));
799                 if (pKdfParams == null)
800                 {
801                         r = GetLastResult();
802                         if (r == E_OUT_OF_MEMORY)
803                         {
804                                 SysTryCatch(NID_SEC_CRYPTO, pKdfParams, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
805
806                         }
807                         else
808                         {
809                                 SysTryCatch(NID_SEC_CRYPTO, pKdfParams, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] The method cannot proceed due to a severe system error.");
810                         }
811
812                 }
813
814                 int derivedKeyLength = pKdfParams->GetDerivedKeyLength();
815
816                 if (derivedKeyLength <= 0)
817                 {
818                         derivedKeyLength = _PKCS05_MAX_KEY_SIZE;
819                 }
820
821                 r = pkcs05Scheme.Construct(derivedKey, derivedKeyLength);
822
823                 if (modeValue == 1)
824                 {
825                         pOutBuffer = std::unique_ptr< ByteBuffer >(pkcs05Scheme.EncryptionScheme2N(*pPbEs2, input));
826                 }
827                 else
828                 {
829                         pOutBuffer = std::unique_ptr< ByteBuffer >(pkcs05Scheme.DecryptionScheme2N(*pPbEs2, input));
830                 }
831
832         }
833                 return pOutBuffer.release();
834
835         case _OID_TYPE_RSA_ENCRYPTION:
836         {
837                 RsaCipher rsaCipher;
838                 if (modeValue == 0)
839                 {
840                         PublicKey pubKey;
841
842                         r = pubKey.SetKey(derivedKey);
843                         SysTryCatch(NID_SEC_CRYPTO, !IsFailed(r), r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] The method cannot proceed due to a severe system error.");
844
845                         rsaCipher.SetPublicKey(pubKey);
846                         pOutBuffer = std::unique_ptr< ByteBuffer >(rsaCipher.DecryptN(input));
847
848                 }
849                 else
850                 {
851                         PrivateKey priKey;
852                         priKey.SetKey(derivedKey);
853                         r = rsaCipher.SetPrivateKey(priKey);
854                         pOutBuffer = std::unique_ptr< ByteBuffer >(rsaCipher.EncryptN(input));
855                 }
856         }
857
858                 return pOutBuffer.release();
859
860         default:
861                 r = E_UNSUPPORTED_ALGORITHM;
862                 SetLastResult(r);
863                 return null;
864         }
865
866
867
868         // initialize the cipher context
869
870         pData = const_cast< byte* >(input.GetPointer());
871         SysTryCatch(NID_SEC_CRYPTO, pData != null, r = E_INVALID_ARG, E_INVALID_ARG, "[E_INVALID_ARG] The specified input parameter is invalid.");
872
873         dataLen = input.GetRemaining();
874         SysTryCatch(NID_SEC_CRYPTO, dataLen > 0, r = E_INVALID_ARG, E_INVALID_ARG, "[E_INVALID_ARG] The specified input parameter is invalid.");
875
876         // block size
877         tempLen = static_cast< int >((dataLen / pCipherAlgorithm->block_size + 1)* pCipherAlgorithm->block_size);
878
879         pOut = std::unique_ptr< byte[] >(new (std::nothrow) byte[tempLen]);
880         SysTryCatch(NID_SEC_CRYPTO, pOut, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
881
882         memset(pOut.get(), 0, tempLen);
883
884         //cipher update operation
885         ret = EVP_CipherUpdate(&cipherCtx, pOut.get(), static_cast< int* >(&outLen), pData, dataLen);
886         SysTryCatch(NID_SEC_CRYPTO, ret == 1, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] The method cannot proceed due to a severe system error.");
887
888         //cipher final operation
889         ret = EVP_CipherFinal(&cipherCtx, pOut.get() + outLen, &finalLen);
890         SysTryCatch(NID_SEC_CRYPTO, ret == 1, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] The method cannot proceed due to a severe system error.");
891         outLen = outLen + finalLen;
892
893         SysTryCatch(NID_SEC_CRYPTO, outLen <= tempLen, r = E_OVERFLOW, E_OVERFLOW, "[E_OVERFLOW] Length mismatch resulting in overflow.");
894
895         //creating bytebuffer and storing output data from cipher final operation in it
896         pOutBuffer = std::unique_ptr< ByteBuffer >(new (std::nothrow) ByteBuffer());
897         SysTryCatch(NID_SEC_CRYPTO, pOutBuffer, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
898
899         r = pOutBuffer->Construct(outLen);
900         SysTryCatch(NID_SEC_CRYPTO, !IsFailed(r), r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
901
902         r = pOutBuffer->SetArray(pOut.get(), 0, outLen);
903         SysTryCatch(NID_SEC_CRYPTO, !IsFailed(r), r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] The method cannot proceed due to a severe system error.");
904
905         pOutBuffer->Flip();
906
907 CATCH:
908
909         if (IsFailed(r))
910         {
911                 pOutBuffer.reset(null);
912         }
913         EVP_CIPHER_CTX_cleanup(&cipherCtx);
914
915         SetLastResult(r);
916         return pOutBuffer.release();
917
918 }
919
920 X509_ALGOR*
921 _PkcsUtility::GenerateKdfParametersN(int iter, unsigned char* pSaltValue, int saltLen, int prfNid, int keyLen)
922 {
923         result r = E_SUCCESS;
924         X509_ALGOR* pKeyfunc = null;
925         PBKDF2PARAM* pKdf = null;
926         ASN1_OCTET_STRING* pSalt = null;
927         int ret = 0;
928
929
930         ClearLastResult();
931
932         pKdf = PBKDF2PARAM_new();
933         SysTryReturn(NID_SEC_CRYPTO, pKdf != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
934
935         pSalt = M_ASN1_OCTET_STRING_new();
936         SysTryCatch(NID_SEC_CRYPTO, pSalt != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
937
938         pKdf->salt->value.octet_string = pSalt;
939         pKdf->salt->type = V_ASN1_OCTET_STRING;
940
941         pSalt->data = static_cast< unsigned char* >(OPENSSL_malloc(saltLen));
942         SysTryCatch(NID_SEC_CRYPTO, pSalt->data != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
943
944         pSalt->length = saltLen;
945
946         memcpy(pSalt->data, pSaltValue, saltLen);
947
948         ASN1_INTEGER_set(pKdf->iter, iter);
949
950         if (keyLen > 0)
951         {
952
953                 pKdf->keylength = M_ASN1_INTEGER_new();
954                 SysTryCatch(NID_SEC_CRYPTO, pKdf->keylength != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
955
956                 ASN1_INTEGER_set(pKdf->keylength, keyLen);
957
958         }
959
960         if (prfNid != NID_hmacWithSHA1)
961         {
962
963                 pKdf->prf = X509_ALGOR_new();
964                 SysTryCatch(NID_SEC_CRYPTO, pKdf->prf != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
965
966                 ret = X509_ALGOR_set0(pKdf->prf, OBJ_nid2obj(prfNid), V_ASN1_NULL, NULL);
967                 SysTryCatch(NID_SEC_CRYPTO, ret > 0, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] The method cannot proceed due to a severe system error.");
968
969         }
970
971         pKeyfunc = X509_ALGOR_new();
972         SysTryCatch(NID_SEC_CRYPTO, pKeyfunc != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
973
974         pKeyfunc->algorithm = OBJ_nid2obj(NID_id_pbkdf2);
975
976         pKeyfunc->parameter = ASN1_TYPE_new();
977         ASN1_item_pack(pKdf, ASN1_ITEM_rptr(PBKDF2PARAM), &pKeyfunc->parameter->value.sequence);
978
979         pKeyfunc->parameter->type = V_ASN1_SEQUENCE;
980
981         PBKDF2PARAM_free(pKdf);
982         return pKeyfunc;
983
984 CATCH:
985
986         PBKDF2PARAM_free(pKdf);
987         X509_ALGOR_free(pKeyfunc);
988         SetLastResult(r);
989         return null;
990
991 }
992
993 IAlgorithmParameters*
994 _PkcsUtility::GernerateParametersFromOidN(Tizen::Base::String algoOid, X509_ALGOR* pAlgoObj)
995 {
996         result r = E_SUCCESS;
997         _OidType oidValue;
998         int pType = 0;
999         int dataLen = 0;
1000         int ret = 0;
1001         void* pVal = null;
1002         long num = 0;
1003         unsigned char iv[EVP_MAX_IV_LENGTH];
1004         const unsigned char* pData = null;
1005         ASN1_STRING* pStr = null;
1006         ASN1_TYPE* pAsn1Type = null;
1007         Tizen::Base::ByteBuffer ivBuffer;
1008         Tizen::Base::ByteBuffer ivBuffer1;
1009         Tizen::Base::ByteBuffer paramBuffer;
1010         Pkcs05PbEs2Parameters pbEs2Obj;
1011         Pkcs05PbKdf2Parameters kdfObj;
1012         Rc2CbcParameters rc2CbcObj;
1013         Pkcs05PbMacParameters pbMacObj;
1014         InitialVector ivObj;
1015         std::unique_ptr< IAlgorithmParameters > pAlgoParams;
1016
1017
1018         ClearLastResult();
1019
1020         oidValue = _PkcsUtility::ConvertOidToEnum(algoOid);
1021
1022         switch (oidValue)
1023         {
1024         case _OID_TYPE_DES_CBC:
1025         // fall through
1026         case _OID_TYPE_DES_CBC_EDE3:
1027         // fall through
1028         case _OID_TYPE_AES_128_CBC:
1029         // fall through
1030         case _OID_TYPE_AES_192_CBC:
1031         // fall through
1032         case _OID_TYPE_AES_256_CBC:
1033         {
1034                 X509_ALGOR_get0(&pAlgoObj->algorithm, &pType, &pVal, pAlgoObj);
1035                 SysTryReturn(NID_SEC_CRYPTO, pType == V_ASN1_OCTET_STRING, null, E_INVALID_ARG, "[E_INVALID_ARG] The specified input parameter is invalid.");
1036
1037                 SysTryReturn(NID_SEC_CRYPTO, pVal != null, null, E_INVALID_ARG, "[E_INVALID_ARG] The specified input parameter is invalid.");
1038
1039                 pStr = reinterpret_cast< ASN1_STRING* >(pVal);
1040                 pData = pStr->data;
1041                 dataLen = pStr->length;
1042
1043                 r = ivBuffer.Construct(dataLen);
1044                 SysTryReturn(NID_SEC_CRYPTO, !IsFailed(r), null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
1045
1046                 r = ivBuffer.SetArray(pData, 0, dataLen);
1047                 SysTryReturn(NID_SEC_CRYPTO, !IsFailed(r), null, E_INVALID_ARG, "[E_INVALID_ARG] The specified input parameter is invalid.");
1048
1049                 ivBuffer.Flip();
1050
1051                 r = ivObj.Construct(ivBuffer);
1052                 SysTryReturn(NID_SEC_CRYPTO, !IsFailed(r), null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
1053
1054                 pAlgoParams = std::unique_ptr< IAlgorithmParameters >(ivObj.CloneN());
1055                 SysTryReturn(NID_SEC_CRYPTO, pAlgoParams, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
1056
1057                 return pAlgoParams.release();
1058         }
1059         break;
1060
1061         case _OID_TYPE_RC2_CBC:
1062         {
1063                 X509_ALGOR_get0(&pAlgoObj->algorithm, &pType, &pVal, pAlgoObj);
1064                 SysTryReturn(NID_SEC_CRYPTO, ((pType == V_ASN1_SEQUENCE) || (pType == V_ASN1_OCTET_STRING)), null, E_INVALID_ARG, "[E_INVALID_ARG] The specified input parameter is invalid.");
1065
1066                 SysTryReturn(NID_SEC_CRYPTO, pVal != null, null, E_INVALID_ARG, "[E_INVALID_ARG] The specified input parameter is invalid.");
1067
1068                 pStr = reinterpret_cast< ASN1_STRING* >(pVal);
1069                 pData = pStr->data;
1070                 dataLen = pStr->length;
1071
1072
1073                 if (pType == V_ASN1_OCTET_STRING)
1074                 {
1075                         r = ivBuffer.Construct(dataLen);
1076                         SysTryReturn(NID_SEC_CRYPTO, !IsFailed(r), null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
1077
1078                         r = ivBuffer.SetArray(pData, 0, dataLen);
1079                         SysTryReturn(NID_SEC_CRYPTO, !IsFailed(r), null, E_INVALID_ARG, "[E_INVALID_ARG] The specified input parameter is invalid.");
1080
1081                         ivBuffer.Flip();
1082
1083                         r = rc2CbcObj.Construct(ivBuffer);
1084                         SysTryReturn(NID_SEC_CRYPTO, !IsFailed(r), null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
1085
1086                 }
1087                 else
1088                 {
1089
1090                         r = ivBuffer.Construct(dataLen);
1091                         SysTryReturn(NID_SEC_CRYPTO, !IsFailed(r), null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
1092
1093                         r = ivBuffer.SetArray(pData, 0, dataLen);
1094                         SysTryReturn(NID_SEC_CRYPTO, !IsFailed(r), null, E_INVALID_ARG, "[E_INVALID_ARG] The specified input parameter is invalid.");
1095
1096                         ivBuffer.Flip();
1097
1098                         const byte* pBuffer = ivBuffer.GetPointer();
1099
1100                         pAsn1Type = d2i_ASN1_TYPE(null, reinterpret_cast< const unsigned char** >(&pBuffer), ivBuffer.GetRemaining());
1101                         SysTryReturn(NID_SEC_CRYPTO, pAsn1Type != null, null, E_INVALID_ARG, "[E_INVALID_ARG] The specified input parameter is invalid.");
1102
1103                         memset(iv, 0, sizeof(iv));
1104
1105                         ret = ASN1_TYPE_get_int_octetstring(pAsn1Type, &num, iv, sizeof(iv));
1106                         SysTryReturn(NID_SEC_CRYPTO, ret > 0, null, E_INVALID_ARG, "[E_INVALID_ARG] The specified input parameter is invalid.");
1107
1108                         r = ivBuffer1.Construct(ret);
1109                         SysTryReturn(NID_SEC_CRYPTO, !IsFailed(r), null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
1110
1111                         r = ivBuffer1.SetArray(iv, 0, ret);
1112                         SysTryReturn(NID_SEC_CRYPTO, !IsFailed(r), null, E_INVALID_ARG, "[E_INVALID_ARG] The specified input parameter is invalid.");
1113
1114                         ivBuffer1.Flip();
1115
1116                         r = rc2CbcObj.Construct(ivBuffer1, num);
1117                         SysTryReturn(NID_SEC_CRYPTO, !IsFailed(r), null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
1118
1119                 }
1120
1121                 pAlgoParams = std::unique_ptr< IAlgorithmParameters >(rc2CbcObj.CloneN());
1122                 SysTryReturn(NID_SEC_CRYPTO, pAlgoParams, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
1123
1124                 return pAlgoParams.release();
1125
1126         }
1127         break;
1128
1129         case _OID_TYPE_HMAC_SHA1:
1130         // fall through
1131         case _OID_TYPE_HMAC_SHA2_224:
1132         // fall through
1133         case _OID_TYPE_HMAC_SHA2_256:
1134         // fall through
1135         case _OID_TYPE_HMAC_SHA2_384:
1136         // fall through
1137         case _OID_TYPE_HMAC_SHA2_512:
1138         // fall through
1139         case _OID_TYPE_RSA_ENCRYPTION:
1140         {
1141                 pAlgoParams = null;
1142                 return pAlgoParams.release();
1143         }
1144         break;
1145
1146         case _OID_TYPE_PBKDF2:
1147         {
1148                 X509_ALGOR_get0(&(pAlgoObj->algorithm), &pType, &pVal, pAlgoObj);
1149                 SysTryReturn(NID_SEC_CRYPTO, pType == V_ASN1_SEQUENCE, null, E_INVALID_ARG, "[E_INVALID_ARG] The specified input parameter is invalid.");
1150
1151                 SysTryReturn(NID_SEC_CRYPTO, pVal != null, null, E_INVALID_ARG, "[E_INVALID_ARG] The specified input parameter is invalid.");
1152
1153                 pStr = reinterpret_cast< ASN1_STRING* >(pVal);
1154                 pData = pStr->data;
1155                 dataLen = pStr->length;
1156
1157                 r = paramBuffer.Construct(dataLen);
1158                 SysTryReturn(NID_SEC_CRYPTO, !IsFailed(r), null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
1159
1160                 r = paramBuffer.SetArray(pData, 0, dataLen);
1161                 SysTryReturn(NID_SEC_CRYPTO, !IsFailed(r), null, E_INVALID_ARG, "[E_INVALID_ARG] The specified input parameter is invalid.");
1162
1163                 paramBuffer.Flip();
1164
1165                 r = kdfObj.Construct(paramBuffer);
1166                 SysTryReturn(NID_SEC_CRYPTO, !IsFailed(r), null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
1167
1168                 pAlgoParams = std::unique_ptr< IAlgorithmParameters >(kdfObj.CloneN());
1169                 SysTryReturn(NID_SEC_CRYPTO, pAlgoParams, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
1170
1171                 return pAlgoParams.release();
1172         }
1173         break;
1174
1175         case _OID_TYPE_PBES2:
1176         {
1177                 X509_ALGOR_get0(&(pAlgoObj->algorithm), &pType, &pVal, pAlgoObj);
1178                 SysTryReturn(NID_SEC_CRYPTO, pType == V_ASN1_SEQUENCE, null, E_INVALID_ARG, "[E_INVALID_ARG] The specified input parameter is invalid.");
1179
1180                 SysTryReturn(NID_SEC_CRYPTO, pVal != null, null, E_INVALID_ARG, "[E_INVALID_ARG] The specified input parameter is invalid.");
1181
1182                 pStr = reinterpret_cast< ASN1_STRING* >(pVal);
1183                 pData = pStr->data;
1184                 dataLen = pStr->length;
1185
1186                 r = paramBuffer.Construct(dataLen);
1187                 SysTryReturn(NID_SEC_CRYPTO, !IsFailed(r), null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
1188
1189                 r = paramBuffer.SetArray(pData, 0, dataLen);
1190                 SysTryReturn(NID_SEC_CRYPTO, !IsFailed(r), null, E_INVALID_ARG, "[E_INVALID_ARG] The specified input parameter is invalid.");
1191
1192                 paramBuffer.Flip();
1193
1194                 r = pbEs2Obj.Construct(paramBuffer);
1195                 SysTryReturn(NID_SEC_CRYPTO, !IsFailed(r), null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
1196
1197                 pAlgoParams = std::unique_ptr< IAlgorithmParameters >(pbEs2Obj.CloneN());
1198                 SysTryReturn(NID_SEC_CRYPTO, pAlgoParams, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
1199
1200                 return pAlgoParams.release();
1201         }
1202         break;
1203
1204         case _OID_TYPE_PBMAC1:
1205         {
1206                 X509_ALGOR_get0(&pAlgoObj->algorithm, &pType, &pVal, pAlgoObj);
1207                 SysTryReturn(NID_SEC_CRYPTO, pType == V_ASN1_SEQUENCE, null, E_INVALID_ARG, "[E_INVALID_ARG] The specified input parameter is invalid.");
1208
1209                 SysTryReturn(NID_SEC_CRYPTO, pVal != null, null, E_INVALID_ARG, "[E_INVALID_ARG] The specified input parameter is invalid.");
1210                 pStr = reinterpret_cast< ASN1_STRING* >(pVal);
1211                 pData = pStr->data;
1212                 dataLen = pStr->length;
1213
1214                 r = paramBuffer.Construct(dataLen);
1215                 SysTryReturn(NID_SEC_CRYPTO, !IsFailed(r), null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
1216
1217                 r = paramBuffer.SetArray(pData, 0, dataLen);
1218                 SysTryReturn(NID_SEC_CRYPTO, !IsFailed(r), null, E_INVALID_ARG, "[E_INVALID_ARG] The specified input parameter is invalid.");
1219
1220                 paramBuffer.Flip();
1221
1222                 r = pbMacObj.Construct(paramBuffer);
1223                 SysTryReturn(NID_SEC_CRYPTO, !IsFailed(r), null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
1224
1225                 pAlgoParams = std::unique_ptr< IAlgorithmParameters >(pbMacObj.CloneN());
1226                 SysTryReturn(NID_SEC_CRYPTO, pAlgoParams, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
1227
1228                 return pAlgoParams.release();
1229         }
1230         break;
1231
1232         default:
1233
1234                 r = E_UNSUPPORTED_ALGORITHM;
1235                 break;
1236         }
1237
1238         SetLastResult(r);
1239         return pAlgoParams.release();
1240
1241 }
1242
1243 X509_ALGOR*
1244 _PkcsUtility::GenerateAlgorithmIdentifierStructureN(Tizen::Base::String algoOid, IAlgorithmParameters* pAlgoParam)
1245 {
1246         result r = E_SUCCESS;
1247         X509_ALGOR* pAlgoObj = null;
1248         _OidType oidValue;
1249         int ret = 0;
1250         int algoNid = 0;
1251         int version = 0;
1252         const byte* pBuf = null;
1253         PBKDF2PARAM* pKdf = null;
1254         PBE2PARAM* pPbes2Obj = null;
1255         PBE2PARAM* pMacObj = null;
1256         ASN1_STRING* pbKdf2Str = null;
1257         ASN1_STRING* pbE2Str = null;
1258         ASN1_STRING* pIv = null;
1259         ASN1_TYPE* pAsn1Type = null;
1260         Tizen::Base::ByteBuffer ivBuffer;
1261
1262         ClearLastResult();
1263
1264         pAlgoObj = X509_ALGOR_new();
1265         SysTryReturn(NID_SEC_CRYPTO, pAlgoObj != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
1266
1267         oidValue = _PkcsUtility::ConvertOidToEnum(algoOid);
1268         algoNid = _PkcsUtility::ConvertToNid(algoOid);
1269
1270         switch (oidValue)
1271         {
1272         case _OID_TYPE_DES_CBC:
1273         // fall through
1274         case _OID_TYPE_DES_CBC_EDE3:
1275         // fall through
1276         case _OID_TYPE_AES_128_CBC:
1277         // fall through
1278         case _OID_TYPE_AES_192_CBC:
1279         // fall through
1280         case _OID_TYPE_AES_256_CBC:
1281         {
1282                 SysTryReturn(NID_SEC_CRYPTO, pAlgoParam != null, null, E_INVALID_ARG, "[E_INVALID_ARG] The specified input parameter is invalid.");
1283
1284                 ivBuffer.Construct(((dynamic_cast< InitialVector* >(pAlgoParam))->GetInitialVector()));
1285                 SysTryReturn(NID_SEC_CRYPTO, ivBuffer.GetRemaining() > 0, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
1286
1287                 pIv = ASN1_STRING_new();
1288                 SysTryReturn(NID_SEC_CRYPTO, pIv != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
1289
1290                 pIv->data = static_cast< unsigned char* >(OPENSSL_malloc(ivBuffer.GetRemaining()));
1291                 SysTryReturn(NID_SEC_CRYPTO, pIv->data != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
1292
1293                 memcpy(pIv->data, ivBuffer.GetPointer(), ivBuffer.GetRemaining());
1294
1295                 pIv->length = ivBuffer.GetRemaining();
1296
1297                 ret = X509_ALGOR_set0(pAlgoObj, OBJ_nid2obj(algoNid), V_ASN1_OCTET_STRING, reinterpret_cast< void* >(pIv));
1298                 SysTryReturn(NID_SEC_CRYPTO, ret > 0, null, E_SYSTEM, "[E_SYSTEM] The method cannot proceed due to a severe system error.");
1299         }
1300         break;
1301
1302         case _OID_TYPE_RC2_CBC:
1303         {
1304                 SysTryReturn(NID_SEC_CRYPTO, pAlgoParam != null, null, E_INVALID_ARG, "[E_INVALID_ARG] The specified input parameter is invalid.");
1305
1306                 ivBuffer.Construct(((dynamic_cast< Rc2CbcParameters* >(pAlgoParam))->GetInitialVector()));
1307                 SysTryReturn(NID_SEC_CRYPTO, ivBuffer.GetRemaining() > 0, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
1308
1309                 version = (dynamic_cast< Rc2CbcParameters* >(pAlgoParam))->GetVersion();
1310
1311                 if (version == 0)
1312                 {
1313                         pIv = ASN1_STRING_new();
1314                         SysTryReturn(NID_SEC_CRYPTO, pIv != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
1315
1316                         pIv->data = static_cast< unsigned char* >(OPENSSL_malloc(ivBuffer.GetRemaining()));
1317                         SysTryReturn(NID_SEC_CRYPTO, pIv->data != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
1318
1319                         memcpy(pIv->data, ivBuffer.GetPointer(), ivBuffer.GetRemaining());
1320
1321                         pIv->length = ivBuffer.GetRemaining();
1322
1323                         ret = X509_ALGOR_set0(pAlgoObj, OBJ_nid2obj(algoNid), V_ASN1_OCTET_STRING, reinterpret_cast< void* >(pIv));
1324                         SysTryReturn(NID_SEC_CRYPTO, ret > 0, null, E_SYSTEM, "[E_SYSTEM] The method cannot proceed due to a severe system error.");
1325
1326                 }
1327                 else
1328                 {
1329
1330                         pAsn1Type = ASN1_TYPE_new();
1331                         SysTryReturn(NID_SEC_CRYPTO, pAsn1Type != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
1332
1333                         ret = ASN1_TYPE_set_int_octetstring(pAsn1Type, version, const_cast< unsigned char* >(ivBuffer.GetPointer()), ivBuffer.GetRemaining());
1334                         SysTryReturn(NID_SEC_CRYPTO, ret > 0, null, E_SYSTEM, "[E_SYSTEM] The method cannot proceed due to a severe system error.");
1335
1336                         ret = X509_ALGOR_set0(pAlgoObj, OBJ_nid2obj(algoNid), V_ASN1_SEQUENCE, reinterpret_cast< void* >(pAsn1Type->value.ptr));
1337                         SysTryReturn(NID_SEC_CRYPTO, ret > 0, null, E_SYSTEM, "[E_SYSTEM] The method cannot proceed due to a severe system error.");
1338
1339                 }
1340
1341         }
1342         break;
1343
1344         case _OID_TYPE_HMAC_SHA1:
1345         // fall through
1346         case _OID_TYPE_HMAC_SHA2_224:
1347         // fall through
1348         case _OID_TYPE_HMAC_SHA2_256:
1349         // fall through
1350         case _OID_TYPE_HMAC_SHA2_384:
1351         // fall through
1352         case _OID_TYPE_RSA_ENCRYPTION:
1353         // fall through
1354         case _OID_TYPE_HMAC_SHA2_512:
1355         {
1356                 ret = X509_ALGOR_set0(pAlgoObj, OBJ_nid2obj(algoNid), V_ASN1_NULL, NULL);
1357                 SysTryReturn(NID_SEC_CRYPTO, ret > 0, null, E_SYSTEM, "[E_SYSTEM] The method cannot proceed due to a severe system error.");
1358         }
1359         break;
1360
1361         case _OID_TYPE_PBKDF2:
1362         {
1363                 SysTryReturn(NID_SEC_CRYPTO, pAlgoParam != null, null, E_INVALID_ARG, "[E_INVALID_ARG] The specified input parameter is invalid.");
1364
1365                 std::unique_ptr< ByteBuffer > pKdfBuffer((dynamic_cast< Pkcs05PbKdf2Parameters* >(pAlgoParam))->GetEncodedDataN());
1366                 if (pKdfBuffer == null)
1367                 {
1368                         r = GetLastResult();
1369                         if (r == E_OUT_OF_MEMORY)
1370                         {
1371                                 SysTryReturn(NID_SEC_CRYPTO, pKdfBuffer, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
1372
1373                         }
1374                         else
1375                         {
1376                                 SysTryReturn(NID_SEC_CRYPTO, pKdfBuffer, null, E_SYSTEM, "[E_SYSTEM] The method cannot proceed due to a severe system error.");
1377                         }
1378
1379                 }
1380
1381                 pBuf = pKdfBuffer->GetPointer();
1382                 SysTryReturn(NID_SEC_CRYPTO, pBuf != null, null, E_INVALID_ARG, "[E_INVALID_ARG] The specified input parameter is invalid.");
1383
1384                 pKdf = d2i_PBKDF2PARAM(null, reinterpret_cast< const unsigned char** >(&pBuf), pKdfBuffer->GetRemaining());
1385                 SysTryReturn(NID_SEC_CRYPTO, pKdf != null, null, E_SYSTEM, "[E_SYSTEM] The method cannot proceed due to a severe system error.");
1386
1387                 ASN1_item_pack(pKdf, ASN1_ITEM_rptr(PBKDF2PARAM), &pbKdf2Str);
1388
1389                 SysTryReturn(NID_SEC_CRYPTO, pbKdf2Str != null, null, E_SYSTEM, "[E_SYSTEM] The method cannot proceed due to a severe system error.");
1390
1391                 ret = X509_ALGOR_set0(pAlgoObj, OBJ_nid2obj(algoNid), V_ASN1_SEQUENCE, pbKdf2Str);
1392                 SysTryReturn(NID_SEC_CRYPTO, ret > 0, null, E_SYSTEM, "[E_SYSTEM] The method cannot proceed due to a severe system error.");
1393         }
1394         break;
1395
1396         case _OID_TYPE_PBES2:
1397         {
1398
1399                 SysTryReturn(NID_SEC_CRYPTO, pAlgoParam != null, null, E_INVALID_ARG, "[E_INVALID_ARG] The specified input parameter is invalid.");
1400
1401                 std::unique_ptr< ByteBuffer > pbEs2Buffer((dynamic_cast< Pkcs05PbEs2Parameters* >(pAlgoParam))->GetEncodedDataN());
1402                 if (pbEs2Buffer == null)
1403                 {
1404                         r = GetLastResult();
1405                         if (r == E_OUT_OF_MEMORY)
1406                         {
1407                                 SysTryReturn(NID_SEC_CRYPTO, pbEs2Buffer, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
1408
1409                         }
1410                         else
1411                         {
1412                                 SysTryReturn(NID_SEC_CRYPTO, pbEs2Buffer, null, E_SYSTEM, "[E_SYSTEM] The method cannot proceed due to a severe system error.");
1413                         }
1414
1415                 }
1416
1417                 pBuf = pbEs2Buffer->GetPointer();
1418                 SysTryReturn(NID_SEC_CRYPTO, pBuf != null, null, E_INVALID_ARG, "[E_INVALID_ARG] The specified input parameter is invalid.");
1419
1420                 pPbes2Obj = d2i_PBE2PARAM(null, reinterpret_cast< const unsigned char** >(&pBuf), pbEs2Buffer->GetRemaining());
1421                 SysTryReturn(NID_SEC_CRYPTO, pPbes2Obj != null, null, E_SYSTEM, "[E_SYSTEM] The method cannot proceed due to a severe system error.");
1422
1423                 ASN1_item_pack(pPbes2Obj, ASN1_ITEM_rptr(PBE2PARAM), &pbE2Str);
1424
1425                 ret = X509_ALGOR_set0(pAlgoObj, OBJ_nid2obj(algoNid), V_ASN1_SEQUENCE, pbE2Str);
1426                 SysTryReturn(NID_SEC_CRYPTO, pbE2Str != null, null, E_SYSTEM, "[E_SYSTEM] The method cannot proceed due to a severe system error.");
1427
1428                 SysTryReturn(NID_SEC_CRYPTO, ret > 0, null, E_SYSTEM, "[E_SYSTEM] The method cannot proceed due to a severe system error.");
1429         }
1430         break;
1431
1432         case _OID_TYPE_PBMAC1:
1433         {
1434                 SysTryReturn(NID_SEC_CRYPTO, pAlgoParam != null, null, E_INVALID_ARG, "[E_INVALID_ARG] The specified input parameter is invalid.");
1435
1436                 std::unique_ptr< ByteBuffer > pbMacBuffer((dynamic_cast< Pkcs05PbMacParameters* >(pAlgoParam))->GetEncodedDataN());
1437                 if (pbMacBuffer == null)
1438                 {
1439                         r = GetLastResult();
1440                         if (r == E_OUT_OF_MEMORY)
1441                         {
1442                                 SysTryReturn(NID_SEC_CRYPTO, pbMacBuffer, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
1443
1444                         }
1445                         else
1446                         {
1447                                 SysTryReturn(NID_SEC_CRYPTO, pbMacBuffer, null, E_SYSTEM, "[E_SYSTEM] The method cannot proceed due to a severe system error.");
1448                         }
1449
1450                 }
1451
1452                 pBuf = pbMacBuffer->GetPointer();
1453                 SysTryReturn(NID_SEC_CRYPTO, pBuf != null, null, E_INVALID_ARG, "[E_INVALID_ARG] The specified input parameter is invalid.");
1454
1455                 pMacObj = d2i_PBE2PARAM(null, reinterpret_cast< const unsigned char** >(&pBuf), pbMacBuffer->GetRemaining());
1456                 SysTryReturn(NID_SEC_CRYPTO, pMacObj != null, null, E_SYSTEM, "[E_SYSTEM] The method cannot proceed due to a severe system error.");
1457
1458                 ASN1_item_pack(pMacObj, ASN1_ITEM_rptr(PBE2PARAM), &pbE2Str);
1459
1460                 ret = X509_ALGOR_set0(pAlgoObj, OBJ_nid2obj(algoNid), V_ASN1_SEQUENCE, pbE2Str);
1461                 SysTryReturn(NID_SEC_CRYPTO, ret > 0, null, E_SYSTEM, "[E_SYSTEM] The method cannot proceed due to a severe system error.");
1462         }
1463         break;
1464
1465         default:
1466                 r = E_UNSUPPORTED_ALGORITHM;
1467                 break;
1468         }
1469
1470         return pAlgoObj;
1471
1472 }
1473
1474
1475
1476 } } } //OSP::SECURITY::PKCS