Commit 5701a0f0 authored by Juan Lang's avatar Juan Lang Committed by Alexandre Julliard

Implement a few utility functions, with tests.

parent 47fbe042
......@@ -178,3 +178,92 @@ PCCERT_CONTEXT WINAPI CertFindCertificateInStore(HCERTSTORE hCertStore,
SetLastError(CRYPT_E_NOT_FOUND);
return NULL;
}
PCRYPT_ATTRIBUTE WINAPI CertFindAttribute(LPCSTR pszObjId, DWORD cAttr,
CRYPT_ATTRIBUTE rgAttr[])
{
PCRYPT_ATTRIBUTE ret = NULL;
DWORD i;
TRACE("%s %ld %p\n", debugstr_a(pszObjId), cAttr, rgAttr);
if (!cAttr)
return NULL;
if (!pszObjId)
{
SetLastError(ERROR_INVALID_PARAMETER);
return NULL;
}
for (i = 0; !ret && i < cAttr; i++)
if (rgAttr[i].pszObjId && !strcmp(pszObjId, rgAttr[i].pszObjId))
ret = &rgAttr[i];
return ret;
}
PCERT_EXTENSION WINAPI CertFindExtension(LPCSTR pszObjId, DWORD cExtensions,
CERT_EXTENSION rgExtensions[])
{
PCERT_EXTENSION ret = NULL;
DWORD i;
TRACE("%s %ld %p\n", debugstr_a(pszObjId), cExtensions, rgExtensions);
if (!cExtensions)
return NULL;
if (!pszObjId)
{
SetLastError(ERROR_INVALID_PARAMETER);
return NULL;
}
for (i = 0; !ret && i < cExtensions; i++)
if (rgExtensions[i].pszObjId && !strcmp(pszObjId,
rgExtensions[i].pszObjId))
ret = &rgExtensions[i];
return ret;
}
PCERT_RDN_ATTR WINAPI CertFindRDNAttr(LPCSTR pszObjId, PCERT_NAME_INFO pName)
{
PCERT_RDN_ATTR ret = NULL;
DWORD i, j;
TRACE("%s %p\n", debugstr_a(pszObjId), pName);
if (!pszObjId)
{
SetLastError(ERROR_INVALID_PARAMETER);
return NULL;
}
for (i = 0; !ret && i < pName->cRDN; i++)
for (j = 0; !ret && j < pName->rgRDN[i].cRDNAttr; j++)
if (pName->rgRDN[i].rgRDNAttr[j].pszObjId && !strcmp(pszObjId,
pName->rgRDN[i].rgRDNAttr[j].pszObjId))
ret = &pName->rgRDN[i].rgRDNAttr[j];
return ret;
}
LONG WINAPI CertVerifyTimeValidity(LPFILETIME pTimeToVerify,
PCERT_INFO pCertInfo)
{
FILETIME fileTime;
LONG ret;
if (!pTimeToVerify)
{
SYSTEMTIME sysTime;
GetSystemTime(&sysTime);
SystemTimeToFileTime(&sysTime, &fileTime);
pTimeToVerify = &fileTime;
}
if ((ret = CompareFileTime(pTimeToVerify, &pCertInfo->NotBefore)) >= 0)
{
ret = CompareFileTime(pTimeToVerify, &pCertInfo->NotAfter);
if (ret < 0)
ret = 0;
}
return ret;
}
......@@ -32,11 +32,11 @@
@ stub CertEnumCTLsInStore
@ stub CertEnumCertificateContextProperties
@ stdcall CertEnumCertificatesInStore(long ptr)
@ stub CertFindAttribute
@ stdcall CertFindAttribute(str long ptr)
@ stub CertFindCTLInStore
@ stdcall CertFindCertificateInStore(long long long long ptr ptr)
@ stub CertFindExtension
@ stub CertFindRDNAttr
@ stdcall CertFindExtension(str long ptr)
@ stdcall CertFindRDNAttr(str ptr)
@ stub CertFindSubjectInCTL
@ stdcall CertFreeCRLContext(ptr)
@ stub CertFreeCTLContext
......@@ -78,7 +78,7 @@
@ stub CertVerifyCTLUsage
@ stub CertVerifyRevocation
@ stub CertVerifySubjectCertificateContext
@ stub CertVerifyTimeValidity
@ stdcall CertVerifyTimeValidity(ptr ptr)
@ stub CertVerifyValidityNesting
@ stub CreateFileU
@ stub CryptAcquireContextU
......
......@@ -113,8 +113,159 @@ static void testAlgIDToOID(void)
}
}
static void test_findAttribute(void)
{
PCRYPT_ATTRIBUTE ret;
CRYPT_ATTR_BLOB blobs[] = {
{ 3, "\x02\x01\x01" },
};
CRYPT_ATTRIBUTE attr = { "1.2.3", sizeof(blobs) / sizeof(blobs[0]), blobs };
/* returns NULL, last error not set */
SetLastError(0xdeadbeef);
ret = CertFindAttribute(NULL, 0, NULL);
ok(ret == NULL, "Expected failure\n");
ok(GetLastError() == 0xdeadbeef, "Last error was set to %08lx\n",
GetLastError());
/* crashes
SetLastError(0xdeadbeef);
ret = CertFindAttribute(NULL, 1, NULL);
*/
/* returns NULL, last error is ERROR_INVALID_PARAMETER */
SetLastError(0xdeadbeef);
ret = CertFindAttribute(NULL, 1, &attr);
ok(!ret && GetLastError() == ERROR_INVALID_PARAMETER,
"Expected ERROR_INVALID_PARAMETER, got %ld (%08lx)\n", GetLastError(),
GetLastError());
/* returns NULL, last error not set */
SetLastError(0xdeadbeef);
ret = CertFindAttribute("bogus", 1, &attr);
ok(ret == NULL, "Expected failure\n");
ok(GetLastError() == 0xdeadbeef, "Last error was set to %08lx\n",
GetLastError());
/* returns NULL, last error not set */
SetLastError(0xdeadbeef);
ret = CertFindAttribute("1.2.4", 1, &attr);
ok(ret == NULL, "Expected failure\n");
ok(GetLastError() == 0xdeadbeef, "Last error was set to %08lx\n",
GetLastError());
/* succeeds, last error not set */
SetLastError(0xdeadbeef);
ret = CertFindAttribute("1.2.3", 1, &attr);
ok(ret != NULL, "CertFindAttribute failed: %08lx\n", GetLastError());
}
static void test_findExtension(void)
{
PCERT_EXTENSION ret;
CERT_EXTENSION ext = { "1.2.3", TRUE, { 3, "\x02\x01\x01" } };
/* returns NULL, last error not set */
SetLastError(0xdeadbeef);
ret = CertFindExtension(NULL, 0, NULL);
ok(ret == NULL, "Expected failure\n");
ok(GetLastError() == 0xdeadbeef, "Last error was set to %08lx\n",
GetLastError());
/* crashes
SetLastError(0xdeadbeef);
ret = CertFindExtension(NULL, 1, NULL);
*/
/* returns NULL, last error is ERROR_INVALID_PARAMETER */
SetLastError(0xdeadbeef);
ret = CertFindExtension(NULL, 1, &ext);
ok(!ret && GetLastError() == ERROR_INVALID_PARAMETER,
"Expected ERROR_INVALID_PARAMETER, got %ld (%08lx)\n", GetLastError(),
GetLastError());
/* returns NULL, last error not set */
SetLastError(0xdeadbeef);
ret = CertFindExtension("bogus", 1, &ext);
ok(ret == NULL, "Expected failure\n");
ok(GetLastError() == 0xdeadbeef, "Last error was set to %08lx\n",
GetLastError());
/* returns NULL, last error not set */
SetLastError(0xdeadbeef);
ret = CertFindExtension("1.2.4", 1, &ext);
ok(ret == NULL, "Expected failure\n");
ok(GetLastError() == 0xdeadbeef, "Last error was set to %08lx\n",
GetLastError());
/* succeeds, last error not set */
SetLastError(0xdeadbeef);
ret = CertFindExtension("1.2.3", 1, &ext);
ok(ret != NULL, "CertFindExtension failed: %08lx\n", GetLastError());
}
static void test_findRDNAttr(void)
{
PCERT_RDN_ATTR ret;
CERT_RDN_ATTR attrs[] = {
{ "1.2.3", CERT_RDN_IA5_STRING, { 11, "\x16\x09Juan Lang" } },
};
CERT_RDN rdns[] = {
{ sizeof(attrs) / sizeof(attrs[0]), attrs },
};
CERT_NAME_INFO nameInfo = { sizeof(rdns) / sizeof(rdns[0]), rdns };
/* crashes
SetLastError(0xdeadbeef);
ret = CertFindRDNAttr(NULL, NULL);
*/
/* returns NULL, last error is ERROR_INVALID_PARAMETER */
SetLastError(0xdeadbeef);
ret = CertFindRDNAttr(NULL, &nameInfo);
ok(!ret && GetLastError() == ERROR_INVALID_PARAMETER,
"Expected ERROR_INVALID_PARAMETER, got %ld (%08lx)\n", GetLastError(),
GetLastError());
/* returns NULL, last error not set */
SetLastError(0xdeadbeef);
ret = CertFindRDNAttr("bogus", &nameInfo);
ok(ret == NULL, "Expected failure\n");
ok(GetLastError() == 0xdeadbeef, "Last error was set to %08lx\n",
GetLastError());
/* returns NULL, last error not set */
SetLastError(0xdeadbeef);
ret = CertFindRDNAttr("1.2.4", &nameInfo);
ok(ret == NULL, "Expected failure\n");
ok(GetLastError() == 0xdeadbeef, "Last error was set to %08lx\n",
GetLastError());
/* succeeds, last error not set */
SetLastError(0xdeadbeef);
ret = CertFindRDNAttr("1.2.3", &nameInfo);
ok(ret != NULL, "CertFindRDNAttr failed: %08lx\n", GetLastError());
}
static void test_verifyTimeValidity(void)
{
SYSTEMTIME sysTime;
FILETIME fileTime;
CERT_INFO info = { 0 };
LONG ret;
GetSystemTime(&sysTime);
SystemTimeToFileTime(&sysTime, &fileTime);
/* crashes
ret = CertVerifyTimeValidity(NULL, NULL);
ret = CertVerifyTimeValidity(&fileTime, NULL);
*/
/* Check with 0 NotBefore and NotAfter */
ret = CertVerifyTimeValidity(&fileTime, &info);
ok(ret == 1, "Expected 1, got %ld\n", ret);
memcpy(&info.NotAfter, &fileTime, sizeof(info.NotAfter));
/* Check with NotAfter equal to comparison time */
ret = CertVerifyTimeValidity(&fileTime, &info);
ok(ret == 0, "Expected 0, got %ld\n", ret);
/* Check with NotBefore after comparison time */
memcpy(&info.NotBefore, &fileTime, sizeof(info.NotBefore));
info.NotBefore.dwLowDateTime += 5000;
ret = CertVerifyTimeValidity(&fileTime, &info);
ok(ret == -1, "Expected -1, got %ld\n", ret);
}
START_TEST(main)
{
testOIDToAlgID();
testAlgIDToOID();
test_findAttribute();
test_findExtension();
test_findRDNAttr();
test_verifyTimeValidity();
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment