[PD1] FIXED TLS Handshake
This commit is contained in:
parent
1cb81d2279
commit
4cf7880e57
5 changed files with 77 additions and 22 deletions
|
@ -9,12 +9,11 @@ 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
|
||||||
clientCert := connection.GetPeerCertificate()
|
clientCert := connection.GetPeerCertificate()
|
||||||
oidValueMap := cryptoUtils.ExtractAllOIDValues(clientCert)
|
oidValueMap := cryptoUtils.ExtractAllOIDValues(clientCert)
|
||||||
fmt.Println(oidValueMap)
|
fmt.Println(oidValueMap)
|
||||||
|
|
||||||
|
|
||||||
for {
|
for {
|
||||||
pac := connection.Receive()
|
pac := connection.Receive()
|
||||||
switch pac.Flag {
|
switch pac.Flag {
|
||||||
|
@ -41,10 +40,10 @@ func Run(port int) {
|
||||||
|
|
||||||
//Read server keystore
|
//Read server keystore
|
||||||
password := AskServerPassword()
|
password := AskServerPassword()
|
||||||
serverKeyStore := cryptoUtils.LoadKeyStore("certs/server/server.p12",password)
|
serverKeyStore := cryptoUtils.LoadKeyStore("certs/server/server.p12", password)
|
||||||
|
|
||||||
//Create server listener
|
//Create server listener
|
||||||
server := networking.NewServer[protocol.Packet](&serverKeyStore,port)
|
server := networking.NewServer[protocol.Packet](&serverKeyStore, port)
|
||||||
go server.ListenLoop()
|
go server.ListenLoop()
|
||||||
|
|
||||||
for {
|
for {
|
||||||
|
|
|
@ -8,7 +8,9 @@ import (
|
||||||
"crypto/tls"
|
"crypto/tls"
|
||||||
"crypto/x509"
|
"crypto/x509"
|
||||||
"encoding/binary"
|
"encoding/binary"
|
||||||
"fmt"
|
"errors"
|
||||||
|
|
||||||
|
//"errors"
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
|
@ -65,7 +67,6 @@ func LoadKeyStore(keyStorePath string, password string) KeyStore {
|
||||||
if err := privKey.Validate(); err != nil {
|
if err := privKey.Validate(); err != nil {
|
||||||
log.Panicln("Private key is not valid")
|
log.Panicln("Private key is not valid")
|
||||||
}
|
}
|
||||||
fmt.Println(cert.SignatureAlgorithm)
|
|
||||||
return KeyStore{cert: cert, caCertChain: caCerts, privKey: privKey}
|
return KeyStore{cert: cert, caCertChain: caCerts, privKey: privKey}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -80,13 +81,64 @@ func (k *KeyStore) GetTLSConfig() *tls.Config {
|
||||||
}
|
}
|
||||||
config := &tls.Config{
|
config := &tls.Config{
|
||||||
Certificates: []tls.Certificate{certificate},
|
Certificates: []tls.Certificate{certificate},
|
||||||
ClientCAs: caCertPool,
|
|
||||||
RootCAs: caCertPool,
|
|
||||||
ClientAuth: tls.RequireAndVerifyClientCert,
|
|
||||||
}
|
}
|
||||||
return config
|
return config
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (k *KeyStore) GetServerTLSConfig() *tls.Config {
|
||||||
|
tlsConfig := k.GetTLSConfig()
|
||||||
|
|
||||||
|
//Add the CA certificate chain to a CertPool
|
||||||
|
caCertPool := x509.NewCertPool()
|
||||||
|
for _, caCert := range k.caCertChain {
|
||||||
|
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
|
||||||
|
return tlsConfig
|
||||||
|
}
|
||||||
|
|
||||||
|
func (k *KeyStore) GetClientTLSConfig() *tls.Config {
|
||||||
|
tlsConfig := k.GetTLSConfig()
|
||||||
|
|
||||||
|
//Add the CA certificate chain to a CertPool
|
||||||
|
caCertPool := x509.NewCertPool()
|
||||||
|
for _, caCert := range k.caCertChain {
|
||||||
|
caCertPool.AddCert(caCert)
|
||||||
|
}
|
||||||
|
tlsConfig.RootCAs = caCertPool
|
||||||
|
tlsConfig.InsecureSkipVerify = true
|
||||||
|
tlsConfig.VerifyPeerCertificate = func(rawCerts [][]byte, _ [][]*x509.Certificate) error {
|
||||||
|
// Verify the peer's certificate
|
||||||
|
opts := x509.VerifyOptions{
|
||||||
|
Roots: caCertPool,
|
||||||
|
}
|
||||||
|
for _, certBytes := range rawCerts {
|
||||||
|
cert, err := x509.ParseCertificate(certBytes)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
oidMap := ExtractAllOIDValues(cert)
|
||||||
|
|
||||||
|
// Check if the certificate is signed by the specified CA
|
||||||
|
_, err = cert.Verify(opts)
|
||||||
|
if err != nil {
|
||||||
|
return errors.New("certificate not signed by trusted CA")
|
||||||
|
}
|
||||||
|
|
||||||
|
//Check if the pseudonym field is set to "SERVER"
|
||||||
|
if oidMap["2.5.4.65"] != "SERVER"{
|
||||||
|
return errors.New("peer isn't the server")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
return tlsConfig
|
||||||
|
}
|
||||||
|
|
||||||
func (k KeyStore) EncryptMessageContent(receiverCert *x509.Certificate, content []byte) []byte {
|
func (k KeyStore) EncryptMessageContent(receiverCert *x509.Certificate, content []byte) []byte {
|
||||||
// Digital envolope
|
// Digital envolope
|
||||||
|
|
||||||
|
|
|
@ -7,7 +7,7 @@ import (
|
||||||
|
|
||||||
|
|
||||||
type ClientTLSConfigProvider interface {
|
type ClientTLSConfigProvider interface {
|
||||||
GetTLSConfig() *tls.Config
|
GetClientTLSConfig() *tls.Config
|
||||||
}
|
}
|
||||||
|
|
||||||
type Client[T any] struct {
|
type Client[T any] struct {
|
||||||
|
@ -15,9 +15,9 @@ type Client[T any] struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewClient[T any](clientTLSConfigProvider ClientTLSConfigProvider) Client[T] {
|
func NewClient[T any](clientTLSConfigProvider ClientTLSConfigProvider) Client[T] {
|
||||||
dialConn, err := tls.Dial("tcp", "localhost:8080", clientTLSConfigProvider.GetTLSConfig())
|
dialConn, err := tls.Dial("tcp", "localhost:8080", clientTLSConfigProvider.GetClientTLSConfig())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Panicln("Could not open connection to server",err)
|
log.Panicln("Server connection error:\n",err)
|
||||||
}
|
}
|
||||||
conn := NewConnection[T](dialConn)
|
conn := NewConnection[T](dialConn)
|
||||||
return Client[T]{Connection: conn}
|
return Client[T]{Connection: conn}
|
||||||
|
|
|
@ -8,7 +8,7 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
type ServerTLSConfigProvider interface {
|
type ServerTLSConfigProvider interface {
|
||||||
GetTLSConfig() *tls.Config
|
GetServerTLSConfig() *tls.Config
|
||||||
}
|
}
|
||||||
|
|
||||||
type Server[T any] struct {
|
type Server[T any] struct {
|
||||||
|
@ -18,7 +18,7 @@ type Server[T any] struct {
|
||||||
|
|
||||||
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.GetTLSConfig())
|
listener, err := tls.Listen("tcp", fmt.Sprintf("0.0.0.0:%d", port), serverTLSConfigProvider.GetServerTLSConfig())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic("Server could not bind to address")
|
panic("Server could not bind to address")
|
||||||
}
|
}
|
||||||
|
@ -39,7 +39,7 @@ func (s *Server[T]) ListenLoop() {
|
||||||
if !ok {
|
if !ok {
|
||||||
panic("Connection is not a TLS connection")
|
panic("Connection is not a TLS connection")
|
||||||
}
|
}
|
||||||
fmt.Println(tlsConn)
|
tlsConn.Handshake()
|
||||||
|
|
||||||
state := tlsConn.ConnectionState()
|
state := tlsConn.ConnectionState()
|
||||||
if len(state.PeerCertificates) == 0 {
|
if len(state.PeerCertificates) == 0 {
|
||||||
|
|
|
@ -17,6 +17,10 @@ cmd="go run ./cmd/server/server.go"
|
||||||
deps=["check"]
|
deps=["check"]
|
||||||
cmd="go run ./cmd/client/client.go -user certs/client1/client1.p12 send CLI1 testsubject"
|
cmd="go run ./cmd/client/client.go -user certs/client1/client1.p12 send CLI1 testsubject"
|
||||||
|
|
||||||
|
[targets.FakeClient1]
|
||||||
|
deps=["check"]
|
||||||
|
cmd="go run ./cmd/client/client.go -user certs/FakeClient1/client1.p12 send CLI1 testsubject"
|
||||||
|
|
||||||
[targets.client2]
|
[targets.client2]
|
||||||
deps=["check"]
|
deps=["check"]
|
||||||
cmd="go run ./cmd/client/client.go -user certs/client2/client2.p12 send CLI1 testsubject"
|
cmd="go run ./cmd/client/client.go -user certs/client2/client2.p12 send CLI1 testsubject"
|
||||||
|
|
Loading…
Reference in a new issue