Commit 3ff584a377 for openssl.org
commit 3ff584a37746825413e694cba2206fcf78a5075f
Author: Iva Marinova <ivamarinova06@gmail.com>
Date: Mon Oct 26 11:43:53 2026 +0200
Encode ASN.1 BOOLEAN TRUE as 0xFF as DER requires
X.690 allows any non-zero content octet for a BOOLEAN TRUE in BER,
but DER (11.1) requires it to be 0xFF. The template decoder stored the
received octet unchanged and the encoder wrote it back, so a TRUE that
was received as e.g. 0x7F, or set by an application as 1, was emitted
as a non-DER encoding when the structure was re-encoded.
Keep accepting any non-zero octet when decoding, since the same decoder
is used for BER structures such as CMS, PKCS#7 and PKCS#12, but store
TRUE as 0xFF. This matches the value already used by ASN1_TYPE_set(),
X509_EXTENSION_set_critical() and X509V3_get_value_bool(), and makes
ASN1_TYPE_cmp() treat all encodings of TRUE as equal. Also always
encode TRUE as 0xFF, whatever non-zero value the field holds.
Signed structures that keep their original encoding, such as the
to-be-signed part of a certificate or CRL, are still written out
unchanged, so their signatures remain valid.
Resolves: https://github.com/openssl/openssl/issues/21331
Resolves: https://github.com/openssl/openssl/issues/33239
Assisted-by: Claude Code:claude-opus-5-5
Reviewed-by: Eugene Syromiatnikov <esyr@openssl.org>
Reviewed-by: Nikola Pajkovsky <nikolap@openssl.org>
Merge-date: Tue Dec 1 17:32:32 2026
Merged-from: https://github.com/openssl/openssl/pull/33400
diff --git a/crypto/asn1/tasn_dec.c b/crypto/asn1/tasn_dec.c
index f1a08d27b1..f5a222c544 100644
--- a/crypto/asn1/tasn_dec.c
+++ b/crypto/asn1/tasn_dec.c
@@ -910,7 +910,11 @@ static int asn1_ex_c2i(ASN1_VALUE **pval, const unsigned char *cont, long len,
} else {
ASN1_BOOLEAN *tbool;
tbool = (ASN1_BOOLEAN *)pval;
- *tbool = *cont;
+ /*
+ * BER allows any non-zero octet for TRUE, DER only 0xFF.
+ * Store the canonical value so that re-encoding yields DER.
+ */
+ *tbool = *cont ? 0xFF : 0;
}
break;
diff --git a/crypto/asn1/tasn_enc.c b/crypto/asn1/tasn_enc.c
index 3307392a84..bd288592da 100644
--- a/crypto/asn1/tasn_enc.c
+++ b/crypto/asn1/tasn_enc.c
@@ -596,7 +596,8 @@ static int asn1_ex_i2c(const ASN1_VALUE **pval, unsigned char *cout, int *putype
if (!*tbool && !it->size)
return -1;
}
- c = (unsigned char)*tbool;
+ /* DER requires TRUE to be encoded as 0xFF */
+ c = *tbool ? 0xFF : 0;
cont = &c;
len = 1;
break;
diff --git a/test/asn1_decode_test.c b/test/asn1_decode_test.c
index 5fd9aad61f..1503baf0a9 100644
--- a/test/asn1_decode_test.c
+++ b/test/asn1_decode_test.c
@@ -473,6 +473,62 @@ err:
return ret;
}
+/*
+ * BER allows any non-zero octet for a BOOLEAN TRUE, DER only 0xFF.
+ * Check that such values are accepted and re-encoded as DER.
+ */
+static const struct {
+ unsigned char in;
+ unsigned char out;
+} boolean_tests[] = {
+ { 0x00, 0x00 },
+ { 0x7F, 0xFF },
+ { 0xFF, 0xFF },
+};
+
+static int test_boolean_reencode(int idx)
+{
+ unsigned char der[] = { V_ASN1_BOOLEAN, 0x01, boolean_tests[idx].in };
+ const unsigned char *p = der;
+ unsigned char *out = NULL;
+ ASN1_TYPE *t = NULL;
+ int ret = 0;
+
+ if (!TEST_ptr(t = d2i_ASN1_TYPE(NULL, &p, sizeof(der)))
+ || !TEST_int_eq(ASN1_TYPE_get(t), V_ASN1_BOOLEAN)
+ || !TEST_int_eq(i2d_ASN1_TYPE(t, &out), 3)
+ || !TEST_uchar_eq(out[2], boolean_tests[idx].out))
+ goto err;
+
+ ret = 1;
+err:
+ OPENSSL_free(out);
+ ASN1_TYPE_free(t);
+ return ret;
+}
+
+static int test_boolean_encode_nonstandard_true(void)
+{
+ static const unsigned char expected[] = { V_ASN1_BOOLEAN, 0x01, 0xFF };
+ unsigned char *out = NULL;
+ ASN1_TYPE *t = NULL;
+ int len, ret = 0;
+
+ if (!TEST_ptr(t = ASN1_TYPE_new()))
+ goto err;
+ t->type = V_ASN1_BOOLEAN;
+ t->value.boolean = 1;
+ len = i2d_ASN1_TYPE(t, &out);
+ if (!TEST_mem_eq(out, len, expected, sizeof(expected)))
+ goto err;
+
+ ret = 1;
+err:
+ OPENSSL_free(out);
+ ASN1_TYPE_free(t);
+ return ret;
+}
+
int setup_tests(void)
{
#ifndef OPENSSL_NO_DEPRECATED_3_0
@@ -491,5 +547,7 @@ int setup_tests(void)
ADD_TEST(test_d2i_read_bio_indefinite_truncated);
ADD_TEST(test_d2i_read_bio_partial_header);
ADD_TEST(test_d2i_bio_concatenated_integers);
+ ADD_ALL_TESTS(test_boolean_reencode, OSSL_NELEM(boolean_tests));
+ ADD_TEST(test_boolean_encode_nonstandard_true);
return 1;
}