-
Notifications
You must be signed in to change notification settings - Fork 153
/
combine.go
221 lines (205 loc) · 6.27 KB
/
combine.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
// Copyright 2020 Anapaya Systems
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package trcs
import (
"bytes"
"crypto/x509/pkix"
"encoding/pem"
"fmt"
"os"
"sort"
"github.com/google/go-cmp/cmp"
"github.com/spf13/cobra"
"github.com/scionproto/scion/pkg/private/serrors"
"github.com/scionproto/scion/pkg/scrypto/cms/protocol"
"github.com/scionproto/scion/pkg/scrypto/cppki"
"github.com/scionproto/scion/private/app/command"
)
func newCombine(pather command.Pather) *cobra.Command {
var flags struct {
out string
payload string
format string
}
cmd := &cobra.Command{
Use: "combine",
Short: "Combine partially signed TRCs",
Long: `'combine' combines the signatures on partially signed TRCs into one single TRC.
The command checks that all parts sign the same TRC payload content.
No further checks are made. Check that the TRC is valid and verifiable with the
appropriate commands.
`,
Example: fmt.Sprintf(` %[1]s combine --payload ISD1-B1-S1.pld -o ISD1-B1-S1.trc `+
`ISD1-B1-S1.org1 ISD1-B1-S1.org2`, pather.CommandPath()),
Args: cobra.MinimumNArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
cmd.SilenceUsage = true
return RunCombine(args, flags.payload, flags.out, flags.format)
},
}
addOutputFlag(&flags.out, cmd)
cmd.Flags().StringVarP(&flags.payload, "payload", "p", "",
"The TRC payload. If provided, it will be used as a reference payload to compare the "+
"partially signed TRC payloads against. It can be either DER or PEM encoded.")
cmd.Flags().StringVar(&flags.format, "format", "der", "Output format (der|pem)")
return cmd
}
// RunCombine combines the partially signed TRC files and writes them to
// the out directory, pld is the payload file.
func RunCombine(files []string, pld, out string, format string) error {
trcs := make(map[string]cppki.SignedTRC)
for _, name := range files {
dec, err := DecodeFromFile(name)
if err != nil {
return serrors.WrapStr("error decoding part", err, "file", name)
}
trcs[name] = dec
}
if err := verifyPayload(pld, trcs); err != nil {
return err
}
packed, err := CombineSignedPayloads(trcs)
if err != nil {
return err
}
if format == "pem" {
packed = pem.EncodeToMemory(&pem.Block{
Type: "TRC",
Bytes: packed,
})
}
if err := os.WriteFile(out, packed, 0644); err != nil {
return serrors.WrapStr("error writing combined TRC", err)
}
fmt.Printf("Successfully combined TRC at %s\n", out)
return nil
}
// CombineSignedPayloads combines the signed TRC payloads and checks that all payloads and
// signer infos are consistent.
func CombineSignedPayloads(trcs map[string]cppki.SignedTRC) ([]byte, error) {
if err := verifyPayload("", trcs); err != nil {
return nil, err
}
infos, err := combineSignerInfos(trcs)
if err != nil {
return nil, err
}
// Extract any payload. They are guaranteed to be the same
var pld []byte
for _, signed := range trcs {
pld = signed.TRC.Raw
break
}
eci, err := protocol.NewDataEncapsulatedContentInfo(pld)
if err != nil {
return nil, serrors.WrapStr("error encoding payload", err)
}
sd := protocol.SignedData{
Version: 1,
EncapContentInfo: eci,
SignerInfos: infos,
DigestAlgorithms: combineDigestAlgorithms(infos),
}
// Write signed TRC.
packed, err := sd.ContentInfoDER()
if err != nil {
return nil, serrors.WrapStr("error packing combined TRC", err)
}
return packed, nil
}
// combineSignerInfos combines all singer infos. It checks that non-unique
// signer infos are equal. The returned slice is sorted.
func combineSignerInfos(trcs map[string]cppki.SignedTRC) ([]protocol.SignerInfo, error) {
type SignerOrigin struct {
Info protocol.SignerInfo
File string
}
var errs serrors.List
infos := make(map[string]SignerOrigin)
for name, signed := range trcs {
for _, si := range signed.SignerInfos {
sid := string(si.SID.FullBytes)
existing, ok := infos[sid]
if !ok {
infos[sid] = SignerOrigin{
Info: si,
File: name,
}
continue
}
if !cmp.Equal(si, existing.Info) {
errs = append(errs, serrors.New("different SignerInfo contents for same subject",
"files", []string{name, existing.File}))
}
}
}
if err := errs.ToError(); err != nil {
return nil, err
}
var l []protocol.SignerInfo
for _, info := range infos {
l = append(l, info.Info)
}
// Keep sorting for consistent output for older go versions.
// Starting from go1.15, the SignerInfos will be sorted when serializing.
sort.Slice(l, func(i, j int) bool {
return bytes.Compare(l[i].SID.FullBytes, l[j].SID.FullBytes) < 0
})
return l, nil
}
func combineDigestAlgorithms(infos []protocol.SignerInfo) []pkix.AlgorithmIdentifier {
var algos []pkix.AlgorithmIdentifier
for _, si := range infos {
if !findDigestAlgorithm(si.DigestAlgorithm, algos) {
algos = append(algos, si.DigestAlgorithm)
}
}
sort.Slice(algos, func(i, j int) bool {
return algos[i].Algorithm.String() < algos[j].Algorithm.String()
})
return algos
}
func findDigestAlgorithm(algo pkix.AlgorithmIdentifier, algos []pkix.AlgorithmIdentifier) bool {
for _, existing := range algos {
if existing.Algorithm.Equal(algo.Algorithm) {
return bytes.Equal(existing.Parameters.FullBytes, algo.Parameters.FullBytes)
}
}
return false
}
func verifyPayload(pld string, trcs map[string]cppki.SignedTRC) error {
var errs serrors.List
var rawPld []byte
if pld != "" {
var err error
rawPld, err = os.ReadFile(pld)
if err != nil {
return serrors.WrapStr("error loading payload", err)
}
block, _ := pem.Decode(rawPld)
if block != nil && block.Type == "TRC PAYLOAD" {
rawPld = block.Bytes
}
}
for name, signed := range trcs {
if rawPld == nil {
rawPld = signed.TRC.Raw
continue
}
if !bytes.Equal(signed.TRC.Raw, rawPld) {
errs = append(errs, serrors.New("different payload contents", "file", name))
}
}
return errs.ToError()
}