blob: e67695a0a82ddf03c929c8d984ead0099b9783d3 [file] [log] [blame]
Mike Frysinger3a446f22022-09-08 07:37:141// Copyright 2022 The ChromiumOS Authors
Leo Laid045adc2022-03-31 07:42:522// 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 Laiaa020c02022-03-31 07:42:529#include <base/logging.h>
10
11#include "trunks/authorization_delegate.h"
Leo Laid045adc2022-03-31 07:42:5212#include "trunks/command_parser.h"
13#include "trunks/tpm_generated.h"
14#include "trunks/trunks_export.h"
15
16namespace trunks {
17
18void 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 Lai40e33d52022-03-31 07:42:5227void 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 Laiaa020c02022-03-31 07:42:5242void RealResponseSerializer::SerializeResponseNvRead(
43 const TPM2B_MAX_NV_BUFFER& data, std::string* response) {
44 std::string parameter;
45 Serialize_TPM2B_MAX_NV_BUFFER(data, &parameter);
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(), &parameter_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 Laid6475cf2022-03-31 07:42:5266void 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 Laid045adc2022-03-31 07:42:5281} // namespace trunks