-
Notifications
You must be signed in to change notification settings - Fork 155
/
decode.go
96 lines (86 loc) · 2.32 KB
/
decode.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
// Copyright 2019 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 decoded
import (
"fmt"
"github.com/scionproto/scion/go/lib/scrypto/cert/v2"
"github.com/scionproto/scion/go/lib/scrypto/trc/v2"
"github.com/scionproto/scion/go/lib/serrors"
)
// ErrParse indicates that parsign failed.
var ErrParse = serrors.New("parse error")
// TRC is a container for the decoded TRC.
type TRC struct {
TRC *trc.TRC
Signed trc.Signed
Raw []byte
}
// DecodeTRC decodes the TRC.
func DecodeTRC(raw []byte) (TRC, error) {
signed, err := trc.ParseSigned(raw)
if err != nil {
return TRC{}, serrors.Wrap(ErrParse, err, "part", "signed")
}
decoded, err := signed.EncodedTRC.Decode()
if err != nil {
return TRC{}, serrors.Wrap(ErrParse, err, "part", "decode payload")
}
d := TRC{
TRC: decoded,
Signed: signed,
Raw: raw,
}
return d, nil
}
func (d TRC) String() string {
if d.TRC == nil {
return "<nil>"
}
return fmt.Sprintf("ISD%d-V%d", d.TRC.ISD, d.TRC.Version)
}
// Chain is a container for the decoded certificate chain.
type Chain struct {
Chain cert.Chain
AS *cert.AS
Issuer *cert.Issuer
Raw []byte
}
// DecodeChain decodes a certificate chain.
func DecodeChain(raw []byte) (Chain, error) {
chain, err := cert.ParseChain(raw)
if err != nil {
return Chain{}, serrors.Wrap(ErrParse, err, "part", "chain")
}
as, err := chain.AS.Encoded.Decode()
if err != nil {
return Chain{}, serrors.Wrap(ErrParse, err, "part", "AS")
}
issuer, err := chain.Issuer.Encoded.Decode()
if err != nil {
return Chain{}, serrors.Wrap(ErrParse, err, "part", "Issuer")
}
d := Chain{
Chain: chain,
AS: as,
Issuer: issuer,
Raw: raw,
}
return d, nil
}
func (d Chain) String() string {
if d.AS == nil {
return "<nil>"
}
return fmt.Sprintf("ISD%s-V%d", d.AS.Subject.FileFmt(true), d.AS.Version)
}