-
Notifications
You must be signed in to change notification settings - Fork 23
/
pkcs7.go
348 lines (294 loc) · 7.63 KB
/
pkcs7.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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
package pe
import (
"crypto"
"crypto/x509/pkix"
"encoding/asn1"
"errors"
"fmt"
"strings"
"time"
"github.com/Velocidex/ordereddict"
"github.com/Velocidex/pkcs7"
"golang.org/x/text/encoding/unicode"
)
type SpcString struct {
Unicode []byte `asn1:"tag:0"`
}
type SpcPeImageData struct {
Flags asn1.BitString
// Flags []asn1.RawValue `asn1:"tag:0,optional"`
File asn1.RawValue
}
type SpcAttributeTypeAndOptionalValue struct {
Type asn1.ObjectIdentifier
Value SpcPeImageData `asn1:"tag:2,optional"`
}
type AlgorithmIdentifier struct {
Type asn1.ObjectIdentifier
}
type DigestInfo struct {
DigestAlgorithm pkix.AlgorithmIdentifier
Digest []byte
}
type SpcIndirectDataContent struct {
Data SpcAttributeTypeAndOptionalValue
MessageDigest DigestInfo
}
func parseIndirectData(pkcs7 *pkcs7.PKCS7) (*SpcIndirectDataContent, error) {
var indirect_data SpcIndirectDataContent
_, err := asn1.Unmarshal(pkcs7.SignedData.ContentInfo.Content.Bytes, &indirect_data)
if err != nil {
return nil, err
}
return &indirect_data, nil
}
type spcSpOpusInfo struct {
ProgramName asn1.RawValue `asn1:"explicit,optional,tag:0"`
MoreInfo asn1.RawValue `asn1:"explicit,optional,tag:1"`
}
type spcString struct {
Value asn1.RawValue
}
type SpcSpOpusInfo struct {
ProgramName string
MoreInfo string
}
func UTF16ToString(in []byte) string {
decoder := unicode.UTF16(unicode.BigEndian, unicode.IgnoreBOM).NewDecoder()
utf8, err := decoder.Bytes(in)
if err != nil {
return string(in)
}
return string(utf8)
}
func UTF16ToStringLE(in []byte) string {
decoder := unicode.UTF16(unicode.LittleEndian, unicode.IgnoreBOM).NewDecoder()
utf8, err := decoder.Bytes(in)
if err != nil {
return string(in)
}
return string(utf8)
}
func decodeSpcString(value asn1.RawValue) string {
var result asn1.RawValue
asn1.Unmarshal(value.Bytes, &result)
// This does not appear very consistent in practice so we just
// guess if it is unicode or utf8.
if len(result.Bytes) > 0 && len(result.Bytes)%2 == 0 && result.Bytes[0] == 0 {
return UTF16ToString(result.Bytes)
}
return string(result.Bytes)
}
func parseSpcSpOpusInfo(bytes []byte) *SpcSpOpusInfo {
var data spcSpOpusInfo
_, err := asn1.Unmarshal(bytes, &data)
if err == nil {
// Since SpcString is a choice we need to decode it by
// hand.
return &SpcSpOpusInfo{
ProgramName: decodeSpcString(data.ProgramName),
MoreInfo: decodeSpcString(data.MoreInfo),
}
}
return nil
}
func getHashForOID(oid asn1.ObjectIdentifier) (crypto.Hash, string, error) {
switch {
case oid.Equal(pkcs7.OIDDigestAlgorithmSHA1),
oid.Equal(pkcs7.OIDDigestAlgorithmECDSASHA1),
oid.Equal(pkcs7.OIDDigestAlgorithmDSA),
oid.Equal(pkcs7.OIDDigestAlgorithmDSASHA1),
oid.Equal(pkcs7.OIDEncryptionAlgorithmRSA):
return crypto.SHA1, "SHA1", nil
case oid.Equal(pkcs7.OIDDigestAlgorithmSHA256),
oid.Equal(pkcs7.OIDDigestAlgorithmECDSASHA256):
return crypto.SHA256, "SHA256", nil
case oid.Equal(pkcs7.OIDDigestAlgorithmSHA384),
oid.Equal(pkcs7.OIDDigestAlgorithmECDSASHA384):
return crypto.SHA384, "SHA384", nil
case oid.Equal(pkcs7.OIDDigestAlgorithmSHA512),
oid.Equal(pkcs7.OIDDigestAlgorithmECDSASHA512):
return crypto.SHA512, "SHA512", nil
}
return crypto.Hash(0), "Unknown", errors.New("Unsupported")
}
func parseTimestamp(bytes []byte) *time.Time {
var result time.Time
_, err := asn1.Unmarshal(bytes, &result)
if err == nil {
return &result
}
return nil
}
func parseMessageDigest(bytes []byte) []byte {
var result []byte
asn1.Unmarshal(bytes, &result)
return result
}
func parseCounterSignature(bytes []byte) *pkcs7.SignerInfo {
var result pkcs7.SignerInfo
_, err := asn1.Unmarshal(bytes, &result)
if err == nil {
return &result
}
return nil
}
func getContentTypeString(bytes []byte) string {
var oid asn1.ObjectIdentifier
asn1.Unmarshal(bytes, &oid)
switch {
case oid.Equal(oidCertificateTrustList):
return fmt.Sprintf("Certificate Trust List")
default:
return fmt.Sprintf("%v", oid)
}
}
type OIDSequence struct {
Type asn1.ObjectIdentifier
}
func (self OIDSequence) Equal(in asn1.ObjectIdentifier) bool {
return self.Type.Equal(in)
}
type OIDWithParamers struct {
Type asn1.ObjectIdentifier
Params asn1.RawValue `asn1:"set,optional"`
}
func (self OIDWithParamers) Equal(in asn1.ObjectIdentifier) bool {
return self.Type.Equal(in)
}
// A hash descriptor for a file or cab in the catalog
type CabHash struct {
Type OIDSequence
Digest []byte
}
type KV struct {
Type OIDSequence
Value asn1.RawValue `asn1:"optional"`
}
func (self KV) Walk(out *ordereddict.Dict) {
if self.Value.Bytes == nil {
return
}
switch {
case self.Type.Equal(OIDSPC_CAB_DATA_OBJID), self.Type.Equal(OIDSPC_PE_IMAGE_DATA_OBJID):
var hash_info CabHash
_, err := asn1.Unmarshal(self.Value.FullBytes, &hash_info)
if err != nil {
return
}
_, hash, _ := getHashForOID(hash_info.Type.Type)
out.Set("HashType", hash)
out.Set("Hash", fmt.Sprintf("%x", hash_info.Digest))
default:
Debug(self)
}
}
// Arbitrary kv string with metadata
type KVString struct {
Key string
Unknown int
Value []byte
}
type CatalogMemberSet struct {
Type asn1.ObjectIdentifier
Value asn1.RawValue `asn1:"optional"`
}
func (self CatalogMemberSet) Walk(out *ordereddict.Dict) {
if self.Value.FullBytes == nil {
return
}
switch {
case self.Type.Equal(OIDCAT_MEMBERINFO2_OBJID), self.Type.Equal(OIDCAT_MEMBERINFO_OBJID):
case self.Type.Equal(OIDNameValueObjId):
var nameValue KVString
_, err := asn1.Unmarshal(self.Value.Bytes, &nameValue)
if err != nil {
Debug(err)
return
}
filename := UTF16ToStringLE(nameValue.Value)
filename = strings.TrimSuffix(filename, "\x00")
out.Set(nameValue.Key, filename)
case self.Type.Equal(OIDIndirectData):
var kv KV
_, err := asn1.Unmarshal(self.Value.Bytes, &kv)
if err != nil {
Debug(err)
return
}
kv.Walk(out)
default:
Debug(self)
}
}
type CatalogList struct {
Digest []byte
Members []asn1.RawValue `asn1:"set,optional"`
}
func (self CatalogList) Walk(out *ordereddict.Dict) {
for _, raw_member := range self.Members {
var member CatalogMemberSet
_, err := asn1.Unmarshal(raw_member.FullBytes, &member)
if err != nil {
Debug(err)
continue
}
member.Walk(out)
}
}
type NameValue struct {
Type asn1.ObjectIdentifier
Value asn1.RawValue
}
func (self NameValue) Walk(out *ordereddict.Dict) {
if self.Value.Bytes == nil {
return
}
switch {
case self.Type.Equal(OIDNameValueObjId):
var nameValue KVString
_, err := asn1.Unmarshal(self.Value.Bytes, &nameValue)
if err != nil {
Debug(err)
return
}
filename := UTF16ToStringLE(nameValue.Value)
filename = strings.TrimSuffix(filename, "\x00")
out.Set(nameValue.Key, filename)
}
}
type CatNameValue struct {
Items []NameValue
}
func (self CatNameValue) Walk(out *ordereddict.Dict) {
for _, item := range self.Items {
item.Walk(out)
}
}
type CertificateTrustList struct {
Type OIDSequence
Digest []byte
Time time.Time
MemberOID OIDSequence
CatalogList []CatalogList
CatNameValue CatNameValue `asn1:"tag:0"`
}
// CTLs are stored in catalog .cat files.
func parseCertificateTrustList(pkcs7 *pkcs7.PKCS7, result *ordereddict.Dict) {
var ctl CertificateTrustList
_, err := asn1.Unmarshal(pkcs7.SignedData.ContentInfo.Content.Bytes, &ctl)
if err != nil {
Debug(err)
return
}
var ctl_set []*ordereddict.Dict
for _, item := range ctl.CatalogList {
item_dict := ordereddict.NewDict().
Set("Hash", fmt.Sprintf("%x", item.Digest))
ctl_set = append(ctl_set, item_dict)
// Walk the ASN.1 struct and parse out interesting fields
item.Walk(item_dict)
}
ctl.CatNameValue.Walk(result)
result.Set("CertificateTrustList", ctl_set)
}