From 04cfe3463953234816e89ac0954f314698616d10 Mon Sep 17 00:00:00 2001 From: Archana Date: Sun, 9 Jan 2022 13:28:28 +0530 Subject: [PATCH] JSON Validation added (1) JSON file structure altered a bit. (2) JSON validated against schema. (3) Corresponding changes in the python script. Signed-off-by: Archana Signed-off-by: Asfandyar Orakzai --- .../driver_jsons/driver_opaque_schema.json | 112 ++++++++++++++++++ .../driver_transparent_schema.json | 106 +++++++++++++++++ .../mbedtls_test_opaque_driver.json | 5 +- .../mbedtls_test_transparent_driver.json | 5 +- .../psa_crypto_driver_wrappers.c.jinja | 4 + scripts/generate_driver_wrappers.py | 53 ++++++--- tests/docker/bionic/Dockerfile | 4 + 7 files changed, 269 insertions(+), 20 deletions(-) create mode 100644 scripts/data_files/driver_jsons/driver_opaque_schema.json create mode 100644 scripts/data_files/driver_jsons/driver_transparent_schema.json diff --git a/scripts/data_files/driver_jsons/driver_opaque_schema.json b/scripts/data_files/driver_jsons/driver_opaque_schema.json new file mode 100644 index 0000000000..53660c8e95 --- /dev/null +++ b/scripts/data_files/driver_jsons/driver_opaque_schema.json @@ -0,0 +1,112 @@ +{ + "$schema": "http://json-schema.org/draft-04/schema#", + "type": "object", + "properties": { + "prefix": { + "type": "string" + }, + "type": { + "type": "string" + }, + "location": { + "type": "string" + }, + "dependency": { + "type": "string" + }, + "headers": { + "type": "array", + "items": [ + { + "type": "string" + } + ] + }, + "capabilities": { + "type": "array", + "items": [ + { + "type": "object", + "properties": { + "_comment": { + "type": "string" + }, + "depend_on": { + "type": "string" + }, + "entry_points": { + "type": "array", + "items": [ + { + "type": "string" + }, + { + "type": "string" + }, + { + "type": "string" + } + ] + } + }, + "required": [ + "_comment", + "depend_on", + "entry_points" + ] + }, + { + "type": "object", + "properties": { + "_comment": { + "type": "string" + }, + "depend_on": { + "type": "string" + }, + "entry_points": { + "type": "array", + "items": [ + { + "type": "string" + }, + { + "type": "string" + } + ] + }, + "name": { + "type": "object", + "properties": { + "copy_key": { + "type": "string" + }, + "get_builtin_key": { + "type": "string" + } + }, + "required": [ + "copy_key", + "get_builtin_key" + ] + } + }, + "required": [ + "_comment", + "depend_on", + "entry_points", + "name" + ] + } + ] + } + }, + "required": [ + "prefix", + "type", + "location", + "dependency", + "headers", + "capabilities" + ] +} diff --git a/scripts/data_files/driver_jsons/driver_transparent_schema.json b/scripts/data_files/driver_jsons/driver_transparent_schema.json new file mode 100644 index 0000000000..dd1178d63a --- /dev/null +++ b/scripts/data_files/driver_jsons/driver_transparent_schema.json @@ -0,0 +1,106 @@ +{ + "$schema": "http://json-schema.org/draft-04/schema#", + "type": "object", + "properties": { + "prefix": { + "type": "string" + }, + "type": { + "type": "string" + }, + "dependency": { + "type": "string" + }, + "headers": { + "type": "array", + "items": [ + { + "type": "string" + } + ] + }, + "capabilities": { + "type": "array", + "items": [ + { + "type": "object", + "properties": { + "_comment": { + "type": "string" + }, + "depend_on": { + "type": "string" + }, + "entry_points": { + "type": "array", + "items": [ + { + "type": "string" + }, + { + "type": "string" + } + ] + }, + "fallback": { + "type": "boolean" + } + }, + "required": [ + "_comment", + "depend_on", + "entry_points", + "fallback" + ] + }, + { + "type": "object", + "properties": { + "_comment": { + "type": "string" + }, + "depend_on": { + "type": "string" + }, + "entry_points": { + "type": "array", + "items": [ + { + "type": "string" + } + ] + }, + "fallback": { + "type": "boolean" + }, + "name": { + "type": "object", + "properties": { + "export_public_key": { + "type": "string" + } + }, + "required": [ + "export_public_key" + ] + } + }, + "required": [ + "_comment", + "depend_on", + "entry_points", + "fallback", + "name" + ] + } + ] + } + }, + "required": [ + "prefix", + "type", + "dependency", + "headers", + "capabilities" + ] +} diff --git a/scripts/data_files/driver_jsons/mbedtls_test_opaque_driver.json b/scripts/data_files/driver_jsons/mbedtls_test_opaque_driver.json index c63e36094d..3747404559 100644 --- a/scripts/data_files/driver_jsons/mbedtls_test_opaque_driver.json +++ b/scripts/data_files/driver_jsons/mbedtls_test_opaque_driver.json @@ -1,7 +1,8 @@ -[{ +{ "prefix": "mbedtls_test", "type": "opaque", "location": "0x7fffff", + "dependency": "defined(PSA_CRYPTO_DRIVER_TEST)", "headers": ["test/drivers/test_driver.h"], "capabilities": [ { @@ -16,4 +17,4 @@ "name": {"copy_key":"mbedtls_test_opaque_copy_key", "get_builtin_key":"mbedtls_test_opaque_get_builtin_key"} } ] -}] +} diff --git a/scripts/data_files/driver_jsons/mbedtls_test_transparent_driver.json b/scripts/data_files/driver_jsons/mbedtls_test_transparent_driver.json index 1b4d615df6..52f6cd3006 100644 --- a/scripts/data_files/driver_jsons/mbedtls_test_transparent_driver.json +++ b/scripts/data_files/driver_jsons/mbedtls_test_transparent_driver.json @@ -1,6 +1,7 @@ -[{ +{ "prefix": "mbedtls_test", "type": "transparent", + "dependency": "defined(PSA_CRYPTO_DRIVER_TEST)", "headers": ["test/drivers/test_driver.h"], "capabilities": [ { @@ -18,4 +19,4 @@ } ] -}] +} diff --git a/scripts/data_files/driver_templates/psa_crypto_driver_wrappers.c.jinja b/scripts/data_files/driver_templates/psa_crypto_driver_wrappers.c.jinja index 3abd1eff10..bea02a506c 100644 --- a/scripts/data_files/driver_templates/psa_crypto_driver_wrappers.c.jinja +++ b/scripts/data_files/driver_templates/psa_crypto_driver_wrappers.c.jinja @@ -39,9 +39,13 @@ #if defined(MBEDTLS_PSA_CRYPTO_DRIVERS) {% for driver in drivers -%} /* Headers for {{driver.prefix}} {{driver.type}} driver */ +{% if driver.dependency is defined -%} +#if {{ driver.dependency }} +{% endif -%} {% for header in driver.headers -%} #include "{{ header }}" {% endfor %} +#endif {% endfor %} #endif /* MBEDTLS_PSA_CRYPTO_DRIVERS */ /* END-driver headers */ diff --git a/scripts/generate_driver_wrappers.py b/scripts/generate_driver_wrappers.py index 8e58b66f51..260be7edd8 100755 --- a/scripts/generate_driver_wrappers.py +++ b/scripts/generate_driver_wrappers.py @@ -23,6 +23,8 @@ import sys import os import json +from jsonschema import validate +from typing import Tuple import argparse import jinja2 from mbedtls_dev import build_tree @@ -51,26 +53,48 @@ def generate_driver_wrapper_file(template_dir: str, output_dir: str, driver_json with open(os.path.join(output_dir, "psa_crypto_driver_wrappers.c"), 'w') as out_file: out_file.write(result) -def validate_mergedjson(merged_driverjson: list) -> int: - """ - Validate the merged Driver JSON for errors that we can catch early - """ - return 0 - -def merge_driverjsonfiles(json_directory: str, jsondriverlistName: str) -> list: +def validate_json(driverjson_data: list, driverschema: list) -> bool: """ - Merge driver JSON files into a single ordered JSON. + Validate the Driver JSON against schema + """ + try: + validate(instance = driverjson_data, schema = driverschema) + except jsonschema.exceptions.ValidationError as err: + print(err) + err = "The driver JSON data is InValid" + return False + + message = "The driver JSON data is Valid" + return True + +def merge_driverjsonfiles(mbedtls_root: str, json_directory: str, jsondriverlistName: str) -> Tuple[bool,list]: + """ + Merge driver JSON files into a single ordered JSON after validation. """ result = list() driverlist = list() + with open(os.path.join(mbedtls_root, 'scripts/data_files/driver_jsons/driver_transparent_schema.json'), 'r') as file: + transparent_driver_schema = json.load(file) + with open(os.path.join(mbedtls_root, 'scripts/data_files/driver_jsons/driver_opaque_schema.json'), 'r') as file: + opaque_driver_schema = json.load(file) + with open(os.path.join(json_directory, jsondriverlistName), 'r') as driverlistfile: driverlist = json.load(driverlistfile) for file_name in driverlist: with open(os.path.join(json_directory, file_name), 'r') as infile: - result.extend(json.load(infile)) - - return result + json_data = json.load(infile) + if json_data['type'] == 'transparent': + ret = validate_json(json_data, transparent_driver_schema) + elif json_data['type'] == 'opaque': + ret = validate_json(json_data, opaque_driver_schema) + else: + ret = False + print("Unknown Driver type") + if ret == False: + return ret, [] + result.append(json_data) + return True, result def main() -> int: @@ -99,12 +123,9 @@ def main() -> int: json_directory = args.json_dir # load list of driver jsons from driverlist.json - merged_driverjson = merge_driverjsonfiles(json_directory, 'driverlist.json') - ret = validate_mergedjson(merged_driverjson) - if ret == 1: - print("Validation failed ") + ret, merged_driverjson = merge_driverjsonfiles(mbedtls_root, json_directory, 'driverlist.json') + if ret == False: return 1 - generate_driver_wrapper_file(template_directory, output_directory, merged_driverjson) return 0 diff --git a/tests/docker/bionic/Dockerfile b/tests/docker/bionic/Dockerfile index 28d33b7553..5580679754 100644 --- a/tests/docker/bionic/Dockerfile +++ b/tests/docker/bionic/Dockerfile @@ -64,6 +64,10 @@ RUN apt-get update \ RUN python3 -m pip install \ jinja2==2.10.1 types-jinja2 + +RUN python3 -m pip install \ + jsonschema + # Build a static, legacy openssl from sources with sslv3 enabled # Based on https://gist.github.com/bmaupin/8caca3a1e8c3c5686141 (build-openssl.sh) # Note: openssl-1.0.2 and earlier has known build issues with parallel make.