blob: d5f847fd54d5ffacae11d46039a113a5fff2b44a [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"""
Joe Subbianib0aba9a2021-08-25 09:56:57 +010021Translate ciphersuite names in Mbed TLS format to OpenSSL and GNUTLS
Joe Subbianif849a932021-07-28 16:50:30 +010022standards.
23
Joe Subbiania25ffab2021-08-06 09:41:27 +010024To test the translation functions run:
25python3 -m unittest translate_cipher.py
Joe Subbianif849a932021-07-28 16:50:30 +010026"""
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 Subbiania25ffab2021-08-06 09:41:27 +010030import unittest
31
32class TestTranslateCiphers(unittest.TestCase):
33 """
34 Ensure translate_ciphers.py translates and formats ciphersuite names
35 correctly
36 """
37 def test_translate_all_cipher_names(self):
38 """
Joe Subbiani54110b32021-09-02 13:02:29 +010039 Translate MbedTLS ciphersuite names to their OpenSSL and
40 GnuTLS counterpart. Use only a small subset of ciphers
41 that exercise each step of the translate functions
Joe Subbiania25ffab2021-08-06 09:41:27 +010042 """
43 ciphers = [
Joe Subbiani49d57bc2021-09-02 18:50:30 +010044 ("TLS-ECDHE-ECDSA-WITH-NULL-SHA",
45 "+ECDHE-ECDSA:+NULL:+SHA1",
46 "ECDHE-ECDSA-NULL-SHA"),
47 ("TLS-ECDHE-ECDSA-WITH-AES-128-GCM-SHA256",
48 "+ECDHE-ECDSA:+AES-128-GCM:+AEAD",
49 "ECDHE-ECDSA-AES128-GCM-SHA256"),
50 ("TLS-DHE-RSA-WITH-3DES-EDE-CBC-SHA",
51 "+DHE-RSA:+3DES-CBC:+SHA1",
52 "EDH-RSA-DES-CBC3-SHA"),
Joe Subbianie5d61062021-09-03 13:30:44 +010053 ("TLS-RSA-WITH-AES-256-CBC-SHA",
54 "+RSA:+AES-256-CBC:+SHA1",
55 "AES256-SHA"),
56 ("TLS-PSK-WITH-3DES-EDE-CBC-SHA",
57 "+PSK:+3DES-CBC:+SHA1",
58 "PSK-3DES-EDE-CBC-SHA"),
Joe Subbiani49d57bc2021-09-02 18:50:30 +010059 ("TLS-ECDHE-ECDSA-WITH-CHACHA20-POLY1305-SHA256",
60 None,
61 "ECDHE-ECDSA-CHACHA20-POLY1305"),
62 ("TLS-ECDHE-ECDSA-WITH-AES-128-CCM",
63 "+ECDHE-ECDSA:+AES-128-CCM:+AEAD",
64 None),
Joe Subbianie5d61062021-09-03 13:30:44 +010065 ("TLS-ECDHE-RSA-WITH-ARIA-256-GCM-SHA384",
66 None,
67 "ECDHE-ARIA256-GCM-SHA384"),
Joe Subbiania25ffab2021-08-06 09:41:27 +010068 ]
69
70 for m, g_exp, o_exp in ciphers:
71
72 if g_exp is not None:
73 g = translate_gnutls(m)
74 self.assertEqual(g, g_exp)
75
76 if o_exp is not None:
77 o = translate_ossl(m)
78 self.assertEqual(o, o_exp)
79
Joe Subbiani0fadf8e2021-07-27 15:22:26 +010080def translate_gnutls(m_cipher):
Joe Subbianif849a932021-07-28 16:50:30 +010081 """
Joe Subbianib0aba9a2021-08-25 09:56:57 +010082 Translate m_cipher from Mbed TLS ciphersuite naming convention
Joe Subbianif849a932021-07-28 16:50:30 +010083 and return the GnuTLS naming convention
84 """
85
Joe Subbiani918ee792021-07-30 16:57:04 +010086 m_cipher = re.sub(r'\ATLS-', '+', m_cipher)
Joe Subbiani83944842021-07-20 18:26:03 +010087 m_cipher = m_cipher.replace("-WITH-", ":+")
88 m_cipher = m_cipher.replace("-EDE", "")
Joe Subbiani3ad58322021-07-21 16:48:54 +010089
Joe Subbiani918ee792021-07-30 16:57:04 +010090 # SHA in Mbed TLS == SHA1 GnuTLS,
91 # if the last 3 chars are SHA append 1
Joe Subbiani3ad58322021-07-21 16:48:54 +010092 if m_cipher[-3:] == "SHA":
Joe Subbiani83944842021-07-20 18:26:03 +010093 m_cipher = m_cipher+"1"
Joe Subbiani3ad58322021-07-21 16:48:54 +010094
95 # CCM or CCM-8 should be followed by ":+AEAD"
Joe Subbiani918ee792021-07-30 16:57:04 +010096 # Replace "GCM:+SHAxyz" with "GCM:+AEAD"
97 if "CCM" in m_cipher or "GCM" in m_cipher:
98 m_cipher = re.sub(r"GCM-SHA\d\d\d", "GCM", m_cipher)
Joe Subbiani83944842021-07-20 18:26:03 +010099 m_cipher = m_cipher+":+AEAD"
Joe Subbiani3ad58322021-07-21 16:48:54 +0100100
101 # Replace the last "-" with ":+"
Joe Subbiani83944842021-07-20 18:26:03 +0100102 else:
Joe Subbianif849a932021-07-28 16:50:30 +0100103 index = m_cipher.rindex("-")
Joe Subbiani918ee792021-07-30 16:57:04 +0100104 m_cipher = m_cipher[:index] + ":+" + m_cipher[index+1:]
Joe Subbiani83944842021-07-20 18:26:03 +0100105
106 return m_cipher
Joe Subbiani3ad58322021-07-21 16:48:54 +0100107
Joe Subbiani83944842021-07-20 18:26:03 +0100108def translate_ossl(m_cipher):
Joe Subbianif849a932021-07-28 16:50:30 +0100109 """
Joe Subbianib0aba9a2021-08-25 09:56:57 +0100110 Translate m_cipher from Mbed TLS ciphersuite naming convention
Joe Subbianif849a932021-07-28 16:50:30 +0100111 and return the OpenSSL naming convention
112 """
113
Joe Subbiani918ee792021-07-30 16:57:04 +0100114 m_cipher = re.sub(r'^TLS-', '', m_cipher)
Joe Subbiani83944842021-07-20 18:26:03 +0100115 m_cipher = m_cipher.replace("-WITH", "")
Joe Subbiani3ad58322021-07-21 16:48:54 +0100116
117 # Remove the "-" from "ABC-xyz"
Joe Subbiani83944842021-07-20 18:26:03 +0100118 m_cipher = m_cipher.replace("AES-", "AES")
119 m_cipher = m_cipher.replace("CAMELLIA-", "CAMELLIA")
120 m_cipher = m_cipher.replace("ARIA-", "ARIA")
Joe Subbiani83944842021-07-20 18:26:03 +0100121
Joe Subbiani3ad58322021-07-21 16:48:54 +0100122 # Remove "RSA" if it is at the beginning
Joe Subbiani918ee792021-07-30 16:57:04 +0100123 m_cipher = re.sub(r'^RSA-', r'', m_cipher)
Joe Subbiani83944842021-07-20 18:26:03 +0100124
Joe Subbiani3ad58322021-07-21 16:48:54 +0100125 # For all circumstances outside of PSK
126 if "PSK" not in m_cipher:
127 m_cipher = m_cipher.replace("-EDE", "")
128 m_cipher = m_cipher.replace("3DES-CBC", "DES-CBC3")
129
130 # Remove "CBC" if it is not prefixed by DES
Joe Subbiani918ee792021-07-30 16:57:04 +0100131 m_cipher = re.sub(r'(?<!DES-)CBC-', r'', m_cipher)
Joe Subbiani3ad58322021-07-21 16:48:54 +0100132
133 # ECDHE-RSA-ARIA does not exist in OpenSSL
Joe Subbiani83944842021-07-20 18:26:03 +0100134 m_cipher = m_cipher.replace("ECDHE-RSA-ARIA", "ECDHE-ARIA")
135
Joe Subbiani3ad58322021-07-21 16:48:54 +0100136 # POLY1305 should not be followed by anything
137 if "POLY1305" in m_cipher:
Joe Subbiani83944842021-07-20 18:26:03 +0100138 index = m_cipher.rindex("POLY1305")
Joe Subbianif849a932021-07-28 16:50:30 +0100139 m_cipher = m_cipher[:index+8]
Joe Subbiani3ad58322021-07-21 16:48:54 +0100140
141 # If DES is being used, Replace DHE with EDH
142 if "DES" in m_cipher and "DHE" in m_cipher and "ECDHE" not in m_cipher:
143 m_cipher = m_cipher.replace("DHE", "EDH")
Joe Subbiani83944842021-07-20 18:26:03 +0100144
145 return m_cipher
Joe Subbiani97cd5992021-07-22 16:08:29 +0100146
Joe Subbiani918ee792021-07-30 16:57:04 +0100147def format_ciphersuite_names(mode, names):
148 t = {"g": translate_gnutls, "o": translate_ossl}[mode]
149 return " ".join(t(c) for c in names)
Joe Subbiani97cd5992021-07-22 16:08:29 +0100150
Joe Subbiani918ee792021-07-30 16:57:04 +0100151def main(target, names):
152 print(format_ciphersuite_names(target, names))
Joe Subbiani97cd5992021-07-22 16:08:29 +0100153
154if __name__ == "__main__":
Joe Subbiani918ee792021-07-30 16:57:04 +0100155 PARSER = argparse.ArgumentParser()
156 PARSER.add_argument('target', metavar='TARGET', choices=['o', 'g'])
157 PARSER.add_argument('names', metavar='NAMES', nargs='+')
158 ARGS = PARSER.parse_args()
159 main(ARGS.target, ARGS.names)