|
| 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 | +// Read returns data from the file. If the file doesn't exist, Read returns a nil slice and error. |
| 32 | +func (s *Storage) Read(context.Context) ([]byte, error) { |
| 33 | + s.m.RLock() |
| 34 | + defer s.m.RUnlock() |
| 35 | + |
| 36 | + data, err := os.ReadFile(s.p) |
| 37 | + if errors.Is(err, os.ErrNotExist) { |
| 38 | + return nil, nil |
| 39 | + } |
| 40 | + if err != nil { |
| 41 | + return nil, err |
| 42 | + } |
| 43 | + if len(data) > 0 { |
| 44 | + data, err = dpapi(decrypt, data) |
| 45 | + } |
| 46 | + return data, err |
| 47 | +} |
| 48 | + |
| 49 | +// Write stores data in the file, creating the file if it doesn't exist. |
| 50 | +func (s *Storage) Write(ctx context.Context, data []byte) error { |
| 51 | + s.m.Lock() |
| 52 | + defer s.m.Unlock() |
| 53 | + |
| 54 | + data, err := dpapi(encrypt, data) |
| 55 | + if err != nil { |
| 56 | + return err |
| 57 | + } |
| 58 | + err = os.WriteFile(s.p, data, 0600) |
| 59 | + if errors.Is(err, os.ErrNotExist) { |
| 60 | + dir := filepath.Dir(s.p) |
| 61 | + if err = os.MkdirAll(dir, 0700); err == nil { |
| 62 | + err = os.WriteFile(s.p, data, 0600) |
| 63 | + } |
| 64 | + } |
| 65 | + return err |
| 66 | +} |
| 67 | + |
| 68 | +type operation int |
| 69 | + |
| 70 | +const ( |
| 71 | + decrypt operation = iota |
| 72 | + encrypt |
| 73 | +) |
| 74 | + |
| 75 | +func dpapi(op operation, data []byte) (result []byte, err error) { |
| 76 | + out := windows.DataBlob{} |
| 77 | + defer func() { |
| 78 | + if out.Data != nil { |
| 79 | + _, e := windows.LocalFree(windows.Handle(unsafe.Pointer(out.Data))) |
| 80 | + // prefer returning DPAPI errors because they're more interesting than LocalFree errors |
| 81 | + if e != nil && err == nil { |
| 82 | + err = e |
| 83 | + } |
| 84 | + } |
| 85 | + }() |
| 86 | + in := windows.DataBlob{Data: &data[0], Size: uint32(len(data))} |
| 87 | + switch op { |
| 88 | + case decrypt: |
| 89 | + // https://learn.microsoft.com/windows/win32/api/dpapi/nf-dpapi-cryptunprotectdata |
| 90 | + err = windows.CryptUnprotectData(&in, nil, nil, 0, nil, windows.CRYPTPROTECT_UI_FORBIDDEN, &out) |
| 91 | + case encrypt: |
| 92 | + // https://learn.microsoft.com/windows/win32/api/dpapi/nf-dpapi-cryptprotectdata |
| 93 | + err = windows.CryptProtectData(&in, nil, nil, 0, nil, windows.CRYPTPROTECT_UI_FORBIDDEN, &out) |
| 94 | + default: |
| 95 | + err = errors.New("invalid operation") |
| 96 | + } |
| 97 | + if err == nil { |
| 98 | + result = make([]byte, out.Size) |
| 99 | + copy(result, unsafe.Slice(out.Data, out.Size)) |
| 100 | + } |
| 101 | + return result, err |
| 102 | +} |
0 commit comments