blob: eec340735bf54f2e10818f16016c2071bf0342ec [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
Joe Subbianif849a932021-07-28 16:50:30 +010024sys.argv[1] should be "g" or "o" for GNUTLS or OpenSSL.
25sys.argv[2] should be a string containing one or more ciphersuite names.
26"""
Joe Subbiania16ccac2021-07-22 18:52:17 +010027
Joe Subbiani3ad58322021-07-21 16:48:54 +010028import re
Joe Subbiani918ee792021-07-30 16:57:04 +010029import argparse
Joe Subbiani83944842021-07-20 18:26:03 +010030
Joe Subbiani0fadf8e2021-07-27 15:22:26 +010031def translate_gnutls(m_cipher):
Joe Subbianif849a932021-07-28 16:50:30 +010032 """
33 Translate m_cipher from MBedTLS ciphersuite naming convention
34 and return the GnuTLS naming convention
35 """
36
Joe Subbiani918ee792021-07-30 16:57:04 +010037 m_cipher = re.sub(r'\ATLS-', '+', m_cipher)
Joe Subbiani83944842021-07-20 18:26:03 +010038 m_cipher = m_cipher.replace("-WITH-", ":+")
39 m_cipher = m_cipher.replace("-EDE", "")
Joe Subbiani3ad58322021-07-21 16:48:54 +010040
Joe Subbiani918ee792021-07-30 16:57:04 +010041 # SHA in Mbed TLS == SHA1 GnuTLS,
42 # if the last 3 chars are SHA append 1
Joe Subbiani3ad58322021-07-21 16:48:54 +010043 if m_cipher[-3:] == "SHA":
Joe Subbiani83944842021-07-20 18:26:03 +010044 m_cipher = m_cipher+"1"
Joe Subbiani3ad58322021-07-21 16:48:54 +010045
46 # CCM or CCM-8 should be followed by ":+AEAD"
Joe Subbiani918ee792021-07-30 16:57:04 +010047 # 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 Subbiani83944842021-07-20 18:26:03 +010050 m_cipher = m_cipher+":+AEAD"
Joe Subbiani3ad58322021-07-21 16:48:54 +010051
52 # Replace the last "-" with ":+"
Joe Subbiani83944842021-07-20 18:26:03 +010053 else:
Joe Subbianif849a932021-07-28 16:50:30 +010054 index = m_cipher.rindex("-")
Joe Subbiani918ee792021-07-30 16:57:04 +010055 m_cipher = m_cipher[:index] + ":+" + m_cipher[index+1:]
Joe Subbiani83944842021-07-20 18:26:03 +010056
57 return m_cipher
Joe Subbiani3ad58322021-07-21 16:48:54 +010058
Joe Subbiani83944842021-07-20 18:26:03 +010059def translate_ossl(m_cipher):
Joe Subbianif849a932021-07-28 16:50:30 +010060 """
61 Translate m_cipher from MBedTLS ciphersuite naming convention
62 and return the OpenSSL naming convention
63 """
64
Joe Subbiani918ee792021-07-30 16:57:04 +010065 m_cipher = re.sub(r'^TLS-', '', m_cipher)
Joe Subbiani83944842021-07-20 18:26:03 +010066 m_cipher = m_cipher.replace("-WITH", "")
Joe Subbiani3ad58322021-07-21 16:48:54 +010067
68 # Remove the "-" from "ABC-xyz"
Joe Subbiani83944842021-07-20 18:26:03 +010069 m_cipher = m_cipher.replace("AES-", "AES")
70 m_cipher = m_cipher.replace("CAMELLIA-", "CAMELLIA")
71 m_cipher = m_cipher.replace("ARIA-", "ARIA")
Joe Subbiani83944842021-07-20 18:26:03 +010072
Joe Subbiani3ad58322021-07-21 16:48:54 +010073 # Remove "RSA" if it is at the beginning
Joe Subbiani918ee792021-07-30 16:57:04 +010074 m_cipher = re.sub(r'^RSA-', r'', m_cipher)
Joe Subbiani83944842021-07-20 18:26:03 +010075
Joe Subbiani3ad58322021-07-21 16:48:54 +010076 # 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 Subbiani918ee792021-07-30 16:57:04 +010082 m_cipher = re.sub(r'(?<!DES-)CBC-', r'', m_cipher)
Joe Subbiani3ad58322021-07-21 16:48:54 +010083
84 # ECDHE-RSA-ARIA does not exist in OpenSSL
Joe Subbiani83944842021-07-20 18:26:03 +010085 m_cipher = m_cipher.replace("ECDHE-RSA-ARIA", "ECDHE-ARIA")
86
Joe Subbiani3ad58322021-07-21 16:48:54 +010087 # POLY1305 should not be followed by anything
88 if "POLY1305" in m_cipher:
Joe Subbiani83944842021-07-20 18:26:03 +010089 index = m_cipher.rindex("POLY1305")
Joe Subbianif849a932021-07-28 16:50:30 +010090 m_cipher = m_cipher[:index+8]
Joe Subbiani3ad58322021-07-21 16:48:54 +010091
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 Subbiani83944842021-07-20 18:26:03 +010095
96 return m_cipher
Joe Subbiani97cd5992021-07-22 16:08:29 +010097
Joe Subbiani918ee792021-07-30 16:57:04 +010098def format_ciphersuite_names(mode, names):
99 t = {"g": translate_gnutls, "o": translate_ossl}[mode]
100 return " ".join(t(c) for c in names)
Joe Subbiani97cd5992021-07-22 16:08:29 +0100101
Joe Subbiani918ee792021-07-30 16:57:04 +0100102def main(target, names):
103 print(format_ciphersuite_names(target, names))
Joe Subbiani97cd5992021-07-22 16:08:29 +0100104
105if __name__ == "__main__":
Joe Subbiani918ee792021-07-30 16:57:04 +0100106 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)