Commit 30d1028d authored by pengjun's avatar pengjun

update cert_auth

parent 2dae7da6
...@@ -36,7 +36,7 @@ require ( ...@@ -36,7 +36,7 @@ require (
github.com/rs/cors v1.6.0 github.com/rs/cors v1.6.0
github.com/spf13/cobra v0.0.5 github.com/spf13/cobra v0.0.5
github.com/stretchr/testify v1.4.0 github.com/stretchr/testify v1.4.0
github.com/tjfoc/gmsm v1.3.1 github.com/tjfoc/gmsm v1.3.2
github.com/valyala/fasthttp v1.5.0 github.com/valyala/fasthttp v1.5.0
github.com/xiang90/probing v0.0.0-20190116061207-43a291ad63a2 // indirect github.com/xiang90/probing v0.0.0-20190116061207-43a291ad63a2 // indirect
go.uber.org/atomic v1.4.0 // indirect go.uber.org/atomic v1.4.0 // indirect
......
...@@ -637,6 +637,8 @@ github.com/syndtr/goleveldb v1.0.1-0.20190923125748-758128399b1d/go.mod h1:9OrXJ ...@@ -637,6 +637,8 @@ github.com/syndtr/goleveldb v1.0.1-0.20190923125748-758128399b1d/go.mod h1:9OrXJ
github.com/timakin/bodyclose v0.0.0-20190721030226-87058b9bfcec/go.mod h1:Qimiffbc6q9tBWlVV6x0P9sat/ao1xEkREYPPj9hphk= github.com/timakin/bodyclose v0.0.0-20190721030226-87058b9bfcec/go.mod h1:Qimiffbc6q9tBWlVV6x0P9sat/ao1xEkREYPPj9hphk=
github.com/tjfoc/gmsm v1.3.1 h1:+k3IAlF81c31/TllJmIfuCYnjl8ziMdTWGWJcP9J1uo= github.com/tjfoc/gmsm v1.3.1 h1:+k3IAlF81c31/TllJmIfuCYnjl8ziMdTWGWJcP9J1uo=
github.com/tjfoc/gmsm v1.3.1/go.mod h1:HaUcFuY0auTiaHB9MHFGCPx5IaLhTUd2atbCFBQXn9w= github.com/tjfoc/gmsm v1.3.1/go.mod h1:HaUcFuY0auTiaHB9MHFGCPx5IaLhTUd2atbCFBQXn9w=
github.com/tjfoc/gmsm v1.3.2 h1:7JVkAn5bvUJ7HtU08iW6UiD+UTmJTIToHCfeFzkcCxM=
github.com/tjfoc/gmsm v1.3.2/go.mod h1:HaUcFuY0auTiaHB9MHFGCPx5IaLhTUd2atbCFBQXn9w=
github.com/tyler-smith/go-bip39 v1.0.1-0.20181017060643-dbb3b84ba2ef h1:wHSqTBrZW24CsNJDfeh9Ex6Pm0Rcpc7qrgKBiL44vF4= github.com/tyler-smith/go-bip39 v1.0.1-0.20181017060643-dbb3b84ba2ef h1:wHSqTBrZW24CsNJDfeh9Ex6Pm0Rcpc7qrgKBiL44vF4=
github.com/tyler-smith/go-bip39 v1.0.1-0.20181017060643-dbb3b84ba2ef/go.mod h1:sJ5fKU0s6JVwZjjcUEX2zFOnvq0ASQ2K9Zr6cf67kNs= github.com/tyler-smith/go-bip39 v1.0.1-0.20181017060643-dbb3b84ba2ef/go.mod h1:sJ5fKU0s6JVwZjjcUEX2zFOnvq0ASQ2K9Zr6cf67kNs=
github.com/ugorji/go/codec v0.0.0-20181204163529-d75b2dcb6bc8/go.mod h1:VFNgLljTbGfSG7qAOspJ7OScBnGdDN/yBr0sguwnwf0= github.com/ugorji/go/codec v0.0.0-20181204163529-d75b2dcb6bc8/go.mod h1:VFNgLljTbGfSG7qAOspJ7OScBnGdDN/yBr0sguwnwf0=
......
...@@ -2,21 +2,253 @@ ...@@ -2,21 +2,253 @@
// Use of this source code is governed by a BSD-style // Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file. // license that can be found in the LICENSE file.
// Package sm2 带证书交易的签名
package sm2 package sm2
import ( import (
"bytes"
"crypto/elliptic"
"errors"
"fmt"
"github.com/33cn/chain33/types"
pkt "github.com/33cn/plugin/plugin/dapp/cert/types"
"math/big"
"github.com/33cn/chain33/common/crypto" "github.com/33cn/chain33/common/crypto"
"github.com/33cn/chain33/system/crypto/sm2" "github.com/tjfoc/gmsm/sm2"
)
//const
const (
SM2PrivateKeyLength = 32
SM2PublicKeyLength = 65
SM2PublicKeyCompressed = 33
) )
type sm2Driver struct { //Driver 驱动
sm2.Driver type Driver struct{}
//GenKey 生成私钥
func (d Driver) GenKey() (crypto.PrivKey, error) {
privKeyBytes := [SM2PrivateKeyLength]byte{}
copy(privKeyBytes[:], crypto.CRandBytes(SM2PrivateKeyLength))
priv, _ := privKeyFromBytes(sm2.P256Sm2(), privKeyBytes[:])
copy(privKeyBytes[:], SerializePrivateKey(priv))
return PrivKeySM2(privKeyBytes), nil
}
//PrivKeyFromBytes 字节转为私钥
func (d Driver) PrivKeyFromBytes(b []byte) (privKey crypto.PrivKey, err error) {
if len(b) != SM2PrivateKeyLength {
return nil, errors.New("invalid priv key byte")
}
privKeyBytes := new([SM2PrivateKeyLength]byte)
copy(privKeyBytes[:], b[:SM2PrivateKeyLength])
priv, _ := privKeyFromBytes(sm2.P256Sm2(), privKeyBytes[:])
copy(privKeyBytes[:], SerializePrivateKey(priv))
return PrivKeySM2(*privKeyBytes), nil
}
//PubKeyFromBytes 字节转为公钥
func (d Driver) PubKeyFromBytes(b []byte) (pubKey crypto.PubKey, err error) {
if len(b) != SM2PublicKeyLength && len(b) != SM2PublicKeyCompressed {
return nil, errors.New("invalid pub key byte")
}
pubKeyBytes := new([SM2PublicKeyLength]byte)
copy(pubKeyBytes[:], b[:])
return PubKeySM2(*pubKeyBytes), nil
}
//SignatureFromBytes 字节转为签名
func (d Driver) SignatureFromBytes(b []byte) (sig crypto.Signature, err error) {
var certSignature pkt.CertSignature
err = types.Decode(b, &certSignature)
if err != nil {
return SignatureSM2(b), nil
}
return &SignatureS{
Signature: SignatureSM2(certSignature.Signature),
uid: certSignature.Uid,
}, nil
}
//PrivKeySM2 私钥
type PrivKeySM2 [SM2PrivateKeyLength]byte
//Bytes 字节格式
func (privKey PrivKeySM2) Bytes() []byte {
s := make([]byte, SM2PrivateKeyLength)
copy(s, privKey[:])
return s
}
//Sign 签名
func (privKey PrivKeySM2) Sign(msg []byte) crypto.Signature {
priv, _ := privKeyFromBytes(sm2.P256Sm2(), privKey[:])
r, s, err := sm2.Sm2Sign(priv, msg, nil)
if err != nil {
return nil
}
//sm2不需要LowS转换
//s = ToLowS(pub, s)
return SignatureSM2(Serialize(r, s))
}
//PubKey 私钥生成公钥
func (privKey PrivKeySM2) PubKey() crypto.PubKey {
_, pub := privKeyFromBytes(sm2.P256Sm2(), privKey[:])
var pubSM2 PubKeySM2
copy(pubSM2[:], sm2.Compress(pub))
return pubSM2
}
//Equals 公钥
func (privKey PrivKeySM2) Equals(other crypto.PrivKey) bool {
if otherSecp, ok := other.(PrivKeySM2); ok {
return bytes.Equal(privKey[:], otherSecp[:])
}
return false
}
func (privKey PrivKeySM2) String() string {
return fmt.Sprintf("PrivKeySM2{*****}")
} }
const name = "auth_sm2" //PubKeySM2 公钥
const id = 258 type PubKeySM2 [SM2PublicKeyLength]byte
//Bytes 字节格式
func (pubKey PubKeySM2) Bytes() []byte {
length := SM2PublicKeyLength
if pubKey.isCompressed() {
length = SM2PublicKeyCompressed
}
s := make([]byte, length)
copy(s, pubKey[0:length])
return s
}
func (pubKey PubKeySM2) isCompressed() bool {
return pubKey[0] != pubkeyUncompressed
}
//VerifyBytes 验证字节
func (pubKey PubKeySM2) VerifyBytes(msg []byte, sig crypto.Signature) bool {
var uid []byte
if wrap, ok := sig.(*SignatureS); ok {
sig = wrap.Signature
uid = wrap.uid
}
sigSM2, ok := sig.(SignatureSM2)
if !ok {
fmt.Printf("convert failed\n")
return false
}
var pub *sm2.PublicKey
if pubKey.isCompressed() {
pub = sm2.Decompress(pubKey[0:SM2PublicKeyCompressed])
} else {
var err error
pub, err = parsePubKey(pubKey[:], sm2.P256Sm2())
if err != nil {
fmt.Printf("parse pubkey failed\n")
return false
}
}
r, s, err := Deserialize(sigSM2)
if err != nil {
fmt.Printf("unmarshal sign failed")
return false
}
//国密签名算法和ecdsa不一样,-s验签不通过,所以不需要LowS检查
//fmt.Printf("verify:%x, r:%d, s:%d\n", crypto.Sm3Hash(msg), r, s)
//lowS := IsLowS(s)
//if !lowS {
// fmt.Printf("lowS check failed")
// return false
//}
return sm2.Sm2Verify(pub, msg, uid, r, s)
}
func (pubKey PubKeySM2) String() string {
return fmt.Sprintf("PubKeySM2{%X}", pubKey[:])
}
//KeyString Must return the full bytes in hex.
// Used for map keying, etc.
func (pubKey PubKeySM2) KeyString() string {
return fmt.Sprintf("%X", pubKey[:])
}
//Equals 相等
func (pubKey PubKeySM2) Equals(other crypto.PubKey) bool {
if otherSecp, ok := other.(PubKeySM2); ok {
return bytes.Equal(pubKey[:], otherSecp[:])
}
return false
}
//SignatureSM2 签名
type SignatureSM2 []byte
//SignatureS 签名
type SignatureS struct {
crypto.Signature
uid []byte
}
//Bytes 字节格式
func (sig SignatureSM2) Bytes() []byte {
s := make([]byte, len(sig))
copy(s, sig[:])
return s
}
//IsZero 是否为0
func (sig SignatureSM2) IsZero() bool { return len(sig) == 0 }
func (sig SignatureSM2) String() string {
fingerprint := make([]byte, len(sig[:]))
copy(fingerprint, sig[:])
return fmt.Sprintf("/%X.../", fingerprint)
}
//Equals 相等
func (sig SignatureSM2) Equals(other crypto.Signature) bool {
if otherEd, ok := other.(SignatureSM2); ok {
return bytes.Equal(sig[:], otherEd[:])
}
return false
}
//const
const (
Name = "auth_sm2"
ID = 258
)
func init() { func init() {
crypto.Register(name, &sm2Driver{}, false) crypto.Register(Name, &Driver{}, false)
crypto.RegisterType(name, id) crypto.RegisterType(Name, ID)
}
func privKeyFromBytes(curve elliptic.Curve, pk []byte) (*sm2.PrivateKey, *sm2.PublicKey) {
x, y := curve.ScalarBaseMult(pk)
priv := &sm2.PrivateKey{
PublicKey: sm2.PublicKey{
Curve: curve,
X: x,
Y: y,
},
D: new(big.Int).SetBytes(pk),
}
return priv, &priv.PublicKey
} }
// 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 sm2
import (
"crypto/elliptic"
"errors"
"fmt"
"math/big"
"github.com/btcsuite/btcd/btcec"
"github.com/tjfoc/gmsm/sm2"
)
const (
pubkeyUncompressed byte = 0x4 // x coord + y coord
)
func canonicalizeInt(val *big.Int) []byte {
b := val.Bytes()
if len(b) == 0 {
b = []byte{0x00}
}
if b[0]&0x80 != 0 {
paddedBytes := make([]byte, len(b)+1)
copy(paddedBytes[1:], b)
b = paddedBytes
}
return b
}
//Serialize 序列化
func Serialize(r, s *big.Int) []byte {
rb := canonicalizeInt(r)
sb := canonicalizeInt(s)
length := 6 + len(rb) + len(sb)
b := make([]byte, length)
b[0] = 0x30
b[1] = byte(length - 2)
b[2] = 0x02
b[3] = byte(len(rb))
offset := copy(b[4:], rb) + 4
b[offset] = 0x02
b[offset+1] = byte(len(sb))
copy(b[offset+2:], sb)
return b
}
//Deserialize 反序列化
func Deserialize(sigStr []byte) (*big.Int, *big.Int, error) {
sig, err := btcec.ParseDERSignature(sigStr, sm2.P256Sm2())
if err != nil {
return nil, nil, err
}
return sig.R, sig.S, nil
}
func parsePubKey(pubKeyStr []byte, curve elliptic.Curve) (key *sm2.PublicKey, err error) {
pubkey := sm2.PublicKey{}
pubkey.Curve = curve
if len(pubKeyStr) == 0 {
return nil, errors.New("pubkey string is empty")
}
pubkey.X = new(big.Int).SetBytes(pubKeyStr[1:33])
pubkey.Y = new(big.Int).SetBytes(pubKeyStr[33:])
if pubkey.X.Cmp(pubkey.Curve.Params().P) >= 0 {
return nil, fmt.Errorf("pubkey X parameter is >= to P")
}
if pubkey.Y.Cmp(pubkey.Curve.Params().P) >= 0 {
return nil, fmt.Errorf("pubkey Y parameter is >= to P")
}
if !pubkey.Curve.IsOnCurve(pubkey.X, pubkey.Y) {
return nil, fmt.Errorf("pubkey isn't on secp256k1 curve")
}
return &pubkey, nil
}
//SerializePublicKey 公钥序列化
func SerializePublicKey(p *sm2.PublicKey, isCompress bool) []byte {
if isCompress {
return sm2.Compress(p)
}
b := make([]byte, 0, SM2PublicKeyLength)
b = append(b, pubkeyUncompressed)
b = paddedAppend(32, b, p.X.Bytes())
return paddedAppend(32, b, p.Y.Bytes())
}
//SerializePrivateKey 私钥序列化
func SerializePrivateKey(p *sm2.PrivateKey) []byte {
b := make([]byte, 0, SM2PrivateKeyLength)
return paddedAppend(SM2PrivateKeyLength, b, p.D.Bytes())
}
func paddedAppend(size uint, dst, src []byte) []byte {
for i := 0; i < int(size)-len(src); i++ {
dst = append(dst, 0)
}
return append(dst, src...)
}
...@@ -11,8 +11,6 @@ import ( ...@@ -11,8 +11,6 @@ import (
"runtime" "runtime"
"sync" "sync"
"bytes"
"github.com/33cn/chain33/common/crypto" "github.com/33cn/chain33/common/crypto"
log "github.com/33cn/chain33/common/log/log15" log "github.com/33cn/chain33/common/log/log15"
"github.com/33cn/chain33/types" "github.com/33cn/chain33/types"
...@@ -45,8 +43,6 @@ type Authority struct { ...@@ -45,8 +43,6 @@ type Authority struct {
validator core.Validator validator core.Validator
// 签名类型 // 签名类型
signType int signType int
// 有效证书缓存
validCertCache [][]byte
// 历史证书缓存 // 历史证书缓存
HistoryCertCache *HistoryCertData HistoryCertCache *HistoryCertData
} }
...@@ -91,7 +87,6 @@ func (auth *Authority) Init(conf *ty.Authority) error { ...@@ -91,7 +87,6 @@ func (auth *Authority) Init(conf *ty.Authority) error {
} }
auth.validator = vldt auth.validator = vldt
auth.validCertCache = make([][]byte, 0)
auth.HistoryCertCache = &HistoryCertData{authConfig, -1, -1} auth.HistoryCertCache = &HistoryCertData{authConfig, -1, -1}
IsAuthEnable = true IsAuthEnable = true
...@@ -139,9 +134,6 @@ func (auth *Authority) ReloadCert(store *types.HistoryCertStore) error { ...@@ -139,9 +134,6 @@ func (auth *Authority) ReloadCert(store *types.HistoryCertStore) error {
auth.validator = vldt auth.validator = vldt
} }
// 清空有效证书缓存
auth.validCertCache = auth.validCertCache[:0]
// 更新最新历史数据 // 更新最新历史数据
auth.HistoryCertCache = &HistoryCertData{auth.authConfig, store.CurHeigth, store.NxtHeight} auth.HistoryCertCache = &HistoryCertData{auth.authConfig, store.CurHeigth, store.NxtHeight}
...@@ -168,9 +160,6 @@ func (auth *Authority) ReloadCertByHeght(currentHeight int64) error { ...@@ -168,9 +160,6 @@ func (auth *Authority) ReloadCertByHeght(currentHeight int64) error {
} }
auth.validator = vldt auth.validator = vldt
// 清空有效证书缓存
auth.validCertCache = auth.validCertCache[:0]
// 更新最新历史数据 // 更新最新历史数据
auth.HistoryCertCache = &HistoryCertData{auth.authConfig, currentHeight, -1} auth.HistoryCertCache = &HistoryCertData{auth.authConfig, currentHeight, -1}
...@@ -248,11 +237,11 @@ func (auth *Authority) Validate(signature *types.Signature) error { ...@@ -248,11 +237,11 @@ func (auth *Authority) Validate(signature *types.Signature) error {
} }
// 是否在有效证书缓存中 // 是否在有效证书缓存中
for _, v := range auth.validCertCache { //for _, v := range auth.validCertCache {
if bytes.Equal(v, cert) { // if bytes.Equal(v, cert) {
return nil // return nil
} // }
} //}
// 校验 // 校验
err = auth.validator.Validate(cert, signature.GetPubkey()) err = auth.validator.Validate(cert, signature.GetPubkey())
...@@ -260,7 +249,6 @@ func (auth *Authority) Validate(signature *types.Signature) error { ...@@ -260,7 +249,6 @@ func (auth *Authority) Validate(signature *types.Signature) error {
alog.Error(fmt.Sprintf("validate cert failed. %s", err.Error())) alog.Error(fmt.Sprintf("validate cert failed. %s", err.Error()))
return fmt.Errorf("validate cert failed. error:%s", err.Error()) return fmt.Errorf("validate cert failed. error:%s", err.Error())
} }
auth.validCertCache = append(auth.validCertCache, cert)
return nil return nil
} }
......
...@@ -59,7 +59,7 @@ var SIGNTYPE = ct.AuthSM2 ...@@ -59,7 +59,7 @@ var SIGNTYPE = ct.AuthSM2
func signtx(tx *types.Transaction, priv crypto.PrivKey, cert []byte) { func signtx(tx *types.Transaction, priv crypto.PrivKey, cert []byte) {
tx.Sign(int32(SIGNTYPE), priv) tx.Sign(int32(SIGNTYPE), priv)
tx.Signature.Signature, _ = utils.EncodeCertToSignature(tx.Signature.Signature, cert) tx.Signature.Signature = utils.EncodeCertToSignature(tx.Signature.Signature, cert, nil)
} }
func signtxs(priv crypto.PrivKey, cert []byte) { func signtxs(priv crypto.PrivKey, cert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QiBCC8fKlLiayf
gxCIZxodcdzpHpzKFhlEJARmhKOPuN1yaTAKBggqgRzPVQGDdQNIADBFAiEAowXR +80blLEiRIzTyY7uYDUpP5K2RtOmfY0NKjAKBggqgRzPVQGDdQNJADBGAiEA8vh+
RYYCWcBT0gVSbHk7k+aJzG3uRdORTbbvmLgbG2QCIF3e0/m0aNRlvF6gPxBJ+JBR 3joELxPxq0n1h07XFGeEnmpxutVoIocuky2HkF4CIQDnWIavlpJOq3tU76cmn3ur
R0sbv9eyrSEFMwx/ZyGJ KQeyi9GM7Uoi25S1QIxu9A==
-----END CERTIFICATE----- -----END CERTIFICATE-----
-----BEGIN PRIVATE KEY-----
MIGTAgEAMBMGByqGSM49AgEGCCqBHM9VAYItBHkwdwIBAQQg86AAL0bRgFW6RhFX
no7CVphI1U2csfrjwPuYn3FXaF2gCgYIKoEcz1UBgi2hRANCAASR8Yb//+y/GMLy
D36FLLO80oxUPtD6AtVoh9UIuC1b0QzA4+zkUDUk3zwdZ1pMZZKGZ48vE6KtAcFB
uqU7L784
-----END PRIVATE KEY-----
-----BEGIN PRIVATE KEY-----
MIGTAgEAMBMGByqGSM49AgEGCCqBHM9VAYItBHkwdwIBAQQg4Ork9oT6d6CRxg0f
EbHlr5eQPUcHWniEgRhDCi2dA/GgCgYIKoEcz1UBgi2hRANCAAQqXuEWh+sW/YtP
FlHmxiFhYi0o3Tb8He9NAaJ6uKe+OF5/eXa+VmRrKKGeE+dG8LrMiJ5+AlIj+ryd
blX5UKZ8
-----END PRIVATE KEY-----
-----BEGIN CERTIFICATE----- -----BEGIN CERTIFICATE-----
MIIB4zCCAYmgAwIBAgIQdKBE3pdDBMaadMbZ30K7aTAKBggqgRzPVQGDdTBHMQsw MIIB4zCCAYmgAwIBAgIQVs0txvOG+iVu/oISaV2KyzAKBggqgRzPVQGDdTBHMQsw
CQYDVQQGEwJVUzETMBEGA1UECBMKQ2FsaWZvcm5pYTEWMBQGA1UEBxMNU2FuIEZy CQYDVQQGEwJV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CqTw8J6As69MFO10XixWHxH
Nm+S1txiqA== qrTJ9LnI3g==
-----END CERTIFICATE----- -----END CERTIFICATE-----
...@@ -11,13 +11,11 @@ import ( ...@@ -11,13 +11,11 @@ import (
"crypto/x509" "crypto/x509"
"encoding/hex" "encoding/hex"
"encoding/pem" "encoding/pem"
"github.com/33cn/chain33/types"
"math/big" "math/big"
"encoding/asn1"
"fmt" "fmt"
"github.com/33cn/chain33/common/crypto"
sm2_util "github.com/33cn/chain33/system/crypto/sm2" sm2_util "github.com/33cn/chain33/system/crypto/sm2"
ecdsa_util "github.com/33cn/plugin/plugin/crypto/ecdsa" ecdsa_util "github.com/33cn/plugin/plugin/crypto/ecdsa"
ty "github.com/33cn/plugin/plugin/dapp/cert/types" ty "github.com/33cn/plugin/plugin/dapp/cert/types"
...@@ -65,22 +63,23 @@ func GetPublicKeySKIFromCert(cert []byte, signType int) (string, error) { ...@@ -65,22 +63,23 @@ func GetPublicKeySKIFromCert(cert []byte, signType int) (string, error) {
} }
// EncodeCertToSignature 证书编码进签名 // EncodeCertToSignature 证书编码进签名
func EncodeCertToSignature(signByte []byte, cert []byte) ([]byte, error) { func EncodeCertToSignature(signByte []byte, cert []byte, uid []byte) []byte {
certSign := crypto.CertSignature{} var certSign ty.CertSignature
certSign.Signature = append(certSign.Signature, signByte...) certSign.Signature = append(certSign.Signature, signByte...)
certSign.Cert = append(certSign.Cert, cert...) certSign.Cert = append(certSign.Cert, cert...)
return asn1.Marshal(certSign) certSign.Uid = append(certSign.Uid, uid...)
return types.Encode(&certSign)
} }
// DecodeCertFromSignature 从签名中解码证书 // DecodeCertFromSignature 从签名中解码证书
func DecodeCertFromSignature(signByte []byte) ([]byte, []byte, error) { func DecodeCertFromSignature(signByte []byte) (*ty.CertSignature, error) {
var certSignature crypto.CertSignature var certSign ty.CertSignature
_, err := asn1.Unmarshal(signByte, &certSignature) err := types.Decode(signByte, &certSign)
if err != nil { if err != nil {
return nil, nil, err return nil, err
} }
return certSignature.Cert, certSignature.Signature, nil return &certSign, nil
} }
// PrivKeyByteFromRaw pem结构转成byte类型私钥 // PrivKeyByteFromRaw pem结构转成byte类型私钥
......
...@@ -38,3 +38,9 @@ message Authority { ...@@ -38,3 +38,9 @@ message Authority {
string cryptoPath = 2; string cryptoPath = 2;
string signType = 3; string signType = 3;
} }
message CertSignature {
bytes signature = 1;
bytes cert = 2;
bytes uid = 3;
}
\ No newline at end of file
...@@ -5,9 +5,8 @@ package types ...@@ -5,9 +5,8 @@ package types
import ( import (
fmt "fmt" fmt "fmt"
math "math"
proto "github.com/golang/protobuf/proto" proto "github.com/golang/protobuf/proto"
math "math"
) )
// Reference imports to suppress errors if they are not otherwise used. // Reference imports to suppress errors if they are not otherwise used.
...@@ -383,6 +382,61 @@ func (m *Authority) GetSignType() string { ...@@ -383,6 +382,61 @@ func (m *Authority) GetSignType() string {
return "" return ""
} }
type CertSignature struct {
Signature []byte `protobuf:"bytes,1,opt,name=signature,proto3" json:"signature,omitempty"`
Cert []byte `protobuf:"bytes,2,opt,name=cert,proto3" json:"cert,omitempty"`
Uid []byte `protobuf:"bytes,3,opt,name=uid,proto3" json:"uid,omitempty"`
XXX_NoUnkeyedLiteral struct{} `json:"-"`
XXX_unrecognized []byte `json:"-"`
XXX_sizecache int32 `json:"-"`
}
func (m *CertSignature) Reset() { *m = CertSignature{} }
func (m *CertSignature) String() string { return proto.CompactTextString(m) }
func (*CertSignature) ProtoMessage() {}
func (*CertSignature) Descriptor() ([]byte, []int) {
return fileDescriptor_a142e29cbef9b1cf, []int{6}
}
func (m *CertSignature) XXX_Unmarshal(b []byte) error {
return xxx_messageInfo_CertSignature.Unmarshal(m, b)
}
func (m *CertSignature) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) {
return xxx_messageInfo_CertSignature.Marshal(b, m, deterministic)
}
func (m *CertSignature) XXX_Merge(src proto.Message) {
xxx_messageInfo_CertSignature.Merge(m, src)
}
func (m *CertSignature) XXX_Size() int {
return xxx_messageInfo_CertSignature.Size(m)
}
func (m *CertSignature) XXX_DiscardUnknown() {
xxx_messageInfo_CertSignature.DiscardUnknown(m)
}
var xxx_messageInfo_CertSignature proto.InternalMessageInfo
func (m *CertSignature) GetSignature() []byte {
if m != nil {
return m.Signature
}
return nil
}
func (m *CertSignature) GetCert() []byte {
if m != nil {
return m.Cert
}
return nil
}
func (m *CertSignature) GetUid() []byte {
if m != nil {
return m.Uid
}
return nil
}
func init() { func init() {
proto.RegisterType((*Cert)(nil), "types.Cert") proto.RegisterType((*Cert)(nil), "types.Cert")
proto.RegisterType((*CertAction)(nil), "types.CertAction") proto.RegisterType((*CertAction)(nil), "types.CertAction")
...@@ -390,31 +444,33 @@ func init() { ...@@ -390,31 +444,33 @@ func init() {
proto.RegisterType((*CertUpdate)(nil), "types.CertUpdate") proto.RegisterType((*CertUpdate)(nil), "types.CertUpdate")
proto.RegisterType((*CertNormal)(nil), "types.CertNormal") proto.RegisterType((*CertNormal)(nil), "types.CertNormal")
proto.RegisterType((*Authority)(nil), "types.Authority") proto.RegisterType((*Authority)(nil), "types.Authority")
proto.RegisterType((*CertSignature)(nil), "types.CertSignature")
} }
func init() { func init() { proto.RegisterFile("cert.proto", fileDescriptor_a142e29cbef9b1cf) }
proto.RegisterFile("cert.proto", fileDescriptor_a142e29cbef9b1cf)
}
var fileDescriptor_a142e29cbef9b1cf = []byte{ var fileDescriptor_a142e29cbef9b1cf = []byte{
// 300 bytes of a gzipped FileDescriptorProto // 340 bytes of a gzipped FileDescriptorProto
0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x94, 0x52, 0xcd, 0x4a, 0xf3, 0x40, 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x94, 0x52, 0xdd, 0x4a, 0xf3, 0x40,
0x14, 0xed, 0x24, 0xfd, 0xbd, 0xfd, 0x28, 0x9f, 0x83, 0x48, 0x70, 0x21, 0x25, 0xab, 0x82, 0x10, 0x10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0x08, 0xfc, 0x65, 0x7c, 0x15,
0x18, 0x83, 0xba, 0x39, 0x13, 0x6f, 0xd2, 0xbe, 0x4d, 0x96, 0x32, 0x5b, 0xd9, 0x9f, 0xbb, 0xdc,
0x3c, 0x17, 0x4d, 0x7b, 0x70, 0x37, 0x1f, 0x01, 0x00, 0x00, 0xff, 0xff, 0x97, 0xaa, 0x8b, 0x1d,
0x7e, 0x02, 0x00, 0x00,
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment