2021-07-22 19:52:17 +02:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
|
|
|
# translate_ciphers.py
|
|
|
|
#
|
|
|
|
# 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.
|
2021-07-28 17:50:30 +02:00
|
|
|
|
|
|
|
"""
|
2023-01-16 04:30:59 +01:00
|
|
|
Translate standard ciphersuite names to GnuTLS, OpenSSL and Mbed TLS standards.
|
2021-07-28 17:50:30 +02:00
|
|
|
|
2021-08-06 10:41:27 +02:00
|
|
|
To test the translation functions run:
|
|
|
|
python3 -m unittest translate_cipher.py
|
2021-07-28 17:50:30 +02:00
|
|
|
"""
|
2021-07-22 19:52:17 +02:00
|
|
|
|
2021-07-21 17:48:54 +02:00
|
|
|
import re
|
2021-07-30 17:57:04 +02:00
|
|
|
import argparse
|
2021-08-06 10:41:27 +02:00
|
|
|
import unittest
|
|
|
|
|
|
|
|
class TestTranslateCiphers(unittest.TestCase):
|
|
|
|
"""
|
|
|
|
Ensure translate_ciphers.py translates and formats ciphersuite names
|
|
|
|
correctly
|
|
|
|
"""
|
|
|
|
def test_translate_all_cipher_names(self):
|
|
|
|
"""
|
2023-01-16 04:30:59 +01:00
|
|
|
Translate standard ciphersuite names to GnuTLS, OpenSSL and
|
|
|
|
Mbed TLS counterpart. Use only a small subset of ciphers
|
|
|
|
that exercise each step of the translation functions
|
2021-08-06 10:41:27 +02:00
|
|
|
"""
|
|
|
|
ciphers = [
|
2023-01-16 04:30:59 +01:00
|
|
|
("TLS_ECDHE_ECDSA_WITH_NULL_SHA",
|
2021-09-02 19:50:30 +02:00
|
|
|
"+ECDHE-ECDSA:+NULL:+SHA1",
|
2023-01-16 04:30:59 +01:00
|
|
|
"ECDHE-ECDSA-NULL-SHA",
|
|
|
|
"TLS-ECDHE-ECDSA-WITH-NULL-SHA"),
|
|
|
|
("TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256",
|
2021-09-02 19:50:30 +02:00
|
|
|
"+ECDHE-ECDSA:+AES-128-GCM:+AEAD",
|
2023-01-16 04:30:59 +01:00
|
|
|
"ECDHE-ECDSA-AES128-GCM-SHA256",
|
|
|
|
"TLS-ECDHE-ECDSA-WITH-AES-128-GCM-SHA256"),
|
|
|
|
("TLS_DHE_RSA_WITH_3DES_EDE_CBC_SHA",
|
2021-09-02 19:50:30 +02:00
|
|
|
"+DHE-RSA:+3DES-CBC:+SHA1",
|
2023-01-16 04:30:59 +01:00
|
|
|
"EDH-RSA-DES-CBC3-SHA",
|
|
|
|
"TLS-DHE-RSA-WITH-3DES-EDE-CBC-SHA"),
|
|
|
|
("TLS_RSA_WITH_AES_256_CBC_SHA",
|
2021-09-03 14:30:44 +02:00
|
|
|
"+RSA:+AES-256-CBC:+SHA1",
|
2023-01-16 04:30:59 +01:00
|
|
|
"AES256-SHA",
|
|
|
|
"TLS-RSA-WITH-AES-256-CBC-SHA"),
|
|
|
|
("TLS_PSK_WITH_3DES_EDE_CBC_SHA",
|
2021-09-03 14:30:44 +02:00
|
|
|
"+PSK:+3DES-CBC:+SHA1",
|
2023-01-16 04:30:59 +01:00
|
|
|
"PSK-3DES-EDE-CBC-SHA",
|
|
|
|
"TLS-PSK-WITH-3DES-EDE-CBC-SHA"),
|
|
|
|
("TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256",
|
2021-09-02 19:50:30 +02:00
|
|
|
None,
|
2023-01-16 04:30:59 +01:00
|
|
|
"ECDHE-ECDSA-CHACHA20-POLY1305",
|
|
|
|
"TLS-ECDHE-ECDSA-WITH-CHACHA20-POLY1305-SHA256"),
|
|
|
|
("TLS_ECDHE_ECDSA_WITH_AES_128_CCM",
|
2021-09-02 19:50:30 +02:00
|
|
|
"+ECDHE-ECDSA:+AES-128-CCM:+AEAD",
|
2021-09-03 14:30:44 +02:00
|
|
|
None,
|
2023-01-16 04:30:59 +01:00
|
|
|
"TLS-ECDHE-ECDSA-WITH-AES-128-CCM"),
|
|
|
|
("TLS_ECDHE_RSA_WITH_ARIA_256_GCM_SHA384",
|
|
|
|
None,
|
|
|
|
"ECDHE-ARIA256-GCM-SHA384",
|
|
|
|
"TLS-ECDHE-RSA-WITH-ARIA-256-GCM-SHA384"),
|
2021-08-06 10:41:27 +02:00
|
|
|
]
|
|
|
|
|
2023-01-16 04:30:59 +01:00
|
|
|
for s, g_exp, o_exp, m_exp in ciphers:
|
2021-08-06 10:41:27 +02:00
|
|
|
|
|
|
|
if g_exp is not None:
|
2023-01-16 04:30:59 +01:00
|
|
|
g = translate_gnutls(s)
|
2021-08-06 10:41:27 +02:00
|
|
|
self.assertEqual(g, g_exp)
|
|
|
|
|
|
|
|
if o_exp is not None:
|
2023-01-16 04:30:59 +01:00
|
|
|
o = translate_ossl(s)
|
2021-08-06 10:41:27 +02:00
|
|
|
self.assertEqual(o, o_exp)
|
|
|
|
|
2023-01-16 04:30:59 +01:00
|
|
|
if m_exp is not None:
|
|
|
|
m = translate_mbedtls(s)
|
|
|
|
self.assertEqual(m, m_exp)
|
|
|
|
|
|
|
|
def translate_gnutls(s_cipher):
|
2021-07-28 17:50:30 +02:00
|
|
|
"""
|
2023-01-16 04:30:59 +01:00
|
|
|
Translate s_cipher from standard ciphersuite naming convention
|
2021-07-28 17:50:30 +02:00
|
|
|
and return the GnuTLS naming convention
|
|
|
|
"""
|
|
|
|
|
2023-01-16 04:30:59 +01:00
|
|
|
# Replace "_" with "-" to handle ciphersuite names based on Mbed TLS
|
|
|
|
# naming convention
|
|
|
|
s_cipher = s_cipher.replace("_", "-")
|
|
|
|
|
|
|
|
s_cipher = re.sub(r'\ATLS-', '+', s_cipher)
|
|
|
|
s_cipher = s_cipher.replace("-WITH-", ":+")
|
|
|
|
s_cipher = s_cipher.replace("-EDE", "")
|
2021-07-21 17:48:54 +02:00
|
|
|
|
2021-07-30 17:57:04 +02:00
|
|
|
# SHA in Mbed TLS == SHA1 GnuTLS,
|
|
|
|
# if the last 3 chars are SHA append 1
|
2023-01-16 04:30:59 +01:00
|
|
|
if s_cipher[-3:] == "SHA":
|
|
|
|
s_cipher = s_cipher+"1"
|
2021-07-21 17:48:54 +02:00
|
|
|
|
|
|
|
# CCM or CCM-8 should be followed by ":+AEAD"
|
2021-07-30 17:57:04 +02:00
|
|
|
# Replace "GCM:+SHAxyz" with "GCM:+AEAD"
|
2023-01-16 04:30:59 +01:00
|
|
|
if "CCM" in s_cipher or "GCM" in s_cipher:
|
|
|
|
s_cipher = re.sub(r"GCM-SHA\d\d\d", "GCM", s_cipher)
|
|
|
|
s_cipher = s_cipher+":+AEAD"
|
2021-07-21 17:48:54 +02:00
|
|
|
|
|
|
|
# Replace the last "-" with ":+"
|
2021-07-20 19:26:03 +02:00
|
|
|
else:
|
2023-01-16 04:30:59 +01:00
|
|
|
index = s_cipher.rindex("-")
|
|
|
|
s_cipher = s_cipher[:index] + ":+" + s_cipher[index+1:]
|
2021-07-20 19:26:03 +02:00
|
|
|
|
2023-01-16 04:30:59 +01:00
|
|
|
return s_cipher
|
2021-07-21 17:48:54 +02:00
|
|
|
|
2023-01-16 04:30:59 +01:00
|
|
|
def translate_ossl(s_cipher):
|
2021-07-28 17:50:30 +02:00
|
|
|
"""
|
2023-01-16 04:30:59 +01:00
|
|
|
Translate s_cipher from standard ciphersuite naming convention
|
2021-07-28 17:50:30 +02:00
|
|
|
and return the OpenSSL naming convention
|
|
|
|
"""
|
|
|
|
|
2023-01-16 04:30:59 +01:00
|
|
|
# Replace "_" with "-" to handle ciphersuite names based on Mbed TLS
|
|
|
|
# naming convention
|
|
|
|
s_cipher = s_cipher.replace("_", "-")
|
|
|
|
|
|
|
|
s_cipher = re.sub(r'^TLS-', '', s_cipher)
|
|
|
|
s_cipher = s_cipher.replace("-WITH", "")
|
2021-07-21 17:48:54 +02:00
|
|
|
|
|
|
|
# Remove the "-" from "ABC-xyz"
|
2023-01-16 04:30:59 +01:00
|
|
|
s_cipher = s_cipher.replace("AES-", "AES")
|
|
|
|
s_cipher = s_cipher.replace("CAMELLIA-", "CAMELLIA")
|
|
|
|
s_cipher = s_cipher.replace("ARIA-", "ARIA")
|
2021-07-20 19:26:03 +02:00
|
|
|
|
2021-07-21 17:48:54 +02:00
|
|
|
# Remove "RSA" if it is at the beginning
|
2023-01-16 04:30:59 +01:00
|
|
|
s_cipher = re.sub(r'^RSA-', r'', s_cipher)
|
2021-07-20 19:26:03 +02:00
|
|
|
|
2021-07-21 17:48:54 +02:00
|
|
|
# For all circumstances outside of PSK
|
2023-01-16 04:30:59 +01:00
|
|
|
if "PSK" not in s_cipher:
|
|
|
|
s_cipher = s_cipher.replace("-EDE", "")
|
|
|
|
s_cipher = s_cipher.replace("3DES-CBC", "DES-CBC3")
|
2021-07-21 17:48:54 +02:00
|
|
|
|
|
|
|
# Remove "CBC" if it is not prefixed by DES
|
2023-01-16 04:30:59 +01:00
|
|
|
s_cipher = re.sub(r'(?<!DES-)CBC-', r'', s_cipher)
|
2021-07-21 17:48:54 +02:00
|
|
|
|
|
|
|
# ECDHE-RSA-ARIA does not exist in OpenSSL
|
2023-01-16 04:30:59 +01:00
|
|
|
s_cipher = s_cipher.replace("ECDHE-RSA-ARIA", "ECDHE-ARIA")
|
2021-07-20 19:26:03 +02:00
|
|
|
|
2021-07-21 17:48:54 +02:00
|
|
|
# POLY1305 should not be followed by anything
|
2023-01-16 04:30:59 +01:00
|
|
|
if "POLY1305" in s_cipher:
|
|
|
|
index = s_cipher.rindex("POLY1305")
|
|
|
|
s_cipher = s_cipher[:index+8]
|
2021-07-20 19:26:03 +02:00
|
|
|
|
2021-07-21 17:48:54 +02:00
|
|
|
# If DES is being used, Replace DHE with EDH
|
2023-01-16 04:30:59 +01:00
|
|
|
if "DES" in s_cipher and "DHE" in s_cipher and "ECDHE" not in s_cipher:
|
|
|
|
s_cipher = s_cipher.replace("DHE", "EDH")
|
|
|
|
|
|
|
|
return s_cipher
|
|
|
|
|
|
|
|
def translate_mbedtls(s_cipher):
|
|
|
|
"""
|
|
|
|
Translate s_cipher from standard ciphersuite naming convention
|
|
|
|
and return Mbed TLS ciphersuite naming convention
|
|
|
|
"""
|
|
|
|
|
|
|
|
# Replace "_" with "-"
|
|
|
|
s_cipher = s_cipher.replace("_", "-")
|
2021-07-20 19:26:03 +02:00
|
|
|
|
2023-01-16 04:30:59 +01:00
|
|
|
return s_cipher
|
2021-07-22 17:08:29 +02:00
|
|
|
|
2021-07-30 17:57:04 +02:00
|
|
|
def format_ciphersuite_names(mode, names):
|
2023-01-16 04:30:59 +01:00
|
|
|
t = {"g": translate_gnutls,
|
|
|
|
"o": translate_ossl,
|
|
|
|
"m": translate_mbedtls
|
|
|
|
}[mode]
|
2021-07-30 17:57:04 +02:00
|
|
|
return " ".join(t(c) for c in names)
|
|
|
|
|
|
|
|
def main(target, names):
|
|
|
|
print(format_ciphersuite_names(target, names))
|
2021-07-22 17:08:29 +02:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2021-07-30 17:57:04 +02:00
|
|
|
PARSER = argparse.ArgumentParser()
|
2023-01-16 04:30:59 +01:00
|
|
|
PARSER.add_argument('target', metavar='TARGET', choices=['o', 'g', 'm'])
|
2021-07-30 17:57:04 +02:00
|
|
|
PARSER.add_argument('names', metavar='NAMES', nargs='+')
|
|
|
|
ARGS = PARSER.parse_args()
|
|
|
|
main(ARGS.target, ARGS.names)
|