send.sh 1.13 KB
SERV=$1
MSG=$2
PHONE=$3
TYPE=$4

# receive public key if one doesn't exist
if [ ! -e server_rsa_key.pub ]
then
  echo "getting new public key from server"
  PUB_KEY=$(curl 192.168.15.75:5000/init)
  printf "%s" "$PUB_KEY" > server_rsa_key.pub
fi

# generate random key
echo "generating random encryption key"
openssl rand -base64 32 > rand.key

# encode data with random key
echo "encoding $MSG with random key"
echo "$MSG" | openssl enc -aes-256-cbc -salt -out data.enc -pass file:./rand.key
#printf "%s" "$MSG" > data

# encrypt random key with server's pubkey
openssl rsautl -encrypt -inkey server_rsa_key.pub -pubin -in rand.key -out rand.key.enc
rm rand.key

# send data as file (with optional encripted symectric key)
curl -F "data=@data.enc" -F "key=@rand.key.enc" 192.168.15.75:5000/data
#curl -F "data=@data" 192.168.15.75:5000/data

# send the rest of the parameter to send the data
echo "calling message sending method of server"
DATA=$(curl -d "serv=$SERV" -d "dest=$PHONE" -d "type=$TYPE" 192.168.15.75:5000/msg)

echo $DATA >> msg_ids.txt

# cleanup
rm ./rand.key.enc
rm ./data.enc

# DEBUGGING
rm /dev/shm/info_colmena
rm ./server_rsa_key.pub