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"
19 #include <saml/saml1/core/Assertions.h>
20 #include <saml/saml1/core/Protocols.h>
24 using namespace opensaml::saml1p;
25 using namespace opensaml::saml1;
27 class SAML1ResponseTest : public CxxTest::TestSuite, public SAMLSignatureTestBase {
30 childElementsFile = data_path + "signature/SAML1Response.xml";
31 SAMLSignatureTestBase::setUp();
35 SAMLSignatureTestBase::tearDown();
38 void testSignature() {
39 auto_ptr_XMLCh issuer("issuer");
40 auto_ptr_XMLCh issueInstant("1970-01-02T01:01:02.100Z");
41 auto_ptr_XMLCh aid("aident");
42 auto_ptr_XMLCh rid("rident");
43 auto_ptr_XMLCh method("method");
44 auto_ptr_XMLCh nameid("John Doe");
46 NameIdentifier* n=NameIdentifierBuilder::buildNameIdentifier();
47 n->setName(nameid.get());
48 Subject* subject=SubjectBuilder::buildSubject();
49 subject->setNameIdentifier(n);
51 AuthenticationStatement* statement=AuthenticationStatementBuilder::buildAuthenticationStatement();
52 statement->setAuthenticationInstant(issueInstant.get());
53 statement->setAuthenticationMethod(method.get());
54 statement->setSubject(subject);
56 Assertion* assertion=AssertionBuilder::buildAssertion();
57 assertion->setAssertionID(aid.get());
58 assertion->setIssueInstant(issueInstant.get());
59 assertion->setIssuer(issuer.get());
60 assertion->getAuthenticationStatements().push_back(statement);
62 // Append a Signature.
63 assertion->setSignature(SignatureBuilder::buildSignature());
64 Locker locker(m_resolver);
65 assertion->getSignature()->setSigningKey(m_resolver->getKey());
68 KeyInfo* keyInfo=KeyInfoBuilder::buildKeyInfo();
69 X509Data* x509Data=X509DataBuilder::buildX509Data();
70 keyInfo->getX509Datas().push_back(x509Data);
71 for_each(m_resolver->getCertificates().begin(),m_resolver->getCertificates().end(),bind1st(_addcert(),x509Data));
72 assertion->getSignature()->setKeyInfo(keyInfo);
74 // Sign assertion while marshalling.
75 vector<Signature*> sigs(1,assertion->getSignature());
76 DOMElement* rootElement = NULL;
78 rootElement=assertion->marshall((DOMDocument*)NULL,&sigs);
80 catch (XMLToolingException& e) {
86 StatusCode* sc=StatusCodeBuilder::buildStatusCode();
87 sc->setValue(&StatusCode::SUCCESS);
88 Status* status=StatusBuilder::buildStatus();
89 status->setStatusCode(sc);
91 auto_ptr<Response> response(ResponseBuilder::buildResponse());
92 response->setResponseID(rid.get());
93 response->setIssueInstant(issueInstant.get());
94 response->setStatus(status);
95 response->getAssertions().push_back(assertion);
96 response->setSignature(SignatureBuilder::buildSignature());
97 response->getSignature()->setSigningKey(m_resolver->getKey());
98 response->getSignature()->setKeyInfo(keyInfo->cloneKeyInfo());
100 // Sign response while marshalling.
102 sigs.push_back(response->getSignature());
105 rootElement=response->marshall((DOMDocument*)NULL,&sigs);
107 catch (XMLToolingException& e) {
113 XMLHelper::serialize(rootElement, buf);
114 istringstream in(buf);
115 DOMDocument* doc=XMLToolingConfig::getConfig().getParser().parse(in);
116 const XMLObjectBuilder* b = XMLObjectBuilder::getBuilder(doc->getDocumentElement());
118 auto_ptr<XMLObject> response2(b->buildFromDocument(doc));
119 assertEquals("Unmarshalled response does not match", expectedChildElementsDOM, response2.get(), false);
122 opensaml::SignatureProfileValidator spv;
123 spv.validate(dynamic_cast<Response*>(response2.get())->getAssertions().front()->getSignature());
124 spv.validate(dynamic_cast<Response*>(response2.get())->getSignature());
126 SignatureValidator sv(new KeyResolver(m_resolver->getKey()));
127 sv.validate(dynamic_cast<Response*>(response2.get())->getAssertions().front()->getSignature());
128 sv.validate(dynamic_cast<Response*>(response2.get())->getSignature());
130 catch (XMLToolingException& e) {