Change license header, remove stale pkg files.
[shibboleth/cpp-opensaml.git] / samltest / encryption / EncryptedAssertionTest.h
1 /**
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.
6  *
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
10  * License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
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.
19  */
20
21 #include "signature/SAMLSignatureTestBase.h"
22
23 #include <fstream>
24 #include <sstream>
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>
32
33 using namespace opensaml::saml2md;
34 using namespace opensaml::saml2;
35
36 class EncryptedAssertionTest : public CxxTest::TestSuite, public SAMLSignatureTestBase {
37     MetadataProvider* m_metadata;
38 public:
39     void setUp() {
40         childElementsFile  = data_path + "signature/SAML2Assertion.xml";
41         SAMLSignatureTestBase::setUp();
42         
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);
47
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());
52
53         m_metadata = opensaml::SAMLConfig::getConfig().MetadataProviderManager.newPlugin(
54             XML_METADATA_PROVIDER,doc->getDocumentElement()
55             );
56         m_metadata->init();
57     }
58
59     void tearDown() {
60         delete m_metadata;
61         m_metadata=nullptr;
62         SAMLSignatureTestBase::tearDown();
63     }
64
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");
71         
72         Issuer* is=IssuerBuilder::buildIssuer();
73         is->setName(issuer.get());
74
75         NameID* n=NameIDBuilder::buildNameID();
76         n->setName(nameid.get());        
77         Subject* subject=SubjectBuilder::buildSubject();
78         subject->setNameID(n);
79
80         AuthnStatement* statement=AuthnStatementBuilder::buildAuthnStatement();
81         statement->setAuthnInstant(issueInstant.get());
82
83         AuthnContext* ac=AuthnContextBuilder::buildAuthnContext();
84         AuthnContextClassRef* acc=AuthnContextClassRefBuilder::buildAuthnContextClassRef();
85         acc->setReference(method.get());
86         ac->setAuthnContextClassRef(acc);
87         statement->setAuthnContext(ac);
88         
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);
95
96         // Append a Signature.
97         Signature* sig=SignatureBuilder::buildSignature();
98         assertion->setSignature(sig);
99
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);
107
108         DOMElement* rootElement = nullptr;
109         try {
110             rootElement=assertion->marshall((DOMDocument*)nullptr,&sigs,cred);
111         }
112         catch (XMLToolingException& e) {
113             TS_TRACE(e.what());
114             throw;
115         }
116         
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)
127             );
128         encrypted->encrypt(*assertion.get(), recipients);
129         
130         // Roundtrip it.
131         string buf;
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());
137         
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);
142         
143         // And check the signature.
144         try {
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());
149         }
150         catch (XMLToolingException& e) {
151             TS_TRACE(e.what());
152             throw;
153         }
154     }
155
156 };