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 | |
| 24 | Format and analyse strings past in via input arguments to match |
| 25 | the expected strings utilised in compat.sh. |
| 26 | |
| 27 | sys.argv[1] should be "g" or "o" for GNUTLS or OpenSSL. |
| 28 | sys.argv[2] should be a string containing one or more ciphersuite names. |
| 29 | """ |
Joe Subbiani | a16ccac | 2021-07-22 18:52:17 +0100 | [diff] [blame] | 30 | |
Joe Subbiani | 3ad5832 | 2021-07-21 16:48:54 +0100 | [diff] [blame] | 31 | import re |
Joe Subbiani | 97cd599 | 2021-07-22 16:08:29 +0100 | [diff] [blame] | 32 | import sys |
Joe Subbiani | 8394484 | 2021-07-20 18:26:03 +0100 | [diff] [blame] | 33 | |
Joe Subbiani | 0fadf8e | 2021-07-27 15:22:26 +0100 | [diff] [blame] | 34 | def translate_gnutls(m_cipher): |
Joe Subbiani | f849a93 | 2021-07-28 16:50:30 +0100 | [diff] [blame^] | 35 | """ |
| 36 | Translate m_cipher from MBedTLS ciphersuite naming convention |
| 37 | and return the GnuTLS naming convention |
| 38 | """ |
| 39 | |
Joe Subbiani | 3ad5832 | 2021-07-21 16:48:54 +0100 | [diff] [blame] | 40 | # Remove "TLS-" |
| 41 | # Replace "-WITH-" with ":+" |
| 42 | # Remove "EDE" |
Joe Subbiani | 8394484 | 2021-07-20 18:26:03 +0100 | [diff] [blame] | 43 | m_cipher = "+" + m_cipher[4:] |
| 44 | m_cipher = m_cipher.replace("-WITH-", ":+") |
| 45 | m_cipher = m_cipher.replace("-EDE", "") |
Joe Subbiani | 3ad5832 | 2021-07-21 16:48:54 +0100 | [diff] [blame] | 46 | |
| 47 | # SHA == SHA1, if the last 3 chars are SHA append 1 |
| 48 | if m_cipher[-3:] == "SHA": |
Joe Subbiani | 8394484 | 2021-07-20 18:26:03 +0100 | [diff] [blame] | 49 | m_cipher = m_cipher+"1" |
Joe Subbiani | 3ad5832 | 2021-07-21 16:48:54 +0100 | [diff] [blame] | 50 | |
| 51 | # CCM or CCM-8 should be followed by ":+AEAD" |
| 52 | if "CCM" in m_cipher: |
Joe Subbiani | 8394484 | 2021-07-20 18:26:03 +0100 | [diff] [blame] | 53 | m_cipher = m_cipher+":+AEAD" |
Joe Subbiani | 3ad5832 | 2021-07-21 16:48:54 +0100 | [diff] [blame] | 54 | |
| 55 | # Replace the last "-" with ":+" |
| 56 | # Replace "GCM:+SHAxyz" with "GCM:+AEAD" |
Joe Subbiani | 8394484 | 2021-07-20 18:26:03 +0100 | [diff] [blame] | 57 | else: |
Joe Subbiani | f849a93 | 2021-07-28 16:50:30 +0100 | [diff] [blame^] | 58 | index = m_cipher.rindex("-") |
Joe Subbiani | 8394484 | 2021-07-20 18:26:03 +0100 | [diff] [blame] | 59 | m_cipher = m_cipher[:index]+":+"+m_cipher[index+1:] |
Joe Subbiani | 3ad5832 | 2021-07-21 16:48:54 +0100 | [diff] [blame] | 60 | m_cipher = re.sub(r"GCM\:\+SHA\d\d\d", "GCM:+AEAD", m_cipher) |
Joe Subbiani | 8394484 | 2021-07-20 18:26:03 +0100 | [diff] [blame] | 61 | |
| 62 | return m_cipher |
Joe Subbiani | 3ad5832 | 2021-07-21 16:48:54 +0100 | [diff] [blame] | 63 | |
Joe Subbiani | 8394484 | 2021-07-20 18:26:03 +0100 | [diff] [blame] | 64 | def translate_ossl(m_cipher): |
Joe Subbiani | f849a93 | 2021-07-28 16:50:30 +0100 | [diff] [blame^] | 65 | """ |
| 66 | Translate m_cipher from MBedTLS ciphersuite naming convention |
| 67 | and return the OpenSSL naming convention |
| 68 | """ |
| 69 | |
Joe Subbiani | 3ad5832 | 2021-07-21 16:48:54 +0100 | [diff] [blame] | 70 | # Remove "TLS-" |
| 71 | # Remove "WITH" |
Joe Subbiani | 8394484 | 2021-07-20 18:26:03 +0100 | [diff] [blame] | 72 | m_cipher = m_cipher[4:] |
| 73 | m_cipher = m_cipher.replace("-WITH", "") |
Joe Subbiani | 3ad5832 | 2021-07-21 16:48:54 +0100 | [diff] [blame] | 74 | |
| 75 | # Remove the "-" from "ABC-xyz" |
Joe Subbiani | 8394484 | 2021-07-20 18:26:03 +0100 | [diff] [blame] | 76 | m_cipher = m_cipher.replace("AES-", "AES") |
| 77 | m_cipher = m_cipher.replace("CAMELLIA-", "CAMELLIA") |
| 78 | m_cipher = m_cipher.replace("ARIA-", "ARIA") |
Joe Subbiani | 8394484 | 2021-07-20 18:26:03 +0100 | [diff] [blame] | 79 | |
Joe Subbiani | 3ad5832 | 2021-07-21 16:48:54 +0100 | [diff] [blame] | 80 | # Remove "RSA" if it is at the beginning |
Joe Subbiani | 8394484 | 2021-07-20 18:26:03 +0100 | [diff] [blame] | 81 | if m_cipher[:4] == "RSA-": |
| 82 | m_cipher = m_cipher[4:] |
| 83 | |
Joe Subbiani | 3ad5832 | 2021-07-21 16:48:54 +0100 | [diff] [blame] | 84 | # For all circumstances outside of PSK |
| 85 | if "PSK" not in m_cipher: |
| 86 | m_cipher = m_cipher.replace("-EDE", "") |
| 87 | m_cipher = m_cipher.replace("3DES-CBC", "DES-CBC3") |
| 88 | |
| 89 | # Remove "CBC" if it is not prefixed by DES |
| 90 | if "CBC" in m_cipher: |
| 91 | index = m_cipher.rindex("CBC") |
| 92 | if m_cipher[index-4:index-1] != "DES": |
| 93 | m_cipher = m_cipher.replace("CBC-", "") |
| 94 | |
| 95 | # ECDHE-RSA-ARIA does not exist in OpenSSL |
Joe Subbiani | 8394484 | 2021-07-20 18:26:03 +0100 | [diff] [blame] | 96 | m_cipher = m_cipher.replace("ECDHE-RSA-ARIA", "ECDHE-ARIA") |
| 97 | |
Joe Subbiani | 3ad5832 | 2021-07-21 16:48:54 +0100 | [diff] [blame] | 98 | # POLY1305 should not be followed by anything |
| 99 | if "POLY1305" in m_cipher: |
Joe Subbiani | 8394484 | 2021-07-20 18:26:03 +0100 | [diff] [blame] | 100 | index = m_cipher.rindex("POLY1305") |
Joe Subbiani | f849a93 | 2021-07-28 16:50:30 +0100 | [diff] [blame^] | 101 | m_cipher = m_cipher[:index+8] |
Joe Subbiani | 3ad5832 | 2021-07-21 16:48:54 +0100 | [diff] [blame] | 102 | |
| 103 | # If DES is being used, Replace DHE with EDH |
| 104 | if "DES" in m_cipher and "DHE" in m_cipher and "ECDHE" not in m_cipher: |
| 105 | m_cipher = m_cipher.replace("DHE", "EDH") |
Joe Subbiani | 8394484 | 2021-07-20 18:26:03 +0100 | [diff] [blame] | 106 | |
| 107 | return m_cipher |
Joe Subbiani | 97cd599 | 2021-07-22 16:08:29 +0100 | [diff] [blame] | 108 | |
Joe Subbiani | 0fadf8e | 2021-07-27 15:22:26 +0100 | [diff] [blame] | 109 | def format_ciphersuite_names(mode, ciphers): |
Joe Subbiani | 0fadf8e | 2021-07-27 15:22:26 +0100 | [diff] [blame] | 110 | try: |
| 111 | t = {"g": translate_gnutls, "o": translate_ossl}[mode] |
| 112 | return " ".join(t(c) for c in ciphers.split()) |
Joe Subbiani | f849a93 | 2021-07-28 16:50:30 +0100 | [diff] [blame^] | 113 | except (KeyError) as e: |
| 114 | print(e) |
| 115 | print("Incorrect use of argument 1, should be either \"g\" or \"o\"") |
Joe Subbiani | 0fadf8e | 2021-07-27 15:22:26 +0100 | [diff] [blame] | 116 | sys.exit(1) |
Joe Subbiani | 97cd599 | 2021-07-22 16:08:29 +0100 | [diff] [blame] | 117 | |
| 118 | def main(): |
Joe Subbiani | 0fadf8e | 2021-07-27 15:22:26 +0100 | [diff] [blame] | 119 | if len(sys.argv) != 3: |
Joe Subbiani | 43592bd | 2021-07-27 16:32:21 +0100 | [diff] [blame] | 120 | print("""Incorrect number of arguments. |
Joe Subbiani | 0fadf8e | 2021-07-27 15:22:26 +0100 | [diff] [blame] | 121 | The first argument with either an \"o\" for OpenSSL or \"g\" for GNUTLS. |
| 122 | The second argument should a single space seperated string of MBedTLS ciphersuite names""") |
| 123 | sys.exit(1) |
| 124 | print(format_ciphersuite_names(sys.argv[1], sys.argv[2])) |
| 125 | sys.exit(0) |
Joe Subbiani | 97cd599 | 2021-07-22 16:08:29 +0100 | [diff] [blame] | 126 | |
| 127 | if __name__ == "__main__": |
Joe Subbiani | a16ccac | 2021-07-22 18:52:17 +0100 | [diff] [blame] | 128 | main() |