|
@@ -36,32 +36,40 @@ func (ta *TLSAuth) ocspQuery(server string, cert *x509.Certificate, issuer *x509
|
|
|
ta.logger.Errorf("TLSAuth: error creating OCSP request: %s", err)
|
|
|
return nil, err
|
|
|
}
|
|
|
+
|
|
|
httpRequest, err := http.NewRequest(http.MethodPost, server, bytes.NewBuffer(req))
|
|
|
if err != nil {
|
|
|
ta.logger.Error("TLSAuth: cannot create HTTP request for OCSP")
|
|
|
return nil, err
|
|
|
}
|
|
|
+
|
|
|
ocspURL, err := url.Parse(server)
|
|
|
if err != nil {
|
|
|
ta.logger.Error("TLSAuth: cannot parse OCSP URL")
|
|
|
return nil, err
|
|
|
}
|
|
|
+
|
|
|
httpRequest.Header.Add("Content-Type", "application/ocsp-request")
|
|
|
httpRequest.Header.Add("Accept", "application/ocsp-response")
|
|
|
httpRequest.Header.Add("host", ocspURL.Host)
|
|
|
+
|
|
|
httpClient := &http.Client{}
|
|
|
+
|
|
|
httpResponse, err := httpClient.Do(httpRequest)
|
|
|
if err != nil {
|
|
|
ta.logger.Error("TLSAuth: cannot send HTTP request to OCSP")
|
|
|
return nil, err
|
|
|
}
|
|
|
defer httpResponse.Body.Close()
|
|
|
+
|
|
|
output, err := io.ReadAll(httpResponse.Body)
|
|
|
if err != nil {
|
|
|
ta.logger.Error("TLSAuth: cannot read HTTP response from OCSP")
|
|
|
return nil, err
|
|
|
}
|
|
|
+
|
|
|
ocspResponse, err := ocsp.ParseResponseForCert(output, cert, issuer)
|
|
|
+
|
|
|
return ocspResponse, err
|
|
|
}
|
|
|
|
|
@@ -72,10 +80,12 @@ func (ta *TLSAuth) isExpired(cert *x509.Certificate) bool {
|
|
|
ta.logger.Errorf("TLSAuth: client certificate is expired (NotAfter: %s)", cert.NotAfter.UTC())
|
|
|
return true
|
|
|
}
|
|
|
+
|
|
|
if cert.NotBefore.UTC().After(now) {
|
|
|
ta.logger.Errorf("TLSAuth: client certificate is not yet valid (NotBefore: %s)", cert.NotBefore.UTC())
|
|
|
return true
|
|
|
}
|
|
|
+
|
|
|
return false
|
|
|
}
|
|
|
|
|
@@ -84,12 +94,14 @@ func (ta *TLSAuth) isOCSPRevoked(cert *x509.Certificate, issuer *x509.Certificat
|
|
|
ta.logger.Infof("TLSAuth: no OCSP Server present in client certificate, skipping OCSP verification")
|
|
|
return false, nil
|
|
|
}
|
|
|
+
|
|
|
for _, server := range cert.OCSPServer {
|
|
|
ocspResponse, err := ta.ocspQuery(server, cert, issuer)
|
|
|
if err != nil {
|
|
|
ta.logger.Errorf("TLSAuth: error querying OCSP server %s: %s", server, err)
|
|
|
continue
|
|
|
}
|
|
|
+
|
|
|
switch ocspResponse.Status {
|
|
|
case ocsp.Good:
|
|
|
return false, nil
|
|
@@ -100,7 +112,9 @@ func (ta *TLSAuth) isOCSPRevoked(cert *x509.Certificate, issuer *x509.Certificat
|
|
|
continue
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
log.Infof("Could not get any valid OCSP response, assuming the cert is revoked")
|
|
|
+
|
|
|
return true, nil
|
|
|
}
|
|
|
|
|
@@ -109,24 +123,29 @@ func (ta *TLSAuth) isCRLRevoked(cert *x509.Certificate) (bool, error) {
|
|
|
ta.logger.Warn("no crl_path, skipping CRL check")
|
|
|
return false, nil
|
|
|
}
|
|
|
+
|
|
|
crlContent, err := os.ReadFile(ta.CrlPath)
|
|
|
if err != nil {
|
|
|
ta.logger.Warnf("could not read CRL file, skipping check: %s", err)
|
|
|
return false, nil
|
|
|
}
|
|
|
+
|
|
|
crl, err := x509.ParseCRL(crlContent)
|
|
|
if err != nil {
|
|
|
ta.logger.Warnf("could not parse CRL file, skipping check: %s", err)
|
|
|
return false, nil
|
|
|
}
|
|
|
+
|
|
|
if crl.HasExpired(time.Now().UTC()) {
|
|
|
ta.logger.Warn("CRL has expired, will still validate the cert against it.")
|
|
|
}
|
|
|
+
|
|
|
for _, revoked := range crl.TBSCertList.RevokedCertificates {
|
|
|
if revoked.SerialNumber.Cmp(cert.SerialNumber) == 0 {
|
|
|
return true, fmt.Errorf("client certificate is revoked by CRL")
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
return false, nil
|
|
|
}
|
|
|
|
|
@@ -143,6 +162,7 @@ func (ta *TLSAuth) isRevoked(cert *x509.Certificate, issuer *x509.Certificate) (
|
|
|
} else {
|
|
|
ta.logger.Tracef("TLSAuth: no cached value for cert %s", sn)
|
|
|
}
|
|
|
+
|
|
|
revoked, err := ta.isOCSPRevoked(cert, issuer)
|
|
|
if err != nil {
|
|
|
ta.revokationCache[sn] = cacheEntry{
|
|
@@ -150,22 +170,27 @@ func (ta *TLSAuth) isRevoked(cert *x509.Certificate, issuer *x509.Certificate) (
|
|
|
err: err,
|
|
|
timestamp: time.Now().UTC(),
|
|
|
}
|
|
|
+
|
|
|
return true, err
|
|
|
}
|
|
|
+
|
|
|
if revoked {
|
|
|
ta.revokationCache[sn] = cacheEntry{
|
|
|
revoked: revoked,
|
|
|
err: err,
|
|
|
timestamp: time.Now().UTC(),
|
|
|
}
|
|
|
+
|
|
|
return true, nil
|
|
|
}
|
|
|
+
|
|
|
revoked, err = ta.isCRLRevoked(cert)
|
|
|
ta.revokationCache[sn] = cacheEntry{
|
|
|
revoked: revoked,
|
|
|
err: err,
|
|
|
timestamp: time.Now().UTC(),
|
|
|
}
|
|
|
+
|
|
|
return revoked, err
|
|
|
}
|
|
|
|
|
@@ -173,6 +198,7 @@ func (ta *TLSAuth) isInvalid(cert *x509.Certificate, issuer *x509.Certificate) (
|
|
|
if ta.isExpired(cert) {
|
|
|
return true, nil
|
|
|
}
|
|
|
+
|
|
|
revoked, err := ta.isRevoked(cert, issuer)
|
|
|
if err != nil {
|
|
|
//Fail securely, if we can't check the revocation status, let's consider the cert invalid
|
|
@@ -189,24 +215,30 @@ func (ta *TLSAuth) SetAllowedOu(allowedOus []string) error {
|
|
|
if ou == "" {
|
|
|
return fmt.Errorf("empty ou isn't allowed")
|
|
|
}
|
|
|
+
|
|
|
//drop & warn on duplicate ou
|
|
|
ok := true
|
|
|
+
|
|
|
for _, validOu := range ta.AllowedOUs {
|
|
|
if validOu == ou {
|
|
|
ta.logger.Warningf("dropping duplicate ou %s", ou)
|
|
|
+
|
|
|
ok = false
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
if ok {
|
|
|
ta.AllowedOUs = append(ta.AllowedOUs, ou)
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
return nil
|
|
|
}
|
|
|
|
|
|
func (ta *TLSAuth) ValidateCert(c *gin.Context) (bool, string, error) {
|
|
|
//Checks cert validity, Returns true + CN if client cert matches requested OU
|
|
|
var clientCert *x509.Certificate
|
|
|
+
|
|
|
if c.Request.TLS == nil || len(c.Request.TLS.PeerCertificates) == 0 {
|
|
|
//do not error if it's not TLS or there are no peer certs
|
|
|
return false, "", nil
|
|
@@ -215,6 +247,7 @@ func (ta *TLSAuth) ValidateCert(c *gin.Context) (bool, string, error) {
|
|
|
if len(c.Request.TLS.VerifiedChains) > 0 {
|
|
|
validOU := false
|
|
|
clientCert = c.Request.TLS.VerifiedChains[0][0]
|
|
|
+
|
|
|
for _, ou := range clientCert.Subject.OrganizationalUnit {
|
|
|
for _, allowedOu := range ta.AllowedOUs {
|
|
|
if allowedOu == ou {
|
|
@@ -223,21 +256,27 @@ func (ta *TLSAuth) ValidateCert(c *gin.Context) (bool, string, error) {
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
if !validOU {
|
|
|
return false, "", fmt.Errorf("client certificate OU (%v) doesn't match expected OU (%v)",
|
|
|
clientCert.Subject.OrganizationalUnit, ta.AllowedOUs)
|
|
|
}
|
|
|
+
|
|
|
revoked, err := ta.isInvalid(clientCert, c.Request.TLS.VerifiedChains[0][1])
|
|
|
if err != nil {
|
|
|
ta.logger.Errorf("TLSAuth: error checking if client certificate is revoked: %s", err)
|
|
|
return false, "", fmt.Errorf("could not check for client certification revokation status: %w", err)
|
|
|
}
|
|
|
+
|
|
|
if revoked {
|
|
|
return false, "", fmt.Errorf("client certificate is revoked")
|
|
|
}
|
|
|
+
|
|
|
ta.logger.Debugf("client OU %v is allowed vs required OU %v", clientCert.Subject.OrganizationalUnit, ta.AllowedOUs)
|
|
|
+
|
|
|
return true, clientCert.Subject.CommonName, nil
|
|
|
}
|
|
|
+
|
|
|
return false, "", fmt.Errorf("no verified cert in request")
|
|
|
}
|
|
|
|
|
@@ -248,9 +287,11 @@ func NewTLSAuth(allowedOus []string, crlPath string, cacheExpiration time.Durati
|
|
|
CrlPath: crlPath,
|
|
|
logger: logger,
|
|
|
}
|
|
|
+
|
|
|
err := ta.SetAllowedOu(allowedOus)
|
|
|
if err != nil {
|
|
|
return nil, err
|
|
|
}
|
|
|
+
|
|
|
return ta, nil
|
|
|
}
|