blob: 66c878ac39b593b465b3783afbfc0676679ba356 [file] [log] [blame]
Joe Subbiania16ccac2021-07-22 18:52:17 +01001#!/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 Subbianif849a932021-07-28 16:50:30 +010019
20"""
21Translate ciphersuite names in MBedTLS format to OpenSSL and GNUTLS
22standards.
23
24Format and analyse strings past in via input arguments to match
25the expected strings utilised in compat.sh.
26
27sys.argv[1] should be "g" or "o" for GNUTLS or OpenSSL.
28sys.argv[2] should be a string containing one or more ciphersuite names.
29"""
Joe Subbiania16ccac2021-07-22 18:52:17 +010030
Joe Subbiani3ad58322021-07-21 16:48:54 +010031import re
Joe Subbiani97cd5992021-07-22 16:08:29 +010032import sys
Joe Subbiani83944842021-07-20 18:26:03 +010033
Joe Subbiani0fadf8e2021-07-27 15:22:26 +010034def translate_gnutls(m_cipher):
Joe Subbianif849a932021-07-28 16:50:30 +010035 """
36 Translate m_cipher from MBedTLS ciphersuite naming convention
37 and return the GnuTLS naming convention
38 """
39
Joe Subbiani3ad58322021-07-21 16:48:54 +010040 # Remove "TLS-"
41 # Replace "-WITH-" with ":+"
42 # Remove "EDE"
Joe Subbiani83944842021-07-20 18:26:03 +010043 m_cipher = "+" + m_cipher[4:]
44 m_cipher = m_cipher.replace("-WITH-", ":+")
45 m_cipher = m_cipher.replace("-EDE", "")
Joe Subbiani3ad58322021-07-21 16:48:54 +010046
47 # SHA == SHA1, if the last 3 chars are SHA append 1
48 if m_cipher[-3:] == "SHA":
Joe Subbiani83944842021-07-20 18:26:03 +010049 m_cipher = m_cipher+"1"
Joe Subbiani3ad58322021-07-21 16:48:54 +010050
51 # CCM or CCM-8 should be followed by ":+AEAD"
52 if "CCM" in m_cipher:
Joe Subbiani83944842021-07-20 18:26:03 +010053 m_cipher = m_cipher+":+AEAD"
Joe Subbiani3ad58322021-07-21 16:48:54 +010054
55 # Replace the last "-" with ":+"
56 # Replace "GCM:+SHAxyz" with "GCM:+AEAD"
Joe Subbiani83944842021-07-20 18:26:03 +010057 else:
Joe Subbianif849a932021-07-28 16:50:30 +010058 index = m_cipher.rindex("-")
Joe Subbiani83944842021-07-20 18:26:03 +010059 m_cipher = m_cipher[:index]+":+"+m_cipher[index+1:]
Joe Subbiani3ad58322021-07-21 16:48:54 +010060 m_cipher = re.sub(r"GCM\:\+SHA\d\d\d", "GCM:+AEAD", m_cipher)
Joe Subbiani83944842021-07-20 18:26:03 +010061
62 return m_cipher
Joe Subbiani3ad58322021-07-21 16:48:54 +010063
Joe Subbiani83944842021-07-20 18:26:03 +010064def translate_ossl(m_cipher):
Joe Subbianif849a932021-07-28 16:50:30 +010065 """
66 Translate m_cipher from MBedTLS ciphersuite naming convention
67 and return the OpenSSL naming convention
68 """
69
Joe Subbiani3ad58322021-07-21 16:48:54 +010070 # Remove "TLS-"
71 # Remove "WITH"
Joe Subbiani83944842021-07-20 18:26:03 +010072 m_cipher = m_cipher[4:]
73 m_cipher = m_cipher.replace("-WITH", "")
Joe Subbiani3ad58322021-07-21 16:48:54 +010074
75 # Remove the "-" from "ABC-xyz"
Joe Subbiani83944842021-07-20 18:26:03 +010076 m_cipher = m_cipher.replace("AES-", "AES")
77 m_cipher = m_cipher.replace("CAMELLIA-", "CAMELLIA")
78 m_cipher = m_cipher.replace("ARIA-", "ARIA")
Joe Subbiani83944842021-07-20 18:26:03 +010079
Joe Subbiani3ad58322021-07-21 16:48:54 +010080 # Remove "RSA" if it is at the beginning
Joe Subbiani83944842021-07-20 18:26:03 +010081 if m_cipher[:4] == "RSA-":
82 m_cipher = m_cipher[4:]
83
Joe Subbiani3ad58322021-07-21 16:48:54 +010084 # 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 Subbiani83944842021-07-20 18:26:03 +010096 m_cipher = m_cipher.replace("ECDHE-RSA-ARIA", "ECDHE-ARIA")
97
Joe Subbiani3ad58322021-07-21 16:48:54 +010098 # POLY1305 should not be followed by anything
99 if "POLY1305" in m_cipher:
Joe Subbiani83944842021-07-20 18:26:03 +0100100 index = m_cipher.rindex("POLY1305")
Joe Subbianif849a932021-07-28 16:50:30 +0100101 m_cipher = m_cipher[:index+8]
Joe Subbiani3ad58322021-07-21 16:48:54 +0100102
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 Subbiani83944842021-07-20 18:26:03 +0100106
107 return m_cipher
Joe Subbiani97cd5992021-07-22 16:08:29 +0100108
Joe Subbiani0fadf8e2021-07-27 15:22:26 +0100109def format_ciphersuite_names(mode, ciphers):
Joe Subbiani0fadf8e2021-07-27 15:22:26 +0100110 try:
111 t = {"g": translate_gnutls, "o": translate_ossl}[mode]
112 return " ".join(t(c) for c in ciphers.split())
Joe Subbianif849a932021-07-28 16:50:30 +0100113 except (KeyError) as e:
114 print(e)
115 print("Incorrect use of argument 1, should be either \"g\" or \"o\"")
Joe Subbiani0fadf8e2021-07-27 15:22:26 +0100116 sys.exit(1)
Joe Subbiani97cd5992021-07-22 16:08:29 +0100117
118def main():
Joe Subbiani0fadf8e2021-07-27 15:22:26 +0100119 if len(sys.argv) != 3:
Joe Subbiani43592bd2021-07-27 16:32:21 +0100120 print("""Incorrect number of arguments.
Joe Subbiani0fadf8e2021-07-27 15:22:26 +0100121The first argument with either an \"o\" for OpenSSL or \"g\" for GNUTLS.
122The 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 Subbiani97cd5992021-07-22 16:08:29 +0100126
127if __name__ == "__main__":
Joe Subbiania16ccac2021-07-22 18:52:17 +0100128 main()