/
cert.go
183 lines (143 loc) · 3.69 KB
/
cert.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
package cert
import (
"crypto/ecdsa"
"crypto/elliptic"
"crypto/x509"
"encoding/pem"
"fmt"
"github.com/pkg/errors"
"github.com/taiyuechain/taiyuechain/cert/crypto/sm2"
"github.com/taiyuechain/taiyuechain/crypto"
"io/ioutil"
"strings"
)
func IsCorrectSY(syCrypto interface{}) bool {
switch pub := syCrypto.(type) {
case *sm2.PublicKey:
if crypto.CryptoType == crypto.CRYPTO_SM2_SM3_SM4 {
return true
}
case *ecdsa.PublicKey:
switch pub.Curve {
case elliptic.P256():
if crypto.CryptoType == crypto.CRYPTO_P256_SH3_AES {
return true
}
}
}
return false
}
func GetCertFromByte(idBytes []byte) (*x509.Certificate, error) {
if idBytes == nil {
return nil, errors.New("GetCertFromByte error: nil idBytes")
}
cert, err := ParseCertificate(idBytes)
if err != nil {
return nil, errors.Wrap(err, "GetCertFromByte error: failed to parse x509 cert")
}
return cert, nil
}
func ParseCertificate(asn1Data []byte) (*x509.Certificate, error) {
if asn1Data == nil {
return nil, errors.New("ParseCertificate error: nil idBytes")
}
cert ,err := x509.ParseCertificate(asn1Data)
if err != nil{
return ParseCertificateSM2(asn1Data)
}
return cert, err
}
func CheckSignatureFrom(son *x509.Certificate, parent *x509.Certificate) error {
switch son.PublicKey.(type) {
case *sm2.PublicKey:
CheckSignatureFromSM2(son, parent)
case *ecdsa.PublicKey:
son.CheckSignatureFrom(parent)
}
return nil
}
func CheckSignature(cert *x509.Certificate) error {
switch cert.PublicKey.(type) {
case *sm2.PublicKey:
CheckSignatureSM2(cert)
case *ecdsa.PublicKey:
cert.CheckSignature(cert.SignatureAlgorithm, cert.RawTBSCertificate, cert.Signature)
}
return nil
}
func IsEqulCert(cert *x509.Certificate, idBytes []byte) error {
needVerfyCert, err := GetCertFromByte(idBytes)
if err != nil {
return err
}
if err != nil {
return err
}
if !cert.Equal(needVerfyCert) {
return errors.New("not equl ")
}
return nil
}
func ReadPemFileByPath(path string) ([]byte, error) {
data, err := ioutil.ReadFile(path)
if err != nil {
panic(fmt.Sprintf(
"Unable to read test certificate from %q - %q "+
"Does a unit test have an incorrect test file name?\n",
path, err))
}
if strings.Contains(string(data), "-BEGIN CERTIFICATE-") {
block, _ := pem.Decode(data)
if block == nil {
panic(fmt.Sprintf(
"Failed to PEM decode test certificate from %q - "+
"Does a unit test have a buggy test cert file?\n",
path))
}
data = block.Bytes
}
return data, nil
}
func GetPubByteFromCert(asn1Data []byte) ([]byte, error) {
cert, err := ParseCertificate(asn1Data)
if err != nil {
return nil, err
}
pubk := cert.PublicKey
switch pub2 := pubk.(type) {
case *ecdsa.PublicKey:
return elliptic.Marshal(pub2.Curve, pub2.X, pub2.Y), nil
case *sm2.PublicKey:
return elliptic.Marshal(pub2.Curve, pub2.X, pub2.Y), nil
}
return nil, errors.New("err public curve")
}
func FromCertBytesToPubKey(asn1Data []byte) (*ecdsa.PublicKey, error) {
//data, err := GetPubByteFromCert(asn1Data)
cert, err := ParseCertificate(asn1Data)
if err != nil {
return nil, err
}
pubk := cert.PublicKey
switch pub2 := pubk.(type) {
case *ecdsa.PublicKey:
return &ecdsa.PublicKey{Curve: pub2.Curve, X: pub2.X, Y: pub2.Y}, nil
case *sm2.PublicKey:
return &ecdsa.PublicKey{Curve: pub2.Curve, X: pub2.X, Y: pub2.Y}, nil
}
return nil, nil
}
func FromCertBytesToPubKey1(asn1Data []byte) (*ecdsa.PublicKey, error) {
cert, err := ParseCertificate(asn1Data)
if err != nil {
return nil, err
}
pubk := cert.PublicKey
switch pub2 := pubk.(type) {
case *ecdsa.PublicKey:
return pub2, nil
case *sm2.PublicKey:
return sm2.ToECDSAPublickey(pub2), nil
}
return nil, errors.New("err public curve")
}