forked from itlabers/ofd-go
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
signature.go
199 lines (183 loc) · 5.36 KB
/
signature.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
package ofd
import (
"archive/zip"
"bytes"
"encoding/base64"
"encoding/xml"
"fmt"
"strings"
"golang.org/x/crypto/cryptobyte"
)
type Signature struct {
SignatureXml
pwd string
rc *zip.ReadCloser
Validator
Category
Content []byte
}
type SignatureXml struct {
XMLName xml.Name `xml:"Signature"`
Text string `xml:",chardata"`
Ofd string `xml:"ofd,attr"`
SignedInfo struct {
Text string `xml:",chardata"`
Provider struct {
Text string `xml:",chardata"`
ProviderName string `xml:"ProviderName,attr"`
Version string `xml:"Version,attr"`
Company string `xml:"Company,attr"`
} `xml:"Provider"`
SignatureMethod struct {
Text string `xml:",chardata"`
} `xml:"SignatureMethod"`
SignatureDateTime struct {
Text string `xml:",chardata"`
} `xml:"SignatureDateTime"`
Parameters struct {
Text string `xml:",chardata"`
Parameter struct {
Text string `xml:",chardata"`
Name string `xml:"Name,attr"`
} `xml:"Parameter"`
} `xml:"Parameters"`
References struct {
Text string `xml:",chardata"`
CheckMethod string `xml:"CheckMethod,attr"`
Reference []struct {
Text string `xml:",chardata"`
FileRef string `xml:"FileRef,attr"`
CheckValue struct {
Text string `xml:",chardata"`
} `xml:"CheckValue"`
} `xml:"Reference"`
} `xml:"References"`
StampAnnot struct {
Text string `xml:",chardata"`
ID string `xml:"ID,attr"`
PageRef string `xml:"PageRef,attr"`
Boundary string `xml:"Boundary,attr"`
} `xml:"StampAnnot"`
} `xml:"SignedInfo"`
SignedValue struct {
Text string `xml:",chardata"`
} `xml:"SignedValue"`
}
func (signature Signature) GetFileContent(path string) ([]byte, error) {
return LoadZipFileContent(signature.rc, path)
}
func (signature Signature) VerifyDigest() (bool, error) {
checkMethod := signature.SignedInfo.References.CheckMethod
if !(strings.EqualFold(checkMethod, SM3_OID) || strings.EqualFold(checkMethod, "sm3")) {
return false, fmt.Errorf("oid(%s) not support ", checkMethod)
}
ref := signature.SignedInfo.References.Reference
for _, value := range ref {
filePath := value.FileRef
if filePath[0] == '/' {
filePath = filePath[1:]
}
content, err := signature.GetFileContent(filePath)
if err != nil {
break
} else {
validator := signature.Validator
hashed := validator.Digest(content)
encodeString := base64.StdEncoding.EncodeToString(hashed)
if !strings.EqualFold(encodeString, value.CheckValue.Text) {
return false, fmt.Errorf("file %s checkValue mistake", filePath)
} else {
continue
}
}
}
return true, nil
}
func (signature Signature) Verify() (bool, error) {
switch signature.Category {
case SIGN:
return signature.verifySign()
case SEAL:
return signature.verifySeal()
default:
return signature.verifySeal()
}
}
func (signature Signature) verifySign() (bool, error) {
signatureMethod := signature.SignedInfo.SignatureMethod.Text
if !strings.EqualFold(SM3WITHSM2_OID, signatureMethod) {
return false, fmt.Errorf("oid(%s) not support ", signatureMethod)
}
signDatPath := signature.pwd + "/" + signature.SignedValue.Text
content, err := signature.GetFileContent(signDatPath)
if err != nil {
return false, err
}
if contentInfo, err := NewContentInfo(cryptobyte.String(content)); err != nil {
return false, err
} else {
content := contentInfo.GetContent()
signedData, err := NewSignedData(content)
if err != nil {
return false, err
}
signerInfoes, err := signedData.GetSignerInfos()
if err != nil {
return false, err
}
cert := signedData.GetCertificates()
for _, sign := range signerInfoes {
msg := sign.GetAuthenticatedAttributes()
encryptedDigest := sign.GetEncryptedDigest()
validator := signature.Validator
result, err := validator.Verify(cert, msg, encryptedDigest)
if !result || err != nil {
return false, err
} else {
continue
}
}
}
return true, nil
}
func (signature Signature) verifySeal() (bool, error) {
signatureMethod := signature.SignedInfo.SignatureMethod.Text
if !strings.EqualFold(SM3WITHSM2_OID, signatureMethod) {
return false, fmt.Errorf("oid(%s) not support ", signatureMethod)
}
signDatPath := signature.SignedValue.Text
content, err := signature.GetFileContent(signDatPath)
if err != nil {
signDatPath = signature.pwd + "/" + signature.SignedValue.Text
content, err = signature.GetFileContent(signDatPath)
if err != nil {
return false, err
}
}
validator := signature.Validator
if ses, err := New_SES_Signature(content); err != nil {
return false, err
} else {
tbs_sign, err := ses.Get_TBS_Sign()
if err != nil {
return false, err
} else {
dataDash := validator.Digest(signature.Content)
if !bytes.Equal(tbs_sign.Get_DataHash(), dataDash) {
return false, fmt.Errorf("%s", "Signatures.xml Hash Value Mistake")
}
seal, err := tbs_sign.Get_Seal()
if err != nil {
return false, err
}
if _, err := validator.Verify(seal.cert, seal.eSealInfo, seal.signature.Bytes); err != nil {
return false, err
}
}
if _, err := validator.Verify(ses.cert, ses.to_sign, ses.signature.Bytes); err != nil {
return false, err
} else {
return true, nil
}
}
}