-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathsig-info.go
178 lines (159 loc) · 4.18 KB
/
sig-info.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
package ndn
import (
"encoding"
"encoding/hex"
"fmt"
"math"
"github.com/usnistgov/ndn-dpdk/ndn/an"
"github.com/usnistgov/ndn-dpdk/ndn/tlv"
"github.com/zyedidia/generic/mapset"
)
// KeyLocator represents KeyLocator in SignatureInfo.
type KeyLocator struct {
Name Name
Digest []byte
}
var (
_ tlv.Fielder = KeyLocator{}
_ encoding.BinaryUnmarshaler = (*KeyLocator)(nil)
)
// Empty returns true if KeyLocator has zero fields.
func (kl KeyLocator) Empty() bool {
return len(kl.Name)+len(kl.Digest) == 0
}
// Field implements tlv.Fielder interface.
func (kl KeyLocator) Field() tlv.Field {
if len(kl.Name) > 0 && len(kl.Digest) > 0 {
return tlv.FieldError(ErrKeyLocator)
}
if len(kl.Digest) > 0 {
return tlv.TLV(an.TtKeyLocator, tlv.TLVBytes(an.TtKeyDigest, kl.Digest))
}
return tlv.TLVFrom(an.TtKeyLocator, kl.Name)
}
// UnmarshalBinary decodes from TLV-VALUE.
func (kl *KeyLocator) UnmarshalBinary(wire []byte) error {
*kl = KeyLocator{}
d := tlv.DecodingBuffer(wire)
for de := range d.IterElements() {
switch de.Type {
case an.TtName:
if e := de.UnmarshalValue(&kl.Name); e != nil {
return e
}
case an.TtKeyDigest:
kl.Digest = de.Value
default:
if de.IsCriticalType() {
return tlv.ErrCritical
}
}
}
if len(kl.Name) > 0 && len(kl.Digest) > 0 {
return ErrKeyLocator
}
return d.ErrUnlessEOF()
}
func (kl KeyLocator) String() string {
if len(kl.Digest) > 0 {
return hex.EncodeToString(kl.Digest)
}
return kl.Name.String()
}
// SigInfo represents SignatureInfo on Interest or Data.
type SigInfo struct {
Type uint32
KeyLocator KeyLocator
Nonce []byte
Time uint64
SeqNum uint64
Extensions []tlv.Element
}
var _ encoding.BinaryUnmarshaler = (*SigInfo)(nil)
// EncodeAs creates a tlv.Fielder for either ISigInfo or DSigInfo TLV-TYPE.
// If si is nil, the encoding result contains SigType=SigNull.
func (si *SigInfo) EncodeAs(typ uint32) tlv.Fielder {
return sigInfoFielder{typ, si}
}
// FindExtension retrieves extension field by TLV-TYPE number.
func (si *SigInfo) FindExtension(typ uint32) *tlv.Element {
for _, ext := range si.Extensions {
if ext.Type == typ {
return &ext
}
}
return nil
}
// UnmarshalBinary decodes from TLV-VALUE.
func (si *SigInfo) UnmarshalBinary(wire []byte) (e error) {
*si = SigInfo{}
d := tlv.DecodingBuffer(wire)
for de := range d.IterElements() {
switch de.Type {
case an.TtSigType:
if si.Type = uint32(de.UnmarshalNNI(math.MaxUint32, &e, ErrSigType)); e != nil {
return e
}
case an.TtKeyLocator:
if e = de.UnmarshalValue(&si.KeyLocator); e != nil {
return e
}
case an.TtSigNonce:
if de.Length() < 1 {
return ErrSigNonce
}
si.Nonce = de.Value
case an.TtSigTime:
if si.Time = de.UnmarshalNNI(math.MaxUint64, &e, tlv.ErrRange); e != nil {
return e
}
case an.TtSigSeqNum:
if si.SeqNum = de.UnmarshalNNI(math.MaxUint64, &e, tlv.ErrRange); e != nil {
return e
}
default:
if sigInfoExtensionTypes.Has(de.Type) {
si.Extensions = append(si.Extensions, de.Element)
} else if de.IsCriticalType() {
return tlv.ErrCritical
}
}
}
return d.ErrUnlessEOF()
}
func (si SigInfo) String() string {
return fmt.Sprintf("%s:%v", an.SigTypeString(si.Type), si.KeyLocator)
}
type sigInfoFielder struct {
typ uint32
si *SigInfo
}
func (sim sigInfoFielder) Field() tlv.Field {
var fields []tlv.Fielder
if si := sim.si; si == nil {
fields = append(fields, tlv.TLVNNI(an.TtSigType, an.SigNull))
} else {
fields = append(fields, tlv.TLVNNI(an.TtSigType, si.Type))
if !si.KeyLocator.Empty() {
fields = append(fields, si.KeyLocator)
}
if si.Time > 0 {
fields = append(fields, tlv.TLVNNI(an.TtSigTime, si.Time))
}
if len(si.Nonce) > 0 {
fields = append(fields, tlv.TLVBytes(an.TtSigNonce, si.Nonce))
}
if si.SeqNum > 0 {
fields = append(fields, tlv.TLVNNI(an.TtSigSeqNum, si.SeqNum))
}
for _, extension := range si.Extensions {
fields = append(fields, extension)
}
}
return tlv.TLVFrom(sim.typ, fields...)
}
var sigInfoExtensionTypes = mapset.New[uint32]()
// RegisterSigInfoExtension registers an extension TLV-TYPE in SigInfo.
func RegisterSigInfoExtension(typ uint32) {
sigInfoExtensionTypes.Put(typ)
}