aboutsummaryrefslogtreecommitdiff
path: root/components/service/attestation/test/component/attestation_reporter_tests.cpp
blob: 02b91ac5d0193216a892456aa1a3ab2348d3442c (plain)
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
/*
 * Copyright (c) 2021, Arm Limited and Contributors. All rights reserved.
 *
 * SPDX-License-Identifier: BSD-3-Clause
 */

#include <psa/error.h>
#include <qcbor/qcbor_spiffy_decode.h>
#include <t_cose/t_cose_sign1_verify.h>
#include <service/attestation/claims/claims_register.h>
#include <service/attestation/claims/sources/event_log/event_log_claim_source.h>
#include <service/attestation/claims/sources/event_log/mock/mock_event_log.h>
#include <service/attestation/claims/sources/preloaded/preloaded_claim_source.h>
#include <service/attestation/reporter/attest_report.h>
#include <service/attestation/key_mngr/attest_key_mngr.h>
#include <protocols/service/attestation/packed-c/eat.h>
#include <CppUTest/TestHarness.h>
#include <psa/crypto.h>
#include "report_dump.h"

TEST_GROUP(AttestationReporterTests)
{
    void setup()
    {
        struct claim_source *claim_source;

        report = NULL;
        report_len;

        psa_crypto_init();
        attest_key_mngr_init();

        /* The set of registered claim_sources determines the content
         * of a generated attestation source.  The set and type of
         * claim_sources registered will be deployment specific.
         */
        claims_register_init();

        /* Boot measurement source */
        claim_source = event_log_claim_source_init(&event_log_claim_source,
            mock_event_log_start(), mock_event_log_size());
        claims_register_add_claim_source(CLAIM_CATEGORY_BOOT_MEASUREMENT, claim_source);
    }

    void teardown()
    {
        attest_report_destroy(report);
        claims_register_deinit();
        attest_key_mngr_deinit();
    }

    struct event_log_claim_source event_log_claim_source;
    const uint8_t *report;
    size_t report_len;
};

TEST(AttestationReporterTests, createReport)
{
    int status;

    /* Client inputs */
    int32_t client_id = 0x552791aa;
    const uint8_t auth_challenge[] = {
         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
    };

    /* Retrieve the IAK handle */
    psa_key_handle_t iak_handle;
    status = attest_key_mngr_get_iak_handle(&iak_handle);
    LONGS_EQUAL(PSA_SUCCESS, status);

    /* Create a report */
    status = attest_report_create(iak_handle, client_id,
        auth_challenge, sizeof(auth_challenge),
        &report, &report_len);

    /* Expect the operation to succeed and a non-zero length
     * report created.
     */
    LONGS_EQUAL(PSA_SUCCESS, status);
    CHECK_TRUE(report);
    CHECK_TRUE(report_len);

    /* Verify the signature */
    struct t_cose_sign1_verify_ctx verify_ctx;
    struct t_cose_key key_pair;

    key_pair.k.key_handle = iak_handle;
    key_pair.crypto_lib = T_COSE_CRYPTO_LIB_PSA;
    UsefulBufC signed_cose;
    UsefulBufC report_body;

    signed_cose.ptr = report;
    signed_cose.len = report_len;

    t_cose_sign1_verify_init(&verify_ctx, 0);
    t_cose_sign1_set_verification_key(&verify_ctx, key_pair);

    status = t_cose_sign1_verify(&verify_ctx, signed_cose, &report_body, NULL);
    LONGS_EQUAL(T_COSE_SUCCESS, status);

    /* Check the report contents */
    QCBORDecodeContext decode_ctx;

    QCBORDecode_Init(&decode_ctx, report_body, QCBOR_DECODE_MODE_NORMAL);
    QCBORDecode_EnterMap(&decode_ctx, NULL);

    /* Check client id */
    int64_t decoded_client_id = 0;
    QCBORDecode_GetInt64InMapN(&decode_ctx,
        EAT_ARM_PSA_CLAIM_ID_CLIENT_ID, &decoded_client_id);

    LONGS_EQUAL(QCBOR_SUCCESS, QCBORDecode_GetError(&decode_ctx));
    LONGS_EQUAL(client_id, decoded_client_id);

    /* Check the auth challenge */
    UsefulBufC auth_challenge_buf;
    auth_challenge_buf.ptr = NULL;
    auth_challenge_buf.len = 0;
    QCBORDecode_GetByteStringInMapN(&decode_ctx,
        EAT_ARM_PSA_CLAIM_ID_CHALLENGE, &auth_challenge_buf);

    LONGS_EQUAL(QCBOR_SUCCESS, QCBORDecode_GetError(&decode_ctx));
    CHECK_TRUE(auth_challenge_buf.ptr);
    UNSIGNED_LONGS_EQUAL(sizeof(auth_challenge), auth_challenge_buf.len);
    MEMCMP_EQUAL(auth_challenge, auth_challenge_buf.ptr, sizeof(auth_challenge));

    /* Shouldn't expect to see the 'NO_SW_COMPONENTS' claim */
    int64_t no_sw = 0;
    QCBORDecode_GetInt64InMapN(&decode_ctx, EAT_ARM_PSA_CLAIM_ID_NO_SW_COMPONENTS, &no_sw);
    LONGS_EQUAL(QCBOR_ERR_LABEL_NOT_FOUND, QCBORDecode_GetAndResetError(&decode_ctx));
    CHECK_FALSE(no_sw);

    /* Check the sw components */
    QCBORDecode_EnterArrayFromMapN(&decode_ctx, EAT_ARM_PSA_CLAIM_ID_SW_COMPONENTS);
    LONGS_EQUAL(QCBOR_SUCCESS, QCBORDecode_GetError(&decode_ctx));

    /* Iterate over all array members */
    size_t sw_component_count = 0;
    while (true) {

        QCBORDecode_EnterMap(&decode_ctx, NULL);

        if (QCBORDecode_GetAndResetError(&decode_ctx) == QCBOR_SUCCESS) {

            CHECK_TRUE(sw_component_count < mock_event_Log_measurement_count());

            UsefulBufC property;
            const struct mock_event_log_measurement *measurement =
                mock_event_Log_measurement(sw_component_count);

            /* Check measurement id */
            QCBORDecode_GetByteStringInMapN(&decode_ctx,
                    EAT_SW_COMPONENT_CLAIM_ID_MEASUREMENT_TYPE, &property);
            LONGS_EQUAL(QCBOR_SUCCESS, QCBORDecode_GetError(&decode_ctx));
            CHECK_TRUE(property.ptr);
            CHECK_TRUE(property.len);
            MEMCMP_EQUAL(measurement->id, property.ptr, property.len);

            /* Check measurement digest */
            QCBORDecode_GetByteStringInMapN(&decode_ctx,
                    EAT_SW_COMPONENT_CLAIM_ID_MEASUREMENT_VALUE, &property);
            LONGS_EQUAL(QCBOR_SUCCESS, QCBORDecode_GetError(&decode_ctx));
            CHECK_TRUE(property.ptr);
            CHECK_TRUE(property.len);
            MEMCMP_EQUAL(measurement->digest, property.ptr, property.len);

            QCBORDecode_ExitMap(&decode_ctx);

            ++sw_component_count;
        }
        else {
            /* No more sw components */
            break;
        }
    }

    QCBORDecode_ExitArray(&decode_ctx);
    LONGS_EQUAL(QCBOR_SUCCESS, QCBORDecode_GetError(&decode_ctx));

    QCBORError qcbor_error;
    QCBORDecode_ExitMap(&decode_ctx);
    qcbor_error = QCBORDecode_Finish(&decode_ctx);
    LONGS_EQUAL(QCBOR_SUCCESS, qcbor_error);
}