-
Notifications
You must be signed in to change notification settings - Fork 108
/
Copy pathlibspdm_req_key_exchange.c
786 lines (713 loc) · 31.6 KB
/
libspdm_req_key_exchange.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
/**
* Copyright Notice:
* Copyright 2021-2022 DMTF. All rights reserved.
* License: BSD 3-Clause License. For full text see link: https://github.com/DMTF/libspdm/blob/main/LICENSE.md
**/
#include "internal/libspdm_requester_lib.h"
#if LIBSPDM_ENABLE_CAPABILITY_KEY_EX_CAP
#pragma pack(1)
typedef struct {
spdm_message_header_t header;
uint16_t req_session_id;
uint8_t session_policy;
uint8_t reserved;
uint8_t random_data[SPDM_RANDOM_DATA_SIZE];
uint8_t exchange_data[LIBSPDM_MAX_DHE_KEY_SIZE];
uint16_t opaque_length;
uint8_t opaque_data[SPDM_MAX_OPAQUE_DATA_SIZE];
} libspdm_key_exchange_request_mine_t;
typedef struct {
spdm_message_header_t header;
uint16_t rsp_session_id;
uint8_t mut_auth_requested;
uint8_t req_slot_id_param;
uint8_t random_data[SPDM_RANDOM_DATA_SIZE];
uint8_t exchange_data[LIBSPDM_MAX_DHE_KEY_SIZE];
uint8_t measurement_summary_hash[LIBSPDM_MAX_HASH_SIZE];
uint16_t opaque_length;
uint8_t opaque_data[SPDM_MAX_OPAQUE_DATA_SIZE];
uint8_t signature[LIBSPDM_MAX_ASYM_KEY_SIZE];
uint8_t verify_data[LIBSPDM_MAX_HASH_SIZE];
} libspdm_key_exchange_response_max_t;
#pragma pack()
/**
* This function verifies the key exchange HMAC based upon TH.
*
* @param spdm_context A pointer to the SPDM context.
* @param session_info The session info of an SPDM session.
* @param hmac_data The HMAC data buffer.
* @param hmac_data_size size in bytes of the HMAC data buffer.
*
* @retval true HMAC verification pass.
* @retval false HMAC verification fail.
**/
bool libspdm_verify_key_exchange_rsp_hmac(libspdm_context_t *spdm_context,
libspdm_session_info_t *session_info,
const void *hmac_data,
size_t hmac_data_size)
{
size_t hash_size;
uint8_t calc_hmac_data[LIBSPDM_MAX_HASH_SIZE];
bool result;
#if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
uint8_t *cert_chain_buffer;
size_t cert_chain_buffer_size;
uint8_t th_curr_data[LIBSPDM_MAX_MESSAGE_BUFFER_SIZE];
size_t th_curr_data_size;
uint8_t hash_data[LIBSPDM_MAX_HASH_SIZE];
#endif
hash_size = libspdm_get_hash_size(spdm_context->connection_info.algorithm.base_hash_algo);
LIBSPDM_ASSERT(hash_size == hmac_data_size);
#if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
result = libspdm_get_peer_cert_chain_buffer(
spdm_context, (const void **)&cert_chain_buffer, &cert_chain_buffer_size);
if (!result) {
return false;
}
th_curr_data_size = sizeof(th_curr_data);
result = libspdm_calculate_th_for_exchange(
spdm_context, session_info, cert_chain_buffer,
cert_chain_buffer_size, &th_curr_data_size, th_curr_data);
if (!result) {
return false;
}
result = libspdm_hash_all (spdm_context->connection_info.algorithm.base_hash_algo,
th_curr_data, th_curr_data_size, hash_data);
if (!result) {
return false;
}
result = libspdm_hmac_all_with_response_finished_key(
session_info->secured_message_context, hash_data,
hash_size, calc_hmac_data);
if (!result) {
return false;
}
#else
result = libspdm_calculate_th_hmac_for_exchange_rsp(
spdm_context, session_info, true, &hash_size, calc_hmac_data);
if (!result) {
return false;
}
#endif
LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "th_curr hmac - "));
LIBSPDM_INTERNAL_DUMP_DATA(calc_hmac_data, hash_size);
LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "\n"));
if (libspdm_const_compare_mem(calc_hmac_data, hmac_data, hash_size) != 0) {
LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "!!! verify_key_exchange_hmac - FAIL !!!\n"));
return false;
}
LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "!!! verify_key_exchange_hmac - PASS !!!\n"));
return true;
}
/**
* This function verifies the key exchange signature based upon TH.
*
* @param spdm_context A pointer to the SPDM context.
* @param session_info The session info of an SPDM session.
* @param sign_data The signature data buffer.
* @param sign_data_size size in bytes of the signature data buffer.
*
* @retval true signature verification pass.
* @retval false signature verification fail.
**/
bool libspdm_verify_key_exchange_rsp_signature(
libspdm_context_t *spdm_context, libspdm_session_info_t *session_info,
const void *sign_data, const size_t sign_data_size)
{
bool result;
const uint8_t *cert_chain_data;
size_t cert_chain_data_size;
const uint8_t *cert_buffer;
size_t cert_buffer_size;
void *context;
#if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
uint8_t *cert_chain_buffer;
size_t cert_chain_buffer_size;
uint8_t th_curr_data[LIBSPDM_MAX_MESSAGE_BUFFER_SIZE];
size_t th_curr_data_size;
#else
uint8_t slot_id;
#endif
#if !(LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT) || (LIBSPDM_DEBUG_PRINT_ENABLE)
size_t hash_size;
uint8_t hash_data[LIBSPDM_MAX_HASH_SIZE];
hash_size = libspdm_get_hash_size(spdm_context->connection_info.algorithm.base_hash_algo);
#endif
#if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
result = libspdm_get_peer_cert_chain_buffer(
spdm_context, (const void **)&cert_chain_buffer, &cert_chain_buffer_size);
if (!result) {
return false;
}
th_curr_data_size = sizeof(th_curr_data);
result = libspdm_calculate_th_for_exchange(
spdm_context, session_info, cert_chain_buffer,
cert_chain_buffer_size, &th_curr_data_size, th_curr_data);
if (!result) {
return false;
}
/* Debug code only - required for debug print of th_curr hash below*/
LIBSPDM_DEBUG_CODE(
if (!libspdm_hash_all(
spdm_context->connection_info.algorithm.base_hash_algo,
th_curr_data, th_curr_data_size, hash_data)) {
return false;
}
);
#else
result = libspdm_calculate_th_hash_for_exchange(
spdm_context, session_info, &hash_size, hash_data);
if (!result) {
return false;
}
#endif
LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "th_curr hash - "));
LIBSPDM_INTERNAL_DUMP_DATA(hash_data, hash_size);
LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "\n"));
LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "signature - "));
LIBSPDM_INTERNAL_DUMP_DATA(sign_data, sign_data_size);
LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "\n"));
#if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
/* Get leaf cert from cert chain */
result = libspdm_get_peer_cert_chain_data(
spdm_context, (const void **)&cert_chain_data, &cert_chain_data_size);
if (!result) {
return false;
}
result = libspdm_x509_get_cert_from_cert_chain(cert_chain_data,
cert_chain_data_size, -1,
&cert_buffer, &cert_buffer_size);
if (!result) {
return false;
}
result = libspdm_asym_get_public_key_from_x509(
spdm_context->connection_info.algorithm.base_asym_algo,
cert_buffer, cert_buffer_size, &context);
if (!result) {
return false;
}
result = libspdm_asym_verify(
spdm_context->connection_info.version, SPDM_KEY_EXCHANGE_RSP,
spdm_context->connection_info.algorithm.base_asym_algo,
spdm_context->connection_info.algorithm.base_hash_algo, context,
th_curr_data, th_curr_data_size, sign_data, sign_data_size);
libspdm_asym_free(spdm_context->connection_info.algorithm.base_asym_algo, context);
#else
slot_id = spdm_context->connection_info.peer_used_cert_chain_slot_id;
if (spdm_context->connection_info.peer_used_cert_chain[slot_id].leaf_cert_public_key != NULL) {
result = libspdm_asym_verify_hash(
spdm_context->connection_info.version, SPDM_KEY_EXCHANGE_RSP,
spdm_context->connection_info.algorithm.base_asym_algo,
spdm_context->connection_info.algorithm.base_hash_algo,
spdm_context->connection_info.peer_used_cert_chain[slot_id].leaf_cert_public_key,
hash_data, hash_size, sign_data, sign_data_size);
} else {
/* Get leaf cert from cert chain*/
result = libspdm_get_peer_cert_chain_data(
spdm_context, (const void **)&cert_chain_data, &cert_chain_data_size);
if (!result) {
return false;
}
result = libspdm_x509_get_cert_from_cert_chain(cert_chain_data,
cert_chain_data_size, -1,
&cert_buffer, &cert_buffer_size);
if (!result) {
return false;
}
result = libspdm_asym_get_public_key_from_x509(
spdm_context->connection_info.algorithm.base_asym_algo,
cert_buffer, cert_buffer_size, &context);
if (!result) {
return false;
}
result = libspdm_asym_verify_hash(
spdm_context->connection_info.version, SPDM_KEY_EXCHANGE_RSP,
spdm_context->connection_info.algorithm.base_asym_algo,
spdm_context->connection_info.algorithm.base_hash_algo, context,
hash_data, hash_size, sign_data, sign_data_size);
libspdm_asym_free(spdm_context->connection_info.algorithm.base_asym_algo, context);
}
#endif
if (!result) {
LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "!!! verify_key_exchange_signature - FAIL !!!\n"));
return false;
}
LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "!!! verify_key_exchange_signature - PASS !!!\n"));
return true;
}
/**
* This function sends KEY_EXCHANGE and receives KEY_EXCHANGE_RSP for SPDM key exchange.
*
* @param spdm_context A pointer to the SPDM context.
* @param measurement_hash_type Measurement_hash_type to the KEY_EXCHANGE request.
* @param slot_id slot_id to the KEY_EXCHANGE request.
* @param session_policy The policy for the session.
* @param session_id session_id from the KEY_EXCHANGE_RSP response.
* @param heartbeat_period Heartbeat_period from the KEY_EXCHANGE_RSP response.
* @param req_slot_id_param req_slot_id_param from the KEY_EXCHANGE_RSP response.
* @param measurement_hash Measurement_hash from the KEY_EXCHANGE_RSP response.
* @param requester_nonce_in If not NULL, a buffer that holds the requester nonce (32 bytes)
* @param requester_nonce If not NULL, a buffer to hold the requester nonce (32 bytes).
* @param responder_nonce If not NULL, a buffer to hold the responder nonce (32 bytes).
**/
static libspdm_return_t libspdm_try_send_receive_key_exchange(
libspdm_context_t *spdm_context, uint8_t measurement_hash_type,
uint8_t slot_id, uint8_t session_policy, uint32_t *session_id,
uint8_t *heartbeat_period,
uint8_t *req_slot_id_param, void *measurement_hash,
const void *requester_random_in,
void *requester_random,
void *responder_random)
{
bool result;
libspdm_return_t status;
libspdm_key_exchange_request_mine_t *spdm_request;
size_t spdm_request_size;
libspdm_key_exchange_response_max_t *spdm_response;
size_t spdm_response_size;
size_t dhe_key_size;
uint32_t measurement_summary_hash_size;
uint32_t signature_size;
uint32_t hmac_size;
uint8_t *ptr;
void *measurement_summary_hash;
uint16_t opaque_length;
uint8_t *signature;
uint8_t *verify_data;
void *dhe_context;
uint16_t req_session_id;
uint16_t rsp_session_id;
libspdm_session_info_t *session_info;
size_t opaque_key_exchange_req_size;
uint8_t th1_hash_data[LIBSPDM_MAX_HASH_SIZE];
uint8_t *message;
size_t message_size;
size_t transport_header_size;
/* -=[Check Parameters Phase]=- */
LIBSPDM_ASSERT((slot_id < SPDM_MAX_SLOT_COUNT) || (slot_id == 0xff));
LIBSPDM_ASSERT((slot_id != 0xff) ||
(spdm_context->local_context.peer_cert_chain_provision_size == 0));
/* -=[Verify State Phase]=- */
if (!libspdm_is_capabilities_flag_supported(
spdm_context, true,
SPDM_GET_CAPABILITIES_REQUEST_FLAGS_KEY_EX_CAP,
SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_KEY_EX_CAP)) {
return LIBSPDM_STATUS_UNSUPPORTED_CAP;
}
if (spdm_context->connection_info.connection_state < LIBSPDM_CONNECTION_STATE_NEGOTIATED) {
return LIBSPDM_STATUS_INVALID_STATE_LOCAL;
}
req_session_id = libspdm_allocate_req_session_id(spdm_context);
if (req_session_id == ((INVALID_SESSION_ID & 0xFFFF0000) >> 16))
{
return LIBSPDM_STATUS_SESSION_NUMBER_EXCEED;
}
libspdm_reset_message_buffer_via_request_code(spdm_context, NULL, SPDM_KEY_EXCHANGE);
/* -=[Construct Request Phase]=- */
spdm_context->connection_info.peer_used_cert_chain_slot_id = slot_id;
transport_header_size = spdm_context->transport_get_header_size(spdm_context);
status = libspdm_acquire_sender_buffer (spdm_context, &message_size, (void **)&message);
if (LIBSPDM_STATUS_IS_ERROR(status)) {
return status;
}
LIBSPDM_ASSERT (message_size >= transport_header_size);
spdm_request = (void *)(message + transport_header_size);
spdm_request_size = message_size - transport_header_size;
spdm_request->header.spdm_version = libspdm_get_connection_version (spdm_context);
spdm_request->header.request_response_code = SPDM_KEY_EXCHANGE;
spdm_request->header.param1 = measurement_hash_type;
spdm_request->header.param2 = slot_id;
if (requester_random_in == NULL) {
if(!libspdm_get_random_number(SPDM_RANDOM_DATA_SIZE, spdm_request->random_data)) {
libspdm_release_sender_buffer (spdm_context);
return LIBSPDM_STATUS_LOW_ENTROPY;
}
} else {
libspdm_copy_mem(spdm_request->random_data, sizeof(spdm_request->random_data),
requester_random_in, SPDM_RANDOM_DATA_SIZE);
}
LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "ClientRandomData (0x%x) - ",
SPDM_RANDOM_DATA_SIZE));
LIBSPDM_INTERNAL_DUMP_DATA(spdm_request->random_data, SPDM_RANDOM_DATA_SIZE);
LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "\n"));
if (requester_random != NULL) {
libspdm_copy_mem(requester_random, SPDM_RANDOM_DATA_SIZE,
spdm_request->random_data, SPDM_RANDOM_DATA_SIZE);
}
spdm_request->req_session_id = req_session_id;
if (spdm_request->header.spdm_version >= SPDM_MESSAGE_VERSION_12) {
spdm_request->session_policy = session_policy;
} else {
spdm_request->session_policy = 0;
}
spdm_request->reserved = 0;
ptr = spdm_request->exchange_data;
dhe_key_size = libspdm_get_dhe_pub_key_size(
spdm_context->connection_info.algorithm.dhe_named_group);
dhe_context = libspdm_secured_message_dhe_new(
spdm_context->connection_info.version,
spdm_context->connection_info.algorithm.dhe_named_group, true);
if (dhe_context == NULL) {
libspdm_release_sender_buffer (spdm_context);
return LIBSPDM_STATUS_CRYPTO_ERROR;
}
result = libspdm_secured_message_dhe_generate_key(
spdm_context->connection_info.algorithm.dhe_named_group,
dhe_context, ptr, &dhe_key_size);
if (!result) {
libspdm_secured_message_dhe_free(
spdm_context->connection_info.algorithm.dhe_named_group, dhe_context);
libspdm_release_sender_buffer (spdm_context);
return LIBSPDM_STATUS_CRYPTO_ERROR;
}
LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "ClientKey (0x%x):\n", dhe_key_size));
LIBSPDM_INTERNAL_DUMP_HEX(ptr, dhe_key_size);
ptr += dhe_key_size;
opaque_key_exchange_req_size =
libspdm_get_opaque_data_supported_version_data_size(spdm_context);
*(uint16_t *)ptr = (uint16_t)opaque_key_exchange_req_size;
ptr += sizeof(uint16_t);
status = libspdm_build_opaque_data_supported_version_data(
spdm_context, &opaque_key_exchange_req_size, ptr);
LIBSPDM_ASSERT(status == LIBSPDM_STATUS_SUCCESS);
ptr += opaque_key_exchange_req_size;
spdm_request_size = (size_t)ptr - (size_t)spdm_request;
/* -=[Send Request Phase]=- */
status = libspdm_send_spdm_request(spdm_context, NULL, spdm_request_size, spdm_request);
if (LIBSPDM_STATUS_IS_ERROR(status)) {
libspdm_secured_message_dhe_free(
spdm_context->connection_info.algorithm.dhe_named_group, dhe_context);
libspdm_release_sender_buffer (spdm_context);
return status;
}
libspdm_release_sender_buffer (spdm_context);
spdm_request = (void *)spdm_context->last_spdm_request;
/* -=[Receive Response Phase]=- */
status = libspdm_acquire_receiver_buffer (spdm_context, &message_size, (void **)&message);
if (LIBSPDM_STATUS_IS_ERROR(status)) {
libspdm_secured_message_dhe_free(
spdm_context->connection_info.algorithm.dhe_named_group, dhe_context);
return status;
}
LIBSPDM_ASSERT (message_size >= transport_header_size);
spdm_response = (void *)(message);
spdm_response_size = message_size;
libspdm_zero_mem(spdm_response, spdm_response_size);
status = libspdm_receive_spdm_response(
spdm_context, NULL, &spdm_response_size, (void **)&spdm_response);
if (LIBSPDM_STATUS_IS_ERROR(status)) {
libspdm_secured_message_dhe_free(
spdm_context->connection_info.algorithm.dhe_named_group, dhe_context);
goto receive_done;
}
/* -=[Validate Response Phase]=- */
if (spdm_response_size < sizeof(spdm_message_header_t)) {
libspdm_secured_message_dhe_free(
spdm_context->connection_info.algorithm.dhe_named_group, dhe_context);
status = LIBSPDM_STATUS_INVALID_MSG_SIZE;
goto receive_done;
}
if (spdm_response->header.spdm_version != spdm_request->header.spdm_version) {
libspdm_secured_message_dhe_free(
spdm_context->connection_info.algorithm.dhe_named_group, dhe_context);
status = LIBSPDM_STATUS_INVALID_MSG_FIELD;
goto receive_done;
}
if (spdm_response->header.request_response_code == SPDM_ERROR) {
status = libspdm_handle_error_response_main(
spdm_context, NULL, &spdm_response_size,
(void **)&spdm_response, SPDM_KEY_EXCHANGE,
SPDM_KEY_EXCHANGE_RSP, sizeof(libspdm_key_exchange_response_max_t));
if (LIBSPDM_STATUS_IS_ERROR(status)) {
libspdm_secured_message_dhe_free(
spdm_context->connection_info.algorithm.dhe_named_group, dhe_context);
goto receive_done;
}
} else if (spdm_response->header.request_response_code != SPDM_KEY_EXCHANGE_RSP) {
libspdm_secured_message_dhe_free(
spdm_context->connection_info.algorithm.dhe_named_group, dhe_context);
status = LIBSPDM_STATUS_INVALID_MSG_FIELD;
goto receive_done;
}
if (spdm_response_size < sizeof(spdm_key_exchange_response_t)) {
libspdm_secured_message_dhe_free(
spdm_context->connection_info.algorithm.dhe_named_group, dhe_context);
status = LIBSPDM_STATUS_INVALID_MSG_SIZE;
goto receive_done;
}
if (!libspdm_is_capabilities_flag_supported(
spdm_context, true,
SPDM_GET_CAPABILITIES_REQUEST_FLAGS_HBEAT_CAP,
SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_HBEAT_CAP)) {
if (spdm_response->header.param1 != 0) {
libspdm_secured_message_dhe_free(
spdm_context->connection_info.algorithm.dhe_named_group, dhe_context);
status = LIBSPDM_STATUS_INVALID_MSG_FIELD;
goto receive_done;
}
}
if (heartbeat_period != NULL) {
*heartbeat_period = spdm_response->header.param1;
}
*req_slot_id_param = spdm_response->req_slot_id_param;
if (spdm_response->mut_auth_requested != 0) {
if (!libspdm_is_capabilities_flag_supported(
spdm_context, true,
SPDM_GET_CAPABILITIES_REQUEST_FLAGS_MUT_AUTH_CAP,
SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_MUT_AUTH_CAP)) {
libspdm_secured_message_dhe_free(
spdm_context->connection_info.algorithm.dhe_named_group, dhe_context);
status = LIBSPDM_STATUS_INVALID_MSG_FIELD;
goto receive_done;
}
if ((spdm_response->mut_auth_requested != SPDM_KEY_EXCHANGE_RESPONSE_MUT_AUTH_REQUESTED) &&
(spdm_response->mut_auth_requested !=
SPDM_KEY_EXCHANGE_RESPONSE_MUT_AUTH_REQUESTED_WITH_ENCAP_REQUEST) &&
(spdm_response->mut_auth_requested !=
SPDM_KEY_EXCHANGE_RESPONSE_MUT_AUTH_REQUESTED_WITH_GET_DIGESTS)) {
libspdm_secured_message_dhe_free(
spdm_context->connection_info.algorithm.dhe_named_group, dhe_context);
status = LIBSPDM_STATUS_INVALID_MSG_FIELD;
goto receive_done;
}
if ((*req_slot_id_param != 0xF) && (*req_slot_id_param >= SPDM_MAX_SLOT_COUNT)) {
libspdm_secured_message_dhe_free(
spdm_context->connection_info.algorithm.dhe_named_group, dhe_context);
status = LIBSPDM_STATUS_INVALID_MSG_FIELD;
goto receive_done;
}
} else {
if (*req_slot_id_param != 0) {
libspdm_secured_message_dhe_free(
spdm_context->connection_info.algorithm.dhe_named_group, dhe_context);
status = LIBSPDM_STATUS_INVALID_MSG_FIELD;
goto receive_done;
}
}
rsp_session_id = spdm_response->rsp_session_id;
*session_id = (req_session_id << 16) | rsp_session_id;
session_info = libspdm_assign_session_id(spdm_context, *session_id, false);
if (session_info == NULL) {
libspdm_secured_message_dhe_free(
spdm_context->connection_info.algorithm.dhe_named_group, dhe_context);
status = LIBSPDM_STATUS_SESSION_NUMBER_EXCEED;
goto receive_done;
}
signature_size = libspdm_get_asym_signature_size(
spdm_context->connection_info.algorithm.base_asym_algo);
measurement_summary_hash_size = libspdm_get_measurement_summary_hash_size(
spdm_context, true, measurement_hash_type);
hmac_size = libspdm_get_hash_size(spdm_context->connection_info.algorithm.base_hash_algo);
if (libspdm_is_capabilities_flag_supported(
spdm_context, true,
SPDM_GET_CAPABILITIES_REQUEST_FLAGS_HANDSHAKE_IN_THE_CLEAR_CAP,
SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_HANDSHAKE_IN_THE_CLEAR_CAP)) {
hmac_size = 0;
}
if (spdm_response_size <
sizeof(spdm_key_exchange_response_t) + dhe_key_size +
measurement_summary_hash_size + sizeof(uint16_t) + signature_size + hmac_size) {
libspdm_free_session_id(spdm_context, *session_id);
libspdm_secured_message_dhe_free(
spdm_context->connection_info.algorithm.dhe_named_group, dhe_context);
status = LIBSPDM_STATUS_INVALID_MSG_SIZE;
goto receive_done;
}
LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "ServerRandomData (0x%x) - ", SPDM_RANDOM_DATA_SIZE));
LIBSPDM_INTERNAL_DUMP_DATA(spdm_response->random_data, SPDM_RANDOM_DATA_SIZE);
LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "\n"));
if (responder_random != NULL) {
libspdm_copy_mem(responder_random, SPDM_RANDOM_DATA_SIZE,
spdm_response->random_data, SPDM_RANDOM_DATA_SIZE);
}
LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "ServerKey (0x%x):\n", dhe_key_size));
LIBSPDM_INTERNAL_DUMP_HEX(spdm_response->exchange_data, dhe_key_size);
ptr = spdm_response->exchange_data;
ptr += dhe_key_size;
measurement_summary_hash = ptr;
LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "measurement_summary_hash (0x%x) - ",
measurement_summary_hash_size));
LIBSPDM_INTERNAL_DUMP_DATA(measurement_summary_hash, measurement_summary_hash_size);
LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "\n"));
ptr += measurement_summary_hash_size;
opaque_length = *(uint16_t *)ptr;
if (opaque_length > SPDM_MAX_OPAQUE_DATA_SIZE) {
libspdm_free_session_id(spdm_context, *session_id);
libspdm_secured_message_dhe_free(
spdm_context->connection_info.algorithm.dhe_named_group, dhe_context);
status = LIBSPDM_STATUS_INVALID_MSG_FIELD;
goto receive_done;
}
ptr += sizeof(uint16_t);
if (spdm_response_size <
sizeof(spdm_key_exchange_response_t) + dhe_key_size +
measurement_summary_hash_size + sizeof(uint16_t) +
opaque_length + signature_size + hmac_size) {
libspdm_free_session_id(spdm_context, *session_id);
libspdm_secured_message_dhe_free(
spdm_context->connection_info.algorithm.dhe_named_group, dhe_context);
status = LIBSPDM_STATUS_INVALID_MSG_SIZE;
goto receive_done;
}
status = libspdm_process_opaque_data_version_selection_data(
spdm_context, opaque_length, ptr);
if (LIBSPDM_STATUS_IS_ERROR(status)) {
libspdm_free_session_id(spdm_context, *session_id);
libspdm_secured_message_dhe_free(
spdm_context->connection_info.algorithm.dhe_named_group, dhe_context);
status = LIBSPDM_STATUS_INVALID_MSG_FIELD;
goto receive_done;
}
ptr += opaque_length;
spdm_response_size = sizeof(spdm_key_exchange_response_t) +
dhe_key_size + measurement_summary_hash_size +
sizeof(uint16_t) + opaque_length + signature_size + hmac_size;
/* -=[Process Response Phase]=- */
status = libspdm_append_message_k(spdm_context, session_info, true, spdm_request,
spdm_request_size);
if (LIBSPDM_STATUS_IS_ERROR(status)) {
libspdm_free_session_id(spdm_context, *session_id);
libspdm_secured_message_dhe_free(
spdm_context->connection_info.algorithm.dhe_named_group, dhe_context);
status = LIBSPDM_STATUS_BUFFER_FULL;
goto receive_done;
}
status = libspdm_append_message_k(spdm_context, session_info, true, spdm_response,
spdm_response_size - signature_size - hmac_size);
if (LIBSPDM_STATUS_IS_ERROR(status)) {
libspdm_free_session_id(spdm_context, *session_id);
libspdm_secured_message_dhe_free(
spdm_context->connection_info.algorithm.dhe_named_group, dhe_context);
status = LIBSPDM_STATUS_BUFFER_FULL;
goto receive_done;
}
signature = ptr;
LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "signature (0x%x):\n", signature_size));
LIBSPDM_INTERNAL_DUMP_HEX(signature, signature_size);
ptr += signature_size;
result = libspdm_verify_key_exchange_rsp_signature(
spdm_context, session_info, signature, signature_size);
if (!result) {
libspdm_free_session_id(spdm_context, *session_id);
libspdm_secured_message_dhe_free(
spdm_context->connection_info.algorithm.dhe_named_group, dhe_context);
status = LIBSPDM_STATUS_VERIF_FAIL;
goto receive_done;
}
status = libspdm_append_message_k(spdm_context, session_info, true, signature, signature_size);
if (LIBSPDM_STATUS_IS_ERROR(status)) {
libspdm_free_session_id(spdm_context, *session_id);
libspdm_secured_message_dhe_free(
spdm_context->connection_info.algorithm.dhe_named_group, dhe_context);
status = LIBSPDM_STATUS_BUFFER_FULL;
goto receive_done;
}
result = libspdm_secured_message_dhe_compute_key(
spdm_context->connection_info.algorithm.dhe_named_group,
dhe_context, spdm_response->exchange_data, dhe_key_size,
session_info->secured_message_context);
libspdm_secured_message_dhe_free(
spdm_context->connection_info.algorithm.dhe_named_group, dhe_context);
if (!result) {
libspdm_free_session_id(spdm_context, *session_id);
status = LIBSPDM_STATUS_CRYPTO_ERROR;
goto receive_done;
}
LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "libspdm_generate_session_handshake_key[%x]\n",
*session_id));
result = libspdm_calculate_th1_hash(spdm_context, session_info, true, th1_hash_data);
if (!result) {
libspdm_free_session_id(spdm_context, *session_id);
return LIBSPDM_STATUS_CRYPTO_ERROR;
}
result = libspdm_generate_session_handshake_key(
session_info->secured_message_context, th1_hash_data);
if (!result) {
libspdm_free_session_id(spdm_context, *session_id);
status = LIBSPDM_STATUS_CRYPTO_ERROR;
goto receive_done;
}
if (!libspdm_is_capabilities_flag_supported(
spdm_context, true,
SPDM_GET_CAPABILITIES_REQUEST_FLAGS_HANDSHAKE_IN_THE_CLEAR_CAP,
SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_HANDSHAKE_IN_THE_CLEAR_CAP)) {
verify_data = ptr;
LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "verify_data (0x%x):\n", hmac_size));
LIBSPDM_INTERNAL_DUMP_HEX(verify_data, hmac_size);
result = libspdm_verify_key_exchange_rsp_hmac(
spdm_context, session_info, verify_data, hmac_size);
if (!result) {
libspdm_free_session_id(spdm_context, *session_id);
status = LIBSPDM_STATUS_VERIF_FAIL;
goto receive_done;
}
ptr += hmac_size;
status = libspdm_append_message_k(spdm_context, session_info, true, verify_data, hmac_size);
if (LIBSPDM_STATUS_IS_ERROR(status)) {
libspdm_free_session_id(spdm_context, *session_id);
status = LIBSPDM_STATUS_BUFFER_FULL;
goto receive_done;
}
}
if (measurement_hash != NULL) {
libspdm_copy_mem(measurement_hash, measurement_summary_hash_size,
measurement_summary_hash, measurement_summary_hash_size);
}
session_info->heartbeat_period = spdm_response->header.param1;
session_info->mut_auth_requested = spdm_response->mut_auth_requested;
session_info->session_policy = session_policy;
/* -=[Update State Phase]=- */
libspdm_secured_message_set_session_state(
session_info->secured_message_context, LIBSPDM_SESSION_STATE_HANDSHAKING);
status = LIBSPDM_STATUS_SUCCESS;
receive_done:
libspdm_release_receiver_buffer (spdm_context);
return status;
}
libspdm_return_t libspdm_send_receive_key_exchange(
libspdm_context_t *spdm_context, uint8_t measurement_hash_type,
uint8_t slot_id, uint8_t session_policy, uint32_t *session_id,
uint8_t *heartbeat_period,
uint8_t *req_slot_id_param, void *measurement_hash)
{
size_t retry;
libspdm_return_t status;
spdm_context->crypto_request = true;
retry = spdm_context->retry_times;
do {
status = libspdm_try_send_receive_key_exchange(
spdm_context, measurement_hash_type, slot_id, session_policy,
session_id, heartbeat_period, req_slot_id_param,
measurement_hash, NULL, NULL, NULL);
if (status != LIBSPDM_STATUS_BUSY_PEER) {
return status;
}
} while (retry-- != 0);
return status;
}
libspdm_return_t libspdm_send_receive_key_exchange_ex(
libspdm_context_t *spdm_context, uint8_t measurement_hash_type,
uint8_t slot_id, uint8_t session_policy, uint32_t *session_id,
uint8_t *heartbeat_period,
uint8_t *req_slot_id_param, void *measurement_hash,
const void *requester_random_in,
void *requester_random,
void *responder_random)
{
size_t retry;
libspdm_return_t status;
spdm_context->crypto_request = true;
retry = spdm_context->retry_times;
do {
status = libspdm_try_send_receive_key_exchange(
spdm_context, measurement_hash_type, slot_id, session_policy,
session_id, heartbeat_period, req_slot_id_param,
measurement_hash, requester_random_in,
requester_random, responder_random);
if (LIBSPDM_STATUS_BUSY_PEER != status) {
return status;
}
} while (retry-- != 0);
return status;
}
#endif /* LIBSPDM_ENABLE_CAPABILITY_KEY_EX_CAP */