Repository navigation
Expand file tree
/
Copy pathframe.go
More file actions
242 lines (216 loc) · 7.21 KB
/
Copy pathframe.go
File metadata and controls
242 lines (216 loc) · 7.21 KB
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
// SPDX-License-Identifier: MIT
package apci
import (
"errors"
"fmt"
"io"
)
const (
// StartByte is the first octet of every APDU.
StartByte = 0x68
// ControlLength is the size of the control field in octets.
ControlLength = 4
// HeaderLength is the size of the start octet plus the length octet.
HeaderLength = 2
// MaxAPDULength is the largest value of the APDU length octet: the
// control field plus the ASDU.
MaxAPDULength = 253
// MaxASDULength is the largest ASDU an I-format APDU can carry.
MaxASDULength = MaxAPDULength - ControlLength
// MaxFrameLength is the largest APDU on the wire, including the start
// and length octets.
MaxFrameLength = HeaderLength + MaxAPDULength
)
// Framing errors returned by [Parse] and [ReadFrame]. They are wrapped with
// detail; test with errors.Is.
var (
// ErrInvalidStart reports an APDU that does not begin with [StartByte].
ErrInvalidStart = errors.New("apci: invalid start byte")
// ErrInvalidLength reports a length octet outside 4..253, a buffer that
// does not match its length octet, or an S/U frame carrying a payload.
ErrInvalidLength = errors.New("apci: invalid APDU length")
// ErrInvalidControl reports a control field that is not a valid I, S or
// U format, including a U frame with no or several functions set.
ErrInvalidControl = errors.New("apci: invalid control field")
// ErrASDUTooLong reports an ASDU larger than [MaxASDULength].
ErrASDUTooLong = errors.New("apci: ASDU too long")
)
// Format is the APDU control field format.
type Format uint8
// Control field formats.
const (
// FormatI is the numbered information transfer format. It carries an ASDU.
FormatI Format = iota
// FormatS is the numbered supervisory format: an acknowledgement.
FormatS
// FormatU is the unnumbered control format: STARTDT, STOPDT and TESTFR.
FormatU
)
// String returns "I", "S" or "U".
func (f Format) String() string {
switch f {
case FormatI:
return "I"
case FormatS:
return "S"
case FormatU:
return "U"
default:
return fmt.Sprintf("Format(%d)", uint8(f))
}
}
// Frame is one decoded APDU.
//
// SendSeq is meaningful for I frames, RecvSeq for I and S frames, Function
// for U frames and ASDU for I frames.
type Frame struct {
Format Format
SendSeq uint16
RecvSeq uint16
Function UFunction
ASDU []byte
}
// NewI returns an I-format frame carrying asdu. The slice is not copied.
func NewI(sendSeq, recvSeq uint16, asdu []byte) Frame {
return Frame{Format: FormatI, SendSeq: sendSeq & SeqMask, RecvSeq: recvSeq & SeqMask, ASDU: asdu}
}
// NewS returns an S-format frame acknowledging every I frame up to, but not
// including, recvSeq.
func NewS(recvSeq uint16) Frame {
return Frame{Format: FormatS, RecvSeq: recvSeq & SeqMask}
}
// NewU returns a U-format frame for fn.
func NewU(fn UFunction) Frame {
return Frame{Format: FormatU, Function: fn}
}
// Len returns the size of the frame on the wire.
func (f Frame) Len() int {
n := HeaderLength + ControlLength
if f.Format == FormatI {
n += len(f.ASDU)
}
return n
}
// AppendBinary appends the wire encoding of f to b.
func (f Frame) AppendBinary(b []byte) ([]byte, error) {
switch f.Format {
case FormatI:
if len(f.ASDU) > MaxASDULength {
return b, fmt.Errorf("%w: %d octets", ErrASDUTooLong, len(f.ASDU))
}
if f.SendSeq > SeqMask || f.RecvSeq > SeqMask {
return b, fmt.Errorf("%w: sequence number out of range", ErrInvalidControl)
}
b = append(b, StartByte, byte(ControlLength+len(f.ASDU)),
byte(f.SendSeq<<1), byte(f.SendSeq>>7),
byte(f.RecvSeq<<1), byte(f.RecvSeq>>7))
return append(b, f.ASDU...), nil
case FormatS:
if f.RecvSeq > SeqMask {
return b, fmt.Errorf("%w: sequence number out of range", ErrInvalidControl)
}
return append(b, StartByte, ControlLength, 0x01, 0x00,
byte(f.RecvSeq<<1), byte(f.RecvSeq>>7)), nil
case FormatU:
if !f.Function.Valid() {
return b, fmt.Errorf("%w: U function 0x%02X", ErrInvalidControl, uint8(f.Function))
}
return append(b, StartByte, ControlLength, byte(f.Function), 0x00, 0x00, 0x00), nil
default:
return b, fmt.Errorf("%w: format %d", ErrInvalidControl, uint8(f.Format))
}
}
// MarshalBinary returns the wire encoding of f.
func (f Frame) MarshalBinary() ([]byte, error) {
return f.AppendBinary(make([]byte, 0, f.Len()))
}
// String returns a compact, log-friendly description of the frame.
func (f Frame) String() string {
switch f.Format {
case FormatI:
return fmt.Sprintf("I N(S)=%d N(R)=%d len=%d", f.SendSeq, f.RecvSeq, len(f.ASDU))
case FormatS:
return fmt.Sprintf("S N(R)=%d", f.RecvSeq)
case FormatU:
return "U " + f.Function.String()
default:
return f.Format.String()
}
}
// Parse decodes exactly one APDU from b, which must hold the whole frame
// including the start and length octets and nothing else. The ASDU of an
// I frame aliases b.
func Parse(b []byte) (Frame, error) {
if len(b) < HeaderLength+ControlLength {
return Frame{}, fmt.Errorf("%w: %d octets", ErrInvalidLength, len(b))
}
if b[0] != StartByte {
return Frame{}, fmt.Errorf("%w: 0x%02X", ErrInvalidStart, b[0])
}
n := int(b[1])
if n < ControlLength || n > MaxAPDULength || n != len(b)-HeaderLength {
return Frame{}, fmt.Errorf("%w: length octet %d, %d octets follow", ErrInvalidLength, n, len(b)-HeaderLength)
}
return parseBody(b[HeaderLength:])
}
// parseBody decodes the control field and payload (everything after the
// length octet).
func parseBody(b []byte) (Frame, error) {
c := b[:ControlLength]
switch {
case c[0]&0x01 == 0:
if c[2]&0x01 != 0 {
return Frame{}, fmt.Errorf("%w: I frame with odd third octet", ErrInvalidControl)
}
return Frame{
Format: FormatI,
SendSeq: uint16(c[0])>>1 | uint16(c[1])<<7,
RecvSeq: uint16(c[2])>>1 | uint16(c[3])<<7,
ASDU: b[ControlLength:],
}, nil
case c[0]&0x03 == 0x01:
if len(b) != ControlLength {
return Frame{}, fmt.Errorf("%w: S frame with payload", ErrInvalidLength)
}
if c[0] != 0x01 || c[1] != 0 || c[2]&0x01 != 0 {
return Frame{}, fmt.Errorf("%w: malformed S frame", ErrInvalidControl)
}
return Frame{Format: FormatS, RecvSeq: uint16(c[2])>>1 | uint16(c[3])<<7}, nil
default:
if len(b) != ControlLength {
return Frame{}, fmt.Errorf("%w: U frame with payload", ErrInvalidLength)
}
fn := UFunction(c[0])
if !fn.Valid() || c[1] != 0 || c[2] != 0 || c[3] != 0 {
return Frame{}, fmt.Errorf("%w: U frame % X", ErrInvalidControl, c)
}
return Frame{Format: FormatU, Function: fn}, nil
}
}
// ReadFrame reads exactly one APDU from r. It never reads past the end of
// the frame, so r needs no buffering for correctness. The returned ASDU is
// freshly allocated.
//
// A clean end of stream before the first octet yields io.EOF; a stream that
// ends inside a frame yields io.ErrUnexpectedEOF.
func ReadFrame(r io.Reader) (Frame, error) {
var hdr [HeaderLength]byte
if _, err := io.ReadFull(r, hdr[:]); err != nil {
return Frame{}, err
}
if hdr[0] != StartByte {
return Frame{}, fmt.Errorf("%w: 0x%02X", ErrInvalidStart, hdr[0])
}
n := int(hdr[1])
if n < ControlLength || n > MaxAPDULength {
return Frame{}, fmt.Errorf("%w: length octet %d", ErrInvalidLength, n)
}
body := make([]byte, n)
if _, err := io.ReadFull(r, body); err != nil {
if errors.Is(err, io.EOF) {
err = io.ErrUnexpectedEOF
}
return Frame{}, err
}
return parseBody(body)
}