lab7: безопасность, mTLS, OpenBao и PKI lifecycle
This commit is contained in:
@@ -0,0 +1,132 @@
|
||||
package tlsconfig
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"crypto/x509"
|
||||
"encoding/pem"
|
||||
"errors"
|
||||
"fmt"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"mtusi-messenger/internal/platform/secretfile"
|
||||
)
|
||||
|
||||
type revocations struct {
|
||||
mu sync.Mutex
|
||||
stamp fileStamp
|
||||
lists []*x509.RevocationList
|
||||
}
|
||||
|
||||
func (l *Loader) revocationLists() ([]*x509.RevocationList, error) {
|
||||
files, err := l.files.snapshot()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
current, err := stamp(files.CRL)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
l.crls.mu.Lock()
|
||||
defer l.crls.mu.Unlock()
|
||||
if l.crls.lists != nil && l.crls.stamp == current {
|
||||
return l.crls.lists, nil
|
||||
}
|
||||
data, err := secretfile.Read(files.CRL)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("read tls revocations: %w", err)
|
||||
}
|
||||
lists, err := parseRevocations(data)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
l.crls.lists = lists
|
||||
l.crls.stamp = current
|
||||
return lists, nil
|
||||
}
|
||||
|
||||
func parseRevocations(data []byte) ([]*x509.RevocationList, error) {
|
||||
lists := []*x509.RevocationList{}
|
||||
for len(bytes.TrimSpace(data)) > 0 {
|
||||
block, rest := pem.Decode(data)
|
||||
if block == nil || block.Type != "X509 CRL" {
|
||||
return nil, errors.New("malformed tls crl pem")
|
||||
}
|
||||
list, err := x509.ParseRevocationList(block.Bytes)
|
||||
if err != nil {
|
||||
return nil, errors.New("malformed tls crl")
|
||||
}
|
||||
lists = append(lists, list)
|
||||
data = rest
|
||||
}
|
||||
if len(lists) == 0 {
|
||||
return nil, errors.New("tls crl bundle is empty")
|
||||
}
|
||||
return lists, nil
|
||||
}
|
||||
|
||||
func (l *Loader) verifyChains(chains [][]*x509.Certificate) error {
|
||||
if len(chains) == 0 {
|
||||
return errors.New("tls peer has no verified chain")
|
||||
}
|
||||
lists, err := l.revocationLists()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
var reason error
|
||||
for _, chain := range chains {
|
||||
if err := verifyRevocations(chain, lists, l.now()); err == nil {
|
||||
return nil
|
||||
} else if reason == nil {
|
||||
reason = err
|
||||
}
|
||||
}
|
||||
return fmt.Errorf("tls certificate chain revocation verification failed: %w", reason)
|
||||
}
|
||||
|
||||
func verifyRevocations(chain []*x509.Certificate, lists []*x509.RevocationList, now time.Time) error {
|
||||
if len(chain) < 2 {
|
||||
return errors.New("tls chain lacks an issuing authority")
|
||||
}
|
||||
for index, cert := range chain[:len(chain)-1] {
|
||||
issuer := chain[index+1]
|
||||
var applicable *x509.RevocationList
|
||||
var signatureErr error
|
||||
for _, list := range lists {
|
||||
if !bytes.Equal(list.RawIssuer, issuer.RawSubject) {
|
||||
continue
|
||||
}
|
||||
if len(list.AuthorityKeyId) > 0 && !bytes.Equal(list.AuthorityKeyId, issuer.SubjectKeyId) {
|
||||
continue
|
||||
}
|
||||
// A bundle may carry several lists for one subject (old and new
|
||||
// issuer versions). An invalid signature disqualifies that list,
|
||||
// not every later candidate for the same issuer.
|
||||
if err := list.CheckSignatureFrom(issuer); err != nil {
|
||||
signatureErr = err
|
||||
continue
|
||||
}
|
||||
if applicable == nil || list.ThisUpdate.After(applicable.ThisUpdate) {
|
||||
applicable = list
|
||||
}
|
||||
}
|
||||
if applicable == nil {
|
||||
if signatureErr != nil {
|
||||
return fmt.Errorf("tls applicable crl signature is invalid: %w", signatureErr)
|
||||
}
|
||||
return errors.New("tls issuing authority crl is missing")
|
||||
}
|
||||
if applicable.ThisUpdate.After(now) {
|
||||
return errors.New("tls applicable crl is not yet valid")
|
||||
}
|
||||
if !applicable.NextUpdate.After(now) || !applicable.NextUpdate.After(applicable.ThisUpdate) {
|
||||
return errors.New("tls applicable crl is stale")
|
||||
}
|
||||
for _, entry := range applicable.RevokedCertificateEntries {
|
||||
if cert.SerialNumber.Cmp(entry.SerialNumber) == 0 {
|
||||
return errors.New("tls certificate is revoked")
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user