@@ -6734,23 +6734,21 @@ CK_RV SoftHSM::C_WrapKey
67346734 CK_RSA_AES_KEY_WRAP_PARAMS_PTR params = (CK_RSA_AES_KEY_WRAP_PARAMS_PTR)pMechanism->pParameter ;
67356735 CK_ULONG emphKeyLen = params->aes_key_bits / 8 ;
67366736 CK_OBJECT_HANDLE hEmphKey = CK_INVALID_HANDLE;
6737- CK_OBJECT_CLASS secretClass = CKO_SECRET_KEY;
6738- CK_KEY_TYPE keyType = CKK_AES;
6739- CK_BBOOL isEmphOnToken = CK_FALSE;
6740- CK_BBOOL isEmphPrivate = CK_TRUE;
6737+ CK_OBJECT_CLASS emphKeyClass = CKO_SECRET_KEY;
6738+ CK_KEY_TYPE emphKeyType = CKK_AES;
67416739 CK_BBOOL bFalse = CK_FALSE;
67426740 CK_BBOOL bTrue = CK_TRUE;
67436741 CK_ATTRIBUTE emph_temp[] = {
6744- {CKA_CLASS, &secretClass , sizeof (CK_OBJECT_CLASS)},
6745- {CKA_KEY_TYPE, &keyType , sizeof (CK_KEY_TYPE)},
6742+ {CKA_CLASS, &emphKeyClass , sizeof (CK_OBJECT_CLASS)},
6743+ {CKA_KEY_TYPE, &emphKeyType , sizeof (CK_KEY_TYPE)},
67466744 {CKA_TOKEN, &bFalse, sizeof (CK_BBOOL)},
6747- {CKA_PRIVATE, &isEmphPrivate , sizeof (CK_BBOOL)},
6745+ {CKA_PRIVATE, &bTrue , sizeof (CK_BBOOL)},
67486746 {CKA_WRAP, &bTrue, sizeof (CK_BBOOL)},
67496747 {CKA_EXTRACTABLE, &bTrue, sizeof (CK_BBOOL)},
67506748 {CKA_VALUE_LEN, &emphKeyLen, sizeof (CK_ULONG)}
67516749 };
67526750 // Generates temporary random AES key of ulAESKeyBits length.
6753- rv = this ->generateAES (hSession, emph_temp, sizeof (emph_temp)/sizeof (CK_ATTRIBUTE), &hEmphKey, isEmphOnToken, isEmphPrivate );
6751+ rv = this ->generateAES (hSession, emph_temp, sizeof (emph_temp)/sizeof (CK_ATTRIBUTE), &hEmphKey, bFalse, bTrue );
67546752 if (rv != CKR_OK)
67556753 {
67566754 // Remove secret that may have been created already when the function fails.
@@ -6767,35 +6765,40 @@ CK_RV SoftHSM::C_WrapKey
67676765 OSObject *emphKey = (OSObject *)handleManager->getObject (hEmphKey);
67686766 if (emphKey == NULL_PTR || !emphKey->isValid ())
67696767 {
6770- return CKR_KEY_HANDLE_INVALID;
6768+ handleManager->destroyObject (hEmphKey);
6769+ if (emphKey) emphKey->destroyObject ();
6770+ hEmphKey = CK_INVALID_HANDLE;
6771+ return CKR_FUNCTION_FAILED;
67716772 }
67726773
6773- // Remove the emph key handle.
6774- handleManager->destroyObject (hEmphKey);
6775- hEmphKey = CK_INVALID_HANDLE;
6776-
67776774 CK_MECHANISM emphMech = {CKM_AES_KEY_WRAP_PAD, NULL_PTR, 0 };
67786775
67796776 // Wraps the target key with the temporary AES key using CKM_AES_KEY_WRAP_PAD (RFC5649).
67806777 rv = SoftHSM::WrapKeySym (&emphMech, token, emphKey, keydata, wrapped_2);
67816778 if (rv != CKR_OK)
67826779 {
6780+ handleManager->destroyObject (hEmphKey);
67836781 emphKey->destroyObject ();
6782+ hEmphKey = CK_INVALID_HANDLE;
67846783 return rv;
67856784 }
67866785
67876786 // Get the AES emph key data
67886787 ByteString emphkeydata;
67896788 ByteString emphKeyValue = emphKey->getByteStringValue (CKA_VALUE);
67906789 token->decrypt (emphKeyValue, emphkeydata);
6790+
6791+ // Remove the emph key handle.
6792+ handleManager->destroyObject (hEmphKey);
67916793 emphKey->destroyObject ();
6794+ hEmphKey = CK_INVALID_HANDLE;
67926795
67936796 CK_MECHANISM oaepMech = {CKM_RSA_PKCS_OAEP, params->oaep_params , sizeof (CK_RSA_AES_KEY_WRAP_PARAMS)};
67946797
67956798 // Wraps the AES emph key with the wrapping RSA key using CKM_RSA_PKCS_OAEP with parameters of OAEPParams.
67966799 rv = SoftHSM::WrapKeyAsym (&oaepMech, token, wrapKey, emphkeydata, wrapped_1);
67976800
6798- // Zeroizes the temporary AES key
6801+ // Zeroizes the temporary AES emph key
67996802 emphkeydata.wipe ();
68006803 emphKeyValue.wipe ();
68016804
@@ -7259,17 +7262,15 @@ CK_RV SoftHSM::C_UnwrapKey
72597262 return rv;
72607263 }
72617264
7262- ByteString wrapped_2 (pWrappedKey + wrappedLen1, wrappedLen2); // the wrapped target key
7263- CK_BBOOL isEmphPrivate = CK_TRUE;
7264- CK_OBJECT_CLASS secretClass = CKO_SECRET_KEY;
7265- CK_KEY_TYPE keyType = CKK_AES;
7265+ CK_OBJECT_CLASS emphKeyClass = CKO_SECRET_KEY;
7266+ CK_KEY_TYPE emphKeyType = CKK_AES;
72667267 CK_BBOOL bFalse = CK_FALSE;
72677268 CK_BBOOL bTrue = CK_TRUE;
72687269 CK_ATTRIBUTE emph_temp[] = {
7269- {CKA_CLASS, &secretClass , sizeof (CK_OBJECT_CLASS)},
7270- {CKA_KEY_TYPE, &keyType , sizeof (CK_KEY_TYPE)},
7270+ {CKA_CLASS, &emphKeyClass , sizeof (CK_OBJECT_CLASS)},
7271+ {CKA_KEY_TYPE, &emphKeyType , sizeof (CK_KEY_TYPE)},
72717272 {CKA_TOKEN, &bFalse, sizeof (CK_BBOOL)},
7272- {CKA_PRIVATE, &isEmphPrivate , sizeof (CK_BBOOL)},
7273+ {CKA_PRIVATE, &bTrue , sizeof (CK_BBOOL)},
72737274 {CKA_UNWRAP, &bTrue, sizeof (CK_BBOOL)}
72747275 };
72757276 // Create the temporary AES object using C_CreateObject
@@ -7288,16 +7289,16 @@ CK_RV SoftHSM::C_UnwrapKey
72887289 return rv;
72897290 }
72907291
7291- // Store the attributes that are being supplied
7292+ // Store the attributes of emphkey
72927293 OSObject *emphKey = (OSObject *)handleManager->getObject (hEmphKey);
72937294 if (emphKey == NULL_PTR || !emphKey->isValid ())
72947295 {
7296+ handleManager->destroyObject (hEmphKey);
7297+ if (emphKey) emphKey->destroyObject ();
7298+ hEmphKey = CK_INVALID_HANDLE;
72957299 emphkeydata.wipe ();
72967300 return CKR_FUNCTION_FAILED;
72977301 }
7298- // remove the emphkey handle
7299- handleManager->destroyObject (hEmphKey);
7300- hEmphKey = CK_INVALID_HANDLE;
73017302
73027303 if (emphKey->startTransaction ())
73037304 {
@@ -7320,27 +7321,38 @@ CK_RV SoftHSM::C_UnwrapKey
73207321 {
73217322 emphKey->abortTransaction ();
73227323 }
7324+
73237325 // Zeroizes the temporary AES key.
73247326 emphkeydata.wipe ();
73257327 emphKeyValue.wipe ();
73267328
73277329 if (!bOK)
73287330 {
7331+ handleManager->destroyObject (hEmphKey);
73297332 emphKey->destroyObject ();
7333+ hEmphKey = CK_INVALID_HANDLE;
73307334 return CKR_FUNCTION_FAILED;
73317335 }
73327336 }
73337337 else
73347338 {
73357339 emphkeydata.wipe ();
7340+ handleManager->destroyObject (hEmphKey);
73367341 emphKey->destroyObject ();
7342+ hEmphKey = CK_INVALID_HANDLE;
73377343 return CKR_FUNCTION_FAILED;
73387344 }
73397345
7346+ ByteString wrapped_2 (pWrappedKey + wrappedLen1, wrappedLen2); // the wrapped target key
73407347 CK_MECHANISM emphMech = {CKM_AES_KEY_WRAP_PAD, NULL_PTR, 0 };
7348+
73417349 // Un-wraps the target key from the second part with the temporary AES key using CKM_AES_KEY_WRAP_PAD (RFC5649)
73427350 rv = UnwrapKeySym (&emphMech, wrapped_2, token, emphKey, keydata);
7351+ // remove the emphkey handle
7352+ handleManager->destroyObject (hEmphKey);
73437353 emphKey->destroyObject ();
7354+ hEmphKey = CK_INVALID_HANDLE;
7355+
73447356 if (rv != CKR_OK)
73457357 {
73467358 return rv;
0 commit comments