summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authortb <>2026-01-04 09:36:34 +0000
committertb <>2026-01-04 09:36:34 +0000
commit92a00177b1792955d45f8653ea60eb986b21c90a (patch)
tree845f7b9513bddc63b0b8e53e189d1f3a2d9dfca9 /src
parentefbc823179eebc9ec3fa32135df0af814522aef6 (diff)
downloadopenbsd-92a00177b1792955d45f8653ea60eb986b21c90a.tar.gz
openbsd-92a00177b1792955d45f8653ea60eb986b21c90a.tar.bz2
openbsd-92a00177b1792955d45f8653ea60eb986b21c90a.zip
asn1basic: add example showing current bogus encoding
There is a bug in i2c_ASN1_BIT_STRING() resulting in nonsense encoding of some BIT STRINGs with trailing zeroes if ASN1_STRING_FLAG_BITS_LEFT is not set (a rare corner case). This test currently passes when it shouldn't.
Diffstat (limited to 'src')
-rw-r--r--src/regress/lib/libcrypto/asn1/asn1basic.c39
1 files changed, 38 insertions, 1 deletions
diff --git a/src/regress/lib/libcrypto/asn1/asn1basic.c b/src/regress/lib/libcrypto/asn1/asn1basic.c
index f900e45be5..7c4e329e4b 100644
--- a/src/regress/lib/libcrypto/asn1/asn1basic.c
+++ b/src/regress/lib/libcrypto/asn1/asn1basic.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: asn1basic.c,v 1.17 2025/12/18 09:15:28 tb Exp $ */ 1/* $OpenBSD: asn1basic.c,v 1.18 2026/01/04 09:36:34 tb Exp $ */
2/* 2/*
3 * Copyright (c) 2017, 2021 Joel Sing <jsing@openbsd.org> 3 * Copyright (c) 2017, 2021 Joel Sing <jsing@openbsd.org>
4 * Copyright (c) 2021 Google, Inc 4 * Copyright (c) 2021 Google, Inc
@@ -66,6 +66,14 @@ const uint8_t asn1_bit_string_primitive[] = {
66 0x04, 0x0a, 0x3b, 0x5f, 0x29, 0x1c, 0xd0, 66 0x04, 0x0a, 0x3b, 0x5f, 0x29, 0x1c, 0xd0,
67}; 67};
68 68
69static const uint8_t asn1_bit_string_trailing_zeroes[] = {
70 0x04, 0x00
71};
72
73static const uint8_t asn1_bit_string_trailing_zeroes_encoded[] = {
74 0x03, 0x03, 0x02, 0x04, 0x00,
75};
76
69static int 77static int
70asn1_bit_string_test(void) 78asn1_bit_string_test(void)
71{ 79{
@@ -166,6 +174,35 @@ asn1_bit_string_test(void)
166 sizeof(asn1_bit_string_primitive))) 174 sizeof(asn1_bit_string_primitive)))
167 goto failed; 175 goto failed;
168 176
177 /*
178 * ASN1_STRING_set() attempts to truncate and picks up wrong unused bits
179 */
180
181 ASN1_BIT_STRING_free(abs);
182 abs = NULL;
183 if ((abs = ASN1_BIT_STRING_new()) == NULL) {
184 fprintf(stderr, "FAIL: ASN1_BIT_STRING_new\n");
185 goto failed;
186 }
187
188 if (!ASN1_STRING_set(abs, asn1_bit_string_trailing_zeroes,
189 sizeof(asn1_bit_string_trailing_zeroes))) {
190 fprintf(stderr, "FAIL: BIT STRING ASN1_BIT_STRING_set trailing zeroes\n");
191 goto failed;
192 }
193
194 freezero(p, len);
195 p = NULL;
196 if ((len = i2d_ASN1_BIT_STRING(abs, &p)) <= 0) {
197 fprintf(stderr, "FAIL: i2d_ASN1_BIT_STRING\n");
198 len = 0;
199 goto failed;
200 }
201 if (!asn1_compare_bytes("BIT STRING trailing zeroes", p, len,
202 asn1_bit_string_trailing_zeroes_encoded,
203 sizeof(asn1_bit_string_trailing_zeroes_encoded)))
204 goto failed;
205
169 failed = 0; 206 failed = 0;
170 207
171 failed: 208 failed: