forked from opentdf/platform
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nanotdf.go
225 lines (187 loc) · 5.91 KB
/
nanotdf.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
//nolint:gomnd // nanotdf magics and lengths are inlined for clarity
package sdk
import (
"encoding/binary"
"errors"
"io"
"github.com/arkavo-org/opentdf-platform/lib/ocrypto"
)
const (
ErrNanoTdfRead = Error("nanotdf read error")
)
type nanoTdf struct {
magicNumber [3]byte
kasUrl *resourceLocator
binding *bindingCfg
sigCfg *signatureConfig
policy *policyInfo
EphemeralPublicKey *eccKey
}
type resourceLocator struct {
protocol urlProtocol
lengthBody uint8
body string
}
func (resourceLocator) isPolicyBody() {}
type bindingCfg struct {
useEcdsaBinding bool
padding uint8
bindingBody ocrypto.ECCMode
}
type signatureConfig struct {
hasSignature bool
signatureMode ocrypto.ECCMode
cipher cipherMode
}
type PolicyBody interface {
isPolicyBody() // marker method to ensure interface implementation
}
type policyInfo struct {
mode uint8
body PolicyBody
binding *eccSignature
}
type remotePolicy struct {
url *resourceLocator
}
func (remotePolicy) isPolicyBody() {}
type embeddedPolicy struct {
lengthBody uint16
body string
}
func (embeddedPolicy) isPolicyBody() {}
type eccSignature struct {
value []byte
}
type eccKey struct {
Key []byte
}
type urlProtocol uint8
const (
urlProtocolHttp urlProtocol = 0
urlProtocolHttps urlProtocol = 1
urlProtocolShared urlProtocol = 255
)
type cipherMode int
const (
cipherModeAes256gcm64Bit cipherMode = 0
cipherModeAes256gcm96Bit cipherMode = 1
cipherModeAes256gcm104Bit cipherMode = 2
cipherModeAes256gcm112Bit cipherMode = 3
cipherModeAes256gcm120Bit cipherMode = 4
cipherModeAes256gcm128Bit cipherMode = 5
)
type policyType uint8
const (
policyTypeRemotePolicy policyType = 0
policyTypEmbeddedPolicyPainText policyType = 1
policyTypeEmbeddedPolicyEncrypted policyType = 2
policyTypeEmbeddedPolicyEncryptedPolicyKeyAccess policyType = 3
)
func deserializeBindingCfg(b byte) *bindingCfg {
cfg := bindingCfg{}
cfg.useEcdsaBinding = (b >> 7 & 0x01) == 1
cfg.padding = 0
cfg.bindingBody = ocrypto.ECCMode((b >> 4) & 0x07)
return &cfg
}
func deserializeSignatureCfg(b byte) *signatureConfig {
cfg := signatureConfig{}
cfg.hasSignature = (b >> 7 & 0x01) == 1
cfg.signatureMode = ocrypto.ECCMode((b >> 4) & 0x07)
cfg.cipher = cipherMode(b & 0x0F)
return &cfg
}
func readPolicyBody(reader io.Reader, mode uint8) (PolicyBody, error) {
switch mode {
case 0:
var resourceLoc resourceLocator
if err := binary.Read(reader, binary.BigEndian, &resourceLoc.protocol); err != nil {
return nil, errors.Join(ErrNanoTdfRead, err)
}
if err := binary.Read(reader, binary.BigEndian, &resourceLoc.lengthBody); err != nil {
return nil, errors.Join(ErrNanoTdfRead, err)
}
body := make([]byte, resourceLoc.lengthBody)
if err := binary.Read(reader, binary.BigEndian, &body); err != nil {
return nil, errors.Join(ErrNanoTdfRead, err)
}
resourceLoc.body = string(body)
return remotePolicy{url: &resourceLoc}, nil
default:
var embedPolicy embeddedPolicy
if err := binary.Read(reader, binary.BigEndian, &embedPolicy.lengthBody); err != nil {
return nil, errors.Join(ErrNanoTdfRead, err)
}
body := make([]byte, embedPolicy.lengthBody)
if err := binary.Read(reader, binary.BigEndian, &body); err != nil {
return nil, errors.Join(ErrNanoTdfRead, err)
}
embedPolicy.body = string(body)
return embeddedPolicy(embedPolicy), nil
}
}
func readEphemeralPublicKey(reader io.Reader, curve ocrypto.ECCMode) (*eccKey, error) {
var numberOfBytes uint8
switch curve {
case ocrypto.ECCModeSecp256r1:
numberOfBytes = 33
case ocrypto.ECCModeSecp384r1:
numberOfBytes = 49
case ocrypto.ECCModeSecp521r1:
numberOfBytes = 67
}
buffer := make([]byte, numberOfBytes)
if err := binary.Read(reader, binary.BigEndian, &buffer); err != nil {
return nil, errors.Join(ErrNanoTdfRead, err)
}
return &eccKey{Key: buffer}, nil
}
func ReadNanoTDFHeader(reader io.Reader) (*nanoTdf, error) {
var nanoTDF nanoTdf
if err := binary.Read(reader, binary.BigEndian, &nanoTDF.magicNumber); err != nil {
return nil, errors.Join(ErrNanoTdfRead, err)
}
nanoTDF.kasUrl = &resourceLocator{}
if err := binary.Read(reader, binary.BigEndian, &nanoTDF.kasUrl.protocol); err != nil {
return nil, errors.Join(ErrNanoTdfRead, err)
}
if err := binary.Read(reader, binary.BigEndian, &nanoTDF.kasUrl.lengthBody); err != nil {
return nil, errors.Join(ErrNanoTdfRead, err)
}
body := make([]byte, nanoTDF.kasUrl.lengthBody)
if err := binary.Read(reader, binary.BigEndian, &body); err != nil {
return nil, errors.Join(ErrNanoTdfRead, err)
}
nanoTDF.kasUrl.body = string(body)
var bindingByte uint8
if err := binary.Read(reader, binary.BigEndian, &bindingByte); err != nil {
return nil, errors.Join(ErrNanoTdfRead, err)
}
nanoTDF.binding = deserializeBindingCfg(bindingByte)
var signatureByte uint8
if err := binary.Read(reader, binary.BigEndian, &signatureByte); err != nil {
return nil, errors.Join(ErrNanoTdfRead, err)
}
nanoTDF.sigCfg = deserializeSignatureCfg(signatureByte)
nanoTDF.policy = &policyInfo{}
if err := binary.Read(reader, binary.BigEndian, &nanoTDF.policy.mode); err != nil {
return nil, errors.Join(ErrNanoTdfRead, err)
}
policyBody, err := readPolicyBody(reader, nanoTDF.policy.mode)
if err != nil {
return nil, errors.Join(ErrNanoTdfRead, err)
}
nanoTDF.policy.body = policyBody
nanoTDF.policy.binding = &eccSignature{}
nanoTDF.policy.binding.value = make([]byte, 8)
if err := binary.Read(reader, binary.BigEndian, &nanoTDF.policy.binding.value); err != nil {
return nil, errors.Join(ErrNanoTdfRead, err)
}
nanoTDF.EphemeralPublicKey = &eccKey{}
if err := binary.Read(reader, binary.BigEndian, &nanoTDF.EphemeralPublicKey.Key); err != nil {
return nil, errors.Join(ErrNanoTdfRead, err)
}
nanoTDF.EphemeralPublicKey, err = readEphemeralPublicKey(reader, nanoTDF.binding.bindingBody)
return &nanoTDF, err
}