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"
23 #include <saml/saml1/core/Assertions.h>
24 #include <saml/saml1/core/Protocols.h>
28 using namespace opensaml::saml1p;
29 using namespace opensaml::saml1;
31 class SAML1ResponseTest : public CxxTest::TestSuite, public SAMLSignatureTestBase {
34 childElementsFile = data_path + "signature/SAML1Response.xml";
35 SAMLSignatureTestBase::setUp();
39 SAMLSignatureTestBase::tearDown();
42 void testSignature() {
43 auto_ptr_XMLCh issuer("issuer");
44 auto_ptr_XMLCh issueInstant("1970-01-02T01:01:02.100Z");
45 auto_ptr_XMLCh aid("aident");
46 auto_ptr_XMLCh rid("rident");
47 auto_ptr_XMLCh method("method");
48 auto_ptr_XMLCh nameid("John Doe");
50 NameIdentifier* n=NameIdentifierBuilder::buildNameIdentifier();
51 n->setName(nameid.get());
52 Subject* subject=SubjectBuilder::buildSubject();
53 subject->setNameIdentifier(n);
55 AuthenticationStatement* statement=AuthenticationStatementBuilder::buildAuthenticationStatement();
56 statement->setAuthenticationInstant(issueInstant.get());
57 statement->setAuthenticationMethod(method.get());
58 statement->setSubject(subject);
60 Assertion* assertion=AssertionBuilder::buildAssertion();
61 assertion->setAssertionID(aid.get());
62 assertion->setIssueInstant(issueInstant.get());
63 assertion->setIssuer(issuer.get());
64 assertion->getAuthenticationStatements().push_back(statement);
66 // Append a Signature.
67 assertion->setSignature(SignatureBuilder::buildSignature());
69 // Sign assertion while marshalling.
70 vector<Signature*> sigs(1,assertion->getSignature());
71 CredentialCriteria cc;
72 cc.setUsage(Credential::SIGNING_CREDENTIAL);
73 Locker locker(m_resolver);
74 const Credential* cred = m_resolver->resolve(&cc);
75 TSM_ASSERT("Retrieved credential was null", cred!=nullptr);
77 DOMElement* rootElement = nullptr;
79 rootElement=assertion->marshall((DOMDocument*)nullptr,&sigs,cred);
81 catch (XMLToolingException& e) {
87 StatusCode* sc=StatusCodeBuilder::buildStatusCode();
88 sc->setValue(&StatusCode::SUCCESS);
89 Status* status=StatusBuilder::buildStatus();
90 status->setStatusCode(sc);
91 sc = StatusCodeBuilder::buildStatusCode();
92 xmltooling::QName subcode("urn:mace:shibboleth", "NoReally", "shib");
93 sc->setValue(&subcode);
94 status->getStatusCode()->setStatusCode(sc);
96 auto_ptr<Response> response(ResponseBuilder::buildResponse());
97 response->setResponseID(rid.get());
98 response->setIssueInstant(issueInstant.get());
99 response->setStatus(status);
100 response->getAssertions().push_back(assertion);
101 response->setSignature(SignatureBuilder::buildSignature());
103 // Sign response while marshalling.
105 sigs.push_back(response->getSignature());
106 rootElement = nullptr;
108 rootElement=response->marshall((DOMDocument*)nullptr,&sigs,cred);
110 catch (XMLToolingException& e) {
116 XMLHelper::serialize(rootElement, buf);
117 istringstream in(buf);
118 DOMDocument* doc=XMLToolingConfig::getConfig().getParser().parse(in);
119 const XMLObjectBuilder* b = XMLObjectBuilder::getBuilder(doc->getDocumentElement());
121 auto_ptr<XMLObject> response2(b->buildFromDocument(doc));
122 assertEquals("Unmarshalled response does not match", expectedChildElementsDOM, response2.get(), false);
124 auto_ptr<Response> response3(dynamic_cast<Response*>(response2.get())->cloneResponse());
127 opensaml::SignatureProfileValidator spv;
128 spv.validate(dynamic_cast<Response*>(response3.get())->getAssertions().front()->getSignature());
129 spv.validate(dynamic_cast<Response*>(response3.get())->getSignature());
131 SignatureValidator sv(cred);
132 sv.validate(dynamic_cast<Response*>(response3.get())->getAssertions().front()->getSignature());
133 sv.validate(dynamic_cast<Response*>(response3.get())->getSignature());
135 catch (XMLToolingException& e) {