123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161 |
- source helpers.sh
- alg_primary_obj=sha256
- alg_primary_key=ecc
- alg_create_obj=sha256
- pcr_specification=sha256:0,1,2,3+sha1:0,1,2,3
- file_pcr_value=pcr.bin
- file_input_data=secret.data
- file_policy=policy.data
- file_primary_key_ctx=context.p_"$alg_primary_obj"_"$alg_primary_key"
- file_unseal_key_pub=opu_"$alg_create_obj"
- file_unseal_key_priv=opr_"$alg_create_obj"
- file_unseal_key_ctx=ctx_load_out_"$alg_primary_obj"_"$alg_primary_key"-\
- "$alg_create_obj"
- file_unseal_key_name=name.load_"$alg_primary_obj"_"$alg_primary_key"-\
- "$alg_create_obj"
- file_unseal_output_data=usl_"$file_unseal_key_ctx"
- secret="12345678"
- cleanup() {
- rm -f $file_input_data $file_primary_key_ctx $file_unseal_key_pub \
- $file_unseal_key_priv $file_unseal_key_ctx $file_unseal_key_name \
- $file_unseal_output_data $file_pcr_value $file_policy
- if [ "$1" != "no-shut-down" ]; then
- shut_down
- fi
- }
- trap cleanup EXIT
- start_up
- cleanup "no-shut-down"
- echo $secret > $file_input_data
- tpm2 clear
- tpm2 createprimary -Q -C e -g $alg_primary_obj -G $alg_primary_key \
- -c $file_primary_key_ctx
- tpm2 create -Q -g $alg_create_obj -u $file_unseal_key_pub \
- -r $file_unseal_key_priv -i $file_input_data -C $file_primary_key_ctx
- tpm2 load -Q -C $file_primary_key_ctx -u $file_unseal_key_pub \
- -r $file_unseal_key_priv -n $file_unseal_key_name -c $file_unseal_key_ctx
- tpm2 unseal -Q -c $file_unseal_key_ctx -o $file_unseal_output_data
- cmp -s $file_unseal_output_data $file_input_data
- rm $file_unseal_key_pub $file_unseal_key_priv $file_unseal_key_name \
- $file_unseal_key_ctx
- cat $file_input_data | tpm2 create -Q -g $alg_create_obj \
- -u $file_unseal_key_pub -r $file_unseal_key_priv -i- -C $file_primary_key_ctx
- tpm2 load -Q -C $file_primary_key_ctx -u $file_unseal_key_pub \
- -r $file_unseal_key_priv -n $file_unseal_key_name -c $file_unseal_key_ctx
- tpm2 unseal -Q -c $file_unseal_key_ctx -o $file_unseal_output_data
- cmp -s $file_unseal_output_data $file_input_data
- rm $file_unseal_key_pub $file_unseal_key_priv $file_unseal_key_name \
- $file_unseal_key_ctx
- tpm2 pcrread -Q -o $file_pcr_value $pcr_specification
- tpm2 createpolicy -Q --policy-pcr -l $pcr_specification -f $file_pcr_value \
- -L $file_policy
- tpm2 create -Q -g $alg_create_obj -u $file_unseal_key_pub \
- -r $file_unseal_key_priv -i- -C $file_primary_key_ctx -L $file_policy \
- -a 'fixedtpm|fixedparent' <<< $secret
- tpm2 load -Q -C $file_primary_key_ctx -u $file_unseal_key_pub \
- -r $file_unseal_key_priv -n $file_unseal_key_name -c $file_unseal_key_ctx
- unsealed=`tpm2 unseal -V --object-context $file_unseal_key_ctx \
- -p pcr:$pcr_specification=$file_pcr_value`
- test "$unsealed" == "$secret"
- trap - ERR
- tpm2 unseal -c $file_unseal_key_ctx 2> /dev/null
- if [ $? != 1 ]; then
- echo "tpm2 unseal didn't fail without a PCR policy!"
- exit 1
- fi
- tpm2 pcrextend 0:sha1=6c10289a8da7f774cf67bd2fc8502cd4b585346a
- tpm2 unseal -c $file_unseal_key_ctx -p pcr:$pcr_specification 2> /dev/null
- if [ $? != 1 ]; then
- echo "tpm2 unseal didn't fail with a PCR state different than the policy!"
- exit 1
- fi
- trap onerror ERR
- rm $file_unseal_key_pub $file_unseal_key_priv $file_unseal_key_name \
- $file_unseal_key_ctx
- tpm2 pcrread -Q -o $file_pcr_value $pcr_specification
- tpm2 createpolicy -Q --policy-pcr -l $pcr_specification -f $file_pcr_value \
- -L $file_policy
- tpm2 create -Q -g $alg_create_obj -u $file_unseal_key_pub \
- -r $file_unseal_key_priv -i- -C $file_primary_key_ctx -L $file_policy \
- -p secretpass <<< $secret
- tpm2 load -Q -C $file_primary_key_ctx -u $file_unseal_key_pub \
- -r $file_unseal_key_priv -n $file_unseal_key_name -c $file_unseal_key_ctx
- unsealed=`tpm2 unseal -c $file_unseal_key_ctx -p secretpass`
- test "$unsealed" == "$secret"
- trap - ERR
- tpm2 unseal -c $file_unseal_key_ctx -p wrongpass 2> /dev/null
- if [ $? != 3 ]; then
- echo "tpm2 unseal didn't fail when using a wrong object password!"
- exit 1
- fi
- trap onerror ERR
- tpm2 createprimary -Q -C o -c prim.ctx
- tpm2 startauthsession -S enc_session.ctx --hmac-session -c prim.ctx
- tpm2 sessionconfig enc_session.ctx --disable-encrypt
- tpm2 create -Q -C prim.ctx -u seal_key.pub -r seal_key.priv -c seal_key.ctx \
- -p sealkeypass -i- <<< $secret -S enc_session.ctx
- tpm2 sessionconfig enc_session.ctx --enable-encrypt
- unsealed=`tpm2 unseal -c seal_key.ctx -p sealkeypass -S enc_session.ctx`
- test "$unsealed" == "$secret"
- tpm2 flushcontext enc_session.ctx
- exit 0
|