github.com/lzy4123/fabric@v2.1.1+incompatible/bccsp/idemix/handlers/issuer.go (about)

     1  /*
     2  Copyright IBM Corp. All Rights Reserved.
     3  
     4  SPDX-License-Identifier: Apache-2.0
     5  */
     6  package handlers
     7  
     8  import (
     9  	"github.com/hyperledger/fabric/bccsp"
    10  	"github.com/pkg/errors"
    11  )
    12  
    13  // issuerSecretKey contains the issuer secret key
    14  // and implements the bccsp.Key interface
    15  type issuerSecretKey struct {
    16  	// sk is the idemix reference to the issuer key
    17  	sk IssuerSecretKey
    18  	// exportable if true, sk can be exported via the Bytes function
    19  	exportable bool
    20  }
    21  
    22  func NewIssuerSecretKey(sk IssuerSecretKey, exportable bool) *issuerSecretKey {
    23  	return &issuerSecretKey{sk: sk, exportable: exportable}
    24  }
    25  
    26  func (k *issuerSecretKey) Bytes() ([]byte, error) {
    27  	if k.exportable {
    28  		return k.sk.Bytes()
    29  	}
    30  
    31  	return nil, errors.New("not exportable")
    32  }
    33  
    34  func (k *issuerSecretKey) SKI() []byte {
    35  	pk, err := k.PublicKey()
    36  	if err != nil {
    37  		return nil
    38  	}
    39  
    40  	return pk.SKI()
    41  }
    42  
    43  func (*issuerSecretKey) Symmetric() bool {
    44  	return false
    45  }
    46  
    47  func (*issuerSecretKey) Private() bool {
    48  	return true
    49  }
    50  
    51  func (k *issuerSecretKey) PublicKey() (bccsp.Key, error) {
    52  	return &issuerPublicKey{k.sk.Public()}, nil
    53  }
    54  
    55  // issuerPublicKey contains the issuer public key
    56  // and implements the bccsp.Key interface
    57  type issuerPublicKey struct {
    58  	pk IssuerPublicKey
    59  }
    60  
    61  func NewIssuerPublicKey(pk IssuerPublicKey) *issuerPublicKey {
    62  	return &issuerPublicKey{pk}
    63  }
    64  
    65  func (k *issuerPublicKey) Bytes() ([]byte, error) {
    66  	return k.pk.Bytes()
    67  }
    68  
    69  func (k *issuerPublicKey) SKI() []byte {
    70  	return k.pk.Hash()
    71  }
    72  
    73  func (*issuerPublicKey) Symmetric() bool {
    74  	return false
    75  }
    76  
    77  func (*issuerPublicKey) Private() bool {
    78  	return false
    79  }
    80  
    81  func (k *issuerPublicKey) PublicKey() (bccsp.Key, error) {
    82  	return k, nil
    83  }
    84  
    85  // IssuerKeyGen generates issuer secret keys.
    86  type IssuerKeyGen struct {
    87  	// exportable is a flag to allow an issuer secret key to be marked as exportable.
    88  	// If a secret key is marked as exportable, its Bytes method will return the key's byte representation.
    89  	Exportable bool
    90  	// Issuer implements the underlying cryptographic algorithms
    91  	Issuer Issuer
    92  }
    93  
    94  func (g *IssuerKeyGen) KeyGen(opts bccsp.KeyGenOpts) (k bccsp.Key, err error) {
    95  	o, ok := opts.(*bccsp.IdemixIssuerKeyGenOpts)
    96  	if !ok {
    97  		return nil, errors.New("invalid options, expected *bccsp.IdemixIssuerKeyGenOpts")
    98  	}
    99  
   100  	// Create a new key pair
   101  	key, err := g.Issuer.NewKey(o.AttributeNames)
   102  	if err != nil {
   103  		return nil, err
   104  	}
   105  
   106  	return &issuerSecretKey{exportable: g.Exportable, sk: key}, nil
   107  }
   108  
   109  // IssuerPublicKeyImporter imports issuer public keys
   110  type IssuerPublicKeyImporter struct {
   111  	// Issuer implements the underlying cryptographic algorithms
   112  	Issuer Issuer
   113  }
   114  
   115  func (i *IssuerPublicKeyImporter) KeyImport(raw interface{}, opts bccsp.KeyImportOpts) (k bccsp.Key, err error) {
   116  	der, ok := raw.([]byte)
   117  	if !ok {
   118  		return nil, errors.New("invalid raw, expected byte array")
   119  	}
   120  
   121  	if len(der) == 0 {
   122  		return nil, errors.New("invalid raw, it must not be nil")
   123  	}
   124  
   125  	o, ok := opts.(*bccsp.IdemixIssuerPublicKeyImportOpts)
   126  	if !ok {
   127  		return nil, errors.New("invalid options, expected *bccsp.IdemixIssuerPublicKeyImportOpts")
   128  	}
   129  
   130  	pk, err := i.Issuer.NewPublicKeyFromBytes(raw.([]byte), o.AttributeNames)
   131  	if err != nil {
   132  		return nil, err
   133  	}
   134  
   135  	return &issuerPublicKey{pk}, nil
   136  }