File: Assertions20SchemaValidators.cpp

package info (click to toggle)
opensaml 3.3.1-3
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • size: 6,464 kB
  • sloc: cpp: 27,961; sh: 4,593; xml: 1,004; makefile: 429; ansic: 18
file content (287 lines) | stat: -rw-r--r-- 12,258 bytes parent folder | download | duplicates (9)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
/**
 * 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.
 *
 * 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
 *
 * 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.
 */

/**
 * Assertions20SchemaValidators.cpp
 *
 * Schema-based validators for SAML 2.0 Assertions classes
 */

#include "internal.h"
#include "exceptions.h"
#include "saml2/core/Assertions.h"

#include <xmltooling/validation/Validator.h>
#include <xmltooling/validation/ValidatorSuite.h>

using namespace opensaml::saml2;
using namespace opensaml;
using namespace xmltooling;
using namespace std;
using samlconstants::SAML20_NS;

namespace opensaml {
    namespace saml2 {

        XMLOBJECTVALIDATOR_SIMPLE(SAML_DLLLOCAL,Action);
        XMLOBJECTVALIDATOR_SIMPLE(SAML_DLLLOCAL,AssertionIDRef);
        XMLOBJECTVALIDATOR_SIMPLE(SAML_DLLLOCAL,AssertionURIRef);
        XMLOBJECTVALIDATOR_SIMPLE(SAML_DLLLOCAL,Audience);
        XMLOBJECTVALIDATOR_SIMPLE(SAML_DLLLOCAL,AuthnContextClassRef);
        XMLOBJECTVALIDATOR_SIMPLE(SAML_DLLLOCAL,AuthnContextDeclRef);
        XMLOBJECTVALIDATOR_SIMPLE(SAML_DLLLOCAL,AuthenticatingAuthority);
        XMLOBJECTVALIDATOR_SIMPLE(SAML_DLLLOCAL,NameIDType);
        XMLOBJECTVALIDATOR_SIMPLE(SAML_DLLLOCAL,NameID);
        XMLOBJECTVALIDATOR_SIMPLE(SAML_DLLLOCAL,Issuer);

        BEGIN_XMLOBJECTVALIDATOR(SAML_DLLLOCAL,EncryptedElementType);
            XMLOBJECTVALIDATOR_REQUIRE(EncryptedElementType,EncryptedData);
        END_XMLOBJECTVALIDATOR;

        BEGIN_XMLOBJECTVALIDATOR_SUB(SAML_DLLLOCAL,EncryptedID,EncryptedElementType);
            EncryptedElementTypeSchemaValidator::validate(xmlObject);
        END_XMLOBJECTVALIDATOR;

        BEGIN_XMLOBJECTVALIDATOR_SUB(SAML_DLLLOCAL,EncryptedAttribute,EncryptedElementType);
            EncryptedElementTypeSchemaValidator::validate(xmlObject);
        END_XMLOBJECTVALIDATOR;

        BEGIN_XMLOBJECTVALIDATOR_SUB(SAML_DLLLOCAL,EncryptedAssertion,EncryptedElementType);
            EncryptedElementTypeSchemaValidator::validate(xmlObject);
        END_XMLOBJECTVALIDATOR;

        BEGIN_XMLOBJECTVALIDATOR(SAML_DLLLOCAL,AudienceRestriction);
            XMLOBJECTVALIDATOR_NONEMPTY(AudienceRestriction,Audience);
        END_XMLOBJECTVALIDATOR;

        BEGIN_XMLOBJECTVALIDATOR(SAML_DLLLOCAL,ProxyRestriction);
            if (ptr->getAudiences().empty()) {
                XMLOBJECTVALIDATOR_REQUIRE_INTEGER(ProxyRestriction,Count);
            }
        END_XMLOBJECTVALIDATOR;

        BEGIN_XMLOBJECTVALIDATOR(SAML_DLLLOCAL,Delegate);
            int count=0;
            if (ptr->getBaseID())
                count++;
            if (ptr->getNameID())
                count++;
            if (ptr->getEncryptedID())
                count++;
            if (count != 1)
                throw ValidationException("Delegate must contain exactly one identifier element.");
        END_XMLOBJECTVALIDATOR;

        BEGIN_XMLOBJECTVALIDATOR(SAML_DLLLOCAL,DelegationRestrictionType);
            XMLOBJECTVALIDATOR_NONEMPTY(DelegationRestrictionType,Delegate);
        END_XMLOBJECTVALIDATOR;

        BEGIN_XMLOBJECTVALIDATOR(SAML_DLLLOCAL,Conditions);
            if (ptr->getOneTimeUses().size() > 1) {
                throw ValidationException("Multiple OneTimeUse condition elements are not permitted.");
            }
            else if (ptr->getProxyRestrictions().size() > 1) {
                throw ValidationException("Multiple ProxyRestriction condition elements are not permitted.");
            }
        END_XMLOBJECTVALIDATOR;

        BEGIN_XMLOBJECTVALIDATOR(SAML_DLLLOCAL,KeyInfoConfirmationDataType);
            XMLOBJECTVALIDATOR_NONEMPTY(KeyInfoConfirmationDataType,KeyInfo);
        END_XMLOBJECTVALIDATOR;

        BEGIN_XMLOBJECTVALIDATOR(SAML_DLLLOCAL,SubjectConfirmation);
            XMLOBJECTVALIDATOR_REQUIRE(SubjectConfirmation,Method);
            int count=0;
            if (ptr->getBaseID())
                count++;
            if (ptr->getNameID())
                count++;
            if (ptr->getEncryptedID())
                count++;
            if (count > 1)
                throw ValidationException("SubjectConfirmation cannot contain multiple identifier elements.");
        END_XMLOBJECTVALIDATOR;

        BEGIN_XMLOBJECTVALIDATOR(SAML_DLLLOCAL,Subject);
            int count=0;
            if (ptr->getBaseID())
                count++;
            if (ptr->getNameID())
                count++;
            if (ptr->getEncryptedID())
                count++;
            if (count > 1)
                throw ValidationException("Subject cannot contain multiple identifier elements.");
        END_XMLOBJECTVALIDATOR;

        BEGIN_XMLOBJECTVALIDATOR(SAML_DLLLOCAL,SubjectLocality);
            XMLOBJECTVALIDATOR_ONEOF(SubjectLocality,Address,DNSName);
        END_XMLOBJECTVALIDATOR;

        BEGIN_XMLOBJECTVALIDATOR(SAML_DLLLOCAL,AuthnContext);
            if (!ptr->getAuthnContextClassRef()) {
                XMLOBJECTVALIDATOR_ONLYONEOF(AuthnContext,AuthnContextDeclRef,AuthnContextDecl);
            }
        END_XMLOBJECTVALIDATOR;

        BEGIN_XMLOBJECTVALIDATOR(SAML_DLLLOCAL,AuthnStatement);
            XMLOBJECTVALIDATOR_REQUIRE(AuthnStatement,AuthnInstant);
            XMLOBJECTVALIDATOR_REQUIRE(AuthnStatement,AuthnContext);
        END_XMLOBJECTVALIDATOR;

        BEGIN_XMLOBJECTVALIDATOR(SAML_DLLLOCAL,Evidence);
            if (!ptr->hasChildren())
                throw ValidationException("Evidence must have at least one child element.");
        END_XMLOBJECTVALIDATOR;

        BEGIN_XMLOBJECTVALIDATOR(SAML_DLLLOCAL,AuthzDecisionStatement);
            XMLOBJECTVALIDATOR_REQUIRE(AuthzDecisionStatement,Resource);
            XMLOBJECTVALIDATOR_REQUIRE(AuthzDecisionStatement,Decision);
            if (!XMLString::equals(ptr->getDecision(),AuthzDecisionStatement::DECISION_PERMIT) &&
                !XMLString::equals(ptr->getDecision(),AuthzDecisionStatement::DECISION_DENY) &&
                !XMLString::equals(ptr->getDecision(),AuthzDecisionStatement::DECISION_INDETERMINATE))
                throw ValidationException("Decision must be one of Deny, Permit, or Indeterminate.");
            XMLOBJECTVALIDATOR_NONEMPTY(AuthzDecisionStatement,Action);
        END_XMLOBJECTVALIDATOR;

        BEGIN_XMLOBJECTVALIDATOR(SAML_DLLLOCAL,Attribute);
            XMLOBJECTVALIDATOR_REQUIRE(Attribute,Name);
        END_XMLOBJECTVALIDATOR;

        BEGIN_XMLOBJECTVALIDATOR(SAML_DLLLOCAL,AttributeStatement);
            XMLOBJECTVALIDATOR_NONEMPTY(AttributeStatement,Attribute);
        END_XMLOBJECTVALIDATOR;

        BEGIN_XMLOBJECTVALIDATOR(SAML_DLLLOCAL,Assertion);
            XMLOBJECTVALIDATOR_REQUIRE(Assertion,Version);
            if (!XMLString::equals(samlconstants::SAML20_VERSION, ptr->getVersion()))
                throw ValidationException("Assertion has wrong SAML Version.");
            XMLOBJECTVALIDATOR_REQUIRE(Assertion,ID);
            XMLOBJECTVALIDATOR_REQUIRE(Assertion,IssueInstant);
            XMLOBJECTVALIDATOR_REQUIRE(Assertion,Issuer);
            if ((!ptr->getAuthnStatements().empty() ||
                !ptr->getAttributeStatements().empty() ||
                !ptr->getAuthzDecisionStatements().empty()) && !ptr->getSubject())
                throw ValidationException("Assertion with standard statements must have a Subject.");
        END_XMLOBJECTVALIDATOR;

        class SAML_DLLLOCAL checkWildcardNS {
        public:
            void operator()(const XMLObject* xmlObject) const {
                const XMLCh* ns=xmlObject->getElementQName().getNamespaceURI();
                if (XMLString::equals(ns,SAML20_NS) || !ns || !*ns) {
                    throw ValidationException(
                        "Object contains an illegal extension child element ($1).",
                        params(1,xmlObject->getElementQName().toString().c_str())
                        );
                }
            }
        };

        BEGIN_XMLOBJECTVALIDATOR(SAML_DLLLOCAL,Advice);
            const vector<XMLObject*>& anys=ptr->getUnknownXMLObjects();
            for_each(anys.begin(),anys.end(),checkWildcardNS());
        END_XMLOBJECTVALIDATOR;

    };
};

#define REGISTER_ELEMENT(cname) \
    q=xmltooling::QName(SAML20_NS,cname::LOCAL_NAME); \
    XMLObjectBuilder::registerBuilder(q,new cname##Builder()); \
    SchemaValidators.registerValidator(q,new cname##SchemaValidator())

#define REGISTER_TYPE(cname) \
    q=xmltooling::QName(SAML20_NS,cname::TYPE_NAME); \
    XMLObjectBuilder::registerBuilder(q,new cname##Builder()); \
    SchemaValidators.registerValidator(q,new cname##SchemaValidator())

#define REGISTER_ELEMENT_NOVAL(cname) \
    q=xmltooling::QName(SAML20_NS,cname::LOCAL_NAME); \
    XMLObjectBuilder::registerBuilder(q,new cname##Builder());

#define REGISTER_TYPE_NOVAL(cname) \
    q=xmltooling::QName(SAML20_NS,cname::TYPE_NAME); \
    XMLObjectBuilder::registerBuilder(q,new cname##Builder());

void opensaml::saml2::registerAssertionClasses() {
    xmltooling::QName q;
    REGISTER_ELEMENT(Action);
    REGISTER_ELEMENT(Advice);
    REGISTER_ELEMENT(Assertion);
    REGISTER_ELEMENT(AssertionIDRef);
    REGISTER_ELEMENT(AssertionURIRef);
    REGISTER_ELEMENT(Attribute);
    REGISTER_ELEMENT(AttributeStatement);
    REGISTER_ELEMENT_NOVAL(AttributeValue);
    REGISTER_ELEMENT(Audience);
    REGISTER_ELEMENT(AudienceRestriction);
    REGISTER_ELEMENT(AuthenticatingAuthority);
    REGISTER_ELEMENT(AuthnContext);
    REGISTER_ELEMENT(AuthnContextClassRef);
    REGISTER_ELEMENT_NOVAL(AuthnContextDecl);
    REGISTER_ELEMENT(AuthnContextDeclRef);
    REGISTER_ELEMENT(AuthnStatement);
    REGISTER_ELEMENT(AuthzDecisionStatement);
    REGISTER_ELEMENT_NOVAL(Condition);
    REGISTER_ELEMENT(Conditions);
    REGISTER_ELEMENT(EncryptedAssertion);
    REGISTER_ELEMENT(EncryptedAttribute);
    REGISTER_ELEMENT(EncryptedID);
    REGISTER_ELEMENT(Evidence);
    REGISTER_ELEMENT(Issuer);
    REGISTER_ELEMENT(NameID);
    REGISTER_ELEMENT_NOVAL(OneTimeUse);
    REGISTER_ELEMENT(ProxyRestriction);
    REGISTER_ELEMENT_NOVAL(Statement);
    REGISTER_ELEMENT(Subject);
    REGISTER_ELEMENT(SubjectConfirmation);
    REGISTER_ELEMENT_NOVAL(SubjectConfirmationData);
    REGISTER_ELEMENT(SubjectLocality);
    REGISTER_TYPE(Action);
    REGISTER_TYPE(Advice);
    REGISTER_TYPE(Assertion);
    REGISTER_TYPE(Attribute);
    REGISTER_TYPE(AttributeStatement);
    REGISTER_TYPE(AudienceRestriction);
    REGISTER_TYPE(AuthnContext);
    REGISTER_TYPE(AuthnStatement);
    REGISTER_TYPE(AuthzDecisionStatement);
    REGISTER_TYPE(Conditions);
    REGISTER_TYPE(Evidence);
    REGISTER_TYPE(KeyInfoConfirmationDataType);
    REGISTER_TYPE(NameIDType);
    REGISTER_TYPE_NOVAL(OneTimeUse);
    REGISTER_TYPE(ProxyRestriction);
    REGISTER_TYPE(Subject);
    REGISTER_TYPE(SubjectConfirmation);
    REGISTER_TYPE(SubjectLocality);

    q=xmltooling::QName(samlconstants::SAML20_DELEGATION_CONDITION_NS,Delegate::LOCAL_NAME);
    XMLObjectBuilder::registerBuilder(q,new DelegateBuilder());
    SchemaValidators.registerValidator(q,new DelegateSchemaValidator());
    q=xmltooling::QName(samlconstants::SAML20_DELEGATION_CONDITION_NS,Delegate::TYPE_NAME);
    XMLObjectBuilder::registerBuilder(q,new DelegateBuilder());
    SchemaValidators.registerValidator(q,new DelegateSchemaValidator());

    q=xmltooling::QName(samlconstants::SAML20_DELEGATION_CONDITION_NS,DelegationRestrictionType::TYPE_NAME);
    XMLObjectBuilder::registerBuilder(q,new DelegationRestrictionTypeBuilder());
    SchemaValidators.registerValidator(q,new DelegationRestrictionTypeSchemaValidator());
}