lab 7 done

This commit is contained in:
Khakimov Dmitrij
2026-11-29 21:59:16 +03:00
parent 20afc2c790
commit 2a7418fc45
170 changed files with 10332 additions and 96 deletions
+69
View File
@@ -0,0 +1,69 @@
package trust
import (
"crypto/tls"
"crypto/x509"
"errors"
"os"
"strings"
)
func Client(ca, cert, key string) (*tls.Config, error) {
roots, err := os.ReadFile(ca)
if err != nil {
return nil, err
}
pool := x509.NewCertPool()
if !pool.AppendCertsFromPEM(roots) {
return nil, errors.New("invalid CA bundle")
}
cfg := &tls.Config{MinVersion: tls.VersionTLS12, RootCAs: pool}
if cert != "" {
leaf, err := tls.LoadX509KeyPair(cert, key)
if err != nil {
return nil, err
}
cfg.Certificates = []tls.Certificate{leaf}
}
return cfg, nil
}
func Server(ca, cert, key, identities, revoked string) (*tls.Config, error) {
cfg := &tls.Config{
MinVersion: tls.VersionTLS12, SessionTicketsDisabled: true,
GetCertificate: func(*tls.ClientHelloInfo) (*tls.Certificate, error) {
leaf, err := tls.LoadX509KeyPair(cert, key)
return &leaf, err
},
}
if identities == "" {
return cfg, nil
}
client, err := Client(ca, "", "")
if err != nil {
return nil, err
}
cfg.ClientAuth, cfg.ClientCAs = tls.RequireAndVerifyClientCert, client.RootCAs
allowed := strings.Fields(identities)
cfg.VerifyConnection = func(state tls.ConnectionState) error {
leaf := state.PeerCertificates[0]
deny, err := os.ReadFile(revoked)
if err != nil {
return errors.New("revocation list unavailable")
}
for _, serial := range strings.Fields(string(deny)) {
if strings.EqualFold(serial, leaf.SerialNumber.Text(16)) {
return errors.New("client certificate revoked")
}
}
if len(leaf.URIs) == 1 {
for _, identity := range allowed {
if leaf.URIs[0].String() == "spiffe://mtusi.lab/"+identity {
return nil
}
}
}
return errors.New("client identity denied")
}
return cfg, nil
}