-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathnative_runtime_fields.go
More file actions
259 lines (242 loc) · 8.5 KB
/
Copy pathnative_runtime_fields.go
File metadata and controls
259 lines (242 loc) · 8.5 KB
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
package main
import (
"bytes"
"context"
"crypto/rsa"
"crypto/x509"
"encoding/base64"
"encoding/json"
"encoding/pem"
"errors"
"fmt"
"io"
"math/big"
"unicode/utf8"
)
const runtimePublicKeyPEM = `-----BEGIN PUBLIC KEY-----
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDA8iMH5c02LilrsERw9t6Pv5Nc
4k6Pz1EaDicBMpdpxKduSZu5OANqUq8er4GM95omAGIOPOh+Nx0spthYA2BqGz+l
6HRkPJ7S236FZz73In/KVuLnwI8JJ2CbuJap8kvheCCZpmAWpb/cPx/3Vr/J6I17
XcW+ML9FoCI6AOvOzwIDAQAB
-----END PUBLIC KEY-----
`
type runtimeFieldOutputWire struct {
EncryptUserInfo string `json:"encrypt_user_info"`
Key string `json:"key"`
}
type nativeRuntimeFieldGenerator struct {
host protocolHostDeps
}
var _ runtimeFieldGenerator = (*nativeRuntimeFieldGenerator)(nil)
func (g *nativeRuntimeFieldGenerator) Generate(ctx context.Context, input runtimeFieldInput) (runtimeFieldOutput, error) {
if ctx == nil {
ctx = context.Background()
}
if err := ctx.Err(); err != nil {
return runtimeFieldOutput{}, err
}
encodedInput, err := json.Marshal(input)
if err != nil {
return runtimeFieldOutput{}, newProtocolError(
protocolInvalidInput,
"runtime fields input is invalid",
errors.New("marshal typed runtime fields input"),
)
}
var host protocolHostDeps
if g != nil {
host = g.host
}
encodedOutput, err := generateNativeRuntimeFieldsRaw(ctx, host, encodedInput)
if err != nil {
return runtimeFieldOutput{}, err
}
return decodeRuntimeFieldOutputJSON(encodedOutput)
}
func generateNativeRuntimeFieldsRaw(ctx context.Context, host protocolHostDeps, raw []byte) ([]byte, error) {
if ctx == nil {
ctx = context.Background()
}
if err := ctx.Err(); err != nil {
return nil, err
}
if !utf8.Valid(raw) {
return nil, newProtocolError(
protocolInvalidInput,
"runtime fields input is invalid",
errors.New("runtime fields raw input is not valid UTF-8"),
)
}
operationHost := protocolHostDepsFor(ctx, host)
if operationHost.Entropy == nil {
return nil, runtimeFieldsBackendIncompatible(errors.New("runtime fields entropy dependency is missing"))
}
var random [16]byte
if err := operationHost.Entropy.Read(random[:]); err != nil {
return nil, normalizeHostDependencyError(
err,
protocolEntropyFailure,
"Secure protocol randomness is unavailable",
"read runtime UUID entropy",
)
}
uuid := reverseMaskUUID(random)
key := []byte(runtimeASCIIKey(uuid))
ciphertext, err := aesCBCEncryptPKCS7(raw, key, key)
if err != nil {
return nil, runtimeFieldsBackendFailure(fmt.Errorf("encrypt runtime user info: %w", err))
}
publicKey, err := runtimePublicKey()
if err != nil {
return nil, err
}
encryptedKey, err := rsaEncryptPKCS1v15Exact(publicKey, key, operationHost.Entropy)
if err != nil {
return nil, err
}
encoded, err := json.Marshal(runtimeFieldOutputWire{
EncryptUserInfo: base64.StdEncoding.EncodeToString(ciphertext),
Key: base64.StdEncoding.EncodeToString(encryptedKey),
})
if err != nil {
return nil, runtimeFieldsBackendFailure(errors.New("marshal runtime fields output"))
}
return encoded, nil
}
func runtimePublicKey() (*rsa.PublicKey, error) {
block, rest := pem.Decode([]byte(runtimePublicKeyPEM))
if block == nil || block.Type != "PUBLIC KEY" || len(rest) != 0 {
return nil, runtimeFieldsBackendIncompatible(errors.New("pinned runtime public key PEM is invalid"))
}
parsed, err := x509.ParsePKIXPublicKey(block.Bytes)
if err != nil {
return nil, runtimeFieldsBackendIncompatible(fmt.Errorf("parse pinned runtime public key: %w", err))
}
publicKey, ok := parsed.(*rsa.PublicKey)
if !ok {
return nil, runtimeFieldsBackendIncompatible(fmt.Errorf("pinned runtime public key type %T is not RSA", parsed))
}
return publicKey, nil
}
func rsaEncryptPKCS1v15Exact(publicKey *rsa.PublicKey, message []byte, entropy protocolEntropy) ([]byte, error) {
if publicKey == nil || publicKey.N == nil || publicKey.N.Sign() <= 0 || publicKey.E <= 1 {
return nil, runtimeFieldsBackendIncompatible(errors.New("runtime RSA public key is invalid"))
}
k := publicKey.Size()
if k < 11 || len(message) > k-11 {
return nil, newProtocolError(
protocolInvalidInput,
"runtime fields input is invalid",
fmt.Errorf("runtime RSA message byte length %d exceeds maximum %d", len(message), k-11),
)
}
if entropy == nil {
return nil, runtimeFieldsBackendIncompatible(errors.New("runtime RSA entropy dependency is missing"))
}
psLength := k - len(message) - 3
encodedMessage := make([]byte, k)
encodedMessage[1] = 2
padding := encodedMessage[2 : 2+psLength]
if err := entropy.Read(padding); err != nil {
return nil, normalizeHostDependencyError(
err,
protocolEntropyFailure,
"Secure protocol randomness is unavailable",
"read runtime RSA padding entropy",
)
}
for i := range padding {
for padding[i] == 0 {
if err := entropy.Read(padding[i : i+1]); err != nil {
return nil, normalizeHostDependencyError(
err,
protocolEntropyFailure,
"Secure protocol randomness is unavailable",
"redraw runtime RSA padding entropy",
)
}
}
}
encodedMessage[2+psLength] = 0
copy(encodedMessage[3+psLength:], message)
messageInteger := new(big.Int).SetBytes(encodedMessage)
if messageInteger.Cmp(publicKey.N) >= 0 {
return nil, runtimeFieldsBackendIncompatible(errors.New("runtime RSA encoded message is not below the modulus"))
}
messageInteger.Exp(messageInteger, big.NewInt(int64(publicKey.E)), publicKey.N)
messageInteger.FillBytes(encodedMessage)
return encodedMessage, nil
}
func decodeRuntimeFieldOutputJSON(raw []byte) (runtimeFieldOutput, error) {
if !utf8.Valid(raw) {
return runtimeFieldOutput{}, runtimeFieldsOutputFailure("runtime fields output is not valid UTF-8")
}
decoder := json.NewDecoder(bytes.NewReader(raw))
first, err := decoder.Token()
if err != nil {
return runtimeFieldOutput{}, runtimeFieldsOutputFailure("runtime fields output is malformed JSON")
}
opening, ok := first.(json.Delim)
if !ok || opening != '{' {
return runtimeFieldOutput{}, runtimeFieldsOutputFailure("runtime fields output is not an object")
}
var output runtimeFieldOutput
seenEncryptUserInfo := false
seenKey := false
for decoder.More() {
fieldToken, err := decoder.Token()
if err != nil {
return runtimeFieldOutput{}, runtimeFieldsOutputFailure("runtime fields output has a malformed field")
}
field, ok := fieldToken.(string)
if !ok {
return runtimeFieldOutput{}, runtimeFieldsOutputFailure("runtime fields output has a non-string field name")
}
switch field {
case "encrypt_user_info":
if seenEncryptUserInfo {
return runtimeFieldOutput{}, runtimeFieldsOutputFailure("runtime fields output has a duplicate field")
}
seenEncryptUserInfo = true
if err := decoder.Decode(&output.EncryptUserInfo); err != nil {
return runtimeFieldOutput{}, runtimeFieldsOutputFailure("runtime fields output has a non-string field value")
}
case "key":
if seenKey {
return runtimeFieldOutput{}, runtimeFieldsOutputFailure("runtime fields output has a duplicate field")
}
seenKey = true
if err := decoder.Decode(&output.Key); err != nil {
return runtimeFieldOutput{}, runtimeFieldsOutputFailure("runtime fields output has a non-string field value")
}
default:
return runtimeFieldOutput{}, runtimeFieldsOutputFailure("runtime fields output has an unknown field")
}
}
closing, err := decoder.Token()
if err != nil {
return runtimeFieldOutput{}, runtimeFieldsOutputFailure("runtime fields output object is incomplete")
}
if delimiter, ok := closing.(json.Delim); !ok || delimiter != '}' {
return runtimeFieldOutput{}, runtimeFieldsOutputFailure("runtime fields output object is malformed")
}
if _, err := decoder.Token(); !errors.Is(err, io.EOF) {
return runtimeFieldOutput{}, runtimeFieldsOutputFailure("runtime fields output has trailing data")
}
if !seenEncryptUserInfo || !seenKey {
return runtimeFieldOutput{}, runtimeFieldsOutputFailure("runtime fields output is missing a required field")
}
if output.EncryptUserInfo == "" || output.Key == "" {
return runtimeFieldOutput{}, runtimeFieldsOutputFailure("runtime fields output has an empty field")
}
return output, nil
}
func runtimeFieldsOutputFailure(category string) error {
return newProtocolError(protocolBackendFailure, "Qoder protocol operation failed", errors.New(category))
}
func runtimeFieldsBackendIncompatible(internal error) error {
return newProtocolError(protocolBackendIncompatible, "runtime fields backend is incompatible", internal)
}
func runtimeFieldsBackendFailure(internal error) error {
return newProtocolError(protocolBackendFailure, "runtime fields operation failed", internal)
}