github.com/lzy4123/fabric@v2.1.1+incompatible/bccsp/pkcs11/ecdsakey.go (about)

     1  /*
     2  Copyright IBM Corp. 2016 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  package pkcs11
    17  
    18  import (
    19  	"crypto/ecdsa"
    20  	"crypto/x509"
    21  	"errors"
    22  	"fmt"
    23  
    24  	"github.com/hyperledger/fabric/bccsp"
    25  )
    26  
    27  type ecdsaPrivateKey struct {
    28  	ski []byte
    29  	pub ecdsaPublicKey
    30  }
    31  
    32  // Bytes converts this key to its byte representation,
    33  // if this operation is allowed.
    34  func (k *ecdsaPrivateKey) Bytes() ([]byte, error) {
    35  	return nil, errors.New("Not supported.")
    36  }
    37  
    38  // SKI returns the subject key identifier of this key.
    39  func (k *ecdsaPrivateKey) SKI() []byte {
    40  	return k.ski
    41  }
    42  
    43  // Symmetric returns true if this key is a symmetric key,
    44  // false if this key is asymmetric
    45  func (k *ecdsaPrivateKey) Symmetric() bool {
    46  	return false
    47  }
    48  
    49  // Private returns true if this key is a private key,
    50  // false otherwise.
    51  func (k *ecdsaPrivateKey) Private() bool {
    52  	return true
    53  }
    54  
    55  // PublicKey returns the corresponding public key part of an asymmetric public/private key pair.
    56  // This method returns an error in symmetric key schemes.
    57  func (k *ecdsaPrivateKey) PublicKey() (bccsp.Key, error) {
    58  	return &k.pub, nil
    59  }
    60  
    61  type ecdsaPublicKey struct {
    62  	ski []byte
    63  	pub *ecdsa.PublicKey
    64  }
    65  
    66  // Bytes converts this key to its byte representation,
    67  // if this operation is allowed.
    68  func (k *ecdsaPublicKey) Bytes() (raw []byte, err error) {
    69  	raw, err = x509.MarshalPKIXPublicKey(k.pub)
    70  	if err != nil {
    71  		return nil, fmt.Errorf("Failed marshalling key [%s]", err)
    72  	}
    73  	return
    74  }
    75  
    76  // SKI returns the subject key identifier of this key.
    77  func (k *ecdsaPublicKey) SKI() []byte {
    78  	return k.ski
    79  }
    80  
    81  // Symmetric returns true if this key is a symmetric key,
    82  // false if this key is asymmetric
    83  func (k *ecdsaPublicKey) Symmetric() bool {
    84  	return false
    85  }
    86  
    87  // Private returns true if this key is a private key,
    88  // false otherwise.
    89  func (k *ecdsaPublicKey) Private() bool {
    90  	return false
    91  }
    92  
    93  // PublicKey returns the corresponding public key part of an asymmetric public/private key pair.
    94  // This method returns an error in symmetric key schemes.
    95  func (k *ecdsaPublicKey) PublicKey() (bccsp.Key, error) {
    96  	return k, nil
    97  }