forked from guardtime/goksi
/
aggr_req.go
316 lines (281 loc) · 9 KB
/
aggr_req.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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
/*
* Copyright 2020 Guardtime, Inc.
*
* This file is part of the Guardtime client SDK.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES, CONDITIONS, OR OTHER LICENSES OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
* "Guardtime" and "KSI" are trademarks or registered trademarks of
* Guardtime, Inc., and no license to trademarks is granted; Guardtime
* reserves and retains all trademark rights.
*/
package pdu
import (
"context"
"github.com/vincentracine/goksi/errors"
"github.com/vincentracine/goksi/hash"
"github.com/vincentracine/goksi/hmac"
"github.com/vincentracine/goksi/templates"
"github.com/vincentracine/goksi/tlv"
)
// AggregationReqSetting is a functional option setter for various aggregation request settings.
type AggregationReqSetting func(*aggregatorReq) error
type aggregatorReq struct {
obj AggregatorReq
}
// NewAggregationReq constructs a new aggregation request wrapped into the AggregatorReq container.
// Optionally additional configuration settings can be added via settings parameter.
func NewAggregationReq(requestHash hash.Imprint, settings ...AggregationReqSetting) (*AggregatorReq, error) {
if !requestHash.IsValid() {
return nil, errors.New(errors.KsiInvalidArgumentError)
}
tmp := aggregatorReq{obj: AggregatorReq{
aggrReq: &AggrReq{
id: newUint64(0),
hash: &requestHash,
},
}}
// Setup adjust settings with provided.
for _, setter := range settings {
if err := setter(&tmp); err != nil {
return nil, errors.KsiErr(err).AppendMessage("Unable to setup aggregation request.")
}
}
return &tmp.obj, nil
}
// AggrReqSetRequestLevel is aggregation requests' configuration method for setting input hash input level.
func AggrReqSetRequestLevel(level byte) AggregationReqSetting {
return func(r *aggregatorReq) error {
if r == nil {
return errors.New(errors.KsiInvalidArgumentError).AppendMessage("Missing aggregator request base object.")
}
if level > 0 {
r.obj.aggrReq.level = newUint64(uint64(level))
}
return nil
}
}
// AggrReqSetRequestID is aggregation requests' configuration method for setting request ID.
// Should be used with care.
func AggrReqSetRequestID(id uint64) AggregationReqSetting {
return func(r *aggregatorReq) error {
if r == nil {
return errors.New(errors.KsiInvalidArgumentError).AppendMessage("Missing aggregator request base object.")
}
*r.obj.aggrReq.id = id
return nil
}
}
// AggregationReq returns aggregation request component from the receiver container.
// In case the aggregation request is missing, nil is returned.
func (r *AggregatorReq) AggregationReq() (*AggrReq, error) {
if r == nil {
return nil, errors.New(errors.KsiInvalidArgumentError)
}
return r.aggrReq, nil
}
// RequestHash returns aggregation request document hash.
// In case the request hash is missing, an error is returned.
func (r *AggrReq) RequestHash() (hash.Imprint, error) {
if r == nil {
return nil, errors.New(errors.KsiInvalidArgumentError)
}
if r.hash == nil {
return nil, errors.New(errors.KsiInvalidStateError).AppendMessage("Missing request hash.")
}
return *r.hash, nil
}
// RequestLevel returns aggregation request input hash level.
// In case the aggregation request is missing, 0 is returned.
func (r *AggrReq) RequestLevel() (byte, error) {
if r == nil {
return 0, errors.New(errors.KsiInvalidArgumentError)
}
if r.level == nil {
return 0, nil
}
if *r.level > 0xff {
return 0, errors.New(errors.KsiInvalidFormatError).AppendMessage("Aggregation level can't be larger than 0xff.")
}
return byte(*r.level), nil
}
// RequestID returns aggregation request ID.
func (r *AggrReq) RequestID() (uint64, error) {
if r == nil {
return 0, errors.New(errors.KsiInvalidArgumentError)
}
if r.id == nil {
return 0, errors.New(errors.KsiInvalidStateError).AppendMessage("Missing request ID.")
}
return *r.id, nil
}
// SetHeader is request header setter.
func (r *AggregatorReq) SetHeader(hdr *Header) error {
if r == nil {
return errors.New(errors.KsiInvalidArgumentError)
}
r.header = hdr
return nil
}
// Header returns aggregator request header.
func (r *AggregatorReq) Header() (*Header, error) {
if r == nil {
return nil, errors.New(errors.KsiInvalidArgumentError)
}
return r.header, nil
}
// HMAC returns the request message authentication code, or nil if not present.
func (r *AggregatorReq) HMAC() (hash.Imprint, error) {
if r == nil {
return nil, errors.New(errors.KsiInvalidArgumentError)
}
return *r.mac, nil
}
// UpdateHMAC updates the request HMAC. The MAC is computed over all PDU message bytes up to (but excluding)
// the hash value within the imprint in the MAC field:
// 1. the TLV header of the PDU element itself;
// 2. the complete header element (both the TLV header and the value of the element);
// 3. the complete payload elements in the order in which they appear in the PDU;
// 4. the TLV header of the MAC element;
// 5. the hash algorithm identifier part of the imprint representing the MAC value.
func (r *AggregatorReq) UpdateHMAC(alg hash.Algorithm, key string) error {
if r == nil {
return errors.New(errors.KsiInvalidArgumentError)
}
if r.header == nil {
return errors.New(errors.KsiInvalidFormatError).AppendMessage("Missing request header.")
}
if !alg.Registered() {
return errors.New(errors.KsiUnknownHashAlgorithm).
AppendMessage("Can not calculate HMAC using an unknown hash algorithm.")
}
if !alg.Trusted() {
return errors.New(errors.KsiInvalidStateError).AppendMessage("Algorithm algorithm is not trusted.")
}
// Initialize the HMAC with a null digest.
r.mac = newImprint(alg.ZeroImprint())
raw, err := r.Encode()
if err != nil {
return err
}
hsr, err := hmac.New(alg, []byte(key))
if err != nil {
return err
}
if _, err := hsr.Write(raw[:(len(raw) - alg.Size())]); err != nil {
return err
}
tmp, err := hsr.Imprint()
if err != nil {
return err
}
r.mac = &tmp
return nil
}
// UpdateRequestID updates aggregation request ID in case it is not set explicitly.
// Note that if the aggregator request does not contain aggregation request component, no operation is performed.
func (r *AggregatorReq) UpdateRequestID(id uint64) error {
if r == nil {
return errors.New(errors.KsiInvalidArgumentError)
}
// Update the request in case of aggregation request.
if r.aggrReq != nil {
if r.aggrReq.id == nil || *r.aggrReq.id == 0 {
r.aggrReq.id = &id
}
}
return nil
}
// Encode serializes the aggregator request into TLV binary representation.
func (r *AggregatorReq) Encode() ([]byte, error) {
if r == nil {
return nil, errors.New(errors.KsiInvalidArgumentError)
}
// Get template.
pduTemplate, err := templates.Get("AggregatorReq")
if err != nil {
return nil, err
}
// Get TLV from template.
rTlv, err := tlv.NewTlv(tlv.ConstructFromObject(r, pduTemplate))
if err != nil {
return nil, err
}
// log.Debug(rTlv)
return rTlv.Raw, nil
}
// Clone returns a deep copy of the origin, or nil in case of an error.
// Note that only request part of the AggregatorReq will be cloned, meaning header and HMAC are ignored.
func (r *AggregatorReq) Clone() (*AggregatorReq, error) {
if r == nil {
return nil, errors.New(errors.KsiInvalidArgumentError)
}
tmp := &AggregatorReq{}
if r.aggrReq != nil {
clone, err := clonePDU(r.aggrReq)
if err != nil {
return nil, err
}
tmp.aggrReq = clone.(*AggrReq)
}
if r.confReq != nil {
clone, err := clonePDU(r.confReq)
if err != nil {
return nil, err
}
tmp.confReq = clone.(*Config)
}
if r.aggrAckReq != nil {
clone, err := clonePDU(r.aggrAckReq)
if err != nil {
return nil, err
}
tmp.aggrAckReq = clone.(*AggrAck)
}
return tmp, nil
}
// NewAggregatorConfigReq constructs a new aggregator configuration request.
func NewAggregatorConfigReq() (*AggregatorReq, error) {
return &AggregatorReq{
// Initialize the conf request with empty instance.
confReq: &Config{},
}, nil
}
// Config returns request config instance.
// In case the configuration is not part of the request, nil is returned.
func (r *AggregatorReq) Config() (*Config, error) {
if r == nil {
return nil, errors.New(errors.KsiInvalidArgumentError)
}
return r.confReq, nil
}
// WithContext returns the original r with its context changed to ctx.
// In case of an error, nil is returned.
func (r *AggregatorReq) WithContext(ctx context.Context) *AggregatorReq {
if r == nil {
return nil
}
switch {
case ctx == nil:
r.ctx = context.Background()
default:
r.ctx = ctx
}
return r
}
// Context returns the request's context.
//
// The returned context is always non-nil, it defaults to the background context.
func (r *AggregatorReq) Context() context.Context {
if r.ctx != nil {
return r.ctx
}
return context.Background()
}