26
26
#include "suit/v4/handlers.h"
27
27
#include "suit/v4/suit.h"
28
28
#include "suit/v4/policy.h"
29
- #include "cbor .h"
29
+ #include "nanocbor/nanocbor .h"
30
30
#include "cose/sign.h"
31
31
32
32
#include "public_key.h"
40
40
static suit_manifest_handler_t _manifest_get_auth_wrapper_handler (int key );
41
41
typedef suit_manifest_handler_t (* suit_manifest_handler_getter_t )(int key );
42
42
43
- int suit_cbor_map_iterate_init (CborValue * map , CborValue * it )
43
+ int suit_cbor_map_iterate_init (nanocbor_value_t * map , nanocbor_value_t * it )
44
44
{
45
- if (! cbor_value_is_map (map )) {
45
+ if (nanocbor_get_type (map ) != NANOCBOR_TYPE_MAP ) {
46
46
LOG_INFO ("suit_v4_parse(): manifest not a map\n)" );
47
47
return SUIT_ERR_INVALID_MANIFEST ;
48
48
}
49
49
50
- cbor_value_enter_container (map , it );
50
+ nanocbor_enter_map (map , it );
51
51
52
52
return SUIT_OK ;
53
53
}
54
54
55
- int suit_cbor_map_iterate (CborValue * it , CborValue * key , CborValue * value )
55
+ int suit_cbor_map_iterate (nanocbor_value_t * it , nanocbor_value_t * key ,
56
+ nanocbor_value_t * value )
56
57
{
57
- if (cbor_value_at_end (it )) {
58
+ if (nanocbor_at_end (it )) {
58
59
return 0 ;
59
60
}
60
61
61
62
* key = * it ;
62
- cbor_value_advance (it );
63
+ nanocbor_skip (it );
63
64
64
65
* value = * it ;
65
- cbor_value_advance (it );
66
+ nanocbor_skip (it );
66
67
67
68
return 1 ;
68
69
}
69
70
70
- int suit_cbor_get_int ( const CborValue * it , int * out )
71
+ int suit_cbor_get_int32 ( nanocbor_value_t * it , int32_t * out )
71
72
{
72
- if (!cbor_value_is_integer (it )) {
73
- LOG_DEBUG ("expected integer type, got %u\n" , cbor_value_get_type (it ));
74
- return SUIT_ERR_INVALID_MANIFEST ;
75
- }
73
+ int res = nanocbor_get_int32 (it , out );
76
74
77
- /* This check tests whether the integer fits into "int", thus the check
78
- * is platform dependent. This is for lack of specification of actually
79
- * allowed values, to be made explicit at some point. */
80
- if (cbor_value_get_int_checked (it , out ) == CborErrorDataTooLarge ) {
81
- LOG_DEBUG ("integer doesn't fit into int type\n" );
75
+ if (res < NANOCBOR_OK ) {
76
+ LOG_DEBUG ("suit_cbor_get_int32() error %u\n" , res );
82
77
return SUIT_ERR_INVALID_MANIFEST ;
83
78
}
84
79
85
80
return SUIT_OK ;
86
81
}
87
82
88
- int suit_cbor_get_string (const CborValue * it , const uint8_t * * buf , size_t * len )
83
+ int suit_cbor_get_string (nanocbor_value_t * it , const uint8_t * * buf , size_t * len )
89
84
{
90
- if (!(cbor_value_is_text_string (it ) || cbor_value_is_byte_string (it ) || cbor_value_is_length_known (it ))) {
85
+ if (nanocbor_get_type (it ) == NANOCBOR_TYPE_TSTR ) {
86
+ if (nanocbor_get_tstr (it , buf , len ) < 0 ) {
87
+ return SUIT_ERR_INVALID_MANIFEST ;
88
+ }
89
+ return SUIT_OK ;
90
+ }
91
+ else if (nanocbor_get_type (it ) == NANOCBOR_TYPE_BSTR ) {
92
+ if (nanocbor_get_bstr (it , buf , len ) < 0 ) {
93
+ return SUIT_ERR_INVALID_MANIFEST ;
94
+ }
95
+ return SUIT_OK ;
96
+ }
97
+ else {
98
+ LOG_DEBUG ("suit_cbor_get_string(): unexpected type: %i\n" ,
99
+ nanocbor_get_type (it ));
91
100
return SUIT_ERR_INVALID_MANIFEST ;
92
101
}
93
- CborValue next = * it ;
94
- cbor_value_get_string_length (it , len );
95
- cbor_value_advance (& next );
96
- * buf = next .ptr - * len ;
97
- return SUIT_OK ;
98
102
}
99
103
100
- int suit_cbor_get_uint32 (const CborValue * it , uint32_t * out )
104
+ int suit_cbor_get_uint32 (nanocbor_value_t * it , uint32_t * out )
101
105
{
102
- int res ;
103
- int64_t val ;
104
- if (!cbor_value_is_unsigned_integer (it )) {
105
- return CborErrorIllegalType ;
106
- }
107
- if ((res = cbor_value_get_int64_checked (it , & val ))) {
108
- return res ;
109
- }
110
- if (val > 0xFFFFFFFF ) {
111
- return CborErrorDataTooLarge ;
106
+ if (nanocbor_get_uint32 (it , out ) < 0 ) {
107
+ return SUIT_ERR_INVALID_MANIFEST ;
112
108
}
113
- * out = (val & 0xFFFFFFFF );
114
-
115
- return CborNoError ;
109
+ return SUIT_OK ;
116
110
}
117
111
118
- int suit_cbor_get_uint (const CborValue * it , unsigned * out )
112
+ int suit_cbor_get_uint (nanocbor_value_t * it , unsigned * out )
119
113
{
120
114
return suit_cbor_get_uint32 (it , (uint32_t * )out );
121
115
}
122
116
123
- int suit_cbor_subparse (CborParser * parser , CborValue * bseq , CborValue * it )
117
+ int suit_cbor_subparse (nanocbor_value_t * bseq , nanocbor_value_t * it )
124
118
{
125
119
const uint8_t * bytes ;
126
120
size_t bytes_len = 0 ;
127
-
128
- if (!cbor_value_is_byte_string (bseq )) {
129
- LOG_DEBUG ("suit_cbor_subparse(): bseq not a byte string\n" );
130
- return -1 ;
121
+ int res = suit_cbor_get_string (bseq , & bytes , & bytes_len );
122
+ if (res != SUIT_OK ) {
123
+ return res ;
131
124
}
132
-
133
- suit_cbor_get_string (bseq , & bytes , & bytes_len );
134
-
135
- return cbor_parser_init (bytes , bytes_len , SUIT_TINYCBOR_VALIDATION_MODE , parser ,
136
- it );
125
+ nanocbor_decoder_init (it , bytes , bytes_len );
126
+ return SUIT_OK ;
137
127
}
138
128
139
129
static int _v4_parse (suit_v4_manifest_t * manifest , const uint8_t * buf ,
140
- size_t len , suit_manifest_handler_getter_t getter )
130
+ size_t len , suit_manifest_handler_getter_t getter )
141
131
{
132
+ nanocbor_value_t it , map , key , value ;
142
133
143
- CborParser parser ;
144
- CborValue it , map , key , value ;
145
- CborError err = cbor_parser_init (buf , len , SUIT_TINYCBOR_VALIDATION_MODE ,
146
- & parser , & it );
147
-
148
- if (err != 0 ) {
149
- return SUIT_ERR_INVALID_MANIFEST ;
150
- }
134
+ nanocbor_decoder_init (& it , buf , len );
151
135
152
136
map = it ;
153
137
154
138
if (suit_cbor_map_iterate_init (& map , & it ) != SUIT_OK ) {
155
- LOG_DEBUG ("manifest not a map!\n" );
139
+ LOG_DEBUG ("suit _v4_parse(): manifest not a map!\n" );
156
140
return SUIT_ERR_INVALID_MANIFEST ;
157
141
}
158
142
159
- LOG_DEBUG ("jumping into map\n)" );
160
-
161
143
while (suit_cbor_map_iterate (& it , & key , & value )) {
162
- int integer_key ;
163
- if (suit_cbor_get_int (& key , & integer_key ) != SUIT_OK ){
144
+ int32_t integer_key ;
145
+ if (suit_cbor_get_int32 (& key , & integer_key ) != SUIT_OK ) {
164
146
return SUIT_ERR_INVALID_MANIFEST ;
165
147
}
166
- LOG_DEBUG ("got key val=%i \n" , integer_key );
148
+ LOG_DEBUG ("got key val=%" PRIi32 " \n" , integer_key );
167
149
suit_manifest_handler_t handler = getter (integer_key );
168
150
169
151
if (handler ) {
170
152
int res = handler (manifest , integer_key , & value );
171
- LOG_DEBUG ("handler res=%i\n" , res );
172
153
if (res < 0 ) {
173
154
LOG_INFO ("handler returned <0\n)" );
174
155
return SUIT_ERR_INVALID_MANIFEST ;
@@ -179,20 +160,21 @@ static int _v4_parse(suit_v4_manifest_t *manifest, const uint8_t *buf,
179
160
}
180
161
}
181
162
182
- cbor_value_leave_container (& map , & it );
163
+ nanocbor_leave_container (& map , & it );
183
164
184
165
return SUIT_OK ;
185
166
}
186
167
187
168
int suit_v4_parse (suit_v4_manifest_t * manifest , const uint8_t * buf ,
188
- size_t len )
169
+ size_t len )
189
170
{
190
171
manifest -> buf = buf ;
191
172
manifest -> len = len ;
192
173
return _v4_parse (manifest , buf , len , _manifest_get_auth_wrapper_handler );
193
174
}
194
175
195
- static int _auth_handler (suit_v4_manifest_t * manifest , int key , CborValue * it )
176
+ static int _auth_handler (suit_v4_manifest_t * manifest , int key ,
177
+ nanocbor_value_t * it )
196
178
{
197
179
(void )key ;
198
180
const uint8_t * cose_buf ;
@@ -211,7 +193,8 @@ static int _auth_handler(suit_v4_manifest_t *manifest, int key, CborValue *it)
211
193
return 0 ;
212
194
}
213
195
214
- static int _manifest_handler (suit_v4_manifest_t * manifest , int key , CborValue * it )
196
+ static int _manifest_handler (suit_v4_manifest_t * manifest , int key ,
197
+ nanocbor_value_t * it )
215
198
{
216
199
(void )key ;
217
200
const uint8_t * manifest_buf ;
@@ -238,19 +221,20 @@ static int _manifest_handler(suit_v4_manifest_t *manifest, int key, CborValue *i
238
221
239
222
LOG_INFO ("suit: verifying manifest signature...\n" );
240
223
int verification = cose_sign_verify (& manifest -> verify , & signature ,
241
- & pkey , manifest -> validation_buf , SUIT_COSE_BUF_SIZE );
224
+ & pkey , manifest -> validation_buf ,
225
+ SUIT_COSE_BUF_SIZE );
242
226
if (verification != 0 ) {
243
227
LOG_INFO ("Unable to validate signature\n" );
244
228
return SUIT_ERR_SIGNATURE ;
245
229
}
246
230
247
231
return _v4_parse (manifest , manifest_buf ,
248
- manifest_len , suit_manifest_get_manifest_handler );
232
+ manifest_len , suit_manifest_get_manifest_handler );
249
233
}
250
234
251
235
static suit_manifest_handler_t _suit_manifest_get_handler (int key ,
252
- const suit_manifest_handler_t * handlers ,
253
- size_t len )
236
+ const suit_manifest_handler_t * handlers ,
237
+ size_t len )
254
238
{
255
239
if (key < 0 || (size_t )key >= len ) {
256
240
return NULL ;
0 commit comments