Line data Source code
1 : /**
2 : * Copyright Notice:
3 : * Copyright 2021-2024 DMTF. All rights reserved.
4 : * License: BSD 3-Clause License. For full text see link: https://github.com/DMTF/libspdm/blob/main/LICENSE.md
5 : **/
6 :
7 : #include "internal/libspdm_requester_lib.h"
8 :
9 : #if (LIBSPDM_ENABLE_CAPABILITY_MUT_AUTH_CAP) && (LIBSPDM_ENABLE_CAPABILITY_ENCAP_CAP) && \
10 : (LIBSPDM_ENABLE_CAPABILITY_CERT_CAP)
11 :
12 1406 : libspdm_return_t libspdm_get_encap_response_certificate(void *spdm_context,
13 : size_t request_size,
14 : void *request,
15 : size_t *response_size,
16 : void *response)
17 : {
18 : spdm_get_certificate_request_t *spdm_request;
19 : spdm_certificate_response_t *spdm_response;
20 : uint16_t offset;
21 : uint16_t length;
22 : size_t remainder_length;
23 : uint8_t slot_id;
24 : libspdm_context_t *context;
25 : libspdm_return_t status;
26 : size_t response_capacity;
27 :
28 1406 : context = spdm_context;
29 1406 : spdm_request = request;
30 :
31 1406 : if (libspdm_get_connection_version(context) < SPDM_MESSAGE_VERSION_11) {
32 0 : return libspdm_generate_encap_error_response(
33 : context, SPDM_ERROR_CODE_UNSUPPORTED_REQUEST,
34 : SPDM_GET_CERTIFICATE, response_size, response);
35 : }
36 :
37 1406 : if (spdm_request->header.spdm_version != libspdm_get_connection_version(context)) {
38 0 : return libspdm_generate_encap_error_response(
39 : context, SPDM_ERROR_CODE_VERSION_MISMATCH,
40 : 0, response_size, response);
41 : }
42 :
43 1406 : if (!libspdm_is_capabilities_flag_supported(
44 : context, true,
45 : SPDM_GET_CAPABILITIES_REQUEST_FLAGS_CERT_CAP, 0)) {
46 0 : return libspdm_generate_encap_error_response(
47 : context, SPDM_ERROR_CODE_UNSUPPORTED_REQUEST,
48 : SPDM_GET_CERTIFICATE, response_size, response);
49 : }
50 :
51 1406 : if (request_size < sizeof(spdm_get_certificate_request_t)) {
52 0 : return libspdm_generate_encap_error_response(
53 : context, SPDM_ERROR_CODE_INVALID_REQUEST, 0,
54 : response_size, response);
55 : }
56 :
57 1406 : slot_id = spdm_request->header.param1;
58 :
59 1406 : if (slot_id >= SPDM_MAX_SLOT_COUNT) {
60 0 : return libspdm_generate_encap_error_response(
61 : context, SPDM_ERROR_CODE_INVALID_REQUEST, 0,
62 : response_size, response);
63 : }
64 :
65 1406 : if (context->local_context
66 1406 : .local_cert_chain_provision[slot_id] == NULL) {
67 0 : return libspdm_generate_encap_error_response(
68 : context, SPDM_ERROR_CODE_UNSPECIFIED,
69 : 0, response_size, response);
70 : }
71 :
72 1406 : offset = spdm_request->offset;
73 1406 : length = spdm_request->length;
74 :
75 1406 : if (spdm_request->header.spdm_version >= SPDM_MESSAGE_VERSION_13) {
76 1 : if (spdm_request->header.param2 &
77 : SPDM_GET_CERTIFICATE_REQUEST_ATTRIBUTES_SLOT_SIZE_REQUESTED) {
78 1 : offset = 0;
79 1 : length = 0;
80 : }
81 : }
82 :
83 1406 : if (length > LIBSPDM_MAX_CERT_CHAIN_BLOCK_LEN) {
84 0 : length = LIBSPDM_MAX_CERT_CHAIN_BLOCK_LEN;
85 : }
86 :
87 1406 : if (offset >= context->local_context
88 1406 : .local_cert_chain_provision_size[slot_id]) {
89 6 : return libspdm_generate_encap_error_response(
90 : context, SPDM_ERROR_CODE_INVALID_REQUEST, 0,
91 : response_size, response);
92 : }
93 :
94 1400 : if ((size_t)(offset + length) >
95 : context->local_context
96 1400 : .local_cert_chain_provision_size[slot_id]) {
97 2 : length = (uint16_t)(
98 : context->local_context
99 2 : .local_cert_chain_provision_size[slot_id] -
100 : offset);
101 : }
102 1400 : remainder_length = context->local_context
103 1400 : .local_cert_chain_provision_size[slot_id] -
104 1400 : (length + offset);
105 :
106 1400 : libspdm_reset_message_buffer_via_request_code(context, NULL,
107 1400 : spdm_request->header.request_response_code);
108 :
109 1400 : LIBSPDM_ASSERT(*response_size >= sizeof(spdm_certificate_response_t) + length);
110 1400 : response_capacity = *response_size;
111 1400 : *response_size = sizeof(spdm_certificate_response_t) + length;
112 1400 : libspdm_zero_mem(response, *response_size);
113 1400 : spdm_response = response;
114 :
115 1400 : spdm_response->header.spdm_version = spdm_request->header.spdm_version;
116 1400 : spdm_response->header.request_response_code = SPDM_CERTIFICATE;
117 1400 : spdm_response->header.param1 = slot_id;
118 1400 : spdm_response->header.param2 = 0;
119 1400 : if ((spdm_request->header.spdm_version >= SPDM_MESSAGE_VERSION_13) &&
120 1 : context->connection_info.multi_key_conn_req) {
121 0 : spdm_response->header.param2 = context->local_context.local_cert_info[slot_id];
122 : }
123 :
124 1400 : spdm_response->portion_length = length;
125 1400 : spdm_response->remainder_length = (uint16_t)remainder_length;
126 1400 : libspdm_copy_mem(spdm_response + 1,
127 : response_capacity - sizeof(spdm_certificate_response_t),
128 : (const uint8_t *)context->local_context
129 1400 : .local_cert_chain_provision[slot_id] + offset,
130 : length);
131 :
132 : /* Cache*/
133 :
134 1400 : status = libspdm_append_message_mut_b(context, spdm_request,
135 : request_size);
136 1400 : if (LIBSPDM_STATUS_IS_ERROR(status)) {
137 0 : return libspdm_generate_encap_error_response(
138 : context, SPDM_ERROR_CODE_UNSPECIFIED, 0,
139 : response_size, response);
140 : }
141 :
142 1400 : status = libspdm_append_message_mut_b(context, spdm_response,
143 : *response_size);
144 1400 : if (LIBSPDM_STATUS_IS_ERROR(status)) {
145 0 : return libspdm_generate_encap_error_response(
146 : context, SPDM_ERROR_CODE_UNSPECIFIED, 0,
147 : response_size, response);
148 : }
149 :
150 1400 : return LIBSPDM_STATUS_SUCCESS;
151 : }
152 :
153 : #endif /* (LIBSPDM_ENABLE_CAPABILITY_MUT_AUTH_CAP) && (..) */
|