+
+#ifdef HAVE_WINDOWS_H
+ /* On Windows, if the credential payload isn't cleartext CBOR, it is encrypted. Decrypt and retry. */
+ if (ret != OC_STACK_OK)
+ {
+ DATA_BLOB encryptedPayload = { .cbData = size, .pbData = data };
+ DATA_BLOB decryptedPayload = { .cbData = 0, .pbData = NULL };
+
+ if (CryptUnprotectData(
+ &encryptedPayload,
+ NULL,
+ NULL,
+ NULL,
+ NULL,
+ CRYPTPROTECT_UI_FORBIDDEN,
+ &decryptedPayload))
+ {
+ ret = CBORPayloadToCred(decryptedPayload.pbData, decryptedPayload.cbData, &gCred);
+
+ /* For the returned data from CryptUnprotectData, LocalFree must be used to free. Don't use OICFree. */
+ OICClearMemory(decryptedPayload.pbData, decryptedPayload.cbData);
+ if (NULL != LocalFree(decryptedPayload.pbData))
+ {
+ OIC_LOG_V(ERROR, TAG, "LocalFree failed on output from CryptUnprotectData; memory may be corrupted or leaked. Last error: %u.", GetLastError());
+ assert(!"LocalFree failed");
+ }
+ }
+ else
+ {
+ /* Credential resource is corrupted, or we no longer have access to the encryption key to decrypt it. */
+ OIC_LOG_V(ERROR, TAG, "Failed to CryptUnprotectData cred resource: %u", GetLastError());
+ ret = OC_STACK_ERROR;
+ }
+ }
+#endif