forked from DMTF/libspdm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlibspdm_req_negotiate_algorithms.c
More file actions
819 lines (782 loc) · 36.3 KB
/
libspdm_req_negotiate_algorithms.c
File metadata and controls
819 lines (782 loc) · 36.3 KB
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
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
/**
* Copyright Notice:
* Copyright 2021-2025 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"
#pragma pack(1)
typedef struct {
spdm_message_header_t header;
uint16_t length;
uint8_t measurement_specification;
uint8_t other_params_support;
uint32_t base_asym_algo;
uint32_t base_hash_algo;
uint32_t pqc_asym_algo;
uint8_t reserved2[8];
uint8_t ext_asym_count;
uint8_t ext_hash_count;
uint8_t reserved3;
uint8_t mel_specification;
spdm_negotiate_algorithms_common_struct_table_t struct_table[
SPDM_NEGOTIATE_ALGORITHMS_MAX_NUM_STRUCT_TABLE_ALG_14];
} libspdm_negotiate_algorithms_request_mine_t;
typedef struct {
spdm_message_header_t header;
uint16_t length;
uint8_t measurement_specification_sel;
uint8_t other_params_selection;
uint32_t measurement_hash_algo;
uint32_t base_asym_sel;
uint32_t base_hash_sel;
uint32_t pqc_asym_sel;
uint8_t reserved2[7];
uint8_t mel_specification_sel;
uint8_t ext_asym_sel_count;
uint8_t ext_hash_sel_count;
uint16_t reserved3;
uint32_t ext_asym_sel;
uint32_t ext_hash_sel;
spdm_negotiate_algorithms_common_struct_table_t struct_table[
SPDM_NEGOTIATE_ALGORITHMS_MAX_NUM_STRUCT_TABLE_ALG_14];
} libspdm_algorithms_response_max_t;
#pragma pack()
static libspdm_return_t
libspdm_negotiate_algorithms_send_request(libspdm_context_t *spdm_context,
libspdm_negotiate_algorithms_request_mine_t **request,
size_t *request_size)
{
libspdm_return_t status;
libspdm_negotiate_algorithms_request_mine_t *spdm_request;
size_t spdm_request_size;
size_t index = 0;
uint8_t *message;
size_t message_size;
size_t transport_header_size;
uint8_t req_param1 = 0;
/* -=[Verify State Phase]=- */
if (spdm_context->connection_info.connection_state !=
LIBSPDM_CONNECTION_STATE_AFTER_CAPABILITIES) {
return LIBSPDM_STATUS_INVALID_STATE_LOCAL;
}
libspdm_reset_message_buffer_via_request_code(spdm_context, NULL, SPDM_NEGOTIATE_ALGORITHMS);
/* -=[Construct Request Phase]=- */
transport_header_size = spdm_context->local_context.capability.transport_header_size;
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_context->local_context.capability.transport_tail_size);
spdm_request = (void *)(message + transport_header_size);
spdm_request_size = message_size - transport_header_size -
spdm_context->local_context.capability.transport_tail_size;
LIBSPDM_ASSERT(spdm_request_size >= sizeof(spdm_negotiate_algorithms_request_t));
libspdm_zero_mem(spdm_request, spdm_request_size);
spdm_request->header.spdm_version = libspdm_get_connection_version (spdm_context);
if (spdm_request->header.spdm_version >= SPDM_MESSAGE_VERSION_11) {
/* Number of Algorithms Structure Tables based on supported algorithms */
if (spdm_context->local_context.algorithm.dhe_named_group != 0) {
req_param1++;
}
if (spdm_context->local_context.algorithm.aead_cipher_suite != 0) {
req_param1++;
}
if (spdm_context->local_context.algorithm.req_base_asym_alg != 0) {
req_param1++;
}
if (spdm_context->local_context.algorithm.key_schedule != 0) {
req_param1++;
}
LIBSPDM_ASSERT(req_param1 <=
SPDM_NEGOTIATE_ALGORITHMS_MAX_NUM_STRUCT_TABLE_ALG);
if (spdm_request->header.spdm_version >= SPDM_MESSAGE_VERSION_14) {
if (spdm_context->local_context.algorithm.req_pqc_asym_alg != 0) {
req_param1++;
}
if (spdm_context->local_context.algorithm.kem_alg != 0) {
req_param1++;
}
LIBSPDM_ASSERT(req_param1 <=
SPDM_NEGOTIATE_ALGORITHMS_MAX_NUM_STRUCT_TABLE_ALG_14);
}
spdm_request->header.param1 = req_param1;
spdm_request->length =
offsetof(libspdm_negotiate_algorithms_request_mine_t, struct_table) +
req_param1 * sizeof(spdm_negotiate_algorithms_common_struct_table_t);
} else {
spdm_request->length = offsetof(libspdm_negotiate_algorithms_request_mine_t, struct_table);
spdm_request->header.param1 = 0;
}
LIBSPDM_ASSERT(spdm_request_size >= spdm_request->length);
spdm_request->header.request_response_code = SPDM_NEGOTIATE_ALGORITHMS;
spdm_request->header.param2 = 0;
spdm_request->measurement_specification =
spdm_context->local_context.algorithm.measurement_spec;
if (spdm_request->header.spdm_version >= SPDM_MESSAGE_VERSION_12) {
spdm_request->other_params_support =
spdm_context->local_context.algorithm.other_params_support &
SPDM_ALGORITHMS_OPAQUE_DATA_FORMAT_MASK;
if (spdm_request->header.spdm_version >= SPDM_MESSAGE_VERSION_13) {
spdm_request->other_params_support =
spdm_context->local_context.algorithm.other_params_support;
spdm_request->mel_specification =
spdm_context->local_context.algorithm.mel_spec;
}
}
if (spdm_request->header.spdm_version >= SPDM_MESSAGE_VERSION_13) {
switch (spdm_context->connection_info.capability.flags &
SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_MULTI_KEY_CAP) {
case 0:
spdm_context->connection_info.multi_key_conn_rsp = false;
break;
case SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_MULTI_KEY_CAP_ONLY:
spdm_context->connection_info.multi_key_conn_rsp = true;
break;
case SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_MULTI_KEY_CAP_NEG:
if ((spdm_context->local_context.algorithm.other_params_support &
SPDM_ALGORITHMS_MULTI_KEY_CONN) == 0) {
spdm_context->connection_info.multi_key_conn_rsp = false;
} else {
spdm_context->connection_info.multi_key_conn_rsp = true;
}
break;
default:
libspdm_release_sender_buffer (spdm_context);
return LIBSPDM_STATUS_INVALID_MSG_FIELD;
}
if (spdm_context->connection_info.multi_key_conn_rsp) {
spdm_request->other_params_support |= SPDM_ALGORITHMS_MULTI_KEY_CONN;
} else {
spdm_request->other_params_support &= ~SPDM_ALGORITHMS_MULTI_KEY_CONN;
}
}
spdm_request->base_asym_algo = spdm_context->local_context.algorithm.base_asym_algo;
spdm_request->base_hash_algo = spdm_context->local_context.algorithm.base_hash_algo;
if (spdm_request->header.spdm_version >= SPDM_MESSAGE_VERSION_14) {
spdm_request->pqc_asym_algo = spdm_context->local_context.algorithm.pqc_asym_algo;
}
spdm_request->ext_asym_count = 0;
spdm_request->ext_hash_count = 0;
if (spdm_request->header.spdm_version >= SPDM_MESSAGE_VERSION_11) {
/* ReqAlgStruct order based on by AlgType */
if (spdm_context->local_context.algorithm.dhe_named_group != 0) {
spdm_request->struct_table[index].alg_type =
SPDM_NEGOTIATE_ALGORITHMS_STRUCT_TABLE_ALG_TYPE_DHE;
spdm_request->struct_table[index].alg_count = 0x20;
spdm_request->struct_table[index].alg_supported =
spdm_context->local_context.algorithm.dhe_named_group;
index++;
}
if (spdm_context->local_context.algorithm.aead_cipher_suite != 0) {
spdm_request->struct_table[index].alg_type =
SPDM_NEGOTIATE_ALGORITHMS_STRUCT_TABLE_ALG_TYPE_AEAD;
spdm_request->struct_table[index].alg_count = 0x20;
spdm_request->struct_table[index].alg_supported =
spdm_context->local_context.algorithm.aead_cipher_suite;
index++;
}
if (spdm_context->local_context.algorithm.req_base_asym_alg != 0) {
spdm_request->struct_table[index].alg_type =
SPDM_NEGOTIATE_ALGORITHMS_STRUCT_TABLE_ALG_TYPE_REQ_BASE_ASYM_ALG;
spdm_request->struct_table[index].alg_count = 0x20;
spdm_request->struct_table[index].alg_supported =
spdm_context->local_context.algorithm.req_base_asym_alg;
index++;
}
if (spdm_context->local_context.algorithm.key_schedule != 0) {
spdm_request->struct_table[index].alg_type =
SPDM_NEGOTIATE_ALGORITHMS_STRUCT_TABLE_ALG_TYPE_KEY_SCHEDULE;
spdm_request->struct_table[index].alg_count = 0x20;
spdm_request->struct_table[index].alg_supported =
spdm_context->local_context.algorithm.key_schedule;
index++;
}
if (spdm_request->header.spdm_version >= SPDM_MESSAGE_VERSION_14) {
if (spdm_context->local_context.algorithm.req_pqc_asym_alg != 0) {
/* Add assert to ensure it can be cast to uint16_t.
* It is enough now and can be enlarged later. */
LIBSPDM_ASSERT(spdm_context->local_context.algorithm.req_pqc_asym_alg <= UINT16_MAX);
spdm_request->struct_table[index].alg_type =
SPDM_NEGOTIATE_ALGORITHMS_STRUCT_TABLE_ALG_TYPE_REQ_PQC_ASYM_ALG;
spdm_request->struct_table[index].alg_count = 0x20;
spdm_request->struct_table[index].alg_supported =
(uint16_t)spdm_context->local_context.algorithm.req_pqc_asym_alg;
index++;
}
if (spdm_context->local_context.algorithm.kem_alg != 0) {
/* Add assert to ensure it can be cast to uint16_t.
* It is enough now and can be enlarged later. */
LIBSPDM_ASSERT(spdm_context->local_context.algorithm.kem_alg <= UINT16_MAX);
spdm_request->struct_table[index].alg_type =
SPDM_NEGOTIATE_ALGORITHMS_STRUCT_TABLE_ALG_TYPE_KEM_ALG;
spdm_request->struct_table[index].alg_count = 0x20;
spdm_request->struct_table[index].alg_supported =
(uint16_t)spdm_context->local_context.algorithm.kem_alg;
index++;
}
}
LIBSPDM_ASSERT(index == spdm_request->header.param1);
}
spdm_request_size = spdm_request->length;
/* -=[Send Request Phase]=- */
status = libspdm_send_spdm_request(spdm_context, NULL, spdm_request_size, spdm_request);
if (LIBSPDM_STATUS_IS_ERROR(status)) {
libspdm_release_sender_buffer (spdm_context);
return status;
}
libspdm_release_sender_buffer (spdm_context);
*request = (void *)spdm_context->last_spdm_request;
*request_size = spdm_request_size;
return status;
}
static libspdm_return_t
libspdm_negotiate_algorithms_process_response(
libspdm_context_t *spdm_context,
libspdm_negotiate_algorithms_request_mine_t *spdm_request,
size_t spdm_request_size)
{
libspdm_return_t status;
libspdm_algorithms_response_max_t *spdm_response;
size_t spdm_response_size;
uint32_t algo_size;
uint32_t pqc_algo_size;
size_t index;
spdm_negotiate_algorithms_common_struct_table_t *struct_table;
uint8_t fixed_alg_size;
uint8_t ext_alg_count;
uint8_t *message;
size_t message_size;
size_t transport_header_size;
uint8_t alg_type_pre;
/* -=[Receive Response Phase]=- */
transport_header_size = spdm_context->local_context.capability.transport_header_size;
status = libspdm_acquire_receiver_buffer (spdm_context, &message_size, (void **)&message);
if (LIBSPDM_STATUS_IS_ERROR(status)) {
return status;
}
LIBSPDM_ASSERT (message_size >= transport_header_size);
(void)(transport_header_size);
spdm_response = (void *)(message);
spdm_response_size = message_size;
status = libspdm_receive_spdm_response(spdm_context, NULL, &spdm_response_size,
(void **)&spdm_response);
if (LIBSPDM_STATUS_IS_ERROR(status)) {
goto receive_done;
}
/* -=[Validate Response Phase]=- */
if (spdm_response_size < sizeof(spdm_message_header_t)) {
status = LIBSPDM_STATUS_INVALID_MSG_SIZE;
goto receive_done;
}
if (spdm_response->header.request_response_code == SPDM_ERROR) {
status = libspdm_handle_simple_error_response(spdm_context, spdm_response->header.param1);
if (LIBSPDM_STATUS_IS_ERROR(status)) {
goto receive_done;
}
} else if (spdm_response->header.request_response_code != SPDM_ALGORITHMS) {
status = LIBSPDM_STATUS_INVALID_MSG_FIELD;
goto receive_done;
}
if (spdm_response->header.spdm_version != spdm_request->header.spdm_version) {
status = LIBSPDM_STATUS_INVALID_MSG_FIELD;
goto receive_done;
}
if (spdm_response_size < sizeof(spdm_algorithms_response_t)) {
status = LIBSPDM_STATUS_INVALID_MSG_SIZE;
goto receive_done;
}
if (!libspdm_onehot0(spdm_response->measurement_specification_sel)) {
status = LIBSPDM_STATUS_INVALID_MSG_FIELD;
goto receive_done;
}
if (spdm_request->header.spdm_version >= SPDM_MESSAGE_VERSION_12) {
if (!libspdm_onehot0(spdm_response->other_params_selection &
SPDM_ALGORITHMS_OPAQUE_DATA_FORMAT_MASK)) {
status = LIBSPDM_STATUS_INVALID_MSG_FIELD;
goto receive_done;
}
}
if (!libspdm_onehot0(spdm_response->measurement_hash_algo)) {
status = LIBSPDM_STATUS_INVALID_MSG_FIELD;
goto receive_done;
}
if (!libspdm_onehot0(spdm_response->base_asym_sel)) {
status = LIBSPDM_STATUS_INVALID_MSG_FIELD;
goto receive_done;
}
if (!libspdm_onehot0(spdm_response->base_hash_sel)) {
status = LIBSPDM_STATUS_INVALID_MSG_FIELD;
goto receive_done;
}
if (spdm_request->header.spdm_version >= SPDM_MESSAGE_VERSION_14) {
if (!libspdm_onehot0(spdm_response->pqc_asym_sel)) {
status = LIBSPDM_STATUS_INVALID_MSG_FIELD;
goto receive_done;
}
}
if (spdm_response->ext_asym_sel_count > 0) {
status = LIBSPDM_STATUS_INVALID_MSG_FIELD;
goto receive_done;
}
if (spdm_response->ext_hash_sel_count > 0) {
status = LIBSPDM_STATUS_INVALID_MSG_FIELD;
goto receive_done;
}
if (spdm_response_size <
sizeof(spdm_algorithms_response_t) +
sizeof(uint32_t) * spdm_response->ext_asym_sel_count +
sizeof(uint32_t) * spdm_response->ext_hash_sel_count +
sizeof(spdm_negotiate_algorithms_common_struct_table_t) * spdm_response->header.param1) {
status = LIBSPDM_STATUS_INVALID_MSG_SIZE;
goto receive_done;
}
struct_table =
(void *)((size_t)spdm_response +
sizeof(spdm_algorithms_response_t) +
sizeof(uint32_t) * spdm_response->ext_asym_sel_count +
sizeof(uint32_t) * spdm_response->ext_hash_sel_count);
if (spdm_response->header.spdm_version >= SPDM_MESSAGE_VERSION_11) {
alg_type_pre = struct_table->alg_type;
/* header.param1 is implicitly checked through spdm_response_size. */
for (index = 0; index < spdm_response->header.param1; index++) {
if ((size_t)spdm_response + spdm_response_size < (size_t)struct_table) {
status = LIBSPDM_STATUS_INVALID_MSG_SIZE;
goto receive_done;
}
if ((size_t)spdm_response + spdm_response_size - (size_t)struct_table <
sizeof(spdm_negotiate_algorithms_common_struct_table_t)) {
status = LIBSPDM_STATUS_INVALID_MSG_SIZE;
goto receive_done;
}
if ((struct_table->alg_type < SPDM_NEGOTIATE_ALGORITHMS_STRUCT_TABLE_ALG_TYPE_DHE) ||
(struct_table->alg_type >
SPDM_NEGOTIATE_ALGORITHMS_STRUCT_TABLE_ALG_TYPE_KEM_ALG)) {
status = LIBSPDM_STATUS_INVALID_MSG_FIELD;
goto receive_done;
}
if ((spdm_response->header.spdm_version < SPDM_MESSAGE_VERSION_14) &&
(struct_table->alg_type >
SPDM_NEGOTIATE_ALGORITHMS_STRUCT_TABLE_ALG_TYPE_KEY_SCHEDULE)) {
status = LIBSPDM_STATUS_INVALID_MSG_FIELD;
goto receive_done;
}
/* AlgType shall monotonically increase for subsequent entries. */
if ((index != 0) && (struct_table->alg_type <= alg_type_pre)) {
status = LIBSPDM_STATUS_INVALID_MSG_FIELD;
goto receive_done;
}
alg_type_pre = struct_table->alg_type;
fixed_alg_size = (struct_table->alg_count >> 4) & 0xF;
ext_alg_count = struct_table->alg_count & 0xF;
if (fixed_alg_size != 2) {
status = LIBSPDM_STATUS_INVALID_MSG_FIELD;
goto receive_done;
}
if (ext_alg_count > 0) {
status = LIBSPDM_STATUS_INVALID_MSG_FIELD;
goto receive_done;
}
if (!libspdm_onehot0(struct_table->alg_supported)) {
status = LIBSPDM_STATUS_INVALID_MSG_FIELD;
goto receive_done;
}
if ((size_t)spdm_response + spdm_response_size -
(size_t)struct_table - sizeof(spdm_negotiate_algorithms_common_struct_table_t) <
sizeof(uint32_t) * ext_alg_count) {
status = LIBSPDM_STATUS_INVALID_MSG_SIZE;
goto receive_done;
}
struct_table =
(void *)((size_t)struct_table +
sizeof(spdm_negotiate_algorithms_common_struct_table_t) +
sizeof(uint32_t) * ext_alg_count);
}
}
spdm_response_size = (size_t)struct_table - (size_t)spdm_response;
if (spdm_response_size != spdm_response->length) {
status = LIBSPDM_STATUS_INVALID_MSG_SIZE;
goto receive_done;
}
/* -=[Process Response Phase]=- */
status = libspdm_append_message_a(spdm_context, spdm_request, spdm_request_size);
if (LIBSPDM_STATUS_IS_ERROR(status)) {
goto receive_done;
}
status = libspdm_append_message_a(spdm_context, spdm_response, spdm_response_size);
if (LIBSPDM_STATUS_IS_ERROR(status)) {
goto receive_done;
}
spdm_context->connection_info.algorithm.measurement_spec =
spdm_response->measurement_specification_sel;
if (spdm_response->header.spdm_version >= SPDM_MESSAGE_VERSION_12) {
spdm_context->connection_info.algorithm.other_params_support =
spdm_response->other_params_selection & SPDM_ALGORITHMS_OPAQUE_DATA_FORMAT_MASK;
if (spdm_response->header.spdm_version >= SPDM_MESSAGE_VERSION_13) {
spdm_context->connection_info.algorithm.other_params_support =
spdm_response->other_params_selection;
spdm_context->connection_info.algorithm.mel_spec =
spdm_response->mel_specification_sel;
}
}
spdm_context->connection_info.algorithm.measurement_hash_algo =
spdm_response->measurement_hash_algo;
spdm_context->connection_info.algorithm.base_asym_algo = spdm_response->base_asym_sel;
spdm_context->connection_info.algorithm.base_hash_algo = spdm_response->base_hash_sel;
if (spdm_response->header.spdm_version >= SPDM_MESSAGE_VERSION_14) {
spdm_context->connection_info.algorithm.pqc_asym_algo = spdm_response->pqc_asym_sel;
}
if (libspdm_is_capabilities_flag_supported(
spdm_context, true, 0,
SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_MEAS_CAP) &&
(spdm_request->measurement_specification != 0)) {
if (spdm_context->connection_info.algorithm.measurement_spec !=
SPDM_MEASUREMENT_SPECIFICATION_DMTF) {
status = LIBSPDM_STATUS_INVALID_MSG_FIELD;
goto receive_done;
}
algo_size = libspdm_get_measurement_hash_size(
spdm_context->connection_info.algorithm.measurement_hash_algo);
if (algo_size == 0) {
status = LIBSPDM_STATUS_NEGOTIATION_FAIL;
goto receive_done;
}
} else {
if (spdm_context->connection_info.algorithm.measurement_spec != 0) {
status = LIBSPDM_STATUS_INVALID_MSG_FIELD;
goto receive_done;
}
}
if (libspdm_is_capabilities_flag_supported(
spdm_context, true, 0,
SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_CERT_CAP) ||
libspdm_is_capabilities_flag_supported(
spdm_context, true, 0,
SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_CHAL_CAP) ||
libspdm_is_capabilities_flag_supported(
spdm_context, true, 0,
SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_MEAS_CAP_SIG) ||
libspdm_is_capabilities_flag_supported(
spdm_context, true,
SPDM_GET_CAPABILITIES_REQUEST_FLAGS_KEY_EX_CAP,
SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_KEY_EX_CAP) ||
libspdm_is_capabilities_flag_supported(
spdm_context, true,
SPDM_GET_CAPABILITIES_REQUEST_FLAGS_PSK_CAP,
SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_PSK_CAP)) {
algo_size = libspdm_get_hash_size(spdm_context->connection_info.algorithm.base_hash_algo);
if (algo_size == 0) {
status = LIBSPDM_STATUS_NEGOTIATION_FAIL;
goto receive_done;
}
if ((spdm_context->connection_info.algorithm.base_hash_algo &
spdm_context->local_context.algorithm.base_hash_algo) == 0) {
status = LIBSPDM_STATUS_NEGOTIATION_FAIL;
goto receive_done;
}
}
if (libspdm_is_capabilities_flag_supported(
spdm_context, true, 0,
SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_CERT_CAP) ||
libspdm_is_capabilities_flag_supported(
spdm_context, true, 0,
SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_CHAL_CAP) ||
libspdm_is_capabilities_flag_supported(
spdm_context, true, 0,
SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_MEAS_CAP_SIG) ||
libspdm_is_capabilities_flag_supported(
spdm_context, true,
SPDM_GET_CAPABILITIES_REQUEST_FLAGS_KEY_EX_CAP,
SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_KEY_EX_CAP)) {
algo_size = libspdm_get_asym_signature_size(
spdm_context->connection_info.algorithm.base_asym_algo);
if (spdm_response->header.spdm_version >= SPDM_MESSAGE_VERSION_14) {
pqc_algo_size = libspdm_get_pqc_asym_signature_size(
spdm_context->connection_info.algorithm.pqc_asym_algo);
} else {
pqc_algo_size = 0;
}
if (((algo_size == 0) && (pqc_algo_size == 0)) ||
((algo_size != 0) && (pqc_algo_size != 0))) {
status = LIBSPDM_STATUS_NEGOTIATION_FAIL;
goto receive_done;
}
if ((algo_size != 0) &&
((spdm_context->connection_info.algorithm.base_asym_algo &
spdm_context->local_context.algorithm.base_asym_algo) == 0)) {
status = LIBSPDM_STATUS_NEGOTIATION_FAIL;
goto receive_done;
}
if ((pqc_algo_size != 0) &&
((spdm_context->connection_info.algorithm.pqc_asym_algo &
spdm_context->local_context.algorithm.pqc_asym_algo) == 0)) {
status = LIBSPDM_STATUS_NEGOTIATION_FAIL;
goto receive_done;
}
}
if (spdm_response->header.spdm_version >= SPDM_MESSAGE_VERSION_11) {
struct_table =
(void *)((size_t)spdm_response +
sizeof(spdm_algorithms_response_t) +
sizeof(uint32_t) * spdm_response->ext_asym_sel_count +
sizeof(uint32_t) * spdm_response->ext_hash_sel_count);
for (index = 0; index < spdm_response->header.param1; index++) {
switch (struct_table->alg_type) {
case SPDM_NEGOTIATE_ALGORITHMS_STRUCT_TABLE_ALG_TYPE_DHE:
spdm_context->connection_info.algorithm.dhe_named_group =
struct_table->alg_supported;
break;
case SPDM_NEGOTIATE_ALGORITHMS_STRUCT_TABLE_ALG_TYPE_AEAD:
spdm_context->connection_info.algorithm.aead_cipher_suite =
struct_table->alg_supported;
break;
case SPDM_NEGOTIATE_ALGORITHMS_STRUCT_TABLE_ALG_TYPE_REQ_BASE_ASYM_ALG:
spdm_context->connection_info.algorithm.req_base_asym_alg =
struct_table->alg_supported;
break;
case SPDM_NEGOTIATE_ALGORITHMS_STRUCT_TABLE_ALG_TYPE_KEY_SCHEDULE:
spdm_context->connection_info.algorithm.key_schedule =
struct_table->alg_supported;
break;
case SPDM_NEGOTIATE_ALGORITHMS_STRUCT_TABLE_ALG_TYPE_REQ_PQC_ASYM_ALG:
spdm_context->connection_info.algorithm.req_pqc_asym_alg =
struct_table->alg_supported;
break;
case SPDM_NEGOTIATE_ALGORITHMS_STRUCT_TABLE_ALG_TYPE_KEM_ALG:
spdm_context->connection_info.algorithm.kem_alg =
struct_table->alg_supported;
break;
}
ext_alg_count = struct_table->alg_count & 0xF;
struct_table =
(void *)((size_t)struct_table +
sizeof(spdm_negotiate_algorithms_common_struct_table_t) +
sizeof(uint32_t) * ext_alg_count);
}
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)) {
algo_size = libspdm_get_dhe_pub_key_size(
spdm_context->connection_info.algorithm.dhe_named_group);
if (spdm_response->header.spdm_version >= SPDM_MESSAGE_VERSION_14) {
pqc_algo_size = libspdm_get_kem_encap_key_size(
spdm_context->connection_info.algorithm.kem_alg);
} else {
pqc_algo_size = 0;
}
if (((algo_size == 0) && (pqc_algo_size == 0)) ||
((algo_size != 0) && (pqc_algo_size != 0))) {
status = LIBSPDM_STATUS_NEGOTIATION_FAIL;
goto receive_done;
}
if ((algo_size != 0) &&
((spdm_context->connection_info.algorithm.dhe_named_group &
spdm_context->local_context.algorithm.dhe_named_group) == 0)) {
status = LIBSPDM_STATUS_NEGOTIATION_FAIL;
goto receive_done;
}
if ((pqc_algo_size != 0) &&
((spdm_context->connection_info.algorithm.kem_alg &
spdm_context->local_context.algorithm.kem_alg) == 0)) {
status = LIBSPDM_STATUS_NEGOTIATION_FAIL;
goto receive_done;
}
}
if (libspdm_is_capabilities_flag_supported(
spdm_context, true,
SPDM_GET_CAPABILITIES_REQUEST_FLAGS_ENCRYPT_CAP,
SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_ENCRYPT_CAP) ||
libspdm_is_capabilities_flag_supported(
spdm_context, true,
SPDM_GET_CAPABILITIES_REQUEST_FLAGS_MAC_CAP,
SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_MAC_CAP)) {
algo_size = libspdm_get_aead_key_size(
spdm_context->connection_info.algorithm.aead_cipher_suite);
if (algo_size == 0) {
status = LIBSPDM_STATUS_NEGOTIATION_FAIL;
goto receive_done;
}
if ((spdm_context->connection_info.algorithm.aead_cipher_suite &
spdm_context->local_context.algorithm.aead_cipher_suite) == 0) {
status = LIBSPDM_STATUS_NEGOTIATION_FAIL;
goto receive_done;
}
}
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)) {
algo_size = libspdm_get_req_asym_signature_size(
spdm_context->connection_info.algorithm.req_base_asym_alg);
if (spdm_response->header.spdm_version >= SPDM_MESSAGE_VERSION_14) {
pqc_algo_size = libspdm_get_req_pqc_asym_signature_size(
spdm_context->connection_info.algorithm.req_pqc_asym_alg);
} else {
pqc_algo_size = 0;
}
if (((algo_size == 0) && (pqc_algo_size == 0)) ||
((algo_size != 0) && (pqc_algo_size != 0))) {
status = LIBSPDM_STATUS_NEGOTIATION_FAIL;
goto receive_done;
}
if ((algo_size != 0) &&
((spdm_context->connection_info.algorithm.req_base_asym_alg &
spdm_context->local_context.algorithm.req_base_asym_alg) == 0)) {
status = LIBSPDM_STATUS_NEGOTIATION_FAIL;
goto receive_done;
}
if ((pqc_algo_size != 0) &&
((spdm_context->connection_info.algorithm.req_pqc_asym_alg &
spdm_context->local_context.algorithm.req_pqc_asym_alg) == 0)) {
status = LIBSPDM_STATUS_NEGOTIATION_FAIL;
goto receive_done;
}
}
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) ||
libspdm_is_capabilities_flag_supported(
spdm_context, true,
SPDM_GET_CAPABILITIES_REQUEST_FLAGS_PSK_CAP,
SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_PSK_CAP)) {
if (spdm_context->connection_info.algorithm.key_schedule !=
SPDM_ALGORITHMS_KEY_SCHEDULE_SPDM) {
status = LIBSPDM_STATUS_NEGOTIATION_FAIL;
goto receive_done;
}
if ((spdm_context->connection_info.algorithm.key_schedule &
spdm_context->local_context.algorithm.key_schedule) == 0) {
status = LIBSPDM_STATUS_NEGOTIATION_FAIL;
goto receive_done;
}
if (spdm_response->header.spdm_version >= SPDM_MESSAGE_VERSION_13) {
if (libspdm_is_capabilities_flag_supported(
spdm_context, true, 0,
SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_MEL_CAP) &&
(spdm_request->mel_specification != 0)) {
if (spdm_context->connection_info.algorithm.mel_spec !=
SPDM_MEL_SPECIFICATION_DMTF) {
status = LIBSPDM_STATUS_INVALID_MSG_FIELD;
goto receive_done;
}
} else {
if (spdm_context->connection_info.algorithm.mel_spec != 0) {
status = LIBSPDM_STATUS_INVALID_MSG_FIELD;
goto receive_done;
}
}
}
}
if (spdm_request->header.spdm_version >= SPDM_MESSAGE_VERSION_13) {
if ((spdm_context->connection_info.algorithm.other_params_support &
SPDM_ALGORITHMS_MULTI_KEY_CONN) == 0) {
if ((spdm_context->local_context.capability.flags &
SPDM_GET_CAPABILITIES_REQUEST_FLAGS_MULTI_KEY_CAP) ==
SPDM_GET_CAPABILITIES_REQUEST_FLAGS_MULTI_KEY_CAP_ONLY) {
status = LIBSPDM_STATUS_NEGOTIATION_FAIL;
goto receive_done;
}
spdm_context->connection_info.multi_key_conn_req = false;
} else {
if ((spdm_context->local_context.capability.flags &
SPDM_GET_CAPABILITIES_REQUEST_FLAGS_MULTI_KEY_CAP) == 0) {
status = LIBSPDM_STATUS_NEGOTIATION_FAIL;
goto receive_done;
}
spdm_context->connection_info.multi_key_conn_req = true;
}
}
} else {
spdm_context->connection_info.algorithm.dhe_named_group = 0;
spdm_context->connection_info.algorithm.aead_cipher_suite = 0;
spdm_context->connection_info.algorithm.req_base_asym_alg = 0;
spdm_context->connection_info.algorithm.key_schedule = 0;
spdm_context->connection_info.algorithm.other_params_support = 0;
spdm_context->connection_info.algorithm.req_pqc_asym_alg = 0;
spdm_context->connection_info.algorithm.kem_alg = 0;
}
LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "base_hash - 0x%08x\n",
spdm_context->connection_info.algorithm.base_hash_algo));
LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "base_asym - 0x%08x\n",
spdm_context->connection_info.algorithm.base_asym_algo));
LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "dhe - 0x%04x\n",
spdm_context->connection_info.algorithm.dhe_named_group));
LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "aead - 0x%04x\n",
spdm_context->connection_info.algorithm.aead_cipher_suite));
LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "req_asym - 0x%04x\n",
spdm_context->connection_info.algorithm.req_base_asym_alg));
LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "pqc_asym - 0x%08x\n",
spdm_context->connection_info.algorithm.pqc_asym_algo));
LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "req_pqc_asym - 0x%04x\n",
spdm_context->connection_info.algorithm.req_pqc_asym_alg));
LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "kem - 0x%04x\n",
spdm_context->connection_info.algorithm.kem_alg));
/* -=[Update State Phase]=- */
spdm_context->connection_info.connection_state = LIBSPDM_CONNECTION_STATE_NEGOTIATED;
/* -=[Log Message Phase]=- */
#if LIBSPDM_ENABLE_MSG_LOG
libspdm_append_msg_log(spdm_context, spdm_response, spdm_response_size);
#endif /* LIBSPDM_ENABLE_MSG_LOG */
status = LIBSPDM_STATUS_SUCCESS;
receive_done:
libspdm_release_receiver_buffer (spdm_context);
return status;
}
/**
* This function sends NEGOTIATE_ALGORITHMS and receives ALGORITHMS.
*
* @param spdm_context A pointer to the SPDM context.
*
* @retval LIBSPDM_STATUS_SUCCESS
* NEGOTIATE_ALGORITHMS was sent and ALGORITHMS was received.
* @retval LIBSPDM_STATUS_INVALID_STATE_LOCAL
* Cannot send NEGOTIATE_ALGORITHMS due to Requester's state.
* @retval LIBSPDM_STATUS_INVALID_MSG_SIZE
* The size of the ALGORITHMS response is invalid.
* @retval LIBSPDM_STATUS_INVALID_MSG_FIELD
* The ALGORITHMS response contains one or more invalid fields.
* @retval LIBSPDM_STATUS_ERROR_PEER
* The Responder returned an unexpected error.
* @retval LIBSPDM_STATUS_BUSY_PEER
* The Responder continually returned Busy error messages.
* @retval LIBSPDM_STATUS_RESYNCH_PEER
* The Responder returned a RequestResynch error message.
* @retval LIBSPDM_STATUS_BUFFER_FULL
* The buffer used to store transcripts is exhausted.
* @retval LIBSPDM_STATUS_NEGOTIATION_FAIL
* The Requester and Responder could not agree on mutual algorithms.
* Note: This return value may be removed in the future.
**/
static libspdm_return_t libspdm_try_negotiate_algorithms(libspdm_context_t *spdm_context)
{
libspdm_return_t status;
libspdm_negotiate_algorithms_request_mine_t *spdm_request;
size_t spdm_request_size;
status = libspdm_negotiate_algorithms_send_request(
spdm_context, &spdm_request, &spdm_request_size);
if (LIBSPDM_STATUS_IS_ERROR(status)) {
return status;
}
status = libspdm_negotiate_algorithms_process_response(
spdm_context, spdm_request, spdm_request_size);
return status;
}
libspdm_return_t libspdm_negotiate_algorithms(libspdm_context_t *spdm_context)
{
size_t retry;
uint64_t retry_delay_time;
libspdm_return_t status;
spdm_context->crypto_request = false;
retry = spdm_context->retry_times;
retry_delay_time = spdm_context->retry_delay_time;
do {
status = libspdm_try_negotiate_algorithms(spdm_context);
if (status != LIBSPDM_STATUS_BUSY_PEER) {
return status;
}
libspdm_sleep(retry_delay_time);
} while (retry-- != 0);
return status;
}