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

# receive public key if I don't have it
if [ ! -e server_rsa_key.pub ]
then
  echo "getting public key from server"
  curl 192.168.15.75:5000/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

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

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

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

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

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