-
Notifications
You must be signed in to change notification settings - Fork 5
/
signed-data.go
220 lines (192 loc) · 5.88 KB
/
signed-data.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
package libICP
import (
"crypto/rsa"
"time"
"github.com/OpenICP-BR/asn1"
)
type signed_data_raw struct {
RawContent asn1.RawContent
Version int
DigestAlgorithms []algorithm_identifier `asn1:"set"`
EncapContentInfo encapsulated_content_info
Certificates []certificate_choice `asn1:"tag:0,optional,set,omitempty"`
CRLs []revocation_info_choice `asn1:"tag:1,optional,omitempty"`
SignerInfos []signer_info_raw `asn1:"set"`
}
// Apply algorithm described on RFC5625 Section 5.1 Page 9. This function MUST be called before marshaling.
func (sd *signed_data_raw) set_appropriate_version() {
if sd.has_other_type_cert() || sd.has_other_type_crl() {
sd.Version = 5
} else {
if sd.has_v2_cert() {
sd.Version = 4
} else {
if sd.has_v1_cert() || sd.has_v3_SignerInfo() || !sd.EncapContentInfo.EContentType.Equal(idData) {
sd.Version = 3
} else {
sd.Version = 1
}
}
}
}
func (sd *signed_data_raw) has_other_type_crl() bool {
for _, crl := range sd.CRLs {
if !is_zero_of_underlying_type(crl.Other) {
return true
}
}
return false
}
func (sd *signed_data_raw) has_other_type_cert() bool {
for _, cert := range sd.Certificates {
if !is_zero_of_underlying_type(cert.Other) {
return true
}
}
return false
}
func (sd *signed_data_raw) has_v1_cert() bool {
for _, cert := range sd.Certificates {
if !is_zero_of_underlying_type(cert.V1AttrCert) {
return true
}
}
return false
}
func (sd *signed_data_raw) has_v2_cert() bool {
for _, cert := range sd.Certificates {
if !is_zero_of_underlying_type(cert.V2AttrCert) {
return true
}
}
return false
}
func (sd *signed_data_raw) has_v3_SignerInfo() bool {
for _, info := range sd.SignerInfos {
if info.Version == 3 {
return true
}
}
return false
}
func (sd *signed_data_raw) update_algs() {
used := make(map[string]bool)
algs := make(map[string]algorithm_identifier)
for _, info := range sd.SignerInfos {
used[info.DigestAlgorithm.ToHex()] = true
}
sd.DigestAlgorithms = make([]algorithm_identifier, len(used))
i := 0
for k, _ := range used {
sd.DigestAlgorithms[i] = algs[k]
i++
}
}
type signer_info_raw struct {
RawContent asn1.RawContent
Version int
Sid_V1 issuer_and_serial `asn1:"optional,omitempty"`
Sid_V3 []byte `asn1:"tag:0,optional,omitempty"`
DigestAlgorithm algorithm_identifier
SignedAttrs []attribute `asn1:"tag:0,set,optional,omitempty"`
SignedRaw []byte `asn1:"-"`
SignatureAlgorithm algorithm_identifier
Signature []byte
UnsignedAttrs []attribute `asn1:"tag:1,set,optional,omitempty"`
}
// Apply rule described on RFC5625 Section 5.3 Page 13. This function MUST be called before marshaling.
func (si *signer_info_raw) SetAppropriateVersion() {
si.Version = 0
if !is_zero_of_underlying_type(si.Sid_V1) {
si.Version = 1
}
if !is_zero_of_underlying_type(si.Sid_V3) {
si.Version = 3
}
}
func (si signer_info_raw) GetBytesToSign() []byte {
// fmt.Println(ToHex(si.SignedRaw))
return si.SignedRaw[2:]
}
func (si signer_info_raw) GetSignatureAlgorithm() algorithm_identifier {
// This may seem counter intuitive, but the Sign function gets the hasher through this function
return si.DigestAlgorithm
}
func (si *signer_info_raw) SetSignature(sig []byte) {
si.Signature = sig
}
func (si *signer_info_raw) BeforeMarshaling() error {
si.SetAppropriateVersion()
return nil
}
func (si *signer_info_raw) RemoveSignedAttrByType(attr_type asn1.ObjectIdentifier) {
for i, attr := range si.SignedAttrs {
if attr.Type.Equal(attr_type) {
si.SignedAttrs[i].Values = nil
si.SignedAttrs = append(si.SignedAttrs[:i], si.SignedAttrs[i+1:]...)
}
}
}
func (si *signer_info_raw) SetContentTypeAttr(content_type asn1.ObjectIdentifier) {
// Ensure we will have exactly one content type signed attribute
si.RemoveSignedAttrByType(idContentType)
// Add content type
attr := attribute{}
attr.Type = idContentType
attr.Values = make([]interface{}, 1)
attr.Values[0] = content_type
si.SignedAttrs = append(si.SignedAttrs, attr)
}
func (si signer_info_raw) DigestEncapContent(encap *encapsulated_content_info) ([]byte, CodedError) {
return encap.HashAs(si.DigestAlgorithm)
}
func (si *signer_info_raw) SetSigningTime(sig_time time.Time) {
// Ensure we will have exactly one singing time signed attribute
si.RemoveSignedAttrByType(idSigningTime)
// Add singing time
attr := attribute{}
attr.Type = idSigningTime
attr.Values = make([]interface{}, 1)
attr.Values[0] = sig_time.UTC()
si.SignedAttrs = append(si.SignedAttrs, attr)
}
func (si *signer_info_raw) SetMessageDigestAttr(encap *encapsulated_content_info) CodedError {
var err CodedError
// Ensure we will have exactly one message digest signed attribute
si.RemoveSignedAttrByType(idMessageDigest)
// Add message digest
attr := attribute{}
attr.Type = idMessageDigest
attr.Values = make([]interface{}, 1)
attr.Values[0], err = encap.HashAs(si.DigestAlgorithm)
if err != nil {
return err
}
si.SignedAttrs = append(si.SignedAttrs, attr)
return nil
}
func (si *signer_info_raw) GetFinalMessageDigest(encap *encapsulated_content_info) ([]byte, CodedError) {
var err error
if si.SignedAttrs == nil && encap == nil {
merr := NewMultiError("signed attributes and encap can't both be nil", ERR_NO_CONTENT, nil)
merr.SetParam("signer_info", si)
return nil, merr
}
if si.SignedAttrs == nil {
return encap.HashAs(si.DigestAlgorithm)
}
cerr := si.SetMessageDigestAttr(encap)
if cerr != nil {
return nil, cerr
}
si.SignedRaw, err = asn1.MarshalWithParams(si.SignedAttrs, "set,explicit")
if err != nil {
merr := NewMultiError("failed to mashal signed attributes", ERR_FAILED_TO_ENCODE, nil, err)
merr.SetParam("signer_info", si)
return nil, merr
}
return get_hasher_and_run(si.DigestAlgorithm, si.SignedRaw)
}
func (si *signer_info_raw) Sign(privkey *rsa.PrivateKey) CodedError {
return Sign(si, privkey)
}