2018-09-25 13:26:06 +00:00
|
|
|
package apdu
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2019-03-14 11:01:44 +00:00
|
|
|
"encoding/binary"
|
|
|
|
"errors"
|
2018-09-25 13:26:06 +00:00
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
)
|
|
|
|
|
2019-03-14 11:01:44 +00:00
|
|
|
var (
|
|
|
|
ErrUnsupportedLenth80 = errors.New("length cannot be 0x80")
|
|
|
|
ErrLengthTooBig = errors.New("length cannot be more than 3 bytes")
|
|
|
|
)
|
|
|
|
|
2018-10-05 11:03:58 +00:00
|
|
|
// ErrTagNotFound is an error returned if a tag is not found in a TLV sequence.
|
2018-09-25 13:26:06 +00:00
|
|
|
type ErrTagNotFound struct {
|
|
|
|
tag uint8
|
|
|
|
}
|
|
|
|
|
2018-10-05 11:03:58 +00:00
|
|
|
// Error implements the error interface
|
2018-09-25 13:26:06 +00:00
|
|
|
func (e *ErrTagNotFound) Error() string {
|
|
|
|
return fmt.Sprintf("tag %x not found", e.tag)
|
|
|
|
}
|
|
|
|
|
2018-10-05 11:03:58 +00:00
|
|
|
// FindTag searches for a tag value within a TLV sequence.
|
2018-09-25 13:26:06 +00:00
|
|
|
func FindTag(raw []byte, tags ...uint8) ([]byte, error) {
|
2018-10-24 11:42:00 +00:00
|
|
|
return findTag(raw, 0, tags...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// FindTagN searches for a tag value within a TLV sequence and returns the n occurrence
|
|
|
|
func FindTagN(raw []byte, n int, tags ...uint8) ([]byte, error) {
|
|
|
|
return findTag(raw, n, tags...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func findTag(raw []byte, occurrence int, tags ...uint8) ([]byte, error) {
|
2018-09-25 13:26:06 +00:00
|
|
|
if len(tags) == 0 {
|
|
|
|
return raw, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
target := tags[0]
|
|
|
|
buf := bytes.NewBuffer(raw)
|
|
|
|
|
|
|
|
var (
|
|
|
|
tag uint8
|
2019-03-14 11:01:44 +00:00
|
|
|
length uint32
|
2018-09-25 13:26:06 +00:00
|
|
|
err error
|
|
|
|
)
|
|
|
|
|
|
|
|
for {
|
|
|
|
tag, err = buf.ReadByte()
|
|
|
|
switch {
|
|
|
|
case err == io.EOF:
|
|
|
|
return []byte{}, &ErrTagNotFound{target}
|
|
|
|
case err != nil:
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-03-14 11:01:44 +00:00
|
|
|
length, buf, err = parseLength(buf)
|
2018-09-25 13:26:06 +00:00
|
|
|
data := make([]byte, length)
|
|
|
|
if length != 0 {
|
|
|
|
_, err = buf.Read(data)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if tag == target {
|
2018-10-24 11:42:00 +00:00
|
|
|
// if it's the last tag in the search path, we start counting the occurrences
|
|
|
|
if len(tags) == 1 && occurrence > 0 {
|
|
|
|
occurrence--
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2018-09-25 13:26:06 +00:00
|
|
|
if len(tags) == 1 {
|
|
|
|
return data, nil
|
|
|
|
}
|
|
|
|
|
2018-10-24 11:42:00 +00:00
|
|
|
return findTag(data, occurrence, tags[1:]...)
|
2018-09-25 13:26:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-03-14 11:01:44 +00:00
|
|
|
|
|
|
|
func parseLength(buf *bytes.Buffer) (uint32, *bytes.Buffer, error) {
|
|
|
|
length, err := buf.ReadByte()
|
|
|
|
if err != nil {
|
|
|
|
return 0, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if length == 0x80 {
|
|
|
|
return 0, nil, ErrUnsupportedLenth80
|
|
|
|
}
|
|
|
|
|
|
|
|
if length > 0x80 {
|
|
|
|
lengthSize := length - 0x80
|
|
|
|
if lengthSize > 3 {
|
|
|
|
return 0, nil, ErrLengthTooBig
|
|
|
|
}
|
|
|
|
|
|
|
|
data := make([]byte, lengthSize)
|
|
|
|
_, err = buf.Read(data)
|
|
|
|
if err != nil {
|
|
|
|
return 0, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
num := make([]byte, 4)
|
|
|
|
copy(num[4-lengthSize:], data)
|
|
|
|
|
|
|
|
return binary.BigEndian.Uint32(num), buf, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return uint32(length), buf, nil
|
|
|
|
}
|