blob: 207c884cf661955007f26b2dc136cf5cdd9603f3 [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"),
53 ("TLS-ECDHE-ECDSA-WITH-CHACHA20-POLY1305-SHA256",
54 None,
55 "ECDHE-ECDSA-CHACHA20-POLY1305"),
56 ("TLS-ECDHE-ECDSA-WITH-AES-128-CCM",
57 "+ECDHE-ECDSA:+AES-128-CCM:+AEAD",
58 None),
Joe Subbiania25ffab2021-08-06 09:41:27 +010059 ]
60
61 for m, g_exp, o_exp in ciphers:
62
63 if g_exp is not None:
64 g = translate_gnutls(m)
65 self.assertEqual(g, g_exp)
66
67 if o_exp is not None:
68 o = translate_ossl(m)
69 self.assertEqual(o, o_exp)
70
Joe Subbiani0fadf8e2021-07-27 15:22:26 +010071def translate_gnutls(m_cipher):
Joe Subbianif849a932021-07-28 16:50:30 +010072 """
Joe Subbianib0aba9a2021-08-25 09:56:57 +010073 Translate m_cipher from Mbed TLS ciphersuite naming convention
Joe Subbianif849a932021-07-28 16:50:30 +010074 and return the GnuTLS naming convention
75 """
76
Joe Subbiani918ee792021-07-30 16:57:04 +010077 m_cipher = re.sub(r'\ATLS-', '+', m_cipher)
Joe Subbiani83944842021-07-20 18:26:03 +010078 m_cipher = m_cipher.replace("-WITH-", ":+")
79 m_cipher = m_cipher.replace("-EDE", "")
Joe Subbiani3ad58322021-07-21 16:48:54 +010080
Joe Subbiani918ee792021-07-30 16:57:04 +010081 # SHA in Mbed TLS == SHA1 GnuTLS,
82 # if the last 3 chars are SHA append 1
Joe Subbiani3ad58322021-07-21 16:48:54 +010083 if m_cipher[-3:] == "SHA":
Joe Subbiani83944842021-07-20 18:26:03 +010084 m_cipher = m_cipher+"1"
Joe Subbiani3ad58322021-07-21 16:48:54 +010085
86 # CCM or CCM-8 should be followed by ":+AEAD"
Joe Subbiani918ee792021-07-30 16:57:04 +010087 # Replace "GCM:+SHAxyz" with "GCM:+AEAD"
88 if "CCM" in m_cipher or "GCM" in m_cipher:
89 m_cipher = re.sub(r"GCM-SHA\d\d\d", "GCM", m_cipher)
Joe Subbiani83944842021-07-20 18:26:03 +010090 m_cipher = m_cipher+":+AEAD"
Joe Subbiani3ad58322021-07-21 16:48:54 +010091
92 # Replace the last "-" with ":+"
Joe Subbiani83944842021-07-20 18:26:03 +010093 else:
Joe Subbianif849a932021-07-28 16:50:30 +010094 index = m_cipher.rindex("-")
Joe Subbiani918ee792021-07-30 16:57:04 +010095 m_cipher = m_cipher[:index] + ":+" + m_cipher[index+1:]
Joe Subbiani83944842021-07-20 18:26:03 +010096
97 return m_cipher
Joe Subbiani3ad58322021-07-21 16:48:54 +010098
Joe Subbiani83944842021-07-20 18:26:03 +010099def translate_ossl(m_cipher):
Joe Subbianif849a932021-07-28 16:50:30 +0100100 """
Joe Subbianib0aba9a2021-08-25 09:56:57 +0100101 Translate m_cipher from Mbed TLS ciphersuite naming convention
Joe Subbianif849a932021-07-28 16:50:30 +0100102 and return the OpenSSL naming convention
103 """
104
Joe Subbiani918ee792021-07-30 16:57:04 +0100105 m_cipher = re.sub(r'^TLS-', '', m_cipher)
Joe Subbiani83944842021-07-20 18:26:03 +0100106 m_cipher = m_cipher.replace("-WITH", "")
Joe Subbiani3ad58322021-07-21 16:48:54 +0100107
108 # Remove the "-" from "ABC-xyz"
Joe Subbiani83944842021-07-20 18:26:03 +0100109 m_cipher = m_cipher.replace("AES-", "AES")
110 m_cipher = m_cipher.replace("CAMELLIA-", "CAMELLIA")
111 m_cipher = m_cipher.replace("ARIA-", "ARIA")
Joe Subbiani83944842021-07-20 18:26:03 +0100112
Joe Subbiani3ad58322021-07-21 16:48:54 +0100113 # Remove "RSA" if it is at the beginning
Joe Subbiani918ee792021-07-30 16:57:04 +0100114 m_cipher = re.sub(r'^RSA-', r'', m_cipher)
Joe Subbiani83944842021-07-20 18:26:03 +0100115
Joe Subbiani3ad58322021-07-21 16:48:54 +0100116 # For all circumstances outside of PSK
117 if "PSK" not in m_cipher:
118 m_cipher = m_cipher.replace("-EDE", "")
119 m_cipher = m_cipher.replace("3DES-CBC", "DES-CBC3")
120
121 # Remove "CBC" if it is not prefixed by DES
Joe Subbiani918ee792021-07-30 16:57:04 +0100122 m_cipher = re.sub(r'(?<!DES-)CBC-', r'', m_cipher)
Joe Subbiani3ad58322021-07-21 16:48:54 +0100123
124 # ECDHE-RSA-ARIA does not exist in OpenSSL
Joe Subbiani83944842021-07-20 18:26:03 +0100125 m_cipher = m_cipher.replace("ECDHE-RSA-ARIA", "ECDHE-ARIA")
126
Joe Subbiani3ad58322021-07-21 16:48:54 +0100127 # POLY1305 should not be followed by anything
128 if "POLY1305" in m_cipher:
Joe Subbiani83944842021-07-20 18:26:03 +0100129 index = m_cipher.rindex("POLY1305")
Joe Subbianif849a932021-07-28 16:50:30 +0100130 m_cipher = m_cipher[:index+8]
Joe Subbiani3ad58322021-07-21 16:48:54 +0100131
132 # If DES is being used, Replace DHE with EDH
133 if "DES" in m_cipher and "DHE" in m_cipher and "ECDHE" not in m_cipher:
134 m_cipher = m_cipher.replace("DHE", "EDH")
Joe Subbiani83944842021-07-20 18:26:03 +0100135
136 return m_cipher
Joe Subbiani97cd5992021-07-22 16:08:29 +0100137
Joe Subbiani918ee792021-07-30 16:57:04 +0100138def format_ciphersuite_names(mode, names):
139 t = {"g": translate_gnutls, "o": translate_ossl}[mode]
140 return " ".join(t(c) for c in names)
Joe Subbiani97cd5992021-07-22 16:08:29 +0100141
Joe Subbiani918ee792021-07-30 16:57:04 +0100142def main(target, names):
143 print(format_ciphersuite_names(target, names))
Joe Subbiani97cd5992021-07-22 16:08:29 +0100144
145if __name__ == "__main__":
Joe Subbiani918ee792021-07-30 16:57:04 +0100146 PARSER = argparse.ArgumentParser()
147 PARSER.add_argument('target', metavar='TARGET', choices=['o', 'g'])
148 PARSER.add_argument('names', metavar='NAMES', nargs='+')
149 ARGS = PARSER.parse_args()
150 main(ARGS.target, ARGS.names)