apdu.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. // Copyright 2018 The go-ethereum Authors
  2. // This file is part of the go-ethereum library.
  3. //
  4. // The go-ethereum library is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Lesser General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // The go-ethereum library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Lesser General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Lesser General Public License
  15. // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
  16. package scwallet
  17. import (
  18. "bytes"
  19. "encoding/binary"
  20. "fmt"
  21. )
  22. // commandAPDU represents an application data unit sent to a smartcard.
  23. type commandAPDU struct {
  24. Cla, Ins, P1, P2 uint8 // Class, Instruction, Parameter 1, Parameter 2
  25. Data []byte // Command data
  26. Le uint8 // Command data length
  27. }
  28. // serialize serializes a command APDU.
  29. func (ca commandAPDU) serialize() ([]byte, error) {
  30. buf := new(bytes.Buffer)
  31. if err := binary.Write(buf, binary.BigEndian, ca.Cla); err != nil {
  32. return nil, err
  33. }
  34. if err := binary.Write(buf, binary.BigEndian, ca.Ins); err != nil {
  35. return nil, err
  36. }
  37. if err := binary.Write(buf, binary.BigEndian, ca.P1); err != nil {
  38. return nil, err
  39. }
  40. if err := binary.Write(buf, binary.BigEndian, ca.P2); err != nil {
  41. return nil, err
  42. }
  43. if len(ca.Data) > 0 {
  44. if err := binary.Write(buf, binary.BigEndian, uint8(len(ca.Data))); err != nil {
  45. return nil, err
  46. }
  47. if err := binary.Write(buf, binary.BigEndian, ca.Data); err != nil {
  48. return nil, err
  49. }
  50. }
  51. if err := binary.Write(buf, binary.BigEndian, ca.Le); err != nil {
  52. return nil, err
  53. }
  54. return buf.Bytes(), nil
  55. }
  56. // responseAPDU represents an application data unit received from a smart card.
  57. type responseAPDU struct {
  58. Data []byte // response data
  59. Sw1, Sw2 uint8 // status words 1 and 2
  60. }
  61. // deserialize deserializes a response APDU.
  62. func (ra *responseAPDU) deserialize(data []byte) error {
  63. if len(data) < 2 {
  64. return fmt.Errorf("can not deserialize data: payload too short (%d < 2)", len(data))
  65. }
  66. ra.Data = make([]byte, len(data)-2)
  67. buf := bytes.NewReader(data)
  68. if err := binary.Read(buf, binary.BigEndian, &ra.Data); err != nil {
  69. return err
  70. }
  71. if err := binary.Read(buf, binary.BigEndian, &ra.Sw1); err != nil {
  72. return err
  73. }
  74. if err := binary.Read(buf, binary.BigEndian, &ra.Sw2); err != nil {
  75. return err
  76. }
  77. return nil
  78. }