keycard-go/globalplatform/secure_channel.go

39 lines
957 B
Go
Raw Normal View History

package globalplatform
import (
2019-03-01 18:44:07 +01:00
"github.com/status-im/keycard-go/apdu"
"github.com/status-im/keycard-go/hexutils"
)
2019-03-05 12:00:23 +01:00
// SecureChannel wraps another channel and sends wrapped commands using SCP02Wrapper.
type SecureChannel struct {
session *Session
c Channel
2019-03-05 12:00:23 +01:00
w *SCP02Wrapper
}
2018-10-05 16:40:32 +02: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,
2019-03-06 10:43:37 +01:00
w: NewSCP02Wrapper(session.Keys().Mac()),
}
}
2018-10-05 16:40:32 +02: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 11:35:56 +02: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)
}