diff --git a/pkg/infra/assembler.go b/pkg/infra/assembler.go index 0778c9ed..2af52902 100644 --- a/pkg/infra/assembler.go +++ b/pkg/infra/assembler.go @@ -17,7 +17,7 @@ type Elements struct { } type Assembler struct { - Signer *Crypto + Signer Crypto } func (a *Assembler) assemble(e *Elements) (*Elements, error) { diff --git a/pkg/infra/config.go b/pkg/infra/config.go index 73865626..fa77f95e 100644 --- a/pkg/infra/config.go +++ b/pkg/infra/config.go @@ -1,7 +1,11 @@ package infra import ( + "crypto/ecdsa" + "crypto/x509" + "encoding/pem" "io/ioutil" + "tape/internal/fabric/bccsp/utils" "github.com/gogo/protobuf/proto" "github.com/hyperledger/fabric-protos-go/msp" @@ -65,7 +69,7 @@ func LoadConfig(f string) (Config, error) { return config, nil } -func (c Config) LoadCrypto() (*Crypto, error) { +func (c Config) LoadCrypto() (*CryptoImpl, error) { var allcerts []string for _, p := range c.Endorsers { allcerts = append(allcerts, p.TLSCACert) @@ -98,7 +102,7 @@ func (c Config) LoadCrypto() (*Crypto, error) { return nil, errors.Wrapf(err, "error get msp id") } - return &Crypto{ + return &CryptoImpl{ Creator: name, PrivKey: priv, SignCert: cert, @@ -136,3 +140,34 @@ func (n *Node) loadConfig() error { n.TLSCARootByte = TLSCARoot return nil } + +func GetPrivateKey(f string) (*ecdsa.PrivateKey, error) { + in, err := ioutil.ReadFile(f) + if err != nil { + return nil, err + } + + k, err := utils.PEMtoPrivateKey(in, []byte{}) + if err != nil { + return nil, err + } + + key, ok := k.(*ecdsa.PrivateKey) + if !ok { + return nil, errors.Errorf("expecting ecdsa key") + } + + return key, nil +} + +func GetCertificate(f string) (*x509.Certificate, []byte, error) { + in, err := ioutil.ReadFile(f) + if err != nil { + return nil, nil, err + } + + block, _ := pem.Decode(in) + + c, err := x509.ParseCertificate(block.Bytes) + return c, in, err +} diff --git a/pkg/infra/crypto.go b/pkg/infra/crypto.go index 6b9e7ad2..8827e791 100644 --- a/pkg/infra/crypto.go +++ b/pkg/infra/crypto.go @@ -6,16 +6,12 @@ import ( "crypto/sha256" "crypto/x509" "encoding/asn1" - "encoding/base64" - "encoding/pem" - "io/ioutil" "math/big" "tape/internal/fabric/bccsp/utils" "tape/internal/fabric/common/crypto" "github.com/hyperledger/fabric-protos-go/common" - "github.com/pkg/errors" ) type CryptoConfig struct { @@ -29,13 +25,13 @@ type ECDSASignature struct { R, S *big.Int } -type Crypto struct { +type CryptoImpl struct { Creator []byte PrivKey *ecdsa.PrivateKey SignCert *x509.Certificate } -func (s *Crypto) Sign(message []byte) ([]byte, error) { +func (s *CryptoImpl) Sign(message []byte) ([]byte, error) { ri, si, err := ecdsa.Sign(rand.Reader, s.PrivKey, digest(message)) if err != nil { return nil, err @@ -49,11 +45,11 @@ func (s *Crypto) Sign(message []byte) ([]byte, error) { return asn1.Marshal(ECDSASignature{ri, si}) } -func (s *Crypto) Serialize() ([]byte, error) { +func (s *CryptoImpl) Serialize() ([]byte, error) { return s.Creator, nil } -func (s *Crypto) NewSignatureHeader() (*common.SignatureHeader, error) { +func (s *CryptoImpl) NewSignatureHeader() (*common.SignatureHeader, error) { creator, err := s.Serialize() if err != nil { return nil, err @@ -74,43 +70,3 @@ func digest(in []byte) []byte { h.Write(in) return h.Sum(nil) } - -func toPEM(in []byte) ([]byte, error) { - d := make([]byte, base64.StdEncoding.DecodedLen(len(in))) - n, err := base64.StdEncoding.Decode(d, in) - if err != nil { - return nil, err - } - return d[:n], nil -} - -func GetPrivateKey(f string) (*ecdsa.PrivateKey, error) { - in, err := ioutil.ReadFile(f) - if err != nil { - return nil, err - } - - k, err := utils.PEMtoPrivateKey(in, []byte{}) - if err != nil { - return nil, err - } - - key, ok := k.(*ecdsa.PrivateKey) - if !ok { - return nil, errors.Errorf("expecting ecdsa key") - } - - return key, nil -} - -func GetCertificate(f string) (*x509.Certificate, []byte, error) { - in, err := ioutil.ReadFile(f) - if err != nil { - return nil, nil, err - } - - block, _ := pem.Decode(in) - - c, err := x509.ParseCertificate(block.Bytes) - return c, in, err -} diff --git a/pkg/infra/initiator.go b/pkg/infra/initiator.go index 9fe13c5f..eaadbd9b 100644 --- a/pkg/infra/initiator.go +++ b/pkg/infra/initiator.go @@ -7,7 +7,7 @@ import ( "golang.org/x/time/rate" ) -func StartCreateProposal(num int, burst int, r float64, config Config, crypto *Crypto, raw chan *Elements, errorCh chan error) { +func StartCreateProposal(num int, burst int, r float64, config Config, crypto Crypto, raw chan *Elements, errorCh chan error) { limit := rate.Inf ctx := context.Background() if r > 0 { diff --git a/pkg/infra/interface.go b/pkg/infra/interface.go new file mode 100644 index 00000000..75b14a11 --- /dev/null +++ b/pkg/infra/interface.go @@ -0,0 +1,14 @@ +package infra + +import ( + "github.com/hyperledger/fabric-protos-go/common" +) + +//to do for #127 SM crypto +//just need to do an impl for this interface and replace + +type Crypto interface { + NewSignatureHeader() (*common.SignatureHeader, error) + Serialize() ([]byte, error) + Sign(message []byte) ([]byte, error) +} diff --git a/pkg/infra/observer.go b/pkg/infra/observer.go index 9ae8cfc8..bb53dd04 100644 --- a/pkg/infra/observer.go +++ b/pkg/infra/observer.go @@ -20,7 +20,7 @@ type Observer struct { logger *log.Logger } -func CreateObservers(ctx context.Context, channel string, nodes []Node, crypto *Crypto, logger *log.Logger) (*Observers, error) { +func CreateObservers(ctx context.Context, channel string, nodes []Node, crypto Crypto, logger *log.Logger) (*Observers, error) { var workers []*Observer for i, node := range nodes { worker, err := CreateObserver(ctx, channel, node, crypto, logger) @@ -39,7 +39,7 @@ func (o *Observers) Start(errorCh chan error, blockCh chan<- *AddressedBlock, no } } -func CreateObserver(ctx context.Context, channel string, node Node, crypto *Crypto, logger *log.Logger) (*Observer, error) { +func CreateObserver(ctx context.Context, channel string, node Node, crypto Crypto, logger *log.Logger) (*Observer, error) { seek, err := CreateSignedDeliverNewestEnv(channel, crypto) if err != nil { return nil, err diff --git a/pkg/infra/proposal.go b/pkg/infra/proposal.go index 4bec270e..ec1b1b51 100644 --- a/pkg/infra/proposal.go +++ b/pkg/infra/proposal.go @@ -13,7 +13,7 @@ import ( "github.com/pkg/errors" ) -func CreateProposal(signer *Crypto, channel, ccname, version string, args ...string) (*peer.Proposal, error) { +func CreateProposal(signer Crypto, channel, ccname, version string, args ...string) (*peer.Proposal, error) { var argsInByte [][]byte for _, arg := range args { argsInByte = append(argsInByte, []byte(arg)) @@ -40,7 +40,7 @@ func CreateProposal(signer *Crypto, channel, ccname, version string, args ...str return prop, nil } -func SignProposal(prop *peer.Proposal, signer *Crypto) (*peer.SignedProposal, error) { +func SignProposal(prop *peer.Proposal, signer Crypto) (*peer.SignedProposal, error) { propBytes, err := proto.Marshal(prop) if err != nil { return nil, err @@ -54,7 +54,7 @@ func SignProposal(prop *peer.Proposal, signer *Crypto) (*peer.SignedProposal, er return &peer.SignedProposal{ProposalBytes: propBytes, Signature: sig}, nil } -func CreateSignedTx(proposal *peer.Proposal, signer *Crypto, resps []*peer.ProposalResponse) (*common.Envelope, error) { +func CreateSignedTx(proposal *peer.Proposal, signer Crypto, resps []*peer.ProposalResponse) (*common.Envelope, error) { if len(resps) == 0 { return nil, errors.Errorf("at least one proposal response is required") } @@ -152,7 +152,7 @@ func CreateSignedTx(proposal *peer.Proposal, signer *Crypto, resps []*peer.Propo return &common.Envelope{Payload: paylBytes, Signature: sig}, nil } -func CreateSignedDeliverNewestEnv(ch string, signer *Crypto) (*common.Envelope, error) { +func CreateSignedDeliverNewestEnv(ch string, signer Crypto) (*common.Envelope, error) { start := &orderer.SeekPosition{ Type: &orderer.SeekPosition_Newest{ Newest: &orderer.SeekNewest{},