Containerize TPM Initialization tool
[aaf/sshsm.git] / bin / abrmdcontainer / initialize_tpm.sh
1 #!/bin/sh
2
3 # 1.Environmental variables
4
5 # 1.a Location of Shared volume and Node's name
6 # These varaibles has to be made avaialble to this script
7
8 echo "Shared Volume location is $ABRMD_DATA"
9 echo "Node name is $TPM_NODE_NAME"
10
11 # 2. Create the directory with the host's name to store the output of Init tool
12 # This is demarcate the generated files which are specific to this host's TPM
13 srkhandle="$(cat ${ABRMD_DATA}/host_${TPM_NODE_NAME}/srkhandle)"
14 passphrase="$(cat ${ABRMD_DATA}/host_${TPM_NODE_NAME}/password-passphrase)"
15 echo "${passphrase}" | gpg --batch --yes --passphrase-fd 0 password.txt.gpg
16 password="$(cat ${ABRMD_DATA}/host_${TPM_NODE_NAME}/password.txt)"
17
18 # 3. Create initial Flag values which are reset upon failure
19 error="NULL"
20 flag="1"
21
22 # 4. TPM initialize
23 echo "tpm2_startup -clear -T tabrmd -V"
24 tpm2_startup -clear -T tabrmd -V
25 if [ $? -ne 0 ]; then echo; echo -e "${RED}Error, Exit.";
26 error=$(echo "TPM Startup failed"); flag="0";
27 echo "flag:${flag}" >> ${ABRMD_DATA}/host_${TPM_NODE_NAME}/tpm_status.yaml;
28 echo "error:${error}" >> ${ABRMD_DATA}/host_${TPM_NODE_NAME}/tpm_status.yaml;
29 exit 1;
30 fi
31 echo ""
32
33 # 5. Take ownership
34 echo "tpm2_takeownership -o new -e new -l new -T tabrmd -V"
35 tpm2_takeownership -o new -e new -l new -T tabrmd -V
36 if [ $? -ne 0 ]; then echo; echo -e "${RED}Error, Exit.";
37 error=$(echo "Error:TPM ownership acquire failed");flag="0";
38 echo "flag:${flag}" >> ${ABRMD_DATA}/host_${TPM_NODE_NAME}/tpm_status.yaml;
39 echo "error:${error}" >> ${ABRMD_DATA}/host_${TPM_NODE_NAME}/tpm_status.yaml;
40 echo "$error"; exit 1;
41 fi
42 echo ""
43
44 # 6. Create Primary Key in RH_OWNER hierarchy
45 rm -f PrimaryKeyBlob
46 echo "tpm2_createprimary -P $password -A o -g 0x000B -G 0x0001 -T tabrmd -V -C PrimaryKeyBlob"
47 tpm2_createprimary -P $password -A o -g 0x000B -G 0x0001 -T tabrmd -V -C PrimaryKeyBlob
48 if [ $? -ne 0 ]; then echo; echo -e "${RED}Error, Exit.";
49 error=$(echo "Error: TPM create Primary key failed");
50 echo "$error"; flag="0";
51 echo "flag:${flag}" >> ${ABRMD_DATA}/host_${TPM_NODE_NAME}/tpm_status.yaml;
52 echo "error:${error}" >> ${ABRMD_DATA}/host_${TPM_NODE_NAME}/tpm_status.yaml;
53 exit 1;
54 fi
55 echo ""
56
57 # 7. Store Primary Key in TPMs NV RAM
58 echo "tpm2_evictcontrol -A o -c ./PrimaryKeyBlob -S ${srkhandle} -T tabrmd -V -P $password"
59 tpm2_evictcontrol -A o -c ./PrimaryKeyBlob -S ${srkhandle} -T tabrmd -V -P $password
60 if [ $? -ne 0 ]; then echo; echo -e "${RED}Error, Exit.";
61 error=$(echo "Error: Inserting Primary Key failed");
62 echo "$error"; flag="0";
63 echo "flag:${flag}" >> ${ABRMD_DATA}/host_${TPM_NODE_NAME}/tpm_status.yaml;
64 echo "errror:${error}" >> ${ABRMD_DATA}/host_${TPM_NODE_NAME}/tpm_status.yaml;
65 exit 1;
66 fi
67 echo ""
68 rm -f PrimaryKeyBlob
69
70 # 8. To test, Read public portion of TPM primary key with stored handle
71 rm -f $out_primary_public
72 echo "tpm2_readpublic -H ${srkhandle} --opu out_primary_public -T tabrmd -V"
73 tpm2_readpublic -H ${srkhandle} --opu out_primary_public -T tabrmd -V
74 if [ $? -ne 0 ]; then echo; echo -e "${RED}Error, Exit.";
75 error=$(echo" Error:Reading Public part of Primary Key failed");
76 echo "$error"; flag="0";
77 echo "flag:${flag}" >> ${ABRMD_DATA}/host_${TPM_NODE_NAME}/tpm_status.yaml;
78 echo "error:${error}" >> ${ABRMD_DATA}/host_${TPM_NODE_NAME}/tpm_status.yaml;
79 exit 1;
80 fi
81 echo ""
82
83 # 9. Update the tpm_status.yaml to report the status of this Script
84 echo "flag:${flag}" >> ${ABRMD_DATA}/host_${TPM_NODE_NAME}/tpm_status.yaml
85 echo "error:${error}" >> ${ABRMD_DATA}/host_${TPM_NODE_NAME}/tpm_status.yaml
86
87 # 10. Copy the public portion of the Primary key to the Shared volume
88 # Use environment variable TPM_NODE_NAME
89 cp out_parent_public ${ABRMD_DATA}/host_${TPM_NODE_NAME}