Handle from ctypes call to DLL function is too large

-1

I'm attempting to read a .p12 file using the CAPI function PFXImportCertStore.

What I have to do is create a structure for CRYPT_DATA_BLOB like so:

class CRYPT_DATA_BLOB(ctypes.Structure):
    _fields_ = [('cbData', ctypes.wintypes.DWORD),   # DWORD cbData
                ('pbData', ctypes.wintypes.LPBYTE))  # BYTE *pbData
               ]

Then I populate it:

buffer = open('sample.p12', 'rb').read()  # <class 'bytes'>
cdb = CRYPT_DATA_BLOB(len(buffer), ctypes.cast(buffer, ctypes.wintypes.LPBYTE))

With my lack of C/C++ usage (only in some cases with "okay" knowledge), I believe at this point I passed an unsigned long (DWORD) representing the size of the buffer in bytes (value is the same as os.stat(file).st_size) and a pointer to the data buffer.

The PFXImportCertStore returns a handle as an HCERTSTORE which is defined as a void pointer:

crypt32 = ctypes.WinDLL('crypt32.dll', use_last_error=True)
crypt32.PFXImportCertStore.restype = ctypes.c_void_p

Now I can call the function and store the handle:

hcertstore = crypt32.PFXImportCertStore(cdb, 'password', 0x00000001)

However, once run I receive the following:

>>> hcertstore
2187995849904
>>> ctypes.WinError(ctypes.get_last_error())
OSError(22, 'The operation completed successfully.', None, 0)

The operation completed, but I'm confident that handle value is invalid since passing it to other functions returns:

ctypes.ArgumentError: argument 1: <class 'OverflowError'>: int too long to convert

Am I perhaps using pointers incorrectly when creating the structure?

python
c++
c
windows
ctypes
asked on Stack Overflow Jun 23, 2020 by datta • edited Jun 23, 2020 by datta

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0