From 39a0e5c01fb047753dc49ce8f0e09dc9029e484f Mon Sep 17 00:00:00 2001 From: afonso Date: Fri, 19 Apr 2024 11:55:16 +0100 Subject: [PATCH] [PD1] Fixed stuff. Unmarshal still returns map[string]interface{}, need to fix --- Projs/PD1/internal/client/client.go | 7 +- Projs/PD1/internal/protocol/protocol.go | 166 +++++++++--------- Projs/PD1/internal/server/datastore.go | 19 ++ Projs/PD1/internal/server/server.go | 24 ++- .../internal/utils/cryptoUtils/cryptoUtils.go | 1 - .../internal/utils/networking/connection.go | 6 +- 6 files changed, 127 insertions(+), 96 deletions(-) diff --git a/Projs/PD1/internal/client/client.go b/Projs/PD1/internal/client/client.go index 00aa35c..8ffca2c 100644 --- a/Projs/PD1/internal/client/client.go +++ b/Projs/PD1/internal/client/client.go @@ -5,6 +5,7 @@ import ( "PD1/internal/utils/cryptoUtils" "PD1/internal/utils/networking" "flag" + "fmt" ) func Run() { @@ -34,7 +35,11 @@ func Run() { certRequestPacket := protocol.NewRequestUserCertPacket(uid) cl.Connection.Send(certRequestPacket) - //certPacket := cl.Connection.Receive() + + var certPacket protocol.Packet + cl.Connection.Receive(&certPacket) + uidCert := (certPacket.Body).(protocol.SendUserCertPacket) + fmt.Println(uidCert) // TODO: Encrypt message //submitMessage(cl, uid, cipherContent) diff --git a/Projs/PD1/internal/protocol/protocol.go b/Projs/PD1/internal/protocol/protocol.go index 1e08ad6..6134233 100644 --- a/Projs/PD1/internal/protocol/protocol.go +++ b/Projs/PD1/internal/protocol/protocol.go @@ -1,116 +1,114 @@ package protocol import ( - "time" + "time" ) type PacketType int const ( - ReqUserCertPkt PacketType = iota - ReqAllMsgPkt - ReqMsgPkt - SubmitMsgPkt - SendUserCertPkt - ServerMsgPkt + ReqUserCertPkt PacketType = iota + ReqAllMsgPkt + ReqMsgPkt + SubmitMsgPkt + SendUserCertPkt + ServerMsgPkt +) + +// Define interfaces for packet bodies +type ( + RequestUserCertPacket struct { + UID string `json:"uid"` + } + + RequestAllMsgPacket struct { + FromUID string `json:"from_uid"` + } + + RequestMsgPacket struct { + Num uint16 `json:"num"` + } + + SubmitMessagePacket struct { + ToUID string `json:"to_uid"` + Content []byte `json:"content"` + } + + SendUserCertPacket struct { + UID string `json:"uid"` + Key []byte `json:"key"` + } + + ServerMessagePacket struct { + FromUID string `json:"from_uid"` + ToUID string `json:"to_uid"` + Content []byte `json:"content"` + Timestamp time.Time `json:"timestamp"` + } ) type PacketBody interface{} type Packet struct { - Flag PacketType - Body PacketBody -} - -// Client --> Server: Ask for a user's certificate -type RequestUserCertPacket struct { - UID string + Flag PacketType `json:"flag"` + Body PacketBody `json:"body"` } func NewRequestUserCertPacket(UID string) Packet { - return Packet{ - Flag: ReqUserCertPkt, - Body: RequestUserCertPacket{ - UID: UID, - }, - } -} - -// Client --> Server: Ask for all the client's messages in the queue -type RequestAllMsgPacket struct { - FromUID string + return Packet{ + Flag: ReqUserCertPkt, + Body: RequestUserCertPacket{ + UID: UID, + }, + } } func NewRequestAllMsgPacket(fromUID string) Packet { - return Packet{ - Flag: ReqAllMsgPkt, - Body: RequestAllMsgPacket{ - FromUID: fromUID, - }, - } -} - -// Client --> Server: Ask for a specific message in the queue -type RequestMsgPacket struct { - Num uint16 + return Packet{ + Flag: ReqAllMsgPkt, + Body: RequestAllMsgPacket{ + FromUID: fromUID, + }, + } } func NewRequestMsgPacket(num uint16) Packet { - return Packet{ - Flag: ReqMsgPkt, - Body: RequestMsgPacket{ - Num: num, - }, - } -} - -// Client --> Server: Send message from client to server -type SubmitMessagePacket struct { - ToUID string - Content []byte + return Packet{ + Flag: ReqMsgPkt, + Body: RequestMsgPacket{ + Num: num, + }, + } } func NewSubmitMessagePacket(toUID string, content []byte) Packet { - return Packet{ - Flag: SubmitMsgPkt, - Body: SubmitMessagePacket{ - ToUID: toUID, - Content: content}, - } -} - -// Server --> Client: Send the client the requested public key -type SendUserCertPacket struct { - UID string - Key []byte + return Packet{ + Flag: SubmitMsgPkt, + Body: SubmitMessagePacket{ + ToUID: toUID, + Content: content, + }, + } } func NewSendUserCertPacket(uid string, key []byte) Packet { - return Packet{ - Flag: SendUserCertPkt, - Body: SendUserCertPacket{ - UID: uid, - Key: key, - }, - } -} - -// Server --> Client: Send the client a message -type ServerMessagePacket struct { - FromUID string - ToUID string - Content []byte - Timestamp time.Time + return Packet{ + Flag: SendUserCertPkt, + Body: SendUserCertPacket{ + UID: uid, + Key: key, + }, + } } func NewServerMessagePacket(fromUID, toUID string, content []byte, timestamp time.Time) Packet { - return Packet{ - Flag: ServerMsgPkt, - Body: ServerMessagePacket{ - FromUID: fromUID, - ToUID: toUID, - Content: content, - Timestamp: timestamp, - }, - } + return Packet{ + Flag: ServerMsgPkt, + Body: ServerMessagePacket{ + FromUID: fromUID, + ToUID: toUID, + Content: content, + Timestamp: timestamp, + }, + } } diff --git a/Projs/PD1/internal/server/datastore.go b/Projs/PD1/internal/server/datastore.go index 9513fdc..7120ea9 100644 --- a/Projs/PD1/internal/server/datastore.go +++ b/Projs/PD1/internal/server/datastore.go @@ -154,3 +154,22 @@ func (ds DataStore) GetUserCertificate(uid string) protocol.Packet { } return protocol.NewSendUserCertPacket(uid, userCert) } + +func userExists(db *sql.DB, uid string) bool { + // Prepare the SQL statement for checking if a user exists + query := ` + SELECT COUNT(*) + FROM users + WHERE UID = ? + ` + + var count int + // Execute the SQL query + err := db.QueryRow(query, uid).Scan(&count) + if err != nil { + log.Panicln("Error checking if user exists") + } + + // If count is greater than 0, the user exists + return count > 0 +} diff --git a/Projs/PD1/internal/server/server.go b/Projs/PD1/internal/server/server.go index c2b4c19..c963342 100644 --- a/Projs/PD1/internal/server/server.go +++ b/Projs/PD1/internal/server/server.go @@ -9,17 +9,29 @@ import ( func clientHandler(connection networking.Connection[protocol.Packet], dataStore DataStore) { defer connection.Conn.Close() - _ = dataStore + + //Get certificate sent by user clientCert := connection.GetPeerCertificate() - oidValueMap := cryptoUtils.ExtractAllOIDValues(clientCert) - fmt.Println(oidValueMap) + //Get the OID values + oidMap := cryptoUtils.ExtractAllOIDValues(clientCert) + //Get the UID of this user + UID := oidMap["2.5.4.65"] + if UID=="" { + panic("User certificate does not specify it's PSEUDONYM") + } for { - pac := connection.Receive() + var pac protocol.Packet + connection.Receive(&pac) switch pac.Flag { case protocol.ReqUserCertPkt: - //userCertPacket := dataStore.GetUserCertificate(uid) - //connection.Send(userCertPacket) + fmt.Printf("Type of pac.Body: %T\n", pac.Body) + UserCertPacket, ok := (pac.Body).(protocol.RequestUserCertPacket) + if !ok { + panic("Could not cast packet to it's type") + } + userCertPacket := dataStore.GetUserCertificate(UserCertPacket.UID) + connection.Send(userCertPacket) case protocol.ReqAllMsgPkt: fmt.Println("ReqAllMsg") case protocol.ReqMsgPkt: diff --git a/Projs/PD1/internal/utils/cryptoUtils/cryptoUtils.go b/Projs/PD1/internal/utils/cryptoUtils/cryptoUtils.go index 58a0fbe..3c5dd1c 100644 --- a/Projs/PD1/internal/utils/cryptoUtils/cryptoUtils.go +++ b/Projs/PD1/internal/utils/cryptoUtils/cryptoUtils.go @@ -94,7 +94,6 @@ func (k *KeyStore) GetServerTLSConfig() *tls.Config { caCertPool.AddCert(caCert) } tlsConfig.ClientCAs = caCertPool - //Request one valid or invalid certificate //FIX: SERVER ACCEPTS CONNECTIONS WITH UNMATCHING OR // NO CERTIFICATE, NEEDS TO BE CHANGED SOMEHOW tlsConfig.ClientAuth = tls.RequireAndVerifyClientCert diff --git a/Projs/PD1/internal/utils/networking/connection.go b/Projs/PD1/internal/utils/networking/connection.go index e1bff4d..28a5997 100644 --- a/Projs/PD1/internal/utils/networking/connection.go +++ b/Projs/PD1/internal/utils/networking/connection.go @@ -26,12 +26,10 @@ func (c Connection[T]) Send(obj T) { } } -func (c Connection[T]) Receive() T { - var obj T - if err := c.decoder.Decode(&obj); err != nil { +func (c Connection[T]) Receive(objPtr *T) { + if err := c.decoder.Decode(objPtr); err != nil { panic("Failed decoding data or reading it from connection") } - return obj } func (c Connection[T]) GetPeerCertificate() *x509.Certificate {