Add ListObject and GetObjectInfo support
Misc: removed pinged print
This commit is contained in:
parent
4f80722970
commit
b28a514e8f
|
@ -4,6 +4,7 @@ import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"encoding/binary"
|
"encoding/binary"
|
||||||
"errors"
|
"errors"
|
||||||
|
"io"
|
||||||
)
|
)
|
||||||
|
|
||||||
func CreateCreateSessionCommand(keySetID uint16, hostChallenge []byte) (*CommandMessage, error) {
|
func CreateCreateSessionCommand(keySetID uint16, hostChallenge []byte) (*CommandMessage, error) {
|
||||||
|
@ -118,6 +119,51 @@ func CreatePutAsymmetricKeyCommand(keyID uint16, label []byte, domains uint16, c
|
||||||
return command, nil
|
return command, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type ListCommandOption func(w io.Writer)
|
||||||
|
|
||||||
|
func NewObjectTypeOption(objectType uint8) ListCommandOption {
|
||||||
|
return func(w io.Writer) {
|
||||||
|
binary.Write(w, binary.BigEndian, ListObjectParamType)
|
||||||
|
binary.Write(w, binary.BigEndian, objectType)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewIDOption(id uint16) ListCommandOption {
|
||||||
|
return func(w io.Writer) {
|
||||||
|
binary.Write(w, binary.BigEndian, ListObjectParamID)
|
||||||
|
binary.Write(w, binary.BigEndian, id)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func CreateListObjectsCommand(options ...ListCommandOption) (*CommandMessage, error) {
|
||||||
|
command := &CommandMessage{
|
||||||
|
CommandType: CommandTypeListObjects,
|
||||||
|
}
|
||||||
|
|
||||||
|
payload := bytes.NewBuffer([]byte{})
|
||||||
|
for _, opt := range options {
|
||||||
|
opt(payload)
|
||||||
|
}
|
||||||
|
|
||||||
|
command.Data = payload.Bytes()
|
||||||
|
|
||||||
|
return command, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func CreateGetObjectInfoCommand(keyID uint16, objectType uint8) (*CommandMessage, error) {
|
||||||
|
command := &CommandMessage{
|
||||||
|
CommandType: CommandTypeGetObjectInfo,
|
||||||
|
}
|
||||||
|
|
||||||
|
payload := bytes.NewBuffer([]byte{})
|
||||||
|
binary.Write(payload, binary.BigEndian, keyID)
|
||||||
|
binary.Write(payload, binary.BigEndian, objectType)
|
||||||
|
|
||||||
|
command.Data = payload.Bytes()
|
||||||
|
|
||||||
|
return command, nil
|
||||||
|
}
|
||||||
|
|
||||||
func CreateCloseSessionCommand() (*CommandMessage, error) {
|
func CreateCloseSessionCommand() (*CommandMessage, error) {
|
||||||
command := &CommandMessage{
|
command := &CommandMessage{
|
||||||
CommandType: CommandTypeCloseSession,
|
CommandType: CommandTypeCloseSession,
|
||||||
|
|
|
@ -35,6 +35,29 @@ type (
|
||||||
KeyID uint16
|
KeyID uint16
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ObjectInfoResponse struct {
|
||||||
|
Capabilities uint64
|
||||||
|
ObjectID uint16
|
||||||
|
Length uint16
|
||||||
|
Domains uint16
|
||||||
|
Type uint8
|
||||||
|
Algorithm Algorithm
|
||||||
|
Sequence uint8
|
||||||
|
Origin uint8
|
||||||
|
Label [40]byte
|
||||||
|
DelegatedCapabilites uint64
|
||||||
|
}
|
||||||
|
|
||||||
|
Object struct {
|
||||||
|
ObjectID uint16
|
||||||
|
ObjectType uint8
|
||||||
|
Sequence uint8
|
||||||
|
}
|
||||||
|
|
||||||
|
ListObjectsResponse struct {
|
||||||
|
Objects []Object
|
||||||
|
}
|
||||||
|
|
||||||
SignDataEddsaResponse struct {
|
SignDataEddsaResponse struct {
|
||||||
Signature []byte
|
Signature []byte
|
||||||
}
|
}
|
||||||
|
@ -90,6 +113,10 @@ func ParseResponse(data []byte) (Response, error) {
|
||||||
return parseSignDataEcdsaResponse(payload)
|
return parseSignDataEcdsaResponse(payload)
|
||||||
case CommandTypePutAsymmetric:
|
case CommandTypePutAsymmetric:
|
||||||
return parsePutAsymmetricKeyResponse(payload)
|
return parsePutAsymmetricKeyResponse(payload)
|
||||||
|
case CommandTypeListObjects:
|
||||||
|
return parseListObjectsResponse(payload)
|
||||||
|
case CommandTypeGetObjectInfo:
|
||||||
|
return parseGetObjectInfoResponse(payload)
|
||||||
case CommandTypeCloseSession:
|
case CommandTypeCloseSession:
|
||||||
return nil, nil
|
return nil, nil
|
||||||
case CommandTypeGetPubKey:
|
case CommandTypeGetPubKey:
|
||||||
|
@ -179,6 +206,34 @@ func parsePutAsymmetricKeyResponse(payload []byte) (Response, error) {
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func parseListObjectsResponse(payload []byte) (Response, error) {
|
||||||
|
if len(payload)%4 != 0 {
|
||||||
|
return nil, errors.New("invalid response payload length")
|
||||||
|
}
|
||||||
|
|
||||||
|
response := ListObjectsResponse{
|
||||||
|
Objects: make([]Object, len(payload)/4),
|
||||||
|
}
|
||||||
|
|
||||||
|
err := binary.Read(bytes.NewReader(payload), binary.BigEndian, &response.Objects)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return &response, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func parseGetObjectInfoResponse(payload []byte) (Response, error) {
|
||||||
|
response := ObjectInfoResponse{}
|
||||||
|
|
||||||
|
err := binary.Read(bytes.NewReader(payload), binary.BigEndian, &response)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return &response, nil
|
||||||
|
}
|
||||||
|
|
||||||
func parseGetPubKeyResponse(payload []byte) (Response, error) {
|
func parseGetPubKeyResponse(payload []byte) (Response, error) {
|
||||||
if len(payload) < 1 {
|
if len(payload) < 1 {
|
||||||
return nil, errors.New("invalid response payload length")
|
return nil, errors.New("invalid response payload length")
|
||||||
|
|
|
@ -78,6 +78,7 @@ const (
|
||||||
ErrorCodeCommandUnexecuted ErrorCode = 0xff
|
ErrorCodeCommandUnexecuted ErrorCode = 0xff
|
||||||
|
|
||||||
// Algorithms
|
// Algorithms
|
||||||
|
AlgorithmP256 Algorithm = 12
|
||||||
AlgorithmSecp256k1 Algorithm = 15
|
AlgorithmSecp256k1 Algorithm = 15
|
||||||
AlgorighmED25519 Algorithm = 46
|
AlgorighmED25519 Algorithm = 46
|
||||||
|
|
||||||
|
@ -155,4 +156,8 @@ const (
|
||||||
ObjectTypeHmacKey uint8 = 0x05
|
ObjectTypeHmacKey uint8 = 0x05
|
||||||
ObjectTypeTemplate uint8 = 0x06
|
ObjectTypeTemplate uint8 = 0x06
|
||||||
ObjectTypeOtpAeadKey uint8 = 0x07
|
ObjectTypeOtpAeadKey uint8 = 0x07
|
||||||
|
|
||||||
|
// list objects params
|
||||||
|
ListObjectParamID uint8 = 0x01
|
||||||
|
ListObjectParamType uint8 = 0x02
|
||||||
)
|
)
|
||||||
|
|
|
@ -71,7 +71,6 @@ func (s *SessionManager) pingRoutine() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
println("pinged")
|
|
||||||
s.keepAlive.Reset(pingInterval)
|
s.keepAlive.Reset(pingInterval)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue