2018-10-05 11:53:35 +00:00
|
|
|
package globalplatform
|
2018-10-02 11:25:04 +00:00
|
|
|
|
|
|
|
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-02 11:25:04 +00:00
|
|
|
)
|
|
|
|
|
2018-10-05 14:40:32 +00:00
|
|
|
// SecureChannel wraps another channel and sends wrapped commands using APDUWrapper.
|
2018-10-02 11:25:04 +00:00
|
|
|
type SecureChannel struct {
|
2018-10-05 11:53:35 +00:00
|
|
|
session *Session
|
2018-10-02 11:25:04 +00:00
|
|
|
c Channel
|
2018-10-05 11:53:35 +00:00
|
|
|
w *APDUWrapper
|
2018-10-02 11:25:04 +00:00
|
|
|
}
|
|
|
|
|
2018-10-05 14:40:32 +00:00
|
|
|
// NewSecureChannel returns a new SecureChannel based on a session and wrapping a Channel c.
|
2018-10-05 11:53:35 +00:00
|
|
|
func NewSecureChannel(session *Session, c Channel) *SecureChannel {
|
2018-10-02 11:25:04 +00:00
|
|
|
return &SecureChannel{
|
|
|
|
session: session,
|
|
|
|
c: c,
|
2018-10-05 11:53:35 +00:00
|
|
|
w: NewAPDUWrapper(session.KeyProvider().Mac()),
|
2018-10-02 11:25:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-05 14:40:32 +00:00
|
|
|
// Send sends wrapped commands to the inner channel.
|
2018-10-02 11:25:04 +00:00
|
|
|
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))
|
2018-10-02 11:25:04 +00:00
|
|
|
wrappedCmd, err := c.w.Wrap(cmd)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.c.Send(wrappedCmd)
|
|
|
|
}
|