blob: 14c080d99d691b04f31e51006f3ee8c59ae59527 [file] [log] [blame]
Laurence Lundbladeaffd65a2018-12-18 10:50:48 -08001/*
2 * attest_token.h
3 *
Laurence Lundbladedada1232019-03-01 09:13:14 -08004 * Copyright (c) 2018-2019, Laurence Lundblade.
Laurence Lundbladeaffd65a2018-12-18 10:50:48 -08005 *
6 * SPDX-License-Identifier: BSD-3-Clause
7 *
8 * See BSD-3-Clause license in README.md
9 */
10
11#ifndef __ATTEST_TOKEN_H__
12#define __ATTEST_TOKEN_H__
13
14#include <stdint.h>
15#include "qcbor.h"
16#include "t_cose_sign1_sign.h"
17
Laurence Lundbladee1610ad2019-02-20 13:53:20 -080018#ifdef __cplusplus
19extern "C" {
20#endif
21
Laurence Lundbladeaffd65a2018-12-18 10:50:48 -080022
23/**
24 * \file attest_token.h
25 *
26 * \brief Attestation Token Creation Interface
27 *
28 * The context and functions here are the way to create an attestation
29 * token. The steps are roughly:
30 *
Laurence Lundbladee1610ad2019-02-20 13:53:20 -080031 * -# Create and initialize an attest_token_ctx indicating the
Laurence Lundbladeaffd65a2018-12-18 10:50:48 -080032 * options, key and such using attest_token_start().
33 *
34 * -# Use various add methods to fill in the payload with claims. The
35 * encoding context can also be borrowed for more rich payloads.
36 *
37 * -# Call attest_token_finish() to create the signature and finish
38 * formatting the COSE signed output.
39 */
40
41
42/**
43 Error codes returned from attestation token creation.
44 */
45enum attest_token_err_t {
46 /** Success */
47 ATTEST_TOKEN_ERR_SUCCESS = 0,
48 /** The buffer passed in to receive the output is too small. */
49 ATTEST_TOKEN_ERR_TOO_SMALL,
50 /** Something went wrong formatting the CBOR, most likely the
51 payload has maps or arrays that are not closed. */
52 ATTEST_TOKEN_ERR_CBOR_FORMATTING,
53 /** A general, unspecific error when creating or decoding the
54 token. */
55 ATTEST_TOKEN_ERR_GENERAL,
56 /** A hash function that is needed to make the token is not
57 available. */
58 ATTEST_TOKEN_ERR_HASH_UNAVAILABLE,
59 /** CBOR Syntax not well-formed -- a CBOR syntax error. */
60 ATTEST_TOKEN_ERR_CBOR_NOT_WELL_FORMED,
61 /** Bad CBOR structure, for example not a map when was is
62 required. */
63 ATTEST_TOKEN_ERR_CBOR_STRUCTURE,
64 /** Bad CBOR type, for example an not a text string, when a text
65 string is required. */
66 ATTETST_TOKEN_ERR_CBOR_TYPE,
67 /** Integer too large, for example an \c int32_t is required, but
68 value only fits in \c int64_t */
69 ATTEST_TOKEN_ERR_INTEGER_VALUE,
70 /** Something is wrong with the COSE signing structure, missing
71 headers or such. */
72 ATTEST_TOKEN_ERR_COSE_SIGN1_FORMAT,
73 /** COSE signature is invalid, data is corrupted. */
74 ATTEST_TOKEN_ERR_COSE_SIGN1_VALIDATION,
75 /** The signing algorithm is not supported. */
76 ATTEST_TOKEN_ERR_UNSUPPORTED_SIG_ALG,
77 /** Out of memory. */
78 ATTEST_TOKEN_ERR_INSUFFICIENT_MEMORY,
79 /** Tampering detected in cryptographic function. */
80 ATTEST_TOKEN_ERR_TAMPERING_DETECTED,
81 /** Verification key is not found or of wrong type. */
Laurence Lundbladee1610ad2019-02-20 13:53:20 -080082 ATTEST_TOKEN_ERR_VERIFICATION_KEY,
Laurence Lundbladedada1232019-03-01 09:13:14 -080083 /** No token was given or validated */
84 ATTEST_TOKEN_ERR_NO_VALID_TOKEN,
85 /** Data item with label wasn't found. */
86 ATTEST_TOKEN_ERR_NOT_FOUND,
87 /** SW Compoments absence not correctly indicated. */
88 ATTEST_TOKEN_ERR_SW_COMPONENTS_MISSING
Laurence Lundbladeaffd65a2018-12-18 10:50:48 -080089};
90
91
92
93/**
94 * Request that the claims internally generated not be added to the
95 * token. This is a test mode that results in a static token that
96 * never changes. Only the nonce is included. The nonce is under
97 * the callers control unlike the other claims.
98 */
99#define TOKEN_OPT_OMIT_CLAIMS 0x40000000
100
101
102/**
103 * A special test mode where a proper signature is not produced. In
104 * its place there is a concatenation of hashes of the payload to be
105 * the same size as the signature. This works and can be used to
106 * verify all of the SW stack except the public signature part. The
107 * token has no security value in this mode because anyone can
108 * replicate it. */
109#define TOKEN_OPT_SHORT_CIRCUIT_SIGN 0x80000000
110
111
112/**
113 * The context for creating an attestation token. The caller of
114 * attest_token must create one of these and pass it to the functions
115 * here. It is small enough that it can go on the stack. It is most of
116 * the memory needed to create a token except the output buffer and
117 * any memory requirements for the cryptographic operations.
118 *
119 * The structure is opaque for the caller.
120 *
121 * This is roughly 148 + 8 + 32 = 188 bytes
122 */
123struct attest_token_ctx {
124 /* Private data structure */
125 QCBOREncodeContext cbor_enc_ctx;
126 uint32_t opt_flags;
127 int32_t key_select;
128 struct t_cose_sign1_ctx signer_ctx;
129};
130
131
132/**
133 * \brief Initialize a token creation context.
134 *
135 * \param[in] me The token creation context to be initialized.
136 * \param[in] opt_flags Flags to select different custom options,
137 for example \ref TOKEN_OPT_OMIT_CLAIMS.
138 * \param[in] key_select Selects which attestation key to sign with.
139 * \param[in] cose_alg_id The algorithm to sign with. The IDs are
140 * defined in [COSE (RFC 8152)]
141 * (https://tools.ietf.org/html/rfc8152) or
142 * in the [IANA COSE Registry]
143 * (https://www.iana.org/assignments/cose/cose.xhtml).
144 * \param[out] out_buffer The output buffer to write the encoded token into.
145 *
146 * \return one of the \ref attest_token_err_t errors.
147 *
148 * The size of the buffer in \c out_buffer->len
149 * determines the size of the token that can be created. It must be
150 * able to hold the final encoded and signed token. The data encoding
151 * overhead is just that of CBOR. The signing overhead depends on the
152 * signing key size. It is about 150 bytes for 256-bit ECDSA.
153 *
154 * If \c out_buffer->ptr is \c NULL and \c out_buffer_ptr->len is
155 * large like \c UINT32_MAX no token will be created but the length of
156 * the token that would be created will be in \c completed_token as
157 * returned by attest_token_finish(). None of the cryptographic
158 * functions run during this, but the sizes of what they would output
159 * is taken into account.
160 */
161enum attest_token_err_t
162attest_token_start(struct attest_token_ctx *me,
163 uint32_t opt_flags,
164 int32_t key_select,
165 int32_t cose_alg_id,
Laurence Lundbladee1610ad2019-02-20 13:53:20 -0800166 const struct q_useful_buf *out_buffer);
Laurence Lundbladeaffd65a2018-12-18 10:50:48 -0800167
168
169
170/**
171 * \brief Get a copy of the CBOR encoding context
172 *
173 * \param[in] me Token creation context.
174 *
175 * \return The CBOR encoding context
176 *
177 * Allows the caller to encode CBOR right into the output buffer using
178 * any of the \c QCBOREncode_AddXXXX() methods. Anything added here
179 * will be part of the payload that gets hashed. This can be used to
180 * make complex CBOR structures. All open arrays and maps must be
181 * close before calling any other \c attest_token methods. \c
182 * QCBOREncode_Finish() should not be closed on this context.
183 */
184QCBOREncodeContext *attest_token_borrow_cbor_cntxt(struct attest_token_ctx *me);
185
186/**
187 * \brief Add a 64-bit signed integer claim
188 *
189 * \param[in] me Token creation context.
190 * \param[in] label Integer label for claim.
191 * \param[in] value The integer claim data.
192 */
193void attest_token_add_integer(struct attest_token_ctx *me,
194 int32_t label,
195 int64_t value);
196
197/**
198 * \brief Add a binary string claim
199 *
200 * \param[in] me Token creation context.
201 * \param[in] label Integer label for claim.
202 * \param[in] value The binary claim data.
203 */
204void attest_token_add_bstr(struct attest_token_ctx *me,
205 int32_t label,
Laurence Lundbladee1610ad2019-02-20 13:53:20 -0800206 const struct q_useful_buf_c *value);
Laurence Lundbladeaffd65a2018-12-18 10:50:48 -0800207
208/**
209 * \brief Add a text string claim
210 *
211 * \param[in] me Token creation context.
212 * \param[in] label Integer label for claim.
213 * \param[in] value The text claim data.
214 */
215void attest_token_add_tstr(struct attest_token_ctx *me,
216 int32_t label,
Laurence Lundbladee1610ad2019-02-20 13:53:20 -0800217 const struct q_useful_buf_c *value);
Laurence Lundbladeaffd65a2018-12-18 10:50:48 -0800218
219/**
220 * \brief Add some already-encoded CBOR to payload
221 *
222 * \param[in] me Token creation context.
223 * \param[in] label Integer label for claim.
224 * \param[in] encoded The already-encoded CBOR.
225 *
226 * Encoded CBOR must be a full map or full array or a non-aggregate
227 * type. It cannot be a partial map or array. It can be nested maps
228 * and arrays, but they must all be complete.
229 */
230void attest_token_add_encoded(struct attest_token_ctx *me,
231 int32_t label,
Laurence Lundbladee1610ad2019-02-20 13:53:20 -0800232 const struct q_useful_buf_c *encoded);
Laurence Lundbladeaffd65a2018-12-18 10:50:48 -0800233
234
235/**
236 * \brief Finish the token, complete the signing and get the result
237 *
238 * \param[in] me Token Creation Context.
239 * \param[out] completed_token Pointer and length to completed token.
240 *
241 * \return one of the \ref attest_token_err_t errors.
242 *
243 * This completes the token after the payload has been added. When
244 * this is called the signing algorithm is run and the final
245 * formatting of the token is completed.
246 */
247enum attest_token_err_t
248attest_token_finish(struct attest_token_ctx *me,
Laurence Lundbladee1610ad2019-02-20 13:53:20 -0800249 struct q_useful_buf_c *completed_token);
250
251#ifdef __cplusplus
252}
253#endif
Laurence Lundbladeaffd65a2018-12-18 10:50:48 -0800254
255#endif /* __ATTEST_TOKEN_H__ */