blob: a8db4bb352b5434de618859b666dcad74921e065 [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"""
Yanray Wangee97f052023-01-16 11:30:59 +080021Translate standard ciphersuite names to GnuTLS, OpenSSL and Mbed TLS standards.
Joe Subbianif849a932021-07-28 16:50:30 +010022
Joe Subbiania25ffab2021-08-06 09:41:27 +010023To test the translation functions run:
24python3 -m unittest translate_cipher.py
Joe Subbianif849a932021-07-28 16:50:30 +010025"""
Joe Subbiania16ccac2021-07-22 18:52:17 +010026
Joe Subbiani3ad58322021-07-21 16:48:54 +010027import re
Joe Subbiani918ee792021-07-30 16:57:04 +010028import argparse
Joe Subbiania25ffab2021-08-06 09:41:27 +010029import unittest
30
31class TestTranslateCiphers(unittest.TestCase):
32 """
33 Ensure translate_ciphers.py translates and formats ciphersuite names
34 correctly
35 """
36 def test_translate_all_cipher_names(self):
37 """
Yanray Wangee97f052023-01-16 11:30:59 +080038 Translate standard ciphersuite names to GnuTLS, OpenSSL and
39 Mbed TLS counterpart. Use only a small subset of ciphers
40 that exercise each step of the translation functions
Joe Subbiania25ffab2021-08-06 09:41:27 +010041 """
42 ciphers = [
Yanray Wangee97f052023-01-16 11:30:59 +080043 ("TLS_ECDHE_ECDSA_WITH_NULL_SHA",
Joe Subbiani49d57bc2021-09-02 18:50:30 +010044 "+ECDHE-ECDSA:+NULL:+SHA1",
Yanray Wangee97f052023-01-16 11:30:59 +080045 "ECDHE-ECDSA-NULL-SHA",
46 "TLS-ECDHE-ECDSA-WITH-NULL-SHA"),
47 ("TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256",
Joe Subbiani49d57bc2021-09-02 18:50:30 +010048 "+ECDHE-ECDSA:+AES-128-GCM:+AEAD",
Yanray Wangee97f052023-01-16 11:30:59 +080049 "ECDHE-ECDSA-AES128-GCM-SHA256",
50 "TLS-ECDHE-ECDSA-WITH-AES-128-GCM-SHA256"),
51 ("TLS_DHE_RSA_WITH_3DES_EDE_CBC_SHA",
Joe Subbiani49d57bc2021-09-02 18:50:30 +010052 "+DHE-RSA:+3DES-CBC:+SHA1",
Yanray Wangee97f052023-01-16 11:30:59 +080053 "EDH-RSA-DES-CBC3-SHA",
54 "TLS-DHE-RSA-WITH-3DES-EDE-CBC-SHA"),
55 ("TLS_RSA_WITH_AES_256_CBC_SHA",
Joe Subbianie5d61062021-09-03 13:30:44 +010056 "+RSA:+AES-256-CBC:+SHA1",
Yanray Wangee97f052023-01-16 11:30:59 +080057 "AES256-SHA",
58 "TLS-RSA-WITH-AES-256-CBC-SHA"),
59 ("TLS_PSK_WITH_3DES_EDE_CBC_SHA",
Joe Subbianie5d61062021-09-03 13:30:44 +010060 "+PSK:+3DES-CBC:+SHA1",
Yanray Wangee97f052023-01-16 11:30:59 +080061 "PSK-3DES-EDE-CBC-SHA",
62 "TLS-PSK-WITH-3DES-EDE-CBC-SHA"),
63 ("TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256",
Joe Subbiani49d57bc2021-09-02 18:50:30 +010064 None,
Yanray Wangee97f052023-01-16 11:30:59 +080065 "ECDHE-ECDSA-CHACHA20-POLY1305",
66 "TLS-ECDHE-ECDSA-WITH-CHACHA20-POLY1305-SHA256"),
67 ("TLS_ECDHE_ECDSA_WITH_AES_128_CCM",
Joe Subbiani49d57bc2021-09-02 18:50:30 +010068 "+ECDHE-ECDSA:+AES-128-CCM:+AEAD",
Joe Subbianie5d61062021-09-03 13:30:44 +010069 None,
Yanray Wangee97f052023-01-16 11:30:59 +080070 "TLS-ECDHE-ECDSA-WITH-AES-128-CCM"),
71 ("TLS_ECDHE_RSA_WITH_ARIA_256_GCM_SHA384",
72 None,
73 "ECDHE-ARIA256-GCM-SHA384",
74 "TLS-ECDHE-RSA-WITH-ARIA-256-GCM-SHA384"),
Joe Subbiania25ffab2021-08-06 09:41:27 +010075 ]
76
Yanray Wangee97f052023-01-16 11:30:59 +080077 for s, g_exp, o_exp, m_exp in ciphers:
Joe Subbiania25ffab2021-08-06 09:41:27 +010078
79 if g_exp is not None:
Yanray Wangee97f052023-01-16 11:30:59 +080080 g = translate_gnutls(s)
Joe Subbiania25ffab2021-08-06 09:41:27 +010081 self.assertEqual(g, g_exp)
82
83 if o_exp is not None:
Yanray Wangee97f052023-01-16 11:30:59 +080084 o = translate_ossl(s)
Joe Subbiania25ffab2021-08-06 09:41:27 +010085 self.assertEqual(o, o_exp)
86
Yanray Wangee97f052023-01-16 11:30:59 +080087 if m_exp is not None:
88 m = translate_mbedtls(s)
89 self.assertEqual(m, m_exp)
90
91def translate_gnutls(s_cipher):
Joe Subbianif849a932021-07-28 16:50:30 +010092 """
Yanray Wangee97f052023-01-16 11:30:59 +080093 Translate s_cipher from standard ciphersuite naming convention
Joe Subbianif849a932021-07-28 16:50:30 +010094 and return the GnuTLS naming convention
95 """
96
Yanray Wangee97f052023-01-16 11:30:59 +080097 # Replace "_" with "-" to handle ciphersuite names based on Mbed TLS
98 # naming convention
99 s_cipher = s_cipher.replace("_", "-")
100
101 s_cipher = re.sub(r'\ATLS-', '+', s_cipher)
102 s_cipher = s_cipher.replace("-WITH-", ":+")
103 s_cipher = s_cipher.replace("-EDE", "")
Joe Subbiani3ad58322021-07-21 16:48:54 +0100104
Joe Subbiani918ee792021-07-30 16:57:04 +0100105 # SHA in Mbed TLS == SHA1 GnuTLS,
106 # if the last 3 chars are SHA append 1
Yanray Wangee97f052023-01-16 11:30:59 +0800107 if s_cipher[-3:] == "SHA":
108 s_cipher = s_cipher+"1"
Joe Subbiani3ad58322021-07-21 16:48:54 +0100109
110 # CCM or CCM-8 should be followed by ":+AEAD"
Joe Subbiani918ee792021-07-30 16:57:04 +0100111 # Replace "GCM:+SHAxyz" with "GCM:+AEAD"
Yanray Wangee97f052023-01-16 11:30:59 +0800112 if "CCM" in s_cipher or "GCM" in s_cipher:
113 s_cipher = re.sub(r"GCM-SHA\d\d\d", "GCM", s_cipher)
114 s_cipher = s_cipher+":+AEAD"
Joe Subbiani3ad58322021-07-21 16:48:54 +0100115
116 # Replace the last "-" with ":+"
Joe Subbiani83944842021-07-20 18:26:03 +0100117 else:
Yanray Wangee97f052023-01-16 11:30:59 +0800118 index = s_cipher.rindex("-")
119 s_cipher = s_cipher[:index] + ":+" + s_cipher[index+1:]
Joe Subbiani83944842021-07-20 18:26:03 +0100120
Yanray Wangee97f052023-01-16 11:30:59 +0800121 return s_cipher
Joe Subbiani3ad58322021-07-21 16:48:54 +0100122
Yanray Wangee97f052023-01-16 11:30:59 +0800123def translate_ossl(s_cipher):
Joe Subbianif849a932021-07-28 16:50:30 +0100124 """
Yanray Wangee97f052023-01-16 11:30:59 +0800125 Translate s_cipher from standard ciphersuite naming convention
Joe Subbianif849a932021-07-28 16:50:30 +0100126 and return the OpenSSL naming convention
127 """
128
Yanray Wangee97f052023-01-16 11:30:59 +0800129 # Replace "_" with "-" to handle ciphersuite names based on Mbed TLS
130 # naming convention
131 s_cipher = s_cipher.replace("_", "-")
132
133 s_cipher = re.sub(r'^TLS-', '', s_cipher)
134 s_cipher = s_cipher.replace("-WITH", "")
Joe Subbiani3ad58322021-07-21 16:48:54 +0100135
136 # Remove the "-" from "ABC-xyz"
Yanray Wangee97f052023-01-16 11:30:59 +0800137 s_cipher = s_cipher.replace("AES-", "AES")
138 s_cipher = s_cipher.replace("CAMELLIA-", "CAMELLIA")
139 s_cipher = s_cipher.replace("ARIA-", "ARIA")
Joe Subbiani83944842021-07-20 18:26:03 +0100140
Joe Subbiani3ad58322021-07-21 16:48:54 +0100141 # Remove "RSA" if it is at the beginning
Yanray Wangee97f052023-01-16 11:30:59 +0800142 s_cipher = re.sub(r'^RSA-', r'', s_cipher)
Joe Subbiani83944842021-07-20 18:26:03 +0100143
Joe Subbiani3ad58322021-07-21 16:48:54 +0100144 # For all circumstances outside of PSK
Yanray Wangee97f052023-01-16 11:30:59 +0800145 if "PSK" not in s_cipher:
146 s_cipher = s_cipher.replace("-EDE", "")
147 s_cipher = s_cipher.replace("3DES-CBC", "DES-CBC3")
Joe Subbiani3ad58322021-07-21 16:48:54 +0100148
149 # Remove "CBC" if it is not prefixed by DES
Yanray Wangee97f052023-01-16 11:30:59 +0800150 s_cipher = re.sub(r'(?<!DES-)CBC-', r'', s_cipher)
Joe Subbiani3ad58322021-07-21 16:48:54 +0100151
152 # ECDHE-RSA-ARIA does not exist in OpenSSL
Yanray Wangee97f052023-01-16 11:30:59 +0800153 s_cipher = s_cipher.replace("ECDHE-RSA-ARIA", "ECDHE-ARIA")
Joe Subbiani83944842021-07-20 18:26:03 +0100154
Joe Subbiani3ad58322021-07-21 16:48:54 +0100155 # POLY1305 should not be followed by anything
Yanray Wangee97f052023-01-16 11:30:59 +0800156 if "POLY1305" in s_cipher:
157 index = s_cipher.rindex("POLY1305")
158 s_cipher = s_cipher[:index+8]
Joe Subbiani3ad58322021-07-21 16:48:54 +0100159
160 # If DES is being used, Replace DHE with EDH
Yanray Wangee97f052023-01-16 11:30:59 +0800161 if "DES" in s_cipher and "DHE" in s_cipher and "ECDHE" not in s_cipher:
162 s_cipher = s_cipher.replace("DHE", "EDH")
Joe Subbiani83944842021-07-20 18:26:03 +0100163
Yanray Wangee97f052023-01-16 11:30:59 +0800164 return s_cipher
165
166def translate_mbedtls(s_cipher):
167 """
168 Translate s_cipher from standard ciphersuite naming convention
169 and return Mbed TLS ciphersuite naming convention
170 """
171
172 # Replace "_" with "-"
173 s_cipher = s_cipher.replace("_", "-")
174
175 return s_cipher
Joe Subbiani97cd5992021-07-22 16:08:29 +0100176
Joe Subbiani918ee792021-07-30 16:57:04 +0100177def format_ciphersuite_names(mode, names):
Yanray Wangee97f052023-01-16 11:30:59 +0800178 t = {"g": translate_gnutls,
179 "o": translate_ossl,
180 "m": translate_mbedtls
181 }[mode]
Gilles Peskine47aab852023-01-26 21:16:34 +0100182 return " ".join(c + '=' + t(c) for c in names)
Joe Subbiani97cd5992021-07-22 16:08:29 +0100183
Joe Subbiani918ee792021-07-30 16:57:04 +0100184def main(target, names):
185 print(format_ciphersuite_names(target, names))
Joe Subbiani97cd5992021-07-22 16:08:29 +0100186
187if __name__ == "__main__":
Joe Subbiani918ee792021-07-30 16:57:04 +0100188 PARSER = argparse.ArgumentParser()
Yanray Wangee97f052023-01-16 11:30:59 +0800189 PARSER.add_argument('target', metavar='TARGET', choices=['o', 'g', 'm'])
Joe Subbiani918ee792021-07-30 16:57:04 +0100190 PARSER.add_argument('names', metavar='NAMES', nargs='+')
191 ARGS = PARSER.parse_args()
192 main(ARGS.target, ARGS.names)