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.
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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.
21 #include "signature/SAMLSignatureTestBase.h"
25 #include <saml/SAMLConfig.h>
26 #include <saml/saml2/core/Assertions.h>
27 #include <saml/saml2/metadata/Metadata.h>
28 #include <saml/saml2/metadata/MetadataProvider.h>
29 #include <saml/saml2/metadata/MetadataCredentialContext.h>
30 #include <saml/saml2/metadata/MetadataCredentialCriteria.h>
31 #include <xmltooling/security/Credential.h>
33 using namespace opensaml::saml2md;
34 using namespace opensaml::saml2;
36 class EncryptedAssertionTest : public CxxTest::TestSuite, public SAMLSignatureTestBase {
37 MetadataProvider* m_metadata;
40 childElementsFile = data_path + "signature/SAML2Assertion.xml";
41 SAMLSignatureTestBase::setUp();
43 string config = data_path + "binding/ExampleMetadataProvider.xml";
44 ifstream in(config.c_str());
45 DOMDocument* doc=XMLToolingConfig::getConfig().getParser().parse(in);
46 XercesJanitor<DOMDocument> janitor(doc);
48 auto_ptr_XMLCh path("path");
49 string s = data_path + "binding/example-metadata.xml";
50 auto_ptr_XMLCh file(s.c_str());
51 doc->getDocumentElement()->setAttributeNS(nullptr,path.get(),file.get());
53 m_metadata = opensaml::SAMLConfig::getConfig().MetadataProviderManager.newPlugin(
54 XML_METADATA_PROVIDER,doc->getDocumentElement()
62 SAMLSignatureTestBase::tearDown();
65 void testEncryptedAssertion() {
66 auto_ptr_XMLCh issuer("issuer");
67 auto_ptr_XMLCh issueInstant("1970-01-02T01:01:02.100Z");
68 auto_ptr_XMLCh id("ident");
69 auto_ptr_XMLCh method("method");
70 auto_ptr_XMLCh nameid("John Doe");
72 Issuer* is=IssuerBuilder::buildIssuer();
73 is->setName(issuer.get());
75 NameID* n=NameIDBuilder::buildNameID();
76 n->setName(nameid.get());
77 Subject* subject=SubjectBuilder::buildSubject();
78 subject->setNameID(n);
80 AuthnStatement* statement=AuthnStatementBuilder::buildAuthnStatement();
81 statement->setAuthnInstant(issueInstant.get());
83 AuthnContext* ac=AuthnContextBuilder::buildAuthnContext();
84 AuthnContextClassRef* acc=AuthnContextClassRefBuilder::buildAuthnContextClassRef();
85 acc->setReference(method.get());
86 ac->setAuthnContextClassRef(acc);
87 statement->setAuthnContext(ac);
89 auto_ptr<Assertion> assertion(AssertionBuilder::buildAssertion());
90 assertion->setID(id.get());
91 assertion->setIssueInstant(issueInstant.get());
92 assertion->setIssuer(is);
93 assertion->setSubject(subject);
94 assertion->getAuthnStatements().push_back(statement);
96 // Append a Signature.
97 Signature* sig=SignatureBuilder::buildSignature();
98 assertion->setSignature(sig);
100 // Sign while marshalling.
101 vector<Signature*> sigs(1,sig);
102 CredentialCriteria cc;
103 cc.setUsage(Credential::SIGNING_CREDENTIAL);
104 Locker locker(m_resolver);
105 const Credential* cred = m_resolver->resolve(&cc);
106 TSM_ASSERT("Retrieved credential was null", cred!=nullptr);
108 DOMElement* rootElement = nullptr;
110 rootElement=assertion->marshall((DOMDocument*)nullptr,&sigs,cred);
112 catch (XMLToolingException& e) {
117 // Now encrypt this puppy to the SP role in the example metadata.
118 auto_ptr<EncryptedAssertion> encrypted(EncryptedAssertionBuilder::buildEncryptedAssertion());
119 Locker mlocker(m_metadata);
120 MetadataProvider::Criteria mc("https://sp.example.org/", &SPSSODescriptor::ELEMENT_QNAME, samlconstants::SAML20P_NS);
121 pair<const EntityDescriptor*,const RoleDescriptor*> sp = m_metadata->getEntityDescriptor(mc);
122 TSM_ASSERT("No metadata for recipient.", sp.first!=nullptr);
123 TSM_ASSERT("No SP role for recipient.", sp.second!=nullptr);
124 MetadataCredentialCriteria mcc(*sp.second);
125 vector< pair<const MetadataProvider*,MetadataCredentialCriteria*> > recipients(
126 1, pair<const MetadataProvider*,MetadataCredentialCriteria*>(m_metadata, &mcc)
128 encrypted->encrypt(*assertion.get(), recipients);
132 XMLHelper::serialize(encrypted->marshall(), buf);
133 //TS_TRACE(buf.c_str());
134 istringstream in(buf);
135 DOMDocument* doc=XMLToolingConfig::getConfig().getParser().parse(in);
136 const XMLObjectBuilder* b = XMLObjectBuilder::getBuilder(doc->getDocumentElement());
138 // Unpack, then decypt with our key.
139 auto_ptr<EncryptedAssertion> encrypted2(dynamic_cast<EncryptedAssertion*>(b->buildFromDocument(doc)));
140 auto_ptr<Assertion> assertion2(dynamic_cast<Assertion*>(encrypted2->decrypt(*m_resolver, sp.first->getEntityID())));
141 assertEquals("Unmarshalled assertion does not match", expectedChildElementsDOM, assertion2.get(), false);
143 // And check the signature.
145 opensaml::SignatureProfileValidator spv;
146 SignatureValidator sv(cred);
147 spv.validate(dynamic_cast<Assertion*>(assertion2.get())->getSignature());
148 sv.validate(dynamic_cast<Assertion*>(assertion2.get())->getSignature());
150 catch (XMLToolingException& e) {