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