[PD1] crypto changes and TLS almost done

This commit is contained in:
Afonso Franco 2024-04-18 17:15:47 +01:00
parent 2c4f1fd2fc
commit 5ae7358a0d
Signed by: afonso
SSH key fingerprint: SHA256:aiLbdlPwXKJS5wMnghdtod0SPy8imZjlVvCyUX9DJNk
13 changed files with 138 additions and 87 deletions

View file

@ -2,7 +2,7 @@ package networking
import (
"crypto/tls"
"net"
"log"
)
@ -17,7 +17,7 @@ type Client[T any] struct {
func NewClient[T any](clientTLSConfigProvider ClientTLSConfigProvider) Client[T] {
dialConn, err := tls.Dial("tcp", "localhost:8080", clientTLSConfigProvider.GetTLSConfigClient())
if err != nil {
panic("Could not open connection to server")
log.Panicln("Could not open connection to server",err)
}
conn := NewConnection[T](dialConn)
return Client[T]{Connection: conn}

View file

@ -1,18 +1,18 @@
package networking
import (
"crypto/tls"
"crypto/x509"
"encoding/json"
"net"
)
type Connection[T any] struct {
Conn net.Conn
Conn *tls.Conn
encoder *json.Encoder
decoder *json.Decoder
}
func NewConnection[T any](netConn net.Conn) Connection[T] {
func NewConnection[T any](netConn *tls.Conn) Connection[T] {
return Connection[T]{
Conn: netConn,
encoder: json.NewEncoder(netConn),
@ -20,16 +20,21 @@ func NewConnection[T any](netConn net.Conn) Connection[T] {
}
}
func (jc Connection[T]) Send(obj T) {
if err := jc.encoder.Encode(&obj); err != nil {
func (c Connection[T]) Send(obj T) {
if err := c.encoder.Encode(&obj); err != nil {
panic("Failed encoding data or sending it to connection")
}
}
func (jc Connection[T]) Receive() T {
func (c Connection[T]) Receive() T {
var obj T
if err := jc.decoder.Decode(&obj); err != nil {
if err := c.decoder.Decode(&obj); err != nil {
panic("Failed decoding data or reading it from connection")
}
return obj
}
func (c Connection[T]) GetPeerCertificate() *x509.Certificate {
state := c.Conn.ConnectionState()
return state.PeerCertificates[0]
}

View file

@ -3,11 +3,12 @@ package networking
import (
"crypto/tls"
"fmt"
"log"
"net"
)
type ServerTLSConfigProvider interface {
GetServerTLSConfig() *tls.Config
GetTLSConfigServer() *tls.Config
}
type Server[T any] struct {
@ -15,16 +16,16 @@ type Server[T any] struct {
C chan Connection[T]
}
func NewServer[T any](serverTLSConfigProvider ServerTLSConfigProvider,port int) Server[T]{
func NewServer[T any](serverTLSConfigProvider ServerTLSConfigProvider, port int) Server[T] {
listener, err := tls.Listen("tcp", fmt.Sprintf("0.0.0.0:%d", port), serverTLSConfigProvider.GetServerTLSConfig())
listener, err := tls.Listen("tcp", fmt.Sprintf("0.0.0.0:%d", port), serverTLSConfigProvider.GetTLSConfigServer())
if err != nil {
panic("Server could not bind to address")
}
return Server[T]{
listener:listener,
C: make(chan Connection[T]),
}
return Server[T]{
listener: listener,
C: make(chan Connection[T]),
}
}
func (s *Server[T]) ListenLoop() {
@ -34,7 +35,16 @@ func (s *Server[T]) ListenLoop() {
if err != nil {
panic("Server could not accept connection")
}
conn := NewConnection[T](listenerConn)
tlsConn, ok := listenerConn.(*tls.Conn)
if !ok {
panic("Connection is not a TLS connection")
}
state := tlsConn.ConnectionState()
if len(state.PeerCertificates) == 0 {
log.Panicln("Client did not provide a certificate")
}
conn := NewConnection[T](tlsConn)
s.C <- conn
}
}