[PD1] cryptoUtils start and protocol tweaks

This commit is contained in:
Tiago Sousa 2024-04-17 19:54:14 +01:00
parent cdaae8fb7e
commit c57c093867
Signed by: tiago
SSH key fingerprint: SHA256:odOD9vln9U7qNe1R8o3UCbE3jkQCkr5/q5mgd5hwua0
4 changed files with 128 additions and 80 deletions

View file

@ -28,13 +28,16 @@ func Run() {
messageContent := readMessageContent()
cl := networking.NewClient[protocol.Packet]()
defer cl.Connection.Conn.Close()
// TODO: cipherSubject := CHAMAR CRYPTO
// TODO: cipherBody := CHAMAR CRYPTO
submitMessage(cl,uid,cipherSubject,cipherBody)
// TODO: getuserinfo client cert
// TODO: ask server for the recieving client's cert
certRequestPacket := protocol.NewRequestPubKey()
cl.Connection.Send(certRequestPacket)
certPacket := cl.Connection.Receive()
// TODO: cipherContent := cryptoUtils.encryptMessageContent()
submitMessage(cl,uid,cipherContent)
case "askqueue":
cl = networking.NewClient[protocol.Packet]()
cl := networking.NewClient[protocol.Packet]()
defer cl.Connection.Conn.Close()
case "getmsg":
@ -42,7 +45,7 @@ func Run() {
panic("Insufficient arguments for 'getmsg' command. Usage: getmsg <NUM>")
}
num := flag.Arg(1)
cl = networking.NewClient[protocol.Packet]()
cl := networking.NewClient[protocol.Packet]()
defer cl.Connection.Conn.Close()
case "help":
@ -54,7 +57,7 @@ func Run() {
}
func submitMessage(cl networking.Client[protocol.Packet],uid string,subject []byte,body []byte) {
pack := protocol.NewSubmitMessage(uid, subject, body)
func submitMessage(cl networking.Client[protocol.Packet],uid string, content []byte) {
pack := protocol.NewSubmitMessage(uid,content)
cl.Connection.Send(pack)
}

View file

@ -10,6 +10,15 @@ func readMessageContent() string {
fmt.Println("Enter message content (limited to 1000 bytes):")
scanner := bufio.NewScanner(os.Stdin)
scanner.Scan()
// FIX: make sure this doesnt die
return scanner.Text()
}
func AskUserPassword() string {
fmt.Println("Enter message content (limited to 1000 bytes):")
scanner := bufio.NewScanner(os.Stdin)
scanner.Scan()
// FIX: make sure this doesnt die
return scanner.Text()
}

View file

@ -1,122 +1,116 @@
package protocol
import (
"time"
"time"
)
type PacketType int
const (
ReqPK PacketType = iota
ReqAllMsg
ReqMsg
SubmitMsg
SendPK
Msg
ReqPK PacketType = iota
ReqAllMsg
ReqMsg
SubmitMsg
SendPK
Msg
)
type PacketBody interface{}
type Packet struct {
Flag PacketType
Body PacketBody
Flag PacketType
Body PacketBody
}
// Client --> Server: Ask for a user's public key
type RequestPubKey struct {
FromUID string
KeyUID string
FromUID string
KeyUID string
}
func NewRequestPubKey(fromUID, keyUID string) Packet {
return Packet{
Flag: ReqPK,
Body: RequestPubKey{
FromUID: fromUID,
KeyUID: keyUID,
},
}
return Packet{
Flag: ReqPK,
Body: RequestPubKey{
FromUID: fromUID,
KeyUID: keyUID,
},
}
}
// Client --> Server: Ask for all the client's messages in the queue
type RequestAllMsg struct {
FromUID string
FromUID string
}
func NewRequestAllMsg(fromUID string) Packet {
return Packet{
Flag: ReqAllMsg,
Body: RequestAllMsg{
FromUID: fromUID,
},
}
return Packet{
Flag: ReqAllMsg,
Body: RequestAllMsg{
FromUID: fromUID,
},
}
}
// Client --> Server: Ask for a specific message in the queue
type RequestMsg struct {
Num uint16
Num uint16
}
func NewRequestMsg(num uint16) Packet {
return Packet{
Flag: ReqMsg,
Body: RequestMsg{
Num: num,
},
}
return Packet{
Flag: ReqMsg,
Body: RequestMsg{
Num: num,
},
}
}
// Client --> Server: Send message from client to server
type SubmitMessage struct {
ToUID string
Subject []byte
Body []byte
ToUID string
Content []byte
}
func NewSubmitMessage(toUID string, subject, body []byte) Packet {
return Packet{
Flag: SubmitMsg,
Body: SubmitMessage{
ToUID: toUID,
Subject: subject,
Body: body,
},
}
func NewSubmitMessage(toUID string, content []byte) Packet {
return Packet{
Flag: SubmitMsg,
Body: SubmitMessage{
ToUID: toUID,
Content: content},
}
}
// Server --> Client: Send the client the requested public key
type SendPubKey struct {
Key []byte
Key []byte
}
func NewSendPubKey(key []byte) Packet {
return Packet{
Flag: SendPK,
Body: SendPubKey{
Key: key,
},
}
return Packet{
Flag: SendPK,
Body: SendPubKey{
Key: key,
},
}
}
// Server --> Client: Send the client a message
type Message struct {
FromUID string
ToUID string
Subject []byte
Body []byte
Timestamp time.Time
type ServerMessage struct {
FromUID string
ToUID string
Content []byte
Timestamp time.Time
}
func NewMessage(fromUID, toUID string, subject, body []byte, timestamp time.Time) Packet {
return Packet{
Flag: Msg,
Body: Message{
FromUID: fromUID,
ToUID: toUID,
Subject: subject,
Body: body,
Timestamp: timestamp,
},
}
func NewMessage(fromUID, toUID string, content []byte, timestamp time.Time) Packet {
return Packet{
Flag: Msg,
Body: ServerMessage{
FromUID: fromUID,
ToUID: toUID,
Content: content,
Timestamp: timestamp,
},
}
}

View file

@ -1,7 +1,49 @@
package cryptoUtils
import "fmt"
import (
"PD1/internal/client"
"PD1/internal/protocol"
"crypto/rsa"
"crypto/x509"
"fmt"
"log"
"os"
"software.sslmate.com/src/go-pkcs12"
)
func Print() {
fmt.Println("crypto package")
}
func getUserInfo(certFilename string) (
rsa.PrivateKey,
*x509.Certificate,
[]*x509.Certificate,
error) {
var privKey rsa.PrivateKey
certFile, err := os.ReadFile(certFilename)
if err != nil {
log.Panicln("Provided certificate %v couldn't be opened", certFilename)
return rsa.PrivateKey{}, nil, nil, err
}
password := client.AskUserPassword()
privKeyInterface, cert, caCerts, err := pkcs12.DecodeChain(certFile, password)
privKey = privKeyInterface.(rsa.PrivateKey)
if err != nil {
log.Panicln("PKCS12 key store couldn't be decoded")
return rsa.PrivateKey{}, nil, nil, err
}
if err := privKey.Validate(); err != nil {
log.Panicln("Private key is not valid")
return rsa.PrivateKey{}, nil, nil, err
}
return privKey, cert, caCerts, nil
}
func encryptMessageContent(privKey rsa.PrivateKey, peerPubKey rsa.PublicKey, content []byte) []byte {
// Digital envolope
func Print(){
fmt.Println("crypto package")
}