[PD1] Fixed stuff. Unmarshal still returns map[string]interface{}, need to fix

This commit is contained in:
Afonso Franco 2024-04-19 11:55:16 +01:00
parent c131aa2aea
commit 39a0e5c01f
Signed by: afonso
SSH key fingerprint: SHA256:aiLbdlPwXKJS5wMnghdtod0SPy8imZjlVvCyUX9DJNk
6 changed files with 127 additions and 96 deletions

View file

@ -5,6 +5,7 @@ import (
"PD1/internal/utils/cryptoUtils" "PD1/internal/utils/cryptoUtils"
"PD1/internal/utils/networking" "PD1/internal/utils/networking"
"flag" "flag"
"fmt"
) )
func Run() { func Run() {
@ -34,7 +35,11 @@ func Run() {
certRequestPacket := protocol.NewRequestUserCertPacket(uid) certRequestPacket := protocol.NewRequestUserCertPacket(uid)
cl.Connection.Send(certRequestPacket) 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 // TODO: Encrypt message
//submitMessage(cl, uid, cipherContent) //submitMessage(cl, uid, cipherContent)

View file

@ -15,16 +15,43 @@ const (
ServerMsgPkt 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 PacketBody interface{}
type Packet struct { type Packet struct {
Flag PacketType Flag PacketType `json:"flag"`
Body PacketBody Body PacketBody `json:"body"`
}
// Client --> Server: Ask for a user's certificate
type RequestUserCertPacket struct {
UID string
} }
func NewRequestUserCertPacket(UID string) Packet { func NewRequestUserCertPacket(UID string) Packet {
@ -36,11 +63,6 @@ func NewRequestUserCertPacket(UID string) Packet {
} }
} }
// Client --> Server: Ask for all the client's messages in the queue
type RequestAllMsgPacket struct {
FromUID string
}
func NewRequestAllMsgPacket(fromUID string) Packet { func NewRequestAllMsgPacket(fromUID string) Packet {
return Packet{ return Packet{
Flag: ReqAllMsgPkt, Flag: ReqAllMsgPkt,
@ -50,11 +72,6 @@ func NewRequestAllMsgPacket(fromUID string) Packet {
} }
} }
// Client --> Server: Ask for a specific message in the queue
type RequestMsgPacket struct {
Num uint16
}
func NewRequestMsgPacket(num uint16) Packet { func NewRequestMsgPacket(num uint16) Packet {
return Packet{ return Packet{
Flag: ReqMsgPkt, Flag: ReqMsgPkt,
@ -64,27 +81,16 @@ func NewRequestMsgPacket(num uint16) Packet {
} }
} }
// Client --> Server: Send message from client to server
type SubmitMessagePacket struct {
ToUID string
Content []byte
}
func NewSubmitMessagePacket(toUID string, content []byte) Packet { func NewSubmitMessagePacket(toUID string, content []byte) Packet {
return Packet{ return Packet{
Flag: SubmitMsgPkt, Flag: SubmitMsgPkt,
Body: SubmitMessagePacket{ Body: SubmitMessagePacket{
ToUID: toUID, ToUID: toUID,
Content: content}, Content: content,
},
} }
} }
// Server --> Client: Send the client the requested public key
type SendUserCertPacket struct {
UID string
Key []byte
}
func NewSendUserCertPacket(uid string, key []byte) Packet { func NewSendUserCertPacket(uid string, key []byte) Packet {
return Packet{ return Packet{
Flag: SendUserCertPkt, Flag: SendUserCertPkt,
@ -95,14 +101,6 @@ func NewSendUserCertPacket(uid string, key []byte) Packet {
} }
} }
// Server --> Client: Send the client a message
type ServerMessagePacket struct {
FromUID string
ToUID string
Content []byte
Timestamp time.Time
}
func NewServerMessagePacket(fromUID, toUID string, content []byte, timestamp time.Time) Packet { func NewServerMessagePacket(fromUID, toUID string, content []byte, timestamp time.Time) Packet {
return Packet{ return Packet{
Flag: ServerMsgPkt, Flag: ServerMsgPkt,

View file

@ -154,3 +154,22 @@ func (ds DataStore) GetUserCertificate(uid string) protocol.Packet {
} }
return protocol.NewSendUserCertPacket(uid, userCert) 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
}

View file

@ -9,17 +9,29 @@ import (
func clientHandler(connection networking.Connection[protocol.Packet], dataStore DataStore) { func clientHandler(connection networking.Connection[protocol.Packet], dataStore DataStore) {
defer connection.Conn.Close() defer connection.Conn.Close()
_ = dataStore
//Get certificate sent by user
clientCert := connection.GetPeerCertificate() clientCert := connection.GetPeerCertificate()
oidValueMap := cryptoUtils.ExtractAllOIDValues(clientCert) //Get the OID values
fmt.Println(oidValueMap) 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 { for {
pac := connection.Receive() var pac protocol.Packet
connection.Receive(&pac)
switch pac.Flag { switch pac.Flag {
case protocol.ReqUserCertPkt: case protocol.ReqUserCertPkt:
//userCertPacket := dataStore.GetUserCertificate(uid) fmt.Printf("Type of pac.Body: %T\n", pac.Body)
//connection.Send(userCertPacket) 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: case protocol.ReqAllMsgPkt:
fmt.Println("ReqAllMsg") fmt.Println("ReqAllMsg")
case protocol.ReqMsgPkt: case protocol.ReqMsgPkt:

View file

@ -94,7 +94,6 @@ func (k *KeyStore) GetServerTLSConfig() *tls.Config {
caCertPool.AddCert(caCert) caCertPool.AddCert(caCert)
} }
tlsConfig.ClientCAs = caCertPool tlsConfig.ClientCAs = caCertPool
//Request one valid or invalid certificate
//FIX: SERVER ACCEPTS CONNECTIONS WITH UNMATCHING OR //FIX: SERVER ACCEPTS CONNECTIONS WITH UNMATCHING OR
// NO CERTIFICATE, NEEDS TO BE CHANGED SOMEHOW // NO CERTIFICATE, NEEDS TO BE CHANGED SOMEHOW
tlsConfig.ClientAuth = tls.RequireAndVerifyClientCert tlsConfig.ClientAuth = tls.RequireAndVerifyClientCert

View file

@ -26,12 +26,10 @@ func (c Connection[T]) Send(obj T) {
} }
} }
func (c Connection[T]) Receive() T { func (c Connection[T]) Receive(objPtr *T) {
var obj T if err := c.decoder.Decode(objPtr); err != nil {
if err := c.decoder.Decode(&obj); err != nil {
panic("Failed decoding data or reading it from connection") panic("Failed decoding data or reading it from connection")
} }
return obj
} }
func (c Connection[T]) GetPeerCertificate() *x509.Certificate { func (c Connection[T]) GetPeerCertificate() *x509.Certificate {