This repository has been archived by the owner on Apr 9, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
signeddata.go
82 lines (67 loc) · 2.18 KB
/
signeddata.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
/*
Copyright IBM Corp. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package protoutil
import (
"bytes"
"fmt"
"github.com/golang/protobuf/proto"
"github.com/hyperledger/fabric-protos-go/common"
)
// SignedData is used to represent the general triplet required to verify a signature
// This is intended to be generic across crypto schemes, while most crypto schemes will
// include the signing identity and a nonce within the Data, this is left to the crypto
// implementation.
type SignedData struct {
Data []byte
Identity []byte
Signature []byte
}
// ConfigUpdateEnvelopeAsSignedData returns the set of signatures for the
// ConfigUpdateEnvelope as SignedData or an error indicating why this was not
// possible.
func ConfigUpdateEnvelopeAsSignedData(ce *common.ConfigUpdateEnvelope) ([]*SignedData, error) {
if ce == nil {
return nil, fmt.Errorf("No signatures for nil SignedConfigItem")
}
result := make([]*SignedData, len(ce.Signatures))
for i, configSig := range ce.Signatures {
sigHeader := &common.SignatureHeader{}
err := proto.Unmarshal(configSig.SignatureHeader, sigHeader)
if err != nil {
return nil, err
}
result[i] = &SignedData{
Data: bytes.Join([][]byte{configSig.SignatureHeader, ce.ConfigUpdate}, nil),
Identity: sigHeader.Creator,
Signature: configSig.Signature,
}
}
return result, nil
}
// EnvelopeAsSignedData returns the signatures for the Envelope as SignedData
// slice of length 1 or an error indicating why this was not possible.
func EnvelopeAsSignedData(env *common.Envelope) ([]*SignedData, error) {
if env == nil {
return nil, fmt.Errorf("No signatures for nil Envelope")
}
payload := &common.Payload{}
err := proto.Unmarshal(env.Payload, payload)
if err != nil {
return nil, err
}
if payload.Header == nil /* || payload.Header.SignatureHeader == nil */ {
return nil, fmt.Errorf("Missing Header")
}
shdr := &common.SignatureHeader{}
err = proto.Unmarshal(payload.Header.SignatureHeader, shdr)
if err != nil {
return nil, fmt.Errorf("GetSignatureHeaderFromBytes failed, err %s", err)
}
return []*SignedData{{
Data: env.Payload,
Identity: shdr.Creator,
Signature: env.Signature,
}}, nil
}