2 回答
![?](http://img1.sycdn.imooc.com/533e4c7b00013f3c02400205-100-100.jpg)
TA贡献1866条经验 获得超5个赞
这是预期的行为。
根据这个:https : //msdn.microsoft.com/en-us/library/windows/desktop/aa387313(v=pPFX
vs.85) .aspx,该结构需要一个预先分配的缓冲区,大小在cbData
字段中,它将随着复制的数据大小而更新。
如果调用pbData
等于NULL
,则仅cbData
更新字段以反映输出缓冲区所需的大小。
![?](http://img1.sycdn.imooc.com/545866c40001561502200220-100-100.jpg)
TA贡献1883条经验 获得超3个赞
JimB 的回答肯定是正确的,但我想添加这个以进行后续处理,以防其他人沿着这条路走下去。我必须用来获取 PFX 文件的实际代码CRYPTOAPI_BLOB是:
var (
crypt32 = syscall.NewLazyDLL("crypt32.dll")
procPFXExportCertStoreEx = crypt32.NewProc("PFXExportCertStoreEx")
procCryptMemAlloc = crypt32.NewProc("CryptMemAlloc")
procCryptMemFree = crypt32.NewProc("CryptMemFree")
)
type CRYPTOAPI_BLOB struct {
cbData uint32
pbData *byte
}
func (b *CRYPTOAPI_BLOB) ToByteArray() []byte {
d := make([]byte, b.cbData)
copy(d, (*[1 << 30]byte)(unsafe.Pointer(b.pbData))[:])
return d
}
func PfxExportCertStore(storeHandle syscall.Handle, password string, flags uint32) (returnData []byte, err error) {
var pfxBlob CRYPTOAPI_BLOB
r1, _, _ := syscall.Syscall6(procPFXExportCertStoreEx.Addr(), 5,
uintptr(storeHandle), //hStore
uintptr(unsafe.Pointer(&pfxBlob)), //*pPFX
uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(password))), //szPassword
0, //*pvPara
uintptr(flags), //dwFlags
0)
r2, _, _ := syscall.Syscall(procCryptMemAlloc.Addr(), 1, uintptr(unsafe.Pointer(&pfxBlob.cbData)), 0, 0)
p := unsafe.Pointer(&r2)
q := (*byte)(p)
pfxBlob.pbData = q
defer syscall.Syscall(procCryptMemFree.Addr(), 1, uintptr(unsafe.Pointer(pfxBlob.pbData)), 0, 0)
r3, _, _ := syscall.Syscall6(procPFXExportCertStoreEx.Addr(), 5,
uintptr(storeHandle), //hStore
uintptr(unsafe.Pointer(&pfxBlob)), //*pPFX
uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(password))), //szPassword
0, //*pvPara
uintptr(flags), //dwFlags
0)
returnData = pfxBlob.ToByteArray()
return
}
(我已经剥离了错误处理以使其更易于阅读)。第一次调用PFXExportCertStoreEx只返回大小,一旦我们有了大小,我们就可以调用来PFXExportCertStoreEx分配一个缓冲区,然后我们将相同的指针传递给PFXExportCertStoreEx,但这次它有分配的缓冲区,我们得到了完整的 PFX文件返回。
- 2 回答
- 0 关注
- 139 浏览
添加回答
举报