2020-12-03 10:12:43 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2020-12-08 01:15:38 +00:00
|
|
|
"bufio"
|
2020-12-03 10:12:43 +00:00
|
|
|
"crypto/rand"
|
|
|
|
"crypto/rsa"
|
|
|
|
"crypto/sha256"
|
2020-12-08 01:15:38 +00:00
|
|
|
"encoding/hex"
|
|
|
|
"fmt"
|
2020-12-03 10:12:43 +00:00
|
|
|
"github.com/rs/zerolog"
|
|
|
|
"github.com/rs/zerolog/log"
|
2020-12-08 01:15:38 +00:00
|
|
|
"net"
|
2020-12-03 10:12:43 +00:00
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Generate RSA keypair
|
|
|
|
func GenerateRSAKeypair() (*rsa.PrivateKey, *rsa.PublicKey) {
|
|
|
|
// Use ConsoleWriter logger
|
2020-12-03 17:32:03 +00:00
|
|
|
log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr}).Hook(FatalHook{})
|
2020-12-03 10:12:43 +00:00
|
|
|
// Generate private/public RSA keypair
|
|
|
|
privateKey, err := rsa.GenerateKey(rand.Reader, 2048)
|
2020-12-21 07:18:42 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal().Err(err).Msg("Error generating RSA keypair")
|
|
|
|
}
|
2020-12-03 10:12:43 +00:00
|
|
|
// Get public key
|
|
|
|
publicKey := privateKey.PublicKey
|
|
|
|
// Return keypair
|
|
|
|
return privateKey, &publicKey
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get public key from sender
|
2020-12-08 01:15:38 +00:00
|
|
|
func GetKey(connection net.Conn) []byte {
|
2020-12-03 10:12:43 +00:00
|
|
|
// Use ConsoleWriter logger
|
2020-12-03 17:32:03 +00:00
|
|
|
log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr}).Hook(FatalHook{})
|
2020-12-08 01:15:38 +00:00
|
|
|
// Send key request to connection
|
|
|
|
_, err := fmt.Fprintln(connection, "key;")
|
2020-12-21 07:18:42 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal().Err(err).Msg("Error sending key request")
|
|
|
|
}
|
2020-12-08 01:15:38 +00:00
|
|
|
// Read received message
|
|
|
|
message, err := bufio.NewReader(connection).ReadString('\n')
|
2020-12-21 07:18:42 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal().Err(err).Msg("Error getting key")
|
|
|
|
}
|
2020-12-08 01:15:38 +00:00
|
|
|
// Process received message
|
|
|
|
procMessage := strings.Split(strings.TrimSpace(message), ";")
|
|
|
|
// If ok code returned
|
|
|
|
if procMessage[0] == "OK" {
|
|
|
|
// Decode received hex string into key
|
|
|
|
key, err := hex.DecodeString(procMessage[1])
|
2020-12-21 07:18:42 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal().Err(err).Msg("Error reading key")
|
|
|
|
}
|
2020-12-03 10:12:43 +00:00
|
|
|
// Return key
|
|
|
|
return key
|
2020-12-21 07:18:42 +00:00
|
|
|
// Otherwise
|
2020-12-03 10:12:43 +00:00
|
|
|
} else {
|
2020-12-08 01:15:38 +00:00
|
|
|
// Fatally log
|
2020-12-21 07:18:42 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal().Msg("Server reported error")
|
|
|
|
}
|
2020-12-03 10:12:43 +00:00
|
|
|
}
|
|
|
|
// Return nil if all else fails
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Encrypt shared key with received public key
|
|
|
|
func EncryptKey(sharedKey string, recvPubKey *rsa.PublicKey) []byte {
|
|
|
|
// Use ConsoleWriter logger
|
2020-12-03 17:32:03 +00:00
|
|
|
log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr}).Hook(FatalHook{})
|
2020-12-03 10:12:43 +00:00
|
|
|
// Encrypt shared key using RSA
|
|
|
|
encryptedSharedKey, err := rsa.EncryptOAEP(sha256.New(), rand.Reader, recvPubKey, []byte(sharedKey), nil)
|
2020-12-21 07:18:42 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal().Err(err).Msg("Error encrypting shared key")
|
|
|
|
}
|
2020-12-03 10:12:43 +00:00
|
|
|
// Return encrypted key
|
|
|
|
return encryptedSharedKey
|
|
|
|
}
|
|
|
|
|
|
|
|
// Decrypt shared key using private RSA key
|
|
|
|
func DecryptKey(encryptedKey []byte, privateKey *rsa.PrivateKey) string {
|
|
|
|
// Decrypt shared key using RSA
|
|
|
|
decryptedKey, err := rsa.DecryptOAEP(sha256.New(), rand.Reader, privateKey, encryptedKey, nil)
|
2020-12-21 07:18:42 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal().Err(err).Msg("Error decrypting shared key")
|
|
|
|
}
|
2020-12-03 10:12:43 +00:00
|
|
|
// Get string of decrypted key
|
|
|
|
sharedKey := string(decryptedKey)
|
|
|
|
// Return shared key
|
|
|
|
return sharedKey
|
2020-12-21 07:18:42 +00:00
|
|
|
}
|