[PD1] Fixed stuff. Unmarshal still returns map[string]interface{}, need to fix
This commit is contained in:
parent
c131aa2aea
commit
39a0e5c01f
6 changed files with 127 additions and 96 deletions
|
@ -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)
|
||||
|
|
|
@ -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,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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:
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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 {
|
||||
|
|
Loading…
Reference in a new issue