github.com/darrenli6/fabric-sdk-example@v0.0.0-20220109053535-94b13b56df8c/msp/revocation_test.go (about)

     1  /*
     2  Copyright IBM Corp. 2017 All Rights Reserved.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8  		 http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package msp
    18  
    19  import (
    20  	"path/filepath"
    21  	"testing"
    22  
    23  	"github.com/hyperledger/fabric/bccsp/sw"
    24  	"github.com/hyperledger/fabric/protos/msp"
    25  	"github.com/op/go-logging"
    26  	"github.com/stretchr/testify/assert"
    27  )
    28  
    29  func init() {
    30  	logging.SetLevel(logging.DEBUG, "msp/identity")
    31  }
    32  
    33  func TestRevocation(t *testing.T) {
    34  	// testdata/revocation
    35  	// 1) a key and a signcert (used to populate the default signing identity);
    36  	// 2) cacert is the CA that signed the intermediate;
    37  	// 3) a revocation list that revokes signcert
    38  	thisMSP := getLocalMSP(t, "testdata/revocation")
    39  
    40  	id, err := thisMSP.GetDefaultSigningIdentity()
    41  	assert.NoError(t, err)
    42  
    43  	// the certificate associated to this id is revoked and so validation should fail!
    44  	err = id.Validate()
    45  	assert.Error(t, err)
    46  
    47  	// This MSP is identical to the previous one, with only 1 difference:
    48  	// the signature on the CRL is invalid
    49  	thisMSP = getLocalMSP(t, "testdata/revocation2")
    50  
    51  	id, err = thisMSP.GetDefaultSigningIdentity()
    52  	assert.NoError(t, err)
    53  
    54  	// the certificate associated to this id is revoked but the signature on the CRL is invalid
    55  	// so validation should succeed
    56  	err = id.Validate()
    57  	assert.NoError(t, err, "Identity found revoked although the signature over the CRL is invalid")
    58  }
    59  
    60  func TestIdentityPolicyPrincipalAgainstRevokedIdentity(t *testing.T) {
    61  	// testdata/revocation
    62  	// 1) a key and a signcert (used to populate the default signing identity);
    63  	// 2) cacert is the CA that signed the intermediate;
    64  	// 3) a revocation list that revokes signcert
    65  	thisMSP := getLocalMSP(t, "testdata/revocation")
    66  
    67  	id, err := thisMSP.GetDefaultSigningIdentity()
    68  	assert.NoError(t, err)
    69  
    70  	idSerialized, err := id.Serialize()
    71  	assert.NoError(t, err)
    72  
    73  	principal := &msp.MSPPrincipal{
    74  		PrincipalClassification: msp.MSPPrincipal_IDENTITY,
    75  		Principal:               idSerialized}
    76  
    77  	err = id.SatisfiesPrincipal(principal)
    78  	assert.Error(t, err)
    79  }
    80  
    81  func TestRevokedIntermediateCA(t *testing.T) {
    82  	// testdata/revokedica
    83  	// 1) a key and a signcert (used to populate the default signing identity);
    84  	// 2) cacert is the CA that signed the intermediate;
    85  	// 3) a revocation list that revokes the intermediate CA cert
    86  	dir := "testdata/revokedica"
    87  	conf, err := GetLocalMspConfig(dir, nil, "DEFAULT")
    88  	assert.NoError(t, err)
    89  
    90  	thisMSP, err := NewBccspMsp()
    91  	assert.NoError(t, err)
    92  	ks, err := sw.NewFileBasedKeyStore(nil, filepath.Join(dir, "keystore"), true)
    93  	assert.NoError(t, err)
    94  	csp, err := sw.New(256, "SHA2", ks)
    95  	assert.NoError(t, err)
    96  	thisMSP.(*bccspmsp).bccsp = csp
    97  
    98  	err = thisMSP.Setup(conf)
    99  	assert.Error(t, err)
   100  	assert.Contains(t, err.Error(), "CA Certificate is not valid, ")
   101  }