|
| 1 | +#!/bin/bash |
| 2 | +# Getting RVPS Parameters |
| 3 | + |
| 4 | +function install_packages() { |
| 5 | + echo "***Installing necessary packages for RVPS values extraction ***" |
| 6 | + dnf install -y python3 python3-cryptography kmod |
| 7 | + echo "***Installation Finished ***" |
| 8 | +} |
| 9 | + |
| 10 | +# Function to mount the image and extract se.img |
| 11 | +function mount_and_extract_image() { |
| 12 | + local img_path=$1 |
| 13 | + |
| 14 | + # Cleanup any previous files and directories |
| 15 | + rm -rf se.img /mnt/myvm |
| 16 | + mkdir /mnt/myvm |
| 17 | + |
| 18 | + # Load nbd module and mount the image |
| 19 | + modprobe nbd |
| 20 | + if [ $? -ne 0 ]; then |
| 21 | + echo "Error: Failed to load nbd module." |
| 22 | + exit 1 |
| 23 | + fi |
| 24 | + |
| 25 | + qemu-nbd -c /dev/nbd3 $img_path |
| 26 | + if [ $? -ne 0 ]; then |
| 27 | + echo "Error: Failed to connect to nbd device." |
| 28 | + exit 1 |
| 29 | + fi |
| 30 | + |
| 31 | + |
| 32 | + mount /dev/nbd3p1 /mnt/myvm |
| 33 | + if [ $? -ne 0 ]; then |
| 34 | + echo "Error: Failed to mount the image. Retrying..." |
| 35 | + sleep 2 |
| 36 | + mount /dev/nbd3p1 /mnt/myvm |
| 37 | + if [ $? -ne 0 ]; then |
| 38 | + echo "Retrial for mounting failed. Please rerun the script" |
| 39 | + exit 1 |
| 40 | + else |
| 41 | + echo "Mounting on second attempt passed" |
| 42 | + fi |
| 43 | + fi |
| 44 | + # Extract and process image |
| 45 | + rm -rf $PWD/output-files |
| 46 | + mkdir -p $PWD/output-files |
| 47 | + rm -rf se.img |
| 48 | + cp /mnt/myvm/se.img ./ |
| 49 | + mv se.img $PWD/output-files/ |
| 50 | + |
| 51 | + umount /mnt/myvm |
| 52 | + qemu-nbd -d /dev/nbd3 |
| 53 | +} |
| 54 | + |
| 55 | +# Function to generate se-sample and ibmse-policy.rego files |
| 56 | +function generate_policy_files() { |
| 57 | + local se_tag=$1 |
| 58 | + local se_image_phkh=$2 |
| 59 | + |
| 60 | + # Create se-sample file |
| 61 | + cat <<EOF > $PWD/output-files/se-sample |
| 62 | +{ |
| 63 | + "se.attestation_phkh": [ |
| 64 | + "$se_image_phkh" |
| 65 | + ], |
| 66 | + "se.tag": [ |
| 67 | + "$se_tag" |
| 68 | + ], |
| 69 | + "se.image_phkh": [ |
| 70 | + "$se_image_phkh" |
| 71 | + ], |
| 72 | + "se.user_data": [ |
| 73 | + "00" |
| 74 | + ], |
| 75 | + "se.version": [ |
| 76 | + "256" |
| 77 | + ] |
| 78 | +} |
| 79 | +EOF |
| 80 | + |
| 81 | + # Create ibmse-policy.rego file |
| 82 | + cat <<EOF > $PWD/output-files/ibmse-policy.rego |
| 83 | +package policy |
| 84 | +import rego.v1 |
| 85 | +default allow = false |
| 86 | +converted_version := sprintf("%v", [input["se.version"]]) |
| 87 | +allow if { |
| 88 | + input["se.attestation_phkh"] == "$se_image_phkh" |
| 89 | + input["se.image_phkh"] == "$se_image_phkh" |
| 90 | + input["se.tag"] == "$se_tag" |
| 91 | + input["se.user_data"] == "00" |
| 92 | + converted_version == "256" |
| 93 | +} |
| 94 | +EOF |
| 95 | + |
| 96 | +} |
| 97 | + |
| 98 | +# Main function |
| 99 | +install_packages |
| 100 | + |
| 101 | +PS3='Please enter your choice: ' |
| 102 | +options=("Generate the RVPS From Local Image from User pc" "Generate RVPS from Volume" "Quit") |
| 103 | +select opt in "${options[@]}" |
| 104 | +do |
| 105 | + case $opt in |
| 106 | + "Generate the RVPS From Local Image from User pc") |
| 107 | + echo "Enter the Qcow2 image with Full path" |
| 108 | + read -r img_path |
| 109 | + |
| 110 | + mount_and_extract_image $img_path |
| 111 | + |
| 112 | + $PWD/static-files/pvextract-hdr -o $PWD/output-files/hdr.bin $PWD/output-files/se.img |
| 113 | + |
| 114 | + # Extract necessary values |
| 115 | + se_tag=$(python3 $PWD/static-files/se_parse_hdr.py $PWD/output-files/hdr.bin $PWD/static-files/HKD.crt | grep se.tag | awk -F ":" '{ print $2 }') |
| 116 | + se_image_phkh=$(python3 $PWD/static-files/se_parse_hdr.py $PWD/output-files/hdr.bin $PWD/static-files/HKD.crt | grep se.image_phkh | awk -F ":" '{ print $2 }') |
| 117 | + |
| 118 | + echo "se.tag: $se_tag" |
| 119 | + echo "se.image_phkh: $se_image_phkh" |
| 120 | + |
| 121 | + generate_policy_files $se_tag $se_image_phkh |
| 122 | + |
| 123 | + provenance=$(cat $PWD/output-files/se-sample | base64 --wrap=0) |
| 124 | + echo "provenance = $provenance" |
| 125 | + |
| 126 | + # Create se-message file |
| 127 | + cat <<EOF > $PWD/output-files/se-message |
| 128 | +{ |
| 129 | + "version" : "0.1.0", |
| 130 | + "type": "sample", |
| 131 | + "payload": "$provenance" |
| 132 | +} |
| 133 | +EOF |
| 134 | + |
| 135 | + ls -lrt $PWD/output-files/hdr.bin $PWD/output-files/se-message $PWD/output-files/ibmse-policy.rego |
| 136 | + ;; |
| 137 | + |
| 138 | + "Generate RVPS from Volume") |
| 139 | + echo "Enter the Libvirt Pool Name" |
| 140 | + read -r LIBVIRT_POOL |
| 141 | + echo "Enter the Libvirt URI Name" |
| 142 | + read -r LIBVIRT_URI |
| 143 | + echo "Enter the Libvirt Volume Name" |
| 144 | + read -r LIBVIRT_VOL |
| 145 | + |
| 146 | + # Download the volume |
| 147 | + echo "Downloading from PODVM Volume..." |
| 148 | + rm -rf $PWD/PODVM-VOL-IMAGE |
| 149 | + mkdir -p $PWD/PODVM-VOL-IMAGE |
| 150 | + virsh -c $LIBVIRT_URI vol-download --vol $LIBVIRT_VOL --pool $LIBVIRT_POOL --file $PWD/PODVM-VOL-IMAGE/podvm_test.qcow2 --sparse |
| 151 | + if [ $? -ne 0 ]; then |
| 152 | + echo "Downloading Failed" |
| 153 | + exit 1 |
| 154 | + fi |
| 155 | + |
| 156 | + img_path=$PWD/PODVM-VOL-IMAGE/podvm_test.qcow2 |
| 157 | + |
| 158 | + mount_and_extract_image $img_path |
| 159 | + |
| 160 | + $PWD/static-files/pvextract-hdr -o $PWD/output-files/hdr.bin $PWD/output-files/se.img |
| 161 | + |
| 162 | + # Extract necessary values |
| 163 | + se_tag=$(python3 $PWD/static-files/se_parse_hdr.py $PWD/output-files/hdr.bin $PWD/static-files/HKD.crt | grep se.tag | awk -F ":" '{ print $2 }') |
| 164 | + se_image_phkh=$(python3 $PWD/static-files/se_parse_hdr.py $PWD/output-files/hdr.bin $PWD/static-files/HKD.crt | grep se.image_phkh | awk -F ":" '{ print $2 }') |
| 165 | + |
| 166 | + echo "se.tag: $se_tag" |
| 167 | + echo "se.image_phkh: $se_image_phkh" |
| 168 | + |
| 169 | + generate_policy_files $se_tag $se_image_phkh |
| 170 | + |
| 171 | + provenance=$(cat $PWD/output-files/se-sample | base64 --wrap=0) |
| 172 | + echo "provenance = $provenance" |
| 173 | + |
| 174 | + # Create se-message file |
| 175 | + cat <<EOF > $PWD/output-files/se-message |
| 176 | +{ |
| 177 | + "version" : "0.1.0", |
| 178 | + "type": "sample", |
| 179 | + "payload": "$provenance" |
| 180 | +} |
| 181 | +EOF |
| 182 | + |
| 183 | + ls -lrt $PWD/output-files/hdr.bin $PWD/output-files/se-message $PWD/output-files/ibmse-policy.rego |
| 184 | + ;; |
| 185 | + |
| 186 | + "Quit") |
| 187 | + break |
| 188 | + ;; |
| 189 | + |
| 190 | + *) echo "Invalid option: $REPLY";; |
| 191 | + esac |
| 192 | +done |
| 193 | + |
0 commit comments