在Go中调用PFXExportCertStoreEx不会返回数据 [英] Calling PFXExportCertStoreEx in Go does not return data
问题描述
我在Windows上的Go 1.6中工作,并尝试将证书容器导出到PFX(此处的最终目标是从证书存储区访问可导出的私钥)。
我打开了一个内存存储并在商店中插入了一个证书:
var storedCertCtx * syscall.CertContext $ $ b storeHandle,err:= syscall.CertOpenStore(syscall.CERT_STORE_PROV_MEMORY,0,0,syscall.CERT_STORE_DEFER_CLOSE_UNTIL_LAST_FREE_FLAG,0)
err = syscall.CertAddCertificateContextToStore(storeHandle,certenum,syscall.CERT_STORE_ADD_ALWAYS,& storedCertCtx)
现在我想要生成该商店的PFX。我已经定义了一个包含的结构数据blob 并希望使用 PFXExportCertStoreEx 获得商店的PFX:
var(
pre>
crypt32 = syscall.NewLazyDLL (crypt32.dll)
procPFXExportCertStoreEx = crypt32.NewProc(PFXExportCertStoreEx)
)
类型CRYPTOAPI_BLOB结构{
DataSize uint32
Data *字节
}
var pfxBlob CRYPTOAPI_BLOB
err = PfxExportCertStore(storeHandle,& pfxBlob,syscall.StringToUTF16Ptr(MyPassword),0,0)
syscall.Syscall6(procPFXExportCertStoreEx.Addr(),5,
uintptr(storeHandle),// hStore
uintptr(unsafe.Pointer(& pfxBlob)),// * pPFX
uintptr(unsafe.Pointer(syscall.StringToUTF16 Ptr(password))),// szPassword
0,// * pvPara
0,// dwFlags
0)
并且这个 half 可以工作。
DataSize
填充了看起来像一个合适的值(即如果我向商店添加更多证书,它会变得更大),但是Data
始终是< nil>
。
由于它是用指针填充的,所以我尝试将它声明为
* uintptr
和uint32
(只是为了查看是否有任何东西被填充),但没有。该值始终未变动(如果手动将垃圾数据放在那里,垃圾数据会在系统调用执行后停留)。
我是否错误地定义了结构?在Go中有很少的例子可以完成这个工作,但是从我从许多C例子中可以看到,这个应该可以工作。
解决方案这是预期的行为。
据此: https://msdn.microsoft.com/en-us/library/windows/desktop/ aa387313(v = vs.85).aspx ,
pPFX
结构需要一个预先分配的缓冲区,大小在cbData
字段,它将用复制数据的大小进行更新。
如果调用是使用
pbData
等于NULL
,只更新cbData
字段以反映输出缓冲区。I'm working in Go 1.6 on Windows and trying to export a certificate container to a PFX (the ultimate goal here is to access an exportable private key from the certificate store).
I have opened a memory store and inserted a certificate into the store:
var storedCertCtx *syscall.CertContext storeHandle, err := syscall.CertOpenStore(syscall.CERT_STORE_PROV_MEMORY, 0, 0, syscall.CERT_STORE_DEFER_CLOSE_UNTIL_LAST_FREE_FLAG, 0) err = syscall.CertAddCertificateContextToStore(storeHandle, certenum, syscall.CERT_STORE_ADD_ALWAYS, &storedCertCtx)
Now I want to generate a PFX of that store. I have defined a struct for containing the data blob and want to use PFXExportCertStoreEx to get a PFX of the store:
var ( crypt32 = syscall.NewLazyDLL("crypt32.dll") procPFXExportCertStoreEx = crypt32.NewProc("PFXExportCertStoreEx") ) type CRYPTOAPI_BLOB struct { DataSize uint32 Data *byte } var pfxBlob CRYPTOAPI_BLOB err = PfxExportCertStore(storeHandle, &pfxBlob, syscall.StringToUTF16Ptr("MyPassword"), 0, 0) syscall.Syscall6(procPFXExportCertStoreEx.Addr(), 5, uintptr(storeHandle), //hStore uintptr(unsafe.Pointer(&pfxBlob)), //*pPFX uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr("password"))), //szPassword 0, //*pvPara 0, //dwFlags 0)
And this half works.
DataSize
is populated with what looks like an appropriate value (i.e. if I add more certificates to the store, it grows bigger), howeverData
is always<nil>
.Seeing as it's meant to be populated with a pointer, I have tried declaring it as
*uintptr
anduint32
(just to see if anything gets populated), but nothing. The value is always untouched (if I manually put junk data in there, the junk data stays after the syscall is executed).Have I defined the struct incorrectly? There is precious few examples to go for getting this done in Go, but from what I can see from the numerous C examples, this should be working.
解决方案This is the expected behavior.
According to this: https://msdn.microsoft.com/en-us/library/windows/desktop/aa387313(v=vs.85).aspx, the
pPFX
struct requires a pre-allocated buffer, with the size in thecbData
field, which will be updated with the size of the data copied in.If the call is made with
pbData
equal toNULL
, only thecbData
field is updated to reflect the size needed for the output buffer.这篇关于在Go中调用PFXExportCertStoreEx不会返回数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!