github.com/kchristidis/fabric@v1.0.4-0.20171028114726-837acd08cde1/protos/common/signed_data.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  
    17  package common
    18  
    19  import (
    20  	"fmt"
    21  
    22  	"github.com/hyperledger/fabric/common/util"
    23  
    24  	"github.com/golang/protobuf/proto"
    25  )
    26  
    27  // SignedData is used to represent the general triplet required to verify a signature
    28  // This is intended to be generic across crypto schemes, while most crypto schemes will
    29  // include the signing identity and a nonce within the Data, this is left to the crypto
    30  // implementation
    31  type SignedData struct {
    32  	Data      []byte
    33  	Identity  []byte
    34  	Signature []byte
    35  }
    36  
    37  // Signable types are those which can map their contents to a set of SignedData
    38  type Signable interface {
    39  	// AsSignedData returns the set of signatures for a structure as SignedData or an error indicating why this was not possible
    40  	AsSignedData() ([]*SignedData, error)
    41  }
    42  
    43  // AsSignedData returns the set of signatures for the ConfigUpdateEnvelope as SignedData or an error indicating why this was not possible
    44  func (ce *ConfigUpdateEnvelope) AsSignedData() ([]*SignedData, error) {
    45  	if ce == nil {
    46  		return nil, fmt.Errorf("No signatures for nil SignedConfigItem")
    47  	}
    48  
    49  	result := make([]*SignedData, len(ce.Signatures))
    50  	for i, configSig := range ce.Signatures {
    51  		sigHeader := &SignatureHeader{}
    52  		err := proto.Unmarshal(configSig.SignatureHeader, sigHeader)
    53  		if err != nil {
    54  			return nil, err
    55  		}
    56  
    57  		result[i] = &SignedData{
    58  			Data:      util.ConcatenateBytes(configSig.SignatureHeader, ce.ConfigUpdate),
    59  			Identity:  sigHeader.Creator,
    60  			Signature: configSig.Signature,
    61  		}
    62  
    63  	}
    64  
    65  	return result, nil
    66  }
    67  
    68  // AsSignedData returns the signatures for the Envelope as SignedData slice of length 1 or an error indicating why this was not possible
    69  func (env *Envelope) AsSignedData() ([]*SignedData, error) {
    70  	if env == nil {
    71  		return nil, fmt.Errorf("No signatures for nil Envelope")
    72  	}
    73  
    74  	payload := &Payload{}
    75  	err := proto.Unmarshal(env.Payload, payload)
    76  	if err != nil {
    77  		return nil, err
    78  	}
    79  
    80  	if payload.Header == nil /* || payload.Header.SignatureHeader == nil */ {
    81  		return nil, fmt.Errorf("Missing Header")
    82  	}
    83  
    84  	shdr := &SignatureHeader{}
    85  	err = proto.Unmarshal(payload.Header.SignatureHeader, shdr)
    86  	if err != nil {
    87  		return nil, fmt.Errorf("GetSignatureHeaderFromBytes failed, err %s", err)
    88  	}
    89  
    90  	return []*SignedData{&SignedData{
    91  		Data:      env.Payload,
    92  		Identity:  shdr.Creator,
    93  		Signature: env.Signature,
    94  	}}, nil
    95  }