Mike Frysinger | 3a446f2 | 2022-09-08 07:37:14 | [diff] [blame] | 1 | // Copyright 2022 The ChromiumOS Authors |
Leo Lai | d045adc | 2022-03-31 07:42:52 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "trunks/real_response_serializer.h" |
| 6 | |
| 7 | #include <string> |
| 8 | |
Leo Lai | aa020c0 | 2022-03-31 07:42:52 | [diff] [blame] | 9 | #include <base/logging.h> |
| 10 | |
| 11 | #include "trunks/authorization_delegate.h" |
Leo Lai | d045adc | 2022-03-31 07:42:52 | [diff] [blame] | 12 | #include "trunks/command_parser.h" |
| 13 | #include "trunks/tpm_generated.h" |
| 14 | #include "trunks/trunks_export.h" |
| 15 | |
| 16 | namespace trunks { |
| 17 | |
| 18 | void RealResponseSerializer::SerializeHeaderOnlyResponse( |
| 19 | TPM_RC rc, std::string* response) { |
| 20 | const TPMI_ST_COMMAND_TAG tag = |
| 21 | (rc == TPM_RC_BAD_TAG ? TPM_ST_RSP_COMMAND : TPM_ST_NO_SESSIONS); |
| 22 | Serialize_TPMI_ST_COMMAND_TAG(tag, response); |
| 23 | Serialize_UINT32(kHeaderSize, response); |
| 24 | Serialize_TPM_RC(rc, response); |
| 25 | } |
| 26 | |
Leo Lai | 40e33d5 | 2022-03-31 07:42:52 | [diff] [blame] | 27 | void RealResponseSerializer::SerializeResponseGetCapability( |
| 28 | TPMI_YES_NO has_more, |
| 29 | const TPMS_CAPABILITY_DATA& cap_data, |
| 30 | std::string* response) { |
| 31 | std::string buffer; |
| 32 | Serialize_TPMI_YES_NO(has_more, &buffer); |
| 33 | Serialize_TPMS_CAPABILITY_DATA(cap_data, &buffer); |
| 34 | const UINT32 size = kHeaderSize + buffer.size(); |
| 35 | // Session is not supported. |
| 36 | Serialize_TPMI_ST_COMMAND_TAG(TPM_ST_NO_SESSIONS, response); |
| 37 | Serialize_UINT32(size, response); |
| 38 | Serialize_TPM_RC(TPM_RC_SUCCESS, response); |
| 39 | response->append(buffer); |
| 40 | } |
| 41 | |
Leo Lai | aa020c0 | 2022-03-31 07:42:52 | [diff] [blame] | 42 | void RealResponseSerializer::SerializeResponseNvRead( |
| 43 | const TPM2B_MAX_NV_BUFFER& data, std::string* response) { |
| 44 | std::string parameter; |
| 45 | Serialize_TPM2B_MAX_NV_BUFFER(data, ¶meter); |
| 46 | // For now, only password session is supported, so just hard-code the logic. |
| 47 | TPMS_AUTH_RESPONSE auth = {}; |
| 48 | auth.session_attributes = kContinueSession; |
| 49 | std::string auth_section; |
| 50 | Serialize_TPMS_AUTH_RESPONSE(auth, &auth_section); |
| 51 | std::string parameter_size; |
| 52 | Serialize_UINT32(parameter.size(), ¶meter_size); |
| 53 | |
| 54 | const UINT32 size = kHeaderSize + parameter_size.size() + parameter.size() + |
| 55 | auth_section.size(); |
| 56 | // Serialize header. |
| 57 | std::string header; |
| 58 | // Session is required. |
| 59 | Serialize_TPMI_ST_COMMAND_TAG(TPM_ST_SESSIONS, &header); |
| 60 | Serialize_UINT32(size, &header); |
| 61 | Serialize_TPM_RC(TPM_RC_SUCCESS, &header); |
| 62 | |
| 63 | *response = header + parameter_size + parameter + auth_section; |
| 64 | } |
| 65 | |
Leo Lai | d6475cf | 2022-03-31 07:42:52 | [diff] [blame] | 66 | void RealResponseSerializer::SerializeResponseNvReadPublic( |
| 67 | const TPM2B_NV_PUBLIC& nv_public, |
| 68 | const TPM2B_NAME& nv_name, |
| 69 | std::string* response) { |
| 70 | std::string buffer; |
| 71 | Serialize_TPM2B_NV_PUBLIC(nv_public, &buffer); |
| 72 | Serialize_TPM2B_NAME(nv_name, &buffer); |
| 73 | const UINT32 size = kHeaderSize + buffer.size(); |
| 74 | // Session is not supported. |
| 75 | Serialize_TPMI_ST_COMMAND_TAG(TPM_ST_NO_SESSIONS, response); |
| 76 | Serialize_UINT32(size, response); |
| 77 | Serialize_TPM_RC(TPM_RC_SUCCESS, response); |
| 78 | response->append(buffer); |
| 79 | } |
| 80 | |
Leo Lai | d045adc | 2022-03-31 07:42:52 | [diff] [blame] | 81 | } // namespace trunks |