-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Sample program demo scripts #2698
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
gilles-peskine-arm
wants to merge
22
commits into
Mbed-TLS:development
Choose a base branch
from
gilles-peskine-arm:sample_program_demo_scripts-development
base: development
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
35b9e58
Add missing compile-time guard
gilles-peskine-arm 3ecc807
Remove spurious dependencies on RSA and BIGNUM
gilles-peskine-arm fa1cb71
Run demo scripts and check that they work
gilles-peskine-arm 1b4f4e2
Demo script for cert_write and cert_req
gilles-peskine-arm 7603ac2
Not all .sh, .pl or .py files are executables
gilles-peskine-arm 744dad3
Move common code of demo scripts into a library
gilles-peskine-arm 6fdf50b
Demo scripts: create a seedfile if the configuration requires it
gilles-peskine-arm 8ad0400
Let demo scripts declare their dependencies
gilles-peskine-arm 964af03
Declare the dependencies of cert_write_demo.sh
gilles-peskine-arm 62d35f4
Declare the dependencies of key_ladder_demo.sh
gilles-peskine-arm 131e98b
Run demo scripts in some builds
gilles-peskine-arm 1884d87
Use cert_app instead of openssl
gilles-peskine-arm 7c75abd
cleanup is part of the external interface
gilles-peskine-arm a81b28c
Print only missing dependencies
gilles-peskine-arm af3249b
Explain why $root_dir needs a complicated calculation
gilles-peskine-arm c203212
Minor readability improvements
gilles-peskine-arm 0910d43
Make should_be_executable a separate method
gilles-peskine-arm fe6e8a3
Fix some mistakes in descriptive messages
gilles-peskine-arm 0277803
Add --quiet option to suppress demos' output
gilles-peskine-arm b5b5a39
Error out if run from the wrong directory
gilles-peskine-arm 041e694
Make --quiet a little less quiet
gilles-peskine-arm a95761f
Pacify Pylint
gilles-peskine-arm File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,137 @@ | ||
| ## Common shell functions used by demo scripts programs/*/*.sh. | ||
|
|
||
| ## How to write a demo script | ||
| ## ========================== | ||
| ## | ||
| ## Include this file near the top of each demo script: | ||
| ## . "${0%/*}/../demo_common.sh" | ||
| ## | ||
| ## Start with a "msg" call that explains the purpose of the script. | ||
| ## Then call the "depends_on" function to ensure that all config | ||
| ## dependencies are met. | ||
| ## | ||
| ## As the last thing in the script, call the cleanup function. | ||
| ## | ||
| ## You can use the functions and variables described below. | ||
|
|
||
| set -e -u | ||
|
|
||
| ## $root_dir is the root directory of the Mbed TLS source tree. | ||
| root_dir="${0%/*}" | ||
| # Find a nice path to the root directory, avoiding unnecessary "../". | ||
| # The code supports demo scripts nested up to 4 levels deep. | ||
| # The code works no matter where the demo script is relative to the current | ||
| # directory, even if it is called with a relative path. | ||
| n=4 # limit the search depth | ||
| while ! [ -d "$root_dir/programs" ] || ! [ -d "$root_dir/library" ]; do | ||
| if [ $n -eq 0 ]; then | ||
| echo >&2 "This doesn't seem to be an Mbed TLS source tree." | ||
| exit 125 | ||
| fi | ||
| n=$((n - 1)) | ||
| case $root_dir in | ||
| .) root_dir="..";; | ||
| ..|?*/..) root_dir="$root_dir/..";; | ||
| ?*/*) root_dir="${root_dir%/*}";; | ||
| /*) root_dir="/";; | ||
| *) root_dir=".";; | ||
| esac | ||
mpg marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| done | ||
|
|
||
| ## $programs_dir is the directory containing the sample programs. | ||
| # Assume an in-tree build. | ||
| programs_dir="$root_dir/programs" | ||
|
|
||
| ## msg LINE... | ||
| ## msg <TEXT_ORIGIN | ||
| ## Display an informational message. | ||
| msg () { | ||
| if [ $# -eq 0 ]; then | ||
| sed 's/^/# /' | ||
| else | ||
| for x in "$@"; do | ||
| echo "# $x" | ||
| done | ||
| fi | ||
| } | ||
|
|
||
| ## run "Message" COMMAND ARGUMENT... | ||
| ## Display the message, then run COMMAND with the specified arguments. | ||
| run () { | ||
| echo | ||
| echo "# $1" | ||
| shift | ||
| echo "+ $*" | ||
| "$@" | ||
| } | ||
|
|
||
| ## Like '!', but stop on failure with 'set -e' | ||
| not () { | ||
| if "$@"; then false; fi | ||
| } | ||
|
|
||
| ## run_bad "Message" COMMAND ARGUMENT... | ||
| ## Like run, but the command is expected to fail. | ||
| run_bad () { | ||
| echo | ||
| echo "$1 This must fail." | ||
| shift | ||
| echo "+ ! $*" | ||
| not "$@" | ||
| } | ||
|
|
||
| ## config_has SYMBOL... | ||
| ## Succeeds if the library configuration has all SYMBOLs set. | ||
| config_has () { | ||
| for x in "$@"; do | ||
| "$programs_dir/test/query_compile_time_config" "$x" | ||
| done | ||
| } | ||
|
|
||
| ## depends_on SYMBOL... | ||
| ## Exit if the library configuration does not have all SYMBOLs set. | ||
| depends_on () { | ||
| m= | ||
| for x in "$@"; do | ||
| if ! config_has "$x"; then | ||
| m="$m $x" | ||
| fi | ||
| done | ||
| if [ -n "$m" ]; then | ||
| cat >&2 <<EOF | ||
| $0: this demo requires the following | ||
| configuration options to be enabled at compile time: | ||
| $m | ||
| EOF | ||
| # Exit with a success status so that this counts as a pass for run_demos.py. | ||
| exit | ||
| fi | ||
| } | ||
|
|
||
| ## Add the names of files to clean up to this whitespace-separated variable. | ||
| ## The file names must not contain whitespace characters. | ||
| files_to_clean= | ||
|
|
||
| ## Call this function at the end of each script. | ||
| ## It is called automatically if the script is killed by a signal. | ||
| cleanup () { | ||
| rm -f -- $files_to_clean | ||
| } | ||
|
|
||
|
|
||
|
|
||
| ################################################################ | ||
| ## End of the public interfaces. Code beyond this point is not | ||
| ## meant to be called directly from a demo script. | ||
|
|
||
| trap 'cleanup; trap - HUP; kill -HUP $$' HUP | ||
| trap 'cleanup; trap - INT; kill -INT $$' INT | ||
| trap 'cleanup; trap - TERM; kill -TERM $$' TERM | ||
|
|
||
| if config_has MBEDTLS_ENTROPY_NV_SEED; then | ||
| # Create a seedfile that's sufficiently long in all library configurations. | ||
| # This is necessary for programs that use randomness. | ||
| # Assume that the name of the seedfile is the default name. | ||
| files_to_clean="$files_to_clean seedfile" | ||
| dd if=/dev/urandom of=seedfile ibs=64 obs=64 count=1 | ||
| fi | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| #!/bin/sh | ||
| . "${0%/*}/../demo_common.sh" | ||
|
|
||
| msg <<'EOF' | ||
| This script demonstrates the use of x509 tools to generate a certificate | ||
| for a private key. It shows both the case of a self-signed certificate and | ||
| the case of a certificate signed by a different key using a certificate | ||
| signing request. | ||
| EOF | ||
|
|
||
| depends_on MBEDTLS_CTR_DRBG_C MBEDTLS_ENTROPY_C MBEDTLS_ERROR_C MBEDTLS_FS_IO MBEDTLS_PEM_WRITE_C MBEDTLS_PK_PARSE_C MBEDTLS_PK_WRITE_C MBEDTLS_SHA256_C MBEDTLS_X509_CRT_PARSE_C MBEDTLS_X509_CRT_WRITE_C MBEDTLS_X509_CSR_PARSE_C MBEDTLS_X509_CSR_WRITE_C | ||
|
|
||
| ca_key="demo_ca.key" | ||
| server_key="demo_server.key" | ||
| csr="demo_csr.req" | ||
| ca_crt="demo_ca.crt" | ||
| server_crt="demo_server.crt" | ||
|
|
||
| files_to_clean="$ca_key $server_key $csr $ca_crt $server_crt" | ||
|
|
||
| run 'Generate a CA (certificate authority) key.' \ | ||
| "$programs_dir/pkey/gen_key" filename="$ca_key" type=ec | ||
|
|
||
| run 'Self-sign the CA certificate.' \ | ||
| "$programs_dir/x509/cert_write" issuer_key="$ca_key" output_file="$ca_crt" \ | ||
| selfsign=1 is_ca=1 \ | ||
| issuer_name="CN=Demo CA,O=Mbed TLS,C=UK" | ||
|
|
||
| run 'The CA certificate is:' \ | ||
| cat "$ca_crt" | ||
|
|
||
| run 'Dump of the CA certificate:' \ | ||
| "$programs_dir/x509/cert_app" mode=file filename="$ca_crt" | ||
|
|
||
| run 'Generate a server key.' \ | ||
| "$programs_dir/pkey/gen_key" filename="$server_key" type=ec | ||
|
|
||
| run 'Issue a signing request for the server key.' \ | ||
| "$programs_dir/x509/cert_req" filename="$server_key" output_file="$csr" \ | ||
| subject_name="CN=Demo server,O=Mbed TLS,C=UK" | ||
|
|
||
| run 'Show information about the certificate signing request' \ | ||
| "$programs_dir/x509/req_app" filename="$csr" | ||
|
|
||
| run 'The CA signs the server key.' \ | ||
| "$programs_dir/x509/cert_write" issuer_key="$ca_key" request_file="$csr" \ | ||
| output_file="$server_crt" | ||
|
|
||
| run 'The server certificate is:' \ | ||
| cat "$server_crt" | ||
|
|
||
| run 'Dump of the server certificate:' \ | ||
| "$programs_dir/x509/cert_app" mode=file filename="$server_crt" | ||
|
|
||
| cleanup |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.