Announcement

Collapse
No announcement yet.

Can't get this WinAPI call to return a handle.

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • #21
    I guess you have an error message from CryptAcquireCertificatePrivateKey.
    With my posted code above, what if you try...

    Code:
     'Insert this just above "IF pDesiredCert THEN CertFreeCertificateContext(pDesiredCert)" line.
     IF pDesiredCert THEN 'CryptAcquireCertificatePrivateKey
       LOCAL RetVal           AS LONG
       LOCAL hProv            AS DWORD
       LOCAL dwKeySpec        AS DWORD
       LOCAL pfCallerFreeProv AS DWORD
    
       RetVal = CryptAcquireCertificatePrivateKey(pDesiredCert, %NULL, %NULL, hProv, dwKeySpec, pfCallerFreeProv)
       dwLastError = GetLastError()
       MessageBox(%HWND_DESKTOP, "Note: CryptAcquireCertificatePrivateKey"                  & $CRLF & _
                                 "can only be used by the owner of a private key." & $CRLF  & $CRLF & _
                                 "hProv            " & $TAB & "0x" & HEX$(hProv)            & $CRLF & _
                                 "dwKeySpec        " & $TAB & "0x" & HEX$(dwKeySpec)        & $CRLF & _
                                 "pfCallerFreeProv " & $TAB & "0x" & HEX$(pfCallerFreeProv) & $CRLF & _
                                 "RetVal           " & $TAB & FORMAT$(RetVal)               & $CRLF & _
                                 WinError$(dwLastError), "PrivateKey", %MB_OK OR %MB_SYSTEMMODAL OR %MB_TOPMOST)
     END IF

    Comment


    • #22
      All = 0
      hProv = 0
      dwKeySpec = 0
      pfCallerFreeProv 0
      RetVal = 0

      The entire thing is quite confusing! I am not sure it I am understanding correctly.

      I made a simple one:


      Code:
      #COMPILE EXE
      #DIM ALL
      
      #INCLUDE "win32api.inc"
      '------------------------------------------------------------------------------
      FUNCTION GetErrorMessage (BYVAL errorCode AS DWORD) AS STRING
      
          LOCAL errorBuffer AS STRINGZ * 1024
          LOCAL numChars    AS DWORD
      
          numChars = FormatMessage(%FORMAT_MESSAGE_FROM_SYSTEM OR %FORMAT_MESSAGE_IGNORE_INSERTS, BYVAL %NULL, errorCode, %LANG_USER_DEFAULT, errorBuffer, SIZEOF(errorBuffer), BYVAL %NULL)
      
          IF numChars THEN
              FUNCTION = LEFT$(errorBuffer, numChars)
          ELSE
              FUNCTION = "Error " & STR$(errorCode)
          END IF
      
      END FUNCTION
      '------------------------------------------------------------------------------      ​
      FUNCTION PBMAIN () AS LONG
      
         LOCAL result            AS DWORD
         LOCAL errorMessage      AS STRING * 1024
         LOCAL hFileStoreHandle  AS DWORD
         LOCAL Cert_File_Name    AS STRINGZ  * 255
         LOCAL pCertContext      AS CERT_CONTEXT PTR
         LOCAL pvFindPara        AS STRINGZ *255
         LOCAL szName            AS ASCIIZ * 256
         LOCAL hProv             AS DWORD 'ptr
         LOCAL dwKeySpec         AS DWORD
         LOCAL pfCallerFreeProv  AS DWORD
      
      
         Cert_File_Name = "comm.crt"
         hFileStoreHandle = CertOpenStore(BYVAL %CERT_STORE_PROV_FILENAME_A, 0, 0, 0, Cert_File_Name)
         errorMessage = GetErrorMessage(GetLasterror)
      
         pCertContext = CertFindCertificateInStore(hFileStoreHandle, %X509_ASN_ENCODING, 0, 0, pvFindPara, 0)
      
         'Next line is to prove we have the "asp-emu" Cert - you can look at the szName
         result = CertGetNameString(pCertContext, %CERT_NAME_SIMPLE_DISPLAY_TYPE, 0, BYVAL %NULL, szName, SIZEOF(szName))
      
         result = CryptAcquireCertificatePrivateKey(pCertContext, %CRYPT_ACQUIRE_SILENT_FLAG OR %CRYPT_ACQUIRE_ALLOW_NCRYPT_KEY_FLAG, %NULL, hProv, dwKeySpec, pfCallerFreeProv)
      
         errorMessage = GetErrorMessage(GetLasterror)
      
      END FUNCTION  ​

      Comment


      • #23
        The goal of my previous post is to get an error message to have an idea of what is going wrong.
        What was it ?

        Comment


        • #24
          Now I understand Pierre! Let me look!

          Comment


          • #25
            David,
            Did you forgot about this thread?

            Comment


            • #26
              Hi Pierre! No I did not forget, just got pulled onto some other things. I was getting all 0 from the code you gave me.

              I talked to a friend of mine and he said I should convert the cert to a PXF and start over.

              So I may have been trying to solve the wrong problem - The Cert stuff is something I know next to nothing about. Guess now it the time to learn.
              Stuart is also helping me. I will get back to this over the weekend..

              Thanks you for ALL THE HELP!! Very much appreciated.

              Comment

              Working...
              X