Looks up a comma-separated list of algorithms and outputs their security properties. Returns an exitcodes.* flag.
(out: OutputBuffer, alg_names: str)
| 1308 | |
| 1309 | |
| 1310 | def algorithm_lookup(out: OutputBuffer, alg_names: str) -> int: |
| 1311 | '''Looks up a comma-separated list of algorithms and outputs their security properties. Returns an exitcodes.* flag.''' |
| 1312 | retval = exitcodes.GOOD |
| 1313 | alg_types = { |
| 1314 | 'kex': 'key exchange algorithms', |
| 1315 | 'key': 'host-key algorithms', |
| 1316 | 'mac': 'message authentication code algorithms', |
| 1317 | 'enc': 'encryption algorithms (ciphers)' |
| 1318 | } |
| 1319 | |
| 1320 | algorithm_names = alg_names.split(",") |
| 1321 | adb = SSH2_KexDB.get_db() |
| 1322 | |
| 1323 | # Use nested dictionary comprehension to iterate an outer dictionary where |
| 1324 | # each key is an alg type that consists of a value (which is itself a |
| 1325 | # dictionary) of alg names. Filter the alg names against the user supplied |
| 1326 | # list of names. |
| 1327 | algorithms_dict = { |
| 1328 | outer_k: { |
| 1329 | inner_k |
| 1330 | for (inner_k, inner_v) in outer_v.items() |
| 1331 | if inner_k in algorithm_names |
| 1332 | } |
| 1333 | for (outer_k, outer_v) in adb.items() |
| 1334 | } |
| 1335 | |
| 1336 | unknown_algorithms: List[str] = [] |
| 1337 | padding = len(max(algorithm_names, key=len)) |
| 1338 | |
| 1339 | for alg_type in alg_types: |
| 1340 | if len(algorithms_dict[alg_type]) > 0: |
| 1341 | title = str(alg_types.get(alg_type)) |
| 1342 | retval = output_algorithms(out, title, adb, alg_type, list(algorithms_dict[alg_type]), unknown_algorithms, False, retval, padding) |
| 1343 | |
| 1344 | algorithms_dict_flattened = [ |
| 1345 | alg_name |
| 1346 | for val in algorithms_dict.values() |
| 1347 | for alg_name in val |
| 1348 | ] |
| 1349 | |
| 1350 | algorithms_not_found = [ |
| 1351 | alg_name |
| 1352 | for alg_name in algorithm_names |
| 1353 | if alg_name not in algorithms_dict_flattened |
| 1354 | ] |
| 1355 | |
| 1356 | similar_algorithms = [ |
| 1357 | alg_unknown + " --> (" + alg_type + ") " + alg_name |
| 1358 | for alg_unknown in algorithms_not_found |
| 1359 | for alg_type, alg_names in adb.items() |
| 1360 | for alg_name in alg_names |
| 1361 | # Perform a case-insensitive comparison using 'casefold' |
| 1362 | # and match substrings using the 'in' operator. |
| 1363 | if alg_unknown.casefold() in alg_name.casefold() |
| 1364 | ] |
| 1365 | |
| 1366 | if len(algorithms_not_found) > 0: |
| 1367 | retval = exitcodes.FAILURE |