blob: 39339c3d23a6a4bc6d1bc9d696682ee1ff619e89 [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 Subbiani97cd5992021-07-22 16:08:29 +010029import sys
Joe Subbiani918ee792021-07-30 16:57:04 +010030import argparse
Joe Subbiani83944842021-07-20 18:26:03 +010031
Joe Subbiani0fadf8e2021-07-27 15:22:26 +010032def translate_gnutls(m_cipher):
Joe Subbianif849a932021-07-28 16:50:30 +010033 """
34 Translate m_cipher from MBedTLS ciphersuite naming convention
35 and return the GnuTLS naming convention
36 """
37
Joe Subbiani918ee792021-07-30 16:57:04 +010038 m_cipher = re.sub(r'\ATLS-', '+', m_cipher)
Joe Subbiani83944842021-07-20 18:26:03 +010039 m_cipher = m_cipher.replace("-WITH-", ":+")
40 m_cipher = m_cipher.replace("-EDE", "")
Joe Subbiani3ad58322021-07-21 16:48:54 +010041
Joe Subbiani918ee792021-07-30 16:57:04 +010042 # SHA in Mbed TLS == SHA1 GnuTLS,
43 # if the last 3 chars are SHA append 1
Joe Subbiani3ad58322021-07-21 16:48:54 +010044 if m_cipher[-3:] == "SHA":
Joe Subbiani83944842021-07-20 18:26:03 +010045 m_cipher = m_cipher+"1"
Joe Subbiani3ad58322021-07-21 16:48:54 +010046
47 # CCM or CCM-8 should be followed by ":+AEAD"
Joe Subbiani918ee792021-07-30 16:57:04 +010048 # Replace "GCM:+SHAxyz" with "GCM:+AEAD"
49 if "CCM" in m_cipher or "GCM" in m_cipher:
50 m_cipher = re.sub(r"GCM-SHA\d\d\d", "GCM", m_cipher)
Joe Subbiani83944842021-07-20 18:26:03 +010051 m_cipher = m_cipher+":+AEAD"
Joe Subbiani3ad58322021-07-21 16:48:54 +010052
53 # Replace the last "-" with ":+"
Joe Subbiani83944842021-07-20 18:26:03 +010054 else:
Joe Subbianif849a932021-07-28 16:50:30 +010055 index = m_cipher.rindex("-")
Joe Subbiani918ee792021-07-30 16:57:04 +010056 m_cipher = m_cipher[:index] + ":+" + m_cipher[index+1:]
Joe Subbiani83944842021-07-20 18:26:03 +010057
58 return m_cipher
Joe Subbiani3ad58322021-07-21 16:48:54 +010059
Joe Subbiani83944842021-07-20 18:26:03 +010060def translate_ossl(m_cipher):
Joe Subbianif849a932021-07-28 16:50:30 +010061 """
62 Translate m_cipher from MBedTLS ciphersuite naming convention
63 and return the OpenSSL naming convention
64 """
65
Joe Subbiani918ee792021-07-30 16:57:04 +010066 m_cipher = re.sub(r'^TLS-', '', m_cipher)
Joe Subbiani83944842021-07-20 18:26:03 +010067 m_cipher = m_cipher.replace("-WITH", "")
Joe Subbiani3ad58322021-07-21 16:48:54 +010068
69 # Remove the "-" from "ABC-xyz"
Joe Subbiani83944842021-07-20 18:26:03 +010070 m_cipher = m_cipher.replace("AES-", "AES")
71 m_cipher = m_cipher.replace("CAMELLIA-", "CAMELLIA")
72 m_cipher = m_cipher.replace("ARIA-", "ARIA")
Joe Subbiani83944842021-07-20 18:26:03 +010073
Joe Subbiani3ad58322021-07-21 16:48:54 +010074 # Remove "RSA" if it is at the beginning
Joe Subbiani918ee792021-07-30 16:57:04 +010075 m_cipher = re.sub(r'^RSA-', r'', m_cipher)
Joe Subbiani83944842021-07-20 18:26:03 +010076
Joe Subbiani3ad58322021-07-21 16:48:54 +010077 # For all circumstances outside of PSK
78 if "PSK" not in m_cipher:
79 m_cipher = m_cipher.replace("-EDE", "")
80 m_cipher = m_cipher.replace("3DES-CBC", "DES-CBC3")
81
82 # Remove "CBC" if it is not prefixed by DES
Joe Subbiani918ee792021-07-30 16:57:04 +010083 m_cipher = re.sub(r'(?<!DES-)CBC-', r'', m_cipher)
Joe Subbiani3ad58322021-07-21 16:48:54 +010084
85 # ECDHE-RSA-ARIA does not exist in OpenSSL
Joe Subbiani83944842021-07-20 18:26:03 +010086 m_cipher = m_cipher.replace("ECDHE-RSA-ARIA", "ECDHE-ARIA")
87
Joe Subbiani3ad58322021-07-21 16:48:54 +010088 # POLY1305 should not be followed by anything
89 if "POLY1305" in m_cipher:
Joe Subbiani83944842021-07-20 18:26:03 +010090 index = m_cipher.rindex("POLY1305")
Joe Subbianif849a932021-07-28 16:50:30 +010091 m_cipher = m_cipher[:index+8]
Joe Subbiani3ad58322021-07-21 16:48:54 +010092
93 # If DES is being used, Replace DHE with EDH
94 if "DES" in m_cipher and "DHE" in m_cipher and "ECDHE" not in m_cipher:
95 m_cipher = m_cipher.replace("DHE", "EDH")
Joe Subbiani83944842021-07-20 18:26:03 +010096
97 return m_cipher
Joe Subbiani97cd5992021-07-22 16:08:29 +010098
Joe Subbiani918ee792021-07-30 16:57:04 +010099def format_ciphersuite_names(mode, names):
100 t = {"g": translate_gnutls, "o": translate_ossl}[mode]
101 return " ".join(t(c) for c in names)
Joe Subbiani97cd5992021-07-22 16:08:29 +0100102
Joe Subbiani918ee792021-07-30 16:57:04 +0100103def main(target, names):
104 print(format_ciphersuite_names(target, names))
Joe Subbiani97cd5992021-07-22 16:08:29 +0100105
106if __name__ == "__main__":
Joe Subbiani918ee792021-07-30 16:57:04 +0100107 PARSER = argparse.ArgumentParser()
108 PARSER.add_argument('target', metavar='TARGET', choices=['o', 'g'])
109 PARSER.add_argument('names', metavar='NAMES', nargs='+')
110 ARGS = PARSER.parse_args()
111 main(ARGS.target, ARGS.names)