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.
17 #include "signature/SAMLSignatureTestBase.h"
20 #include <saml/SAMLConfig.h>
21 #include <saml/saml2/core/Assertions.h>
22 #include <saml/saml2/metadata/Metadata.h>
23 #include <saml/saml2/metadata/MetadataProvider.h>
24 #include <saml/saml2/metadata/MetadataCredentialContext.h>
25 #include <saml/saml2/metadata/MetadataCredentialCriteria.h>
27 using namespace opensaml::saml2md;
28 using namespace opensaml::saml2;
30 class EncryptedAssertionTest : public CxxTest::TestSuite, public SAMLSignatureTestBase {
31 MetadataProvider* m_metadata;
34 childElementsFile = data_path + "signature/SAML2Assertion.xml";
35 SAMLSignatureTestBase::setUp();
37 string config = data_path + "binding/ExampleMetadataProvider.xml";
38 ifstream in(config.c_str());
39 DOMDocument* doc=XMLToolingConfig::getConfig().getParser().parse(in);
40 XercesJanitor<DOMDocument> janitor(doc);
42 auto_ptr_XMLCh path("path");
43 string s = data_path + "binding/example-metadata.xml";
44 auto_ptr_XMLCh file(s.c_str());
45 doc->getDocumentElement()->setAttributeNS(NULL,path.get(),file.get());
47 m_metadata = opensaml::SAMLConfig::getConfig().MetadataProviderManager.newPlugin(
48 XML_METADATA_PROVIDER,doc->getDocumentElement()
56 SAMLSignatureTestBase::tearDown();
59 void testEncryptedAssertion() {
60 auto_ptr_XMLCh issuer("issuer");
61 auto_ptr_XMLCh issueInstant("1970-01-02T01:01:02.100Z");
62 auto_ptr_XMLCh id("ident");
63 auto_ptr_XMLCh method("method");
64 auto_ptr_XMLCh nameid("John Doe");
66 Issuer* is=IssuerBuilder::buildIssuer();
67 is->setName(issuer.get());
69 NameID* n=NameIDBuilder::buildNameID();
70 n->setName(nameid.get());
71 Subject* subject=SubjectBuilder::buildSubject();
72 subject->setNameID(n);
74 AuthnStatement* statement=AuthnStatementBuilder::buildAuthnStatement();
75 statement->setAuthnInstant(issueInstant.get());
77 AuthnContext* ac=AuthnContextBuilder::buildAuthnContext();
78 AuthnContextClassRef* acc=AuthnContextClassRefBuilder::buildAuthnContextClassRef();
79 acc->setReference(method.get());
80 ac->setAuthnContextClassRef(acc);
81 statement->setAuthnContext(ac);
83 auto_ptr<Assertion> assertion(AssertionBuilder::buildAssertion());
84 assertion->setID(id.get());
85 assertion->setIssueInstant(issueInstant.get());
86 assertion->setIssuer(is);
87 assertion->setSubject(subject);
88 assertion->getAuthnStatements().push_back(statement);
90 // Append a Signature.
91 Signature* sig=SignatureBuilder::buildSignature();
92 assertion->setSignature(sig);
94 // Sign while marshalling.
95 vector<Signature*> sigs(1,sig);
96 CredentialCriteria cc;
97 cc.setUsage(CredentialCriteria::SIGNING_CREDENTIAL);
98 Locker locker(m_resolver);
99 const Credential* cred = m_resolver->resolve(&cc);
100 TSM_ASSERT("Retrieved credential was null", cred!=NULL);
102 DOMElement* rootElement = NULL;
104 rootElement=assertion->marshall((DOMDocument*)NULL,&sigs,cred);
106 catch (XMLToolingException& e) {
111 // Now encrypt this puppy to the SP role in the example metadata.
112 auto_ptr<EncryptedAssertion> encrypted(EncryptedAssertionBuilder::buildEncryptedAssertion());
113 Locker mlocker(m_metadata);
114 const EntityDescriptor* sp = m_metadata->getEntityDescriptor("https://sp.example.org/");
115 TSM_ASSERT("No metadata for recipient.", sp!=NULL);
116 const SPSSODescriptor* sprole = sp->getSPSSODescriptor(samlconstants::SAML20P_NS);
117 TSM_ASSERT("No SP role for recipient.", sprole!=NULL);
118 MetadataCredentialCriteria mcc(*sprole);
119 vector< pair<const MetadataProvider*,MetadataCredentialCriteria*> > recipients(1, make_pair(m_metadata, &mcc));
120 encrypted->encrypt(*assertion.get(), recipients);
124 XMLHelper::serialize(encrypted->marshall(), buf);
125 //TS_TRACE(buf.c_str());
126 istringstream in(buf);
127 DOMDocument* doc=XMLToolingConfig::getConfig().getParser().parse(in);
128 const XMLObjectBuilder* b = XMLObjectBuilder::getBuilder(doc->getDocumentElement());
130 // Unpack, then decypt with our key.
131 auto_ptr<EncryptedAssertion> encrypted2(dynamic_cast<EncryptedAssertion*>(b->buildFromDocument(doc)));
132 auto_ptr<Assertion> assertion2(dynamic_cast<Assertion*>(encrypted2->decrypt(*m_resolver, sp->getEntityID())));
133 assertEquals("Unmarshalled assertion does not match", expectedChildElementsDOM, assertion2.get(), false);
135 // And check the signature.
137 opensaml::SignatureProfileValidator spv;
138 SignatureValidator sv(cred);
139 spv.validate(dynamic_cast<Assertion*>(assertion2.get())->getSignature());
140 sv.validate(dynamic_cast<Assertion*>(assertion2.get())->getSignature());
142 catch (XMLToolingException& e) {