github.com/s7techlab/cckit@v0.10.5/identity/cert_identity.go (about) 1 package identity 2 3 import ( 4 "crypto/x509" 5 "encoding/pem" 6 "fmt" 7 "time" 8 9 "github.com/golang/protobuf/proto" 10 "github.com/hyperledger/fabric-chaincode-go/pkg/cid" 11 "github.com/hyperledger/fabric-chaincode-go/shim" 12 protomsp "github.com/hyperledger/fabric-protos-go/msp" 13 "github.com/hyperledger/fabric/msp" 14 ) 15 16 // New creates CertIdentity struct from an mspID and certificate 17 func New(mspID string, certPEM []byte) (ci *CertIdentity, err error) { 18 cert, err := Certificate(certPEM) 19 if err != nil { 20 return nil, err 21 } 22 return &CertIdentity{mspID, cert}, nil 23 } 24 25 // FromStub creates Identity interface from tx creator mspID and certificate (stub.GetCreator) 26 func FromStub(stub shim.ChaincodeStubInterface) (*CertIdentity, error) { 27 clientIdentity, err := cid.New(stub) 28 if err != nil { 29 return nil, fmt.Errorf(`client identity from stub: %w`, err) 30 } 31 mspID, err := clientIdentity.GetMSPID() 32 if err != nil { 33 return nil, err 34 } 35 cert, err := clientIdentity.GetX509Certificate() 36 if err != nil { 37 return nil, err 38 } 39 return &CertIdentity{mspID, cert}, nil 40 } 41 42 // FromSerialized converts msp.SerializedIdentity struct to Identity interface{} 43 func FromSerialized(s protomsp.SerializedIdentity) (ci *CertIdentity, err error) { 44 return New(s.Mspid, s.IdBytes) 45 } 46 47 // CertIdentity structs holds data of tx creator 48 type CertIdentity struct { 49 MspID string 50 Cert *x509.Certificate 51 } 52 53 // GetID get id based in certificate subject and issuer 54 func (ci CertIdentity) GetID() string { 55 return IDByCert(ci.Cert) 56 } 57 58 func (ci CertIdentity) ExpiresAt() time.Time { 59 return ci.Cert.NotAfter 60 } 61 62 func (ci CertIdentity) GetMSPIdentifier() string { 63 return ci.MspID 64 } 65 66 func (ci CertIdentity) GetIdentifier() *msp.IdentityIdentifier { 67 return &msp.IdentityIdentifier{ 68 Mspid: ci.MspID, 69 Id: ci.GetID(), 70 } 71 } 72 73 func (ci CertIdentity) Validate() error { 74 return nil 75 } 76 77 func (ci CertIdentity) Verify(msg []byte, sig []byte) error { 78 return nil 79 } 80 81 func (ci CertIdentity) Anonymous() bool { 82 return false 83 } 84 85 func (ci CertIdentity) GetOrganizationalUnits() []*msp.OUIdentifier { 86 return nil 87 } 88 89 // GetSubject returns invoker's certificate subject 90 func (ci CertIdentity) GetSubject() string { 91 return GetDN(&ci.Cert.Subject) 92 } 93 94 // GetIssuer returns invoker's certificate issuer 95 func (ci CertIdentity) GetIssuer() string { 96 return GetDN(&ci.Cert.Issuer) 97 } 98 99 func (ci CertIdentity) GetPublicKey() interface{} { 100 return ci.Cert.PublicKey 101 } 102 103 // GetPEM certificate encoded to PEM 104 func (ci CertIdentity) GetPEM() []byte { 105 return pem.EncodeToMemory(&pem.Block{ 106 Type: `CERTIFICATE`, 107 Bytes: ci.Cert.Raw, 108 }) 109 } 110 111 // ToSerialized converts CertIdentity to *msp.SerializedIdentity 112 func (ci CertIdentity) ToSerialized() *protomsp.SerializedIdentity { 113 return &protomsp.SerializedIdentity{ 114 Mspid: ci.MspID, 115 IdBytes: ci.GetPEM(), 116 } 117 } 118 119 func (ci CertIdentity) Serialize() ([]byte, error) { 120 return ci.ToBytes() 121 } 122 123 // ToBytes converts to serializedIdentity and then to json 124 func (ci CertIdentity) ToBytes() ([]byte, error) { 125 return proto.Marshal(ci.ToSerialized()) 126 } 127 128 func (ci CertIdentity) SatisfiesPrincipal(principal *protomsp.MSPPrincipal) error { 129 return nil 130 } 131 132 func (ci CertIdentity) Sign(msg []byte) ([]byte, error) { 133 return nil, nil 134 } 135 136 func (ci CertIdentity) GetPublicVersion() msp.Identity { 137 return nil 138 }