[PD1] cryptoUtils start and protocol tweaks
This commit is contained in:
parent
cdaae8fb7e
commit
c57c093867
4 changed files with 128 additions and 80 deletions
|
@ -28,13 +28,16 @@ func Run() {
|
||||||
messageContent := readMessageContent()
|
messageContent := readMessageContent()
|
||||||
cl := networking.NewClient[protocol.Packet]()
|
cl := networking.NewClient[protocol.Packet]()
|
||||||
defer cl.Connection.Conn.Close()
|
defer cl.Connection.Conn.Close()
|
||||||
|
// TODO: getuserinfo client cert
|
||||||
// TODO: cipherSubject := CHAMAR CRYPTO
|
// TODO: ask server for the recieving client's cert
|
||||||
// TODO: cipherBody := CHAMAR CRYPTO
|
certRequestPacket := protocol.NewRequestPubKey()
|
||||||
submitMessage(cl,uid,cipherSubject,cipherBody)
|
cl.Connection.Send(certRequestPacket)
|
||||||
|
certPacket := cl.Connection.Receive()
|
||||||
|
// TODO: cipherContent := cryptoUtils.encryptMessageContent()
|
||||||
|
submitMessage(cl,uid,cipherContent)
|
||||||
|
|
||||||
case "askqueue":
|
case "askqueue":
|
||||||
cl = networking.NewClient[protocol.Packet]()
|
cl := networking.NewClient[protocol.Packet]()
|
||||||
defer cl.Connection.Conn.Close()
|
defer cl.Connection.Conn.Close()
|
||||||
|
|
||||||
case "getmsg":
|
case "getmsg":
|
||||||
|
@ -42,7 +45,7 @@ func Run() {
|
||||||
panic("Insufficient arguments for 'getmsg' command. Usage: getmsg <NUM>")
|
panic("Insufficient arguments for 'getmsg' command. Usage: getmsg <NUM>")
|
||||||
}
|
}
|
||||||
num := flag.Arg(1)
|
num := flag.Arg(1)
|
||||||
cl = networking.NewClient[protocol.Packet]()
|
cl := networking.NewClient[protocol.Packet]()
|
||||||
defer cl.Connection.Conn.Close()
|
defer cl.Connection.Conn.Close()
|
||||||
|
|
||||||
case "help":
|
case "help":
|
||||||
|
@ -54,7 +57,7 @@ func Run() {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func submitMessage(cl networking.Client[protocol.Packet],uid string,subject []byte,body []byte) {
|
func submitMessage(cl networking.Client[protocol.Packet],uid string, content []byte) {
|
||||||
pack := protocol.NewSubmitMessage(uid, subject, body)
|
pack := protocol.NewSubmitMessage(uid,content)
|
||||||
cl.Connection.Send(pack)
|
cl.Connection.Send(pack)
|
||||||
}
|
}
|
||||||
|
|
|
@ -10,6 +10,15 @@ func readMessageContent() string {
|
||||||
fmt.Println("Enter message content (limited to 1000 bytes):")
|
fmt.Println("Enter message content (limited to 1000 bytes):")
|
||||||
scanner := bufio.NewScanner(os.Stdin)
|
scanner := bufio.NewScanner(os.Stdin)
|
||||||
scanner.Scan()
|
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()
|
return scanner.Text()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,122 +1,116 @@
|
||||||
package protocol
|
package protocol
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
type PacketType int
|
type PacketType int
|
||||||
|
|
||||||
const (
|
const (
|
||||||
ReqPK PacketType = iota
|
ReqPK PacketType = iota
|
||||||
ReqAllMsg
|
ReqAllMsg
|
||||||
ReqMsg
|
ReqMsg
|
||||||
SubmitMsg
|
SubmitMsg
|
||||||
SendPK
|
SendPK
|
||||||
Msg
|
Msg
|
||||||
)
|
)
|
||||||
|
|
||||||
type PacketBody interface{}
|
type PacketBody interface{}
|
||||||
|
|
||||||
type Packet struct {
|
type Packet struct {
|
||||||
Flag PacketType
|
Flag PacketType
|
||||||
Body PacketBody
|
Body PacketBody
|
||||||
}
|
}
|
||||||
|
|
||||||
// Client --> Server: Ask for a user's public key
|
// Client --> Server: Ask for a user's public key
|
||||||
type RequestPubKey struct {
|
type RequestPubKey struct {
|
||||||
FromUID string
|
FromUID string
|
||||||
KeyUID string
|
KeyUID string
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewRequestPubKey(fromUID, keyUID string) Packet {
|
func NewRequestPubKey(fromUID, keyUID string) Packet {
|
||||||
return Packet{
|
return Packet{
|
||||||
Flag: ReqPK,
|
Flag: ReqPK,
|
||||||
Body: RequestPubKey{
|
Body: RequestPubKey{
|
||||||
FromUID: fromUID,
|
FromUID: fromUID,
|
||||||
KeyUID: keyUID,
|
KeyUID: keyUID,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Client --> Server: Ask for all the client's messages in the queue
|
// Client --> Server: Ask for all the client's messages in the queue
|
||||||
type RequestAllMsg struct {
|
type RequestAllMsg struct {
|
||||||
FromUID string
|
FromUID string
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewRequestAllMsg(fromUID string) Packet {
|
func NewRequestAllMsg(fromUID string) Packet {
|
||||||
return Packet{
|
return Packet{
|
||||||
Flag: ReqAllMsg,
|
Flag: ReqAllMsg,
|
||||||
Body: RequestAllMsg{
|
Body: RequestAllMsg{
|
||||||
FromUID: fromUID,
|
FromUID: fromUID,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Client --> Server: Ask for a specific message in the queue
|
// Client --> Server: Ask for a specific message in the queue
|
||||||
type RequestMsg struct {
|
type RequestMsg struct {
|
||||||
Num uint16
|
Num uint16
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewRequestMsg(num uint16) Packet {
|
func NewRequestMsg(num uint16) Packet {
|
||||||
return Packet{
|
return Packet{
|
||||||
Flag: ReqMsg,
|
Flag: ReqMsg,
|
||||||
Body: RequestMsg{
|
Body: RequestMsg{
|
||||||
Num: num,
|
Num: num,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Client --> Server: Send message from client to server
|
// Client --> Server: Send message from client to server
|
||||||
type SubmitMessage struct {
|
type SubmitMessage struct {
|
||||||
ToUID string
|
ToUID string
|
||||||
Subject []byte
|
Content []byte
|
||||||
Body []byte
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewSubmitMessage(toUID string, subject, body []byte) Packet {
|
func NewSubmitMessage(toUID string, content []byte) Packet {
|
||||||
return Packet{
|
return Packet{
|
||||||
Flag: SubmitMsg,
|
Flag: SubmitMsg,
|
||||||
Body: SubmitMessage{
|
Body: SubmitMessage{
|
||||||
ToUID: toUID,
|
ToUID: toUID,
|
||||||
Subject: subject,
|
Content: content},
|
||||||
Body: body,
|
}
|
||||||
},
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Server --> Client: Send the client the requested public key
|
// Server --> Client: Send the client the requested public key
|
||||||
type SendPubKey struct {
|
type SendPubKey struct {
|
||||||
Key []byte
|
Key []byte
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewSendPubKey(key []byte) Packet {
|
func NewSendPubKey(key []byte) Packet {
|
||||||
return Packet{
|
return Packet{
|
||||||
Flag: SendPK,
|
Flag: SendPK,
|
||||||
Body: SendPubKey{
|
Body: SendPubKey{
|
||||||
Key: key,
|
Key: key,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Server --> Client: Send the client a message
|
// Server --> Client: Send the client a message
|
||||||
type Message struct {
|
type ServerMessage struct {
|
||||||
FromUID string
|
FromUID string
|
||||||
ToUID string
|
ToUID string
|
||||||
Subject []byte
|
Content []byte
|
||||||
Body []byte
|
Timestamp time.Time
|
||||||
Timestamp time.Time
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewMessage(fromUID, toUID string, subject, body []byte, timestamp time.Time) Packet {
|
func NewMessage(fromUID, toUID string, content []byte, timestamp time.Time) Packet {
|
||||||
return Packet{
|
return Packet{
|
||||||
Flag: Msg,
|
Flag: Msg,
|
||||||
Body: Message{
|
Body: ServerMessage{
|
||||||
FromUID: fromUID,
|
FromUID: fromUID,
|
||||||
ToUID: toUID,
|
ToUID: toUID,
|
||||||
Subject: subject,
|
Content: content,
|
||||||
Body: body,
|
Timestamp: timestamp,
|
||||||
Timestamp: timestamp,
|
},
|
||||||
},
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,7 +1,49 @@
|
||||||
package cryptoUtils
|
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")
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue