forked from guardtime/goksi
/
rfc3161.go
175 lines (158 loc) · 5.47 KB
/
rfc3161.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
/*
* 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 (
"time"
"github.com/vincentracine/goksi/errors"
"github.com/vincentracine/goksi/hash"
)
// AggregationTime returns aggregation chain aggregation time.
// If time is not present, an error is returned.
func (r *RFC3161) AggregationTime() (time.Time, error) {
if r == nil {
return time.Time{}, errors.New(errors.KsiInvalidArgumentError)
}
if r.aggrTime == nil {
return time.Time{}, errors.New(errors.KsiInvalidStateError).
AppendMessage("Inconsistent RFC3161 record.").
AppendMessage("Missing aggregation time.")
}
return time.Unix(int64(*r.aggrTime), 0), nil
}
// ChainIndex returns aggregation chain index.
// If chain index is not present, an error is returned.
func (r *RFC3161) ChainIndex() ([]uint64, error) {
if r == nil {
return nil, errors.New(errors.KsiInvalidArgumentError)
}
if r.chainIndex == nil {
return nil, errors.New(errors.KsiInvalidStateError).
AppendMessage("Inconsistent RFC3161 record.").
AppendMessage("Missing chain index.")
}
return *r.chainIndex, nil
}
// InputData returns input data.
// If data is not present, nil is returned.
func (r *RFC3161) InputData() ([]byte, error) {
if r == nil {
return nil, errors.New(errors.KsiInvalidArgumentError)
}
if r.inputData == nil {
return nil, nil
}
return *r.inputData, nil
}
// InputHash returns input hash.
// If hash is not present, an error is returned.
func (r *RFC3161) InputHash() (hash.Imprint, error) {
if r == nil {
return nil, errors.New(errors.KsiInvalidArgumentError)
}
if r.inputHash == nil {
return nil, errors.New(errors.KsiInvalidStateError).
AppendMessage("Inconsistent RFC3161 record.").
AppendMessage("Missing input hash.")
}
return *r.inputHash, nil
}
// TstInfoAlgo returns the hash function used to hash the TSTInfo structure.
// If hash is not present, an error is returned.
func (r *RFC3161) TstInfoAlgo() (hash.Algorithm, error) {
if r == nil {
return hash.SHA_NA, errors.New(errors.KsiInvalidArgumentError)
}
if r.tstInfoAlgo == nil {
return hash.SHA_NA, errors.New(errors.KsiInvalidStateError).
AppendMessage("Inconsistent RFC3161 record.").
AppendMessage("Missing TST info algorithm.")
}
return hash.Algorithm(*r.tstInfoAlgo), nil
}
// SigAttrAlgo returns the hash function used to hash the SignedAttributes structure.
// If hash is not present, an error is returned.
func (r *RFC3161) SigAttrAlgo() (hash.Algorithm, error) {
if r == nil {
return hash.SHA_NA, errors.New(errors.KsiInvalidArgumentError)
}
if r.sigAttrAlgo == nil {
return hash.SHA_NA, errors.New(errors.KsiInvalidStateError).
AppendMessage("Inconsistent RFC3161 record.").
AppendMessage("Missing signature attribute algorithm.")
}
return hash.Algorithm(*r.sigAttrAlgo), nil
}
// OutputHash calculates and returns the output hash of the RFC3161 record.
func (r *RFC3161) OutputHash(algorithm hash.Algorithm) (hash.Imprint, error) {
if r == nil {
return nil, errors.New(errors.KsiInvalidArgumentError)
}
if r.inputHash == nil ||
r.tstInfoAlgo == nil || r.tstInfoPrefix == nil || r.tstInfoSuffix == nil ||
r.sigAttrAlgo == nil || r.sigAttrPrefix == nil || r.sigAttrSuffix == nil {
return nil, errors.New(errors.KsiInvalidStateError).
AppendMessage("Inconsistent RFC3161 record.").
AppendMessage("Missing mandatory elements.")
}
tstInfoAlgo := hash.Algorithm(*r.tstInfoAlgo)
sigAttrAlgo := hash.Algorithm(*r.sigAttrAlgo)
if !tstInfoAlgo.Defined() || !sigAttrAlgo.Defined() {
return nil, errors.New(errors.KsiUnknownHashAlgorithm).
AppendMessage("RFC3161 record contains unknown hash algorithm.")
}
tstInfoHsh, err := preSufHasher(*r.tstInfoPrefix, *r.inputHash, *r.tstInfoSuffix, tstInfoAlgo)
if err != nil {
return nil, errors.KsiErr(err).AppendMessage("Failed to calculate TSTInfo digest.")
}
sigAttrHsh, err := preSufHasher(*r.sigAttrPrefix, tstInfoHsh, *r.sigAttrSuffix, sigAttrAlgo)
if err != nil {
return nil, errors.KsiErr(err).AppendMessage("Failed to calculate signed attributes digest.")
}
hsr, err := algorithm.New()
if err != nil {
return nil, err
}
if _, err := hsr.Write(sigAttrHsh); err != nil {
return nil, err
}
return hsr.Imprint()
}
func preSufHasher(prefix []byte, hsh hash.Imprint, suffix []byte, algorithm hash.Algorithm) (hash.Imprint, error) {
if !hsh.IsValid() || !algorithm.Defined() {
return nil, errors.New(errors.KsiInvalidArgumentError)
}
hsr, err := algorithm.New()
if err != nil {
return nil, err
}
if len(prefix) != 0 {
if _, err := hsr.Write(prefix); err != nil {
return nil, err
}
}
if _, err := hsr.Write(hsh.Digest()); err != nil {
return nil, err
}
if len(suffix) != 0 {
if _, err := hsr.Write(suffix); err != nil {
return nil, err
}
}
return hsr.Imprint()
}