| Joe Subbiani | a16ccac | 2021-07-22 18:52:17 +0100 | [diff] [blame] | 1 | #!/usr/bin/env python3 | 
|  | 2 |  | 
|  | 3 | # translate_ciphers.py | 
|  | 4 | # | 
|  | 5 | # Copyright The Mbed TLS Contributors | 
| Dave Rodgman | 16799db | 2023-11-02 19:47:20 +0000 | [diff] [blame] | 6 | # SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later | 
| Joe Subbiani | f849a93 | 2021-07-28 16:50:30 +0100 | [diff] [blame] | 7 |  | 
|  | 8 | """ | 
| Yanray Wang | ee97f05 | 2023-01-16 11:30:59 +0800 | [diff] [blame] | 9 | Translate standard ciphersuite names to GnuTLS, OpenSSL and Mbed TLS standards. | 
| Joe Subbiani | f849a93 | 2021-07-28 16:50:30 +0100 | [diff] [blame] | 10 |  | 
| Joe Subbiani | a25ffab | 2021-08-06 09:41:27 +0100 | [diff] [blame] | 11 | To test the translation functions run: | 
|  | 12 | python3 -m unittest translate_cipher.py | 
| Joe Subbiani | f849a93 | 2021-07-28 16:50:30 +0100 | [diff] [blame] | 13 | """ | 
| Joe Subbiani | a16ccac | 2021-07-22 18:52:17 +0100 | [diff] [blame] | 14 |  | 
| Joe Subbiani | 3ad5832 | 2021-07-21 16:48:54 +0100 | [diff] [blame] | 15 | import re | 
| Joe Subbiani | 918ee79 | 2021-07-30 16:57:04 +0100 | [diff] [blame] | 16 | import argparse | 
| Joe Subbiani | a25ffab | 2021-08-06 09:41:27 +0100 | [diff] [blame] | 17 | import unittest | 
|  | 18 |  | 
|  | 19 | class TestTranslateCiphers(unittest.TestCase): | 
|  | 20 | """ | 
|  | 21 | Ensure translate_ciphers.py translates and formats ciphersuite names | 
|  | 22 | correctly | 
|  | 23 | """ | 
|  | 24 | def test_translate_all_cipher_names(self): | 
|  | 25 | """ | 
| Yanray Wang | ee97f05 | 2023-01-16 11:30:59 +0800 | [diff] [blame] | 26 | Translate standard ciphersuite names to GnuTLS, OpenSSL and | 
|  | 27 | Mbed TLS counterpart. Use only a small subset of ciphers | 
|  | 28 | that exercise each step of the translation functions | 
| Joe Subbiani | a25ffab | 2021-08-06 09:41:27 +0100 | [diff] [blame] | 29 | """ | 
|  | 30 | ciphers = [ | 
| Yanray Wang | ee97f05 | 2023-01-16 11:30:59 +0800 | [diff] [blame] | 31 | ("TLS_ECDHE_ECDSA_WITH_NULL_SHA", | 
| Joe Subbiani | 49d57bc | 2021-09-02 18:50:30 +0100 | [diff] [blame] | 32 | "+ECDHE-ECDSA:+NULL:+SHA1", | 
| Yanray Wang | ee97f05 | 2023-01-16 11:30:59 +0800 | [diff] [blame] | 33 | "ECDHE-ECDSA-NULL-SHA", | 
|  | 34 | "TLS-ECDHE-ECDSA-WITH-NULL-SHA"), | 
|  | 35 | ("TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256", | 
| Joe Subbiani | 49d57bc | 2021-09-02 18:50:30 +0100 | [diff] [blame] | 36 | "+ECDHE-ECDSA:+AES-128-GCM:+AEAD", | 
| Yanray Wang | ee97f05 | 2023-01-16 11:30:59 +0800 | [diff] [blame] | 37 | "ECDHE-ECDSA-AES128-GCM-SHA256", | 
|  | 38 | "TLS-ECDHE-ECDSA-WITH-AES-128-GCM-SHA256"), | 
|  | 39 | ("TLS_DHE_RSA_WITH_3DES_EDE_CBC_SHA", | 
| Joe Subbiani | 49d57bc | 2021-09-02 18:50:30 +0100 | [diff] [blame] | 40 | "+DHE-RSA:+3DES-CBC:+SHA1", | 
| Yanray Wang | ee97f05 | 2023-01-16 11:30:59 +0800 | [diff] [blame] | 41 | "EDH-RSA-DES-CBC3-SHA", | 
|  | 42 | "TLS-DHE-RSA-WITH-3DES-EDE-CBC-SHA"), | 
|  | 43 | ("TLS_RSA_WITH_AES_256_CBC_SHA", | 
| Joe Subbiani | e5d6106 | 2021-09-03 13:30:44 +0100 | [diff] [blame] | 44 | "+RSA:+AES-256-CBC:+SHA1", | 
| Yanray Wang | ee97f05 | 2023-01-16 11:30:59 +0800 | [diff] [blame] | 45 | "AES256-SHA", | 
|  | 46 | "TLS-RSA-WITH-AES-256-CBC-SHA"), | 
|  | 47 | ("TLS_PSK_WITH_3DES_EDE_CBC_SHA", | 
| Joe Subbiani | e5d6106 | 2021-09-03 13:30:44 +0100 | [diff] [blame] | 48 | "+PSK:+3DES-CBC:+SHA1", | 
| Yanray Wang | ee97f05 | 2023-01-16 11:30:59 +0800 | [diff] [blame] | 49 | "PSK-3DES-EDE-CBC-SHA", | 
|  | 50 | "TLS-PSK-WITH-3DES-EDE-CBC-SHA"), | 
|  | 51 | ("TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256", | 
| Joe Subbiani | 49d57bc | 2021-09-02 18:50:30 +0100 | [diff] [blame] | 52 | None, | 
| Yanray Wang | ee97f05 | 2023-01-16 11:30:59 +0800 | [diff] [blame] | 53 | "ECDHE-ECDSA-CHACHA20-POLY1305", | 
|  | 54 | "TLS-ECDHE-ECDSA-WITH-CHACHA20-POLY1305-SHA256"), | 
|  | 55 | ("TLS_ECDHE_ECDSA_WITH_AES_128_CCM", | 
| Joe Subbiani | 49d57bc | 2021-09-02 18:50:30 +0100 | [diff] [blame] | 56 | "+ECDHE-ECDSA:+AES-128-CCM:+AEAD", | 
| Joe Subbiani | e5d6106 | 2021-09-03 13:30:44 +0100 | [diff] [blame] | 57 | None, | 
| Yanray Wang | ee97f05 | 2023-01-16 11:30:59 +0800 | [diff] [blame] | 58 | "TLS-ECDHE-ECDSA-WITH-AES-128-CCM"), | 
|  | 59 | ("TLS_ECDHE_RSA_WITH_ARIA_256_GCM_SHA384", | 
|  | 60 | None, | 
|  | 61 | "ECDHE-ARIA256-GCM-SHA384", | 
|  | 62 | "TLS-ECDHE-RSA-WITH-ARIA-256-GCM-SHA384"), | 
| Joe Subbiani | a25ffab | 2021-08-06 09:41:27 +0100 | [diff] [blame] | 63 | ] | 
|  | 64 |  | 
| Yanray Wang | ee97f05 | 2023-01-16 11:30:59 +0800 | [diff] [blame] | 65 | for s, g_exp, o_exp, m_exp in ciphers: | 
| Joe Subbiani | a25ffab | 2021-08-06 09:41:27 +0100 | [diff] [blame] | 66 |  | 
|  | 67 | if g_exp is not None: | 
| Yanray Wang | ee97f05 | 2023-01-16 11:30:59 +0800 | [diff] [blame] | 68 | g = translate_gnutls(s) | 
| Joe Subbiani | a25ffab | 2021-08-06 09:41:27 +0100 | [diff] [blame] | 69 | self.assertEqual(g, g_exp) | 
|  | 70 |  | 
|  | 71 | if o_exp is not None: | 
| Yanray Wang | ee97f05 | 2023-01-16 11:30:59 +0800 | [diff] [blame] | 72 | o = translate_ossl(s) | 
| Joe Subbiani | a25ffab | 2021-08-06 09:41:27 +0100 | [diff] [blame] | 73 | self.assertEqual(o, o_exp) | 
|  | 74 |  | 
| Yanray Wang | ee97f05 | 2023-01-16 11:30:59 +0800 | [diff] [blame] | 75 | if m_exp is not None: | 
|  | 76 | m = translate_mbedtls(s) | 
|  | 77 | self.assertEqual(m, m_exp) | 
|  | 78 |  | 
|  | 79 | def translate_gnutls(s_cipher): | 
| Joe Subbiani | f849a93 | 2021-07-28 16:50:30 +0100 | [diff] [blame] | 80 | """ | 
| Yanray Wang | ee97f05 | 2023-01-16 11:30:59 +0800 | [diff] [blame] | 81 | Translate s_cipher from standard ciphersuite naming convention | 
| Joe Subbiani | f849a93 | 2021-07-28 16:50:30 +0100 | [diff] [blame] | 82 | and return the GnuTLS naming convention | 
|  | 83 | """ | 
|  | 84 |  | 
| Yanray Wang | ee97f05 | 2023-01-16 11:30:59 +0800 | [diff] [blame] | 85 | # Replace "_" with "-" to handle ciphersuite names based on Mbed TLS | 
|  | 86 | # naming convention | 
|  | 87 | s_cipher = s_cipher.replace("_", "-") | 
|  | 88 |  | 
|  | 89 | s_cipher = re.sub(r'\ATLS-', '+', s_cipher) | 
|  | 90 | s_cipher = s_cipher.replace("-WITH-", ":+") | 
|  | 91 | s_cipher = s_cipher.replace("-EDE", "") | 
| Joe Subbiani | 3ad5832 | 2021-07-21 16:48:54 +0100 | [diff] [blame] | 92 |  | 
| Joe Subbiani | 918ee79 | 2021-07-30 16:57:04 +0100 | [diff] [blame] | 93 | # SHA in Mbed TLS == SHA1 GnuTLS, | 
|  | 94 | # if the last 3 chars are SHA append 1 | 
| Yanray Wang | ee97f05 | 2023-01-16 11:30:59 +0800 | [diff] [blame] | 95 | if s_cipher[-3:] == "SHA": | 
|  | 96 | s_cipher = s_cipher+"1" | 
| Joe Subbiani | 3ad5832 | 2021-07-21 16:48:54 +0100 | [diff] [blame] | 97 |  | 
|  | 98 | # CCM or CCM-8 should be followed by ":+AEAD" | 
| Joe Subbiani | 918ee79 | 2021-07-30 16:57:04 +0100 | [diff] [blame] | 99 | # Replace "GCM:+SHAxyz" with "GCM:+AEAD" | 
| Yanray Wang | ee97f05 | 2023-01-16 11:30:59 +0800 | [diff] [blame] | 100 | if "CCM" in s_cipher or "GCM" in s_cipher: | 
|  | 101 | s_cipher = re.sub(r"GCM-SHA\d\d\d", "GCM", s_cipher) | 
|  | 102 | s_cipher = s_cipher+":+AEAD" | 
| Joe Subbiani | 3ad5832 | 2021-07-21 16:48:54 +0100 | [diff] [blame] | 103 |  | 
|  | 104 | # Replace the last "-" with ":+" | 
| Joe Subbiani | 8394484 | 2021-07-20 18:26:03 +0100 | [diff] [blame] | 105 | else: | 
| Yanray Wang | ee97f05 | 2023-01-16 11:30:59 +0800 | [diff] [blame] | 106 | index = s_cipher.rindex("-") | 
|  | 107 | s_cipher = s_cipher[:index] + ":+" + s_cipher[index+1:] | 
| Joe Subbiani | 8394484 | 2021-07-20 18:26:03 +0100 | [diff] [blame] | 108 |  | 
| Yanray Wang | ee97f05 | 2023-01-16 11:30:59 +0800 | [diff] [blame] | 109 | return s_cipher | 
| Joe Subbiani | 3ad5832 | 2021-07-21 16:48:54 +0100 | [diff] [blame] | 110 |  | 
| Yanray Wang | ee97f05 | 2023-01-16 11:30:59 +0800 | [diff] [blame] | 111 | def translate_ossl(s_cipher): | 
| Joe Subbiani | f849a93 | 2021-07-28 16:50:30 +0100 | [diff] [blame] | 112 | """ | 
| Yanray Wang | ee97f05 | 2023-01-16 11:30:59 +0800 | [diff] [blame] | 113 | Translate s_cipher from standard ciphersuite naming convention | 
| Joe Subbiani | f849a93 | 2021-07-28 16:50:30 +0100 | [diff] [blame] | 114 | and return the OpenSSL naming convention | 
|  | 115 | """ | 
|  | 116 |  | 
| Yanray Wang | ee97f05 | 2023-01-16 11:30:59 +0800 | [diff] [blame] | 117 | # Replace "_" with "-" to handle ciphersuite names based on Mbed TLS | 
|  | 118 | # naming convention | 
|  | 119 | s_cipher = s_cipher.replace("_", "-") | 
|  | 120 |  | 
|  | 121 | s_cipher = re.sub(r'^TLS-', '', s_cipher) | 
|  | 122 | s_cipher = s_cipher.replace("-WITH", "") | 
| Joe Subbiani | 3ad5832 | 2021-07-21 16:48:54 +0100 | [diff] [blame] | 123 |  | 
|  | 124 | # Remove the "-" from "ABC-xyz" | 
| Yanray Wang | ee97f05 | 2023-01-16 11:30:59 +0800 | [diff] [blame] | 125 | s_cipher = s_cipher.replace("AES-", "AES") | 
|  | 126 | s_cipher = s_cipher.replace("CAMELLIA-", "CAMELLIA") | 
|  | 127 | s_cipher = s_cipher.replace("ARIA-", "ARIA") | 
| Joe Subbiani | 8394484 | 2021-07-20 18:26:03 +0100 | [diff] [blame] | 128 |  | 
| Joe Subbiani | 3ad5832 | 2021-07-21 16:48:54 +0100 | [diff] [blame] | 129 | # Remove "RSA" if it is at the beginning | 
| Yanray Wang | ee97f05 | 2023-01-16 11:30:59 +0800 | [diff] [blame] | 130 | s_cipher = re.sub(r'^RSA-', r'', s_cipher) | 
| Joe Subbiani | 8394484 | 2021-07-20 18:26:03 +0100 | [diff] [blame] | 131 |  | 
| Joe Subbiani | 3ad5832 | 2021-07-21 16:48:54 +0100 | [diff] [blame] | 132 | # For all circumstances outside of PSK | 
| Yanray Wang | ee97f05 | 2023-01-16 11:30:59 +0800 | [diff] [blame] | 133 | if "PSK" not in s_cipher: | 
|  | 134 | s_cipher = s_cipher.replace("-EDE", "") | 
|  | 135 | s_cipher = s_cipher.replace("3DES-CBC", "DES-CBC3") | 
| Joe Subbiani | 3ad5832 | 2021-07-21 16:48:54 +0100 | [diff] [blame] | 136 |  | 
|  | 137 | # Remove "CBC" if it is not prefixed by DES | 
| Yanray Wang | ee97f05 | 2023-01-16 11:30:59 +0800 | [diff] [blame] | 138 | s_cipher = re.sub(r'(?<!DES-)CBC-', r'', s_cipher) | 
| Joe Subbiani | 3ad5832 | 2021-07-21 16:48:54 +0100 | [diff] [blame] | 139 |  | 
|  | 140 | # ECDHE-RSA-ARIA does not exist in OpenSSL | 
| Yanray Wang | ee97f05 | 2023-01-16 11:30:59 +0800 | [diff] [blame] | 141 | s_cipher = s_cipher.replace("ECDHE-RSA-ARIA", "ECDHE-ARIA") | 
| Joe Subbiani | 8394484 | 2021-07-20 18:26:03 +0100 | [diff] [blame] | 142 |  | 
| Joe Subbiani | 3ad5832 | 2021-07-21 16:48:54 +0100 | [diff] [blame] | 143 | # POLY1305 should not be followed by anything | 
| Yanray Wang | ee97f05 | 2023-01-16 11:30:59 +0800 | [diff] [blame] | 144 | if "POLY1305" in s_cipher: | 
|  | 145 | index = s_cipher.rindex("POLY1305") | 
|  | 146 | s_cipher = s_cipher[:index+8] | 
| Joe Subbiani | 3ad5832 | 2021-07-21 16:48:54 +0100 | [diff] [blame] | 147 |  | 
|  | 148 | # If DES is being used, Replace DHE with EDH | 
| Yanray Wang | ee97f05 | 2023-01-16 11:30:59 +0800 | [diff] [blame] | 149 | if "DES" in s_cipher and "DHE" in s_cipher and "ECDHE" not in s_cipher: | 
|  | 150 | s_cipher = s_cipher.replace("DHE", "EDH") | 
| Joe Subbiani | 8394484 | 2021-07-20 18:26:03 +0100 | [diff] [blame] | 151 |  | 
| Yanray Wang | ee97f05 | 2023-01-16 11:30:59 +0800 | [diff] [blame] | 152 | return s_cipher | 
|  | 153 |  | 
|  | 154 | def translate_mbedtls(s_cipher): | 
|  | 155 | """ | 
|  | 156 | Translate s_cipher from standard ciphersuite naming convention | 
|  | 157 | and return Mbed TLS ciphersuite naming convention | 
|  | 158 | """ | 
|  | 159 |  | 
|  | 160 | # Replace "_" with "-" | 
|  | 161 | s_cipher = s_cipher.replace("_", "-") | 
|  | 162 |  | 
|  | 163 | return s_cipher | 
| Joe Subbiani | 97cd599 | 2021-07-22 16:08:29 +0100 | [diff] [blame] | 164 |  | 
| Joe Subbiani | 918ee79 | 2021-07-30 16:57:04 +0100 | [diff] [blame] | 165 | def format_ciphersuite_names(mode, names): | 
| Yanray Wang | ee97f05 | 2023-01-16 11:30:59 +0800 | [diff] [blame] | 166 | t = {"g": translate_gnutls, | 
|  | 167 | "o": translate_ossl, | 
|  | 168 | "m": translate_mbedtls | 
|  | 169 | }[mode] | 
| Gilles Peskine | 47aab85 | 2023-01-26 21:16:34 +0100 | [diff] [blame] | 170 | return " ".join(c + '=' + t(c) for c in names) | 
| Joe Subbiani | 97cd599 | 2021-07-22 16:08:29 +0100 | [diff] [blame] | 171 |  | 
| Joe Subbiani | 918ee79 | 2021-07-30 16:57:04 +0100 | [diff] [blame] | 172 | def main(target, names): | 
|  | 173 | print(format_ciphersuite_names(target, names)) | 
| Joe Subbiani | 97cd599 | 2021-07-22 16:08:29 +0100 | [diff] [blame] | 174 |  | 
|  | 175 | if __name__ == "__main__": | 
| Joe Subbiani | 918ee79 | 2021-07-30 16:57:04 +0100 | [diff] [blame] | 176 | PARSER = argparse.ArgumentParser() | 
| Yanray Wang | ee97f05 | 2023-01-16 11:30:59 +0800 | [diff] [blame] | 177 | PARSER.add_argument('target', metavar='TARGET', choices=['o', 'g', 'm']) | 
| Joe Subbiani | 918ee79 | 2021-07-30 16:57:04 +0100 | [diff] [blame] | 178 | PARSER.add_argument('names', metavar='NAMES', nargs='+') | 
|  | 179 | ARGS = PARSER.parse_args() | 
|  | 180 | main(ARGS.target, ARGS.names) |