github.com/leonlxy/hyperledger@v1.0.0-alpha.0.20170427033203-34922035d248/bccsp/sw/rsakey.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 sw
    17  
    18  import (
    19  	"crypto/rsa"
    20  	"crypto/x509"
    21  	"fmt"
    22  
    23  	"crypto/sha256"
    24  
    25  	"errors"
    26  
    27  	"encoding/asn1"
    28  	"math/big"
    29  
    30  	"github.com/hyperledger/fabric/bccsp"
    31  )
    32  
    33  // rsaPublicKey reflects the ASN.1 structure of a PKCS#1 public key.
    34  type rsaPublicKeyASN struct {
    35  	N *big.Int
    36  	E int
    37  }
    38  
    39  type rsaPrivateKey struct {
    40  	privKey *rsa.PrivateKey
    41  }
    42  
    43  // Bytes converts this key to its byte representation,
    44  // if this operation is allowed.
    45  func (k *rsaPrivateKey) Bytes() (raw []byte, err error) {
    46  	return nil, errors.New("Not supported.")
    47  }
    48  
    49  // SKI returns the subject key identifier of this key.
    50  func (k *rsaPrivateKey) SKI() (ski []byte) {
    51  	if k.privKey == nil {
    52  		return nil
    53  	}
    54  
    55  	// Marshall the public key
    56  	raw, _ := asn1.Marshal(rsaPublicKeyASN{
    57  		N: k.privKey.N,
    58  		E: k.privKey.E,
    59  	})
    60  
    61  	// Hash it
    62  	hash := sha256.New()
    63  	hash.Write(raw)
    64  	return hash.Sum(nil)
    65  }
    66  
    67  // Symmetric returns true if this key is a symmetric key,
    68  // false is this key is asymmetric
    69  func (k *rsaPrivateKey) Symmetric() bool {
    70  	return false
    71  }
    72  
    73  // Private returns true if this key is an asymmetric private key,
    74  // false otherwise.
    75  func (k *rsaPrivateKey) Private() bool {
    76  	return true
    77  }
    78  
    79  // PublicKey returns the corresponding public key part of an asymmetric public/private key pair.
    80  // This method returns an error in symmetric key schemes.
    81  func (k *rsaPrivateKey) PublicKey() (bccsp.Key, error) {
    82  	return &rsaPublicKey{&k.privKey.PublicKey}, nil
    83  }
    84  
    85  type rsaPublicKey struct {
    86  	pubKey *rsa.PublicKey
    87  }
    88  
    89  // Bytes converts this key to its byte representation,
    90  // if this operation is allowed.
    91  func (k *rsaPublicKey) Bytes() (raw []byte, err error) {
    92  	if k.pubKey == nil {
    93  		return nil, errors.New("Failed marshalling key. Key is nil.")
    94  	}
    95  	raw, err = x509.MarshalPKIXPublicKey(k.pubKey)
    96  	if err != nil {
    97  		return nil, fmt.Errorf("Failed marshalling key [%s]", err)
    98  	}
    99  	return
   100  }
   101  
   102  // SKI returns the subject key identifier of this key.
   103  func (k *rsaPublicKey) SKI() (ski []byte) {
   104  	if k.pubKey == nil {
   105  		return nil
   106  	}
   107  
   108  	// Marshall the public key
   109  	raw, _ := asn1.Marshal(rsaPublicKeyASN{
   110  		N: k.pubKey.N,
   111  		E: k.pubKey.E,
   112  	})
   113  
   114  	// Hash it
   115  	hash := sha256.New()
   116  	hash.Write(raw)
   117  	return hash.Sum(nil)
   118  }
   119  
   120  // Symmetric returns true if this key is a symmetric key,
   121  // false is this key is asymmetric
   122  func (k *rsaPublicKey) Symmetric() bool {
   123  	return false
   124  }
   125  
   126  // Private returns true if this key is an asymmetric private key,
   127  // false otherwise.
   128  func (k *rsaPublicKey) Private() bool {
   129  	return false
   130  }
   131  
   132  // PublicKey returns the corresponding public key part of an asymmetric public/private key pair.
   133  // This method returns an error in symmetric key schemes.
   134  func (k *rsaPublicKey) PublicKey() (bccsp.Key, error) {
   135  	return k, nil
   136  }