Branch data Line data Source code
1 : : /**
2 : : * Copyright Notice:
3 : : * Copyright 2025-2026 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_responder_lib.h"
8 : :
9 : : #if (LIBSPDM_ENABLE_CAPABILITY_ENCAP_CAP) && (LIBSPDM_ENABLE_CAPABILITY_EVENT_CAP)
10 : :
11 : 7 : libspdm_return_t libspdm_get_encap_request_send_event(
12 : : void *context,
13 : : uint32_t session_id,
14 : : size_t *encap_request_size,
15 : : void *encap_request)
16 : : {
17 : : libspdm_context_t *spdm_context;
18 : : libspdm_encap_context_t *encap_context;
19 : : spdm_send_event_request_t *spdm_request;
20 : : libspdm_session_info_t *session_info;
21 : : libspdm_session_state_t session_state;
22 : : uint32_t event_count;
23 : : size_t events_list_size;
24 : :
25 : 7 : spdm_context = context;
26 : 7 : spdm_request = encap_request;
27 : :
28 : 7 : encap_context = libspdm_get_encap_context(spdm_context, &session_id);
29 [ + + ]: 7 : if (encap_context == NULL) {
30 : : /* session_id does not refer to an existing session. */
31 : 1 : return LIBSPDM_STATUS_INVALID_STATE_LOCAL;
32 : : }
33 : :
34 : 6 : encap_context->last_encap_request_size = 0;
35 : :
36 : 6 : session_info = libspdm_get_session_info_via_session_id(spdm_context, session_id);
37 [ - + ]: 6 : if (session_info == NULL) {
38 : 0 : return LIBSPDM_STATUS_INVALID_STATE_LOCAL;
39 : : }
40 : 6 : session_state = libspdm_secured_message_get_session_state(
41 : : session_info->secured_message_context);
42 [ + + ]: 6 : if (session_state != LIBSPDM_SESSION_STATE_ESTABLISHED) {
43 : 1 : return LIBSPDM_STATUS_INVALID_STATE_LOCAL;
44 : : }
45 : :
46 : 5 : spdm_request->header.spdm_version = libspdm_get_connection_version(spdm_context);
47 : 5 : spdm_request->header.request_response_code = SPDM_SEND_EVENT;
48 : 5 : spdm_request->header.param1 = 0;
49 : 5 : spdm_request->header.param2 = 0;
50 : :
51 : 5 : events_list_size = *encap_request_size - sizeof(spdm_send_event_request_t);
52 : :
53 [ + + ]: 5 : if (!libspdm_generate_event_list(spdm_context, libspdm_get_connection_version(spdm_context),
54 : : session_id, &event_count, &events_list_size,
55 : 5 : (uint8_t *)(spdm_request + 1))) {
56 : 1 : return LIBSPDM_STATUS_INVALID_STATE_LOCAL;
57 : : }
58 : :
59 [ - + ]: 4 : LIBSPDM_ASSERT(event_count != 0);
60 [ - + ]: 4 : LIBSPDM_ASSERT(events_list_size != 0);
61 : :
62 : 4 : spdm_request->event_count = event_count;
63 : :
64 : 4 : *encap_request_size = sizeof(spdm_send_event_request_t) + events_list_size;
65 : :
66 : 4 : libspdm_copy_mem(&encap_context->last_encap_request_header,
67 : : sizeof(encap_context->last_encap_request_header),
68 : 4 : &spdm_request->header, sizeof(spdm_message_header_t));
69 : 4 : encap_context->last_encap_request_size = *encap_request_size;
70 : :
71 : 4 : return LIBSPDM_STATUS_SUCCESS;
72 : : }
73 : :
74 : 4 : libspdm_return_t libspdm_process_encap_response_event_ack(
75 : : libspdm_context_t *spdm_context, size_t encap_response_size,
76 : : const void *encap_response, bool *need_continue)
77 : : {
78 : : libspdm_return_t status;
79 : : const spdm_event_ack_response_t *spdm_response;
80 : : size_t spdm_response_size;
81 : : uint32_t session_id;
82 : :
83 [ + + ]: 4 : if (spdm_context->last_spdm_request_session_id_valid) {
84 : : libspdm_session_info_t *session_info;
85 : : libspdm_session_state_t session_state;
86 : :
87 : 3 : session_id = spdm_context->last_spdm_request_session_id;
88 : 3 : session_info = libspdm_get_session_info_via_session_id(spdm_context, session_id);
89 : :
90 [ - + ]: 3 : if (session_info == NULL) {
91 : 0 : return LIBSPDM_STATUS_INVALID_STATE_LOCAL;
92 : : }
93 : :
94 : 3 : session_state = libspdm_secured_message_get_session_state(
95 : : session_info->secured_message_context);
96 : :
97 [ - + ]: 3 : if (session_state != LIBSPDM_SESSION_STATE_ESTABLISHED) {
98 : 0 : return LIBSPDM_STATUS_INVALID_STATE_LOCAL;
99 : : }
100 : : } else {
101 : 1 : return LIBSPDM_STATUS_ERROR_PEER;
102 : : }
103 : :
104 : 3 : spdm_response = encap_response;
105 : 3 : spdm_response_size = encap_response_size;
106 : :
107 [ + + ]: 3 : if (spdm_response->header.spdm_version != libspdm_get_connection_version(spdm_context)) {
108 : 1 : return LIBSPDM_STATUS_INVALID_MSG_FIELD;
109 : : }
110 : :
111 [ - + ]: 2 : if (spdm_response->header.request_response_code == SPDM_ERROR) {
112 : 0 : status = libspdm_handle_encap_error_response_main(spdm_response->header.param1);
113 [ # # ]: 0 : if (LIBSPDM_STATUS_IS_ERROR(status)) {
114 : 0 : return status;
115 : : }
116 [ - + ]: 2 : } else if (spdm_response->header.request_response_code != SPDM_EVENT_ACK) {
117 : 0 : return LIBSPDM_STATUS_INVALID_MSG_FIELD;
118 : : }
119 : :
120 [ - + ]: 2 : if (spdm_response_size != sizeof(spdm_event_ack_response_t)) {
121 : 0 : return LIBSPDM_STATUS_INVALID_MSG_SIZE;
122 : : }
123 : :
124 : 2 : *need_continue = false;
125 : :
126 : 2 : return LIBSPDM_STATUS_SUCCESS;
127 : : }
128 : :
129 : : #endif /* (LIBSPDM_ENABLE_CAPABILITY_ENCAP_CAP) && (LIBSPDM_ENABLE_CAPABILITY_EVENT_CAP) */
|