forked from itlabers/ofd-go
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
content_info.go
186 lines (156 loc) · 5.85 KB
/
content_info.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
package ofd
import (
std "encoding/asn1"
"golang.org/x/crypto/cryptobyte"
"golang.org/x/crypto/cryptobyte/asn1"
)
type ContentInfo struct {
contentType std.ObjectIdentifier
content cryptobyte.String
}
func NewContentInfo(input cryptobyte.String) (*ContentInfo, error) {
var contentInfo cryptobyte.String
input.ReadASN1(&contentInfo, asn1.SEQUENCE)
var contentType std.ObjectIdentifier
contentInfo.ReadASN1ObjectIdentifier(&contentType)
var content cryptobyte.String
var tag asn1.Tag
contentInfo.ReadAnyASN1(&content, &tag)
return &ContentInfo{
contentType: contentType,
content: content,
}, nil
}
func (contentInfo *ContentInfo) GetContent() []byte {
return contentInfo.content
}
type SignedData struct {
version int64
digestAlgorithms cryptobyte.String
contentInfo cryptobyte.String
certificates cryptobyte.String
extendedCertificatesAndCertificates cryptobyte.String
crls cryptobyte.String
signerInfos cryptobyte.String
}
func NewSignedData(content cryptobyte.String) (*SignedData, error) {
var signedData cryptobyte.String
content.ReadASN1(&signedData, asn1.SEQUENCE)
var version int64
signedData.ReadASN1Integer(&version)
var digestAlgorithms cryptobyte.String
signedData.ReadASN1(&digestAlgorithms, asn1.SET)
var contentInfo cryptobyte.String
signedData.ReadASN1(&contentInfo, asn1.SEQUENCE)
var certificates cryptobyte.String
signedData.ReadASN1(&certificates, asn1.SEQUENCE)
var extendedCertificatesAndCertificates cryptobyte.String
var exit bool
signedData.ReadOptionalASN1(&extendedCertificatesAndCertificates, &exit, asn1.SEQUENCE)
var crls cryptobyte.String
signedData.ReadOptionalASN1(&crls, &exit, asn1.SEQUENCE)
var signerInfos cryptobyte.String
signedData.ReadASN1(&signerInfos, asn1.SET)
return &SignedData{
version: version,
digestAlgorithms: digestAlgorithms,
contentInfo: content,
certificates: certificates,
extendedCertificatesAndCertificates: extendedCertificatesAndCertificates,
crls: crls,
signerInfos: signerInfos,
}, nil
}
func (signedData *SignedData) GetCertificates() []byte {
return signedData.certificates
}
func (signedData *SignedData) GetSignerInfos() ([]SignerInfo, error) {
var signerInfoSet []SignerInfo
signerInfos := signedData.signerInfos
exist := true
for exist {
var signerInfo cryptobyte.String
exist = signerInfos.ReadASN1(&signerInfo, asn1.SEQUENCE)
if exist {
item, err := NewSignerInfo(signerInfo)
if err != nil {
break
}
signerInfoSet = append(signerInfoSet, *item)
}
}
return signerInfoSet, nil
}
type SignerInfo struct {
version int64
issuerAndSerialNumber cryptobyte.String
digestAlgorithm cryptobyte.String
authenticatedAttributes cryptobyte.String
digestEncryptionAlgorithm cryptobyte.String
encryptedDigest cryptobyte.String
unauthenticatedAttributes cryptobyte.String
}
func NewSignerInfo(signInfo cryptobyte.String) (*SignerInfo, error) {
var version int64
signInfo.ReadASN1Integer(&version)
var issuerAndSerialNumber cryptobyte.String
signInfo.ReadASN1(&issuerAndSerialNumber, asn1.SEQUENCE)
var digestAlgorithm cryptobyte.String
signInfo.ReadASN1(&digestAlgorithm, asn1.SEQUENCE)
var authenticatedAttributes cryptobyte.String
var tag asn1.Tag
signInfo.ReadAnyASN1Element(&authenticatedAttributes, &tag)
var digestEncryptionAlgorithm cryptobyte.String
signInfo.ReadASN1(&digestEncryptionAlgorithm, asn1.SEQUENCE)
var encryptedDigest cryptobyte.String
signInfo.ReadASN1(&encryptedDigest, asn1.SEQUENCE)
var unauthenticatedAttributes cryptobyte.String
var exit bool
signInfo.ReadOptionalASN1(&unauthenticatedAttributes, &exit, asn1.SEQUENCE)
return &SignerInfo{
version: version,
issuerAndSerialNumber: issuerAndSerialNumber,
digestAlgorithm: digestAlgorithm,
authenticatedAttributes: authenticatedAttributes,
digestEncryptionAlgorithm: digestEncryptionAlgorithm,
encryptedDigest: encryptedDigest,
unauthenticatedAttributes: unauthenticatedAttributes,
}, nil
}
func (signerInfo *SignerInfo) GetVersion() int64 {
return signerInfo.version
}
func (signerInfo *SignerInfo) GetIssuerAndSerialNumber() cryptobyte.String {
return signerInfo.issuerAndSerialNumber
}
func (signerInfo *SignerInfo) GetDigestAlgorithm() string {
digestAlgorithm := signerInfo.digestAlgorithm
var digestAlgorithmOid std.ObjectIdentifier
digestAlgorithm.ReadASN1ObjectIdentifier(&digestAlgorithmOid)
return digestAlgorithmOid.String()
}
func (signerInfo *SignerInfo) GetAuthenticatedAttributes() cryptobyte.String {
authenticatedAttributes := signerInfo.authenticatedAttributes
tag := authenticatedAttributes[0]
if tag == 0xA0 {
authenticatedAttributes[0] = 0x31
}
return authenticatedAttributes
}
func (signerInfo *SignerInfo) GetDigestEncryptionAlgorithm() string {
digestEncryptionAlgorithm := signerInfo.digestEncryptionAlgorithm
var digestEncryptionAlgorithmOid std.ObjectIdentifier
digestEncryptionAlgorithm.ReadASN1ObjectIdentifier(&digestEncryptionAlgorithmOid)
return digestEncryptionAlgorithmOid.String()
}
func (signerInfo *SignerInfo) GetEncryptedDigest() cryptobyte.String {
return signerInfo.encryptedDigest
}
func (signerInfo *SignerInfo) GetUnauthenticatedAttributes() cryptobyte.String {
unauthenticatedAttributes := signerInfo.unauthenticatedAttributes
tag := unauthenticatedAttributes[0]
if tag == 0xA1 {
unauthenticatedAttributes[0] = 0x31
}
return unauthenticatedAttributes
}