Results 1 to 9 of 9

Thread: Please Please help with this API : CertFindCertificateInStore

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jul 2001
    Location
    australia
    Posts
    94

    Please Please help with this API : CertFindCertificateInStore

    can somebody please help me with the "CertFindCertificateInStore" API? I want to use it in VB but I can only find C samples in MSDN. THANKS VERY MUCH!

  2. #2

    Thread Starter
    Lively Member
    Join Date
    Jul 2001
    Location
    australia
    Posts
    94
    any body please?

  3. #3
    PowerPoster
    Join Date
    Jul 2002
    Location
    Dublin, Ireland
    Posts
    2,148
    C declaration:
    Code:
    WINCRYPT32API
    PCCERT_CONTEXT
    WINAPI
    CertFindCertificateInStore(
        IN HCERTSTORE hCertStore,
        IN DWORD dwCertEncodingType,
        IN DWORD dwFindFlags,
        IN DWORD dwFindType,
        IN const void *pvFindPara,
        IN PCCERT_CONTEXT pPrevCertContext
        );
    which is documented on this MSDN page and is exported by Crypt32.lib...

    Therefore in VB this would be:
    VB Code:
    1. Private Type CERT_CONTEXT
    2.     dwCertEncodingType As Long
    3.     pbCertEncoded As Long '<-Pointer to byte...
    4.     cbCertEncoded As Long
    5.     pCertInfo As Long '<--Pointer to CERT_INFO  
    6.     hCertStore As Long
    7. End Type
    8.  
    9. Private Declare Function CertFindCertificateInStore Lib "Crypt32.dll" ( ByVal hCertStore As Long , _
    10.                             ByVal dwCertEncodingType As Long, _
    11.                             ByVal dwFindFlags As Long, _
    12.                             ByVal dwFindType As Long, _
    13.                             ByVal pvFindPara As Long,
    14.                             pPrevCertContext As CERT_CONTEXT ) As Long
    15.  
    16. Private Declare Function CertFindCertificateInStoreByLong Lib "Crypt32.dll" _
    17. Alias "CertFindCertificateInStore "( ByVal hCertStore As Long , _
    18.                             ByVal dwCertEncodingType As Long, _
    19.                             ByVal dwFindFlags As Long, _
    20.                             ByVal dwFindType As Long, _
    21.                             ByVal pvFindPara As Long,
    22.                             ByVal pPrevCertContext As Long ) As Long

    Hope this helps...
    Duncan

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Jul 2001
    Location
    australia
    Posts
    94
    Let me try try , thanks Merrion

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Jul 2001
    Location
    australia
    Posts
    94
    I have finished the following types declarations :

    Code:
    ' Converted from c structs
    
    Type CRYPT_INTEGER_BLOB
        cbData As Long
        pbData() As Byte
    End Type
    
    Type CERT_NAME_BLOB
        cbData As Long
        pbData() As Byte
    End Type
    
    Type CRYPT_OBJID_BLOB
        cbData As Long
        pbData() As Byte
    End Type
    
    Type CRYPT_BIT_BLOB
        cbData As Long
        pbData() As Byte
        cUnusedBits As Long
    End Type
    
    Type CERT_EXTENSION
        pszObjId As String
        fCritical As Boolean
        Value As CRYPT_OBJID_BLOB
    End Type
    
    Type CRYPT_ALGORITHM_IDENTIFIER
        pszObjId As String
        Parameters As CRYPT_OBJID_BLOB
    End Type
    
    Type CERT_PUBLIC_KEY_INFO
        Algorithm As CRYPT_ALGORITHM_IDENTIFIER
        PublicKey As CRYPT_BIT_BLOB
    End Type
    
    Type CERT_INFO
        dwVersion As Long
        SerialNumber As CRYPT_INTEGER_BLOB
        SignatureAlgorithm As CRYPT_ALGORITHM_IDENTIFIER
        Issuer As CERT_NAME_BLOB
        NotBefore As Date
        NotAfter As Date
        Subject As CERT_NAME_BLOB
        SubjectPublicKeyInfo As CERT_PUBLIC_KEY_INFO
        IssuerUniqueId As CRYPT_BIT_BLOB
        SubjectUniqueId As CRYPT_BIT_BLOB
        cExtension As Long
        rgExtension As CERT_EXTENSION
    End Type
    
    Public Type CERT_CONTEXT
        dwCertEncodingType As Long
        pbCertEncoded As Byte
        cbCertEncoded As Long
        pCertInfo As CERT_INFO
        hCertStore As Long
    End Type
    It will be better if i can find C type mapping to Visual Basics...
    Last edited by dimsumZ; Aug 2nd, 2002 at 05:10 AM.

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Jul 2001
    Location
    australia
    Posts
    94
    My target is find user's cert to use private to encrypt data, by now i only can do 2 steps :

    1. Get CSP context
    2. Get System Store handler
    3. (stucked) Find certificate in store

    Code as follows :

    Code:
    private sub Form_Load()
    
    ' Get CSP context
    Dim context_Handler As Long
    CryptAcquireContext context_Handler, "administrator", _
    "Microsoft Base Cryptographic Provider v1.0", 1, 0
    
    ' Get System Store handler, can use MY or CA or ROOT or SPC
    Dim system_Store_Handler as Long
    system_Store_Handler = CertOpenSystemStore(context_Handler, "MY")
    
    ' This is the hard part... Find cert in store
    Dim i as Long
    Dim j As CERT_CONTEXT
        
    i = CertFindCertificateInStore( _
            CLng(system_Store_Handler), _
            CLng(MY_ENCODING_TYPE), _
            0, _
            CLng(CERT_FIND_ANY), _
            "", _
            j)
    ' always get 0, help
    
    End Sub

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Jul 2001
    Location
    australia
    Posts
    94
    API declarations :

    Code:
    Public Declare Function CryptAcquireContext Lib "advapi32.dll" Alias "CryptAcquireContextA" _
        (ByRef phProv As Long, _
        ByVal pszContainer As String, _
        ByVal pszProvider As String, _
        ByVal dwProvType As Long, _
        ByVal dwFlags As Long) As Long
    
    Public Declare Function CertOpenSystemStore Lib "Crypt32" Alias "CertOpenSystemStoreA" ( _
        hCryptProv As Long, _
        szSubsystemProtocol As String) As Long
    
    Public Declare Function CertFindCertificateInStore Lib "Crypt32" ( _
        hCertStore As Long, _
        dwCertEncodingType As Long, _
        dwFindFlags As Long, _
        dwFindType As Long, _
        pvFindPara As String, _
        pPrevCertContext As CERT_CONTEXT _
    ) As Long
    Any Hints Please?

  8. #8
    PowerPoster
    Join Date
    Jul 2002
    Location
    Dublin, Ireland
    Posts
    2,148
    The first time you call CertFindCertificateInStore you need to pass NULL in pPrevContext - which is why you need the CertFindCertificateInStoreByLong Alias.

    VB Code:
    1. Dim i as Long
    2. Dim j As CERT_CONTEXT
    3.    
    4. i = CertFindCertificateInStoreByLong( _
    5.         CLng(system_Store_Handler), _
    6.         CLng(MY_ENCODING_TYPE), _
    7.         0, _
    8.         CLng(CERT_FIND_ANY), _
    9.         "", _
    10.         vbNull) '<--This must be null the first time...
    11.  
    12. '\\ i now points to a memory address...you need to use RTLMovememory to get the data thence...

    I usually ALIAS RtlMovememory to prevent GPFs - for CERT_CONTEXT it woulkd be thus:

    VB Code:
    1. Private Declare Sub CopyMemoryCERT_CONTEXT Lib "kernel32" Alias "RtlMoveMemory" (Destination As CERT_CONTEXT, ByVal Source As Long, ByVal Length As Long)
    2. Private Declare Function IsBadReadPtr Lib "kernel32" (ByVal lp As Long, ByVal ucb As Long) As Long
    3. Private Declare Function IsBadWritePtr Lib "kernel32" (ByVal lp As Long, ByVal ucb As Long) As Long

    which you would add to your code thus:

    VB Code:
    1. '...
    2. If Not IsBadReadPtr(i, Len(j)) Then
    3.    Call CopyMemoryCERT_CONTEXT(j,i,Len(j))
    4. End If

    HTH,
    Duncan

  9. #9

    Thread Starter
    Lively Member
    Join Date
    Jul 2001
    Location
    australia
    Posts
    94
    Many thanks, Merrion!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width