blob: eac7c6e98147c5778dca17ac09ac8bffc4244cca [file] [log] [blame]
Laurence Lundbladeaffd65a2018-12-18 10:50:48 -08001/*
2 * attest_token.h
3 *
4 * Copyright (c) 2018-2019, Laurence Lundblade. All rights reserved.
5 *
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,
83 /** */
84 ATTEST_TOKEN_ERR_NO_VALID_TOKEN
Laurence Lundbladeaffd65a2018-12-18 10:50:48 -080085};
86
87
88
89/**
90 * Request that the claims internally generated not be added to the
91 * token. This is a test mode that results in a static token that
92 * never changes. Only the nonce is included. The nonce is under
93 * the callers control unlike the other claims.
94 */
95#define TOKEN_OPT_OMIT_CLAIMS 0x40000000
96
97
98/**
99 * A special test mode where a proper signature is not produced. In
100 * its place there is a concatenation of hashes of the payload to be
101 * the same size as the signature. This works and can be used to
102 * verify all of the SW stack except the public signature part. The
103 * token has no security value in this mode because anyone can
104 * replicate it. */
105#define TOKEN_OPT_SHORT_CIRCUIT_SIGN 0x80000000
106
107
108/**
109 * The context for creating an attestation token. The caller of
110 * attest_token must create one of these and pass it to the functions
111 * here. It is small enough that it can go on the stack. It is most of
112 * the memory needed to create a token except the output buffer and
113 * any memory requirements for the cryptographic operations.
114 *
115 * The structure is opaque for the caller.
116 *
117 * This is roughly 148 + 8 + 32 = 188 bytes
118 */
119struct attest_token_ctx {
120 /* Private data structure */
121 QCBOREncodeContext cbor_enc_ctx;
122 uint32_t opt_flags;
123 int32_t key_select;
124 struct t_cose_sign1_ctx signer_ctx;
125};
126
127
128/**
129 * \brief Initialize a token creation context.
130 *
131 * \param[in] me The token creation context to be initialized.
132 * \param[in] opt_flags Flags to select different custom options,
133 for example \ref TOKEN_OPT_OMIT_CLAIMS.
134 * \param[in] key_select Selects which attestation key to sign with.
135 * \param[in] cose_alg_id The algorithm to sign with. The IDs are
136 * defined in [COSE (RFC 8152)]
137 * (https://tools.ietf.org/html/rfc8152) or
138 * in the [IANA COSE Registry]
139 * (https://www.iana.org/assignments/cose/cose.xhtml).
140 * \param[out] out_buffer The output buffer to write the encoded token into.
141 *
142 * \return one of the \ref attest_token_err_t errors.
143 *
144 * The size of the buffer in \c out_buffer->len
145 * determines the size of the token that can be created. It must be
146 * able to hold the final encoded and signed token. The data encoding
147 * overhead is just that of CBOR. The signing overhead depends on the
148 * signing key size. It is about 150 bytes for 256-bit ECDSA.
149 *
150 * If \c out_buffer->ptr is \c NULL and \c out_buffer_ptr->len is
151 * large like \c UINT32_MAX no token will be created but the length of
152 * the token that would be created will be in \c completed_token as
153 * returned by attest_token_finish(). None of the cryptographic
154 * functions run during this, but the sizes of what they would output
155 * is taken into account.
156 */
157enum attest_token_err_t
158attest_token_start(struct attest_token_ctx *me,
159 uint32_t opt_flags,
160 int32_t key_select,
161 int32_t cose_alg_id,
Laurence Lundbladee1610ad2019-02-20 13:53:20 -0800162 const struct q_useful_buf *out_buffer);
Laurence Lundbladeaffd65a2018-12-18 10:50:48 -0800163
164
165
166/**
167 * \brief Get a copy of the CBOR encoding context
168 *
169 * \param[in] me Token creation context.
170 *
171 * \return The CBOR encoding context
172 *
173 * Allows the caller to encode CBOR right into the output buffer using
174 * any of the \c QCBOREncode_AddXXXX() methods. Anything added here
175 * will be part of the payload that gets hashed. This can be used to
176 * make complex CBOR structures. All open arrays and maps must be
177 * close before calling any other \c attest_token methods. \c
178 * QCBOREncode_Finish() should not be closed on this context.
179 */
180QCBOREncodeContext *attest_token_borrow_cbor_cntxt(struct attest_token_ctx *me);
181
182/**
183 * \brief Add a 64-bit signed integer claim
184 *
185 * \param[in] me Token creation context.
186 * \param[in] label Integer label for claim.
187 * \param[in] value The integer claim data.
188 */
189void attest_token_add_integer(struct attest_token_ctx *me,
190 int32_t label,
191 int64_t value);
192
193/**
194 * \brief Add a binary string claim
195 *
196 * \param[in] me Token creation context.
197 * \param[in] label Integer label for claim.
198 * \param[in] value The binary claim data.
199 */
200void attest_token_add_bstr(struct attest_token_ctx *me,
201 int32_t label,
Laurence Lundbladee1610ad2019-02-20 13:53:20 -0800202 const struct q_useful_buf_c *value);
Laurence Lundbladeaffd65a2018-12-18 10:50:48 -0800203
204/**
205 * \brief Add a text string claim
206 *
207 * \param[in] me Token creation context.
208 * \param[in] label Integer label for claim.
209 * \param[in] value The text claim data.
210 */
211void attest_token_add_tstr(struct attest_token_ctx *me,
212 int32_t label,
Laurence Lundbladee1610ad2019-02-20 13:53:20 -0800213 const struct q_useful_buf_c *value);
Laurence Lundbladeaffd65a2018-12-18 10:50:48 -0800214
215/**
216 * \brief Add some already-encoded CBOR to payload
217 *
218 * \param[in] me Token creation context.
219 * \param[in] label Integer label for claim.
220 * \param[in] encoded The already-encoded CBOR.
221 *
222 * Encoded CBOR must be a full map or full array or a non-aggregate
223 * type. It cannot be a partial map or array. It can be nested maps
224 * and arrays, but they must all be complete.
225 */
226void attest_token_add_encoded(struct attest_token_ctx *me,
227 int32_t label,
Laurence Lundbladee1610ad2019-02-20 13:53:20 -0800228 const struct q_useful_buf_c *encoded);
Laurence Lundbladeaffd65a2018-12-18 10:50:48 -0800229
230
231/**
232 * \brief Finish the token, complete the signing and get the result
233 *
234 * \param[in] me Token Creation Context.
235 * \param[out] completed_token Pointer and length to completed token.
236 *
237 * \return one of the \ref attest_token_err_t errors.
238 *
239 * This completes the token after the payload has been added. When
240 * this is called the signing algorithm is run and the final
241 * formatting of the token is completed.
242 */
243enum attest_token_err_t
244attest_token_finish(struct attest_token_ctx *me,
Laurence Lundbladee1610ad2019-02-20 13:53:20 -0800245 struct q_useful_buf_c *completed_token);
246
247#ifdef __cplusplus
248}
249#endif
Laurence Lundbladeaffd65a2018-12-18 10:50:48 -0800250
251#endif /* __ATTEST_TOKEN_H__ */