|
| 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 | + "math" |
| 13 | + "os" |
| 14 | + "path/filepath" |
| 15 | + "sync" |
| 16 | + "unsafe" |
| 17 | + |
| 18 | + "golang.org/x/sys/windows" |
| 19 | +) |
| 20 | + |
| 21 | +// Storage stores data in a file encrypted by the Windows data protection API. |
| 22 | +type Storage struct { |
| 23 | + m *sync.RWMutex |
| 24 | + p string |
| 25 | +} |
| 26 | + |
| 27 | +// New is the constructor for Storage. "p" is the path to the file in which to store data. |
| 28 | +func New(p string) (*Storage, error) { |
| 29 | + return &Storage{m: &sync.RWMutex{}, p: p}, nil |
| 30 | +} |
| 31 | + |
| 32 | +// Read returns data from the file. If the file doesn't exist, Read returns a nil slice and error. |
| 33 | +func (s *Storage) Read(context.Context) ([]byte, error) { |
| 34 | + s.m.RLock() |
| 35 | + defer s.m.RUnlock() |
| 36 | + |
| 37 | + data, err := os.ReadFile(s.p) |
| 38 | + if errors.Is(err, os.ErrNotExist) { |
| 39 | + return nil, nil |
| 40 | + } |
| 41 | + if err != nil { |
| 42 | + return nil, err |
| 43 | + } |
| 44 | + if len(data) > 0 { |
| 45 | + data, err = dpapi(decrypt, data) |
| 46 | + } |
| 47 | + return data, err |
| 48 | +} |
| 49 | + |
| 50 | +// Write stores data in the file, creating the file if it doesn't exist. |
| 51 | +func (s *Storage) Write(ctx context.Context, data []byte) error { |
| 52 | + s.m.Lock() |
| 53 | + defer s.m.Unlock() |
| 54 | + |
| 55 | + data, err := dpapi(encrypt, data) |
| 56 | + if err != nil { |
| 57 | + return err |
| 58 | + } |
| 59 | + err = os.WriteFile(s.p, data, 0600) |
| 60 | + if errors.Is(err, os.ErrNotExist) { |
| 61 | + dir := filepath.Dir(s.p) |
| 62 | + if err = os.MkdirAll(dir, 0700); err == nil { |
| 63 | + err = os.WriteFile(s.p, data, 0600) |
| 64 | + } |
| 65 | + } |
| 66 | + return err |
| 67 | +} |
| 68 | + |
| 69 | +type operation int |
| 70 | + |
| 71 | +const ( |
| 72 | + decrypt operation = iota |
| 73 | + encrypt |
| 74 | +) |
| 75 | + |
| 76 | +func dpapi(op operation, data []byte) (result []byte, err error) { |
| 77 | + out := windows.DataBlob{} |
| 78 | + defer func() { |
| 79 | + if out.Data != nil { |
| 80 | + _, e := windows.LocalFree(windows.Handle(unsafe.Pointer(out.Data))) |
| 81 | + // prefer returning DPAPI errors because they're more interesting than LocalFree errors |
| 82 | + if e != nil && err == nil { |
| 83 | + err = e |
| 84 | + } |
| 85 | + } |
| 86 | + }() |
| 87 | + in := windows.DataBlob{Data: &data[0], Size: uint32(len(data))} |
| 88 | + switch op { |
| 89 | + case decrypt: |
| 90 | + // https://learn.microsoft.com/windows/win32/api/dpapi/nf-dpapi-cryptunprotectdata |
| 91 | + err = windows.CryptUnprotectData(&in, nil, nil, 0, nil, 1, &out) |
| 92 | + case encrypt: |
| 93 | + // https://learn.microsoft.com/windows/win32/api/dpapi/nf-dpapi-cryptprotectdata |
| 94 | + err = windows.CryptProtectData(&in, nil, nil, 0, nil, 1, &out) |
| 95 | + default: |
| 96 | + err = errors.New("invalid operation") |
| 97 | + } |
| 98 | + if err == nil { |
| 99 | + // cast out.Data to a pointer to an arbitrarily long array, then slice the array and copy out.Size bytes from the |
| 100 | + // slice to result. This avoids allocating memory for a throwaway buffer but imposes a max size on the data because |
| 101 | + // the fictive array backing the slice can't be larger than the address space or the maximum value of an int. Those |
| 102 | + // values vary by platform, so the array size here is a compromise for 32-bit systems and allows ~2 GB of data. |
| 103 | + result = make([]byte, out.Size) |
| 104 | + source := (*[math.MaxInt32 - 1]byte)(unsafe.Pointer(out.Data))[:] |
| 105 | + copy(result, source) |
| 106 | + } |
| 107 | + return result, err |
| 108 | +} |
0 commit comments