X-Git-Url: http://www.project-moonshot.org/gitweb/?a=blobdiff_plain;f=samltest%2Fsignature%2FSAML1RequestTest.h;h=6b63d1b891df1e2025c750a4424ebaa08e5424b1;hb=e8d75900802dfa84c06290f88e365fd355ce6881;hp=61ca7f961bb4b83c54b72b32cbea45131618f8a6;hpb=9bc387d0cb9ce0a9151d9c081fc9dd823bdd2094;p=shibboleth%2Fcpp-opensaml.git diff --git a/samltest/signature/SAML1RequestTest.h b/samltest/signature/SAML1RequestTest.h index 61ca7f9..6b63d1b 100644 --- a/samltest/signature/SAML1RequestTest.h +++ b/samltest/signature/SAML1RequestTest.h @@ -14,100 +14,25 @@ * limitations under the License. */ -#include "internal.h" -#include -#include +#include "signature/SAMLSignatureTestBase.h" -#include +#include +#include #include -#include -#include -#include -#include -#include -#include -#include +using namespace opensaml::saml1p; using namespace opensaml::saml1; -using namespace xmlsignature; - -class TestValidator : public Validator -{ -public: - TestValidator() {} - virtual ~TestValidator() {} - - Validator* clone() const { - return new TestValidator(); - } - - void validate(const XMLObject* xmlObject) const { - DSIGSignature* sig=dynamic_cast(xmlObject)->getXMLSignature(); - if (!sig) - throw SignatureException("Only a marshalled Signature object can be verified."); - XSECKeyInfoResolverDefault resolver; - sig->setKeyInfoResolver(&resolver); // It will clone the resolver for us. - try { - if (!sig->verify()) - throw SignatureException("Signature did not verify."); - } - catch(XSECException& e) { - auto_ptr_char temp(e.getMsg()); - throw SignatureException(string("Caught an XMLSecurity exception verifying signature: ") + temp.get()); - } - catch(XSECCryptoException& e) { - throw SignatureException(string("Caught an XMLSecurity exception verifying signature: ") + e.getMsg()); - } - } -}; - -class _addcert : public std::binary_function { -public: - void operator()(X509Data* bag, XSECCryptoX509* cert) const { - safeBuffer& buf=cert->getDEREncodingSB(); - X509Certificate* x=X509CertificateBuilder::buildX509Certificate(); - x->setValue(buf.sbStrToXMLCh()); - bag->getX509Certificates().push_back(x); - } -}; -class SAML1RequestTest : public CxxTest::TestSuite, public SAMLObjectBaseTestCase { - XSECCryptoKey* m_key; - vector m_certs; +class SAML1RequestTest : public CxxTest::TestSuite, public SAMLSignatureTestBase { public: void setUp() { childElementsFile = data_path + "signature/SAML1Request.xml"; - SAMLObjectBaseTestCase::setUp(); - string keypath=data_path + "key.pem"; - BIO* in=BIO_new(BIO_s_file_internal()); - if (in && BIO_read_filename(in,keypath.c_str())>0) { - EVP_PKEY* pkey=PEM_read_bio_PrivateKey(in, NULL, NULL, NULL); - if (pkey) { - m_key=new OpenSSLCryptoKeyRSA(pkey); - EVP_PKEY_free(pkey); - } - } - if (in) BIO_free(in); - TS_ASSERT(m_key!=NULL); - - string certpath=data_path + "cert.pem"; - in=BIO_new(BIO_s_file_internal()); - if (in && BIO_read_filename(in,certpath.c_str())>0) { - X509* x=NULL; - while (x=PEM_read_bio_X509(in,NULL,NULL,NULL)) { - m_certs.push_back(new OpenSSLCryptoX509(x)); - X509_free(x); - } - } - if (in) BIO_free(in); - TS_ASSERT(m_certs.size()>0); + SAMLSignatureTestBase::setUp(); } void tearDown() { - SAMLObjectBaseTestCase::tearDown(); - delete m_key; - for_each(m_certs.begin(),m_certs.end(),xmltooling::cleanup()); + SAMLSignatureTestBase::tearDown(); } void testSignature() { @@ -133,13 +58,14 @@ public: // Append a Signature. Signature* sig=SignatureBuilder::buildSignature(); request->setSignature(sig); - sig->setSigningKey(m_key->clone()); + Locker locker(m_resolver); + sig->setSigningKey(m_resolver->getKey()); // Build KeyInfo. KeyInfo* keyInfo=KeyInfoBuilder::buildKeyInfo(); X509Data* x509Data=X509DataBuilder::buildX509Data(); keyInfo->getX509Datas().push_back(x509Data); - for_each(m_certs.begin(),m_certs.end(),bind1st(_addcert(),x509Data)); + for_each(m_resolver->getCertificates().begin(),m_resolver->getCertificates().end(),bind1st(_addcert(),x509Data)); sig->setKeyInfo(keyInfo); // Sign while marshalling. @@ -159,12 +85,14 @@ public: DOMDocument* doc=XMLToolingConfig::getConfig().getParser().parse(in); const XMLObjectBuilder* b = XMLObjectBuilder::getBuilder(doc->getDocumentElement()); - assertEquals(expectedChildElementsDOM, b->buildFromDocument(doc)); + auto_ptr request2(b->buildFromDocument(doc)); + assertEquals("Unmarshalled request does not match", expectedChildElementsDOM, request2.get(), false); try { - request->getSignature()->registerValidator(new SignatureProfileValidator()); - request->getSignature()->registerValidator(new TestValidator()); - request->getSignature()->validate(true); + SignatureProfileValidator spv; + SignatureValidator sv(new KeyResolver(m_resolver->getKey())); + spv.validate(dynamic_cast(request2.get())->getSignature()); + sv.validate(dynamic_cast(request2.get())->getSignature()); } catch (XMLToolingException& e) { TS_TRACE(e.what());