1 /*
2  * t_cose_make_test_messages.h
3  *
4  * Copyright (c) 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 __T_COSE_MAKE_TEST_MESSAGES__
12 #define __T_COSE_MAKE_TEST_MESSAGES__
13 
14 
15 #include <stdint.h>
16 #include <stdbool.h>
17 #include "qcbor/qcbor.h"
18 #include "t_cose_common.h"
19 #include "t_cose_sign1_sign.h"
20 
21 
22 #ifdef __cplusplus
23 extern "C" {
24 #endif
25 
26 
27 /**
28  * \file t_cose_make_test_messages.h
29  *
30  * \brief Create a test \c COSE_Sign1 message for testing the verifier.
31  *
32  */
33 
34 
35 /**
36  * Various flags to pass to t_cose_test_message_sign1_sign() to
37  * make different types of test messages for testing verification
38  */
39 
40 /** Make test message with a bstr label, which is not allowed by
41   * COSE */
42 #define T_COSE_TEST_PARAMETER_LABEL 0x80000000U
43 
44 /** Format of the crit parameter is made invalid */
45 #define T_COSE_TEST_BAD_CRIT_PARAMETER   0x40000000
46 
47 /** An extra parameter is added. It has nested structure to be sure
48  *  such are skipped correctly */
49 #define T_COSE_TEST_EXTRA_PARAMETER 0x20000000
50 
51 /** The protected parameters bucked is left out of the COSE_Sign1
52  *  message entirely */
53 #define T_COSE_TEST_NO_PROTECTED_PARAMETERS 0x10000000
54 
55 /** The unprotected parameters bucked is left out of the COSE_Sign1
56  *  message entirely */
57 #define T_COSE_TEST_NO_UNPROTECTED_PARAMETERS 0x08000000
58 
59 /** Simple not-well-formed CBOR is added to the unprotected parameters
60  *  bucket */
61 #define T_COSE_TEST_NOT_WELL_FORMED_1 0x04000000
62 
63 /** Not-well-formed CBOR nested in a map is added to the unprotected
64  *  parameters bucket */
65 #define T_COSE_TEST_NOT_WELL_FORMED_2 0x02000000
66 
67 /** The crit parameter lists several integer critical labels and the
68  *  labeled parameters exists and they are not understood */
69 #define T_COSE_TEST_UNKNOWN_CRIT_UINT_PARAMETER 0x01000000
70 
71 /** The crit parameter lists critical labels, but none of them
72  *  occur */
73 #define T_COSE_TEST_CRIT_PARAMETER_EXIST 0x00800000
74 
75 /** Exceed the limit on number of T_COSE_PARAMETER_LIST_MAX on number
76  * of crit parameters this implementation can handle */
77 #define T_COSE_TEST_TOO_MANY_CRIT_PARAMETER_EXIST 0x00400000
78 
79 /** One of the labels in the crit parameter is of the wrong type */
80 #define T_COSE_TEST_BAD_CRIT_LABEL 0x00200000
81 
82 /** The crit parameter is in the unprotected bucket */
83 #define T_COSE_TEST_CRIT_NOT_PROTECTED 0x00100000
84 
85 /** More than T_COSE_PARAMETER_LIST_MAX unknown parameters occured */
86 #define T_COSE_TEST_TOO_MANY_UNKNOWN 0x00080000
87 
88 /** The crit parameter lists several text string critical labels and
89  * the labeled parameters exists and they are not understood */
90 #define T_COSE_TEST_UNKNOWN_CRIT_TSTR_PARAMETER 0x00040000
91 
92 /** One of each type of parameter the verify handles is added, plus
93  *  some unknown parameters */
94 #define T_COSE_TEST_ALL_PARAMETERS 0x00020000
95 
96 /** An invalid CBOR type is in the protected bucket */
97 #define T_COSE_TEST_BAD_PROTECTED 0x00010000
98 
99 /** The unprotected header bucket is an array, not a map */
100 #define T_COSE_TEST_UNPROTECTED_NOT_MAP 0x00008000
101 
102 /** A kid is added to the protected parameters and is thus a duplicate
103  *  parameter in both protected and unprotected buckets */
104 #define T_COSE_TEST_KID_IN_PROTECTED 0x00004000
105 
106 /** The integer CoAP content type is larger than UINT16_MAX, larger
107  *  than it is allowed */
108 #define T_COSE_TEST_TOO_LARGE_CONTENT_TYPE 0x00002000
109 
110 /** The protected parameters are not a complete map. Supposed to have
111  *  1 item, but has zero */
112 #define T_COSE_TEST_UNCLOSED_PROTECTED 0x00001000
113 
114 /** The content ID parameter occurs in both protected and unprotected
115  *  bucket */
116 #define T_COSE_TEST_DUP_CONTENT_ID 0x00000800
117 
118 /** The bstr wrapped protected parameters is zero length */
119 #define T_COSE_TEST_EMPTY_PROTECTED_PARAMETERS 0x00000400
120 
121 /** The list of critical labels parameter is empty. This is not
122  * allowed by COSE */
123 #define T_COSE_TEST_EMPTY_CRIT_PARAMETER 0x00000200
124 
125 /** Exceed the limit on number of T_COSE_PARAMETER_LIST_MAX on number
126  * of crit parameters this implementation can handle */
127 #define T_COSE_TEST_TOO_MANY_TSTR_CRIT_LABLELS 0x00000100
128 
129 
130 /**
131  * Replica of t_cose_sign1_sign() with modifications to output various
132  * good and bad messages for testing of t_cose_sign1_verify() .
133  *
134  * \c test_message_options is one of \c T_COSE_TEST_XXX
135  */
136 enum t_cose_err_t
137 t_cose_test_message_sign1_sign(struct t_cose_sign1_sign_ctx *me,
138                                uint32_t                      test_message_options,
139                                struct q_useful_buf_c         payload,
140                                struct q_useful_buf           out_buf,
141                                struct q_useful_buf_c        *result);
142 
143 
144 #ifdef __cplusplus
145 }
146 #endif
147 
148 #endif /* __T_COSE_MAKE_TEST_MESSAGES__ */
149