Skip to content

Commit

Permalink
[FABC-769] Add the ability to recover from panic
Browse files Browse the repository at this point in the history
Added the ability to gracefully recover from any panics
that might be encountered

Change-Id: Iec6347ac667fb6583e32e4939f3ed2b9c9229c8d
Signed-off-by: Saad Karim <skarim@us.ibm.com>
  • Loading branch information
Saad Karim committed Dec 7, 2018
1 parent a0ebc50 commit e064dcc
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 28 deletions.
2 changes: 1 addition & 1 deletion lib/server/idemix/enroll.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ func (h *EnrollRequestHandler) Authenticate() error {
}

// GenerateNonce generates a nonce for an Idemix enroll request
func (h *EnrollRequestHandler) GenerateNonce() *fp256bn.BIG {
func (h *EnrollRequestHandler) GenerateNonce() (*fp256bn.BIG, error) {
return h.IdmxLib.RandModOrder(h.Issuer.IdemixRand())
}

Expand Down
7 changes: 5 additions & 2 deletions lib/server/idemix/enroll_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ func TestHandleIdemixEnrollForCredentialError(t *testing.T) {
}
rmo := idemix.RandModOrder(rnd)
idemixlib.On("GetRand").Return(rnd, nil)
idemixlib.On("RandModOrder", rnd).Return(rmo)
idemixlib.On("RandModOrder", rnd).Return(rmo, nil)

issuerCred := NewIssuerCredential(testPublicKeyFile, testSecretKeyFile, idemixlib)
issuer := new(mocks.MyIssuer)
Expand All @@ -160,7 +160,10 @@ func TestHandleIdemixEnrollForCredentialError(t *testing.T) {
ctx.On("GetIssuer").Return(issuer, nil)
ctx.On("IsBasicAuth").Return(true)
handler := EnrollRequestHandler{Ctx: ctx, IdmxLib: idemixlib, Issuer: issuer}
nonce := handler.GenerateNonce()
nonce, err := handler.GenerateNonce()
if err != nil {
t.Fatalf("Failed to generate nonce: %s", err.Error())
}

credReq, _, err := newIdemixCredentialRequest(t, nonce)
if err != nil {
Expand Down
63 changes: 50 additions & 13 deletions lib/server/idemix/idemixlib.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,17 @@ import (
"github.com/hyperledger/fabric-amcl/amcl"
fp256bn "github.com/hyperledger/fabric-amcl/amcl/FP256BN"
"github.com/hyperledger/fabric/idemix"
"github.com/pkg/errors"
)

// Lib represents idemix library
type Lib interface {
NewIssuerKey(AttributeNames []string, rng *amcl.RAND) (*idemix.IssuerKey, error)
NewCredential(key *idemix.IssuerKey, m *idemix.CredRequest, attrs []*fp256bn.BIG, rng *amcl.RAND) (*idemix.Credential, error)
CreateCRI(key *ecdsa.PrivateKey, unrevokedHandles []*fp256bn.BIG, epoch int, alg idemix.RevocationAlgorithm, rng *amcl.RAND) (*idemix.CredentialRevocationInformation, error)
GenerateLongTermRevocationKey() (*ecdsa.PrivateKey, error)
GetRand() (*amcl.RAND, error)
RandModOrder(rng *amcl.RAND) *fp256bn.BIG
NewIssuerKey(AttributeNames []string, rng *amcl.RAND) (ik *idemix.IssuerKey, err error)
NewCredential(key *idemix.IssuerKey, m *idemix.CredRequest, attrs []*fp256bn.BIG, rng *amcl.RAND) (cred *idemix.Credential, err error)
CreateCRI(key *ecdsa.PrivateKey, unrevokedHandles []*fp256bn.BIG, epoch int, alg idemix.RevocationAlgorithm, rng *amcl.RAND) (cri *idemix.CredentialRevocationInformation, err error)
GenerateLongTermRevocationKey() (pk *ecdsa.PrivateKey, err error)
GetRand() (rand *amcl.RAND, err error)
RandModOrder(rng *amcl.RAND) (big *fp256bn.BIG, err error)
}

// libImpl is adapter for idemix library. It implements Lib interface
Expand All @@ -32,21 +33,57 @@ func NewLib() Lib {
return &libImpl{}
}

func (i *libImpl) GetRand() (*amcl.RAND, error) {
func (i *libImpl) GetRand() (rand *amcl.RAND, err error) {
defer func() {
r := recover()
if r != nil {
err = errors.Errorf("failure: %s", r)
}
}()
return idemix.GetRand()
}
func (i *libImpl) NewCredential(key *idemix.IssuerKey, m *idemix.CredRequest, attrs []*fp256bn.BIG, rng *amcl.RAND) (*idemix.Credential, error) {
func (i *libImpl) NewCredential(key *idemix.IssuerKey, m *idemix.CredRequest, attrs []*fp256bn.BIG, rng *amcl.RAND) (cred *idemix.Credential, err error) {
defer func() {
r := recover()
if r != nil {
err = errors.Errorf("failure: %s", r)
}
}()
return idemix.NewCredential(key, m, attrs, rng)
}
func (i *libImpl) RandModOrder(rng *amcl.RAND) *fp256bn.BIG {
return idemix.RandModOrder(rng)
func (i *libImpl) RandModOrder(rng *amcl.RAND) (big *fp256bn.BIG, err error) {
defer func() {
r := recover()
if r != nil {
err = errors.Errorf("failure: %s", r)
}
}()
return idemix.RandModOrder(rng), nil
}
func (i *libImpl) NewIssuerKey(AttributeNames []string, rng *amcl.RAND) (*idemix.IssuerKey, error) {
func (i *libImpl) NewIssuerKey(AttributeNames []string, rng *amcl.RAND) (ik *idemix.IssuerKey, err error) {
defer func() {
r := recover()
if r != nil {
err = errors.Errorf("failure: %s", r)
}
}()
return idemix.NewIssuerKey(AttributeNames, rng)
}
func (i *libImpl) CreateCRI(key *ecdsa.PrivateKey, unrevokedHandles []*fp256bn.BIG, epoch int, alg idemix.RevocationAlgorithm, rng *amcl.RAND) (*idemix.CredentialRevocationInformation, error) {
func (i *libImpl) CreateCRI(key *ecdsa.PrivateKey, unrevokedHandles []*fp256bn.BIG, epoch int, alg idemix.RevocationAlgorithm, rng *amcl.RAND) (cri *idemix.CredentialRevocationInformation, err error) {
defer func() {
r := recover()
if r != nil {
err = errors.Errorf("failure: %s", r)
}
}()
return idemix.CreateCRI(key, unrevokedHandles, epoch, alg, rng)
}
func (i *libImpl) GenerateLongTermRevocationKey() (*ecdsa.PrivateKey, error) {
func (i *libImpl) GenerateLongTermRevocationKey() (pk *ecdsa.PrivateKey, err error) {
defer func() {
r := recover()
if r != nil {
err = errors.Errorf("failure: %s", r)
}
}()
return idemix.GenerateLongTermRevocationKey()
}
19 changes: 10 additions & 9 deletions lib/server/idemix/mocks/Lib.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 5 additions & 2 deletions lib/server/idemix/nonce.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,12 @@ func NewNonceManager(issuer MyIssuer, clock Clock, level int) (NonceManager, err
// GetNonce returns a new nonce
func (nm *nonceManager) GetNonce() (*fp256bn.BIG, error) {
idmixLib := nm.issuer.IdemixLib()
nonce := idmixLib.RandModOrder(nm.issuer.IdemixRand())
nonce, err := idmixLib.RandModOrder(nm.issuer.IdemixRand())
if err != nil {
return nil, err
}
nonceBytes := idemix.BigToBytes(nonce)
err := nm.insertNonceInDB(&Nonce{
err = nm.insertNonceInDB(&Nonce{
Val: util.B64Encode(nonceBytes),
Expiry: nm.clock.Now().UTC().Add(nm.nonceExpiration),
Level: nm.level,
Expand Down
2 changes: 1 addition & 1 deletion lib/server/idemix/nonce_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func TestGetNonce(t *testing.T) {
t.Fatalf("Error generating a random number")
}
rmo := idemix.RandModOrder(rnd)
lib.On("RandModOrder", rnd).Return(rmo)
lib.On("RandModOrder", rnd).Return(rmo, nil)

issuer.On("IdemixRand").Return(rnd)

Expand Down

0 comments on commit e064dcc

Please sign in to comment.