https://issues.shibboleth.net/jira/browse/SSPCPP-380
[shibboleth/cpp-sp.git] / shibsp / handler / impl / SAML2Consumer.cpp
index 1c7af13..6af77e7 100644 (file)
@@ -1,23 +1,27 @@
-/*
- *  Copyright 2001-2009 Internet2
+/**
+ * Licensed to the University Corporation for Advanced Internet
+ * Development, Inc. (UCAID) under one or more contributor license
+ * agreements. See the NOTICE file distributed with this work for
+ * additional information regarding copyright ownership.
  *
- * 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
+ * UCAID licenses this file to you 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
  *
- *     http://www.apache.org/licenses/LICENSE-2.0
+ * http://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.
+ * 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.
  */
 
 /**
  * SAML2Consumer.cpp
  *
- * SAML 2.0 assertion consumer service
+ * SAML 2.0 assertion consumer service.
  */
 
 #include "internal.h"
@@ -45,6 +49,8 @@ using namespace opensaml;
 # ifndef min
 #  define min(a,b)            (((a) < (b)) ? (a) : (b))
 # endif
+#else
+# include "lite/SAMLConstants.h"
 #endif
 
 using namespace shibsp;
@@ -64,7 +70,7 @@ namespace shibsp {
         SAML2Consumer(const DOMElement* e, const char* appId)
             : AssertionConsumerService(e, appId, Category::getInstance(SHIBSP_LOGCAT".SSO.SAML2")) {
 #ifndef SHIBSP_LITE
-            m_ssoRule = NULL;
+            m_ssoRule = nullptr;
             if (SPConfig::getConfig().isEnabled(SPConfig::OutOfProcess))
                 m_ssoRule = SAMLConfig::getConfig().SecurityPolicyRuleManager.newPlugin(BEARER_POLICY_RULE, e);
 #endif
@@ -87,11 +93,15 @@ namespace shibsp {
             const HTTPRequest& httpRequest,
             HTTPResponse& httpResponse,
             SecurityPolicy& policy,
-            const PropertySet* settings,
+            const PropertySet*,
             const XMLObject& xmlObject
             ) const;
 
         SecurityPolicyRule* m_ssoRule;
+#else
+        const XMLCh* getProtocolFamily() const {
+            return samlconstants::SAML20P_NS;
+        }
 #endif
     };
 
@@ -125,7 +135,7 @@ void SAML2Consumer::implementProtocol(
     const HTTPRequest& httpRequest,
     HTTPResponse& httpResponse,
     SecurityPolicy& policy,
-    const PropertySet* settings,
+    const PropertySet*,
     const XMLObject& xmlObject
     ) const
 {
@@ -149,8 +159,8 @@ void SAML2Consumer::implementProtocol(
         throw FatalProfileException("Incoming message contained no SAML assertions.");
 
     // Maintain list of "legit" tokens to feed to SP subsystems.
-    const Subject* ssoSubject=NULL;
-    const AuthnStatement* ssoStatement=NULL;
+    const Subject* ssoSubject=nullptr;
+    const AuthnStatement* ssoStatement=nullptr;
     vector<const opensaml::Assertion*> tokens;
 
     // Also track "bad" tokens that we'll cache but not use.
@@ -161,7 +171,7 @@ void SAML2Consumer::implementProtocol(
     vector<saml2::Assertion*> ownedtokens;
 
     // With this flag on, we ignore any unsigned assertions.
-    const EntityDescriptor* entity = NULL;
+    const EntityDescriptor* entity = nullptr;
     pair<bool,bool> flag = make_pair(false,false);
     if (alreadySecured && policy.getIssuerMetadata()) {
         entity = dynamic_cast<const EntityDescriptor*>(policy.getIssuerMetadata()->getParent());
@@ -176,13 +186,13 @@ void SAML2Consumer::implementProtocol(
     string contextualError;
 
     // Ensure the Bearer rule is in the policy set.
-    if (find_if(policy.getRules(), _rulenamed(BEARER_POLICY_RULE)) == NULL)
+    if (find_if(policy.getRules(), _rulenamed(BEARER_POLICY_RULE)) == nullptr)
         policy.getRules().push_back(m_ssoRule);
 
     // Populate recipient as audience.
     policy.getAudiences().push_back(application.getRelyingParty(entity)->getXMLString("entityID").second);
 
-    time_t now = time(NULL);
+    time_t now = time(nullptr);
     for (vector<saml2::Assertion*>::const_iterator a = assertions.begin(); a!=assertions.end(); ++a) {
         try {
             // Skip unsigned assertion?
@@ -228,10 +238,19 @@ void SAML2Consumer::implementProtocol(
             // Save off the first valid SSO statement, but favor the "soonest" session expiration.
             const vector<AuthnStatement*>& statements = const_cast<const saml2::Assertion*>(*a)->getAuthnStatements();
             for (vector<AuthnStatement*>::const_iterator s = statements.begin(); s!=statements.end(); ++s) {
-                if (authnskew.first && authnskew.second && (*s)->getAuthnInstant() && (now - (*s)->getAuthnInstantEpoch() > authnskew.second))
-                    contextualError = "The gap between now and the time you logged into your identity provider exceeds the limit.";
-                else if (!ssoStatement || (*s)->getSessionNotOnOrAfterEpoch() < ssoStatement->getSessionNotOnOrAfterEpoch())
+                if ((*s)->getAuthnInstant() && (*s)->getAuthnInstantEpoch() - XMLToolingConfig::getConfig().clock_skew_secs > now) {
+                    contextualError = "The login time at your identity provider was future-dated.";
+                }
+                else if (authnskew.first && authnskew.second && (*s)->getAuthnInstant() &&
+                        (*s)->getAuthnInstantEpoch() <= now && (now - (*s)->getAuthnInstantEpoch() > authnskew.second)) {
+                    contextualError = "The gap between now and the time you logged into your identity provider exceeds the allowed limit.";
+                }
+                else if (authnskew.first && authnskew.second && (*s)->getAuthnInstant() == nullptr) {
+                    contextualError = "Your identity provider did not supply a time of login, violating local policy.";
+                }
+                else if (!ssoStatement || (*s)->getSessionNotOnOrAfterEpoch() < ssoStatement->getSessionNotOnOrAfterEpoch()) {
                     ssoStatement = *s;
+                }
             }
 
             // Save off the first valid Subject, but favor an unencrypted NameID over anything else.
@@ -253,11 +272,11 @@ void SAML2Consumer::implementProtocol(
 
     for (vector<saml2::EncryptedAssertion*>::const_iterator ea = encassertions.begin(); cr && ea!=encassertions.end(); ++ea) {
         // Attempt to decrypt it.
-        saml2::Assertion* decrypted=NULL;
+        saml2::Assertion* decrypted=nullptr;
         try {
             Locker credlocker(cr);
             auto_ptr<MetadataCredentialCriteria> mcc(
-                policy.getIssuerMetadata() ? new MetadataCredentialCriteria(*policy.getIssuerMetadata()) : NULL
+                policy.getIssuerMetadata() ? new MetadataCredentialCriteria(*policy.getIssuerMetadata()) : nullptr
                 );
             auto_ptr<XMLObject> wrapper((*ea)->decrypt(*cr, application.getRelyingParty(entity)->getXMLString("entityID").second, mcc.get()));
             decrypted = dynamic_cast<saml2::Assertion*>(wrapper.get());
@@ -353,7 +372,7 @@ void SAML2Consumer::implementProtocol(
             else {
                 Locker credlocker(cr);
                 auto_ptr<MetadataCredentialCriteria> mcc(
-                    policy.getIssuerMetadata() ? new MetadataCredentialCriteria(*policy.getIssuerMetadata()) : NULL
+                    policy.getIssuerMetadata() ? new MetadataCredentialCriteria(*policy.getIssuerMetadata()) : nullptr
                     );
                 try {
                     auto_ptr<XMLObject> decryptedID(encname->decrypt(*cr,application.getRelyingParty(entity)->getXMLString("entityID").second,mcc.get()));
@@ -399,10 +418,10 @@ void SAML2Consumer::implementProtocol(
                 application,
                 policy.getIssuerMetadata(),
                 samlconstants::SAML20P_NS,
-                NULL,
+                nullptr,
                 ssoName,
-                (authnContext && authnContext->getAuthnContextClassRef()) ? authnContext->getAuthnContextClassRef()->getReference() : NULL,
-                (authnContext && authnContext->getAuthnContextDeclRef()) ? authnContext->getAuthnContextDeclRef()->getReference() : NULL,
+                (authnContext && authnContext->getAuthnContextClassRef()) ? authnContext->getAuthnContextClassRef()->getReference() : nullptr,
+                (authnContext && authnContext->getAuthnContextDeclRef()) ? authnContext->getAuthnContextDeclRef()->getReference() : nullptr,
                 &tokens
                 )
             );
@@ -423,12 +442,12 @@ void SAML2Consumer::implementProtocol(
             entity,
             samlconstants::SAML20P_NS,
             ssoName,
-            ssoStatement->getAuthnInstant() ? ssoStatement->getAuthnInstant()->getRawData() : NULL,
+            ssoStatement->getAuthnInstant() ? ssoStatement->getAuthnInstant()->getRawData() : nullptr,
             ssoStatement->getSessionIndex(),
-            (authnContext && authnContext->getAuthnContextClassRef()) ? authnContext->getAuthnContextClassRef()->getReference() : NULL,
-            (authnContext && authnContext->getAuthnContextDeclRef()) ? authnContext->getAuthnContextDeclRef()->getReference() : NULL,
+            (authnContext && authnContext->getAuthnContextClassRef()) ? authnContext->getAuthnContextClassRef()->getReference() : nullptr,
+            (authnContext && authnContext->getAuthnContextDeclRef()) ? authnContext->getAuthnContextDeclRef()->getReference() : nullptr,
             &tokens,
-            ctx.get() ? &ctx->getResolvedAttributes() : NULL
+            ctx.get() ? &ctx->getResolvedAttributes() : nullptr
             );
 
         if (ownedName)