Make CryptImport/ExportPublicKeyInfoEx behave the way MSDN describes

them, and a couple of tests.
This commit is contained in:
Juan Lang 2005-09-30 10:35:31 +00:00 committed by Alexandre Julliard
parent 4c5067ef8f
commit 5b0812fbcf
2 changed files with 94 additions and 22 deletions

View File

@ -2360,8 +2360,6 @@ BOOL WINAPI CryptEncodeObjectEx(DWORD dwCertEncodingType, LPCSTR lpszStructType,
} }
else if (!strcmp(lpszStructType, szOID_CERT_EXTENSIONS)) else if (!strcmp(lpszStructType, szOID_CERT_EXTENSIONS))
encodeFunc = CRYPT_AsnEncodeExtensions; encodeFunc = CRYPT_AsnEncodeExtensions;
else if (!strcmp(lpszStructType, szOID_RSA_RSA))
encodeFunc = CRYPT_AsnEncodeRsaPubKey;
else if (!strcmp(lpszStructType, szOID_RSA_signingTime)) else if (!strcmp(lpszStructType, szOID_RSA_signingTime))
encodeFunc = CRYPT_AsnEncodeUtcTime; encodeFunc = CRYPT_AsnEncodeUtcTime;
else if (!strcmp(lpszStructType, szOID_CRL_REASON_CODE)) else if (!strcmp(lpszStructType, szOID_CRL_REASON_CODE))
@ -5486,8 +5484,6 @@ BOOL WINAPI CryptDecodeObjectEx(DWORD dwCertEncodingType, LPCSTR lpszStructType,
} }
else if (!strcmp(lpszStructType, szOID_CERT_EXTENSIONS)) else if (!strcmp(lpszStructType, szOID_CERT_EXTENSIONS))
decodeFunc = CRYPT_AsnDecodeExtensions; decodeFunc = CRYPT_AsnDecodeExtensions;
else if (!strcmp(lpszStructType, szOID_RSA_RSA))
decodeFunc = CRYPT_AsnDecodeRsaPubKey;
else if (!strcmp(lpszStructType, szOID_RSA_signingTime)) else if (!strcmp(lpszStructType, szOID_RSA_signingTime))
decodeFunc = CRYPT_AsnDecodeUtcTime; decodeFunc = CRYPT_AsnDecodeUtcTime;
else if (!strcmp(lpszStructType, szOID_CRL_REASON_CODE)) else if (!strcmp(lpszStructType, szOID_CRL_REASON_CODE))
@ -5531,9 +5527,9 @@ BOOL WINAPI CryptExportPublicKeyInfo(HCRYPTPROV hCryptProv, DWORD dwKeySpec,
NULL, 0, NULL, pInfo, pcbInfo); NULL, 0, NULL, pInfo, pcbInfo);
} }
BOOL WINAPI CryptExportPublicKeyInfoEx(HCRYPTPROV hCryptProv, DWORD dwKeySpec, static BOOL WINAPI CRYPT_ExportRsaPublicKeyInfoEx(HCRYPTPROV hCryptProv,
DWORD dwCertEncodingType, LPSTR pszPublicKeyObjId, DWORD dwFlags, DWORD dwKeySpec, DWORD dwCertEncodingType, LPSTR pszPublicKeyObjId,
void *pvAuxInfo, PCERT_PUBLIC_KEY_INFO pInfo, DWORD *pcbInfo) DWORD dwFlags, void *pvAuxInfo, PCERT_PUBLIC_KEY_INFO pInfo, DWORD *pcbInfo)
{ {
BOOL ret; BOOL ret;
HCRYPTKEY key; HCRYPTKEY key;
@ -5542,12 +5538,6 @@ BOOL WINAPI CryptExportPublicKeyInfoEx(HCRYPTPROV hCryptProv, DWORD dwKeySpec,
dwCertEncodingType, debugstr_a(pszPublicKeyObjId), dwFlags, pvAuxInfo, dwCertEncodingType, debugstr_a(pszPublicKeyObjId), dwFlags, pvAuxInfo,
pInfo, pcbInfo); pInfo, pcbInfo);
if (!hCryptProv)
{
SetLastError(ERROR_INVALID_PARAMETER);
return FALSE;
}
if (!pszPublicKeyObjId) if (!pszPublicKeyObjId)
pszPublicKeyObjId = szOID_RSA_RSA; pszPublicKeyObjId = szOID_RSA_RSA;
if ((ret = CryptGetUserKey(hCryptProv, dwKeySpec, &key))) if ((ret = CryptGetUserKey(hCryptProv, dwKeySpec, &key)))
@ -5568,7 +5558,7 @@ BOOL WINAPI CryptExportPublicKeyInfoEx(HCRYPTPROV hCryptProv, DWORD dwKeySpec,
DWORD encodedLen = 0; DWORD encodedLen = 0;
ret = CryptEncodeObject(dwCertEncodingType, ret = CryptEncodeObject(dwCertEncodingType,
pszPublicKeyObjId, pubKey, NULL, &encodedLen); RSA_CSP_PUBLICKEYBLOB, pubKey, NULL, &encodedLen);
if (ret) if (ret)
{ {
DWORD sizeNeeded = sizeof(CERT_PUBLIC_KEY_INFO) + DWORD sizeNeeded = sizeof(CERT_PUBLIC_KEY_INFO) +
@ -5590,13 +5580,14 @@ BOOL WINAPI CryptExportPublicKeyInfoEx(HCRYPTPROV hCryptProv, DWORD dwKeySpec,
pszPublicKeyObjId); pszPublicKeyObjId);
pInfo->Algorithm.Parameters.cbData = 0; pInfo->Algorithm.Parameters.cbData = 0;
pInfo->Algorithm.Parameters.pbData = NULL; pInfo->Algorithm.Parameters.pbData = NULL;
pInfo->PublicKey.pbData = (BYTE *)pInfo->Algorithm.pszObjId pInfo->PublicKey.pbData =
(BYTE *)pInfo->Algorithm.pszObjId
+ lstrlenA(pInfo->Algorithm.pszObjId) + 1; + lstrlenA(pInfo->Algorithm.pszObjId) + 1;
pInfo->PublicKey.cbData = encodedLen; pInfo->PublicKey.cbData = encodedLen;
pInfo->PublicKey.cUnusedBits = 0; pInfo->PublicKey.cUnusedBits = 0;
ret = CryptEncodeObject(dwCertEncodingType, ret = CryptEncodeObject(dwCertEncodingType,
pszPublicKeyObjId, pubKey, pInfo->PublicKey.pbData, RSA_CSP_PUBLICKEYBLOB, pubKey,
&pInfo->PublicKey.cbData); pInfo->PublicKey.pbData, &pInfo->PublicKey.cbData);
} }
} }
} }
@ -5610,6 +5601,40 @@ BOOL WINAPI CryptExportPublicKeyInfoEx(HCRYPTPROV hCryptProv, DWORD dwKeySpec,
return ret; return ret;
} }
typedef BOOL (WINAPI *ExportPublicKeyInfoExFunc)(HCRYPTPROV hCryptProv,
DWORD dwKeySpec, DWORD dwCertEncodingType, LPSTR pszPublicKeyObjId,
DWORD dwFlags, void *pvAuxInfo, PCERT_PUBLIC_KEY_INFO pInfo, DWORD *pcbInfo);
BOOL WINAPI CryptExportPublicKeyInfoEx(HCRYPTPROV hCryptProv, DWORD dwKeySpec,
DWORD dwCertEncodingType, LPSTR pszPublicKeyObjId, DWORD dwFlags,
void *pvAuxInfo, PCERT_PUBLIC_KEY_INFO pInfo, DWORD *pcbInfo)
{
BOOL ret;
ExportPublicKeyInfoExFunc exportFunc = NULL;
HMODULE lib = NULL;
TRACE("(%ld, %ld, %08lx, %s, %08lx, %p, %p, %p)\n", hCryptProv, dwKeySpec,
dwCertEncodingType, debugstr_a(pszPublicKeyObjId), dwFlags, pvAuxInfo,
pInfo, pcbInfo);
if (!hCryptProv)
{
SetLastError(ERROR_INVALID_PARAMETER);
return FALSE;
}
if (pszPublicKeyObjId)
exportFunc = CRYPT_GetFunc(dwCertEncodingType, pszPublicKeyObjId,
CRYPT_OID_EXPORT_PUBLIC_KEY_INFO_FUNC, &lib);
if (!exportFunc)
exportFunc = CRYPT_ExportRsaPublicKeyInfoEx;
ret = exportFunc(hCryptProv, dwKeySpec, dwCertEncodingType,
pszPublicKeyObjId, dwFlags, pvAuxInfo, pInfo, pcbInfo);
if (lib)
FreeLibrary(lib);
return ret;
}
BOOL WINAPI CryptImportPublicKeyInfo(HCRYPTPROV hCryptProv, BOOL WINAPI CryptImportPublicKeyInfo(HCRYPTPROV hCryptProv,
DWORD dwCertEncodingType, PCERT_PUBLIC_KEY_INFO pInfo, HCRYPTKEY *phKey) DWORD dwCertEncodingType, PCERT_PUBLIC_KEY_INFO pInfo, HCRYPTKEY *phKey)
{ {
@ -5617,7 +5642,7 @@ BOOL WINAPI CryptImportPublicKeyInfo(HCRYPTPROV hCryptProv,
0, 0, NULL, phKey); 0, 0, NULL, phKey);
} }
BOOL WINAPI CryptImportPublicKeyInfoEx(HCRYPTPROV hCryptProv, static BOOL WINAPI CRYPT_ImportRsaPublicKeyInfoEx(HCRYPTPROV hCryptProv,
DWORD dwCertEncodingType, PCERT_PUBLIC_KEY_INFO pInfo, ALG_ID aiKeyAlg, DWORD dwCertEncodingType, PCERT_PUBLIC_KEY_INFO pInfo, ALG_ID aiKeyAlg,
DWORD dwFlags, void *pvAuxInfo, HCRYPTKEY *phKey) DWORD dwFlags, void *pvAuxInfo, HCRYPTKEY *phKey)
{ {
@ -5627,7 +5652,7 @@ BOOL WINAPI CryptImportPublicKeyInfoEx(HCRYPTPROV hCryptProv,
TRACE("(%ld, %ld, %p, %d, %08lx, %p, %p)\n", hCryptProv, TRACE("(%ld, %ld, %p, %d, %08lx, %p, %p)\n", hCryptProv,
dwCertEncodingType, pInfo, aiKeyAlg, dwFlags, pvAuxInfo, phKey); dwCertEncodingType, pInfo, aiKeyAlg, dwFlags, pvAuxInfo, phKey);
ret = CryptDecodeObject(dwCertEncodingType, pInfo->Algorithm.pszObjId, ret = CryptDecodeObject(dwCertEncodingType, RSA_CSP_PUBLICKEYBLOB,
pInfo->PublicKey.pbData, pInfo->PublicKey.cbData, 0, NULL, &pubKeySize); pInfo->PublicKey.pbData, pInfo->PublicKey.cbData, 0, NULL, &pubKeySize);
if (ret) if (ret)
{ {
@ -5635,9 +5660,9 @@ BOOL WINAPI CryptImportPublicKeyInfoEx(HCRYPTPROV hCryptProv,
if (pubKey) if (pubKey)
{ {
ret = CryptDecodeObject(dwCertEncodingType, ret = CryptDecodeObject(dwCertEncodingType, RSA_CSP_PUBLICKEYBLOB,
pInfo->Algorithm.pszObjId, pInfo->PublicKey.pbData, pInfo->PublicKey.pbData, pInfo->PublicKey.cbData, 0, pubKey,
pInfo->PublicKey.cbData, 0, pubKey, &pubKeySize); &pubKeySize);
if (ret) if (ret)
ret = CryptImportKey(hCryptProv, pubKey, pubKeySize, 0, 0, ret = CryptImportKey(hCryptProv, pubKey, pubKeySize, 0, 0,
phKey); phKey);
@ -5648,3 +5673,29 @@ BOOL WINAPI CryptImportPublicKeyInfoEx(HCRYPTPROV hCryptProv,
} }
return ret; return ret;
} }
typedef BOOL (WINAPI *ImportPublicKeyInfoExFunc)(HCRYPTPROV hCryptProv,
DWORD dwCertEncodingType, PCERT_PUBLIC_KEY_INFO pInfo, ALG_ID aiKeyAlg,
DWORD dwFlags, void *pvAuxInfo, HCRYPTKEY *phKey);
BOOL WINAPI CryptImportPublicKeyInfoEx(HCRYPTPROV hCryptProv,
DWORD dwCertEncodingType, PCERT_PUBLIC_KEY_INFO pInfo, ALG_ID aiKeyAlg,
DWORD dwFlags, void *pvAuxInfo, HCRYPTKEY *phKey)
{
BOOL ret;
ImportPublicKeyInfoExFunc importFunc = NULL;
HMODULE lib = NULL;
TRACE("(%ld, %ld, %p, %d, %08lx, %p, %p)\n", hCryptProv,
dwCertEncodingType, pInfo, aiKeyAlg, dwFlags, pvAuxInfo, phKey);
importFunc = CRYPT_GetFunc(dwCertEncodingType, pInfo->Algorithm.pszObjId,
CRYPT_OID_IMPORT_PUBLIC_KEY_INFO_FUNC, &lib);
if (!importFunc)
importFunc = CRYPT_ImportRsaPublicKeyInfoEx;
ret = importFunc(hCryptProv, dwCertEncodingType, pInfo, aiKeyAlg, dwFlags,
pvAuxInfo, phKey);
if (lib)
FreeLibrary(lib);
return ret;
}

View File

@ -1507,6 +1507,16 @@ static void test_encodeRsaPublicKey(DWORD dwEncoding)
ok(!memcmp(buf, rsaPubKeys[0].encoded, bufSize), "Unexpected value\n"); ok(!memcmp(buf, rsaPubKeys[0].encoded, bufSize), "Unexpected value\n");
LocalFree(buf); LocalFree(buf);
} }
/* Check a couple of RSA-related OIDs */
hdr->aiKeyAlg = CALG_RSA_KEYX;
ret = CryptEncodeObjectEx(dwEncoding, szOID_RSA_RSA,
toEncode, CRYPT_ENCODE_ALLOC_FLAG, NULL, (BYTE *)&buf, &bufSize);
ok(!ret && GetLastError() == ERROR_FILE_NOT_FOUND,
"Expected ERROR_FILE_NOT_FOUND, got %08lx\n", GetLastError());
ret = CryptEncodeObjectEx(dwEncoding, szOID_RSA_SHA1RSA,
toEncode, CRYPT_ENCODE_ALLOC_FLAG, NULL, (BYTE *)&buf, &bufSize);
ok(!ret && GetLastError() == ERROR_FILE_NOT_FOUND,
"Expected ERROR_FILE_NOT_FOUND, got %08lx\n", GetLastError());
/* Finally, all valid */ /* Finally, all valid */
hdr->aiKeyAlg = CALG_RSA_KEYX; hdr->aiKeyAlg = CALG_RSA_KEYX;
for (i = 0; i < sizeof(rsaPubKeys) / sizeof(rsaPubKeys[0]); i++) for (i = 0; i < sizeof(rsaPubKeys) / sizeof(rsaPubKeys[0]); i++)
@ -1541,6 +1551,17 @@ static void test_decodeRsaPublicKey(DWORD dwEncoding)
CRYPT_DECODE_ALLOC_FLAG, NULL, (BYTE *)&buf, &bufSize); CRYPT_DECODE_ALLOC_FLAG, NULL, (BYTE *)&buf, &bufSize);
ok(!ret && GetLastError() == CRYPT_E_ASN1_EOD, ok(!ret && GetLastError() == CRYPT_E_ASN1_EOD,
"Expected CRYPT_E_ASN1_EOD, got %08lx\n", CRYPT_E_ASN1_EOD); "Expected CRYPT_E_ASN1_EOD, got %08lx\n", CRYPT_E_ASN1_EOD);
/* Try with a couple of RSA-related OIDs */
ret = CryptDecodeObjectEx(dwEncoding, szOID_RSA_RSA,
rsaPubKeys[0].encoded, rsaPubKeys[0].encoded[1] + 2,
CRYPT_DECODE_ALLOC_FLAG, NULL, (BYTE *)&buf, &bufSize);
ok(!ret && GetLastError() == ERROR_FILE_NOT_FOUND,
"Expected ERROR_FILE_NOT_FOUND, got %08lx\n", GetLastError());
ret = CryptDecodeObjectEx(dwEncoding, szOID_RSA_SHA1RSA,
rsaPubKeys[0].encoded, rsaPubKeys[0].encoded[1] + 2,
CRYPT_DECODE_ALLOC_FLAG, NULL, (BYTE *)&buf, &bufSize);
ok(!ret && GetLastError() == ERROR_FILE_NOT_FOUND,
"Expected ERROR_FILE_NOT_FOUND, got %08lx\n", GetLastError());
/* Now try success cases */ /* Now try success cases */
for (i = 0; i < sizeof(rsaPubKeys) / sizeof(rsaPubKeys[0]); i++) for (i = 0; i < sizeof(rsaPubKeys) / sizeof(rsaPubKeys[0]); i++)
{ {