Joe Subbiani | 3ad5832 | 2021-07-21 16:48:54 +0100 | [diff] [blame] | 1 | import re |
Joe Subbiani | 97cd599 | 2021-07-22 16:08:29 +0100 | [diff] [blame^] | 2 | import sys |
Joe Subbiani | 8394484 | 2021-07-20 18:26:03 +0100 | [diff] [blame] | 3 | |
| 4 | def translate_gnu(m_cipher): |
Joe Subbiani | 3ad5832 | 2021-07-21 16:48:54 +0100 | [diff] [blame] | 5 | # Remove "TLS-" |
| 6 | # Replace "-WITH-" with ":+" |
| 7 | # Remove "EDE" |
Joe Subbiani | 8394484 | 2021-07-20 18:26:03 +0100 | [diff] [blame] | 8 | m_cipher = "+" + m_cipher[4:] |
| 9 | m_cipher = m_cipher.replace("-WITH-", ":+") |
| 10 | m_cipher = m_cipher.replace("-EDE", "") |
Joe Subbiani | 3ad5832 | 2021-07-21 16:48:54 +0100 | [diff] [blame] | 11 | |
| 12 | # SHA == SHA1, if the last 3 chars are SHA append 1 |
| 13 | if m_cipher[-3:] == "SHA": |
Joe Subbiani | 8394484 | 2021-07-20 18:26:03 +0100 | [diff] [blame] | 14 | m_cipher = m_cipher+"1" |
Joe Subbiani | 3ad5832 | 2021-07-21 16:48:54 +0100 | [diff] [blame] | 15 | |
| 16 | # CCM or CCM-8 should be followed by ":+AEAD" |
| 17 | if "CCM" in m_cipher: |
Joe Subbiani | 8394484 | 2021-07-20 18:26:03 +0100 | [diff] [blame] | 18 | m_cipher = m_cipher+":+AEAD" |
Joe Subbiani | 3ad5832 | 2021-07-21 16:48:54 +0100 | [diff] [blame] | 19 | |
| 20 | # Replace the last "-" with ":+" |
| 21 | # Replace "GCM:+SHAxyz" with "GCM:+AEAD" |
Joe Subbiani | 8394484 | 2021-07-20 18:26:03 +0100 | [diff] [blame] | 22 | else: |
| 23 | index=m_cipher.rindex("-") |
| 24 | m_cipher = m_cipher[:index]+":+"+m_cipher[index+1:] |
Joe Subbiani | 3ad5832 | 2021-07-21 16:48:54 +0100 | [diff] [blame] | 25 | 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] | 26 | |
| 27 | return m_cipher |
Joe Subbiani | 3ad5832 | 2021-07-21 16:48:54 +0100 | [diff] [blame] | 28 | |
Joe Subbiani | 8394484 | 2021-07-20 18:26:03 +0100 | [diff] [blame] | 29 | def translate_ossl(m_cipher): |
Joe Subbiani | 3ad5832 | 2021-07-21 16:48:54 +0100 | [diff] [blame] | 30 | # Remove "TLS-" |
| 31 | # Remove "WITH" |
Joe Subbiani | 8394484 | 2021-07-20 18:26:03 +0100 | [diff] [blame] | 32 | m_cipher = m_cipher[4:] |
| 33 | m_cipher = m_cipher.replace("-WITH", "") |
Joe Subbiani | 3ad5832 | 2021-07-21 16:48:54 +0100 | [diff] [blame] | 34 | |
| 35 | # Remove the "-" from "ABC-xyz" |
Joe Subbiani | 8394484 | 2021-07-20 18:26:03 +0100 | [diff] [blame] | 36 | m_cipher = m_cipher.replace("AES-", "AES") |
| 37 | m_cipher = m_cipher.replace("CAMELLIA-", "CAMELLIA") |
| 38 | m_cipher = m_cipher.replace("ARIA-", "ARIA") |
Joe Subbiani | 8394484 | 2021-07-20 18:26:03 +0100 | [diff] [blame] | 39 | |
Joe Subbiani | 3ad5832 | 2021-07-21 16:48:54 +0100 | [diff] [blame] | 40 | # Remove "RSA" if it is at the beginning |
Joe Subbiani | 8394484 | 2021-07-20 18:26:03 +0100 | [diff] [blame] | 41 | if m_cipher[:4] == "RSA-": |
| 42 | m_cipher = m_cipher[4:] |
| 43 | |
Joe Subbiani | 3ad5832 | 2021-07-21 16:48:54 +0100 | [diff] [blame] | 44 | # For all circumstances outside of PSK |
| 45 | if "PSK" not in m_cipher: |
| 46 | m_cipher = m_cipher.replace("-EDE", "") |
| 47 | m_cipher = m_cipher.replace("3DES-CBC", "DES-CBC3") |
| 48 | |
| 49 | # Remove "CBC" if it is not prefixed by DES |
| 50 | if "CBC" in m_cipher: |
| 51 | index = m_cipher.rindex("CBC") |
| 52 | if m_cipher[index-4:index-1] != "DES": |
| 53 | m_cipher = m_cipher.replace("CBC-", "") |
| 54 | |
| 55 | # ECDHE-RSA-ARIA does not exist in OpenSSL |
Joe Subbiani | 8394484 | 2021-07-20 18:26:03 +0100 | [diff] [blame] | 56 | m_cipher = m_cipher.replace("ECDHE-RSA-ARIA", "ECDHE-ARIA") |
| 57 | |
Joe Subbiani | 3ad5832 | 2021-07-21 16:48:54 +0100 | [diff] [blame] | 58 | # POLY1305 should not be followed by anything |
| 59 | if "POLY1305" in m_cipher: |
Joe Subbiani | 8394484 | 2021-07-20 18:26:03 +0100 | [diff] [blame] | 60 | index = m_cipher.rindex("POLY1305") |
| 61 | m_cipher=m_cipher[:index+8] |
Joe Subbiani | 3ad5832 | 2021-07-21 16:48:54 +0100 | [diff] [blame] | 62 | |
| 63 | # If DES is being used, Replace DHE with EDH |
| 64 | if "DES" in m_cipher and "DHE" in m_cipher and "ECDHE" not in m_cipher: |
| 65 | m_cipher = m_cipher.replace("DHE", "EDH") |
Joe Subbiani | 8394484 | 2021-07-20 18:26:03 +0100 | [diff] [blame] | 66 | |
| 67 | return m_cipher |
Joe Subbiani | 97cd599 | 2021-07-22 16:08:29 +0100 | [diff] [blame^] | 68 | |
| 69 | def format_g(m_ciphers): |
| 70 | #ciphers = (re.findall(r"TLS-.+\s*\\", m_ciphers)) |
| 71 | m_ciphers = m_ciphers.split() |
| 72 | g_ciphers = [] |
| 73 | for i in m_ciphers: |
| 74 | g_ciphers.append(translate_gnu(i)) |
| 75 | return " ".join(g_ciphers) |
| 76 | |
| 77 | def format_o(m_ciphers): |
| 78 | m_ciphers = m_ciphers.split() |
| 79 | o_ciphers = [] |
| 80 | for i in m_ciphers: |
| 81 | o_ciphers.append(translate_ossl(i)) |
| 82 | return " ".join(o_ciphers) |
| 83 | |
| 84 | def main(): |
| 85 | # print command line arguments |
| 86 | if len(sys.argv) <= 2: |
| 87 | exit(1) |
| 88 | if sys.argv[1] == "g": |
| 89 | print(format_g(sys.argv[2])) |
| 90 | exit(0) |
| 91 | elif sys.argv[1] == "o": |
| 92 | print(format_o(sys.argv[2])) |
| 93 | exit(0) |
| 94 | else: |
| 95 | exit(1) |
| 96 | |
| 97 | if __name__ == "__main__": |
| 98 | main() |