4eca2f613cfa2b08aafeb44eaeb9502f016add7c
[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         if (dataLen % pCipherAlgorithm->block_size != 0)
885         {
886                 // set padding
887                 ret = EVP_CIPHER_CTX_set_padding(&cipherCtx, static_cast< int >(true));
888                 SysTryCatch(NID_SEC_CRYPTO, ret == 1, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] The method cannot proceed due to a severe system error.");
889         }
890
891         //cipher update operation
892         ret = EVP_CipherUpdate(&cipherCtx, pOut.get(), static_cast< int* >(&outLen), pData, dataLen);
893         SysTryCatch(NID_SEC_CRYPTO, ret == 1, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] The method cannot proceed due to a severe system error.");
894
895         //cipher final operation
896         ret = EVP_CipherFinal(&cipherCtx, pOut.get() + outLen, &finalLen);
897         SysTryCatch(NID_SEC_CRYPTO, ret == 1, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] The method cannot proceed due to a severe system error.");
898         outLen = outLen + finalLen;
899
900         SysTryCatch(NID_SEC_CRYPTO, outLen <= tempLen, r = E_OVERFLOW, E_OVERFLOW, "[E_OVERFLOW] Length mismatch resulting in overflow.");
901
902         //creating bytebuffer and storing output data from cipher final operation in it
903         pOutBuffer = std::unique_ptr< ByteBuffer >(new (std::nothrow) ByteBuffer());
904         SysTryCatch(NID_SEC_CRYPTO, pOutBuffer, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
905
906         r = pOutBuffer->Construct(outLen);
907         SysTryCatch(NID_SEC_CRYPTO, !IsFailed(r), r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
908
909         r = pOutBuffer->SetArray(pOut.get(), 0, outLen);
910         SysTryCatch(NID_SEC_CRYPTO, !IsFailed(r), r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] The method cannot proceed due to a severe system error.");
911
912         pOutBuffer->Flip();
913
914 CATCH:
915
916         if (IsFailed(r))
917         {
918                 pOutBuffer.reset(null);
919         }
920         EVP_CIPHER_CTX_cleanup(&cipherCtx);
921
922         SetLastResult(r);
923         return pOutBuffer.release();
924
925 }
926
927 X509_ALGOR*
928 _PkcsUtility::GenerateKdfParametersN(int iter, unsigned char* pSaltValue, int saltLen, int prfNid, int keyLen)
929 {
930         result r = E_SUCCESS;
931         X509_ALGOR* pKeyfunc = null;
932         PBKDF2PARAM* pKdf = null;
933         ASN1_OCTET_STRING* pSalt = null;
934         int ret = 0;
935
936
937         ClearLastResult();
938
939         pKdf = PBKDF2PARAM_new();
940         SysTryReturn(NID_SEC_CRYPTO, pKdf != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
941
942         pSalt = M_ASN1_OCTET_STRING_new();
943         SysTryCatch(NID_SEC_CRYPTO, pSalt != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
944
945         pKdf->salt->value.octet_string = pSalt;
946         pKdf->salt->type = V_ASN1_OCTET_STRING;
947
948         pSalt->data = static_cast< unsigned char* >(OPENSSL_malloc(saltLen));
949         SysTryCatch(NID_SEC_CRYPTO, pSalt->data != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
950
951         pSalt->length = saltLen;
952
953         memcpy(pSalt->data, pSaltValue, saltLen);
954
955         ASN1_INTEGER_set(pKdf->iter, iter);
956
957         if (keyLen > 0)
958         {
959
960                 pKdf->keylength = M_ASN1_INTEGER_new();
961                 SysTryCatch(NID_SEC_CRYPTO, pKdf->keylength != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
962
963                 ASN1_INTEGER_set(pKdf->keylength, keyLen);
964
965         }
966
967         if (prfNid != NID_hmacWithSHA1)
968         {
969
970                 pKdf->prf = X509_ALGOR_new();
971                 SysTryCatch(NID_SEC_CRYPTO, pKdf->prf != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
972
973                 ret = X509_ALGOR_set0(pKdf->prf, OBJ_nid2obj(prfNid), V_ASN1_NULL, NULL);
974                 SysTryCatch(NID_SEC_CRYPTO, ret > 0, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] The method cannot proceed due to a severe system error.");
975
976         }
977
978         pKeyfunc = X509_ALGOR_new();
979         SysTryCatch(NID_SEC_CRYPTO, pKeyfunc != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
980
981         pKeyfunc->algorithm = OBJ_nid2obj(NID_id_pbkdf2);
982
983         pKeyfunc->parameter = ASN1_TYPE_new();
984         ASN1_item_pack(pKdf, ASN1_ITEM_rptr(PBKDF2PARAM), &pKeyfunc->parameter->value.sequence);
985
986         pKeyfunc->parameter->type = V_ASN1_SEQUENCE;
987
988         PBKDF2PARAM_free(pKdf);
989         return pKeyfunc;
990
991 CATCH:
992
993         PBKDF2PARAM_free(pKdf);
994         X509_ALGOR_free(pKeyfunc);
995         SetLastResult(r);
996         return null;
997
998 }
999
1000 IAlgorithmParameters*
1001 _PkcsUtility::GernerateParametersFromOidN(Tizen::Base::String algoOid, X509_ALGOR* pAlgoObj)
1002 {
1003         result r = E_SUCCESS;
1004         _OidType oidValue;
1005         int pType = 0;
1006         int dataLen = 0;
1007         int ret = 0;
1008         void* pVal = null;
1009         long num = 0;
1010         unsigned char iv[EVP_MAX_IV_LENGTH];
1011         const unsigned char* pData = null;
1012         ASN1_STRING* pStr = null;
1013         ASN1_TYPE* pAsn1Type = null;
1014         Tizen::Base::ByteBuffer ivBuffer;
1015         Tizen::Base::ByteBuffer ivBuffer1;
1016         Tizen::Base::ByteBuffer paramBuffer;
1017         Pkcs05PbEs2Parameters pbEs2Obj;
1018         Pkcs05PbKdf2Parameters kdfObj;
1019         Rc2CbcParameters rc2CbcObj;
1020         Pkcs05PbMacParameters pbMacObj;
1021         InitialVector ivObj;
1022         std::unique_ptr< IAlgorithmParameters > pAlgoParams;
1023
1024
1025         ClearLastResult();
1026
1027         oidValue = _PkcsUtility::ConvertOidToEnum(algoOid);
1028
1029         switch (oidValue)
1030         {
1031         case _OID_TYPE_DES_CBC:
1032         // fall through
1033         case _OID_TYPE_DES_CBC_EDE3:
1034         // fall through
1035         case _OID_TYPE_AES_128_CBC:
1036         // fall through
1037         case _OID_TYPE_AES_192_CBC:
1038         // fall through
1039         case _OID_TYPE_AES_256_CBC:
1040         {
1041                 X509_ALGOR_get0(&pAlgoObj->algorithm, &pType, &pVal, pAlgoObj);
1042                 SysTryReturn(NID_SEC_CRYPTO, pType == V_ASN1_OCTET_STRING, null, E_INVALID_ARG, "[E_INVALID_ARG] The specified input parameter is invalid.");
1043
1044                 SysTryReturn(NID_SEC_CRYPTO, pVal != null, null, E_INVALID_ARG, "[E_INVALID_ARG] The specified input parameter is invalid.");
1045
1046                 pStr = reinterpret_cast< ASN1_STRING* >(pVal);
1047                 pData = pStr->data;
1048                 dataLen = pStr->length;
1049
1050                 r = ivBuffer.Construct(dataLen);
1051                 SysTryReturn(NID_SEC_CRYPTO, !IsFailed(r), null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
1052
1053                 r = ivBuffer.SetArray(pData, 0, dataLen);
1054                 SysTryReturn(NID_SEC_CRYPTO, !IsFailed(r), null, E_INVALID_ARG, "[E_INVALID_ARG] The specified input parameter is invalid.");
1055
1056                 ivBuffer.Flip();
1057
1058                 r = ivObj.Construct(ivBuffer);
1059                 SysTryReturn(NID_SEC_CRYPTO, !IsFailed(r), null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
1060
1061                 pAlgoParams = std::unique_ptr< IAlgorithmParameters >(ivObj.CloneN());
1062                 SysTryReturn(NID_SEC_CRYPTO, pAlgoParams, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
1063
1064                 return pAlgoParams.release();
1065         }
1066         break;
1067
1068         case _OID_TYPE_RC2_CBC:
1069         {
1070                 X509_ALGOR_get0(&pAlgoObj->algorithm, &pType, &pVal, pAlgoObj);
1071                 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.");
1072
1073                 SysTryReturn(NID_SEC_CRYPTO, pVal != null, null, E_INVALID_ARG, "[E_INVALID_ARG] The specified input parameter is invalid.");
1074
1075                 pStr = reinterpret_cast< ASN1_STRING* >(pVal);
1076                 pData = pStr->data;
1077                 dataLen = pStr->length;
1078
1079
1080                 if (pType == V_ASN1_OCTET_STRING)
1081                 {
1082                         r = ivBuffer.Construct(dataLen);
1083                         SysTryReturn(NID_SEC_CRYPTO, !IsFailed(r), null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
1084
1085                         r = ivBuffer.SetArray(pData, 0, dataLen);
1086                         SysTryReturn(NID_SEC_CRYPTO, !IsFailed(r), null, E_INVALID_ARG, "[E_INVALID_ARG] The specified input parameter is invalid.");
1087
1088                         ivBuffer.Flip();
1089
1090                         r = rc2CbcObj.Construct(ivBuffer);
1091                         SysTryReturn(NID_SEC_CRYPTO, !IsFailed(r), null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
1092
1093                 }
1094                 else
1095                 {
1096
1097                         r = ivBuffer.Construct(dataLen);
1098                         SysTryReturn(NID_SEC_CRYPTO, !IsFailed(r), null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
1099
1100                         r = ivBuffer.SetArray(pData, 0, dataLen);
1101                         SysTryReturn(NID_SEC_CRYPTO, !IsFailed(r), null, E_INVALID_ARG, "[E_INVALID_ARG] The specified input parameter is invalid.");
1102
1103                         ivBuffer.Flip();
1104
1105                         const byte* pBuffer = ivBuffer.GetPointer();
1106
1107                         pAsn1Type = d2i_ASN1_TYPE(null, reinterpret_cast< const unsigned char** >(&pBuffer), ivBuffer.GetRemaining());
1108                         SysTryReturn(NID_SEC_CRYPTO, pAsn1Type != null, null, E_INVALID_ARG, "[E_INVALID_ARG] The specified input parameter is invalid.");
1109
1110                         memset(iv, 0, sizeof(iv));
1111
1112                         ret = ASN1_TYPE_get_int_octetstring(pAsn1Type, &num, iv, sizeof(iv));
1113                         SysTryReturn(NID_SEC_CRYPTO, ret > 0, null, E_INVALID_ARG, "[E_INVALID_ARG] The specified input parameter is invalid.");
1114
1115                         r = ivBuffer1.Construct(ret);
1116                         SysTryReturn(NID_SEC_CRYPTO, !IsFailed(r), null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
1117
1118                         r = ivBuffer1.SetArray(iv, 0, ret);
1119                         SysTryReturn(NID_SEC_CRYPTO, !IsFailed(r), null, E_INVALID_ARG, "[E_INVALID_ARG] The specified input parameter is invalid.");
1120
1121                         ivBuffer1.Flip();
1122
1123                         r = rc2CbcObj.Construct(ivBuffer1, num);
1124                         SysTryReturn(NID_SEC_CRYPTO, !IsFailed(r), null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
1125
1126                 }
1127
1128                 pAlgoParams = std::unique_ptr< IAlgorithmParameters >(rc2CbcObj.CloneN());
1129                 SysTryReturn(NID_SEC_CRYPTO, pAlgoParams, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
1130
1131                 return pAlgoParams.release();
1132
1133         }
1134         break;
1135
1136         case _OID_TYPE_HMAC_SHA1:
1137         // fall through
1138         case _OID_TYPE_HMAC_SHA2_224:
1139         // fall through
1140         case _OID_TYPE_HMAC_SHA2_256:
1141         // fall through
1142         case _OID_TYPE_HMAC_SHA2_384:
1143         // fall through
1144         case _OID_TYPE_HMAC_SHA2_512:
1145         // fall through
1146         case _OID_TYPE_RSA_ENCRYPTION:
1147         {
1148                 pAlgoParams = null;
1149                 return pAlgoParams.release();
1150         }
1151         break;
1152
1153         case _OID_TYPE_PBKDF2:
1154         {
1155                 X509_ALGOR_get0(&(pAlgoObj->algorithm), &pType, &pVal, pAlgoObj);
1156                 SysTryReturn(NID_SEC_CRYPTO, pType == V_ASN1_SEQUENCE, null, E_INVALID_ARG, "[E_INVALID_ARG] The specified input parameter is invalid.");
1157
1158                 SysTryReturn(NID_SEC_CRYPTO, pVal != null, null, E_INVALID_ARG, "[E_INVALID_ARG] The specified input parameter is invalid.");
1159
1160                 pStr = reinterpret_cast< ASN1_STRING* >(pVal);
1161                 pData = pStr->data;
1162                 dataLen = pStr->length;
1163
1164                 r = paramBuffer.Construct(dataLen);
1165                 SysTryReturn(NID_SEC_CRYPTO, !IsFailed(r), null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
1166
1167                 r = paramBuffer.SetArray(pData, 0, dataLen);
1168                 SysTryReturn(NID_SEC_CRYPTO, !IsFailed(r), null, E_INVALID_ARG, "[E_INVALID_ARG] The specified input parameter is invalid.");
1169
1170                 paramBuffer.Flip();
1171
1172                 r = kdfObj.Construct(paramBuffer);
1173                 SysTryReturn(NID_SEC_CRYPTO, !IsFailed(r), null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
1174
1175                 pAlgoParams = std::unique_ptr< IAlgorithmParameters >(kdfObj.CloneN());
1176                 SysTryReturn(NID_SEC_CRYPTO, pAlgoParams, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
1177
1178                 return pAlgoParams.release();
1179         }
1180         break;
1181
1182         case _OID_TYPE_PBES2:
1183         {
1184                 X509_ALGOR_get0(&(pAlgoObj->algorithm), &pType, &pVal, pAlgoObj);
1185                 SysTryReturn(NID_SEC_CRYPTO, pType == V_ASN1_SEQUENCE, null, E_INVALID_ARG, "[E_INVALID_ARG] The specified input parameter is invalid.");
1186
1187                 SysTryReturn(NID_SEC_CRYPTO, pVal != null, null, E_INVALID_ARG, "[E_INVALID_ARG] The specified input parameter is invalid.");
1188
1189                 pStr = reinterpret_cast< ASN1_STRING* >(pVal);
1190                 pData = pStr->data;
1191                 dataLen = pStr->length;
1192
1193                 r = paramBuffer.Construct(dataLen);
1194                 SysTryReturn(NID_SEC_CRYPTO, !IsFailed(r), null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
1195
1196                 r = paramBuffer.SetArray(pData, 0, dataLen);
1197                 SysTryReturn(NID_SEC_CRYPTO, !IsFailed(r), null, E_INVALID_ARG, "[E_INVALID_ARG] The specified input parameter is invalid.");
1198
1199                 paramBuffer.Flip();
1200
1201                 r = pbEs2Obj.Construct(paramBuffer);
1202                 SysTryReturn(NID_SEC_CRYPTO, !IsFailed(r), null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
1203
1204                 pAlgoParams = std::unique_ptr< IAlgorithmParameters >(pbEs2Obj.CloneN());
1205                 SysTryReturn(NID_SEC_CRYPTO, pAlgoParams, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
1206
1207                 return pAlgoParams.release();
1208         }
1209         break;
1210
1211         case _OID_TYPE_PBMAC1:
1212         {
1213                 X509_ALGOR_get0(&pAlgoObj->algorithm, &pType, &pVal, pAlgoObj);
1214                 SysTryReturn(NID_SEC_CRYPTO, pType == V_ASN1_SEQUENCE, null, E_INVALID_ARG, "[E_INVALID_ARG] The specified input parameter is invalid.");
1215
1216                 SysTryReturn(NID_SEC_CRYPTO, pVal != null, null, E_INVALID_ARG, "[E_INVALID_ARG] The specified input parameter is invalid.");
1217                 pStr = reinterpret_cast< ASN1_STRING* >(pVal);
1218                 pData = pStr->data;
1219                 dataLen = pStr->length;
1220
1221                 r = paramBuffer.Construct(dataLen);
1222                 SysTryReturn(NID_SEC_CRYPTO, !IsFailed(r), null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
1223
1224                 r = paramBuffer.SetArray(pData, 0, dataLen);
1225                 SysTryReturn(NID_SEC_CRYPTO, !IsFailed(r), null, E_INVALID_ARG, "[E_INVALID_ARG] The specified input parameter is invalid.");
1226
1227                 paramBuffer.Flip();
1228
1229                 r = pbMacObj.Construct(paramBuffer);
1230                 SysTryReturn(NID_SEC_CRYPTO, !IsFailed(r), null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
1231
1232                 pAlgoParams = std::unique_ptr< IAlgorithmParameters >(pbMacObj.CloneN());
1233                 SysTryReturn(NID_SEC_CRYPTO, pAlgoParams, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
1234
1235                 return pAlgoParams.release();
1236         }
1237         break;
1238
1239         default:
1240
1241                 r = E_UNSUPPORTED_ALGORITHM;
1242                 break;
1243         }
1244
1245         SetLastResult(r);
1246         return pAlgoParams.release();
1247
1248 }
1249
1250 X509_ALGOR*
1251 _PkcsUtility::GenerateAlgorithmIdentifierStructureN(Tizen::Base::String algoOid, IAlgorithmParameters* pAlgoParam)
1252 {
1253         result r = E_SUCCESS;
1254         X509_ALGOR* pAlgoObj = null;
1255         _OidType oidValue;
1256         int ret = 0;
1257         int algoNid = 0;
1258         int version = 0;
1259         const byte* pBuf = null;
1260         PBKDF2PARAM* pKdf = null;
1261         PBE2PARAM* pPbes2Obj = null;
1262         PBE2PARAM* pMacObj = null;
1263         ASN1_STRING* pbKdf2Str = null;
1264         ASN1_STRING* pbE2Str = null;
1265         ASN1_STRING* pIv = null;
1266         ASN1_TYPE* pAsn1Type = null;
1267         Tizen::Base::ByteBuffer ivBuffer;
1268
1269         ClearLastResult();
1270
1271         pAlgoObj = X509_ALGOR_new();
1272         SysTryReturn(NID_SEC_CRYPTO, pAlgoObj != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
1273
1274         oidValue = _PkcsUtility::ConvertOidToEnum(algoOid);
1275         algoNid = _PkcsUtility::ConvertToNid(algoOid);
1276
1277         switch (oidValue)
1278         {
1279         case _OID_TYPE_DES_CBC:
1280         // fall through
1281         case _OID_TYPE_DES_CBC_EDE3:
1282         // fall through
1283         case _OID_TYPE_AES_128_CBC:
1284         // fall through
1285         case _OID_TYPE_AES_192_CBC:
1286         // fall through
1287         case _OID_TYPE_AES_256_CBC:
1288         {
1289                 InitialVector* pInitialVector = dynamic_cast< InitialVector* >(pAlgoParam);
1290                 SysTryCatch(NID_SEC_CRYPTO, pInitialVector != null, r = E_INVALID_ARG, E_INVALID_ARG, "[E_INVALID_ARG] The specified input parameter is invalid.");
1291
1292                 ivBuffer.Construct(pInitialVector->GetInitialVector());
1293                 SysTryCatch(NID_SEC_CRYPTO, ivBuffer.GetRemaining() > 0, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
1294
1295                 pIv = ASN1_STRING_new();
1296                 SysTryCatch(NID_SEC_CRYPTO, pIv != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
1297
1298                 pIv->data = static_cast< unsigned char* >(OPENSSL_malloc(ivBuffer.GetRemaining()));
1299                 SysTryCatch(NID_SEC_CRYPTO, pIv->data != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
1300
1301                 memcpy(pIv->data, ivBuffer.GetPointer(), ivBuffer.GetRemaining());
1302
1303                 pIv->length = ivBuffer.GetRemaining();
1304
1305                 ret = X509_ALGOR_set0(pAlgoObj, OBJ_nid2obj(algoNid), V_ASN1_OCTET_STRING, reinterpret_cast< void* >(pIv));
1306                 SysTryCatch(NID_SEC_CRYPTO, ret > 0, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] The method cannot proceed due to a severe system error.");
1307         }
1308         break;
1309
1310         case _OID_TYPE_RC2_CBC:
1311         {
1312                 Rc2CbcParameters* pRc2CbcParams = dynamic_cast< Rc2CbcParameters* >(pAlgoParam);
1313                 SysTryCatch(NID_SEC_CRYPTO, pRc2CbcParams != null, r = E_INVALID_ARG, E_INVALID_ARG, "[E_INVALID_ARG] The specified input parameter is invalid.");
1314
1315                 ivBuffer.Construct(pRc2CbcParams->GetInitialVector());
1316                 SysTryCatch(NID_SEC_CRYPTO, ivBuffer.GetRemaining() > 0, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
1317
1318                 version = (pRc2CbcParams)->GetVersion();
1319
1320                 if (version == 0)
1321                 {
1322                         pIv = ASN1_STRING_new();
1323                         SysTryCatch(NID_SEC_CRYPTO, pIv != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
1324
1325                         pIv->data = static_cast< unsigned char* >(OPENSSL_malloc(ivBuffer.GetRemaining()));
1326                         SysTryCatch(NID_SEC_CRYPTO, pIv->data != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
1327
1328                         memcpy(pIv->data, ivBuffer.GetPointer(), ivBuffer.GetRemaining());
1329
1330                         pIv->length = ivBuffer.GetRemaining();
1331
1332                         ret = X509_ALGOR_set0(pAlgoObj, OBJ_nid2obj(algoNid), V_ASN1_OCTET_STRING, reinterpret_cast< void* >(pIv));
1333                         SysTryCatch(NID_SEC_CRYPTO, ret > 0, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] The method cannot proceed due to a severe system error.");
1334
1335                 }
1336                 else
1337                 {
1338
1339                         pAsn1Type = ASN1_TYPE_new();
1340                         SysTryCatch(NID_SEC_CRYPTO, pAsn1Type != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
1341
1342                         ret = ASN1_TYPE_set_int_octetstring(pAsn1Type, version, const_cast< unsigned char* >(ivBuffer.GetPointer()), ivBuffer.GetRemaining());
1343                         SysTryCatch(NID_SEC_CRYPTO, ret > 0, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] The method cannot proceed due to a severe system error.");
1344
1345                         ret = X509_ALGOR_set0(pAlgoObj, OBJ_nid2obj(algoNid), V_ASN1_SEQUENCE, reinterpret_cast< void* >(pAsn1Type->value.ptr));
1346                         SysTryCatch(NID_SEC_CRYPTO, ret > 0, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] The method cannot proceed due to a severe system error.");
1347
1348                 }
1349
1350         }
1351         break;
1352
1353         case _OID_TYPE_HMAC_SHA1:
1354         // fall through
1355         case _OID_TYPE_HMAC_SHA2_224:
1356         // fall through
1357         case _OID_TYPE_HMAC_SHA2_256:
1358         // fall through
1359         case _OID_TYPE_HMAC_SHA2_384:
1360         // fall through
1361         case _OID_TYPE_RSA_ENCRYPTION:
1362         // fall through
1363         case _OID_TYPE_HMAC_SHA2_512:
1364         {
1365                 ret = X509_ALGOR_set0(pAlgoObj, OBJ_nid2obj(algoNid), V_ASN1_NULL, NULL);
1366                 SysTryCatch(NID_SEC_CRYPTO, ret > 0, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] The method cannot proceed due to a severe system error.");
1367         }
1368         break;
1369
1370         case _OID_TYPE_PBKDF2:
1371         {
1372                 Pkcs05PbKdf2Parameters* pPkcs05PbKdf2Param = dynamic_cast< Pkcs05PbKdf2Parameters* >(pAlgoParam);
1373                 SysTryCatch(NID_SEC_CRYPTO, pPkcs05PbKdf2Param != null, r = E_INVALID_ARG, E_INVALID_ARG, "[E_INVALID_ARG] The specified input parameter is invalid.");
1374
1375                 std::unique_ptr< ByteBuffer > pKdfBuffer(pPkcs05PbKdf2Param->GetEncodedDataN());
1376                 if (pKdfBuffer == null)
1377                 {
1378                         r = GetLastResult();
1379                         if (r == E_OUT_OF_MEMORY)
1380                         {
1381                                 SysTryCatch(NID_SEC_CRYPTO, pKdfBuffer, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
1382
1383                         }
1384                         else
1385                         {
1386                                 SysTryCatch(NID_SEC_CRYPTO, pKdfBuffer, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] The method cannot proceed due to a severe system error.");
1387                         }
1388
1389                 }
1390
1391                 pBuf = pKdfBuffer->GetPointer();
1392                 SysTryCatch(NID_SEC_CRYPTO, pBuf != null, r = E_INVALID_ARG, E_INVALID_ARG, "[E_INVALID_ARG] The specified input parameter is invalid.");
1393
1394                 pKdf = d2i_PBKDF2PARAM(null, reinterpret_cast< const unsigned char** >(&pBuf), pKdfBuffer->GetRemaining());
1395                 SysTryCatch(NID_SEC_CRYPTO, pKdf != null, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] The method cannot proceed due to a severe system error.");
1396
1397                 ASN1_item_pack(pKdf, ASN1_ITEM_rptr(PBKDF2PARAM), &pbKdf2Str);
1398
1399                 SysTryCatch(NID_SEC_CRYPTO, pbKdf2Str != null, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] The method cannot proceed due to a severe system error.");
1400
1401                 ret = X509_ALGOR_set0(pAlgoObj, OBJ_nid2obj(algoNid), V_ASN1_SEQUENCE, pbKdf2Str);
1402                 SysTryCatch(NID_SEC_CRYPTO, ret > 0, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] The method cannot proceed due to a severe system error.");
1403         }
1404         break;
1405
1406         case _OID_TYPE_PBES2:
1407         {
1408                 Pkcs05PbEs2Parameters* pPkcs05PbEs2Param = dynamic_cast< Pkcs05PbEs2Parameters* >(pAlgoParam);
1409                 SysTryCatch(NID_SEC_CRYPTO, pPkcs05PbEs2Param != null, r = E_INVALID_ARG, E_INVALID_ARG, "[E_INVALID_ARG] The specified input parameter is invalid.");
1410
1411                 std::unique_ptr< ByteBuffer > pbEs2Buffer(pPkcs05PbEs2Param->GetEncodedDataN());
1412                 if (pbEs2Buffer == null)
1413                 {
1414                         r = GetLastResult();
1415                         if (r == E_OUT_OF_MEMORY)
1416                         {
1417                                 SysTryCatch(NID_SEC_CRYPTO, pbEs2Buffer, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
1418
1419                         }
1420                         else
1421                         {
1422                                 SysTryCatch(NID_SEC_CRYPTO, pbEs2Buffer, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] The method cannot proceed due to a severe system error.");
1423                         }
1424
1425                 }
1426
1427                 pBuf = pbEs2Buffer->GetPointer();
1428                 SysTryCatch(NID_SEC_CRYPTO, pBuf != null, r = E_INVALID_ARG, E_INVALID_ARG, "[E_INVALID_ARG] The specified input parameter is invalid.");
1429
1430                 pPbes2Obj = d2i_PBE2PARAM(null, reinterpret_cast< const unsigned char** >(&pBuf), pbEs2Buffer->GetRemaining());
1431                 SysTryCatch(NID_SEC_CRYPTO, pPbes2Obj != null, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] The method cannot proceed due to a severe system error.");
1432
1433                 ASN1_item_pack(pPbes2Obj, ASN1_ITEM_rptr(PBE2PARAM), &pbE2Str);
1434
1435                 ret = X509_ALGOR_set0(pAlgoObj, OBJ_nid2obj(algoNid), V_ASN1_SEQUENCE, pbE2Str);
1436                 SysTryCatch(NID_SEC_CRYPTO, pbE2Str != null, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] The method cannot proceed due to a severe system error.");
1437
1438                 SysTryCatch(NID_SEC_CRYPTO, ret > 0, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] The method cannot proceed due to a severe system error.");
1439         }
1440         break;
1441
1442         case _OID_TYPE_PBMAC1:
1443         {
1444                 Pkcs05PbMacParameters* pPkcs05PbMacParam = dynamic_cast< Pkcs05PbMacParameters* >(pAlgoParam);
1445                 SysTryCatch(NID_SEC_CRYPTO, pPkcs05PbMacParam != null, r = E_INVALID_ARG, E_INVALID_ARG, "[E_INVALID_ARG] The specified input parameter is invalid.");
1446
1447                 std::unique_ptr< ByteBuffer > pbMacBuffer(pPkcs05PbKdf2Param->GetEncodedDataN());
1448                 if (pbMacBuffer == null)
1449                 {
1450                         r = GetLastResult();
1451                         if (r == E_OUT_OF_MEMORY)
1452                         {
1453                                 SysTryCatch(NID_SEC_CRYPTO, pbMacBuffer, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
1454
1455                         }
1456                         else
1457                         {
1458                                 SysTryCatch(NID_SEC_CRYPTO, pbMacBuffer, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] The method cannot proceed due to a severe system error.");
1459                         }
1460
1461                 }
1462
1463                 pBuf = pbMacBuffer->GetPointer();
1464                 SysTryCatch(NID_SEC_CRYPTO, pBuf != null, r = E_INVALID_ARG, E_INVALID_ARG, "[E_INVALID_ARG] The specified input parameter is invalid.");
1465
1466                 pMacObj = d2i_PBE2PARAM(null, reinterpret_cast< const unsigned char** >(&pBuf), pbMacBuffer->GetRemaining());
1467                 SysTryCatch(NID_SEC_CRYPTO, pMacObj != null, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] The method cannot proceed due to a severe system error.");
1468
1469                 ASN1_item_pack(pMacObj, ASN1_ITEM_rptr(PBE2PARAM), &pbE2Str);
1470
1471                 ret = X509_ALGOR_set0(pAlgoObj, OBJ_nid2obj(algoNid), V_ASN1_SEQUENCE, pbE2Str);
1472                 SysTryCatch(NID_SEC_CRYPTO, ret > 0, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] The method cannot proceed due to a severe system error.");
1473         }
1474         break;
1475
1476         default:
1477                 r = E_UNSUPPORTED_ALGORITHM;
1478                 break;
1479         }
1480
1481 CATCH:
1482
1483         if (IsFailed(r))
1484         {
1485                 X509_ALGOR_free(pAlgoObj);
1486                 if (pIv != null)
1487                 {
1488                         ASN1_STRING_free(pIv);
1489                 }
1490
1491                 if (pAsn1Type != null)
1492                 {
1493                         ASN1_TYPE_free(pAsn1Type);
1494                 }
1495                 if (pKdf != null)
1496                 {
1497                         PBKDF2PARAM_free(pKdf);
1498                 }
1499
1500                 if (pPbes2Obj != null)
1501                 {
1502                         PBE2PARAM_free(pPbes2Obj);
1503                 }
1504
1505                 if (pMacObj != null)
1506                 {
1507                         PBE2PARAM_free(pMacObj);
1508                 }
1509
1510                 return null;
1511         }
1512         return pAlgoObj;
1513
1514 }
1515
1516
1517
1518 } } } //OSP::SECURITY::PKCS