keycard-go/globalplatform/secure_channel.go

39 lines
977 B
Go
Raw Normal View History

package globalplatform
import (
2018-11-06 09:25:54 +00:00
"github.com/status-im/hardware-wallet-go/apdu"
"github.com/status-im/hardware-wallet-go/hexutils"
)
2018-10-05 14:40:32 +00:00
// SecureChannel wraps another channel and sends wrapped commands using APDUWrapper.
type SecureChannel struct {
session *Session
c Channel
w *APDUWrapper
}
2018-10-05 14:40:32 +00:00
// NewSecureChannel returns a new SecureChannel based on a session and wrapping a Channel c.
func NewSecureChannel(session *Session, c Channel) *SecureChannel {
return &SecureChannel{
session: session,
c: c,
w: NewAPDUWrapper(session.KeyProvider().Mac()),
}
}
2018-10-05 14:40:32 +00:00
// Send sends wrapped commands to the inner channel.
func (c *SecureChannel) Send(cmd *apdu.Command) (*apdu.Response, error) {
rawCmd, err := cmd.Serialize()
if err != nil {
return nil, err
}
2018-10-05 09:35:56 +00:00
logger.Debug("wrapping apdu command", "hex", hexutils.BytesToHexWithSpaces(rawCmd))
wrappedCmd, err := c.w.Wrap(cmd)
if err != nil {
return nil, err
}
return c.c.Send(wrappedCmd)
}