70eb860f313a2d13b340c7fea1621a900d9f6671
[shibboleth/cpp-opensaml.git] / samltest / encryption / EncryptedAssertionTest.h
1 /*
2  *  Copyright 2001-2007 Internet2
3  * 
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
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 #include "signature/SAMLSignatureTestBase.h"
18
19 #include <fstream>
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>
26
27 using namespace opensaml::saml2md;
28 using namespace opensaml::saml2;
29
30 class EncryptedAssertionTest : public CxxTest::TestSuite, public SAMLSignatureTestBase {
31     MetadataProvider* m_metadata;
32 public:
33     void setUp() {
34         childElementsFile  = data_path + "signature/SAML2Assertion.xml";
35         SAMLSignatureTestBase::setUp();
36         
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);
41
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());
46
47         m_metadata = opensaml::SAMLConfig::getConfig().MetadataProviderManager.newPlugin(
48             XML_METADATA_PROVIDER,doc->getDocumentElement()
49             );
50         m_metadata->init();
51     }
52
53     void tearDown() {
54         delete m_metadata;
55         m_metadata=NULL;
56         SAMLSignatureTestBase::tearDown();
57     }
58
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");
65         
66         Issuer* is=IssuerBuilder::buildIssuer();
67         is->setName(issuer.get());
68
69         NameID* n=NameIDBuilder::buildNameID();
70         n->setName(nameid.get());        
71         Subject* subject=SubjectBuilder::buildSubject();
72         subject->setNameID(n);
73
74         AuthnStatement* statement=AuthnStatementBuilder::buildAuthnStatement();
75         statement->setAuthnInstant(issueInstant.get());
76
77         AuthnContext* ac=AuthnContextBuilder::buildAuthnContext();
78         AuthnContextClassRef* acc=AuthnContextClassRefBuilder::buildAuthnContextClassRef();
79         acc->setReference(method.get());
80         ac->setAuthnContextClassRef(acc);
81         statement->setAuthnContext(ac);
82         
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);
89
90         // Append a Signature.
91         Signature* sig=SignatureBuilder::buildSignature();
92         assertion->setSignature(sig);
93
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);
101
102         DOMElement* rootElement = NULL;
103         try {
104             rootElement=assertion->marshall((DOMDocument*)NULL,&sigs,cred);
105         }
106         catch (XMLToolingException& e) {
107             TS_TRACE(e.what());
108             throw;
109         }
110         
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);
121         
122         // Roundtrip it.
123         string buf;
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());
129         
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);
134         
135         // And check the signature.
136         try {
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());
141         }
142         catch (XMLToolingException& e) {
143             TS_TRACE(e.what());
144             throw;
145         }
146     }
147
148 };