-
Notifications
You must be signed in to change notification settings - Fork 7
/
json_encoding.go
273 lines (229 loc) · 6.72 KB
/
json_encoding.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
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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
package opaque
import (
"crypto/x509"
"encoding/json"
)
func (pmt ProtocolMessageType) String() string {
return ProtocolMessageTypeToStringMap[pmt]
}
// ProtocolMessageTypeToStringMap maps the Protocol Message Type to its string equivalent.
var ProtocolMessageTypeToStringMap = map[ProtocolMessageType]string{
ProtocolMessageTypeRegistrationRequest: "OPAQUE Registration Request",
ProtocolMessageTypeRegistrationResponse: "OPAQUE Registration Response",
ProtocolMessageTypeRegistrationUpload: "OPAQUE Registration Upload",
ProtocolMessageTypeCredentialRequest: "OPAQUE Credential Request",
ProtocolMessageTypeCredentialResponse: "OPAQUE Credential Response",
}
type registrationRequestJSON struct {
UserID []byte
OprfData []byte
}
// MarshalJSON encodes the RegistrationRequest.
func (rr *RegistrationRequest) MarshalJSON() ([]byte, error) {
rrJSON := ®istrationRequestJSON{
UserID: rr.UserID,
OprfData: rr.OprfData,
}
return json.Marshal(rrJSON)
}
// UnmarshalRegistrationRequestJSON decodes to a RegistrationRequest.
func UnmarshalRegistrationRequestJSON(b []byte) (*RegistrationRequest, error) {
h := &RegistrationRequest{}
err := json.Unmarshal(b, h)
if err != nil {
return nil, err
}
return h, nil
}
type registrationResponseJSON struct {
OprfData []byte
ServerPublicKey []byte
SecretTypes []byte
CleartextTypes []byte
}
// MarshalJSON encodes the RegistrationResponse.
func (rr *RegistrationResponse) MarshalJSON() ([]byte, error) {
rawPubKey, err := x509.MarshalPKIXPublicKey(rr.ServerPublicKey)
if err != nil {
return nil, err
}
var secTypes []byte
for _, s := range rr.CredentialEncodingPolicy.SecretTypes {
secTypes = append(secTypes, byte(s))
}
var clearTypes []byte
for _, cl := range rr.CredentialEncodingPolicy.CleartextTypes {
clearTypes = append(clearTypes, byte(cl))
}
rrJSON := ®istrationResponseJSON{
OprfData: rr.OprfData,
ServerPublicKey: rawPubKey,
SecretTypes: secTypes,
CleartextTypes: clearTypes,
}
return json.Marshal(rrJSON)
}
// UnmarshalRegistrationResponseJSON decodes to a RegistrationResponse.
func UnmarshalRegistrationResponseJSON(b []byte) (*RegistrationResponse, error) {
rrJSON := ®istrationResponseJSON{}
err := json.Unmarshal(b, rrJSON)
if err != nil {
return nil, err
}
pubKey, err := x509.ParsePKIXPublicKey(rrJSON.ServerPublicKey)
if err != nil {
return nil, err
}
var secTypes []CredentialType
for _, s := range rrJSON.SecretTypes {
secTypes = append(secTypes, CredentialType(s))
}
var clearTypes []CredentialType
for _, cl := range rrJSON.CleartextTypes {
clearTypes = append(clearTypes, CredentialType(cl))
}
cred := &CredentialEncodingPolicy{
SecretTypes: secTypes,
CleartextTypes: clearTypes,
}
r := &RegistrationResponse{
OprfData: rrJSON.OprfData,
ServerPublicKey: pubKey,
CredentialEncodingPolicy: cred,
}
return r, nil
}
type registrationUploadJSON struct {
UserPublicKey []byte
Nonce []byte
EncryptedCreds []byte
AuthenticatedCreds []byte
AuthTag []byte
}
// MarshalJSON encodes the RegistrationUpload.
func (rr *RegistrationUpload) MarshalJSON() ([]byte, error) {
rawPubKey, err := x509.MarshalPKIXPublicKey(rr.ClientPublicKey)
if err != nil {
return nil, err
}
rrJSON := ®istrationUploadJSON{
UserPublicKey: rawPubKey,
Nonce: rr.Envelope.Nonce,
EncryptedCreds: rr.Envelope.EncryptedCreds,
AuthenticatedCreds: rr.Envelope.AuthenticatedCreds,
AuthTag: rr.Envelope.AuthTag,
}
return json.Marshal(rrJSON)
}
// UnmarshalRegistrationUploadJSON decodes to a RegistrationRequest.
func UnmarshalRegistrationUploadJSON(b []byte) (*RegistrationUpload, error) {
rrJSON := ®istrationUploadJSON{}
err := json.Unmarshal(b, rrJSON)
if err != nil {
return nil, err
}
pubKey, err := x509.ParsePKIXPublicKey(rrJSON.UserPublicKey)
if err != nil {
return nil, err
}
env := &Envelope{
Nonce: rrJSON.Nonce,
EncryptedCreds: rrJSON.EncryptedCreds,
AuthenticatedCreds: rrJSON.AuthenticatedCreds,
AuthTag: rrJSON.AuthTag,
}
r := &RegistrationUpload{
Envelope: env,
ClientPublicKey: pubKey,
}
return r, nil
}
type credentialRequestJSON struct {
UserID []byte
OprfData []byte
}
// MarshalJSON encodes the CredentialRequest.
func (cr *CredentialRequest) MarshalJSON() ([]byte, error) {
crJSON := &credentialRequestJSON{
UserID: cr.UserID,
OprfData: cr.OprfData,
}
return json.Marshal(crJSON)
}
// UnmarshalCredentialRequestJSON decodes to a CredentialRequest.
func UnmarshalCredentialRequestJSON(b []byte) (*CredentialRequest, error) {
c := &CredentialRequest{}
err := json.Unmarshal(b, c)
if err != nil {
return nil, err
}
return c, nil
}
type credentialResponseJSON struct {
OprfData []byte
Nonce []byte
EncryptedCreds []byte
AuthenticatedCreds []byte
AuthTag []byte
ServerPublicKey []byte
}
// MarshalJSON encodes the CredentialResponse.
func (cr *CredentialResponse) MarshalJSON() ([]byte, error) {
rawPubKey, err := x509.MarshalPKIXPublicKey(cr.serverPublicKey)
if err != nil {
return nil, err
}
crJSON := &credentialResponseJSON{
OprfData: cr.OprfData,
Nonce: cr.Envelope.Nonce,
EncryptedCreds: cr.Envelope.EncryptedCreds,
AuthenticatedCreds: cr.Envelope.AuthenticatedCreds,
AuthTag: cr.Envelope.AuthTag,
ServerPublicKey: rawPubKey,
}
return json.Marshal(crJSON)
}
// UnmarshalCredentialResponseJSON decodes to a CredentialResponse.
func UnmarshalCredentialResponseJSON(b []byte) (*CredentialResponse, error) {
crJSON := &credentialResponseJSON{}
err := json.Unmarshal(b, crJSON)
if err != nil {
return nil, err
}
pubKey, err := x509.ParsePKIXPublicKey(crJSON.ServerPublicKey)
if err != nil {
return nil, err
}
env := &Envelope{
Nonce: crJSON.Nonce,
EncryptedCreds: crJSON.EncryptedCreds,
AuthenticatedCreds: crJSON.AuthenticatedCreds,
AuthTag: crJSON.AuthTag,
}
cr := &CredentialResponse{
OprfData: crJSON.OprfData,
Envelope: env,
serverPublicKey: pubKey,
}
return cr, nil
}
// String returns the string equivalent of the Credential Type.
func (ct CredentialType) String() string {
switch ct {
case CredentialTypeUserPrivateKey:
return "User Private Key"
case CredentialTypeUserPublicKey:
return "User Public Key"
case CredentialTypeServerPublicKey:
return "Server Public Key"
case CredentialTypeUserIdentity:
return "User Identity"
case CredentialTypeServerIdentity:
return "Server Identity"
}
return "Unrecognized Credential Type"
}
// MarshalText encodes the Credential Type.
func (ct CredentialType) MarshalText() ([]byte, error) {
return []byte(ct.String()), nil
}