2 // Open Service Platform
3 // Copyright (c) 2013 Samsung Electronics Co., Ltd.
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
9 // http://www.apache.org/licenses/LICENSE-2.0
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.
19 * @file FSecPkcs_PkcsUtility.cpp
20 * @brief This is the implementation file for _PkcsUtility class.
22 * This header file contains the implementation of _PkcsUtility class.
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>
48 #include "FSecPkcs_PkcsUtility.h"
50 using namespace Tizen::Base;
51 using namespace Tizen::Security::Crypto;
53 namespace Tizen { namespace Security { namespace Pkcs
55 static const int _PKCS05_MAX_KEY_SIZE = 32;
58 _PkcsUtility::IsParameterSupported(Tizen::Base::String algorithm)
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)
74 _PkcsUtility::ConvertOidToEnum(Tizen::Base::String algorithm)
80 _OidType oidValue = _OID_TYPE_UNKNOWN;
82 if (algorithm == OID_PBKDF2)
84 oidValue = _OID_TYPE_PBKDF2;
86 else if (algorithm == OID_PBES2)
88 oidValue = _OID_TYPE_PBES2;
90 else if (algorithm == OID_PBMAC1)
92 oidValue = _OID_TYPE_PBMAC1;
94 else if (algorithm == OID_HMAC_SHA1)
96 oidValue = _OID_TYPE_HMAC_SHA1;
98 else if (algorithm == OID_HMAC_SHA2_224)
100 oidValue = _OID_TYPE_HMAC_SHA2_224;
102 else if (algorithm == OID_HMAC_SHA2_256)
104 oidValue = _OID_TYPE_HMAC_SHA2_256;
106 else if (algorithm == OID_HMAC_SHA2_384)
108 oidValue = _OID_TYPE_HMAC_SHA2_384;
110 else if (algorithm == OID_HMAC_SHA2_512)
112 oidValue = _OID_TYPE_HMAC_SHA2_512;
114 else if (algorithm == OID_DES_CBC)
116 oidValue = _OID_TYPE_DES_CBC;
118 else if (algorithm == OID_DES_CBC_EDE3)
120 oidValue = _OID_TYPE_DES_CBC_EDE3;
122 else if (algorithm == OID_AES_128_CBC)
124 oidValue = _OID_TYPE_AES_128_CBC;
126 else if (algorithm == OID_AES_192_CBC)
128 oidValue = _OID_TYPE_AES_192_CBC;
130 else if (algorithm == OID_AES_256_CBC)
132 oidValue = _OID_TYPE_AES_256_CBC;
134 else if (algorithm == OID_RC2_CBC)
136 oidValue = _OID_TYPE_RC2_CBC;
138 else if (algorithm == OID_RSA_ENCRYPTION)
140 oidValue = _OID_TYPE_RSA_ENCRYPTION;
144 r = E_UNSUPPORTED_ALGORITHM;
152 _PkcsUtility::ConvertToOid(int nidValue)
154 result r = E_SUCCESS;
155 Tizen::Base::String oidValue = null;
159 if (nidValue == NID_id_pbkdf2)
161 oidValue = OID_PBKDF2;
163 else if (nidValue == NID_pbes2)
165 oidValue = OID_PBES2;
167 else if (nidValue == NID_pbmac1)
169 oidValue = OID_PBMAC1;
171 else if (nidValue == NID_hmacWithSHA1)
173 oidValue = OID_HMAC_SHA1;
175 else if (nidValue == NID_hmacWithSHA224)
177 oidValue = OID_HMAC_SHA2_224;
179 else if (nidValue == NID_hmacWithSHA256)
181 oidValue = OID_HMAC_SHA2_256;
183 else if (nidValue == NID_hmacWithSHA384)
185 oidValue = OID_HMAC_SHA2_384;
187 else if (nidValue == NID_hmacWithSHA512)
189 oidValue = OID_HMAC_SHA2_512;
191 else if (nidValue == NID_des_cbc)
193 oidValue = OID_DES_CBC;
195 else if (nidValue == NID_des_ede3_cbc)
197 oidValue = OID_DES_CBC_EDE3;
199 else if (nidValue == NID_aes_128_cbc)
201 oidValue = OID_AES_128_CBC;
203 else if (nidValue == NID_aes_192_cbc)
205 oidValue = OID_AES_192_CBC;
207 else if (nidValue == NID_aes_256_cbc)
209 oidValue = OID_AES_256_CBC;
211 else if (nidValue == NID_rc2_cbc)
213 oidValue = OID_RC2_CBC;
215 else if (nidValue == NID_rsaEncryption)
217 oidValue = OID_RSA_ENCRYPTION;
219 else if (nidValue == NID_commonName)
221 oidValue = OID_ATTR_COMMON_NAME;
223 else if (nidValue == NID_countryName)
225 oidValue = OID_ATTR_COUNTRY_NAME;
227 else if (nidValue == NID_localityName)
229 oidValue = OID_ATTR_LOCALITY_NAME;
231 else if (nidValue == NID_stateOrProvinceName)
233 oidValue = OID_ATTR_STATE_OR_PROV_NAME;
235 else if (nidValue == NID_organizationName)
237 oidValue = OID_ATTR_ORG_NAME;
239 else if (nidValue == NID_organizationalUnitName)
241 oidValue = OID_ATTR_ORG_UNIT_NAME;
243 else if (nidValue == NID_givenName)
245 oidValue = OID_ATTR_GIVEN_NAME;
247 else if (nidValue == NID_surname)
249 oidValue = OID_ATTR_SURNAME;
251 else if (nidValue == NID_initials)
253 oidValue = OID_ATTR_INITIAL;
255 else if (nidValue == NID_serialNumber)
257 oidValue = OID_ATTR_SERIAL_NUMBER;
259 else if (nidValue == NID_title)
261 oidValue = OID_ATTR_TITLE;
263 else if (nidValue == NID_pkcs9_emailAddress)
265 oidValue = OID_ATTR_EMAIL_ADDRESS;
267 else if (nidValue == NID_generationQualifier)
269 oidValue = OID_ATTR_GEN_QUALIFIER;
271 else if (nidValue == NID_pseudonym)
273 oidValue = OID_ATTR_PSEUDONYM;
275 else if (nidValue == NID_domainComponent)
277 oidValue = OID_ATTR_DOMAIN_COMPONENT;
279 else if (nidValue == NID_dnQualifier)
281 oidValue = OID_ATTR_DN_QUALIFIER;
283 else if (nidValue == NID_name)
285 oidValue = OID_ATTR_NAME;
289 r = E_UNSUPPORTED_ALGORITHM;
297 _PkcsUtility::ConvertToTagValue(int ans1Type)
299 Pkcs08TagValue tagValue;
303 case V_ASN1_PRINTABLESTRING:
304 tagValue = PKCS08_TAG_PRINTABLE_STRING;
308 tagValue = PKCS08_TAG_RESERVED;
312 tagValue = PKCS08_TAG_BOOLEAN;
316 tagValue = PKCS08_TAG_INTEGER;
319 case V_ASN1_BIT_STRING:
320 tagValue = PKCS08_TAG_BITSTRING;
323 case V_ASN1_OCTET_STRING:
324 tagValue = PKCS08_TAG_OCTETSTRING;
328 tagValue = PKCS08_TAG_NULL;
332 tagValue = PKCS08_TAG_OBJECT_ID;
335 case V_ASN1_OBJECT_DESCRIPTOR:
336 tagValue = PKCS08_TAG_OBJECT_DES;
339 case V_ASN1_EXTERNAL:
340 tagValue = PKCS08_TAG_EXTERNAL;
344 tagValue = PKCS08_TAG_REAL;
347 case V_ASN1_ENUMERATED:
348 tagValue = PKCS08_TAG_ENUM;
352 tagValue = PKCS08_TAG_EMBEDDED;
355 case V_ASN1_UTF8STRING:
356 tagValue = PKCS08_TAG_UTF8STRING;
360 tagValue = PKCS08_TAG_REL_OBJ_ID;
363 case V_ASN1_SEQUENCE:
364 tagValue = PKCS08_TAG_SEQUENCE;
368 tagValue = PKCS08_TAG_SET;
371 case V_ASN1_NUMERICSTRING:
372 tagValue = PKCS08_TAG_CHAR_STRING;
375 case V_ASN1_TELETEXSTRING:
376 tagValue = PKCS08_TAG_TELETEXT_STRING;
379 case V_ASN1_VIDEOTEXSTRING:
380 tagValue = PKCS08_TAG_VIDEOTEXT_STRING;
383 case V_ASN1_IA5STRING:
384 tagValue = PKCS08_TAG_IA5STRING;
388 tagValue = PKCS08_TAG_UTC_TIME;
391 case V_ASN1_GENERALIZEDTIME:
392 tagValue = PKCS08_TAG_GEN_TIME;
395 case V_ASN1_GRAPHICSTRING:
396 tagValue = PKCS08_TAG_GRAPHICS_STRING;
399 case V_ASN1_VISIBLESTRING:
400 tagValue = PKCS08_TAG_VISIBLE_STRING;
403 case V_ASN1_GENERALSTRING:
404 tagValue = PKCS08_TAG_GENERAL_STRING;
407 case V_ASN1_UNIVERSALSTRING:
408 tagValue = PKCS08_TAG_UNIVERSAL_STRING;
412 tagValue = PKCS08_TAG_CHARACTER_STRING;
415 case V_ASN1_BMPSTRING:
416 tagValue = PKCS08_TAG_BMP_STRING;
420 tagValue = PKCS08_TAG_UNKNOWN;
421 SetLastResult(E_INVALID_ARG);
426 SetLastResult(E_SUCCESS);
431 _PkcsUtility::ConvertToNid(Tizen::Base::String algorithm)
433 result r = E_SUCCESS;
438 if (algorithm == OID_PBKDF2)
442 else if (algorithm == OID_PBES2)
446 else if (algorithm == OID_PBMAC1)
450 else if (algorithm == OID_HMAC_SHA1)
452 nid = NID_hmacWithSHA1;
454 else if (algorithm == OID_HMAC_SHA2_224)
456 nid = NID_hmacWithSHA224;
458 else if (algorithm == OID_HMAC_SHA2_256)
460 nid = NID_hmacWithSHA256;
462 else if (algorithm == OID_HMAC_SHA2_384)
464 nid = NID_hmacWithSHA384;
466 else if (algorithm == OID_HMAC_SHA2_512)
468 nid = NID_hmacWithSHA512;
470 else if (algorithm == OID_DES_CBC)
474 else if (algorithm == OID_DES_CBC_EDE3)
476 nid = NID_des_ede3_cbc;
478 else if (algorithm == OID_AES_128_CBC)
480 nid = NID_aes_128_cbc;
482 else if (algorithm == OID_AES_192_CBC)
484 nid = NID_aes_192_cbc;
486 else if (algorithm == OID_AES_256_CBC)
488 nid = NID_aes_256_cbc;
490 else if (algorithm == OID_RC2_CBC)
494 else if (algorithm == OID_RSA_ENCRYPTION)
496 nid = NID_rsaEncryption;
498 else if (algorithm == OID_ATTR_COUNTRY_NAME)
500 nid = NID_countryName;
502 else if (algorithm == OID_ATTR_NAME)
506 else if (algorithm == OID_ATTR_COMMON_NAME)
508 nid = NID_commonName;
510 else if (algorithm == OID_ATTR_LOCALITY_NAME)
512 nid = NID_localityName;
514 else if (algorithm == OID_ATTR_STATE_OR_PROV_NAME)
516 nid = NID_stateOrProvinceName;
518 else if (algorithm == OID_ATTR_ORG_NAME)
520 nid = NID_organizationName;
522 else if (algorithm == OID_ATTR_ORG_UNIT_NAME)
524 nid = NID_organizationalUnitName;
526 else if (algorithm == OID_ATTR_GIVEN_NAME)
530 else if (algorithm == OID_ATTR_SURNAME)
534 else if (algorithm == OID_ATTR_INITIAL)
538 else if (algorithm == OID_ATTR_SERIAL_NUMBER)
540 nid = NID_serialNumber;
542 else if (algorithm == OID_ATTR_TITLE)
546 else if (algorithm == OID_ATTR_EMAIL_ADDRESS)
548 nid = NID_pkcs9_emailAddress;
550 else if (algorithm == OID_ATTR_GEN_QUALIFIER)
552 nid = NID_generationQualifier;
554 else if (algorithm == OID_ATTR_PSEUDONYM)
558 else if (algorithm == OID_ATTR_DOMAIN_COMPONENT)
560 nid = NID_domainComponent;
562 else if (algorithm == OID_ATTR_DN_QUALIFIER)
564 nid = NID_dnQualifier;
568 r = E_UNSUPPORTED_ALGORITHM;
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)
578 result r = E_SUCCESS;
579 EVP_CIPHER_CTX cipherCtx;
580 std::unique_ptr< byte[] > pOut;
587 std::unique_ptr< ByteBuffer > pOutBuffer;
589 Tizen::Base::String encOid = null;
590 const evp_cipher_st* pCipherAlgorithm = null;
591 _OidType oidValue = _OID_TYPE_UNKNOWN;
595 EVP_CIPHER_CTX_init(&cipherCtx);
596 encOid = algo.GetAlgorithmObjectId();
597 oidValue = _PkcsUtility::ConvertOidToEnum(encOid);
601 case _OID_TYPE_DES_CBC:
603 std::unique_ptr< InitialVector > pIvObj(dynamic_cast< InitialVector* >(algo.GetParametersN()));
607 if (r == E_OUT_OF_MEMORY)
609 SysTryCatch(NID_SEC_CRYPTO, pIvObj, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
614 SysTryCatch(NID_SEC_CRYPTO, pIvObj, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] The method cannot proceed due to a severe system error.");
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.");
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.");
628 case _OID_TYPE_DES_CBC_EDE3:
630 std::unique_ptr< InitialVector > pIvObj(dynamic_cast< InitialVector* >(algo.GetParametersN()));
634 if (r == E_OUT_OF_MEMORY)
636 SysTryCatch(NID_SEC_CRYPTO, pIvObj, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
641 SysTryCatch(NID_SEC_CRYPTO, pIvObj, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] The method cannot proceed due to a severe system error.");
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.");
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.");
656 case _OID_TYPE_AES_128_CBC:
658 std::unique_ptr< InitialVector > pIvObj(dynamic_cast< InitialVector* >(algo.GetParametersN()));
662 if (r == E_OUT_OF_MEMORY)
664 SysTryCatch(NID_SEC_CRYPTO, pIvObj, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
669 SysTryCatch(NID_SEC_CRYPTO, pIvObj, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] The method cannot proceed due to a severe system error.");
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.");
678 pCipherAlgorithm = EVP_aes_128_cbc();
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.");
685 case _OID_TYPE_AES_192_CBC:
687 std::unique_ptr< InitialVector > pIvObj(dynamic_cast< InitialVector* >(algo.GetParametersN()));
691 if (r == E_OUT_OF_MEMORY)
693 SysTryCatch(NID_SEC_CRYPTO, pIvObj, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
698 SysTryCatch(NID_SEC_CRYPTO, pIvObj, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] The method cannot proceed due to a severe system error.");
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.");
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.");
713 case _OID_TYPE_AES_256_CBC:
715 std::unique_ptr< InitialVector > pIvObj(dynamic_cast< InitialVector* >(algo.GetParametersN()));
719 if (r == E_OUT_OF_MEMORY)
721 SysTryCatch(NID_SEC_CRYPTO, pIvObj, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
726 SysTryCatch(NID_SEC_CRYPTO, pIvObj, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] The method cannot proceed due to a severe system error.");
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.");
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.");
742 case _OID_TYPE_RC2_CBC:
744 std::unique_ptr< Rc2CbcParameters > pRcObj(dynamic_cast< Rc2CbcParameters* >(algo.GetParametersN()));
748 if (r == E_OUT_OF_MEMORY)
750 SysTryCatch(NID_SEC_CRYPTO, pRcObj, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
755 SysTryCatch(NID_SEC_CRYPTO, pRcObj, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] The method cannot proceed due to a severe system error.");
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.");
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.");
768 EVP_CIPHER_CTX_set_key_length(&cipherCtx, derivedKey.GetRemaining());
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.");
777 case _OID_TYPE_PBES2:
779 Pkcs05Schemes pkcs05Scheme;
780 std::unique_ptr< Pkcs05PbEs2Parameters > pPbEs2(dynamic_cast< Pkcs05PbEs2Parameters* >(algo.GetParametersN()));
784 if (r == E_OUT_OF_MEMORY)
786 SysTryCatch(NID_SEC_CRYPTO, pPbEs2, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
791 SysTryCatch(NID_SEC_CRYPTO, pPbEs2, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] The method cannot proceed due to a severe system error.");
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.");
798 std::unique_ptr< Pkcs05PbKdf2Parameters > pKdfParams(dynamic_cast< Pkcs05PbKdf2Parameters* >(pPbEs2->GetKeyDerivationAlgorithm().GetParametersN()));
799 if (pKdfParams == null)
802 if (r == E_OUT_OF_MEMORY)
804 SysTryCatch(NID_SEC_CRYPTO, pKdfParams, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
809 SysTryCatch(NID_SEC_CRYPTO, pKdfParams, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] The method cannot proceed due to a severe system error.");
814 int derivedKeyLength = pKdfParams->GetDerivedKeyLength();
816 if (derivedKeyLength <= 0)
818 derivedKeyLength = _PKCS05_MAX_KEY_SIZE;
821 r = pkcs05Scheme.Construct(derivedKey, derivedKeyLength);
825 pOutBuffer = std::unique_ptr< ByteBuffer >(pkcs05Scheme.EncryptionScheme2N(*pPbEs2, input));
829 pOutBuffer = std::unique_ptr< ByteBuffer >(pkcs05Scheme.DecryptionScheme2N(*pPbEs2, input));
833 return pOutBuffer.release();
835 case _OID_TYPE_RSA_ENCRYPTION:
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.");
845 rsaCipher.SetPublicKey(pubKey);
846 pOutBuffer = std::unique_ptr< ByteBuffer >(rsaCipher.DecryptN(input));
852 priKey.SetKey(derivedKey);
853 r = rsaCipher.SetPrivateKey(priKey);
854 pOutBuffer = std::unique_ptr< ByteBuffer >(rsaCipher.EncryptN(input));
858 return pOutBuffer.release();
861 r = E_UNSUPPORTED_ALGORITHM;
868 // initialize the cipher context
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.");
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.");
877 tempLen = static_cast< int >((dataLen / pCipherAlgorithm->block_size + 1)* pCipherAlgorithm->block_size);
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.");
882 memset(pOut.get(), 0, tempLen);
884 if (dataLen % pCipherAlgorithm->block_size != 0)
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.");
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.");
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;
900 SysTryCatch(NID_SEC_CRYPTO, outLen <= tempLen, r = E_OVERFLOW, E_OVERFLOW, "[E_OVERFLOW] Length mismatch resulting in overflow.");
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.");
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.");
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.");
918 pOutBuffer.reset(null);
920 EVP_CIPHER_CTX_cleanup(&cipherCtx);
923 return pOutBuffer.release();
928 _PkcsUtility::GenerateKdfParametersN(int iter, unsigned char* pSaltValue, int saltLen, int prfNid, int keyLen)
930 result r = E_SUCCESS;
931 X509_ALGOR* pKeyfunc = null;
932 PBKDF2PARAM* pKdf = null;
933 ASN1_OCTET_STRING* pSalt = null;
939 pKdf = PBKDF2PARAM_new();
940 SysTryReturn(NID_SEC_CRYPTO, pKdf != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
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.");
945 pKdf->salt->value.octet_string = pSalt;
946 pKdf->salt->type = V_ASN1_OCTET_STRING;
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.");
951 pSalt->length = saltLen;
953 memcpy(pSalt->data, pSaltValue, saltLen);
955 ASN1_INTEGER_set(pKdf->iter, iter);
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.");
963 ASN1_INTEGER_set(pKdf->keylength, keyLen);
967 if (prfNid != NID_hmacWithSHA1)
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.");
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.");
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.");
981 pKeyfunc->algorithm = OBJ_nid2obj(NID_id_pbkdf2);
983 pKeyfunc->parameter = ASN1_TYPE_new();
984 ASN1_item_pack(pKdf, ASN1_ITEM_rptr(PBKDF2PARAM), &pKeyfunc->parameter->value.sequence);
986 pKeyfunc->parameter->type = V_ASN1_SEQUENCE;
988 PBKDF2PARAM_free(pKdf);
993 PBKDF2PARAM_free(pKdf);
994 X509_ALGOR_free(pKeyfunc);
1000 IAlgorithmParameters*
1001 _PkcsUtility::GernerateParametersFromOidN(Tizen::Base::String algoOid, X509_ALGOR* pAlgoObj)
1003 result r = E_SUCCESS;
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;
1027 oidValue = _PkcsUtility::ConvertOidToEnum(algoOid);
1031 case _OID_TYPE_DES_CBC:
1033 case _OID_TYPE_DES_CBC_EDE3:
1035 case _OID_TYPE_AES_128_CBC:
1037 case _OID_TYPE_AES_192_CBC:
1039 case _OID_TYPE_AES_256_CBC:
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.");
1044 SysTryReturn(NID_SEC_CRYPTO, pVal != null, null, E_INVALID_ARG, "[E_INVALID_ARG] The specified input parameter is invalid.");
1046 pStr = reinterpret_cast< ASN1_STRING* >(pVal);
1048 dataLen = pStr->length;
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.");
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.");
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.");
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.");
1064 return pAlgoParams.release();
1068 case _OID_TYPE_RC2_CBC:
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.");
1073 SysTryReturn(NID_SEC_CRYPTO, pVal != null, null, E_INVALID_ARG, "[E_INVALID_ARG] The specified input parameter is invalid.");
1075 pStr = reinterpret_cast< ASN1_STRING* >(pVal);
1077 dataLen = pStr->length;
1080 if (pType == V_ASN1_OCTET_STRING)
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.");
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.");
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.");
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.");
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.");
1105 const byte* pBuffer = ivBuffer.GetPointer();
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.");
1110 memset(iv, 0, sizeof(iv));
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.");
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.");
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.");
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.");
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.");
1131 return pAlgoParams.release();
1136 case _OID_TYPE_HMAC_SHA1:
1138 case _OID_TYPE_HMAC_SHA2_224:
1140 case _OID_TYPE_HMAC_SHA2_256:
1142 case _OID_TYPE_HMAC_SHA2_384:
1144 case _OID_TYPE_HMAC_SHA2_512:
1146 case _OID_TYPE_RSA_ENCRYPTION:
1149 return pAlgoParams.release();
1153 case _OID_TYPE_PBKDF2:
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.");
1158 SysTryReturn(NID_SEC_CRYPTO, pVal != null, null, E_INVALID_ARG, "[E_INVALID_ARG] The specified input parameter is invalid.");
1160 pStr = reinterpret_cast< ASN1_STRING* >(pVal);
1162 dataLen = pStr->length;
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.");
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.");
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.");
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.");
1178 return pAlgoParams.release();
1182 case _OID_TYPE_PBES2:
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.");
1187 SysTryReturn(NID_SEC_CRYPTO, pVal != null, null, E_INVALID_ARG, "[E_INVALID_ARG] The specified input parameter is invalid.");
1189 pStr = reinterpret_cast< ASN1_STRING* >(pVal);
1191 dataLen = pStr->length;
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.");
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.");
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.");
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.");
1207 return pAlgoParams.release();
1211 case _OID_TYPE_PBMAC1:
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.");
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);
1219 dataLen = pStr->length;
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.");
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.");
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.");
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.");
1235 return pAlgoParams.release();
1241 r = E_UNSUPPORTED_ALGORITHM;
1246 return pAlgoParams.release();
1251 _PkcsUtility::GenerateAlgorithmIdentifierStructureN(Tizen::Base::String algoOid, IAlgorithmParameters* pAlgoParam)
1253 result r = E_SUCCESS;
1254 X509_ALGOR* pAlgoObj = null;
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;
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.");
1274 oidValue = _PkcsUtility::ConvertOidToEnum(algoOid);
1275 algoNid = _PkcsUtility::ConvertToNid(algoOid);
1279 case _OID_TYPE_DES_CBC:
1281 case _OID_TYPE_DES_CBC_EDE3:
1283 case _OID_TYPE_AES_128_CBC:
1285 case _OID_TYPE_AES_192_CBC:
1287 case _OID_TYPE_AES_256_CBC:
1289 SysTryReturn(NID_SEC_CRYPTO, pAlgoParam != null, null, E_INVALID_ARG, "[E_INVALID_ARG] The specified input parameter is invalid.");
1291 ivBuffer.Construct(((dynamic_cast< InitialVector* >(pAlgoParam))->GetInitialVector()));
1292 SysTryReturn(NID_SEC_CRYPTO, ivBuffer.GetRemaining() > 0, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
1294 pIv = ASN1_STRING_new();
1295 SysTryReturn(NID_SEC_CRYPTO, pIv != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
1297 pIv->data = static_cast< unsigned char* >(OPENSSL_malloc(ivBuffer.GetRemaining()));
1298 SysTryReturn(NID_SEC_CRYPTO, pIv->data != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
1300 memcpy(pIv->data, ivBuffer.GetPointer(), ivBuffer.GetRemaining());
1302 pIv->length = ivBuffer.GetRemaining();
1304 ret = X509_ALGOR_set0(pAlgoObj, OBJ_nid2obj(algoNid), V_ASN1_OCTET_STRING, reinterpret_cast< void* >(pIv));
1305 SysTryReturn(NID_SEC_CRYPTO, ret > 0, null, E_SYSTEM, "[E_SYSTEM] The method cannot proceed due to a severe system error.");
1309 case _OID_TYPE_RC2_CBC:
1311 SysTryReturn(NID_SEC_CRYPTO, pAlgoParam != null, null, E_INVALID_ARG, "[E_INVALID_ARG] The specified input parameter is invalid.");
1313 ivBuffer.Construct(((dynamic_cast< Rc2CbcParameters* >(pAlgoParam))->GetInitialVector()));
1314 SysTryReturn(NID_SEC_CRYPTO, ivBuffer.GetRemaining() > 0, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
1316 version = (dynamic_cast< Rc2CbcParameters* >(pAlgoParam))->GetVersion();
1320 pIv = ASN1_STRING_new();
1321 SysTryReturn(NID_SEC_CRYPTO, pIv != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
1323 pIv->data = static_cast< unsigned char* >(OPENSSL_malloc(ivBuffer.GetRemaining()));
1324 SysTryReturn(NID_SEC_CRYPTO, pIv->data != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
1326 memcpy(pIv->data, ivBuffer.GetPointer(), ivBuffer.GetRemaining());
1328 pIv->length = ivBuffer.GetRemaining();
1330 ret = X509_ALGOR_set0(pAlgoObj, OBJ_nid2obj(algoNid), V_ASN1_OCTET_STRING, reinterpret_cast< void* >(pIv));
1331 SysTryReturn(NID_SEC_CRYPTO, ret > 0, null, E_SYSTEM, "[E_SYSTEM] The method cannot proceed due to a severe system error.");
1337 pAsn1Type = ASN1_TYPE_new();
1338 SysTryReturn(NID_SEC_CRYPTO, pAsn1Type != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
1340 ret = ASN1_TYPE_set_int_octetstring(pAsn1Type, version, const_cast< unsigned char* >(ivBuffer.GetPointer()), ivBuffer.GetRemaining());
1341 SysTryReturn(NID_SEC_CRYPTO, ret > 0, null, E_SYSTEM, "[E_SYSTEM] The method cannot proceed due to a severe system error.");
1343 ret = X509_ALGOR_set0(pAlgoObj, OBJ_nid2obj(algoNid), V_ASN1_SEQUENCE, reinterpret_cast< void* >(pAsn1Type->value.ptr));
1344 SysTryReturn(NID_SEC_CRYPTO, ret > 0, null, E_SYSTEM, "[E_SYSTEM] The method cannot proceed due to a severe system error.");
1351 case _OID_TYPE_HMAC_SHA1:
1353 case _OID_TYPE_HMAC_SHA2_224:
1355 case _OID_TYPE_HMAC_SHA2_256:
1357 case _OID_TYPE_HMAC_SHA2_384:
1359 case _OID_TYPE_RSA_ENCRYPTION:
1361 case _OID_TYPE_HMAC_SHA2_512:
1363 ret = X509_ALGOR_set0(pAlgoObj, OBJ_nid2obj(algoNid), V_ASN1_NULL, NULL);
1364 SysTryReturn(NID_SEC_CRYPTO, ret > 0, null, E_SYSTEM, "[E_SYSTEM] The method cannot proceed due to a severe system error.");
1368 case _OID_TYPE_PBKDF2:
1370 SysTryReturn(NID_SEC_CRYPTO, pAlgoParam != null, null, E_INVALID_ARG, "[E_INVALID_ARG] The specified input parameter is invalid.");
1372 std::unique_ptr< ByteBuffer > pKdfBuffer((dynamic_cast< Pkcs05PbKdf2Parameters* >(pAlgoParam))->GetEncodedDataN());
1373 if (pKdfBuffer == null)
1375 r = GetLastResult();
1376 if (r == E_OUT_OF_MEMORY)
1378 SysTryReturn(NID_SEC_CRYPTO, pKdfBuffer, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
1383 SysTryReturn(NID_SEC_CRYPTO, pKdfBuffer, null, E_SYSTEM, "[E_SYSTEM] The method cannot proceed due to a severe system error.");
1388 pBuf = pKdfBuffer->GetPointer();
1389 SysTryReturn(NID_SEC_CRYPTO, pBuf != null, null, E_INVALID_ARG, "[E_INVALID_ARG] The specified input parameter is invalid.");
1391 pKdf = d2i_PBKDF2PARAM(null, reinterpret_cast< const unsigned char** >(&pBuf), pKdfBuffer->GetRemaining());
1392 SysTryReturn(NID_SEC_CRYPTO, pKdf != null, null, E_SYSTEM, "[E_SYSTEM] The method cannot proceed due to a severe system error.");
1394 ASN1_item_pack(pKdf, ASN1_ITEM_rptr(PBKDF2PARAM), &pbKdf2Str);
1396 SysTryReturn(NID_SEC_CRYPTO, pbKdf2Str != null, null, E_SYSTEM, "[E_SYSTEM] The method cannot proceed due to a severe system error.");
1398 ret = X509_ALGOR_set0(pAlgoObj, OBJ_nid2obj(algoNid), V_ASN1_SEQUENCE, pbKdf2Str);
1399 SysTryReturn(NID_SEC_CRYPTO, ret > 0, null, E_SYSTEM, "[E_SYSTEM] The method cannot proceed due to a severe system error.");
1403 case _OID_TYPE_PBES2:
1406 SysTryReturn(NID_SEC_CRYPTO, pAlgoParam != null, null, E_INVALID_ARG, "[E_INVALID_ARG] The specified input parameter is invalid.");
1408 std::unique_ptr< ByteBuffer > pbEs2Buffer((dynamic_cast< Pkcs05PbEs2Parameters* >(pAlgoParam))->GetEncodedDataN());
1409 if (pbEs2Buffer == null)
1411 r = GetLastResult();
1412 if (r == E_OUT_OF_MEMORY)
1414 SysTryReturn(NID_SEC_CRYPTO, pbEs2Buffer, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
1419 SysTryReturn(NID_SEC_CRYPTO, pbEs2Buffer, null, E_SYSTEM, "[E_SYSTEM] The method cannot proceed due to a severe system error.");
1424 pBuf = pbEs2Buffer->GetPointer();
1425 SysTryReturn(NID_SEC_CRYPTO, pBuf != null, null, E_INVALID_ARG, "[E_INVALID_ARG] The specified input parameter is invalid.");
1427 pPbes2Obj = d2i_PBE2PARAM(null, reinterpret_cast< const unsigned char** >(&pBuf), pbEs2Buffer->GetRemaining());
1428 SysTryReturn(NID_SEC_CRYPTO, pPbes2Obj != null, null, E_SYSTEM, "[E_SYSTEM] The method cannot proceed due to a severe system error.");
1430 ASN1_item_pack(pPbes2Obj, ASN1_ITEM_rptr(PBE2PARAM), &pbE2Str);
1432 ret = X509_ALGOR_set0(pAlgoObj, OBJ_nid2obj(algoNid), V_ASN1_SEQUENCE, pbE2Str);
1433 SysTryReturn(NID_SEC_CRYPTO, pbE2Str != null, null, E_SYSTEM, "[E_SYSTEM] The method cannot proceed due to a severe system error.");
1435 SysTryReturn(NID_SEC_CRYPTO, ret > 0, null, E_SYSTEM, "[E_SYSTEM] The method cannot proceed due to a severe system error.");
1439 case _OID_TYPE_PBMAC1:
1441 SysTryReturn(NID_SEC_CRYPTO, pAlgoParam != null, null, E_INVALID_ARG, "[E_INVALID_ARG] The specified input parameter is invalid.");
1443 std::unique_ptr< ByteBuffer > pbMacBuffer((dynamic_cast< Pkcs05PbMacParameters* >(pAlgoParam))->GetEncodedDataN());
1444 if (pbMacBuffer == null)
1446 r = GetLastResult();
1447 if (r == E_OUT_OF_MEMORY)
1449 SysTryReturn(NID_SEC_CRYPTO, pbMacBuffer, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
1454 SysTryReturn(NID_SEC_CRYPTO, pbMacBuffer, null, E_SYSTEM, "[E_SYSTEM] The method cannot proceed due to a severe system error.");
1459 pBuf = pbMacBuffer->GetPointer();
1460 SysTryReturn(NID_SEC_CRYPTO, pBuf != null, null, E_INVALID_ARG, "[E_INVALID_ARG] The specified input parameter is invalid.");
1462 pMacObj = d2i_PBE2PARAM(null, reinterpret_cast< const unsigned char** >(&pBuf), pbMacBuffer->GetRemaining());
1463 SysTryReturn(NID_SEC_CRYPTO, pMacObj != null, null, E_SYSTEM, "[E_SYSTEM] The method cannot proceed due to a severe system error.");
1465 ASN1_item_pack(pMacObj, ASN1_ITEM_rptr(PBE2PARAM), &pbE2Str);
1467 ret = X509_ALGOR_set0(pAlgoObj, OBJ_nid2obj(algoNid), V_ASN1_SEQUENCE, pbE2Str);
1468 SysTryReturn(NID_SEC_CRYPTO, ret > 0, null, E_SYSTEM, "[E_SYSTEM] The method cannot proceed due to a severe system error.");
1473 r = E_UNSUPPORTED_ALGORITHM;
1483 } } } //OSP::SECURITY::PKCS