From a3d911b0ae1b49dfa94142a56ac28c7b58abb6f2 Mon Sep 17 00:00:00 2001 From: Jerry Yu Date: Tue, 23 May 2023 17:21:52 +0800 Subject: [PATCH 1/8] add script for server9_bad_saltlen Signed-off-by: Jerry Yu --- .../generate_server9_bad_saltlen.py | 100 ++++++++++++++++++ 1 file changed, 100 insertions(+) create mode 100755 scripts/mbedtls_dev/generate_server9_bad_saltlen.py diff --git a/scripts/mbedtls_dev/generate_server9_bad_saltlen.py b/scripts/mbedtls_dev/generate_server9_bad_saltlen.py new file mode 100755 index 000000000..68a8a5f15 --- /dev/null +++ b/scripts/mbedtls_dev/generate_server9_bad_saltlen.py @@ -0,0 +1,100 @@ +#!/usr/bin/env python3 +"""Generate server9-bad-saltlen.crt + +`server9-bad-saltlen.crt (announcing saltlen = 0xDE, signed with another len)`. It can not generate +with normal command. This script is to generate the file. +""" + +# Copyright The Mbed TLS Contributors +# SPDX-License-Identifier: Apache-2.0 +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import subprocess +import argparse +from asn1crypto import pem, x509, core #type: ignore #pylint: disable=import-error + +OPENSSL_RSA_PSS_CERT_COMMAND = r''' +openssl x509 -req -CA {ca_name}.crt -CAkey {ca_name}.key -set_serial 24 {ca_password} \ + {openssl_extfile} -days 3650 -outform DER -in {csr} \ + -sigopt rsa_padding_mode:pss -sigopt rsa_pss_saltlen:{anounce_saltlen} \ + -sigopt rsa_mgf1_md:sha256 +''' +SIG_OPT = \ + r'-sigopt rsa_padding_mode:pss -sigopt rsa_pss_saltlen:{saltlen} -sigopt rsa_mgf1_md:sha256' +OPENSSL_RSA_PSS_DGST_COMMAND = r'''openssl dgst -sign {ca_name}.key {ca_password} \ + -sigopt rsa_padding_mode:pss -sigopt rsa_pss_saltlen:{actual_saltlen} \ + -sigopt rsa_mgf1_md:sha256''' + + +def auto_int(x): + return int(x, 0) + + +def build_argparser(parser): + """Build argument parser""" + parser.description = __doc__ + parser.add_argument('--ca-name', type=str, required=True, + help='Basename of CA files') + parser.add_argument('--ca-password', type=str, + required=True, help='CA key file password') + parser.add_argument('--csr', type=str, required=True, + help='CSR file for generating certificate') + parser.add_argument('--openssl-extfile', type=str, + required=True, help='X905 v3 extension config file') + parser.add_argument('--anounce_saltlen', type=auto_int, + required=True, help='Announced salt length') + parser.add_argument('--actual_saltlen', type=auto_int, + required=True, help='Actual salt length') + parser.add_argument('--output', type=str, required=True) + + +def main(): + parser = argparse.ArgumentParser() + build_argparser(parser) + args = parser.parse_args() + + return generate(**vars(args)) + +def generate(**kwargs): + """Generate different slt length certificate file.""" + ca_password = kwargs.get('ca_password', '') + if ca_password: + kwargs['ca_password'] = r'-passin "pass:{ca_password}"'.format( + **kwargs) + else: + kwargs['ca_password'] = '' + extfile = kwargs.get('openssl_extfile', '') + if extfile: + kwargs['openssl_extfile'] = '-extfile {openssl_extfile}'.format( + **kwargs) + else: + kwargs['openssl_extfile'] = '' + + cmd = OPENSSL_RSA_PSS_CERT_COMMAND.format(**kwargs) + der_bytes = subprocess.check_output(cmd, shell=True) + target_certificate = x509.Certificate.load(der_bytes) + + cmd = OPENSSL_RSA_PSS_DGST_COMMAND.format(**kwargs) + #pylint: disable=unexpected-keyword-arg + der_bytes = subprocess.check_output(cmd, + input=target_certificate['tbs_certificate'].dump(), + shell=True) + + with open(kwargs.get('output'), 'wb') as f: + target_certificate['signature_value'] = core.OctetBitString(der_bytes) + f.write(pem.armor('CERTIFICATE', target_certificate.dump())) + + +if __name__ == '__main__': + main() From 09977e230754ce52c3cd4bc96232da702878d1dc Mon Sep 17 00:00:00 2001 From: Jerry Yu Date: Thu, 25 May 2023 10:53:38 +0800 Subject: [PATCH 2/8] Add asn1crypto to python maintainer requirements Signed-off-by: Jerry Yu --- scripts/maintainer.requirements.txt | 3 +++ 1 file changed, 3 insertions(+) diff --git a/scripts/maintainer.requirements.txt b/scripts/maintainer.requirements.txt index b149921a2..dd2826486 100644 --- a/scripts/maintainer.requirements.txt +++ b/scripts/maintainer.requirements.txt @@ -8,3 +8,6 @@ clang # For building some test vectors pycryptodomex pycryptodome-test-vectors + +# For building `tests/data_files/server9-bad-saltlen.crt` +asn1crypto From ca3790d6538de1bec60c7d7ca09a94a35f83e8d7 Mon Sep 17 00:00:00 2001 From: Jerry Yu Date: Wed, 24 May 2023 18:00:54 +0800 Subject: [PATCH 3/8] Add server9-bad-saltlen generate command Signed-off-by: Jerry Yu --- tests/data_files/Makefile | 15 +++++++++++---- tests/data_files/opensslcnf/server9.crt.v3_ext | 4 ++++ 2 files changed, 15 insertions(+), 4 deletions(-) create mode 100644 tests/data_files/opensslcnf/server9.crt.v3_ext diff --git a/tests/data_files/Makefile b/tests/data_files/Makefile index 21ca489c1..1c0bde4df 100644 --- a/tests/data_files/Makefile +++ b/tests/data_files/Makefile @@ -577,10 +577,6 @@ server9-with-ca.crt: server9.crt $(test_ca_crt) cat $^ > $@ all_final += server9-with-ca.crt -# FIXME: This file needs special sequence. It should be update manually -server9-bad-saltlen.crt: server9.csr $(test_ca_crt) $(test_ca_key_file_rsa) - false - server9-bad-mgfhash.crt: server9.csr $(test_ca_crt) $(test_ca_key_file_rsa) $(OPENSSL) x509 -req -extfile $(cli_crt_extensions_file) -extensions cli-rsa \ -passin "pass:$(test_ca_pwd_rsa)" -CA $(test_ca_crt) -CAkey $(test_ca_key_file_rsa) \ @@ -590,6 +586,17 @@ server9-bad-mgfhash.crt: server9.csr $(test_ca_crt) $(test_ca_key_file_rsa) -in $< -out $@ all_final += server9-bad-mgfhash.crt +server9-bad-saltlen.crt: server9.csr \ + $(test_ca_crt) $(test_ca_key_file_rsa) \ + opensslcnf/server9.crt.v3_ext \ + ../../scripts/mbedtls_dev/generate_server9_bad_saltlen.py + ../../scripts/mbedtls_dev/generate_server9_bad_saltlen.py --ca-name test-ca \ + --ca-password $(test_ca_pwd_rsa) --csr server9.csr \ + --openssl-extfile opensslcnf/server9.crt.v3_ext \ + --anounce_saltlen 0xde --actual_saltlen 0x20 \ + --output $@ +all_final += server9-bad-saltlen.crt + # server10* server10.crt: server10.key test-int-ca3.crt test-int-ca3.key diff --git a/tests/data_files/opensslcnf/server9.crt.v3_ext b/tests/data_files/opensslcnf/server9.crt.v3_ext new file mode 100644 index 000000000..f8d201bea --- /dev/null +++ b/tests/data_files/opensslcnf/server9.crt.v3_ext @@ -0,0 +1,4 @@ +basicConstraints = CA:false +subjectKeyIdentifier=hash +authorityKeyIdentifier=keyid:always,issuer:always + From 2f3f9680331cd9c692fe4395c578e927d1a32132 Mon Sep 17 00:00:00 2001 From: Jerry Yu Date: Wed, 18 Oct 2023 15:06:54 +0800 Subject: [PATCH 4/8] fix wrong typo and indent issue Signed-off-by: Jerry Yu --- .../generate_server9_bad_saltlen.py | 2 +- tests/data_files/Makefile | 25 ++++++++----------- 2 files changed, 12 insertions(+), 15 deletions(-) diff --git a/scripts/mbedtls_dev/generate_server9_bad_saltlen.py b/scripts/mbedtls_dev/generate_server9_bad_saltlen.py index 68a8a5f15..813e6dc0f 100755 --- a/scripts/mbedtls_dev/generate_server9_bad_saltlen.py +++ b/scripts/mbedtls_dev/generate_server9_bad_saltlen.py @@ -67,7 +67,7 @@ def main(): return generate(**vars(args)) def generate(**kwargs): - """Generate different slt length certificate file.""" + """Generate different salt length certificate file.""" ca_password = kwargs.get('ca_password', '') if ca_password: kwargs['ca_password'] = r'-passin "pass:{ca_password}"'.format( diff --git a/tests/data_files/Makefile b/tests/data_files/Makefile index 1c0bde4df..4ab5786e1 100644 --- a/tests/data_files/Makefile +++ b/tests/data_files/Makefile @@ -203,10 +203,8 @@ test-ca2.ku-%.crt: test-ca2.ku-%.crt.openssl.v3_ext $(test_ca_key_file_ec) test- $(OPENSSL) x509 -req -in test-ca2.req.sha256 -extfile $< \ -signkey $(test_ca_key_file_ec) -days 3653 -out $@ -all_final += test-ca2.ku-crl.crt \ - test-ca2.ku-crt.crt \ - test-ca2.ku-crt_crl.crt \ - test-ca2.ku-ds.crt +all_final += test-ca2.ku-crl.crt test-ca2.ku-crt.crt test-ca2.ku-crt_crl.crt \ + test-ca2.ku-ds.crt test-ca2-future.crt: $(test_ca_key_file_ec) test-ca2.req.sha256 $(MBEDTLS_CERT_WRITE) is_ca=1 serial=13926223505202072808 request_file=test-ca2.req.sha256 selfsign=1 \ @@ -586,10 +584,9 @@ server9-bad-mgfhash.crt: server9.csr $(test_ca_crt) $(test_ca_key_file_rsa) -in $< -out $@ all_final += server9-bad-mgfhash.crt -server9-bad-saltlen.crt: server9.csr \ - $(test_ca_crt) $(test_ca_key_file_rsa) \ - opensslcnf/server9.crt.v3_ext \ - ../../scripts/mbedtls_dev/generate_server9_bad_saltlen.py +server9-bad-saltlen.crt: server9.csr $(test_ca_crt) $(test_ca_key_file_rsa) \ + opensslcnf/server9.crt.v3_ext \ + ../../scripts/mbedtls_dev/generate_server9_bad_saltlen.py ../../scripts/mbedtls_dev/generate_server9_bad_saltlen.py --ca-name test-ca \ --ca-password $(test_ca_pwd_rsa) --csr server9.csr \ --openssl-extfile opensslcnf/server9.crt.v3_ext \ @@ -1549,9 +1546,9 @@ server6-ss-child.csr : server6.key all_intermediate += server6-ss-child.csr server6-ss-child.crt: server6-ss-child.csr server5-selfsigned.crt server5.key server6-ss-child.crt.openssl.v3_ext $(OPENSSL) x509 -req -CA server5-selfsigned.crt -CAkey server5.key \ - -extfile server6-ss-child.crt.openssl.v3_ext \ - -set_serial 0x53a2cb5822399474a7ec79ec \ - -days 3650 -sha256 -in $< -out $@ + -extfile server6-ss-child.crt.openssl.v3_ext \ + -set_serial 0x53a2cb5822399474a7ec79ec \ + -days 3650 -sha256 -in $< -out $@ all_final += server6-ss-child.crt @@ -1718,9 +1715,9 @@ crl.pem: $(test_ca_crt) $(test_ca_key_file_rsa) $(test_ca_config_file) $(OPENSSL) ca -gencrl -batch -cert $(test_ca_crt) -keyfile $(test_ca_key_file_rsa) -key $(test_ca_pwd_rsa) -config $(test_ca_server1_config_file) -md sha1 -crldays 3653 -out $@ crl-futureRevocationDate.pem: $(test_ca_crt) $(test_ca_key_file_rsa) \ - $(test_ca_config_file) \ - test-ca.server1.future-crl.db \ - test-ca.server1.future-crl.opensslconf + $(test_ca_config_file) \ + test-ca.server1.future-crl.db \ + test-ca.server1.future-crl.opensslconf $(FAKETIME) -f '+10y' $(OPENSSL) ca -gencrl \ -config test-ca.server1.future-crl.opensslconf -crldays 365 \ -passin "pass:$(test_ca_pwd_rsa)" -out $@ From 974be516dc84f780298cbddfaaf221b815f47cfa Mon Sep 17 00:00:00 2001 From: Jerry Yu Date: Tue, 24 Oct 2023 15:42:30 +0800 Subject: [PATCH 5/8] move asn1crypto to ci.requirements.txt Signed-off-by: Jerry Yu --- scripts/ci.requirements.txt | 4 ++++ scripts/maintainer.requirements.txt | 3 --- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/scripts/ci.requirements.txt b/scripts/ci.requirements.txt index 7dbcfe8e0..69c2db07a 100644 --- a/scripts/ci.requirements.txt +++ b/scripts/ci.requirements.txt @@ -18,3 +18,7 @@ mypy >= 0.780 # for mypy and pylint under Python 3.5, and we also get something good enough # to run audit-validity-dates.py on Python >=3.6. cryptography # >= 35.0.0 + +# For building `tests/data_files/server9-bad-saltlen.crt` and check python +# files. +asn1crypto diff --git a/scripts/maintainer.requirements.txt b/scripts/maintainer.requirements.txt index dd2826486..b149921a2 100644 --- a/scripts/maintainer.requirements.txt +++ b/scripts/maintainer.requirements.txt @@ -8,6 +8,3 @@ clang # For building some test vectors pycryptodomex pycryptodome-test-vectors - -# For building `tests/data_files/server9-bad-saltlen.crt` -asn1crypto From baf7ba44c489d54a8f727497cb51fe88c5550595 Mon Sep 17 00:00:00 2001 From: Jerry Yu Date: Tue, 24 Oct 2023 15:44:00 +0800 Subject: [PATCH 6/8] improve document Signed-off-by: Jerry Yu --- scripts/mbedtls_dev/generate_server9_bad_saltlen.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/scripts/mbedtls_dev/generate_server9_bad_saltlen.py b/scripts/mbedtls_dev/generate_server9_bad_saltlen.py index 813e6dc0f..36682152a 100755 --- a/scripts/mbedtls_dev/generate_server9_bad_saltlen.py +++ b/scripts/mbedtls_dev/generate_server9_bad_saltlen.py @@ -1,8 +1,7 @@ #!/usr/bin/env python3 """Generate server9-bad-saltlen.crt -`server9-bad-saltlen.crt (announcing saltlen = 0xDE, signed with another len)`. It can not generate -with normal command. This script is to generate the file. +Generate a certificate signed with RSA-PSS, with an incorrect salt length. """ # Copyright The Mbed TLS Contributors From 6f21dd5694c92ba4d8d188ddce62df07350fa3ee Mon Sep 17 00:00:00 2001 From: Jerry Yu Date: Tue, 24 Oct 2023 15:45:41 +0800 Subject: [PATCH 7/8] move script to `tests/scripts` Signed-off-by: Jerry Yu --- tests/data_files/Makefile | 4 ++-- .../scripts}/generate_server9_bad_saltlen.py | 0 2 files changed, 2 insertions(+), 2 deletions(-) rename {scripts/mbedtls_dev => tests/scripts}/generate_server9_bad_saltlen.py (100%) diff --git a/tests/data_files/Makefile b/tests/data_files/Makefile index 4ab5786e1..1d5257fa7 100644 --- a/tests/data_files/Makefile +++ b/tests/data_files/Makefile @@ -586,8 +586,8 @@ all_final += server9-bad-mgfhash.crt server9-bad-saltlen.crt: server9.csr $(test_ca_crt) $(test_ca_key_file_rsa) \ opensslcnf/server9.crt.v3_ext \ - ../../scripts/mbedtls_dev/generate_server9_bad_saltlen.py - ../../scripts/mbedtls_dev/generate_server9_bad_saltlen.py --ca-name test-ca \ + ../scripts/generate_server9_bad_saltlen.py + ../scripts/generate_server9_bad_saltlen.py --ca-name test-ca \ --ca-password $(test_ca_pwd_rsa) --csr server9.csr \ --openssl-extfile opensslcnf/server9.crt.v3_ext \ --anounce_saltlen 0xde --actual_saltlen 0x20 \ diff --git a/scripts/mbedtls_dev/generate_server9_bad_saltlen.py b/tests/scripts/generate_server9_bad_saltlen.py similarity index 100% rename from scripts/mbedtls_dev/generate_server9_bad_saltlen.py rename to tests/scripts/generate_server9_bad_saltlen.py From de7ead0a649d79e9a79371ffe1e2a034fbdace07 Mon Sep 17 00:00:00 2001 From: Jerry Yu Date: Thu, 9 Nov 2023 10:10:33 +0800 Subject: [PATCH 8/8] Update license Co-authored-by: Gilles Peskine Signed-off-by: Jerry Yu --- tests/scripts/generate_server9_bad_saltlen.py | 14 +------------- 1 file changed, 1 insertion(+), 13 deletions(-) diff --git a/tests/scripts/generate_server9_bad_saltlen.py b/tests/scripts/generate_server9_bad_saltlen.py index 36682152a..9af4dd3b6 100755 --- a/tests/scripts/generate_server9_bad_saltlen.py +++ b/tests/scripts/generate_server9_bad_saltlen.py @@ -5,19 +5,7 @@ Generate a certificate signed with RSA-PSS, with an incorrect salt length. """ # Copyright The Mbed TLS Contributors -# SPDX-License-Identifier: Apache-2.0 -# -# Licensed under the Apache License, Version 2.0 (the "License"); you may -# not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT -# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. +# SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later import subprocess import argparse