mirror of
https://github.com/openziti/ziti.git
synced 2026-09-10 08:45:41 +00:00
949de99ee4
* fixes #3809 support CSR submission during OIDC authentication - accepts an optional CSR during OIDC login (all auth methods) and signs it into a session-bound certificate with a SPIFFE ID derived from the identity and API session - returns the signed certificate PEM as a top-level "session_cert" field in the token endpoint JSON response (CodeExchange, RefreshToken, TokenExchange) - adds cert-binding verification on RefreshToken and TokenExchange: if z_cfs is present the peer cert fingerprint must match (strict), otherwise falls back to SPIFFE ID verification - supports cert rotation via csr_pem form parameter on refresh and token exchange; replaces the session cert fingerprint while preserving the authenticating cert fingerprint - adds AuthCertFingerprints (z_acfs) claim to track permanent auth cert fingerprints separately from rotatable session cert fingerprints - only the leaf certificate fingerprint is added to z_cfs and z_acfs, intermediates are never included - invalid CSR returns 400 Bad Request in OIDC error format - propagates updated CustomClaims (including CertFingerprints) from access token to renewed refresh token so rotated fingerprints are enforced on subsequent refreshes - adds CertGenerated field to ApiSessionEvent - advertises OIDC_AUTH_WITH_CSR controller capability when OIDC is enabled - adds unit tests for verifyCertBinding (fingerprint, SPIFFE ID, edge cases) and CsrPem field parsing - adds integration tests for initial CSR auth (updb, cert, ext-jwt), cert-binding on refresh/exchange, CSR rotation with cert auth, SPIFFE fallback and z_cfs transition, and CSR property forging resistance * address pr concerns * add z_cfs len tests on junk chain certs * strip csr subject info, replace w/ santized values * fix csr rotation rejection/paths during token exchange/refresh
175 lines
4.9 KiB
Go
175 lines
4.9 KiB
Go
/*
|
|
Copyright NetFoundry Inc.
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
you may not use this file except in compliance with the License.
|
|
You may obtain a copy of the License at
|
|
|
|
https://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
See the License for the specific language governing permissions and
|
|
limitations under the License.
|
|
*/
|
|
|
|
package model
|
|
|
|
import (
|
|
"crypto/x509"
|
|
"crypto/x509/pkix"
|
|
"fmt"
|
|
"net/url"
|
|
"time"
|
|
|
|
"github.com/openziti/ziti/v2/common/cert"
|
|
"github.com/openziti/ziti/v2/common/eid"
|
|
"github.com/openziti/ziti/v2/controller/apierror"
|
|
"github.com/openziti/ziti/v2/controller/change"
|
|
"github.com/openziti/ziti/v2/controller/db"
|
|
"github.com/openziti/ziti/v2/controller/models"
|
|
"go.etcd.io/bbolt"
|
|
)
|
|
|
|
func NewApiSessionCertificateManager(env Env) *ApiSessionCertificateManager {
|
|
manager := &ApiSessionCertificateManager{
|
|
baseEntityManager: newBaseEntityManager[*ApiSessionCertificate, *db.ApiSessionCertificate](env, env.GetStores().ApiSessionCertificate),
|
|
}
|
|
manager.impl = manager
|
|
|
|
return manager
|
|
}
|
|
|
|
type ApiSessionCertificateManager struct {
|
|
baseEntityManager[*ApiSessionCertificate, *db.ApiSessionCertificate]
|
|
}
|
|
|
|
func (self *ApiSessionCertificateManager) NewModelEntity() *ApiSessionCertificate {
|
|
return &ApiSessionCertificate{}
|
|
}
|
|
|
|
func (self *ApiSessionCertificateManager) Create(entity *ApiSessionCertificate, ctx *change.Context) (string, error) {
|
|
return self.createEntity(entity, ctx.NewMutateContext())
|
|
}
|
|
|
|
func (self *ApiSessionCertificateManager) CreateFromCSR(identity *Identity, apiSession *ApiSession, isJwt bool, lifespan time.Duration, csrPem []byte, ctx *change.Context) (*ApiSessionCertificate, error) {
|
|
notBefore := time.Now()
|
|
notAfter := time.Now().Add(lifespan)
|
|
|
|
csr, err := cert.ParseCsrPem(csrPem)
|
|
|
|
if err != nil {
|
|
apiErr := apierror.NewCouldNotProcessCsr()
|
|
apiErr.Cause = err
|
|
apiErr.AppendCause = true
|
|
return nil, apiErr
|
|
}
|
|
|
|
newId := eid.New()
|
|
|
|
trustDomain := self.env.GetConfig().SpiffeIdTrustDomain.Hostname()
|
|
spiffeId := &url.URL{
|
|
Scheme: "spiffe",
|
|
Host: trustDomain,
|
|
Path: fmt.Sprintf("identity/%s/apiSession/%s/apiSessionCertificate/%s", identity.Id, apiSession.Id, newId),
|
|
}
|
|
|
|
certRaw, err := self.env.GetApiClientCsrSigner().SignCsr(csr, &cert.SigningOpts{
|
|
NotAfter: ¬After,
|
|
NotBefore: ¬Before,
|
|
URIs: []*url.URL{
|
|
spiffeId,
|
|
},
|
|
Subject: &pkix.Name{CommonName: identity.Id},
|
|
})
|
|
|
|
if err != nil {
|
|
apiErr := apierror.NewCouldNotProcessCsr()
|
|
apiErr.Cause = err
|
|
apiErr.AppendCause = true
|
|
return nil, apiErr
|
|
}
|
|
|
|
newCert, _ := x509.ParseCertificate(certRaw)
|
|
|
|
fp := self.env.GetFingerprintGenerator().FromCert(newCert)
|
|
|
|
chainPem, err := self.env.GetManagers().Enrollment.GetCertChainPem(certRaw)
|
|
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
entity := &ApiSessionCertificate{
|
|
BaseEntity: models.BaseEntity{
|
|
Id: newId,
|
|
},
|
|
ApiSessionId: apiSession.Id,
|
|
Subject: newCert.Subject.String(),
|
|
Fingerprint: fp,
|
|
ValidAfter: ¬Before,
|
|
ValidBefore: ¬After,
|
|
PEM: chainPem,
|
|
}
|
|
|
|
if isJwt {
|
|
// can't create if using bearer tokens, the API Session will not exist
|
|
return entity, nil
|
|
}
|
|
|
|
entity.Id, err = self.Create(entity, ctx)
|
|
|
|
return entity, err
|
|
}
|
|
|
|
func (self *ApiSessionCertificateManager) IsUpdated(_ string) bool {
|
|
return false
|
|
}
|
|
|
|
func (self *ApiSessionCertificateManager) Delete(id string, ctx *change.Context) error {
|
|
return self.deleteEntity(id, ctx)
|
|
}
|
|
|
|
func (self *ApiSessionCertificateManager) Query(tx *bbolt.Tx, query string) (*ApiSessionCertificateListResult, error) {
|
|
result := &ApiSessionCertificateListResult{manager: self}
|
|
err := self.ListWithTx(tx, query, result.collect)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return result, nil
|
|
}
|
|
|
|
func (self *ApiSessionCertificateManager) ReadByApiSessionId(tx *bbolt.Tx, apiSessionId string) ([]*ApiSessionCertificate, error) {
|
|
var result []*ApiSessionCertificate
|
|
|
|
certIds := self.env.GetStores().ApiSession.GetRelatedEntitiesIdList(tx, apiSessionId, db.EntityTypeApiSessionCertificates)
|
|
for _, key := range certIds {
|
|
apiSessionCert, err := self.readInTx(tx, key)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
result = append(result, apiSessionCert)
|
|
}
|
|
|
|
return result, nil
|
|
}
|
|
|
|
type ApiSessionCertificateListResult struct {
|
|
manager *ApiSessionCertificateManager
|
|
ApiSessionCertificates []*ApiSessionCertificate
|
|
models.QueryMetaData
|
|
}
|
|
|
|
func (result *ApiSessionCertificateListResult) collect(tx *bbolt.Tx, ids []string, queryMetaData *models.QueryMetaData) error {
|
|
result.QueryMetaData = *queryMetaData
|
|
for _, key := range ids {
|
|
ApiSessionCertificate, err := result.manager.readInTx(tx, key)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
result.ApiSessionCertificates = append(result.ApiSessionCertificates, ApiSessionCertificate)
|
|
}
|
|
return nil
|
|
}
|