2 * Copyright 2001-2005 Internet2
\r
4 * Licensed under the Apache License, Version 2.0 (the "License");
\r
5 * you may not use this file except in compliance with the License.
\r
6 * You may obtain a copy of the License at
\r
8 * http://www.apache.org/licenses/LICENSE-2.0
\r
10 * Unless required by applicable law or agreed to in writing, software
\r
11 * distributed under the License is distributed on an "AS IS" BASIS,
\r
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
13 * See the License for the specific language governing permissions and
\r
14 * limitations under the License.
\r
17 #include "internal.h"
\r
18 #include <saml/saml1/core/Assertions.h>
\r
19 #include <saml/signature/SigningContext.h>
\r
20 #include <saml/signature/VerifyingContext.h>
\r
22 using namespace opensaml::saml1;
\r
25 #include <openssl/pem.h>
\r
26 #include <xercesc/util/XMLUniDefs.hpp>
\r
27 #include <xsec/enc/XSECKeyInfoResolverDefault.hpp>
\r
28 #include <xsec/enc/OpenSSL/OpenSSLCryptoX509.hpp>
\r
29 #include <xsec/enc/OpenSSL/OpenSSLCryptoKeyRSA.hpp>
\r
30 #include <xmltooling/signature/Signature.h>
\r
32 class TestContext : public VerifyingContext
\r
34 SigningContext* m_signing;
\r
35 vector<XSECCryptoX509*> m_certs;
\r
37 TestContext(const XMLCh* uri) : VerifyingContext(uri), m_signing(NULL) {
\r
38 OpenSSLCryptoKeyRSA* key=NULL;
\r
39 string keypath=data_path + "key.pem";
\r
40 BIO* in=BIO_new(BIO_s_file_internal());
\r
41 if (in && BIO_read_filename(in,keypath.c_str())>0) {
\r
42 EVP_PKEY* pkey=PEM_read_bio_PrivateKey(in, NULL, NULL, NULL);
\r
44 key=new OpenSSLCryptoKeyRSA(pkey);
\r
45 EVP_PKEY_free(pkey);
\r
48 if (in) BIO_free(in);
\r
49 TS_ASSERT(key!=NULL);
\r
51 string certpath=data_path + "cert.pem";
\r
52 in=BIO_new(BIO_s_file_internal());
\r
53 if (in && BIO_read_filename(in,certpath.c_str())>0) {
\r
55 while (x=PEM_read_bio_X509(in,NULL,NULL,NULL)) {
\r
56 m_certs.push_back(new OpenSSLCryptoX509(x));
\r
60 if (in) BIO_free(in);
\r
61 TS_ASSERT(m_certs.size()>0);
\r
62 m_signing=new SigningContext(uri, key, &m_certs);
\r
65 virtual ~TestContext() {
\r
67 for_each(m_certs.begin(),m_certs.end(),xmltooling::cleanup<XSECCryptoX509>());
\r
70 SigningContext* getSigningContext() { return m_signing; }
\r
72 void verifySignature(DSIGSignature* sig) const {
\r
73 VerifyingContext::verifySignature(sig);
\r
74 sig->setSigningKey(NULL);
\r
75 XSECKeyInfoResolverDefault resolver;
\r
76 sig->setKeyInfoResolver(&resolver);
\r
81 class SAML1AssertionTest : public CxxTest::TestSuite, public SAMLObjectBaseTestCase {
\r
84 childElementsFile = data_path + "signature/SAML1Assertion.xml";
\r
85 SAMLObjectBaseTestCase::setUp();
\r
89 SAMLObjectBaseTestCase::tearDown();
\r
92 void testSignature() {
\r
93 auto_ptr_XMLCh issuer("issuer");
\r
94 auto_ptr_XMLCh issueInstant("1970-01-02T01:01:02.100Z");
\r
95 auto_ptr_XMLCh id("ident");
\r
96 auto_ptr_XMLCh method("method");
\r
97 auto_ptr_XMLCh nameid("John Doe");
\r
99 NameIdentifier* n=NameIdentifierBuilder::buildNameIdentifier();
\r
100 n->setName(nameid.get());
\r
101 Subject* subject=SubjectBuilder::buildSubject();
\r
102 subject->setNameIdentifier(n);
\r
104 AuthenticationStatement* statement=AuthenticationStatementBuilder::buildAuthenticationStatement();
\r
105 statement->setAuthenticationInstant(issueInstant.get());
\r
106 statement->setAuthenticationMethod(method.get());
\r
107 statement->setSubject(subject);
\r
109 auto_ptr<Assertion> assertion(AssertionBuilder::buildAssertion());
\r
110 assertion->setAssertionID(id.get());
\r
111 assertion->setIssueInstant(issueInstant.get());
\r
112 assertion->setIssuer(issuer.get());
\r
113 assertion->getAuthenticationStatements().push_back(statement);
\r
115 // Append a Signature.
\r
116 xmlsignature::Signature* sig=xmlsignature::SignatureBuilder::newSignature();
\r
117 assertion->setSignature(sig);
\r
119 // Signing context for the assertion.
\r
120 TestContext tc(id.get());
\r
121 MarshallingContext mctx(sig,tc.getSigningContext());
\r
122 DOMElement* rootElement = assertion->marshall((DOMDocument*)NULL,&mctx);
\r
125 XMLHelper::serialize(rootElement, buf);
\r
126 istringstream in(buf);
\r
127 DOMDocument* doc=XMLToolingConfig::getConfig().getParser().parse(in);
\r
128 const XMLObjectBuilder* b = XMLObjectBuilder::getBuilder(doc->getDocumentElement());
\r
130 assertEquals(expectedChildElementsDOM, b->buildFromDocument(doc));
\r
133 assertion->getSignature()->verify(tc);
\r
135 catch (xmlsignature::SignatureException& e) {
\r
136 TS_TRACE(e.what());
\r