使用SCH_CRED_FORMAT_CERT_HASH时,AcquireCredentialsHandle在内核模式下失败

时间:2018-08-03 09:18:14

标签: c++ c wdk kmdf schannel

我在内核驱动程序中调用AcquireCredentialsHandle,在dwCredFormat设置为SCH_CRED_FORMAT_CERT_HASH的情况下传入SCHANNEL_CRED。它以SEC_E_NO_CREDENTIALS失败。这是我的代码:

BYTE certHashBytes[20] = { 0x6d,0x64,0xed,0x56,0xd2,0x94,0x15,0xf4,0x49,0x08,0xaf,0x18,0xf1,0xca,0xf5,0xa2,0xc8,0x01,0x20,0x96 };
CredHandle credHandle;
RtlZeroMemory(&credHandle, sizeof(CredHandle));

SCHANNEL_CRED schannelCred;
RtlZeroMemory(&schannelCred, sizeof(SCHANNEL_CRED));
schannelCred.dwVersion = 4;
schannelCred.cCreds = 1;
schannelCred.paCred = certHashBytes;
schannelCred.dwCredFormat = 1;

UNICODE_STRING unispName;
RtlUnicodeStringInit(&unispName, L"Microsoft Unified Security Protocol Provider");
TimeStamp ts;

SECURITY_STATUS res = AcquireCredentialsHandle(NULL, &unispName, SECPKG_CRED_INBOUND, NULL, &schannelCred, NULL, NULL, &credHandle, &ts);
DbgPrintEx(DPFLTR_IHVNETWORK_ID, DPFLTR_INFO_LEVEL, "AcquireCredentialsHandle %x\n", res);

对于用户帐户和本地计算机,我的证书哈希绝对正确,并且正确安装在MY存储区中。我知道这是因为它在用户模式下工作正常,如下所示:

HCERTSTORE certStore = CertOpenSystemStore(NULL, L"MY");
BYTE certHashBytes[20] = { 0x6d,0x64,0xed,0x56,0xd2,0x94,0x15,0xf4,0x49,0x08,0xaf,0x18,0xf1,0xca,0xf5,0xa2,0xc8,0x01,0x20,0x96 };
CERT_NAME_BLOB certHash { 20, certHashBytes };
PCCERT_CONTEXT cert = CertFindCertificateInStore(certStore, X509_ASN_ENCODING | PKCS_7_ASN_ENCODING, 0, CERT_FIND_SHA1_HASH, &certHash, NULL);

CredHandle credHandle;
ZeroMemory(&credHandle, sizeof(CredHandle));

SCHANNEL_CRED cred;
ZeroMemory(&cred, sizeof(SCHANNEL_CRED));
cred.dwVersion = SCHANNEL_CRED_VERSION;
cred.cCreds = 1;
cred.paCred = &cert;

SECURITY_STATUS res = AcquireCredentialsHandle(NULL, const_cast<LPWSTR>(UNISP_NAME), SECPKG_CRED_INBOUND, NULL, &cred, NULL, NULL, &credHandle, NULL);

我相信我已按照MSDN的说明正确使用SCH_CRED_FORMAT_CERT_HASH-怎么了?

1 个答案:

答案 0 :(得分:0)

如果不调试,很难确定,但是我看到了一些可能是问题的地方: -如果无法验证证书链;或者是自签名的;或者在执行代码检查CRL时机器无法访问Internet,则呼叫将失败。在这种情况下,请使用CRYPT_E_NO_REVOCATION_CHECK -如果您的证书的目的是正确的,可以向远程服务器证明身份?

Windows中最近对安全性进行了一些增强,使其在证书方面非常挑剔。自签名证书有时比签名证书更容易测试。我已经看到越来越多的正在工作的应用程序因未获得100%证明的证书而停止工作。缺少它,我看不出问题出在哪里。