1a2a1a34a30b966e5e85eb0e7c14043ee4e75feb
[shibboleth/cpp-opensaml.git] / saml / saml2 / profile / impl / Assertion20Validator.cpp
1 /**
2  * Licensed to the University Corporation for Advanced Internet
3  * Development, Inc. (UCAID) under one or more contributor license
4  * agreements. See the NOTICE file distributed with this work for
5  * additional information regarding copyright ownership.
6  *
7  * UCAID licenses this file to you under the Apache License,
8  * Version 2.0 (the "License"); you may not use this file except
9  * in compliance with the License. You may obtain a copy of the
10  * License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing,
15  * software distributed under the License is distributed on an
16  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
17  * either express or implied. See the License for the specific
18  * language governing permissions and limitations under the License.
19  */
20
21 /**
22  * Assertion20Validator.cpp
23  * 
24  * SAML 2.0 basic assertion validator.
25  */
26
27 #include "internal.h"
28 #include "saml2/core/Assertions.h"
29 #include "saml2/profile/AssertionValidator.h"
30
31 #include <boost/bind.hpp>
32 #include <xmltooling/logging.h>
33 #include <xmltooling/XMLToolingConfig.h>
34 #include <xmltooling/util/NDC.h>
35
36 using namespace opensaml::saml2;
37 using namespace xmltooling::logging;
38 using namespace xmltooling;
39 using namespace boost;
40 using namespace std;
41
42 AssertionValidator::AssertionValidator(const XMLCh* recipient, const vector<const XMLCh*>* audiences, time_t ts)
43     : m_recipient(recipient), m_audiences(audiences), m_ts(ts)
44 {
45 }
46
47 AssertionValidator::~AssertionValidator()
48 {
49 }
50
51 void AssertionValidator::validate(const xmltooling::XMLObject* xmlObject) const
52 {
53     const Assertion* a=dynamic_cast<const Assertion*>(xmlObject);
54     if (!a)
55         throw ValidationException("Validator only applies to SAML 2.0 Assertion objects.");
56     validateAssertion(*a);
57 }
58
59 void AssertionValidator::validateAssertion(const Assertion& assertion) const
60 {
61 #ifdef _DEBUG
62     xmltooling::NDC ndc("validate");
63 #endif
64
65     const Conditions* conds = assertion.getConditions();
66     if (!conds)
67         return;
68     
69     // First verify the time conditions, using the specified timestamp, if non-zero.
70     if (m_ts>0) {
71         unsigned int skew = XMLToolingConfig::getConfig().clock_skew_secs;
72         time_t t=conds->getNotBeforeEpoch();
73         if (m_ts+skew < t)
74             throw ValidationException("Assertion is not yet valid.");
75         t=conds->getNotOnOrAfterEpoch();
76         if (t <= m_ts-skew)
77             throw ValidationException("Assertion is no longer valid.");
78     }
79
80     // Now we process conditions, starting with the known types and then extensions.
81     const vector<AudienceRestriction*>& acvec = conds->getAudienceRestrictions();
82     for_each(acvec.begin(), acvec.end(), boost::bind(&AssertionValidator::validateCondition, this, _1));
83
84     const vector<OneTimeUse*>& dncvec = conds->getOneTimeUses();
85     for_each(dncvec.begin(), dncvec.end(), boost::bind(&AssertionValidator::validateCondition, this, _1));
86
87     const vector<Condition*>& convec = conds->getConditions();
88     for_each(convec.begin(), convec.end(), boost::bind(&AssertionValidator::validateCondition, this, _1));
89 }
90
91 void AssertionValidator::validateCondition(const Condition* c) const
92 {
93     const AudienceRestriction* ac=dynamic_cast<const AudienceRestriction*>(c);
94     if (!ac) {
95         Category::getInstance(SAML_LOGCAT".AssertionValidator").error("unrecognized Condition in assertion (%s)",
96             c->getSchemaType() ? c->getSchemaType()->toString().c_str() : c->getElementQName().toString().c_str());
97         throw ValidationException("Assertion contains an unrecognized condition.");
98     }
99
100     bool found = false;
101     const vector<Audience*>& auds1 = ac->getAudiences();
102     for (vector<Audience*>::const_iterator a = auds1.begin(); !found && a!=auds1.end(); ++a) {
103         if (XMLString::equals(m_recipient, (*a)->getAudienceURI())) {
104             found = true;
105         }
106         else if (m_audiences) {
107             for (vector<const XMLCh*>::const_iterator a2 = m_audiences->begin(); !found && a2!=m_audiences->end(); ++a2) {
108                 found = XMLString::equals((*a)->getAudienceURI(), *a2);
109             }
110         }
111     }
112
113     if (!found) {
114         ostringstream os;
115         os << *ac;
116         Category::getInstance(SAML_LOGCAT".AssertionValidator").error("unacceptable AudienceRestriction in assertion (%s)", os.str().c_str());
117         throw ValidationException("Assertion contains an unacceptable AudienceRestriction.");
118     }
119 }