証明書ストアへの証明書の追加

[CAPICOM は、次のオペレーティング システムで使用できる 32 ビットのみのコンポーネントです: Windows Server 2008、Windows Vista、Windows XP。 代わりに、.NET Frameworkを使用してセキュリティ機能を実装します。 詳細については、「 CAPICOM を使用する代替手段」を参照してください。

証明書 は、ストアが読み取り/書き込みアクセス許可で開かれている場合は、証明書ストアに追加したり、 証明書 ストアから削除したりすることができます。 Active Directory ストアに対する読み取り/書き込みアクセス許可は付与されません。 証明書はメモリ ストアに追加したり、メモリ ストアから削除したりできますが、メモリ ストアの変更はセッション間で保持されません。

証明書は、Add メソッドを使用して読み取り/書き込みアクセス許可で開かれた証明書ストアに 追加 できます。 Remove メソッドを使用して、読み取り/書き込みアクセス許可で開かれた証明書ストアから証明書を 削除 できます。 新しいストアは、CAPICOM_CURRENT_USER_STOREとCAPICOM_LOCAL_MACHINE_STOREの場所に作成および保存できます。 これらの場所のいずれかで新しく作成されたストアは、読み取り/書き込みアクセス許可を使用して開くことができます。

次の例では、2 つの証明書ストアが開かれています。 F で始まる姓を持つサブジェクトの証明書は、Active Directory ストアから取得されます。 CAPICOM_CURRENT_USER_STORE CAPICOM_CA_STOREストアは読み取り/書き込みストアとして開き、Active Directory ストア内の証明書のコレクションからの最初の証明書がCAPICOM_CA_STOREの証明書に追加されます。

デモの目的で、この例では、CAPICOM_MEMORY_STORE、CAPICOM_CURRENT_USER_STORE、CAPICOM_LOCAL_MACHINE_STOREの場所での店舗のオープンを示しています。 この例では、開いているストアからすべての証明書をエクスポートし、エクスポートした証明書をファイルに書き込み、読み込み直して、別のストアにインポートする方法を示します。 新しくインポートされた証明書は列挙されて表示されます。

CAPICOM エラーでは、 Err.Number の負の 10 進値が返されます。 詳細については、「 CAPICOM_ERROR_CODE」を参照してください。 Err.Number の正の 10 進値については、「Winerror.h」を参照してください。

次の例は、Store オブジェクトの宣言と、それらのオブジェクトのインスタンスの作成で、早期バインディングを使用して証明書 ストア を開く方法を示しています。

Sub AddCert()
On Error GoTo ErrorHandler
' The following shows two different ways to declare and
' create a store object.

Dim myADstore As New Store

Dim myCAstore As Store
Set myCAstore = New Store

' In this example, the Active Directory store will be searched for a 
' certificate with a subject name that begins with the letter F. 
' This is done by using the string "SN=F*" as the name of the store.

Dim SubjectNameSN As String
SubjectNameSN = "SN=F*"

' Active Directory stores can only be opened with read-only
' access.

myADstore.Open CAPICOM_ACTIVE_DIRECTORY_USER_STORE,
                SubjectNameSN , CAPICOM_STORE_OPEN_READ_ONLY

'  This example assumes that the store opened and that
'  at least one certificate was returned.
'  A complete application would ensure that at least one certificate
'  was in the store before proceeding and would
'  also select one or more of the certificates returned
'  to be added instead of using the first certificate
'  in the collection.

'  Open the MY store so that a certificate can be added.

myCAstore.Open CAPICOM_CURRENT_USER_STORE, CAPICOM_MY_STORE,
                    CAPICOM_STORE_OPEN_READ_WRITE

myCAstore.Add myADstore.certificates.Item(1)

' Release the two store objects.

Set myCAstore = Nothing
Set myADstore = Nothing
Exit Sub

ErrorHandler:
If Err.Number > 0 Then
    MsgBox "Visual Basic error found:" & Err.Description
Else
    MsgBox "CAPICOM error found : " & Err.Number
End If
End Sub