| 1 |
// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 |
// Licensed under the MIT License. See LICENSE in the project root for license information. |
| 3 |
|
| 4 |
//go:build windows |
| 5 |
// +build windows |
| 6 |
|
| 7 |
package accessor |
| 8 |
|
| 9 |
import ( |
| 10 |
"context" |
| 11 |
"errors" |
| 12 |
"os" |
| 13 |
"path/filepath" |
| 14 |
"sync" |
| 15 |
"unsafe" |
| 16 |
|
| 17 |
"golang.org/x/sys/windows" |
| 18 |
) |
| 19 |
|
| 20 |
// Storage stores data in a file encrypted by the Windows data protection API. |
| 21 |
type Storage struct { |
| 22 |
m *sync.RWMutex |
| 23 |
p string |
| 24 |
} |
| 25 |
|
| 26 |
// New is the constructor for Storage. "p" is the path to the file in which to store data. |
| 27 |
func New(p string) (*Storage, error) { |
| 28 |
return &Storage{m: &sync.RWMutex{}, p: p}, nil |
| 29 |
} |
| 30 |
|
| 31 |
// Delete deletes the file, if it exists. |
| 32 |
func (s *Storage) Delete(context.Context) error { |
| 33 |
s.m.Lock() |
| 34 |
defer s.m.Unlock() |
| 35 |
err := os.Remove(s.p) |
| 36 |
if errors.Is(err, os.ErrNotExist) { |
| 37 |
return nil |
| 38 |
} |
| 39 |
return err |
| 40 |
} |
| 41 |
|
| 42 |
// Read returns data from the file. If the file doesn't exist, Read returns a nil slice and error. |
| 43 |
func (s *Storage) Read(context.Context) ([]byte, error) { |
| 44 |
s.m.RLock() |
| 45 |
defer s.m.RUnlock() |
| 46 |
|
| 47 |
data, err := os.ReadFile(s.p) |
| 48 |
if errors.Is(err, os.ErrNotExist) { |
| 49 |
return nil, nil |
| 50 |
} |
| 51 |
if err != nil { |
| 52 |
return nil, err |
| 53 |
} |
| 54 |
if len(data) > 0 { |
| 55 |
data, err = dpapi(decrypt, data) |
| 56 |
} |
| 57 |
return data, err |
| 58 |
} |
| 59 |
|
| 60 |
// Write stores data in the file, creating the file if it doesn't exist. |
| 61 |
func (s *Storage) Write(_ context.Context, data []byte) error { |
| 62 |
s.m.Lock() |
| 63 |
defer s.m.Unlock() |
| 64 |
|
| 65 |
data, err := dpapi(encrypt, data) |
| 66 |
if err != nil { |
| 67 |
return err |
| 68 |
} |
| 69 |
err = os.WriteFile(s.p, data, 0600) |
| 70 |
if errors.Is(err, os.ErrNotExist) { |
| 71 |
dir := filepath.Dir(s.p) |
| 72 |
if err = os.MkdirAll(dir, 0700); err == nil { |
| 73 |
err = os.WriteFile(s.p, data, 0600) |
| 74 |
} |
| 75 |
} |
| 76 |
return err |
| 77 |
} |
| 78 |
|
| 79 |
type operation int |
| 80 |
|
| 81 |
const ( |
| 82 |
decrypt operation = iota |
| 83 |
encrypt |
| 84 |
) |
| 85 |
|
| 86 |
func dpapi(op operation, data []byte) (result []byte, err error) { |
| 87 |
out := windows.DataBlob{} |
| 88 |
defer func() { |
| 89 |
if out.Data != nil { |
| 90 |
_, e := windows.LocalFree(windows.Handle(unsafe.Pointer(out.Data))) |
| 91 |
// prefer returning DPAPI errors because they're more interesting than LocalFree errors |
| 92 |
if e != nil && err == nil { |
| 93 |
err = e |
| 94 |
} |
| 95 |
} |
| 96 |
}() |
| 97 |
in := windows.DataBlob{Data: &data[0], Size: uint32(len(data))} |
| 98 |
switch op { |
| 99 |
case decrypt: |
| 100 |
// https://learn.microsoft.com/windows/win32/api/dpapi/nf-dpapi-cryptunprotectdata |
| 101 |
err = windows.CryptUnprotectData(&in, nil, nil, 0, nil, windows.CRYPTPROTECT_UI_FORBIDDEN, &out) |
| 102 |
case encrypt: |
| 103 |
// https://learn.microsoft.com/windows/win32/api/dpapi/nf-dpapi-cryptprotectdata |
| 104 |
err = windows.CryptProtectData(&in, nil, nil, 0, nil, windows.CRYPTPROTECT_UI_FORBIDDEN, &out) |
| 105 |
default: |
| 106 |
err = errors.New("invalid operation") |
| 107 |
} |
| 108 |
if err == nil { |
| 109 |
result = make([]byte, out.Size) |
| 110 |
copy(result, unsafe.Slice(out.Data, out.Size)) |
| 111 |
} |
| 112 |
return result, err |
| 113 |
} |
| 114 |
|
| 115 |
var _ Accessor = (*Storage)(nil) |