-
Notifications
You must be signed in to change notification settings - Fork 25
/
verify.go
65 lines (51 loc) · 1.66 KB
/
verify.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
package ca
import (
"errors"
"crypto/x509"
"encoding/pem"
sm2X509 "github.com/tjfoc/gmsm/x509"
)
// 验证
func (this CA) Verify(rootPEM string, certPEM string, opts x509.VerifyOptions) (bool, error) {
roots := x509.NewCertPool()
ok := roots.AppendCertsFromPEM([]byte(rootPEM))
if !ok {
return false, errors.New("CA: failed to parse root certificate")
}
block, _ := pem.Decode([]byte(certPEM))
if block == nil {
return false, errors.New("CA: failed to parse certificate PEM")
}
cert, err := x509.ParseCertificate(block.Bytes)
if err != nil {
return false, errors.New("CA: failed to parse certificate: " + err.Error())
}
// 重设
opts.Roots = roots
if _, err := cert.Verify(opts); err != nil {
return false, errors.New("CA: failed to verify certificate: " + err.Error())
}
return true, nil
}
// SM2 验证
func (this CA) SM2Verify(rootPEM string, certPEM string, opts sm2X509.VerifyOptions) (bool, error) {
roots := sm2X509.NewCertPool()
ok := roots.AppendCertsFromPEM([]byte(rootPEM))
if !ok {
return false, errors.New("failed to parse root certificate")
}
block, _ := pem.Decode([]byte(certPEM))
if block == nil {
return false, errors.New("failed to parse certificate PEM")
}
cert, err := sm2X509.ParseCertificate(block.Bytes)
if err != nil {
return false, errors.New("failed to parse certificate: " + err.Error())
}
// 重设
opts.Roots = roots
if _, err := cert.Verify(opts); err != nil {
return false, errors.New("failed to verify certificate: " + err.Error())
}
return true, nil
}