keycard-go/globalplatform/secure_channel.go

39 lines
965 B
Go
Raw Normal View History

package globalplatform
import (
2018-10-19 08:54:02 +00:00
"github.com/status-im/smartcard-go/apdu"
"github.com/status-im/smartcard-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)
}