Skip to content

Commit be52486

Browse files
authored
Revisit TCG DICE TCB Freshness extension rendering. (#70)
Instead of using a new type with a single field the extension callback now excpects n20_slice_t as context.
1 parent 14f3224 commit be52486

3 files changed

Lines changed: 16 additions & 44 deletions

File tree

include/nat20/x509_ext_tcg_dice_tcb_freshness.h

Lines changed: 5 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -27,30 +27,6 @@
2727
extern "C" {
2828
#endif
2929

30-
/**
31-
* @brief TCG DICE TCB Freshness X509 extension context.
32-
*
33-
* This is the context expected by
34-
* @ref n20_x509_ext_tcg_dice_tcb_freshness_content.
35-
* An instance of this object must be passed to the callback.
36-
* This is typically done using @ref n20_x509_extension by
37-
* initializing @ref n20_x509_extension_t.content_cb with
38-
* @ref n20_x509_ext_tcg_dice_tcb_freshness_content and setting
39-
* @ref n20_x509_extension_t.context to an instance of this
40-
* struct.
41-
*
42-
* (See TCG DICE Attestation Architecture Version 1.1, Section 6.3.)
43-
* @sa OID_TCG_DICE_TCB_FRESHNESS
44-
*/
45-
struct n20_x509_ext_tcg_dice_tcb_freshness_s {
46-
/**
47-
* @brief Nonce used as the freshness indicator.
48-
*
49-
* If nonce.buffer is NULL, the nonce is not rendered in the extension.
50-
*/
51-
n20_slice_t nonce;
52-
};
53-
5430
/**
5531
* @brief Alias for @ref n20_x509_ext_tcg_dice_tcb_freshness_s
5632
*/
@@ -60,11 +36,13 @@ typedef struct n20_x509_ext_tcg_dice_tcb_freshness_s n20_x509_ext_tcg_dice_tcb_f
6036
* @brief Renders the value of a TCG DICE TCB Freshness X509 extension.
6137
*
6238
* The function expects a pointer to an instance of
63-
* @ref n20_x509_ext_tcg_dice_tcb_freshness_t as @p context argument.
39+
* @ref n20_slice_t as @p context argument.
6440
*
65-
* If @p context is NULL, nothing is rendered, which would leave the resulting TCG DICE TCB
66-
* Freshness extension malformed.
41+
* If @p context is NULL, or if @p context->buffer is NULL, nothing is rendered,
42+
* which would leave the resulting TCG DICE TCB Freshness extension malformed.
6743
*
44+
* (See TCG DICE Attestation Architecture Version 1.1, Section 6.3.)
45+
* @sa OID_TCG_DICE_TCB_FRESHNESS
6846
* This function is typically not used directly but instead
6947
* passed to @ref n20_x509_extension by initializing an
7048
* instance of @ref n20_x509_extensions_t

src/core/test/x509_ext_tcg_dice_tcb_freshness.cpp

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,18 @@
1414
* limitations under the License.
1515
*/
1616

17-
#include "nat20/x509_ext_tcg_dice_tcb_freshness.h"
18-
1917
#include <gtest/gtest.h>
18+
#include <nat20/oid.h>
19+
#include <nat20/types.h>
20+
#include <nat20/x509.h>
21+
#include <nat20/x509_ext_tcg_dice_tcb_freshness.h>
2022

2123
#include <cstdint>
2224
#include <cstring>
2325
#include <optional>
2426
#include <tuple>
2527
#include <vector>
2628

27-
#include "nat20/oid.h"
28-
#include "nat20/x509.h"
29-
3029
class X509ExtTcgTcbFreshnessTest
3130
: public testing::TestWithParam<
3231
std::tuple<std::optional<std::vector<uint8_t>>, std::vector<uint8_t> const>> {};
@@ -69,15 +68,11 @@ INSTANTIATE_TEST_CASE_P(TcgTcbFreshnessEncoding,
6968

7069
TEST_P(X509ExtTcgTcbFreshnessTest, TcgTcbFreshnessEncoding) {
7170
auto [optional_nonce, expected] = GetParam();
72-
n20_x509_ext_tcg_dice_tcb_freshness_t freshness;
73-
std::memset(&freshness, 0, sizeof(freshness));
71+
n20_slice_t freshness = N20_SLICE_NULL;
7472

7573
if (optional_nonce.has_value()) {
76-
freshness.nonce.buffer = optional_nonce.value().data();
77-
freshness.nonce.size = optional_nonce.value().size();
78-
} else {
79-
freshness.nonce.buffer = nullptr;
80-
freshness.nonce.size = 0;
74+
freshness.buffer = optional_nonce.value().data();
75+
freshness.size = optional_nonce.value().size();
8176
}
8277

8378
n20_x509_extension_t extensions[] = {

src/core/x509_ext_tcg_dice_tcb_freshness.c

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,11 @@
2121

2222
static void n20_x509_ext_tcg_dice_tcb_freshness_sequence_content(n20_stream_t *const s,
2323
void *context) {
24-
n20_x509_ext_tcg_dice_tcb_freshness_t const *const tcg_dice_tcb_freshness =
25-
(n20_x509_ext_tcg_dice_tcb_freshness_t const *)context;
24+
n20_slice_t const *const nonce = (n20_slice_t const *)context;
2625

27-
// tcg_dice_tcb_freshness is never NULL since it's checked by
28-
// n20_x509_ext_tcg_dice_tcb_freshness_content.
29-
n20_asn1_octetstring(s, &tcg_dice_tcb_freshness->nonce, n20_asn1_tag_info_no_override());
26+
/* nonce is never NULL because it is checked by
27+
* n20_x509_ext_tcg_dice_tcb_freshness_content. */
28+
n20_asn1_octetstring(s, nonce, n20_asn1_tag_info_no_override());
3029
}
3130

3231
void n20_x509_ext_tcg_dice_tcb_freshness_content(n20_stream_t *const s, void *context) {

0 commit comments

Comments
 (0)