43 lines
1.3 KiB
Go
43 lines
1.3 KiB
Go
//go:build windows
|
|
|
|
package plugin
|
|
|
|
import (
|
|
"errors"
|
|
"unsafe"
|
|
|
|
"golang.org/x/sys/windows"
|
|
)
|
|
|
|
func protectSecretBytes(plain []byte) ([]byte, error) {
|
|
input := windows.DataBlob{Size: uint32(len(plain))}
|
|
if len(plain) > 0 {
|
|
input.Data = &plain[0]
|
|
}
|
|
var output windows.DataBlob
|
|
if err := windows.CryptProtectData(&input, nil, nil, 0, nil, windows.CRYPTPROTECT_UI_FORBIDDEN, &output); err != nil {
|
|
return nil, err
|
|
}
|
|
defer windows.LocalFree(windows.Handle(unsafe.Pointer(output.Data)))
|
|
if output.Size == 0 || output.Data == nil {
|
|
return nil, errors.New("DPAPI returned an empty protected value")
|
|
}
|
|
return append([]byte(nil), unsafe.Slice(output.Data, output.Size)...), nil
|
|
}
|
|
|
|
func unprotectSecretBytes(ciphertext []byte) ([]byte, error) {
|
|
input := windows.DataBlob{Size: uint32(len(ciphertext))}
|
|
if len(ciphertext) > 0 {
|
|
input.Data = &ciphertext[0]
|
|
}
|
|
var output windows.DataBlob
|
|
if err := windows.CryptUnprotectData(&input, nil, nil, 0, nil, windows.CRYPTPROTECT_UI_FORBIDDEN, &output); err != nil {
|
|
return nil, err
|
|
}
|
|
defer windows.LocalFree(windows.Handle(unsafe.Pointer(output.Data)))
|
|
if output.Size == 0 || output.Data == nil {
|
|
return nil, errors.New("DPAPI returned an empty plaintext value")
|
|
}
|
|
return append([]byte(nil), unsafe.Slice(output.Data, output.Size)...), nil
|
|
}
|