From: Scott Cantor Date: Wed, 26 Jul 2006 03:05:48 +0000 (+0000) Subject: Replace custom code with CredentialResolver. X-Git-Tag: 1.0-alpha1~205 X-Git-Url: http://www.project-moonshot.org/gitweb/?p=shibboleth%2Fcpp-xmltooling.git;a=commitdiff_plain;h=29ac236d46c15d0183af0fa084b703bd2e05d64c Replace custom code with CredentialResolver. --- diff --git a/xmltoolingtest/EncryptionTest.h b/xmltoolingtest/EncryptionTest.h index 154f8d0..3f88890 100644 --- a/xmltoolingtest/EncryptionTest.h +++ b/xmltoolingtest/EncryptionTest.h @@ -18,16 +18,11 @@ #include #include +#include #include -#include #include #include -#include -#include -#include -#include -#include using namespace xmlencryption; @@ -42,53 +37,34 @@ public: }; class EncryptionTest : public CxxTest::TestSuite { - XSECCryptoKey* m_key; - vector m_certs; + CredentialResolver* m_resolver; public: void 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); - + m_resolver=NULL; + string config = data_path + "FilesystemCredentialResolver.xml"; + ifstream in(config.c_str()); + DOMDocument* doc=XMLToolingConfig::getConfig().getParser().parse(in); + XercesJanitor janitor(doc); + m_resolver = XMLToolingConfig::getConfig().CredentialResolverManager.newPlugin( + FILESYSTEM_CREDENTIAL_RESOLVER,doc->getDocumentElement() + ); } void tearDown() { - delete m_key; - for_each(m_certs.begin(),m_certs.end(),xmltooling::cleanup()); + delete m_resolver; } - void testBasic() { - TS_TRACE("testBasic"); - + void testEncryption() { string path=data_path + "ComplexXMLObject.xml"; ifstream fs(path.c_str()); DOMDocument* doc=XMLToolingConfig::getConfig().getParser().parse(fs); TS_ASSERT(doc!=NULL); try { + Locker locker(m_resolver); Encrypter encrypter; Encrypter::EncryptionParams ep; - Encrypter::KeyEncryptionParams kep(DSIGConstants::s_unicodeStrURIRSA_1_5,m_key->clone()); + Encrypter::KeyEncryptionParams kep(DSIGConstants::s_unicodeStrURIRSA_1_5,m_resolver->getKey()); auto_ptr encData(encrypter.encryptElement(doc->getDocumentElement(),ep,&kep)); string buf; @@ -99,7 +75,7 @@ public: dynamic_cast(XMLObjectBuilder::buildOneFromElement(doc2->getDocumentElement(),true)) ); - Decrypter decrypter(new KeyResolver(m_key->clone())); + Decrypter decrypter(new KeyResolver(m_resolver->getKey())); DOMDocumentFragment* frag = decrypter.decryptData(encData2.get()); XMLHelper::serialize(static_cast(frag->getFirstChild()), buf); //TS_TRACE(buf.c_str()); diff --git a/xmltoolingtest/SignatureTest.h b/xmltoolingtest/SignatureTest.h index d52c0a2..8f3ec2c 100644 --- a/xmltoolingtest/SignatureTest.h +++ b/xmltoolingtest/SignatureTest.h @@ -16,17 +16,12 @@ #include "XMLObjectBaseTestCase.h" +#include #include #include -#include #include #include -#include -#include -#include -#include -#include class TestContext : public ContentReference { @@ -82,38 +77,22 @@ public: }; class SignatureTest : public CxxTest::TestSuite { - XSECCryptoKey* m_key; - vector m_certs; + CredentialResolver* m_resolver; public: void setUp() { + m_resolver=NULL; QName qname(SimpleXMLObject::NAMESPACE,SimpleXMLObject::LOCAL_NAME); QName qtype(SimpleXMLObject::NAMESPACE,SimpleXMLObject::TYPE_NAME); XMLObjectBuilder::registerBuilder(qname, new SimpleXMLObjectBuilder()); XMLObjectBuilder::registerBuilder(qtype, new SimpleXMLObjectBuilder()); - 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); - + string config = data_path + "FilesystemCredentialResolver.xml"; + ifstream in(config.c_str()); + DOMDocument* doc=XMLToolingConfig::getConfig().getParser().parse(in); + XercesJanitor janitor(doc); + m_resolver = XMLToolingConfig::getConfig().CredentialResolverManager.newPlugin( + FILESYSTEM_CREDENTIAL_RESOLVER,doc->getDocumentElement() + ); } void tearDown() { @@ -121,13 +100,10 @@ public: QName qtype(SimpleXMLObject::NAMESPACE,SimpleXMLObject::TYPE_NAME); XMLObjectBuilder::deregisterBuilder(qname); XMLObjectBuilder::deregisterBuilder(qtype); - delete m_key; - for_each(m_certs.begin(),m_certs.end(),xmltooling::cleanup()); + delete m_resolver; } void testSignature() { - TS_TRACE("testSignature"); - QName qname(SimpleXMLObject::NAMESPACE,SimpleXMLObject::LOCAL_NAME); const SimpleXMLObjectBuilder* b=dynamic_cast(XMLObjectBuilder::getBuilder(qname)); TS_ASSERT(b!=NULL); @@ -148,13 +124,15 @@ public: Signature* sig=SignatureBuilder::buildSignature(); sxObject->setSignature(sig); sig->setContentReference(new TestContext(&chNull)); - 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); // Signing context for the whole document. @@ -179,7 +157,7 @@ public: TS_ASSERT(sxObject2->getSignature()!=NULL); try { - TestValidator tv(&chNull,m_key->clone()); + TestValidator tv(&chNull,m_resolver->getKey()); tv.validate(sxObject2->getSignature()); } catch (XMLToolingException& e) { diff --git a/xmltoolingtest/xmltoolingtest.vcproj b/xmltoolingtest/xmltoolingtest.vcproj index 4502fe7..3bf88f1 100644 --- a/xmltoolingtest/xmltoolingtest.vcproj +++ b/xmltoolingtest/xmltoolingtest.vcproj @@ -62,7 +62,7 @@ /> @@ -304,7 +304,7 @@ >