/
secure_string.go
162 lines (121 loc) · 3.68 KB
/
secure_string.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
//go:build !windows
// +build !windows
// Package secstr provides methods and structs for working with protected (secure) strings
package secstr
// ////////////////////////////////////////////////////////////////////////////////// //
// //
// Copyright (c) 2024 ESSENTIAL KAOS //
// Apache License, Version 2.0 <https://www.apache.org/licenses/LICENSE-2.0> //
// //
// ////////////////////////////////////////////////////////////////////////////////// //
import (
"fmt"
"runtime"
"golang.org/x/sys/unix"
)
// ////////////////////////////////////////////////////////////////////////////////// //
// String contains protected data
type String struct {
Data []byte
}
// ////////////////////////////////////////////////////////////////////////////////// //
// NewSecureString creates new secure string
func NewSecureString(data any) (*String, error) {
switch v := data.(type) {
case []byte:
return secureStringFromSlice(v)
case *string:
return secureStringFromStringPointer(v)
case string:
return secureStringFromString(v)
default:
return nil, fmt.Errorf("Unsupported data type for secure string: %t", data)
}
}
// ////////////////////////////////////////////////////////////////////////////////// //
// IsEmpty returns false if string is empty
func (s *String) IsEmpty() bool {
return s == nil || len(s.Data) == 0
}
// Destroy destroys data
func (s *String) Destroy() error {
if s == nil {
return nil
}
return destroySecureString(s)
}
// ////////////////////////////////////////////////////////////////////////////////// //
// secureStringFromSlice creates secure string from byte slice
func secureStringFromSlice(data []byte) (*String, error) {
var err error
s := &String{}
// Destroy source data
defer clearByteSlice(data)
s.Data, err = unix.Mmap(
-1, 0, len(data),
unix.PROT_READ|unix.PROT_WRITE, // Pages may be read and written
unix.MAP_ANON|unix.MAP_PRIVATE, // The mapping is not backed by any file + private copy-on-write
)
if err != nil {
return nil, err
}
// Lock memory with data
err = unix.Mlock(s.Data)
if err != nil {
unix.Munmap(s.Data)
return nil, err
}
// Copy data
for i := range data {
s.Data[i] = data[i]
}
// Protect memory region with data
err = unix.Mprotect(s.Data, unix.PROT_READ) // The memory can be read
if err != nil {
unix.Munmap(s.Data)
clearByteSlice(s.Data) // Destroy data if memory cannot be protected
return nil, err
}
// Set finalizer
runtime.SetFinalizer(s, destroySecureString)
return s, nil
}
// secureStringFromString creates secure string from string
func secureStringFromString(data string) (*String, error) {
return secureStringFromSlice([]byte(data))
}
// secureStringFromStringPointer creates secure string from string pointer
func secureStringFromStringPointer(data *string) (*String, error) {
s, err := secureStringFromSlice([]byte(*data))
// Clear source data
*data = ""
return s, err
}
// ////////////////////////////////////////////////////////////////////////////////// //
func destroySecureString(s *String) error {
if s.Data == nil {
return nil
}
err := unix.Mprotect(s.Data, unix.PROT_READ|unix.PROT_WRITE)
if err != nil {
return err
}
clearByteSlice(s.Data) // Clear data
// Unlock memory
err = unix.Munlock(s.Data)
if err != nil {
return err
}
err = unix.Munmap(s.Data)
if err != nil {
return err
}
s.Data = nil // Mark as nil for GC
return nil
}
// clearByteSlice clears byte slice
func clearByteSlice(s []byte) {
for i := range s {
s[i] = 0
}
}