Update Initialization tool
[aaf/sshsm.git] / bin / abrmdcontainer / create_primary.sh
1 #!/bin/sh
2
3 # Utility Script to create a primary key
4 # Uses TCTI as device
5 # It takes three arguments, a STORAGE HANDLE, the RH_OWNER Password and the KEY PASSWORD
6 SRKHANDLE=$1
7 O_PASSWORD=$2
8 KEY_PASSWORD=$3
9
10 # TPM Startup
11 echo "tpm2_startup --clear -T device --verbose"
12 tpm2_startup --clear -T device --verbose
13 if [ $? -ne 0 ]; then echo; echo -e "${RED}Error, Exit.";
14 error=$(echo "TPM Startup failed"); flag="0";
15 echo "flag:${flag}" >> ${WORKDIR}/tpm_status.yaml;
16 echo "error:${error}" >> ${WORKDIR}/tpm_status.yaml;
17 exit 1;
18 fi
19 echo ""
20
21 #Check if Primary Key already exists
22 echo "tpm2_readpublic -H ${SRKHANDLE} --opu out_primary_public -T device --verbose"
23 tpm2_readpublic -H ${SRKHANDLE} --opu out_primary_public -T device -V
24
25 if [ $? -ne 0 ]; then echo; echo -e "${YELLOW} Primary Key does not exist, creating...";
26     rm -f PrimaryKeyBlob
27     echo "tpm2_createprimary -P ${O_PASSWORD} -K ${KEY_PASSWORD} -H o -g 0x000B
28         -G 0x0001 -T device -V -C PrimaryKeyBlob"
29
30     tpm2_createprimary -P ${O_PASSWORD} -K ${KEY_PASSWORD} -H o -g 0x000B \
31         -G 0x0001 -T device -V -C PrimaryKeyBlob
32
33     if [ $? -ne 0 ]; then echo; echo -e "${RED}Error, Exit.";
34         error=$(echo "Error: TPM create Primary key failed");
35         echo "$error"; flag="0";
36         echo "flag:${flag}" >> ${WORKDIR}/tpm_status.yaml;
37         echo "error:${error}" >> ${WORKDIR}/tpm_status.yaml;
38         exit 1;
39     fi
40     echo ""
41
42
43     #Store Primary Key in TPMs NV RAM
44     echo "tpm2_evictcontrol -A o -c ./PrimaryKeyBlob -S ${SRKHANDLE}
45         -T device -V -P ${O_PASSWORD}"
46
47     tpm2_evictcontrol -A o -c ./PrimaryKeyBlob -S ${SRKHANDLE} \
48         -T device -V -P ${O_PASSWORD}
49
50     if [ $? -ne 0 ]; then echo; echo -e "${RED}Error, Exit.";
51     error=$(echo "Error: Inserting Primary Key failed");
52     echo "$error"; flag="0";
53     echo "flag:${flag}" >> ${WORKDIR}/tpm_status.yaml;
54     echo "errror:${error}" >> ${WORKDIR}/tpm_status.yaml;
55     rm -f PrimaryKeyBlob
56     exit 1;
57     fi
58     echo ""
59     rm -f PrimaryKeyBlob
60 fi
61
62 #END