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 "XMLObjectBaseTestCase.h"
19 #include <xmltooling/encryption/Decrypter.h>
20 #include <xmltooling/encryption/Encrypter.h>
21 #include <xmltooling/security/Credential.h>
22 #include <xmltooling/security/CredentialCriteria.h>
23 #include <xmltooling/security/CredentialResolver.h>
26 #include <xercesc/util/XMLUniDefs.hpp>
27 #include <xsec/dsig/DSIGReference.hpp>
29 using namespace xmlencryption;
31 class EncryptionTest : public CxxTest::TestSuite {
32 CredentialResolver* m_resolver;
36 string config = data_path + "FilesystemCredentialResolver.xml";
37 ifstream in(config.c_str());
38 DOMDocument* doc=XMLToolingConfig::getConfig().getParser().parse(in);
39 XercesJanitor<DOMDocument> janitor(doc);
40 m_resolver = XMLToolingConfig::getConfig().CredentialResolverManager.newPlugin(
41 FILESYSTEM_CREDENTIAL_RESOLVER,doc->getDocumentElement()
43 XMLObjectBuilder::registerDefaultBuilder(new UnknownElementBuilder());
47 XMLObjectBuilder::deregisterDefaultBuilder();
51 void testEncryption() {
52 string path=data_path + "ComplexXMLObject.xml";
53 ifstream fs(path.c_str());
54 DOMDocument* doc=XMLToolingConfig::getConfig().getParser().parse(fs);
58 CredentialCriteria cc;
59 cc.setUsage(CredentialCriteria::ENCRYPTION_CREDENTIAL);
60 Locker locker(m_resolver);
61 const Credential* cred=m_resolver->resolve(&cc);
62 TSM_ASSERT("Retrieved credential was null", cred!=NULL);
65 Encrypter::EncryptionParams ep;
66 Encrypter::KeyEncryptionParams kep(*cred);
67 auto_ptr<EncryptedData> encData(encrypter.encryptElement(doc->getDocumentElement(),ep,&kep));
70 XMLHelper::serialize(encData->marshall(), buf);
71 //TS_TRACE(buf.c_str());
72 istringstream is(buf);
73 DOMDocument* doc2=XMLToolingConfig::getConfig().getValidatingParser().parse(is);
74 auto_ptr<EncryptedData> encData2(
75 dynamic_cast<EncryptedData*>(XMLObjectBuilder::buildOneFromElement(doc2->getDocumentElement(),true))
78 Decrypter decrypter(m_resolver);
79 DOMDocumentFragment* frag = decrypter.decryptData(*encData2.get());
80 XMLHelper::serialize(static_cast<DOMElement*>(frag->getFirstChild()), buf);
81 //TS_TRACE(buf.c_str());
82 TS_ASSERT(doc->getDocumentElement()->isEqualNode(frag->getFirstChild()));
86 catch (XMLToolingException& e) {