github.com/lzy4123/fabric@v2.1.1+incompatible/bccsp/idemix/bridge/issuer.go (about) 1 /* 2 Copyright IBM Corp. All Rights Reserved. 3 4 SPDX-License-Identifier: Apache-2.0 5 */ 6 package bridge 7 8 import ( 9 "fmt" 10 11 "github.com/golang/protobuf/proto" 12 "github.com/hyperledger/fabric-amcl/amcl" 13 "github.com/hyperledger/fabric/bccsp" 14 "github.com/hyperledger/fabric/bccsp/idemix/handlers" 15 cryptolib "github.com/hyperledger/fabric/idemix" 16 "github.com/pkg/errors" 17 ) 18 19 // IssuerPublicKey encapsulate an idemix issuer public key. 20 type IssuerPublicKey struct { 21 PK *cryptolib.IssuerPublicKey 22 } 23 24 func (o *IssuerPublicKey) Bytes() ([]byte, error) { 25 return proto.Marshal(o.PK) 26 } 27 28 func (o *IssuerPublicKey) Hash() []byte { 29 return o.PK.Hash 30 } 31 32 // IssuerPublicKey encapsulate an idemix issuer secret key. 33 type IssuerSecretKey struct { 34 SK *cryptolib.IssuerKey 35 } 36 37 func (o *IssuerSecretKey) Bytes() ([]byte, error) { 38 return proto.Marshal(o.SK) 39 } 40 41 func (o *IssuerSecretKey) Public() handlers.IssuerPublicKey { 42 return &IssuerPublicKey{o.SK.Ipk} 43 } 44 45 // Issuer encapsulates the idemix algorithms to generate issuer key-pairs 46 type Issuer struct { 47 NewRand func() *amcl.RAND 48 } 49 50 // NewKey generates a new issuer key-pair 51 func (i *Issuer) NewKey(attributeNames []string) (res handlers.IssuerSecretKey, err error) { 52 defer func() { 53 if r := recover(); r != nil { 54 res = nil 55 err = errors.Errorf("failure [%s]", r) 56 } 57 }() 58 59 sk, err := cryptolib.NewIssuerKey(attributeNames, i.NewRand()) 60 if err != nil { 61 return 62 } 63 64 res = &IssuerSecretKey{SK: sk} 65 66 return 67 } 68 69 func (*Issuer) NewPublicKeyFromBytes(raw []byte, attributes []string) (res handlers.IssuerPublicKey, err error) { 70 defer func() { 71 if r := recover(); r != nil { 72 res = nil 73 err = errors.Errorf("failure [%s]", r) 74 } 75 }() 76 77 ipk := new(cryptolib.IssuerPublicKey) 78 err = proto.Unmarshal(raw, ipk) 79 if err != nil { 80 return nil, errors.WithStack(&bccsp.IdemixIssuerPublicKeyImporterError{ 81 Type: bccsp.IdemixIssuerPublicKeyImporterUnmarshallingError, 82 ErrorMsg: "failed to unmarshal issuer public key", 83 Cause: err}) 84 } 85 86 err = ipk.SetHash() 87 if err != nil { 88 return nil, errors.WithStack(&bccsp.IdemixIssuerPublicKeyImporterError{ 89 Type: bccsp.IdemixIssuerPublicKeyImporterHashError, 90 ErrorMsg: "setting the hash of the issuer public key failed", 91 Cause: err}) 92 } 93 94 err = ipk.Check() 95 if err != nil { 96 return nil, errors.WithStack(&bccsp.IdemixIssuerPublicKeyImporterError{ 97 Type: bccsp.IdemixIssuerPublicKeyImporterValidationError, 98 ErrorMsg: "invalid issuer public key", 99 Cause: err}) 100 } 101 102 if len(attributes) != 0 { 103 // Check the attributes 104 if len(attributes) != len(ipk.AttributeNames) { 105 return nil, errors.WithStack(&bccsp.IdemixIssuerPublicKeyImporterError{ 106 Type: bccsp.IdemixIssuerPublicKeyImporterNumAttributesError, 107 ErrorMsg: fmt.Sprintf("invalid number of attributes, expected [%d], got [%d]", 108 len(ipk.AttributeNames), len(attributes)), 109 }) 110 } 111 112 for i, attr := range attributes { 113 if ipk.AttributeNames[i] != attr { 114 return nil, errors.WithStack(&bccsp.IdemixIssuerPublicKeyImporterError{ 115 Type: bccsp.IdemixIssuerPublicKeyImporterAttributeNameError, 116 ErrorMsg: fmt.Sprintf("invalid attribute name at position [%d]", i), 117 }) 118 } 119 } 120 } 121 122 res = &IssuerPublicKey{PK: ipk} 123 124 return 125 }