Debian Codegraph @

crt/aws-c-cal/tests/der_test.c

raw symbols SCIP
1 /**
2 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3 * SPDX-License-Identifier: Apache-2.0.
4 */
5
6 #include <aws/cal/cal.h>
7 #include <aws/cal/private/der.h>
8
9 #include <aws/testing/aws_test_harness.h>
10
11 #include "test_case_helper.h"
12
13 /* clang-format off */
14 /* note that this int is unsigned, with the high bit set, so needs to be encoded specially */
15 static uint8_t s_bigint[] = {
16 0x8f, 0xe2, 0x41, 0x2a, 0x08, 0xe8, 0x51, 0xa8, 0x8c, 0xb3, 0xe8, 0x53, 0xe7, 0xd5, 0x49, 0x50, 0xb3, 0x27, 0x8a,
17 0x2b, 0xcb, 0xea, 0xb5, 0x42, 0x73, 0xea, 0x02, 0x57, 0xcc, 0x65, 0x33, 0xee, 0x88, 0x20, 0x61, 0xa1, 0x17, 0x56,
18 0xc1, 0x24, 0x18, 0xe3, 0xa8, 0x08, 0xd3, 0xbe, 0xd9, 0x31, 0xf3, 0x37, 0x0b, 0x94, 0xb8, 0xcc, 0x43, 0x08, 0x0b,
19 0x70, 0x24, 0xf7, 0x9c, 0xb1, 0x8d, 0x5d, 0xd6, 0x6d, 0x82, 0xd0, 0x54, 0x09, 0x84, 0xf8, 0x9f, 0x97, 0x01, 0x75,
20 0x05, 0x9c, 0x89, 0xd4, 0xd5, 0xc9, 0x1e, 0xc9, 0x13, 0xd7, 0x2a, 0x6b, 0x30, 0x91, 0x19, 0xd6, 0xd4, 0x42, 0xe0,
21 0xc4, 0x9d, 0x7c, 0x92, 0x71, 0xe1, 0xb2, 0x2f, 0x5c, 0x8d, 0xee, 0xf0, 0xf1, 0x17, 0x1e, 0xd2, 0x5f, 0x31, 0x5b,
22 0xb1, 0x9c, 0xbc, 0x20, 0x55, 0xbf, 0x3a, 0x37, 0x42, 0x45, 0x75, 0xdc, 0x90, 0x65,
23 };
24
25 static uint8_t s_bigint_zero[] = { 0x00 };
26
27 static uint8_t s_encoded_bigint[] = {
28 0x02 /* INTEGER */,
29 0x81 /* 1 byte length */,
30 0x81 /* 0x81 bytes */,
31 0x00 /* unsigned */,
32 0x8f, 0xe2, 0x41, 0x2a, 0x08, 0xe8, 0x51, 0xa8, 0x8c, 0xb3, 0xe8, 0x53, 0xe7, 0xd5, 0x49, 0x50, 0xb3, 0x27, 0x8a,
33 0x2b, 0xcb, 0xea, 0xb5, 0x42, 0x73, 0xea, 0x02, 0x57, 0xcc, 0x65, 0x33, 0xee, 0x88, 0x20, 0x61, 0xa1, 0x17, 0x56,
34 0xc1, 0x24, 0x18, 0xe3, 0xa8, 0x08, 0xd3, 0xbe, 0xd9, 0x31, 0xf3, 0x37, 0x0b, 0x94, 0xb8, 0xcc, 0x43, 0x08, 0x0b,
35 0x70, 0x24, 0xf7, 0x9c, 0xb1, 0x8d, 0x5d, 0xd6, 0x6d, 0x82, 0xd0, 0x54, 0x09, 0x84, 0xf8, 0x9f, 0x97, 0x01, 0x75,
36 0x05, 0x9c, 0x89, 0xd4, 0xd5, 0xc9, 0x1e, 0xc9, 0x13, 0xd7, 0x2a, 0x6b, 0x30, 0x91, 0x19, 0xd6, 0xd4, 0x42, 0xe0,
37 0xc4, 0x9d, 0x7c, 0x92, 0x71, 0xe1, 0xb2, 0x2f, 0x5c, 0x8d, 0xee, 0xf0, 0xf1, 0x17, 0x1e, 0xd2, 0x5f, 0x31, 0x5b,
38 0xb1, 0x9c, 0xbc, 0x20, 0x55, 0xbf, 0x3a, 0x37, 0x42, 0x45, 0x75, 0xdc, 0x90, 0x65,
39 };
40
41 static uint8_t s_encoded_bigint_zero[] = {
42 0x02 /* INTEGER */,
43 0x01 /* 1 byte length */,
44 0x00 /* unsigned */,
45 };
46
47 const uint8_t s_encoded_true[] = {0x01, 0x01, 0xff};
48 const uint8_t s_encoded_false[] = {0x01, 0x01, 0x00};
49
50 const uint8_t s_encoded_null[] = {0x05, 0x00};
51
52 static uint8_t s_bit_string[] = {
53 0x47, 0xeb, 0x99, 0x5a, 0xdf, 0x9e, 0x70, 0x0d, 0xfb, 0xa7, 0x31, 0x32, 0xc1, 0x5f, 0x5c, 0x24,
54 0xc2, 0xe0, 0xbf, 0xc6, 0x24, 0xaf, 0x15, 0x66, 0x0e, 0xb8, 0x6a, 0x2e, 0xab, 0x2b, 0xc4, 0x97,
55 0x1f, 0xe3, 0xcb, 0xdc, 0x63, 0xa5, 0x25, 0xec, 0xc7, 0xb4, 0x28, 0x61, 0x66, 0x36, 0xa1, 0x31,
56 0x1b, 0xbf, 0xdd, 0xd0, 0xfc, 0xbf, 0x17, 0x94, 0x90, 0x1d, 0xe5, 0x5e, 0xc7, 0x11, 0x5e, 0xc9,
57 0x55, 0x9f, 0xeb, 0xa3, 0x3e, 0x14, 0xc7, 0x99, 0xa6, 0xcb, 0xba, 0xa1, 0x46, 0x0f, 0x39, 0xd4,
58 0x44, 0xc4, 0xc8, 0x4b, 0x76, 0x0e, 0x20, 0x5d, 0x6d, 0xa9, 0x34, 0x9e, 0xd4, 0xd5, 0x87, 0x42,
59 0xeb, 0x24, 0x26, 0x51, 0x14, 0x90, 0xb4, 0x0f, 0x06, 0x5e, 0x52, 0x88, 0x32, 0x7a, 0x95, 0x20,
60 0xa0, 0xfd, 0xf7, 0xe5, 0x7d, 0x60, 0xdd, 0x72, 0x68, 0x9b, 0xf5, 0x7b, 0x05, 0x8f, 0x6d, 0x1e,
61 };
62
63 static uint8_t s_encoded_bit_string[] = {
64 0x03, /* BIT_STRING */
65 0x81, /* 1 byte length */
66 0x81, /* 0x81 bytes */
67 0x00, /* 0 trailing unused bits */
68 0x47, 0xeb, 0x99, 0x5a, 0xdf, 0x9e, 0x70, 0x0d, 0xfb, 0xa7, 0x31, 0x32, 0xc1, 0x5f, 0x5c, 0x24,
69 0xc2, 0xe0, 0xbf, 0xc6, 0x24, 0xaf, 0x15, 0x66, 0x0e, 0xb8, 0x6a, 0x2e, 0xab, 0x2b, 0xc4, 0x97,
70 0x1f, 0xe3, 0xcb, 0xdc, 0x63, 0xa5, 0x25, 0xec, 0xc7, 0xb4, 0x28, 0x61, 0x66, 0x36, 0xa1, 0x31,
71 0x1b, 0xbf, 0xdd, 0xd0, 0xfc, 0xbf, 0x17, 0x94, 0x90, 0x1d, 0xe5, 0x5e, 0xc7, 0x11, 0x5e, 0xc9,
72 0x55, 0x9f, 0xeb, 0xa3, 0x3e, 0x14, 0xc7, 0x99, 0xa6, 0xcb, 0xba, 0xa1, 0x46, 0x0f, 0x39, 0xd4,
73 0x44, 0xc4, 0xc8, 0x4b, 0x76, 0x0e, 0x20, 0x5d, 0x6d, 0xa9, 0x34, 0x9e, 0xd4, 0xd5, 0x87, 0x42,
74 0xeb, 0x24, 0x26, 0x51, 0x14, 0x90, 0xb4, 0x0f, 0x06, 0x5e, 0x52, 0x88, 0x32, 0x7a, 0x95, 0x20,
75 0xa0, 0xfd, 0xf7, 0xe5, 0x7d, 0x60, 0xdd, 0x72, 0x68, 0x9b, 0xf5, 0x7b, 0x05, 0x8f, 0x6d, 0x1e,
76 };
77
78 static uint8_t s_octet_string[] = {
79 0x38, 0x10, 0x60, 0xe2, 0x70, 0x69, 0x91, 0x4a,
80 0x8b, 0xb5, 0x22, 0x57, 0x2a, 0x62, 0xef, 0xde,
81 0x15, 0x7d, 0x59, 0xd6, 0x4e, 0x20, 0x9a, 0x45,
82 0x2b, 0xe3, 0xfd, 0xfc, 0x68, 0xba, 0xaf, 0xbf,
83 0x9c, 0x17, 0xb0, 0x8e, 0x6d, 0xc4, 0x29, 0x1e,
84 0xe3, 0x21, 0xac, 0xbb, 0x5a, 0x8a, 0xc9, 0x67,
85 0x0a, 0xd4, 0x45, 0x93, 0x10, 0xc0, 0x26, 0xeb,
86 0x0a, 0x83, 0xc2, 0xb1, 0x40, 0x87, 0x36, 0xf7,
87 0xa0, 0x26, 0xda, 0xb9, 0xbb, 0x46, 0x73, 0x88,
88 0x7a, 0x67, 0xb9, 0xe6, 0xb3, 0x6f, 0xea, 0x59,
89 0x28, 0x8a, 0xd3, 0x92, 0x72, 0xf6, 0x7b, 0x89,
90 0xa0, 0xd8, 0x2d, 0x9e, 0x40, 0xeb, 0x1e, 0xbb,
91 0x6e, 0xae, 0xf0, 0x5a, 0xed, 0x16, 0xc9, 0xe3,
92 0x27, 0x59, 0x37, 0x8f, 0xf3, 0x4a, 0x98, 0x60,
93 0xf8, 0xfb, 0xa7, 0x0a, 0xee, 0x1b, 0x6e, 0x91,
94 0x95, 0x96, 0xcf, 0x0d, 0x56, 0xac, 0xab, 0x35,
95 };
96
97 static uint8_t s_encoded_octet_string[] = {
98 0x04, /* OCTET_STRING */
99 0x81, /* 1 byte length */
100 0x80, /* 0x80 bytes */
101 0x38, 0x10, 0x60, 0xe2, 0x70, 0x69, 0x91, 0x4a,
102 0x8b, 0xb5, 0x22, 0x57, 0x2a, 0x62, 0xef, 0xde,
103 0x15, 0x7d, 0x59, 0xd6, 0x4e, 0x20, 0x9a, 0x45,
104 0x2b, 0xe3, 0xfd, 0xfc, 0x68, 0xba, 0xaf, 0xbf,
105 0x9c, 0x17, 0xb0, 0x8e, 0x6d, 0xc4, 0x29, 0x1e,
106 0xe3, 0x21, 0xac, 0xbb, 0x5a, 0x8a, 0xc9, 0x67,
107 0x0a, 0xd4, 0x45, 0x93, 0x10, 0xc0, 0x26, 0xeb,
108 0x0a, 0x83, 0xc2, 0xb1, 0x40, 0x87, 0x36, 0xf7,
109 0xa0, 0x26, 0xda, 0xb9, 0xbb, 0x46, 0x73, 0x88,
110 0x7a, 0x67, 0xb9, 0xe6, 0xb3, 0x6f, 0xea, 0x59,
111 0x28, 0x8a, 0xd3, 0x92, 0x72, 0xf6, 0x7b, 0x89,
112 0xa0, 0xd8, 0x2d, 0x9e, 0x40, 0xeb, 0x1e, 0xbb,
113 0x6e, 0xae, 0xf0, 0x5a, 0xed, 0x16, 0xc9, 0xe3,
114 0x27, 0x59, 0x37, 0x8f, 0xf3, 0x4a, 0x98, 0x60,
115 0xf8, 0xfb, 0xa7, 0x0a, 0xee, 0x1b, 0x6e, 0x91,
116 0x95, 0x96, 0xcf, 0x0d, 0x56, 0xac, 0xab, 0x35,
117 };
118
119 /* SEQUENCE [BOOLEAN true, BOOLEAN false] */
120 static uint8_t s_encoded_sequence[] = {
121 0x30, /* SEQUENCE */
122 0x06, /* 6 bytes */
123 0x01, 0x01, 0xff, /* BOOLEAN true */
124 0x01, 0x01, 0x00, /* BOOLEAN false */
125 };
126
127 /* SET [BOOLEAN true, BOOLEAN false] */
128 static uint8_t s_encoded_set[] = {
129 0x31, /* SET */
130 0x06, /* 6 bytes */
131 0x01, 0x01, 0xff, /* BOOLEAN true */
132 0x01, 0x01, 0x00, /* BOOLEAN false */
133 0x0a, /* trailing newline */
134 };
135
136 static uint8_t s_encoded_key_pair[] = {
137 0x30, 0x74, /* SEQUENCE, 116 bytes */
138 0x02, 0x01, 0x01, /* INTEGER, 1 byte, value: 1 */
139 0x04, 0x20, /* OCTET_STRING, 32 bytes */
140 0x9d, 0x6d, 0x10, 0x36, 0xbe, 0x66, 0x10, 0xeb, 0x8c, 0x66, 0xe6, 0x39, 0xa3, 0x1e, 0x47, 0xbc,
141 0x46, 0x6f, 0x46, 0x70, 0x59, 0x36, 0x32, 0x84, 0x46, 0x0c, 0x97, 0xb8, 0xda, 0x00, 0x19, 0xe2,
142 0xa0, 0x07, /* context-defined container 0, 7 bytes */
143 0x06, 0x05, 0x2b, 0x81, 0x04, 0x00, 0x0a, /* OID, 5 bytes */
144 0xa1, 0x44, /* context-defined container 1, 68 bytes */
145 0x03, 0x42, /* BIT_STRING, 66 bytes */
146 0x00, 0x04, 0xd1, 0xcf, 0x9c, 0x8a, 0xb4, 0x76, 0x58, 0x70, 0xd9, 0x35, 0x1c, 0xdc, 0x88, 0xbb,
147 0x43, 0x19, 0x77, 0xe4, 0xde, 0xba, 0xda, 0x81, 0x58, 0x54, 0x92, 0x93, 0x8d, 0x85, 0xce, 0xf9,
148 0x04, 0xf3, 0x8e, 0x86, 0x95, 0x46, 0xa3, 0x43, 0xdd, 0x67, 0x8c, 0x8e, 0xb5, 0xf4, 0x33, 0x8e,
149 0x95, 0x4a, 0x93, 0x96, 0xcf, 0xe4, 0x8f, 0x32, 0x78, 0x88, 0xe8, 0x5a, 0xde, 0x59, 0x3f, 0x63,
150 0xaf, 0xf2,
151 0x0a, /* trailing newline */
152 };
153 /* clang-format on */
154
155 static int s_der_encode_integer(struct aws_allocator *allocator, void *ctx) {
156 (void)ctx;
157 aws_cal_library_test_init(allocator);
158 struct aws_der_encoder *encoder = aws_der_encoder_new(allocator, 1024);
159 ASSERT_NOT_NULL(encoder);
160 struct aws_byte_cursor bigint_cur = aws_byte_cursor_from_array(s_bigint, AWS_ARRAY_SIZE(s_bigint));
161 ASSERT_SUCCESS(aws_der_encoder_write_unsigned_integer(encoder, bigint_cur));
162 struct aws_byte_cursor encoded;
163 ASSERT_SUCCESS(aws_der_encoder_get_contents(encoder, &encoded));
164
165 ASSERT_BIN_ARRAYS_EQUALS(s_encoded_bigint, AWS_ARRAY_SIZE(s_encoded_bigint), encoded.ptr, encoded.len);
166 aws_der_encoder_destroy(encoder);
167 aws_cal_library_clean_up();
168 return 0;
169 }
170
171 AWS_TEST_CASE(der_encode_integer, s_der_encode_integer)
172
173 static int s_der_encode_integer_zero(struct aws_allocator *allocator, void *ctx) {
174 (void)ctx;
175 aws_cal_library_test_init(allocator);
176 struct aws_der_encoder *encoder = aws_der_encoder_new(allocator, 1024);
177 ASSERT_NOT_NULL(encoder);
178 struct aws_byte_cursor bigint_cur = aws_byte_cursor_from_array(s_bigint_zero, AWS_ARRAY_SIZE(s_bigint_zero));
179 ASSERT_SUCCESS(aws_der_encoder_write_unsigned_integer(encoder, bigint_cur));
180 struct aws_byte_cursor encoded;
181 ASSERT_SUCCESS(aws_der_encoder_get_contents(encoder, &encoded));
182
183 ASSERT_BIN_ARRAYS_EQUALS(s_encoded_bigint_zero, AWS_ARRAY_SIZE(s_encoded_bigint_zero), encoded.ptr, encoded.len);
184 aws_der_encoder_destroy(encoder);
185 aws_cal_library_clean_up();
186 return 0;
187 }
188
189 AWS_TEST_CASE(der_encode_integer_zero, s_der_encode_integer_zero)
190
191 static int s_der_encode_boolean(struct aws_allocator *allocator, void *ctx) {
192 (void)ctx;
193 aws_cal_library_test_init(allocator);
194 bool flag = true;
195 struct aws_der_encoder *encoder = aws_der_encoder_new(allocator, 1024);
196 ASSERT_NOT_NULL(encoder);
197 ASSERT_SUCCESS(aws_der_encoder_write_boolean(encoder, flag));
198 struct aws_byte_cursor encoded;
199 ASSERT_SUCCESS(aws_der_encoder_get_contents(encoder, &encoded));
200
201 ASSERT_BIN_ARRAYS_EQUALS(s_encoded_true, AWS_ARRAY_SIZE(s_encoded_true), encoded.ptr, encoded.len);
202 aws_der_encoder_destroy(encoder);
203
204 flag = false;
205 encoder = aws_der_encoder_new(allocator, 1024);
206 ASSERT_NOT_NULL(encoder);
207 ASSERT_SUCCESS(aws_der_encoder_write_boolean(encoder, flag));
208 ASSERT_SUCCESS(aws_der_encoder_get_contents(encoder, &encoded));
209 ASSERT_BIN_ARRAYS_EQUALS(s_encoded_false, AWS_ARRAY_SIZE(s_encoded_false), encoded.ptr, encoded.len);
210 aws_der_encoder_destroy(encoder);
211
212 aws_cal_library_clean_up();
213 return 0;
214 }
215
216 AWS_TEST_CASE(der_encode_boolean, s_der_encode_boolean)
217
218 static int s_der_encode_null(struct aws_allocator *allocator, void *ctx) {
219 (void)ctx;
220 aws_cal_library_test_init(allocator);
221 struct aws_der_encoder *encoder = aws_der_encoder_new(allocator, 1024);
222 ASSERT_NOT_NULL(encoder);
223 ASSERT_SUCCESS(aws_der_encoder_write_null(encoder));
224 struct aws_byte_cursor encoded;
225 ASSERT_SUCCESS(aws_der_encoder_get_contents(encoder, &encoded));
226
227 ASSERT_BIN_ARRAYS_EQUALS(s_encoded_null, AWS_ARRAY_SIZE(s_encoded_null), encoded.ptr, encoded.len);
228
229 aws_der_encoder_destroy(encoder);
230 aws_cal_library_clean_up();
231 return 0;
232 }
233
234 AWS_TEST_CASE(der_encode_null, s_der_encode_null)
235
236 static int s_der_encode_bit_string(struct aws_allocator *allocator, void *ctx) {
237 (void)ctx;
238 aws_cal_library_test_init(allocator);
239 struct aws_der_encoder *encoder = aws_der_encoder_new(allocator, 1024);
240 ASSERT_NOT_NULL(encoder);
241 struct aws_byte_cursor bit_string = aws_byte_cursor_from_array(s_bit_string, AWS_ARRAY_SIZE(s_bit_string));
242 ASSERT_SUCCESS(aws_der_encoder_write_bit_string(encoder, bit_string));
243 struct aws_byte_cursor encoded;
244 ASSERT_SUCCESS(aws_der_encoder_get_contents(encoder, &encoded));
245
246 ASSERT_BIN_ARRAYS_EQUALS(s_encoded_bit_string, AWS_ARRAY_SIZE(s_encoded_bit_string), encoded.ptr, encoded.len);
247 aws_der_encoder_destroy(encoder);
248 aws_cal_library_clean_up();
249 return 0;
250 }
251
252 AWS_TEST_CASE(der_encode_bit_string, s_der_encode_bit_string)
253
254 static int s_der_encode_octet_string(struct aws_allocator *allocator, void *ctx) {
255 (void)ctx;
256 aws_cal_library_test_init(allocator);
257 struct aws_der_encoder *encoder = aws_der_encoder_new(allocator, 1024);
258 ASSERT_NOT_NULL(encoder);
259 struct aws_byte_cursor octet_string = aws_byte_cursor_from_array(s_octet_string, AWS_ARRAY_SIZE(s_octet_string));
260 ASSERT_SUCCESS(aws_der_encoder_write_octet_string(encoder, octet_string));
261 struct aws_byte_cursor encoded;
262 ASSERT_SUCCESS(aws_der_encoder_get_contents(encoder, &encoded));
263
264 ASSERT_BIN_ARRAYS_EQUALS(s_encoded_octet_string, AWS_ARRAY_SIZE(s_encoded_octet_string), encoded.ptr, encoded.len);
265 aws_der_encoder_destroy(encoder);
266 aws_cal_library_clean_up();
267 return 0;
268 }
269
270 AWS_TEST_CASE(der_encode_octet_string, s_der_encode_octet_string)
271
272 static int s_der_encode_sequence(struct aws_allocator *allocator, void *ctx) {
273 (void)ctx;
274 aws_cal_library_test_init(allocator);
275 struct aws_der_encoder *encoder = aws_der_encoder_new(allocator, 1024);
276 ASSERT_NOT_NULL(encoder);
277 ASSERT_SUCCESS(aws_der_encoder_begin_sequence(encoder));
278 ASSERT_SUCCESS(aws_der_encoder_write_boolean(encoder, true));
279 ASSERT_SUCCESS(aws_der_encoder_write_boolean(encoder, false));
280 ASSERT_SUCCESS(aws_der_encoder_end_sequence(encoder));
281 struct aws_byte_cursor encoded;
282 ASSERT_SUCCESS(aws_der_encoder_get_contents(encoder, &encoded));
283
284 ASSERT_BIN_ARRAYS_EQUALS(s_encoded_sequence, AWS_ARRAY_SIZE(s_encoded_sequence), encoded.ptr, encoded.len);
285 aws_der_encoder_destroy(encoder);
286 aws_cal_library_clean_up();
287 return 0;
288 }
289
290 AWS_TEST_CASE(der_encode_sequence, s_der_encode_sequence)
291
292 static int s_der_encode_set(struct aws_allocator *allocator, void *ctx) {
293 (void)ctx;
294 aws_cal_library_test_init(allocator);
295 struct aws_der_encoder *encoder = aws_der_encoder_new(allocator, 1024);
296 ASSERT_NOT_NULL(encoder);
297 ASSERT_SUCCESS(aws_der_encoder_begin_set(encoder));
298 ASSERT_SUCCESS(aws_der_encoder_write_boolean(encoder, true));
299 ASSERT_SUCCESS(aws_der_encoder_write_boolean(encoder, false));
300 ASSERT_SUCCESS(aws_der_encoder_end_set(encoder));
301 struct aws_byte_cursor encoded;
302 ASSERT_SUCCESS(aws_der_encoder_get_contents(encoder, &encoded));
303
304 ASSERT_BIN_ARRAYS_EQUALS(s_encoded_set, AWS_ARRAY_SIZE(s_encoded_set) - 1, encoded.ptr, encoded.len);
305 aws_der_encoder_destroy(encoder);
306 aws_cal_library_clean_up();
307 return 0;
308 }
309
310 AWS_TEST_CASE(der_encode_set, s_der_encode_set)
311
312 static int s_der_decode_integer(struct aws_allocator *allocator, void *ctx) {
313 (void)ctx;
314 aws_cal_library_test_init(allocator);
315 const size_t encoded_size = AWS_ARRAY_SIZE(s_encoded_bigint);
316 const size_t decoded_size = AWS_ARRAY_SIZE(s_bigint);
317 struct aws_byte_cursor input = aws_byte_cursor_from_array(s_encoded_bigint, encoded_size);
318 struct aws_der_decoder *decoder = aws_der_decoder_new(allocator, input);
319 ASSERT_NOT_NULL(decoder);
320 ASSERT_TRUE(aws_der_decoder_next(decoder));
321
322 ASSERT_INT_EQUALS(AWS_DER_INTEGER, aws_der_decoder_tlv_type(decoder));
323 ASSERT_INT_EQUALS(decoded_size, aws_der_decoder_tlv_length(decoder));
324 struct aws_byte_cursor decoded;
325 ASSERT_SUCCESS(aws_der_decoder_tlv_unsigned_integer(decoder, &decoded));
326 ASSERT_BIN_ARRAYS_EQUALS(s_bigint, decoded_size, decoded.ptr, decoded.len);
327 ASSERT_FALSE(aws_der_decoder_next(decoder));
328 aws_der_decoder_destroy(decoder);
329
330 aws_cal_library_clean_up();
331 return 0;
332 }
333
334 AWS_TEST_CASE(der_decode_integer, s_der_decode_integer)
335
336 static int s_der_decode_integer_zero(struct aws_allocator *allocator, void *ctx) {
337 (void)ctx;
338 aws_cal_library_test_init(allocator);
339 const size_t encoded_size = AWS_ARRAY_SIZE(s_encoded_bigint_zero);
340 const size_t decoded_size = AWS_ARRAY_SIZE(s_bigint_zero);
341 struct aws_byte_cursor input = aws_byte_cursor_from_array(s_encoded_bigint_zero, encoded_size);
342 struct aws_der_decoder *decoder = aws_der_decoder_new(allocator, input);
343 ASSERT_NOT_NULL(decoder);
344 ASSERT_TRUE(aws_der_decoder_next(decoder));
345
346 ASSERT_INT_EQUALS(AWS_DER_INTEGER, aws_der_decoder_tlv_type(decoder));
347 ASSERT_INT_EQUALS(decoded_size, aws_der_decoder_tlv_length(decoder));
348 struct aws_byte_cursor decoded;
349 ASSERT_SUCCESS(aws_der_decoder_tlv_unsigned_integer(decoder, &decoded));
350 ASSERT_BIN_ARRAYS_EQUALS(s_bigint_zero, decoded_size, decoded.ptr, decoded.len);
351 ASSERT_FALSE(aws_der_decoder_next(decoder));
352 aws_der_decoder_destroy(decoder);
353
354 aws_cal_library_clean_up();
355 return 0;
356 }
357
358 AWS_TEST_CASE(der_decode_integer_zero, s_der_decode_integer_zero)
359
360 static int s_der_decode_boolean(struct aws_allocator *allocator, void *ctx) {
361 (void)ctx;
362 aws_cal_library_test_init(allocator);
363 bool flag = false;
364 const size_t encoded_size = AWS_ARRAY_SIZE(s_encoded_true);
365 struct aws_byte_cursor input = aws_byte_cursor_from_array(s_encoded_true, encoded_size);
366 struct aws_der_decoder *decoder = aws_der_decoder_new(allocator, input);
367 ASSERT_NOT_NULL(decoder);
368
369 ASSERT_TRUE(aws_der_decoder_next(decoder));
370 ASSERT_INT_EQUALS(AWS_DER_BOOLEAN, aws_der_decoder_tlv_type(decoder));
371 ASSERT_INT_EQUALS(1, aws_der_decoder_tlv_length(decoder));
372 ASSERT_SUCCESS(aws_der_decoder_tlv_boolean(decoder, &flag));
373 ASSERT_TRUE(flag);
374 ASSERT_FALSE(aws_der_decoder_next(decoder));
375 aws_der_decoder_destroy(decoder);
376
377 input = aws_byte_cursor_from_array(s_encoded_false, encoded_size);
378 decoder = aws_der_decoder_new(allocator, input);
379 ASSERT_NOT_NULL(decoder);
380 ASSERT_TRUE(aws_der_decoder_next(decoder));
381 ASSERT_INT_EQUALS(AWS_DER_BOOLEAN, aws_der_decoder_tlv_type(decoder));
382 ASSERT_INT_EQUALS(1, aws_der_decoder_tlv_length(decoder));
383 ASSERT_SUCCESS(aws_der_decoder_tlv_boolean(decoder, &flag));
384 ASSERT_FALSE(flag);
385 ASSERT_FALSE(aws_der_decoder_next(decoder));
386 aws_der_decoder_destroy(decoder);
387 aws_cal_library_clean_up();
388 return 0;
389 }
390
391 AWS_TEST_CASE(der_decode_boolean, s_der_decode_boolean)
392
393 static int s_der_decode_null(struct aws_allocator *allocator, void *ctx) {
394 (void)ctx;
395 aws_cal_library_test_init(allocator);
396 struct aws_byte_cursor input = aws_byte_cursor_from_array(s_encoded_null, AWS_ARRAY_SIZE(s_encoded_null));
397 struct aws_der_decoder *decoder = aws_der_decoder_new(allocator, input);
398 ASSERT_NOT_NULL(decoder);
399 ASSERT_TRUE(aws_der_decoder_next(decoder));
400 ASSERT_INT_EQUALS(AWS_DER_NULL, aws_der_decoder_tlv_type(decoder));
401 ASSERT_INT_EQUALS(0, aws_der_decoder_tlv_length(decoder));
402 ASSERT_FALSE(aws_der_decoder_next(decoder));
403 aws_der_decoder_destroy(decoder);
404 aws_cal_library_clean_up();
405 return 0;
406 }
407
408 AWS_TEST_CASE(der_decode_null, s_der_decode_null)
409
410 static int s_der_decode_bit_string(struct aws_allocator *allocator, void *ctx) {
411 (void)ctx;
412 aws_cal_library_test_init(allocator);
413 const size_t encoded_size = AWS_ARRAY_SIZE(s_encoded_bit_string);
414 const size_t decoded_size = AWS_ARRAY_SIZE(s_bit_string);
415 struct aws_byte_cursor input = aws_byte_cursor_from_array(s_encoded_bit_string, encoded_size);
416 struct aws_der_decoder *decoder = aws_der_decoder_new(allocator, input);
417 ASSERT_NOT_NULL(decoder);
418 ASSERT_TRUE(aws_der_decoder_next(decoder));
419
420 ASSERT_INT_EQUALS(AWS_DER_BIT_STRING, aws_der_decoder_tlv_type(decoder));
421 ASSERT_INT_EQUALS(decoded_size, aws_der_decoder_tlv_length(decoder));
422 struct aws_byte_cursor decoded;
423 ASSERT_SUCCESS(aws_der_decoder_tlv_string(decoder, &decoded));
424 ASSERT_BIN_ARRAYS_EQUALS(s_bit_string, decoded_size, decoded.ptr, decoded.len);
425 ASSERT_FALSE(aws_der_decoder_next(decoder));
426 aws_der_decoder_destroy(decoder);
427 aws_cal_library_clean_up();
428 return 0;
429 }
430
431 AWS_TEST_CASE(der_decode_bit_string, s_der_decode_bit_string)
432
433 static int s_der_decode_octet_string(struct aws_allocator *allocator, void *ctx) {
434 (void)ctx;
435 aws_cal_library_test_init(allocator);
436 const size_t encoded_size = AWS_ARRAY_SIZE(s_encoded_octet_string);
437 const size_t decoded_size = AWS_ARRAY_SIZE(s_bit_string);
438 struct aws_byte_cursor input = aws_byte_cursor_from_array(s_encoded_octet_string, encoded_size);
439 struct aws_der_decoder *decoder = aws_der_decoder_new(allocator, input);
440 ASSERT_NOT_NULL(decoder);
441 ASSERT_TRUE(aws_der_decoder_next(decoder));
442
443 ASSERT_INT_EQUALS(AWS_DER_OCTET_STRING, aws_der_decoder_tlv_type(decoder));
444 ASSERT_INT_EQUALS(decoded_size, aws_der_decoder_tlv_length(decoder));
445 struct aws_byte_cursor decoded;
446 ASSERT_SUCCESS(aws_der_decoder_tlv_string(decoder, &decoded));
447 ASSERT_BIN_ARRAYS_EQUALS(s_octet_string, decoded_size, decoded.ptr, decoded.len);
448 ASSERT_FALSE(aws_der_decoder_next(decoder));
449 aws_der_decoder_destroy(decoder);
450 aws_cal_library_clean_up();
451 return 0;
452 }
453
454 AWS_TEST_CASE(der_decode_octet_string, s_der_decode_octet_string)
455
456 static int s_der_decode_sequence(struct aws_allocator *allocator, void *ctx) {
457 (void)ctx;
458 aws_cal_library_test_init(allocator);
459 const size_t encoded_size = AWS_ARRAY_SIZE(s_encoded_sequence);
460 const size_t decoded_size = AWS_ARRAY_SIZE(s_encoded_true) + AWS_ARRAY_SIZE(s_encoded_false);
461 struct aws_byte_cursor input = aws_byte_cursor_from_array(s_encoded_sequence, encoded_size);
462 struct aws_der_decoder *decoder = aws_der_decoder_new(allocator, input);
463 ASSERT_NOT_NULL(decoder);
464
465 /* Verify SEQUENCE */
466 ASSERT_TRUE(aws_der_decoder_next(decoder));
467 ASSERT_INT_EQUALS(AWS_DER_SEQUENCE, aws_der_decoder_tlv_type(decoder));
468 ASSERT_INT_EQUALS(decoded_size, aws_der_decoder_tlv_length(decoder));
469 ASSERT_INT_EQUALS(2, aws_der_decoder_tlv_count(decoder));
470
471 /* Verify true, then false */
472 bool decoded_flag = false;
473 ASSERT_TRUE(aws_der_decoder_next(decoder));
474 ASSERT_INT_EQUALS(AWS_DER_BOOLEAN, aws_der_decoder_tlv_type(decoder));
475 ASSERT_INT_EQUALS(1, aws_der_decoder_tlv_length(decoder));
476 ASSERT_SUCCESS(aws_der_decoder_tlv_boolean(decoder, &decoded_flag));
477 ASSERT_TRUE(decoded_flag);
478
479 ASSERT_TRUE(aws_der_decoder_next(decoder));
480 ASSERT_INT_EQUALS(AWS_DER_BOOLEAN, aws_der_decoder_tlv_type(decoder));
481 ASSERT_INT_EQUALS(1, aws_der_decoder_tlv_length(decoder));
482 ASSERT_SUCCESS(aws_der_decoder_tlv_boolean(decoder, &decoded_flag));
483 ASSERT_FALSE(decoded_flag);
484
485 ASSERT_FALSE(aws_der_decoder_next(decoder));
486 aws_der_decoder_destroy(decoder);
487 aws_cal_library_clean_up();
488 return 0;
489 }
490
491 AWS_TEST_CASE(der_decode_sequence, s_der_decode_sequence)
492
493 static int s_der_decode_set(struct aws_allocator *allocator, void *ctx) {
494 (void)ctx;
495 aws_cal_library_test_init(allocator);
496 const size_t encoded_size = AWS_ARRAY_SIZE(s_encoded_set);
497 const size_t decoded_size = AWS_ARRAY_SIZE(s_encoded_true) + AWS_ARRAY_SIZE(s_encoded_false);
498 struct aws_byte_cursor input = aws_byte_cursor_from_array(s_encoded_set, encoded_size);
499 struct aws_der_decoder *decoder = aws_der_decoder_new(allocator, input);
500 ASSERT_NOT_NULL(decoder);
501
502 /* Verify SET */
503 ASSERT_TRUE(aws_der_decoder_next(decoder));
504 ASSERT_INT_EQUALS(AWS_DER_SET, aws_der_decoder_tlv_type(decoder));
505 ASSERT_INT_EQUALS(decoded_size, aws_der_decoder_tlv_length(decoder));
506 ASSERT_INT_EQUALS(2, aws_der_decoder_tlv_count(decoder));
507
508 /* Verify true, then false */
509 bool decoded_flag = false;
510 ASSERT_TRUE(aws_der_decoder_next(decoder));
511 ASSERT_INT_EQUALS(AWS_DER_BOOLEAN, aws_der_decoder_tlv_type(decoder));
512 ASSERT_INT_EQUALS(1, aws_der_decoder_tlv_length(decoder));
513 ASSERT_SUCCESS(aws_der_decoder_tlv_boolean(decoder, &decoded_flag));
514 ASSERT_TRUE(decoded_flag);
515
516 ASSERT_TRUE(aws_der_decoder_next(decoder));
517 ASSERT_INT_EQUALS(AWS_DER_BOOLEAN, aws_der_decoder_tlv_type(decoder));
518 ASSERT_INT_EQUALS(1, aws_der_decoder_tlv_length(decoder));
519 ASSERT_SUCCESS(aws_der_decoder_tlv_boolean(decoder, &decoded_flag));
520 ASSERT_FALSE(decoded_flag);
521
522 ASSERT_FALSE(aws_der_decoder_next(decoder));
523 aws_der_decoder_destroy(decoder);
524 aws_cal_library_clean_up();
525 return 0;
526 }
527
528 AWS_TEST_CASE(der_decode_set, s_der_decode_set)
529
530 static int s_der_decode_key_pair(struct aws_allocator *allocator, void *ctx) {
531 (void)ctx;
532 aws_cal_library_test_init(allocator);
533 const size_t encoded_size = AWS_ARRAY_SIZE(s_encoded_key_pair);
534 struct aws_byte_cursor input = aws_byte_cursor_from_array(s_encoded_key_pair, encoded_size);
535 struct aws_der_decoder *decoder = aws_der_decoder_new(allocator, input);
536 ASSERT_NOT_NULL(decoder);
537
538 /* SEQUENCE */
539 ASSERT_TRUE(aws_der_decoder_next(decoder));
540 ASSERT_INT_EQUALS(AWS_DER_SEQUENCE, aws_der_decoder_tlv_type(decoder));
541 ASSERT_INT_EQUALS(4, aws_der_decoder_tlv_count(decoder));
542
543 /* INTEGER 1 */
544 struct aws_byte_cursor integer;
545 ASSERT_TRUE(aws_der_decoder_next(decoder));
546 ASSERT_INT_EQUALS(AWS_DER_INTEGER, aws_der_decoder_tlv_type(decoder));
547 ASSERT_SUCCESS(aws_der_decoder_tlv_unsigned_integer(decoder, &integer));
548 ASSERT_BIN_ARRAYS_EQUALS("\x01", 1, integer.ptr, integer.len);
549
550 /* 32 byte private key */
551 struct aws_byte_cursor private_key;
552 ASSERT_TRUE(aws_der_decoder_next(decoder));
553 ASSERT_INT_EQUALS(AWS_DER_OCTET_STRING, aws_der_decoder_tlv_type(decoder));
554 ASSERT_SUCCESS(aws_der_decoder_tlv_string(decoder, &private_key));
555 ASSERT_INT_EQUALS(32, private_key.len);
556
557 /* container */
558 ASSERT_TRUE(aws_der_decoder_next(decoder));
559 ASSERT_TRUE(aws_der_decoder_tlv_type(decoder) == (AWS_DER_CLASS_CONTEXT | AWS_DER_FORM_CONSTRUCTED));
560 ASSERT_INT_EQUALS(7, aws_der_decoder_tlv_length(decoder));
561 ASSERT_INT_EQUALS(1, aws_der_decoder_tlv_count(decoder));
562
563 /* 5 byte OID */
564 struct aws_byte_cursor oid;
565 ASSERT_TRUE(aws_der_decoder_next(decoder));
566 ASSERT_INT_EQUALS(AWS_DER_OBJECT_IDENTIFIER, aws_der_decoder_tlv_type(decoder));
567 ASSERT_INT_EQUALS(5, aws_der_decoder_tlv_length(decoder));
568 ASSERT_SUCCESS(aws_der_decoder_tlv_blob(decoder, &oid));
569 ASSERT_BIN_ARRAYS_EQUALS("\x2b\x81\x04\x00\x0a", 5, oid.ptr, oid.len);
570
571 /* container */
572 ASSERT_TRUE(aws_der_decoder_next(decoder));
573 ASSERT_TRUE(aws_der_decoder_tlv_type(decoder) == (AWS_DER_CLASS_CONTEXT | AWS_DER_FORM_CONSTRUCTED | 1));
574 ASSERT_INT_EQUALS(68, aws_der_decoder_tlv_length(decoder));
575 ASSERT_INT_EQUALS(1, aws_der_decoder_tlv_count(decoder));
576
577 /* 64 byte public key */
578 struct aws_byte_cursor public_key;
579 ASSERT_TRUE(aws_der_decoder_next(decoder));
580 ASSERT_INT_EQUALS(AWS_DER_BIT_STRING, aws_der_decoder_tlv_type(decoder));
581 ASSERT_SUCCESS(aws_der_decoder_tlv_string(decoder, &public_key));
582 ASSERT_INT_EQUALS(65, public_key.len);
583
584 ASSERT_FALSE(aws_der_decoder_next(decoder));
585 aws_der_decoder_destroy(decoder);
586 aws_cal_library_clean_up();
587 return 0;
588 }
589
590 AWS_TEST_CASE(der_decode_key_pair, s_der_decode_key_pair)
591
592 static int s_der_decode_negative_int(struct aws_allocator *allocator, void *ctx) {
593 (void)ctx;
594 aws_cal_library_test_init(allocator);
595
596 uint8_t negative_der[] = {0x02 /*int*/, 0x01 /*len 1*/, 0xfd /*-3*/};
597
598 const size_t encoded_size = AWS_ARRAY_SIZE(negative_der);
599 struct aws_byte_cursor input = aws_byte_cursor_from_array(negative_der, encoded_size);
600 struct aws_der_decoder *decoder = aws_der_decoder_new(allocator, input);
601 ASSERT_NULL(decoder);
602
603 ASSERT_INT_EQUALS(AWS_ERROR_CAL_DER_UNSUPPORTED_NEGATIVE_INT, aws_last_error());
604 aws_der_decoder_destroy(decoder);
605
606 aws_cal_library_clean_up();
607 return 0;
608 }
609 AWS_TEST_CASE(der_decode_negative_int, s_der_decode_negative_int)
610
611 static int s_der_decode_positive_int(struct aws_allocator *allocator, void *ctx) {
612 (void)ctx;
613 aws_cal_library_test_init(allocator);
614
615 uint8_t negative_der[] = {0x02 /*int*/, 0x02 /*len 2*/, 0x00, 0xfd /*253*/};
616
617 const size_t encoded_size = AWS_ARRAY_SIZE(negative_der);
618 struct aws_byte_cursor input = aws_byte_cursor_from_array(negative_der, encoded_size);
619 struct aws_der_decoder *decoder = aws_der_decoder_new(allocator, input);
620 ASSERT_NOT_NULL(decoder);
621
622 ASSERT_TRUE(aws_der_decoder_next(decoder));
623 struct aws_byte_cursor cur;
624 ASSERT_SUCCESS(aws_der_decoder_tlv_unsigned_integer(decoder, &cur));
625 aws_der_decoder_destroy(decoder);
626
627 aws_cal_library_clean_up();
628 return 0;
629 }
630 AWS_TEST_CASE(der_decode_positive_int, s_der_decode_positive_int)
631
632 static int s_der_decode_zero_int(struct aws_allocator *allocator, void *ctx) {
633 (void)ctx;
634 aws_cal_library_test_init(allocator);
635
636 uint8_t negative_der[] = {0x02 /*int*/, 0x01 /*len 2*/, 0x00 /*0*/};
637
638 const size_t encoded_size = AWS_ARRAY_SIZE(negative_der);
639 struct aws_byte_cursor input = aws_byte_cursor_from_array(negative_der, encoded_size);
640 struct aws_der_decoder *decoder = aws_der_decoder_new(allocator, input);
641 ASSERT_NOT_NULL(decoder);
642
643 ASSERT_TRUE(aws_der_decoder_next(decoder));
644 struct aws_byte_cursor cur;
645 ASSERT_SUCCESS(aws_der_decoder_tlv_unsigned_integer(decoder, &cur));
646 aws_der_decoder_destroy(decoder);
647
648 aws_cal_library_clean_up();
649 return 0;
650 }
651 AWS_TEST_CASE(der_decode_zero_int, s_der_decode_zero_int)
652
653 static int s_der_decode_bad_length(struct aws_allocator *allocator, void *ctx) {
654 (void)ctx;
655 aws_cal_library_test_init(allocator);
656
657 uint8_t negative_der[] = {0x02 /*int*/, 0x09 /*len 9*/, 0x00 /*0*/};
658
659 const size_t encoded_size = AWS_ARRAY_SIZE(negative_der);
660 struct aws_byte_cursor input = aws_byte_cursor_from_array(negative_der, encoded_size);
661 struct aws_der_decoder *decoder = aws_der_decoder_new(allocator, input);
662 ASSERT_NULL(decoder);
663
664 ASSERT_INT_EQUALS(AWS_ERROR_CAL_MALFORMED_ASN1_ENCOUNTERED, aws_last_error());
665 aws_der_decoder_destroy(decoder);
666
667 aws_cal_library_clean_up();
668 return 0;
669 }
670 AWS_TEST_CASE(der_decode_bad_length, s_der_decode_bad_length)
671
672 static int s_der_decode_zero_length_int(struct aws_allocator *allocator, void *ctx) {
673 (void)ctx;
674 aws_cal_library_test_init(allocator);
675
676 uint8_t negative_der[] = {0x02 /*int*/, 0x00 /*len 9*/, 0x00 /*0*/};
677
678 const size_t encoded_size = AWS_ARRAY_SIZE(negative_der);
679 struct aws_byte_cursor input = aws_byte_cursor_from_array(negative_der, encoded_size);
680 struct aws_der_decoder *decoder = aws_der_decoder_new(allocator, input);
681 ASSERT_NULL(decoder);
682
683 ASSERT_INT_EQUALS(AWS_ERROR_CAL_MALFORMED_ASN1_ENCOUNTERED, aws_last_error());
684 aws_der_decoder_destroy(decoder);
685
686 aws_cal_library_clean_up();
687 return 0;
688 }
689 AWS_TEST_CASE(der_decode_zero_length_int, s_der_decode_zero_length_int)
690
691 static int s_der_decode_zero_length_bit_string(struct aws_allocator *allocator, void *ctx) {
692 (void)ctx;
693 aws_cal_library_test_init(allocator);
694
695 uint8_t zero_bitstring_der[] = {0x03 /*int*/, 0x00 /*len 0*/};
696
697 const size_t encoded_size = AWS_ARRAY_SIZE(zero_bitstring_der);
698 struct aws_byte_cursor input = aws_byte_cursor_from_array(zero_bitstring_der, encoded_size);
699 struct aws_der_decoder *decoder = aws_der_decoder_new(allocator, input);
700 ASSERT_NULL(decoder);
701
702 ASSERT_INT_EQUALS(AWS_ERROR_CAL_MALFORMED_ASN1_ENCOUNTERED, aws_last_error());
703
704 aws_der_decoder_destroy(decoder);
705
706 aws_cal_library_clean_up();
707 return 0;
708 }
709 AWS_TEST_CASE(der_decode_zero_length_bit_string, s_der_decode_zero_length_bit_string)
710
711 static int s_der_decode_empty_bit_string(struct aws_allocator *allocator, void *ctx) {
712 (void)ctx;
713 aws_cal_library_test_init(allocator);
714
715 uint8_t zero_bitstring_der[] = {0x03 /*int*/, 0x01 /*len 1*/, 0x00};
716
717 const size_t encoded_size = AWS_ARRAY_SIZE(zero_bitstring_der);
718 struct aws_byte_cursor input = aws_byte_cursor_from_array(zero_bitstring_der, encoded_size);
719 struct aws_der_decoder *decoder = aws_der_decoder_new(allocator, input);
720 ASSERT_NOT_NULL(decoder);
721
722 ASSERT_TRUE(aws_der_decoder_next(decoder));
723
724 ASSERT_INT_EQUALS(AWS_DER_BIT_STRING, aws_der_decoder_tlv_type(decoder));
725
726 struct aws_byte_cursor cur = {0};
727 ASSERT_SUCCESS(aws_der_decoder_tlv_string(decoder, &cur));
728
729 ASSERT_INT_EQUALS(0, cur.len);
730
731 aws_der_decoder_destroy(decoder);
732
733 aws_cal_library_clean_up();
734 return 0;
735 }
736 AWS_TEST_CASE(der_decode_empty_bit_string, s_der_decode_empty_bit_string)
737
738 static int s_der_decode_empty_bit_string_bad_padding(struct aws_allocator *allocator, void *ctx) {
739 (void)ctx;
740 aws_cal_library_test_init(allocator);
741
742 uint8_t zero_bitstring_der[] = {0x03 /*int*/, 0x01 /*len 1*/, 0x03};
743
744 const size_t encoded_size = AWS_ARRAY_SIZE(zero_bitstring_der);
745 struct aws_byte_cursor input = aws_byte_cursor_from_array(zero_bitstring_der, encoded_size);
746 struct aws_der_decoder *decoder = aws_der_decoder_new(allocator, input);
747 ASSERT_NULL(decoder);
748
749 ASSERT_INT_EQUALS(AWS_ERROR_CAL_MALFORMED_ASN1_ENCOUNTERED, aws_last_error());
750
751 aws_der_decoder_destroy(decoder);
752
753 aws_cal_library_clean_up();
754 return 0;
755 }
756 AWS_TEST_CASE(der_decode_empty_bit_string_bad_padding, s_der_decode_empty_bit_string_bad_padding)
757
758 static int s_der_roundtrip_context_specific_tags(struct aws_allocator *allocator, void *ctx) {
759 (void)ctx;
760 aws_cal_library_test_init(allocator);
761
762 struct aws_der_encoder *encoder = aws_der_encoder_new(allocator, 256);
763
764 uint8_t int_value[] = {59, 3, 71};
765 struct aws_byte_cursor int_cur = aws_byte_cursor_from_array(int_value, 3);
766
767 ASSERT_SUCCESS(aws_der_encoder_begin_sequence(encoder));
768 ASSERT_SUCCESS(aws_der_encoder_begin_context_aware_tag(encoder, true, 1));
769 ASSERT_SUCCESS(aws_der_encoder_begin_sequence(encoder));
770 ASSERT_SUCCESS(aws_der_encoder_write_unsigned_integer(encoder, int_cur));
771 ASSERT_SUCCESS(aws_der_encoder_end_sequence(encoder));
772 ASSERT_SUCCESS(aws_der_encoder_end_context_aware_tag(encoder));
773 ASSERT_SUCCESS(aws_der_encoder_end_sequence(encoder));
774
775 struct aws_byte_cursor contents;
776 AWS_ZERO_STRUCT(contents);
777 ASSERT_SUCCESS(aws_der_encoder_get_contents(encoder, &contents));
778
779 struct aws_der_decoder *decoder = aws_der_decoder_new(allocator, contents);
780
781 ASSERT_TRUE(aws_der_decoder_next(decoder));
782 ASSERT_INT_EQUALS(aws_der_decoder_tlv_type(decoder), AWS_DER_SEQUENCE);
783
784 ASSERT_TRUE(aws_der_decoder_next(decoder));
785 ASSERT_INT_EQUALS(aws_der_decoder_tlv_type(decoder), AWS_DER_CLASS_CONTEXT | AWS_DER_FORM_CONSTRUCTED | 1);
786
787 ASSERT_TRUE(aws_der_decoder_next(decoder));
788 ASSERT_INT_EQUALS(aws_der_decoder_tlv_type(decoder), AWS_DER_SEQUENCE);
789
790 ASSERT_TRUE(aws_der_decoder_next(decoder));
791 ASSERT_INT_EQUALS(aws_der_decoder_tlv_type(decoder), AWS_DER_INTEGER);
792
793 struct aws_byte_cursor actual_cur;
794 AWS_ZERO_STRUCT(actual_cur);
795 ASSERT_SUCCESS(aws_der_decoder_tlv_unsigned_integer(decoder, &actual_cur));
796 ASSERT_BIN_ARRAYS_EQUALS(int_cur.ptr, int_cur.len, actual_cur.ptr, actual_cur.len);
797
798 aws_der_encoder_destroy(encoder);
799 aws_der_decoder_destroy(decoder);
800
801 aws_cal_library_clean_up();
802 return 0;
803 }
804 AWS_TEST_CASE(der_roundtrip_context_specific_tags, s_der_roundtrip_context_specific_tags)