2 * Copyright 2001-2007 Internet2
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 #include <saml/SAMLConfig.h>
19 #include <saml/saml2/metadata/Metadata.h>
20 #include <saml/saml2/metadata/MetadataProvider.h>
21 #include <xmltooling/security/AbstractPKIXTrustEngine.h>
23 using namespace opensaml::saml2;
24 using namespace opensaml::saml2md;
25 using namespace xmlsignature;
28 class SampleTrustEngine : public AbstractPKIXTrustEngine {
30 SampleTrustEngine() {}
31 ~SampleTrustEngine() {}
33 class SampleIterator : public PKIXValidationInfoIterator {
34 vector<XSECCryptoX509CRL*> m_crls;
35 KeyResolver::ResolvedCertificates m_certs;
36 KeyResolver* m_resolver;
39 SampleIterator(const KeyResolver& keyResolver)
40 : PKIXValidationInfoIterator(keyResolver), m_resolver(NULL), m_done(false) {
41 string config = data_path + "security/FilesystemKeyResolver.xml";
42 ifstream in(config.c_str());
43 DOMDocument* doc=XMLToolingConfig::getConfig().getParser().parse(in);
44 XercesJanitor<DOMDocument> janitor(doc);
45 m_resolver = XMLToolingConfig::getConfig().KeyResolverManager.newPlugin(
46 FILESYSTEM_KEY_RESOLVER,doc->getDocumentElement()
48 m_resolver->resolveCertificates((KeyInfo*)NULL,m_certs);
62 int getVerificationDepth() const {
66 const vector<XSECCryptoX509*>& getTrustAnchors() const {
70 const vector<XSECCryptoX509CRL*>& getCRLs() const {
75 PKIXValidationInfoIterator* getPKIXValidationInfoIterator(
76 const KeyInfoSource& keyInfoSource,
77 const KeyResolver& keyResolver
79 dynamic_cast<const RoleDescriptor&>(keyInfoSource);
80 return new SampleIterator(keyResolver);
85 class AbstractPKIXTrustEngineTest : public CxxTest::TestSuite, public SAMLObjectBaseTestCase {
88 SAMLObjectBaseTestCase::setUp();
92 SAMLObjectBaseTestCase::tearDown();
95 void testExplicitKeyTrustEngine() {
96 string config = data_path + "security/XMLMetadataProvider.xml";
97 ifstream in(config.c_str());
98 DOMDocument* doc=XMLToolingConfig::getConfig().getParser().parse(in);
99 XercesJanitor<DOMDocument> janitor(doc);
101 auto_ptr_XMLCh path("path");
102 string s = data_path + "security/example-metadata.xml";
103 auto_ptr_XMLCh file(s.c_str());
104 doc->getDocumentElement()->setAttributeNS(NULL,path.get(),file.get());
106 // Build metadata provider.
107 auto_ptr<MetadataProvider> metadataProvider(
108 opensaml::SAMLConfig::getConfig().MetadataProviderManager.newPlugin(XML_METADATA_PROVIDER,doc->getDocumentElement())
111 metadataProvider->init();
113 catch (XMLToolingException& ex) {
118 // Build trust engine.
119 auto_ptr<TrustEngine> trustEngine(new SampleTrustEngine());
121 // Get signed assertion.
122 config = data_path + "signature/SAML2Assertion.xml";
123 ifstream in2(config.c_str());
124 DOMDocument* doc2=XMLToolingConfig::getConfig().getParser().parse(in2);
125 XercesJanitor<DOMDocument> janitor2(doc2);
126 auto_ptr<Assertion> assertion(dynamic_cast<Assertion*>(XMLObjectBuilder::getBuilder(doc2->getDocumentElement())->buildFromDocument(doc2)));
129 Locker locker(metadataProvider.get());
130 const EntityDescriptor* descriptor = metadataProvider->getEntityDescriptor("https://idp.example.org");
131 TSM_ASSERT("Retrieved entity descriptor was null", descriptor!=NULL);
133 RoleDescriptor* role=descriptor->getIDPSSODescriptors().front();
134 TSM_ASSERT("Role not present", role!=NULL);
136 Signature* sig=assertion->getSignature();
137 TSM_ASSERT("Signature not present", sig!=NULL);
138 TSM_ASSERT("Signature failed to validate.", trustEngine->validate(*sig, *role, metadataProvider->getKeyResolver()));
140 descriptor = metadataProvider->getEntityDescriptor("https://idp2.example.org");
141 TSM_ASSERT("Retrieved entity descriptor was null", descriptor!=NULL);
143 role=descriptor->getIDPSSODescriptors().front();
144 TSM_ASSERT("Role not present", role!=NULL);
146 TSM_ASSERT("Signature validated.", !trustEngine->validate(*sig, *role, metadataProvider->getKeyResolver()));