2 * Licensed to the University Corporation for Advanced Internet
3 * Development, Inc. (UCAID) under one or more contributor license
4 * agreements. See the NOTICE file distributed with this work for
5 * additional information regarding copyright ownership.
7 * UCAID licenses this file to you under the Apache License,
8 * Version 2.0 (the "License"); you may not use this file except
9 * in compliance with the License. You may obtain a copy of the
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
17 * either express or implied. See the License for the specific
18 * language governing permissions and limitations under the License.
21 #include "XMLObjectBaseTestCase.h"
23 #include <xmltooling/encryption/Decrypter.h>
24 #include <xmltooling/encryption/Encrypter.h>
25 #include <xmltooling/encryption/Encryption.h>
26 #include <xmltooling/security/Credential.h>
27 #include <xmltooling/security/CredentialCriteria.h>
28 #include <xmltooling/security/CredentialResolver.h>
31 #include <xercesc/util/XMLUniDefs.hpp>
32 #include <xsec/dsig/DSIGReference.hpp>
34 using namespace xmlencryption;
36 class EncryptionTest : public CxxTest::TestSuite {
37 CredentialResolver* m_resolver;
41 string config = data_path + "FilesystemCredentialResolver.xml";
42 ifstream in(config.c_str());
43 DOMDocument* doc=XMLToolingConfig::getConfig().getParser().parse(in);
44 XercesJanitor<DOMDocument> janitor(doc);
45 m_resolver = XMLToolingConfig::getConfig().CredentialResolverManager.newPlugin(
46 CHAINING_CREDENTIAL_RESOLVER,doc->getDocumentElement()
48 XMLObjectBuilder::registerDefaultBuilder(new UnknownElementBuilder());
52 XMLObjectBuilder::deregisterDefaultBuilder();
56 void testEncryption() {
57 string path=data_path + "ComplexXMLObject.xml";
58 ifstream fs(path.c_str());
59 DOMDocument* doc=XMLToolingConfig::getConfig().getParser().parse(fs);
60 TS_ASSERT(doc!=nullptr);
63 CredentialCriteria cc;
64 cc.setUsage(Credential::ENCRYPTION_CREDENTIAL);
65 Locker locker(m_resolver);
66 const Credential* cred=m_resolver->resolve(&cc);
67 TSM_ASSERT("Retrieved credential was null", cred!=nullptr);
70 Encrypter::EncryptionParams ep;
71 Encrypter::KeyEncryptionParams kep(*cred);
72 auto_ptr<EncryptedData> encData(encrypter.encryptElement(doc->getDocumentElement(),ep,&kep));
75 XMLHelper::serialize(encData->marshall(), buf);
76 //TS_TRACE(buf.c_str());
77 istringstream is(buf);
78 DOMDocument* doc2=XMLToolingConfig::getConfig().getValidatingParser().parse(is);
79 auto_ptr<EncryptedData> encData2(
80 dynamic_cast<EncryptedData*>(XMLObjectBuilder::buildOneFromElement(doc2->getDocumentElement(),true))
83 Decrypter decrypter(m_resolver);
84 DOMDocumentFragment* frag = decrypter.decryptData(*encData2.get());
85 XMLHelper::serialize(static_cast<DOMElement*>(frag->getFirstChild()), buf);
86 //TS_TRACE(buf.c_str());
87 TS_ASSERT(doc->getDocumentElement()->isEqualNode(frag->getFirstChild()));
91 catch (XMLToolingException& e) {