MSP接口实现,即bccspmsp结构体及方法,bccspmsp定义如下:
type bccspmsp struct { rootCerts []Identity //信任的CA证书列表 intermediateCerts []Identity //信任的中间证书列表 tlsRootCerts [][]byte //信任的CA TLS 证书列表 tlsIntermediateCerts [][]byte //信任的中间TLS 证书列表 certificationTreeInternalNodesMap map[string]bool //待定 signer SigningIdentity //签名身份 admins []Identity //管理身份列表 bccsp bccsp.BCCSP //加密服务提供者 name string //MSP名字 opts *x509.VerifyOptions //MSP成员验证选项 CRL []*pkix.CertificateList //证书吊销列表 ouIdentifiers map[string][][]byte //组织列表 cryptoConfig *m.FabricCryptoConfig //加密选项}//代码在msp/mspimpl.go
涉及方法如下:
func NewBccspMsp() (MSP, error) //创建bccsp实例,以及创建并初始化bccspmsp实例func (msp *bccspmsp) Setup(conf1 *m.MSPConfig) error 根据MSPConfig设置MSP实例func (msp *bccspmsp) GetType() ProviderType //获取MSP类型,即FABRICfunc (msp *bccspmsp) GetIdentifier() (string, error) //获取MSP名字func (msp *bccspmsp) GetTLSRootCerts() [][]byte //获取信任的CA TLS 证书列表msp.tlsRootCertsfunc (msp *bccspmsp) GetTLSIntermediateCerts() [][]byte //获取信任的中间TLS 证书列表msp.tlsIntermediateCertsfunc (msp *bccspmsp) GetDefaultSigningIdentity() (SigningIdentity, error) 获取默认的签名身份msp.signerfunc (msp *bccspmsp) GetSigningIdentity(identifier *IdentityIdentifier) (SigningIdentity, error) //暂未实现,可忽略func (msp *bccspmsp) Validate(id Identity) error //校验身份是否有效,调取msp.validateIdentity(id)实现func (msp *bccspmsp) DeserializeIdentity(serializedID []byte) (Identity, error) //身份反序列化func (msp *bccspmsp) SatisfiesPrincipal(id Identity, principal *m.MSPPrincipal) error //验证给定的身份与principal中所描述的类型是否相匹配//代码在msp/mspimpl.go
func (msp *bccspmsp) Setup(conf1 *m.MSPConfig) error代码如下:
conf := &m.FabricMSPConfig{}err := proto.Unmarshal(conf1.Config, conf) //将conf1.Config []byte解码为FabricMSPConfigmsp.name = conf.Name err := msp.setupCrypto(conf) //设置加密选项msp.cryptoConfigerr := msp.setupCAs(conf) //设置MSP成员验证选项msp.opts,并添加信任的CA证书msp.rootCerts和信任的中间证书msp.intermediateCertserr := msp.setupAdmins(conf) //设置管理身份列表msp.adminserr := msp.setupCRLs(conf) //设置证书吊销列表msp.CRLerr := msp.finalizeSetupCAs(conf); err != nil //设置msp.certificationTreeInternalNodesMaperr := msp.setupSigningIdentity(conf) //设置签名身份msp.signererr := msp.setupOUs(conf) //设置组织列表msp.ouIdentifierserr := msp.setupTLSCAs(conf) //设置并添加信任的CA TLS 证书列表msp.tlsRootCerts,以及信任的CA TLS 证书列表msp.tlsIntermediateCertsfor i, admin := range msp.admins { err = admin.Validate() //确保管理员是有效的成员}//代码在msp/mspimpl.go
func (msp *bccspmsp) validateIdentity(id *identity)代码如下:
validationChain, err := msp.getCertificationChainForBCCSPIdentity(id) //获取BCCSP身份认证链err = msp.validateIdentityAgainstChain(id, validationChain) //根据链验证身份err = msp.validateIdentityOUs(id) //验证身份中所携带的组织信息有效//代码在msp/mspimpl.go
结构体定义:
type mspManagerImpl struct { mspsMap map[string]MSP //MSP的映射 up bool //是否正常启用}//代码在msp/mspmgrimpl.go
方法:
func NewMSPManager() MSPManager //创建mspManagerImpl实例func (mgr *mspManagerImpl) Setup(msps []MSP) error //将msps装入mgr.mspsMapfunc (mgr *mspManagerImpl) GetMSPs() (map[string]MSP, error) //获取mgr.mspsMapfunc (mgr *mspManagerImpl) DeserializeIdentity(serializedID []byte) (Identity, error) //调用msp.DeserializeIdentity()实现身份反序列化//代码在msp/mspmgrimpl.go
identity结构体定义(身份):
type identity struct { id *IdentityIdentifier //身份标识符(含Mspid和Id,均为string) cert *x509.Certificate //代表身份的x509证书 pk bccsp.Key //身份公钥 msp *bccspmsp //拥有此实例的MSP实例}//代码在msp/identities.go
补充IdentityIdentifier结构体定义(身份标识符):
type IdentityIdentifier struct { Mspid string //Msp id Id string //Id}//代码在msp/msp.go
identity结构体涉及方法如下:
func newIdentity(id *IdentityIdentifier, cert *x509.Certificate, pk bccsp.Key, msp *bccspmsp) (Identity, error) //创建identity实例func NewSerializedIdentity(mspID string, certPEM []byte) ([]byte, error) //新建身份SerializedIdentity并序列化func (id *identity) SatisfiesPrincipal(principal *msp.MSPPrincipal) error //调用msp的SatisfiesPrincipal检查身份与principal中所描述的类型是否匹配func (id *identity) GetIdentifier() *IdentityIdentifier //获取id.idfunc (id *identity) GetMSPIdentifier() string //获取id.id.Mspidfunc (id *identity) Validate() error //调取id.msp.Validate(id)校验身份是否有效func (id *identity) GetOrganizationalUnits() []*OUIdentifier //获取组织单元func (id *identity) Verify(msg []byte, sig []byte) error //用这个身份校验消息签名func (id *identity) Serialize() ([]byte, error)//身份序列化func (id *identity) getHashOpt(hashFamily string) (bccsp.HashOpts, error) //调取bccsp.GetHashOpt//代码在msp/identities.go
signingidentity结构体定义(签名身份):
type signingidentity struct { identity //嵌入identity signer crypto.Signer //crypto标准库中Signer接口}//代码在msp/identities.go
signingidentity结构体涉及方法如下:
//新建signingidentity实例func newSigningIdentity(id *IdentityIdentifier, cert *x509.Certificate, pk bccsp.Key, signer crypto.Signer, msp *bccspmsp) (SigningIdentity, error) func (id *signingidentity) Sign(msg []byte) ([]byte, error) //签名msgfunc (id *signingidentity) GetPublicVersion() Identity //获取id.identity//代码在msp/identities.go
MSPConfig相关结构体定义:
FabricMSPConfig定义与bccspmsp接近,FabricMSPConfig序列化后以[]byte存入MSPConfig.Config中。
type MSPConfig struct { Type int32 Config []byte}type FabricMSPConfig struct { Name string //MSP名字 RootCerts [][]byte //信任的CA证书列表 IntermediateCerts [][]byte //信任的中间证书列表 Admins [][]byte //管理身份列表 RevocationList [][]byte //证书吊销列表 SigningIdentity *SigningIdentityInfo //签名身份 OrganizationalUnitIdentifiers []*FabricOUIdentifier //组织列表 CryptoConfig *FabricCryptoConfig //加密选项 TlsRootCerts [][]byte //信任的CA TLS 证书列表 TlsIntermediateCerts [][]byte //信任的中间TLS 证书列表}//代码在protos/msp/msp_config.pb.go
涉及的方法如下:
func GetLocalMspConfig(dir string, bccspConfig *factory.FactoryOpts, ID string) (*msp.MSPConfig, error) //获取本地MSP配置//代码在protos/msp/configbuilder.go
func GetLocalMspConfig(dir string, bccspConfig *factory.FactoryOpts, ID string) (*msp.MSPConfig, error)实现代码如下:
SetupBCCSPKeystoreConfig()核心代码为bccspConfig.SwOpts.FileKeystore = &factory.FileKeystoreOpts{KeyStorePath: keystoreDir},目的是在FileKeystore或KeyStorePath为空时设置默认值。
signcertDir := filepath.Join(dir, signcerts) //signcerts为"signcerts",signcertDir即/etc/hyperledger/fabric/msp/signcerts/keystoreDir := filepath.Join(dir, keystore) //keystore为"keystore",keystoreDir即/etc/hyperledger/fabric/msp/keystore/bccspConfig = SetupBCCSPKeystoreConfig(bccspConfig, keystoreDir) //设置bccspConfig.SwOpts.Ephemeral = false和bccspConfig.SwOpts.FileKeystore = &factory.FileKeystoreOpts{KeyStorePath: keystoreDir} //bccspConfig.SwOpts.Ephemeral是否短暂的err := factory.InitFactories(bccspConfig) //初始化bccsp factory,并创建bccsp实例signcert, err := getPemMaterialFromDir(signcertDir) //读取X.509证书的PEM文件sigid := &msp.SigningIdentityInfo{PublicSigner: signcert[0], PrivateSigner: nil} //构造SigningIdentityInforeturn getMspConfig(dir, ID, sigid) //分别读取cacerts、admincerts、tlscacerts文件,以及config.yaml中组织信息,构造msp.FabricMSPConfig,序列化后用于构造msp.MSPConfig//代码在msp/configbuilder.go
mgmt涉及方法如下:
func LoadLocalMsp(dir string, bccspConfig *factory.FactoryOpts, mspID string) error //从指定目录加载本地MSPfunc GetLocalMSP() msp.MSP //调取msp.NewBccspMsp()创建bccspmsp实例func GetLocalSigningIdentityOrPanic() msp.SigningIdentity //GetLocalMSP().GetDefaultSigningIdentity()//代码在msp/mgmt/mgmt.go
func LoadLocalMsp(dir string, bccspConfig *factory.FactoryOpts, mspID string) error代码如下:
conf, err := msp.GetLocalMspConfig(dir, bccspConfig, mspID) //获取本地MSP配置,序列化后写入msp.MSPConfig,即confreturn GetLocalMSP().Setup(conf) //调取msp.NewBccspMsp()创建bccspmsp实例,调取bccspmsp.Setup(conf)解码conf.Config并设置bccspmsp//代码在msp/mgmt/mgmt.go
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/31557831/viewspace-2218545/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/31557831/viewspace-2218545/