/
sign_enveloped.go
281 lines (256 loc) · 7.74 KB
/
sign_enveloped.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
package pkcs7
import (
"crypto"
"crypto/rand"
"crypto/x509"
"crypto/x509/pkix"
"encoding/asn1"
"errors"
"github.com/aacfactory/afssl/gmsm/pkcs"
"github.com/aacfactory/afssl/gmsm/sm2"
"github.com/aacfactory/afssl/gmsm/smx509"
)
type signedEnvelopedData struct {
Version int `asn1:"default:1"`
RecipientInfos []recipientInfo `asn1:"set"`
DigestAlgorithmIdentifiers []pkix.AlgorithmIdentifier `asn1:"set"`
EncryptedContentInfo encryptedContentInfo
Certificates rawCertificates `asn1:"optional,tag:0"`
CRLs []x509.RevocationList `asn1:"optional,tag:1"`
SignerInfos []signerInfo `asn1:"set"`
}
func (data signedEnvelopedData) GetRecipient(cert *smx509.Certificate) *recipientInfo {
for _, recp := range data.RecipientInfos {
if isCertMatchForIssuerAndSerial(cert, recp.IssuerAndSerialNumber) {
return &recp
}
}
return nil
}
func (data signedEnvelopedData) GetEncryptedContentInfo() *encryptedContentInfo {
return &data.EncryptedContentInfo
}
func parseSignedEnvelopedData(data []byte) (*PKCS7, error) {
var sed signedEnvelopedData
if _, err := asn1.Unmarshal(data, &sed); err != nil {
return nil, err
}
certs, err := sed.Certificates.Parse()
if err != nil {
return nil, err
}
return &PKCS7{
Certificates: certs,
CRLs: sed.CRLs,
Signers: sed.SignerInfos,
raw: sed}, nil
}
type VerifyFunc func() error
func (p7 *PKCS7) DecryptAndVerifyOnlyOne(pkey crypto.PrivateKey, verifyFunc VerifyFunc) ([]byte, error) {
sed, ok := p7.raw.(signedEnvelopedData)
if !ok {
return nil, errors.New("pkcs7: it's NOT SignedAndEvelopedData")
}
if len(sed.RecipientInfos) != 1 {
return nil, errors.New("pkcs7: more than one recipients or no receipient")
}
defer func() {
p7.Content = nil
}()
plaintext, err := decryptSED(p7, &sed, &sed.RecipientInfos[0], pkey)
if err != nil {
return nil, err
}
if verifyFunc != nil {
p7.Content = plaintext
if err = verifyFunc(); err != nil {
return nil, err
}
}
return plaintext, nil
}
func (p7 *PKCS7) DecryptAndVerify(cert *smx509.Certificate, pkey crypto.PrivateKey, verifyFunc VerifyFunc) ([]byte, error) {
sed, ok := p7.raw.(signedEnvelopedData)
if !ok {
return nil, errors.New("pkcs7: it's NOT SignedAndEvelopedData")
}
recipient := sed.GetRecipient(cert)
if recipient == nil {
return nil, errors.New("pkcs7: no enveloped recipient for provided certificate")
}
defer func() {
p7.Content = nil
}()
plaintext, err := decryptSED(p7, &sed, recipient, pkey)
if err != nil {
return nil, err
}
if verifyFunc != nil {
p7.Content = plaintext
if err = verifyFunc(); err != nil {
return nil, err
}
}
return plaintext, nil
}
func decryptSED(p7 *PKCS7, sed *signedEnvelopedData, recipient *recipientInfo, pkey crypto.PrivateKey) ([]byte, error) {
switch pkey := pkey.(type) {
case crypto.Decrypter:
// Generic case to handle anything that provides the crypto.Decrypter interface.
contentKey, err := pkey.Decrypt(rand.Reader, recipient.EncryptedKey, nil)
if err != nil {
return nil, err
}
return sed.GetEncryptedContentInfo().decrypt(contentKey)
default:
return nil, ErrUnsupportedAlgorithm
}
}
type SignedAndEnvelopedData struct {
sed signedEnvelopedData
certs []*smx509.Certificate
data, cek []byte
digestOid asn1.ObjectIdentifier
isSM bool
}
func NewSignedAndEnvelopedData(data []byte, cipher pkcs.Cipher) (*SignedAndEnvelopedData, error) {
var key []byte
var err error
key = make([]byte, cipher.KeySize())
_, err = rand.Read(key)
if err != nil {
return nil, err
}
id, ciphertext, err := cipher.Encrypt(key, data)
if err != nil {
return nil, err
}
sed := signedEnvelopedData{
Version: 1, // 0 or 1?
EncryptedContentInfo: encryptedContentInfo{
ContentType: OIDData,
ContentEncryptionAlgorithm: *id,
EncryptedContent: marshalEncryptedContent(ciphertext),
},
}
return &SignedAndEnvelopedData{sed: sed, data: data, cek: key, digestOid: OIDDigestAlgorithmSHA1, isSM: false}, nil
}
func NewSMSignedAndEnvelopedData(data []byte, cipher pkcs.Cipher) (*SignedAndEnvelopedData, error) {
sd, err := NewSignedAndEnvelopedData(data, cipher)
if err != nil {
return nil, err
}
sd.digestOid = OIDDigestAlgorithmSM3
sd.isSM = true
sd.sed.EncryptedContentInfo.ContentType = SM2OIDData
return sd, nil
}
func (saed *SignedAndEnvelopedData) SetDigestAlgorithm(d asn1.ObjectIdentifier) {
saed.digestOid = d
}
func (saed *SignedAndEnvelopedData) AddSigner(ee *smx509.Certificate, pkey crypto.PrivateKey) error {
var parents []*smx509.Certificate
return saed.AddSignerChain(ee, pkey, parents)
}
func (saed *SignedAndEnvelopedData) AddSignerChain(ee *smx509.Certificate, pkey crypto.PrivateKey, parents []*smx509.Certificate) error {
var ias issuerAndSerial
ias.SerialNumber = ee.SerialNumber
if len(parents) == 0 {
// no parent, the issuer is the end-entity cert itself
ias.IssuerName = asn1.RawValue{FullBytes: ee.RawIssuer}
} else {
err := verifyPartialChain(ee, parents)
if err != nil {
return err
}
// the first parent is the issuer
ias.IssuerName = asn1.RawValue{FullBytes: parents[0].RawSubject}
}
saed.sed.DigestAlgorithmIdentifiers = append(saed.sed.DigestAlgorithmIdentifiers,
pkix.AlgorithmIdentifier{Algorithm: saed.digestOid},
)
hasher, err := getHashForOID(saed.digestOid)
if err != nil {
return err
}
signatureOid, err := getOIDForEncryptionAlgorithm(pkey, saed.digestOid)
if err != nil {
return err
}
key, ok := pkey.(crypto.Signer)
if !ok {
return errors.New("pkcs7: private key does not implement crypto.Signer")
}
var signOpt crypto.SignerOpts
var tobeSigned []byte
if saed.isSM {
signOpt = sm2.DefaultSignerOpts
tobeSigned = saed.data
} else {
signOpt = hasher
h := newHash(hasher, saed.digestOid)
h.Write(saed.data)
tobeSigned = h.Sum(nil)
}
signature, err := key.Sign(rand.Reader, tobeSigned, signOpt)
if err != nil {
return err
}
signer := signerInfo{
DigestAlgorithm: pkix.AlgorithmIdentifier{Algorithm: saed.digestOid},
DigestEncryptionAlgorithm: pkix.AlgorithmIdentifier{Algorithm: signatureOid},
IssuerAndSerialNumber: ias,
EncryptedDigest: signature,
Version: 1,
}
saed.certs = append(saed.certs, ee)
if len(parents) > 0 {
saed.certs = append(saed.certs, parents...)
}
saed.sed.SignerInfos = append(saed.sed.SignerInfos, signer)
return nil
}
func (saed *SignedAndEnvelopedData) AddCertificate(cert *smx509.Certificate) {
saed.certs = append(saed.certs, cert)
}
func (saed *SignedAndEnvelopedData) AddRecipient(recipient *smx509.Certificate) error {
encryptedKey, err := encryptKey(saed.cek, recipient)
if err != nil {
return err
}
ias, err := cert2issuerAndSerial(recipient)
if err != nil {
return err
}
var keyEncryptionAlgorithm = OIDEncryptionAlgorithmRSA
if recipient.SignatureAlgorithm == smx509.SM2WithSM3 {
keyEncryptionAlgorithm = OIDKeyEncryptionAlgorithmSM2
} else if saed.isSM {
return errors.New("pkcs7: Shangmi does not support RSA")
}
info := recipientInfo{
Version: 1,
IssuerAndSerialNumber: ias,
KeyEncryptionAlgorithm: pkix.AlgorithmIdentifier{
Algorithm: keyEncryptionAlgorithm,
},
EncryptedKey: encryptedKey,
}
saed.sed.RecipientInfos = append(saed.sed.RecipientInfos, info)
return nil
}
func (saed *SignedAndEnvelopedData) Finish() ([]byte, error) {
saed.sed.Certificates = marshalCertificates(saed.certs)
inner, err := asn1.Marshal(saed.sed)
if err != nil {
return nil, err
}
outer := contentInfo{
ContentType: OIDSignedEnvelopedData,
Content: asn1.RawValue{Class: 2, Tag: 0, Bytes: inner, IsCompound: true},
}
if saed.isSM {
outer.ContentType = SM2OIDSignedEnvelopedData
}
return asn1.Marshal(outer)
}