Microsoft ICertAdmin2 Interface from Python ctypes

2019-08-07 09:29发布

I'm trying to use the Microsofts ICertAdmin2 interface from Python using ctypes.

http://msdn.microsoft.com/en-us/library/windows/desktop/aa383234(v=vs.85).aspx

So far I can load the Certadm.dll. e.g.

import ctypes
# Load DLL into memory.
dll = ctypes.WinDLL ("c:\\windows\\system32\\Certadm.dll")

However, I'm not sure what I now need to do to access the methods from within ICertAdmin2 e.g. ICertAdmin2::ImportKey.

Any guidance would be really appreciated.

UPDATE

Now using the Python 'comtypes' library. This is what I'm trying.

>>import comtypes
>>certadmin = CreateObject("CertificateAuthority.Admin") 
>>certadmin._ICertAdmin__com_DenyRequest('WIN-3CF41NBPT85\cjndem-CA', 42)

Traceback (most recent call last):
File "<input>", line 1, in <module>
COMError: (-2147024809, 'The parameter is incorrect.', (u'CCertAdmin::DenyRequest: The         parameter is incorrect. 0x80070057 (WIN32: 87)', u'CertificateAuthority.Admin', None, 0, None))

UPDATE 3

This seems to work now:

>>import comtypes
>>from comtypes.client import CreateObject
>>out = comtypes.BSTR('')
>>certadmin = CreateObject("CertificateAuthority.Admin")
>>certadmin._ICertAdmin2__com_GetArchivedKey('ca1\\simpleca', 51, 1 , out)
>>print out

BSTR(u'MIIiHwYJKoZIhvcNAQcCoIIiEDC...........

However this:

>>import comtypes
>>from comtypes.client import CreateObject
>>out = comtypes.c_long()
>>request = CreateObject("CertificateAuthority.Request")
>>request._ICertRequest2__com_GetIssuedCertificate('ca1\\simpleca', 1, u'-1', out )
>>print out

COMError: (-2147024809, 'The parameter is incorrect.', (u'CCertRequest::GetIssuedCertificate: The parameter is incorrect. 0x80070057 (WIN32: 87)', u'CertificateAuthority.Request', None, 0, None))

1条回答
老娘就宠你
2楼-- · 2019-08-07 10:10

The last parameter of GetIssuedCertificate is a pointer to a LONG. Use byref() to pass it. Also you should pass a BSTR as first and third arg.

out = request.GetIssuedCertificate( 'ca1\\simpleca' ), 1, None )
查看更多
登录 后发表回答