mirror of
https://github.com/Mbed-TLS/mbedtls.git
synced 2025-07-28 00:21:48 +03:00
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 <archana.madhavan@silabs.com> Signed-off-by: Asfandyar Orakzai <asfandyar.orakzai@silabs.com>
This commit is contained in:
committed by
Asfandyar Orakzai
parent
05efa17549
commit
04cfe34639
112
scripts/data_files/driver_jsons/driver_opaque_schema.json
Normal file
112
scripts/data_files/driver_jsons/driver_opaque_schema.json
Normal file
@ -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"
|
||||
]
|
||||
}
|
106
scripts/data_files/driver_jsons/driver_transparent_schema.json
Normal file
106
scripts/data_files/driver_jsons/driver_transparent_schema.json
Normal file
@ -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"
|
||||
]
|
||||
}
|
@ -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"}
|
||||
}
|
||||
]
|
||||
}]
|
||||
}
|
||||
|
@ -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 @@
|
||||
}
|
||||
|
||||
]
|
||||
}]
|
||||
}
|
||||
|
@ -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 */
|
||||
|
@ -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
|
||||
|
@ -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.
|
||||
|
Reference in New Issue
Block a user