apdu_response.h 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. // Copyright 2017 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef COMPONENTS_APDU_APDU_RESPONSE_H_
  5. #define COMPONENTS_APDU_APDU_RESPONSE_H_
  6. #include <stdint.h>
  7. #include <vector>
  8. #include "base/component_export.h"
  9. #include "base/containers/span.h"
  10. #include "base/gtest_prod_util.h"
  11. #include "third_party/abseil-cpp/absl/types/optional.h"
  12. namespace apdu {
  13. // APDU responses are defined as part of ISO 7816-4. Serialized responses
  14. // consist of a data field of varying length, up to a maximum 65536, and a
  15. // two byte status field.
  16. class COMPONENT_EXPORT(APDU) ApduResponse {
  17. public:
  18. // Status bytes are specified in ISO 7816-4.
  19. enum class Status : uint16_t {
  20. SW_NO_ERROR = 0x9000,
  21. SW_CONDITIONS_NOT_SATISFIED = 0x6985,
  22. SW_COMMAND_NOT_ALLOWED = 0x6986,
  23. SW_WRONG_DATA = 0x6A80,
  24. SW_WRONG_LENGTH = 0x6700,
  25. SW_INS_NOT_SUPPORTED = 0x6D00,
  26. };
  27. // Create a APDU response from the serialized message.
  28. static absl::optional<ApduResponse> CreateFromMessage(
  29. base::span<const uint8_t> data);
  30. ApduResponse(std::vector<uint8_t> data, Status response_status);
  31. ApduResponse(ApduResponse&& that);
  32. ApduResponse& operator=(ApduResponse&& that);
  33. ApduResponse(const ApduResponse&) = delete;
  34. ApduResponse& operator=(const ApduResponse&) = delete;
  35. ~ApduResponse();
  36. std::vector<uint8_t> GetEncodedResponse() const;
  37. const std::vector<uint8_t>& data() const { return data_; }
  38. Status status() const { return response_status_; }
  39. private:
  40. FRIEND_TEST_ALL_PREFIXES(ApduTest, TestDeserializeResponse);
  41. std::vector<uint8_t> data_;
  42. Status response_status_;
  43. };
  44. } // namespace apdu
  45. #endif // COMPONENTS_APDU_APDU_RESPONSE_H_