|
| 1 | +import os |
| 2 | +import sys |
| 3 | +import logging |
| 4 | +from ndn.utils import timestamp |
| 5 | +from ndn.encoding import Name, Component |
| 6 | +from ndn.security import TpmFile, KeychainSqlite3 |
| 7 | +from ndn.app import NDNApp |
| 8 | +from ndn.app_support.light_versec import compile_lvs, Checker, DEFAULT_USER_FNS |
| 9 | + |
| 10 | + |
| 11 | +logging.basicConfig(format='[{asctime}]{levelname}:{message}', |
| 12 | + datefmt='%Y-%m-%d %H:%M:%S', |
| 13 | + level=logging.INFO, |
| 14 | + style='{') |
| 15 | + |
| 16 | +lvs_text = r''' |
| 17 | +#KEY: "KEY"/_/_/_ |
| 18 | +#site: "lvs-test" |
| 19 | +#article: #site/"article"/author/post/_version & {_version: $eq_type("v=0")} <= #author |
| 20 | +#author: #site/"author"/author/"KEY"/_/admin/_ <= #admin |
| 21 | +#admin: #site/"admin"/admin/#KEY <= #root |
| 22 | +#root: #site/#KEY |
| 23 | +''' |
| 24 | + |
| 25 | + |
| 26 | +def main(): |
| 27 | + basedir = os.path.dirname(os.path.abspath(sys.argv[0])) |
| 28 | + tpm_path = os.path.join(basedir, 'privKeys') |
| 29 | + pib_path = os.path.join(basedir, 'pib.db') |
| 30 | + keychain = KeychainSqlite3(pib_path, TpmFile(tpm_path)) |
| 31 | + |
| 32 | + trust_anchor = keychain['/lvs-test'].default_key().default_cert() |
| 33 | + admin_cert = keychain['/lvs-test/admin/ndn'].default_key().default_cert() |
| 34 | + author_cert = keychain['/lvs-test/author/xinyu'].default_key().default_cert() |
| 35 | + print(f'Trust anchor name: {Name.to_str(trust_anchor.name)}') |
| 36 | + print(f'Admin name: {Name.to_str(admin_cert.name)}') |
| 37 | + print(f'Author name: {Name.to_str(author_cert.name)}') |
| 38 | + |
| 39 | + lvs_model = compile_lvs(lvs_text) |
| 40 | + checker = Checker(lvs_model, DEFAULT_USER_FNS) |
| 41 | + # The following manual checks are listed for demonstration only. |
| 42 | + # In real implementation they are automatically done |
| 43 | + root_of_trust = checker.root_of_trust() |
| 44 | + print(f'LVS model root of trust: {root_of_trust}') |
| 45 | + print(f'LVS model user functions provided: {checker.validate_user_fns()}') |
| 46 | + ta_matches = sum((m[0] for m in checker.match(trust_anchor.name)), start=[]) |
| 47 | + assert len(ta_matches) > 0 |
| 48 | + assert root_of_trust.issubset(ta_matches) |
| 49 | + print(f'Trust anchor matches the root of trust: OK') |
| 50 | + |
| 51 | + app = NDNApp(keychain=keychain) |
| 52 | + |
| 53 | + @app.route('/lvs-test/article/xinyu/hello') |
| 54 | + def on_interest(name, param, _app_param): |
| 55 | + print(f'>> I: {Name.to_str(name)}, {param}') |
| 56 | + content = "Hello,".encode() |
| 57 | + data_name = name + [Component.from_version(timestamp())] |
| 58 | + app.put_data(data_name, content=content, freshness_period=10000) |
| 59 | + print(f'<< D: {Name.to_str(data_name)}') |
| 60 | + print(f'Content: {content.decode()}') |
| 61 | + print('') |
| 62 | + |
| 63 | + @app.route('/lvs-test/article/xinyu/world') |
| 64 | + def on_interest(name, param, _app_param): |
| 65 | + print(f'>> I: {Name.to_str(name)}, {param}') |
| 66 | + content = "world!".encode() |
| 67 | + data_name = name + [Component.from_version(timestamp())] |
| 68 | + app.put_data(data_name, content=content, freshness_period=10000) |
| 69 | + print(f'<< D: {Name.to_str(data_name)}') |
| 70 | + print(f'Content: {content.decode()}') |
| 71 | + print('') |
| 72 | + |
| 73 | + @app.route(trust_anchor.name) |
| 74 | + def on_interest(name, param, _app_param): |
| 75 | + print(f'>> I: {Name.to_str(name)}, {param}') |
| 76 | + app.put_raw_packet(trust_anchor.data) |
| 77 | + print(f'<< D: {Name.to_str(trust_anchor.name)}') |
| 78 | + print('') |
| 79 | + |
| 80 | + @app.route(admin_cert.name) |
| 81 | + def on_interest(name, param, _app_param): |
| 82 | + print(f'>> I: {Name.to_str(name)}, {param}') |
| 83 | + app.put_raw_packet(admin_cert.data) |
| 84 | + print(f'<< D: {Name.to_str(admin_cert.name)}') |
| 85 | + print('') |
| 86 | + |
| 87 | + @app.route(author_cert.name) |
| 88 | + def on_interest(name, param, _app_param): |
| 89 | + print(f'>> I: {Name.to_str(name)}, {param}') |
| 90 | + app.put_raw_packet(author_cert.data) |
| 91 | + print(f'<< D: {Name.to_str(author_cert.name)}') |
| 92 | + print('') |
| 93 | + |
| 94 | + print('Start serving ...') |
| 95 | + app.run_forever() |
| 96 | + |
| 97 | + |
| 98 | +if __name__ == '__main__': |
| 99 | + main() |
0 commit comments