このトピックでは、システムがダイジェスト メソッドをサポートしていることを確認する方法について説明します。
XPS Digital Signature は Crypto API を使用します。この API は、システムが特定のダイジェスト メソッドをサポートしていることを確認するためのメソッドを提供します。 Crypto API の CryptXmlEnumAlgorithmInfo 関数を使用して、システムでサポートされているダイジェスト メソッドを列挙するには、呼び出し元がコールバック メソッドとデータ構造を提供する必要があります。 CryptXmlEnumAlgorithmInfo 関数は、コールバック メソッドを使用して列挙データを呼び出し元に渡します。
この例で使用されるデータ構造を次のコード例に示し、次のフィールドを含みます。
| フィールド | 説明 |
|---|---|
| userDigestAlgorithm | 確認するダイジェスト アルゴリズムの URI を含む文字列を指す LPWSTR フィールド。 |
| userDigestAlgorithmSupported | 証明書がダイジェスト アルゴリズムをサポートしているかどうかを示す ブール 値。 |
struct DigestMethodData
{
LPCWSTR userDigestAlgorithm;
BOOL userDigestAlgorithmSupported;
};
ダイジェスト メソッドを列挙する Crypto API メソッドは、コールバック メソッドを使用して呼び出し元にデータを返します。 CryptXmlEnumAlgorithmInfo は、システムでサポートされているダイジェスト メソッドを列挙し、コールバック メソッドが FALSE を 返すまで、またはシステムでサポートされているすべてのダイジェスト メソッドが列挙されるまで、列挙する各ダイジェスト メソッドのコールバック メソッドを呼び出します。 この例のコールバック メソッドは、 CryptXmlEnumAlgorithmInfo によって渡されるダイジェスト メソッドと、呼び出し元のメソッドによって提供されるダイジェスト メソッドを比較します。
BOOL WINAPI
EnumDigestMethodCallback (
__in const CRYPT_XML_ALGORITHM_INFO *certMethodInfo,
__inout_opt void *userArg
)
{
// MAX_ALG_ID_LEN is used to set the maximum length of the
// algorithm URI in the string comparison. The URI is not
// likely to be longer than 128 characters so a fixed-size
// buffer is used in this example.
// To make this function more robust, consider
// setting this value dynamically.
static const size_t MAX_ALG_ID_LEN = 128;
DigestMethodData *certificateAlgorithmData =
(DigestMethodData*)userArg;
if (NULL != userArg) {
// Assign user data to local data structure
certificateAlgorithmData = (DigestMethodData*)userArg;
} else {
// Unable to continue this enumeration without
// data from calling method.
return FALSE;
}
// For each algorithm in the enumeration, check to see
// if the URI of the current supported algorithm matches
// the URI passed in userArg.
int cmpResult = 0;
cmpResult = wcsncmp(
certMethodInfo->wszAlgorithmURI,
certificateAlgorithmData->userDigestAlgorithm,
MAX_ALG_ID_LEN );
if ( 0 == cmpResult )
{
// This is a match...
// set supported value to true
certificateAlgorithmData->userDigestAlgorithmSupported = TRUE;
// ...and return FALSE to stop any further enumeration
return FALSE;
}
else
{
// no match was found
// return TRUE to continue enumeration
return TRUE;
}
}
次のコード サンプルでは、検証機能を 1 つのメソッドにラップします。このメソッドは、システムがダイジェスト メソッドをサポートしているかどうかを示す ブール 値を返します。
BOOL
SupportsDigestAlgorithm (
__in LPCWSTR digestMethodToCheck
)
{
HRESULT hr = S_OK;
// Initialize the structure that will hold information about the
// digest method to check
DigestMethodData certificateAlgorithmData;
certificateAlgorithmData.userDigestAlgorithmSupported = FALSE;
certificateAlgorithmData.userDigestAlgorithm = digestMethodToCheck;
// Enumerate the algorithms that are supported on the system,
// the callback method compares each supported algorithm to the one
// passed in digestMethodToCheck and returns true in the
// certificateAlgorithmData.userDigestAlgorithmSupported field if
// the provided digest algorithm is supported by system.
//
// Note that CRYPT_XML_GROUP_ID_HASH is set to enumerate
// digest methods
hr = CryptXmlEnumAlgorithmInfo(
CRYPT_XML_GROUP_ID_HASH, // NOTE: CRYPT_XML_GROUP_ID_HASH
CRYPT_XML_FLAG_DISABLE_EXTENSIONS,
(void*)&certificateAlgorithmData,
EnumDigestMethodCallback);
return certificateAlgorithmData.userDigestAlgorithmSupported;
}
関連トピック
-
次の手順
-
ファイル から証明書を読み込む
-
ドキュメント に証明書チェーンを埋め込む
-
この例で使用
-
CryptXmlEnumAlgorithmInfo
-
詳細