Skip to content

#192: Support multiple IdP signing certificates #193

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 1, 2019
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 25 additions & 19 deletions service_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,48 +205,54 @@ func (sp *ServiceProvider) GetSSOBindingLocation(binding string) string {
return ""
}

// getIDPSigningCert returns the certificate which we can use to verify things
// getIDPSigningCerts returns the certificates which we can use to verify things
// signed by the IDP in PEM format, or nil if no such certificate is found.
func (sp *ServiceProvider) getIDPSigningCert() (*x509.Certificate, error) {
certStr := ""
func (sp *ServiceProvider) getIDPSigningCerts() ([]*x509.Certificate, error) {
var certStrs []string
for _, idpSSODescriptor := range sp.IDPMetadata.IDPSSODescriptors {
for _, keyDescriptor := range idpSSODescriptor.KeyDescriptors {
if keyDescriptor.Use == "signing" {
certStr = keyDescriptor.KeyInfo.Certificate
break
certStrs = append(certStrs, keyDescriptor.KeyInfo.Certificate)
}
}
}

// If there are no explicitly signing certs, just return the first
// non-empty cert we find.
if certStr == "" {
if len(certStrs) == 0 {
for _, idpSSODescriptor := range sp.IDPMetadata.IDPSSODescriptors {
for _, keyDescriptor := range idpSSODescriptor.KeyDescriptors {
if keyDescriptor.Use == "" && keyDescriptor.KeyInfo.Certificate != "" {
certStr = keyDescriptor.KeyInfo.Certificate
certStrs = append(certStrs, keyDescriptor.KeyInfo.Certificate)
break
}
}
}
}

if certStr == "" {
if len(certStrs) == 0 {
return nil, errors.New("cannot find any signing certificate in the IDP SSO descriptor")
}

var certs []*x509.Certificate

// cleanup whitespace
certStr = regexp.MustCompile(`\s+`).ReplaceAllString(certStr, "")
certBytes, err := base64.StdEncoding.DecodeString(certStr)
if err != nil {
return nil, fmt.Errorf("cannot parse certificate: %s", err)
}
regex := regexp.MustCompile(`\s+`)
for _, certStr := range certStrs {
certStr = regex.ReplaceAllString(certStr, "")
certBytes, err := base64.StdEncoding.DecodeString(certStr)
if err != nil {
return nil, fmt.Errorf("cannot parse certificate: %s", err)
}

parsedCert, err := x509.ParseCertificate(certBytes)
if err != nil {
return nil, err
parsedCert, err := x509.ParseCertificate(certBytes)
if err != nil {
return nil, err
}
certs = append(certs, parsedCert)
}
return parsedCert, nil

return certs, nil
}

// MakeAuthenticationRequest produces a new AuthnRequest object for idpURL.
Expand Down Expand Up @@ -627,13 +633,13 @@ func (sp *ServiceProvider) validateSigned(responseEl *etree.Element) error {

// validateSignature returns nill iff the Signature embedded in the element is valid
func (sp *ServiceProvider) validateSignature(el *etree.Element) error {
cert, err := sp.getIDPSigningCert()
certs, err := sp.getIDPSigningCerts()
if err != nil {
return err
}

certificateStore := dsig.MemoryX509CertificateStore{
Roots: []*x509.Certificate{cert},
Roots: certs,
}

validationContext := dsig.NewDefaultValidationContext(&certificateStore)
Expand Down