/
keys.go
109 lines (93 loc) · 2.91 KB
/
keys.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
// Copyright Fuzamei Corp. 2018 All Rights Reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package utils
import (
"crypto/ecdsa"
"crypto/elliptic"
"crypto/sha256"
"crypto/x509"
"encoding/hex"
"encoding/pem"
"math/big"
"github.com/33cn/chain33/types"
"fmt"
ecdsa_util "github.com/33cn/chain33/system/crypto/secp256r1"
sm2_util "github.com/33cn/chain33/system/crypto/sm2"
ty "github.com/33cn/plugin/plugin/dapp/cert/types"
"github.com/pkg/errors"
"github.com/tjfoc/gmsm/sm2"
)
// SKI 计算ski
func SKI(curve elliptic.Curve, x, y *big.Int) (ski []byte) {
raw := elliptic.Marshal(curve, x, y)
hash := sha256.New()
hash.Write(raw)
return hash.Sum(nil)
}
// GetPublicKeySKIFromCert 从cert字节中获取公钥ski
func GetPublicKeySKIFromCert(cert []byte, signType int) (string, error) {
dcert, _ := pem.Decode(cert)
if dcert == nil {
return "", errors.Errorf("Unable to decode cert bytes [%v]", cert)
}
var ski []byte
switch signType {
case ty.AuthECDSA:
x509Cert, err := x509.ParseCertificate(dcert.Bytes)
if err != nil {
return "", errors.Errorf("Unable to parse cert from decoded bytes: %s", err)
}
ecdsaPk := x509Cert.PublicKey.(*ecdsa.PublicKey)
ski = SKI(ecdsaPk.Curve, ecdsaPk.X, ecdsaPk.Y)
case ty.AuthSM2:
sm2Cert, err := sm2.ParseCertificate(dcert.Bytes)
if err != nil {
return "", errors.Errorf("Unable to parse cert from decoded bytes: %s", err)
}
sm2Pk := sm2Cert.PublicKey.(*ecdsa.PublicKey)
ski = SKI(sm2Pk.Curve, sm2Pk.X, sm2Pk.Y)
default:
return "", errors.Errorf("unknow public key type")
}
return hex.EncodeToString(ski), nil
}
// EncodeCertToSignature 证书编码进签名
func EncodeCertToSignature(signByte []byte, cert []byte, uid []byte) []byte {
var certSign ty.CertSignature
certSign.Signature = append(certSign.Signature, signByte...)
certSign.Cert = append(certSign.Cert, cert...)
certSign.Uid = append(certSign.Uid, uid...)
return types.Encode(&certSign)
}
// DecodeCertFromSignature 从签名中解码证书
func DecodeCertFromSignature(signByte []byte) (*ty.CertSignature, error) {
var certSign ty.CertSignature
err := types.Decode(signByte, &certSign)
if err != nil {
return nil, err
}
return &certSign, nil
}
// PrivKeyByteFromRaw pem结构转成byte类型私钥
func PrivKeyByteFromRaw(raw []byte, signType int) ([]byte, error) {
block, _ := pem.Decode(raw)
if block == nil {
return nil, fmt.Errorf("Failed decoding PEM. Block must be different from nil. [% x]", raw)
}
switch signType {
case ty.AuthECDSA:
key, err := x509.ParsePKCS8PrivateKey(block.Bytes)
if err != nil {
return nil, err
}
return ecdsa_util.SerializePrivateKey(key.(*ecdsa.PrivateKey)), nil
case ty.AuthSM2:
key, err := sm2.ParsePKCS8PrivateKey(block.Bytes, nil)
if err != nil {
return nil, err
}
return sm2_util.SerializePrivateKey(key), nil
}
return nil, errors.Errorf("unknow public key type")
}