fapi-policy_signed_delegation.sh 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. # In this test a backend issues two policies to a user "A":
  2. # 1. A user policy that allows "A" to authorize key usage
  3. # 2. An offline delegation policy that allows "A" to delegate his access rights
  4. # two user "B" without further interaction with the backend.
  5. # set -e
  6. source helpers.sh
  7. start_up
  8. CRYPTO_PROFILE="RSA"
  9. setup_fapi $CRYPTO_PROFILE
  10. # Extract value from JSON file
  11. function jsonValue {
  12. KEY=$1
  13. num=$2
  14. awk -F"[,:}]" '{for(i=1;i<=NF;i++){if($i~/'$KEY'\042/){print $(i+1)}}}' | tr -d '"' | sed -n ${num}p
  15. }
  16. function cleanup {
  17. # In case the test is skipped, no key is created and a
  18. # failure is expected here. Therefore, we need to pass a successful
  19. # execution in any case
  20. tss2 delete --path=/ && true
  21. shut_down
  22. }
  23. trap cleanup EXIT
  24. # The delegation test needs at least the commit
  25. # b843960b6e601a786b469832392dc0a12e13cf34 of TSS to be executed successfully.
  26. # This commit will be introduced in version > 3.0.3. Will skip the test if
  27. # version is below.
  28. if [[ "$(echo $TSS_VERSION | sed 's/[^0-9]*//g')" -le "303" && "$TSS_VERSION" != "master" ]]; then
  29. echo "TSS version does not support test"
  30. exit 077
  31. fi
  32. PATH_REVOCATION="/nv/Owner/ctr"
  33. PATH_FEATURE_KEY_SIGN="HS/SRK/signkey"
  34. # Backend Policies
  35. POLICY_AUTH_BACKEND_PEM="pol_authorize_backend_pem.json"
  36. PATH_POL_AUTHORIZE="/policy/pol_authorize"
  37. # User Policies
  38. POLICY_USER="pol_user.json"
  39. PATH_POL_USER="/policy/pol_user"
  40. POLICY_TO_BE_SIGNED_USER="pol_to_be_signed_user.json"
  41. POLICY_AUTHORIZED_USER="pol_authorized_user.json"
  42. PATH_POL_AUTHORIZED_USER="/policy/pol_authorized_user"
  43. # Offline Delegation Policy
  44. POLICY_OFFLINE_DELEGATION="pol_offline_delegation.json"
  45. PATH_POL_OFFLINE_DELEGATION="/policy/pol_offline_delegation"
  46. POLICY_TO_BE_SIGNED_OFFLINE_DELEGATION="pol_to_be_signed_offline_delegation.json"
  47. POLICY_AUTHORIZED_OFFLINE_DELEGATION="pol_authorized_offline_delegation.json"
  48. PATH_POL_AUTHORIZED_OFFLINE_DELEGATION="/policy/pol_authorized_offline_delegation"
  49. # Sub Policy
  50. POLICY_SUB_POLICY="pol_sub_policy.json"
  51. PATH_POL_SUB_POLICY="/policy/pol_sub_policy"
  52. POLICY_TO_BE_SIGNED_SUB_POLICY="pol_to_be_signed_sub_policy.json"
  53. POLICY_AUTHORIZED_SUB_POLICY="pol_authorized_sub_policy.json"
  54. PATH_POL_AUTHORIZED_SUB_POLICY="/policy/pol_authorized_sub_policy"
  55. # Data for Authorization
  56. TEST_SIGNATURE_FILE=$TEMP_DIR/test_signature.file
  57. OUTPUT_FILE=$TEMP_DIR/data2sign.file
  58. SIGNATURE_FILE=$TEMP_DIR/signature.file
  59. search_string="\"policy\":["
  60. LOG_FILE=$TEMP_DIR/log.file
  61. touch $LOG_FILE
  62. EMPTY_FILE=$TEMP_DIR/empty.file
  63. BIG_FILE=$TEMP_DIR/big_file.file
  64. # 0. Create Keys
  65. openssl ecparam -name secp256r1 -genkey -noout -out $TEMP_DIR/key_backend_priv.pem
  66. openssl ec -in $TEMP_DIR/key_backend_priv.pem -pubout -out $TEMP_DIR/key_backend_pub.pem
  67. openssl ecparam -name secp256r1 -genkey -noout -out $TEMP_DIR/key_user_priv.pem
  68. openssl ec -in $TEMP_DIR/key_user_priv.pem -pubout -out $TEMP_DIR/key_user_pub.pem
  69. openssl ecparam -name secp256r1 -genkey -noout -out $TEMP_DIR/key_delegated_priv.pem
  70. openssl ec -in $TEMP_DIR/key_delegated_priv.pem -pubout -out $TEMP_DIR/key_delegated_pub.pem
  71. # 1. Create necessary policy templates
  72. ## Backend Authorization Policy
  73. cat <<EOF > $TEMP_DIR/$POLICY_AUTH_BACKEND_PEM
  74. {
  75. "description":"Initial Authorization Policy",
  76. "policy":[
  77. {
  78. "type": "POLICYAUTHORIZE",
  79. "policyRef": [ 0, 2, 3, 4, 5 ],
  80. "keyPEM": "`cat $TEMP_DIR/key_backend_pub.pem`"
  81. }
  82. ]
  83. }
  84. EOF
  85. # USER POLICY
  86. cat <<EOF > $TEMP_DIR/$POLICY_USER
  87. {
  88. "description": "User Policy",
  89. "policy": [
  90. {
  91. "type": "NV",
  92. "nvPath": "`echo $PATH_REVOCATION`",
  93. "operandB": "0000000000000002",
  94. "operation": "neq"
  95. },
  96. {
  97. "type": "CounterTimer",
  98. "operandB": "5000",
  99. "operation": "signed_lt"
  100. },
  101. {
  102. "type": "Signed",
  103. "keyPEM": "`cat $TEMP_DIR/key_user_pub.pem`",
  104. "keyPEMhashAlg": "SHA256"
  105. }
  106. ]
  107. }
  108. EOF
  109. ## Offline Delegation Policy
  110. cat <<EOF > $TEMP_DIR/$POLICY_OFFLINE_DELEGATION
  111. {
  112. "description":"Offline Delegation",
  113. "policy":[
  114. {
  115. "type": "POLICYAUTHORIZE",
  116. "policyRef": [ 5, 2, 3, 4, 5 ],
  117. "keyPEM": "`cat $TEMP_DIR/key_user_pub.pem`"
  118. },
  119. {
  120. "type": "NV",
  121. "nvPath": "`echo $PATH_REVOCATION`",
  122. "operandB": "0000000000000002",
  123. "operation": "neq"
  124. }
  125. ]
  126. }
  127. EOF
  128. ## SUB POLICY (will be signed by user)
  129. cat <<EOF > $TEMP_DIR/$POLICY_SUB_POLICY
  130. {
  131. "description": "Sub Policy",
  132. "policy": [
  133. {
  134. "type": "CounterTimer",
  135. "operandB": "5000",
  136. "operation": "signed_lt"
  137. },
  138. {
  139. "type": "Signed",
  140. "keyPEM": "`cat $TEMP_DIR/key_delegated_pub.pem`",
  141. "keyPEMhashAlg": "SHA256"
  142. }
  143. ]
  144. }
  145. EOF
  146. # 2. Create Setting
  147. tss2 provision
  148. ## Create feature key with authorize policy
  149. tss2 import --path $PATH_POL_AUTHORIZE --importData $TEMP_DIR/$POLICY_AUTH_BACKEND_PEM
  150. tss2 createkey --path $PATH_FEATURE_KEY_SIGN --type="sign, noda, 0x81000002" --policyPath $PATH_POL_AUTHORIZE --authValue ""
  151. ## Create revocation counter
  152. tss2 createnv --path $PATH_REVOCATION --type=counter --authValue ""
  153. tss2 nvincrement --nvPath $PATH_REVOCATION
  154. # 3. Backend creates User Policy and Offline Delegation Policy
  155. ## 3.1 Get digests of policies and sign for authorization
  156. ### Sign digest of User Policy with key_backend_priv
  157. tss2 import --path $PATH_POL_USER --importData $TEMP_DIR/$POLICY_USER
  158. tss2 createkey --path HS/SRK/tmpkey --policyPath $PATH_POL_USER --type="sign, noda" --authValue ""
  159. tss2 exportpolicy --path HS/SRK/tmpkey --jsonPolicy $TEMP_DIR/$POLICY_TO_BE_SIGNED_USER -f
  160. tss2 delete --path HS/SRK/tmpkey
  161. tss2 delete --path $PATH_POL_USER
  162. digest_user=`cat $TEMP_DIR/$POLICY_TO_BE_SIGNED_USER | jsonValue "digest" 1`
  163. digest_user="$digest_user""0002030405"
  164. echo -n $digest_user | \
  165. xxd -r -p | openssl dgst -sha256 -sign $TEMP_DIR/key_backend_priv.pem -hex | \
  166. sed 's/^.* //' > signed_user_policy.sig
  167. ### Sign digest of Offline Delegation Policy with key_backend_priv
  168. tss2 import --path $PATH_POL_OFFLINE_DELEGATION --importData $TEMP_DIR/$POLICY_OFFLINE_DELEGATION
  169. tss2 createkey --path HS/SRK/tmpkey --policyPath $PATH_POL_OFFLINE_DELEGATION --type="sign, noda" --authValue ""
  170. tss2 exportpolicy --path HS/SRK/tmpkey --jsonPolicy $TEMP_DIR/$POLICY_TO_BE_SIGNED_OFFLINE_DELEGATION -f
  171. tss2 delete --path HS/SRK/tmpkey
  172. tss2 delete --path $PATH_POL_OFFLINE_DELEGATION
  173. digest_offline_delegation=`cat $TEMP_DIR/$POLICY_TO_BE_SIGNED_OFFLINE_DELEGATION | jsonValue "digest" 1`
  174. digest_offline_delegation="$digest_offline_delegation""0002030405"
  175. echo -n $digest_offline_delegation | \
  176. xxd -r -p | openssl dgst -sha256 -sign $TEMP_DIR/key_backend_priv.pem -hex | \
  177. sed 's/^.* //' > signed_policy_offline_delegation.sig
  178. ### Sign digest of Sub Policy with key_user_priv
  179. tss2 import --path $PATH_POL_SUB_POLICY --importData $TEMP_DIR/$POLICY_SUB_POLICY
  180. tss2 createkey --path HS/SRK/tmpkey --policyPath $PATH_POL_SUB_POLICY --type="sign, noda" --authValue ""
  181. tss2 exportpolicy --path HS/SRK/tmpkey --jsonPolicy $TEMP_DIR/$POLICY_TO_BE_SIGNED_SUB_POLICY -f
  182. tss2 delete --path HS/SRK/tmpkey
  183. tss2 delete --path $PATH_POL_SUB_POLICY
  184. digest_sub_policy=`cat $TEMP_DIR/$POLICY_TO_BE_SIGNED_SUB_POLICY | jsonValue "digest" 1`
  185. digest_sub_policy="$digest_sub_policy""0502030405"
  186. echo -n $digest_sub_policy | \
  187. xxd -r -p | openssl dgst -sha256 -sign $TEMP_DIR/key_user_priv.pem -hex | \
  188. sed 's/^.* //' > signed_policy_sub_policy.sig
  189. ## 3.2 Create Authorized Policies from Template
  190. ### Create Authorized User Policy
  191. POLICY_AUTH_TEMPLATE_USER=$(cat <<EOF
  192. "policyAuthorizations":[
  193. {
  194. "type": "pem",
  195. "policyRef": [ 0, 2, 3, 4, 5 ],
  196. "key": "`cat $TEMP_DIR/key_backend_pub.pem`",
  197. "signature": "`cat signed_user_policy.sig`"
  198. }
  199. ],
  200. EOF
  201. )
  202. AUTHORIZED_POLICY_TMP=""
  203. while read line; do
  204. # reading each line
  205. if [[ $search_string == $line ]]; then
  206. AUTHORIZED_POLICY_TMP="$AUTHORIZED_POLICY_TMP"$'\n'"$POLICY_AUTH_TEMPLATE_USER"
  207. fi
  208. AUTHORIZED_POLICY_TMP="$AUTHORIZED_POLICY_TMP"$'\n'"$line"
  209. done < $TEMP_DIR/$POLICY_TO_BE_SIGNED_USER
  210. AUTHORIZED_POLICY_TMP="$AUTHORIZED_POLICY_TMP"$'\n'"}"
  211. echo "$AUTHORIZED_POLICY_TMP" > $TEMP_DIR/$POLICY_AUTHORIZED_USER
  212. ### Create Offline Delegation und Sub Policy
  213. POLICY_AUTH_TEMPLATE_OFFLINE_DELEGATION=$(cat <<EOF
  214. "policyAuthorizations":[
  215. {
  216. "type": "pem",
  217. "policyRef": [ 0, 2, 3, 4, 5 ],
  218. "key": "`cat $TEMP_DIR/key_backend_pub.pem`",
  219. "signature": "`cat signed_policy_offline_delegation.sig`"
  220. }
  221. ],
  222. EOF
  223. )
  224. CONCATENATED_POLICY=""
  225. while read line; do
  226. # reading each line
  227. if [[ $search_string == $line ]]; then
  228. CONCATENATED_POLICY="$CONCATENATED_POLICY"$'\n'"$POLICY_AUTH_TEMPLATE_OFFLINE_DELEGATION"
  229. fi
  230. CONCATENATED_POLICY="$CONCATENATED_POLICY"$'\n'"$line"
  231. done < $TEMP_DIR/$POLICY_TO_BE_SIGNED_OFFLINE_DELEGATION
  232. CONCATENATED_POLICY="$CONCATENATED_POLICY"$'\n'"}"
  233. echo "$CONCATENATED_POLICY" > $TEMP_DIR/$POLICY_AUTHORIZED_OFFLINE_DELEGATION
  234. POLICY_AUTH_TEMPLATE_SUB_POLICY=$(cat <<EOF
  235. "policyAuthorizations":[
  236. {
  237. "type": "pem",
  238. "policyRef": [ 5, 2, 3, 4, 5 ],
  239. "key": "`cat $TEMP_DIR/key_user_pub.pem`",
  240. "signature": "`cat signed_policy_sub_policy.sig`"
  241. }
  242. ],
  243. EOF
  244. )
  245. CONCATENATED_POLICY=""
  246. while read line; do
  247. # reading each line
  248. if [[ $search_string == $line ]]; then
  249. CONCATENATED_POLICY="$CONCATENATED_POLICY"$'\n'"$POLICY_AUTH_TEMPLATE_SUB_POLICY"
  250. fi
  251. CONCATENATED_POLICY="$CONCATENATED_POLICY"$'\n'"$line"
  252. done < $TEMP_DIR/$POLICY_TO_BE_SIGNED_SUB_POLICY
  253. CONCATENATED_POLICY="$CONCATENATED_POLICY"$'\n'"}"
  254. echo "$CONCATENATED_POLICY" > $TEMP_DIR/$POLICY_AUTHORIZED_SUB_POLICY
  255. ## 4. Authorize with authorized policies
  256. ### Create some digest data to sign
  257. DIGEST_FILE=$TEMP_DIR/digest.file
  258. echo -n 01234567890123456789 > $DIGEST_FILE
  259. ### Import the authorized policies
  260. tss2 import --path $PATH_POL_AUTHORIZED_USER --importData $TEMP_DIR/$POLICY_AUTHORIZED_USER
  261. tss2 import --path $PATH_POL_AUTHORIZED_OFFLINE_DELEGATION --importData $TEMP_DIR/$POLICY_AUTHORIZED_OFFLINE_DELEGATION
  262. tss2 import --path $PATH_POL_AUTHORIZED_SUB_POLICY --importData $TEMP_DIR/$POLICY_AUTHORIZED_SUB_POLICY
  263. ## 4.1 User authorizes key usage with the user policy
  264. echo "1. User authorizes key usage with the user policy"
  265. expect -c "
  266. spawn tss2 sign --keyPath=$PATH_FEATURE_KEY_SIGN --digest=$DIGEST_FILE --signature=$TEST_SIGNATURE_FILE -f
  267. expect -re \"Select a branch for P_RSA2048SHA256/HS/SRK/signkey .* Your choice:\" {
  268. set branches [split \$expect_out(buffer) \"\n\"]
  269. set lstSize [llength \$branches]
  270. set index 0
  271. foreach branch \$branches {
  272. if {[regexp -nocase \"/policy/pol_authorized_user\" \$branch]} {
  273. send \"\$index\r\"
  274. break
  275. }
  276. incr index
  277. }
  278. if {\$index >= \$lstSize} {
  279. send_user \"\nError: Branch @ index \$index not found\n\"
  280. exit 1
  281. }
  282. expect \"Filename for nonce output: \" {
  283. send \"$OUTPUT_FILE\r\"
  284. expect \"Filename for signature input: \" {
  285. exec openssl dgst -sha256 -sign $TEMP_DIR/key_user_priv.pem -out $SIGNATURE_FILE $OUTPUT_FILE
  286. send \"$SIGNATURE_FILE\r\"
  287. send_user \"\n\"
  288. }
  289. }
  290. set ret [wait]
  291. if {[lindex \$ret 2] || [lindex \$ret 3] != 0} {
  292. send_user \"\n[lindex \$ret]\n\"
  293. send_user \"Command failed\n\"
  294. exit 1
  295. }
  296. }
  297. "
  298. ## 4.2 Delegated User authorizes key usage with the offline delegation policy
  299. echo "2. Delegated User authorizes key usage with the offline delegation policy"
  300. expect -c "
  301. spawn tss2 sign --keyPath=$PATH_FEATURE_KEY_SIGN --digest=$DIGEST_FILE --signature=$TEST_SIGNATURE_FILE -f
  302. expect -re \"Select a branch for P_RSA2048SHA256/HS/SRK/signkey .* Your choice:\" {
  303. set branches [split \$expect_out(buffer) \"\n\"]
  304. set lstSize [llength \$branches]
  305. set index 0
  306. foreach branch \$branches {
  307. if {[regexp -nocase \"/policy/pol_authorized_offline_delegation\" \$branch]} {
  308. send \"\$index\r\"
  309. break
  310. }
  311. incr index
  312. }
  313. if {\$index >= \$lstSize} {
  314. send_user \"\nError: Branch @ index \$index not found\n\"
  315. exit 1
  316. }
  317. expect \"Filename for nonce output: \" {
  318. send \"$OUTPUT_FILE\r\"
  319. expect \"Filename for signature input: \" {
  320. exec openssl dgst -sha256 -sign $TEMP_DIR/key_delegated_priv.pem -out $SIGNATURE_FILE $OUTPUT_FILE
  321. send \"$SIGNATURE_FILE\r\"
  322. send_user \"\n\"
  323. }
  324. }
  325. set ret [wait]
  326. if {[lindex \$ret 2] || [lindex \$ret 3] != 0} {
  327. send_user \"\n[lindex \$ret]\n\"
  328. send_user \"Command failed 2\n\"
  329. exit 1
  330. }
  331. }
  332. "
  333. exit 0