github.com/hxx258456/fabric-ca-gm@v0.0.3-0.20221111064038-a268ad7e3a37/lib/client/credential/x509/signer.go (about) 1 /* 2 Copyright IBM Corp. All Rights Reserved. 3 4 SPDX-License-Identifier: Apache-2.0 5 */ 6 7 package x509 8 9 import ( 10 "github.com/hxx258456/ccgo/x509" 11 12 "github.com/hxx258456/fabric-ca-gm/internal/pkg/util" 13 "github.com/hxx258456/fabric-ca-gm/lib/attrmgr" 14 "github.com/hxx258456/fabric-gm/bccsp" 15 "github.com/pkg/errors" 16 ) 17 18 // NewSigner is constructor for Signer 19 func NewSigner(key bccsp.Key, cert []byte) (*Signer, error) { 20 s := &Signer{ 21 key: key, 22 certBytes: cert, 23 } 24 var err error 25 s.cert, err = util.GetX509CertificateFromPEM(s.certBytes) 26 if err != nil { 27 return nil, errors.WithMessage(err, "Failed to unmarshal X509 certificate bytes") 28 } 29 s.name = util.GetEnrollmentIDFromX509Certificate(s.cert) 30 return s, nil 31 } 32 33 // Signer represents a signer 34 // Each identity may have multiple signers and currently one ecert 35 // TODO 是否需要将x509改为国密x509? 36 type Signer struct { 37 // Private key 38 key bccsp.Key 39 // Certificate bytes 40 certBytes []byte 41 // X509 certificate that is constructed from the cert bytes associated with this signer 42 cert *x509.Certificate 43 // Common name from the certificate associated with this signer 44 name string 45 } 46 47 // Key returns the key bytes of this signer 48 func (s *Signer) Key() bccsp.Key { 49 return s.key 50 } 51 52 // Cert returns the cert bytes of this signer 53 func (s *Signer) Cert() []byte { 54 return s.certBytes 55 } 56 57 // GetX509Cert returns the X509 certificate for this signer 58 func (s *Signer) GetX509Cert() *x509.Certificate { 59 return s.cert 60 } 61 62 // GetName returns common name that is retrieved from the Subject of the certificate 63 // associated with this signer 64 func (s *Signer) GetName() string { 65 return s.name 66 } 67 68 // Attributes returns the attributes that are in the certificate 69 func (s *Signer) Attributes() (*attrmgr.Attributes, error) { 70 cert := s.GetX509Cert() 71 // TODO 将x509证书转为sm2证书 72 // sm2Cert := sw.ParseX509Certificate2Sm2(cert) 73 attrs, err := attrmgr.New().GetAttributesFromCert(cert) 74 if err != nil { 75 return nil, errors.Errorf("Failed getting attributes for '%s': %s", s.name, err) 76 } 77 return attrs, nil 78 }