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 |
| 6 | # SPDX-License-Identifier: Apache-2.0 |
| 7 | # |
| 8 | # Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 9 | # not use this file except in compliance with the License. |
| 10 | # You may obtain a copy of the License at |
| 11 | # |
| 12 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 13 | # |
| 14 | # Unless required by applicable law or agreed to in writing, software |
| 15 | # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 16 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 17 | # See the License for the specific language governing permissions and |
| 18 | # limitations under the License. |
Joe Subbiani | f849a93 | 2021-07-28 16:50:30 +0100 | [diff] [blame] | 19 | |
| 20 | """ |
Joe Subbiani | b0aba9a | 2021-08-25 09:56:57 +0100 | [diff] [blame] | 21 | Translate ciphersuite names in Mbed TLS format to OpenSSL and GNUTLS |
Joe Subbiani | f849a93 | 2021-07-28 16:50:30 +0100 | [diff] [blame] | 22 | standards. |
| 23 | |
Joe Subbiani | a25ffab | 2021-08-06 09:41:27 +0100 | [diff] [blame] | 24 | To test the translation functions run: |
| 25 | python3 -m unittest translate_cipher.py |
Joe Subbiani | f849a93 | 2021-07-28 16:50:30 +0100 | [diff] [blame] | 26 | """ |
Joe Subbiani | a16ccac | 2021-07-22 18:52:17 +0100 | [diff] [blame] | 27 | |
Joe Subbiani | 3ad5832 | 2021-07-21 16:48:54 +0100 | [diff] [blame] | 28 | import re |
Joe Subbiani | 918ee79 | 2021-07-30 16:57:04 +0100 | [diff] [blame] | 29 | import argparse |
Joe Subbiani | a25ffab | 2021-08-06 09:41:27 +0100 | [diff] [blame] | 30 | import unittest |
| 31 | |
| 32 | class TestTranslateCiphers(unittest.TestCase): |
| 33 | """ |
| 34 | Ensure translate_ciphers.py translates and formats ciphersuite names |
| 35 | correctly |
| 36 | """ |
| 37 | def test_translate_all_cipher_names(self): |
| 38 | """ |
Joe Subbiani | 54110b3 | 2021-09-02 13:02:29 +0100 | [diff] [blame^] | 39 | Translate MbedTLS ciphersuite names to their OpenSSL and |
| 40 | GnuTLS counterpart. Use only a small subset of ciphers |
| 41 | that exercise each step of the translate functions |
Joe Subbiani | a25ffab | 2021-08-06 09:41:27 +0100 | [diff] [blame] | 42 | """ |
| 43 | ciphers = [ |
Joe Subbiani | 54110b3 | 2021-09-02 13:02:29 +0100 | [diff] [blame^] | 44 | ("TLS-ECDHE-ECDSA-WITH-NULL-SHA", |
| 45 | "+ECDHE-ECDSA:+NULL:+SHA1", |
| 46 | "ECDHE-ECDSA-NULL-SHA"), |
| 47 | ("TLS-ECDHE-ECDSA-WITH-AES-128-GCM-SHA256", |
| 48 | "+ECDHE-ECDSA:+AES-128-GCM:+AEAD", |
| 49 | "ECDHE-ECDSA-AES128-GCM-SHA256"), |
| 50 | ("TLS-DHE-RSA-WITH-3DES-EDE-CBC-SHA", |
| 51 | "+DHE-RSA:+3DES-CBC:+SHA1", |
| 52 | "EDH-RSA-DES-CBC3-SHA"), |
| 53 | ("TLS-ECDHE-ECDSA-WITH-CHACHA20-POLY1305-SHA256", |
| 54 | None, |
| 55 | "ECDHE-ECDSA-CHACHA20-POLY1305"), |
| 56 | ("TLS-ECDHE-ECDSA-WITH-AES-128-CCM", |
| 57 | "+ECDHE-ECDSA:+AES-128-CCM:+AEAD", |
| 58 | None), |
Joe Subbiani | a25ffab | 2021-08-06 09:41:27 +0100 | [diff] [blame] | 59 | ] |
| 60 | |
| 61 | for m, g_exp, o_exp in ciphers: |
| 62 | |
| 63 | if g_exp is not None: |
| 64 | g = translate_gnutls(m) |
| 65 | self.assertEqual(g, g_exp) |
| 66 | |
| 67 | if o_exp is not None: |
| 68 | o = translate_ossl(m) |
| 69 | self.assertEqual(o, o_exp) |
| 70 | |
Joe Subbiani | 0fadf8e | 2021-07-27 15:22:26 +0100 | [diff] [blame] | 71 | def translate_gnutls(m_cipher): |
Joe Subbiani | f849a93 | 2021-07-28 16:50:30 +0100 | [diff] [blame] | 72 | """ |
Joe Subbiani | b0aba9a | 2021-08-25 09:56:57 +0100 | [diff] [blame] | 73 | Translate m_cipher from Mbed TLS ciphersuite naming convention |
Joe Subbiani | f849a93 | 2021-07-28 16:50:30 +0100 | [diff] [blame] | 74 | and return the GnuTLS naming convention |
| 75 | """ |
| 76 | |
Joe Subbiani | 918ee79 | 2021-07-30 16:57:04 +0100 | [diff] [blame] | 77 | m_cipher = re.sub(r'\ATLS-', '+', m_cipher) |
Joe Subbiani | 8394484 | 2021-07-20 18:26:03 +0100 | [diff] [blame] | 78 | m_cipher = m_cipher.replace("-WITH-", ":+") |
| 79 | m_cipher = m_cipher.replace("-EDE", "") |
Joe Subbiani | 3ad5832 | 2021-07-21 16:48:54 +0100 | [diff] [blame] | 80 | |
Joe Subbiani | 918ee79 | 2021-07-30 16:57:04 +0100 | [diff] [blame] | 81 | # SHA in Mbed TLS == SHA1 GnuTLS, |
| 82 | # if the last 3 chars are SHA append 1 |
Joe Subbiani | 3ad5832 | 2021-07-21 16:48:54 +0100 | [diff] [blame] | 83 | if m_cipher[-3:] == "SHA": |
Joe Subbiani | 8394484 | 2021-07-20 18:26:03 +0100 | [diff] [blame] | 84 | m_cipher = m_cipher+"1" |
Joe Subbiani | 3ad5832 | 2021-07-21 16:48:54 +0100 | [diff] [blame] | 85 | |
| 86 | # CCM or CCM-8 should be followed by ":+AEAD" |
Joe Subbiani | 918ee79 | 2021-07-30 16:57:04 +0100 | [diff] [blame] | 87 | # Replace "GCM:+SHAxyz" with "GCM:+AEAD" |
| 88 | if "CCM" in m_cipher or "GCM" in m_cipher: |
| 89 | m_cipher = re.sub(r"GCM-SHA\d\d\d", "GCM", m_cipher) |
Joe Subbiani | 8394484 | 2021-07-20 18:26:03 +0100 | [diff] [blame] | 90 | m_cipher = m_cipher+":+AEAD" |
Joe Subbiani | 3ad5832 | 2021-07-21 16:48:54 +0100 | [diff] [blame] | 91 | |
| 92 | # Replace the last "-" with ":+" |
Joe Subbiani | 8394484 | 2021-07-20 18:26:03 +0100 | [diff] [blame] | 93 | else: |
Joe Subbiani | f849a93 | 2021-07-28 16:50:30 +0100 | [diff] [blame] | 94 | index = m_cipher.rindex("-") |
Joe Subbiani | 918ee79 | 2021-07-30 16:57:04 +0100 | [diff] [blame] | 95 | m_cipher = m_cipher[:index] + ":+" + m_cipher[index+1:] |
Joe Subbiani | 8394484 | 2021-07-20 18:26:03 +0100 | [diff] [blame] | 96 | |
| 97 | return m_cipher |
Joe Subbiani | 3ad5832 | 2021-07-21 16:48:54 +0100 | [diff] [blame] | 98 | |
Joe Subbiani | 8394484 | 2021-07-20 18:26:03 +0100 | [diff] [blame] | 99 | def translate_ossl(m_cipher): |
Joe Subbiani | f849a93 | 2021-07-28 16:50:30 +0100 | [diff] [blame] | 100 | """ |
Joe Subbiani | b0aba9a | 2021-08-25 09:56:57 +0100 | [diff] [blame] | 101 | Translate m_cipher from Mbed TLS ciphersuite naming convention |
Joe Subbiani | f849a93 | 2021-07-28 16:50:30 +0100 | [diff] [blame] | 102 | and return the OpenSSL naming convention |
| 103 | """ |
| 104 | |
Joe Subbiani | 918ee79 | 2021-07-30 16:57:04 +0100 | [diff] [blame] | 105 | m_cipher = re.sub(r'^TLS-', '', m_cipher) |
Joe Subbiani | 8394484 | 2021-07-20 18:26:03 +0100 | [diff] [blame] | 106 | m_cipher = m_cipher.replace("-WITH", "") |
Joe Subbiani | 3ad5832 | 2021-07-21 16:48:54 +0100 | [diff] [blame] | 107 | |
| 108 | # Remove the "-" from "ABC-xyz" |
Joe Subbiani | 8394484 | 2021-07-20 18:26:03 +0100 | [diff] [blame] | 109 | m_cipher = m_cipher.replace("AES-", "AES") |
| 110 | m_cipher = m_cipher.replace("CAMELLIA-", "CAMELLIA") |
| 111 | m_cipher = m_cipher.replace("ARIA-", "ARIA") |
Joe Subbiani | 8394484 | 2021-07-20 18:26:03 +0100 | [diff] [blame] | 112 | |
Joe Subbiani | 3ad5832 | 2021-07-21 16:48:54 +0100 | [diff] [blame] | 113 | # Remove "RSA" if it is at the beginning |
Joe Subbiani | 918ee79 | 2021-07-30 16:57:04 +0100 | [diff] [blame] | 114 | m_cipher = re.sub(r'^RSA-', r'', m_cipher) |
Joe Subbiani | 8394484 | 2021-07-20 18:26:03 +0100 | [diff] [blame] | 115 | |
Joe Subbiani | 3ad5832 | 2021-07-21 16:48:54 +0100 | [diff] [blame] | 116 | # For all circumstances outside of PSK |
| 117 | if "PSK" not in m_cipher: |
| 118 | m_cipher = m_cipher.replace("-EDE", "") |
| 119 | m_cipher = m_cipher.replace("3DES-CBC", "DES-CBC3") |
| 120 | |
| 121 | # Remove "CBC" if it is not prefixed by DES |
Joe Subbiani | 918ee79 | 2021-07-30 16:57:04 +0100 | [diff] [blame] | 122 | m_cipher = re.sub(r'(?<!DES-)CBC-', r'', m_cipher) |
Joe Subbiani | 3ad5832 | 2021-07-21 16:48:54 +0100 | [diff] [blame] | 123 | |
| 124 | # ECDHE-RSA-ARIA does not exist in OpenSSL |
Joe Subbiani | 8394484 | 2021-07-20 18:26:03 +0100 | [diff] [blame] | 125 | m_cipher = m_cipher.replace("ECDHE-RSA-ARIA", "ECDHE-ARIA") |
| 126 | |
Joe Subbiani | 3ad5832 | 2021-07-21 16:48:54 +0100 | [diff] [blame] | 127 | # POLY1305 should not be followed by anything |
| 128 | if "POLY1305" in m_cipher: |
Joe Subbiani | 8394484 | 2021-07-20 18:26:03 +0100 | [diff] [blame] | 129 | index = m_cipher.rindex("POLY1305") |
Joe Subbiani | f849a93 | 2021-07-28 16:50:30 +0100 | [diff] [blame] | 130 | m_cipher = m_cipher[:index+8] |
Joe Subbiani | 3ad5832 | 2021-07-21 16:48:54 +0100 | [diff] [blame] | 131 | |
| 132 | # If DES is being used, Replace DHE with EDH |
| 133 | if "DES" in m_cipher and "DHE" in m_cipher and "ECDHE" not in m_cipher: |
| 134 | m_cipher = m_cipher.replace("DHE", "EDH") |
Joe Subbiani | 8394484 | 2021-07-20 18:26:03 +0100 | [diff] [blame] | 135 | |
| 136 | return m_cipher |
Joe Subbiani | 97cd599 | 2021-07-22 16:08:29 +0100 | [diff] [blame] | 137 | |
Joe Subbiani | 918ee79 | 2021-07-30 16:57:04 +0100 | [diff] [blame] | 138 | def format_ciphersuite_names(mode, names): |
| 139 | t = {"g": translate_gnutls, "o": translate_ossl}[mode] |
| 140 | return " ".join(t(c) for c in names) |
Joe Subbiani | 97cd599 | 2021-07-22 16:08:29 +0100 | [diff] [blame] | 141 | |
Joe Subbiani | 918ee79 | 2021-07-30 16:57:04 +0100 | [diff] [blame] | 142 | def main(target, names): |
| 143 | print(format_ciphersuite_names(target, names)) |
Joe Subbiani | 97cd599 | 2021-07-22 16:08:29 +0100 | [diff] [blame] | 144 | |
| 145 | if __name__ == "__main__": |
Joe Subbiani | 918ee79 | 2021-07-30 16:57:04 +0100 | [diff] [blame] | 146 | PARSER = argparse.ArgumentParser() |
| 147 | PARSER.add_argument('target', metavar='TARGET', choices=['o', 'g']) |
| 148 | PARSER.add_argument('names', metavar='NAMES', nargs='+') |
| 149 | ARGS = PARSER.parse_args() |
| 150 | main(ARGS.target, ARGS.names) |