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 | """ |
| 21 | Translate ciphersuite names in MBedTLS format to OpenSSL and GNUTLS |
| 22 | standards. |
| 23 | |
Joe Subbiani | f849a93 | 2021-07-28 16:50:30 +0100 | [diff] [blame] | 24 | sys.argv[1] should be "g" or "o" for GNUTLS or OpenSSL. |
| 25 | sys.argv[2] should be a string containing one or more ciphersuite names. |
| 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 | 8394484 | 2021-07-20 18:26:03 +0100 | [diff] [blame] | 30 | |
Joe Subbiani | 0fadf8e | 2021-07-27 15:22:26 +0100 | [diff] [blame] | 31 | def translate_gnutls(m_cipher): |
Joe Subbiani | f849a93 | 2021-07-28 16:50:30 +0100 | [diff] [blame] | 32 | """ |
| 33 | Translate m_cipher from MBedTLS ciphersuite naming convention |
| 34 | and return the GnuTLS naming convention |
| 35 | """ |
| 36 | |
Joe Subbiani | 918ee79 | 2021-07-30 16:57:04 +0100 | [diff] [blame] | 37 | m_cipher = re.sub(r'\ATLS-', '+', m_cipher) |
Joe Subbiani | 8394484 | 2021-07-20 18:26:03 +0100 | [diff] [blame] | 38 | m_cipher = m_cipher.replace("-WITH-", ":+") |
| 39 | m_cipher = m_cipher.replace("-EDE", "") |
Joe Subbiani | 3ad5832 | 2021-07-21 16:48:54 +0100 | [diff] [blame] | 40 | |
Joe Subbiani | 918ee79 | 2021-07-30 16:57:04 +0100 | [diff] [blame] | 41 | # SHA in Mbed TLS == SHA1 GnuTLS, |
| 42 | # if the last 3 chars are SHA append 1 |
Joe Subbiani | 3ad5832 | 2021-07-21 16:48:54 +0100 | [diff] [blame] | 43 | if m_cipher[-3:] == "SHA": |
Joe Subbiani | 8394484 | 2021-07-20 18:26:03 +0100 | [diff] [blame] | 44 | m_cipher = m_cipher+"1" |
Joe Subbiani | 3ad5832 | 2021-07-21 16:48:54 +0100 | [diff] [blame] | 45 | |
| 46 | # CCM or CCM-8 should be followed by ":+AEAD" |
Joe Subbiani | 918ee79 | 2021-07-30 16:57:04 +0100 | [diff] [blame] | 47 | # Replace "GCM:+SHAxyz" with "GCM:+AEAD" |
| 48 | if "CCM" in m_cipher or "GCM" in m_cipher: |
| 49 | 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] | 50 | m_cipher = m_cipher+":+AEAD" |
Joe Subbiani | 3ad5832 | 2021-07-21 16:48:54 +0100 | [diff] [blame] | 51 | |
| 52 | # Replace the last "-" with ":+" |
Joe Subbiani | 8394484 | 2021-07-20 18:26:03 +0100 | [diff] [blame] | 53 | else: |
Joe Subbiani | f849a93 | 2021-07-28 16:50:30 +0100 | [diff] [blame] | 54 | index = m_cipher.rindex("-") |
Joe Subbiani | 918ee79 | 2021-07-30 16:57:04 +0100 | [diff] [blame] | 55 | m_cipher = m_cipher[:index] + ":+" + m_cipher[index+1:] |
Joe Subbiani | 8394484 | 2021-07-20 18:26:03 +0100 | [diff] [blame] | 56 | |
| 57 | return m_cipher |
Joe Subbiani | 3ad5832 | 2021-07-21 16:48:54 +0100 | [diff] [blame] | 58 | |
Joe Subbiani | 8394484 | 2021-07-20 18:26:03 +0100 | [diff] [blame] | 59 | def translate_ossl(m_cipher): |
Joe Subbiani | f849a93 | 2021-07-28 16:50:30 +0100 | [diff] [blame] | 60 | """ |
| 61 | Translate m_cipher from MBedTLS ciphersuite naming convention |
| 62 | and return the OpenSSL naming convention |
| 63 | """ |
| 64 | |
Joe Subbiani | 918ee79 | 2021-07-30 16:57:04 +0100 | [diff] [blame] | 65 | m_cipher = re.sub(r'^TLS-', '', m_cipher) |
Joe Subbiani | 8394484 | 2021-07-20 18:26:03 +0100 | [diff] [blame] | 66 | m_cipher = m_cipher.replace("-WITH", "") |
Joe Subbiani | 3ad5832 | 2021-07-21 16:48:54 +0100 | [diff] [blame] | 67 | |
| 68 | # Remove the "-" from "ABC-xyz" |
Joe Subbiani | 8394484 | 2021-07-20 18:26:03 +0100 | [diff] [blame] | 69 | m_cipher = m_cipher.replace("AES-", "AES") |
| 70 | m_cipher = m_cipher.replace("CAMELLIA-", "CAMELLIA") |
| 71 | m_cipher = m_cipher.replace("ARIA-", "ARIA") |
Joe Subbiani | 8394484 | 2021-07-20 18:26:03 +0100 | [diff] [blame] | 72 | |
Joe Subbiani | 3ad5832 | 2021-07-21 16:48:54 +0100 | [diff] [blame] | 73 | # Remove "RSA" if it is at the beginning |
Joe Subbiani | 918ee79 | 2021-07-30 16:57:04 +0100 | [diff] [blame] | 74 | m_cipher = re.sub(r'^RSA-', r'', m_cipher) |
Joe Subbiani | 8394484 | 2021-07-20 18:26:03 +0100 | [diff] [blame] | 75 | |
Joe Subbiani | 3ad5832 | 2021-07-21 16:48:54 +0100 | [diff] [blame] | 76 | # For all circumstances outside of PSK |
| 77 | if "PSK" not in m_cipher: |
| 78 | m_cipher = m_cipher.replace("-EDE", "") |
| 79 | m_cipher = m_cipher.replace("3DES-CBC", "DES-CBC3") |
| 80 | |
| 81 | # Remove "CBC" if it is not prefixed by DES |
Joe Subbiani | 918ee79 | 2021-07-30 16:57:04 +0100 | [diff] [blame] | 82 | m_cipher = re.sub(r'(?<!DES-)CBC-', r'', m_cipher) |
Joe Subbiani | 3ad5832 | 2021-07-21 16:48:54 +0100 | [diff] [blame] | 83 | |
| 84 | # ECDHE-RSA-ARIA does not exist in OpenSSL |
Joe Subbiani | 8394484 | 2021-07-20 18:26:03 +0100 | [diff] [blame] | 85 | m_cipher = m_cipher.replace("ECDHE-RSA-ARIA", "ECDHE-ARIA") |
| 86 | |
Joe Subbiani | 3ad5832 | 2021-07-21 16:48:54 +0100 | [diff] [blame] | 87 | # POLY1305 should not be followed by anything |
| 88 | if "POLY1305" in m_cipher: |
Joe Subbiani | 8394484 | 2021-07-20 18:26:03 +0100 | [diff] [blame] | 89 | index = m_cipher.rindex("POLY1305") |
Joe Subbiani | f849a93 | 2021-07-28 16:50:30 +0100 | [diff] [blame] | 90 | m_cipher = m_cipher[:index+8] |
Joe Subbiani | 3ad5832 | 2021-07-21 16:48:54 +0100 | [diff] [blame] | 91 | |
| 92 | # If DES is being used, Replace DHE with EDH |
| 93 | if "DES" in m_cipher and "DHE" in m_cipher and "ECDHE" not in m_cipher: |
| 94 | m_cipher = m_cipher.replace("DHE", "EDH") |
Joe Subbiani | 8394484 | 2021-07-20 18:26:03 +0100 | [diff] [blame] | 95 | |
| 96 | return m_cipher |
Joe Subbiani | 97cd599 | 2021-07-22 16:08:29 +0100 | [diff] [blame] | 97 | |
Joe Subbiani | 918ee79 | 2021-07-30 16:57:04 +0100 | [diff] [blame] | 98 | def format_ciphersuite_names(mode, names): |
| 99 | t = {"g": translate_gnutls, "o": translate_ossl}[mode] |
| 100 | return " ".join(t(c) for c in names) |
Joe Subbiani | 97cd599 | 2021-07-22 16:08:29 +0100 | [diff] [blame] | 101 | |
Joe Subbiani | 918ee79 | 2021-07-30 16:57:04 +0100 | [diff] [blame] | 102 | def main(target, names): |
| 103 | print(format_ciphersuite_names(target, names)) |
Joe Subbiani | 97cd599 | 2021-07-22 16:08:29 +0100 | [diff] [blame] | 104 | |
| 105 | if __name__ == "__main__": |
Joe Subbiani | 918ee79 | 2021-07-30 16:57:04 +0100 | [diff] [blame] | 106 | PARSER = argparse.ArgumentParser() |
| 107 | PARSER.add_argument('target', metavar='TARGET', choices=['o', 'g']) |
| 108 | PARSER.add_argument('names', metavar='NAMES', nargs='+') |
| 109 | ARGS = PARSER.parse_args() |
| 110 | main(ARGS.target, ARGS.names) |