Improve coding style and consistancy

- Replace uses of mbed and gnu with mbedtls and gnutls respectivley.
 - Uses sys.exit() rather than exit()
 - Rename format() as it is an inbuilt python function
 - Add error information if incorrect arguments are passsed to
   translate_ciphers.py

Signed-off-by: Joe Subbiani <joe.subbiani@arm.com>
This commit is contained in:
Joe Subbiani 2021-07-27 15:22:26 +01:00
parent 34d62620fb
commit 0fadf8ef7d
3 changed files with 41 additions and 34 deletions

View File

@ -121,13 +121,13 @@ Ot_CIPHERS=$( echo "$Ot_CIPHERS" | sed -e 's/[[:space:]][[:space:]]*/ /g' -e 's/
# Upon fail, print them to view the differences # Upon fail, print them to view the differences
if [ "$Mt_CIPHERS" != "$M_CIPHERS" ] if [ "$Mt_CIPHERS" != "$M_CIPHERS" ]
then then
echo "MBED Translated: $M_CIPHERS" echo "MBEDTLS Translated: $M_CIPHERS"
echo "MBED Original: $Mt_CIPHERS" echo "MBEDTLS Original: $Mt_CIPHERS"
fi fi
if [ "$Gt_CIPHERS" != "$G_CIPHERS" ] if [ "$Gt_CIPHERS" != "$G_CIPHERS" ]
then then
echo "GNU Translated: $G_CIPHERS" echo "GNUTLS Translated: $G_CIPHERS"
echo "GNU Original: $Gt_CIPHERS" echo "GNUTLS Original: $Gt_CIPHERS"
fi fi
if [ "$Ot_CIPHERS" != "$O_CIPHERS" ] if [ "$Ot_CIPHERS" != "$O_CIPHERS" ]
then then

View File

@ -21,7 +21,7 @@
# #
# Test translate_ciphers.py by running every MBedTLS ciphersuite name # Test translate_ciphers.py by running every MBedTLS ciphersuite name
# combination through the translate functions and comparing them to their # combination through the translate functions and comparing them to their
# correct GNU or OpenSSL counterpart. # correct GNUTLS or OpenSSL counterpart.
from translate_ciphers import * from translate_ciphers import *
@ -170,21 +170,17 @@ def test_all_common():
"PSK-3DES-EDE-CBC-SHA", "PSK-3DES-EDE-CBC-SHA",
"PSK-AES128-CBC-SHA", "PSK-AES128-CBC-SHA",
"PSK-AES256-CBC-SHA", "PSK-AES256-CBC-SHA",
#"PSK-DES-CBC3-SHA",
#"PSK-AES128-SHA",
#"PSK-AES256-SHA",
] ]
for i in range(len(m_ciphers)): for i in range(len(m_ciphers)):
g = translate_gnu(m_ciphers[i]) g = translate_gnutls(m_ciphers[i])
assert_equal(g, g_ciphers[i]) assert_equal(g, g_ciphers[i])
o = translate_ossl(m_ciphers[i]) o = translate_ossl(m_ciphers[i])
assert_equal(o, o_ciphers[i]) assert_equal(o, o_ciphers[i])
def test_mbed_ossl_common(): def test_mbedtls_ossl_common():
m_ciphers = [ m_ciphers = [
"TLS-ECDH-ECDSA-WITH-NULL-SHA", "TLS-ECDH-ECDSA-WITH-NULL-SHA",
"TLS-ECDH-ECDSA-WITH-3DES-EDE-CBC-SHA", "TLS-ECDH-ECDSA-WITH-3DES-EDE-CBC-SHA",
@ -235,7 +231,6 @@ def test_mbed_ossl_common():
"DES-CBC-SHA", "DES-CBC-SHA",
"EDH-RSA-DES-CBC-SHA", "EDH-RSA-DES-CBC-SHA",
#"DHE-RSA-DES-CBC-SHA",
"ECDHE-ARIA256-GCM-SHA384", "ECDHE-ARIA256-GCM-SHA384",
"DHE-RSA-ARIA256-GCM-SHA384", "DHE-RSA-ARIA256-GCM-SHA384",
@ -260,7 +255,7 @@ def test_mbed_ossl_common():
o = translate_ossl(m_ciphers[i]) o = translate_ossl(m_ciphers[i])
assert_equal(o, o_ciphers[i]) assert_equal(o, o_ciphers[i])
def test_mbed_gnu_common(): def test_mbedtls_gnutls_common():
m_ciphers = [ m_ciphers = [
"TLS-ECDHE-ECDSA-WITH-CAMELLIA-128-CBC-SHA256", "TLS-ECDHE-ECDSA-WITH-CAMELLIA-128-CBC-SHA256",
"TLS-ECDHE-ECDSA-WITH-CAMELLIA-256-CBC-SHA384", "TLS-ECDHE-ECDSA-WITH-CAMELLIA-256-CBC-SHA384",
@ -442,9 +437,9 @@ def test_mbed_gnu_common():
for i in range(len(m_ciphers)): for i in range(len(m_ciphers)):
g = translate_gnu(m_ciphers[i]) g = translate_gnutls(m_ciphers[i])
assert_equal(g, g_ciphers[i]) assert_equal(g, g_ciphers[i])
test_all_common() test_all_common()
test_mbed_ossl_common() test_mbedtls_ossl_common()
test_mbed_gnu_common() test_mbedtls_gnutls_common()

View File

@ -19,20 +19,20 @@
# #
# Purpose # Purpose
# #
# Translate ciphersuite names in MBedTLS format to OpenSSL and GNU # Translate ciphersuite names in MBedTLS format to OpenSSL and GNUTLS
# standards. # standards.
# #
# Format and analyse strings past in via input arguments to match # Format and analyse strings past in via input arguments to match
# the expected strings utilised in compat.sh. # the expected strings utilised in compat.sh.
# #
# sys.argv[1] should be "g" or "o" for GNU or OpenSSL. # sys.argv[1] should be "g" or "o" for GNUTLS or OpenSSL.
# sys.argv[2] should be a string containing one or more # sys.argv[2] should be a string containing one or more
# ciphersuite names. # ciphersuite names.
import re import re
import sys import sys
def translate_gnu(m_cipher): def translate_gnutls(m_cipher):
# Remove "TLS-" # Remove "TLS-"
# Replace "-WITH-" with ":+" # Replace "-WITH-" with ":+"
# Remove "EDE" # Remove "EDE"
@ -97,23 +97,35 @@ def translate_ossl(m_cipher):
return m_cipher return m_cipher
def format(mode, ciphers): def format_ciphersuite_names(mode, ciphers):
ciphers = ciphers.split() #ciphers = ciphers.split()
t_ciphers = [] #t_ciphers = []
if mode == "g": #if mode == "g":
for i in ciphers: # for i in ciphers:
t_ciphers.append(translate_gnu(i)) # t_ciphers.append(translate_gnutls(i))
if mode == "o": #elif mode == "o":
for i in ciphers: # for i in ciphers:
t_ciphers.append(translate_ossl(i)) # t_ciphers.append(translate_ossl(i))
return " ".join(t_ciphers) #else:
# print("Incorrect use of argument 1, should be either \"g\" or \"o\"")
# exit(1)
#return " ".join(t_ciphers)
try:
t = {"g": translate_gnutls, "o": translate_ossl}[mode]
return " ".join(t(c) for c in ciphers.split())
except Exception as E:
if E != mode: print(E)
else: print("Incorrect use of argument 1, should be either \"g\" or \"o\"")
sys.exit(1)
def main(): def main():
# print command line arguments if len(sys.argv) != 3:
if len(sys.argv) <= 2: print("""Incorrect number of arguments.
exit(1) The first argument with either an \"o\" for OpenSSL or \"g\" for GNUTLS.
else: The second argument should a single space seperated string of MBedTLS ciphersuite names""")
print(format(sys.argv[1], sys.argv[2])) sys.exit(1)
print(format_ciphersuite_names(sys.argv[1], sys.argv[2]))
sys.exit(0)
if __name__ == "__main__": if __name__ == "__main__":
main() main()