6573d4c7c9872f67ce940e8be9e4fdf23213d9eb
[shibboleth/cpp-xmltooling.git] / xmltoolingtest / EncryptionTest.h
1 /*
2  *  Copyright 2001-2010 Internet2
3  * 
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
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 #include "XMLObjectBaseTestCase.h"
18
19 #include <xmltooling/encryption/Decrypter.h>
20 #include <xmltooling/encryption/Encrypter.h>
21 #include <xmltooling/encryption/Encryption.h>
22 #include <xmltooling/security/Credential.h>
23 #include <xmltooling/security/CredentialCriteria.h>
24 #include <xmltooling/security/CredentialResolver.h>
25
26 #include <fstream>
27 #include <xercesc/util/XMLUniDefs.hpp>
28 #include <xsec/dsig/DSIGReference.hpp>
29
30 using namespace xmlencryption;
31
32 class EncryptionTest : public CxxTest::TestSuite {
33     CredentialResolver* m_resolver;
34 public:
35     void setUp() {
36         m_resolver=nullptr;
37         string config = data_path + "FilesystemCredentialResolver.xml";
38         ifstream in(config.c_str());
39         DOMDocument* doc=XMLToolingConfig::getConfig().getParser().parse(in);
40         XercesJanitor<DOMDocument> janitor(doc);
41         m_resolver = XMLToolingConfig::getConfig().CredentialResolverManager.newPlugin(
42             FILESYSTEM_CREDENTIAL_RESOLVER,doc->getDocumentElement()
43             );
44         XMLObjectBuilder::registerDefaultBuilder(new UnknownElementBuilder());
45     }
46
47     void tearDown() {
48         XMLObjectBuilder::deregisterDefaultBuilder();
49         delete m_resolver;
50     }
51
52     void testEncryption() {
53         string path=data_path + "ComplexXMLObject.xml";
54         ifstream fs(path.c_str());
55         DOMDocument* doc=XMLToolingConfig::getConfig().getParser().parse(fs);
56         TS_ASSERT(doc!=nullptr);
57
58         try {
59             CredentialCriteria cc;
60             cc.setUsage(Credential::ENCRYPTION_CREDENTIAL);
61             Locker locker(m_resolver);
62             const Credential* cred=m_resolver->resolve(&cc);
63             TSM_ASSERT("Retrieved credential was null", cred!=nullptr);
64
65             Encrypter encrypter;
66             Encrypter::EncryptionParams ep;
67             Encrypter::KeyEncryptionParams kep(*cred);
68             auto_ptr<EncryptedData> encData(encrypter.encryptElement(doc->getDocumentElement(),ep,&kep));
69
70             string buf;
71             XMLHelper::serialize(encData->marshall(), buf);
72             //TS_TRACE(buf.c_str());
73             istringstream is(buf);
74             DOMDocument* doc2=XMLToolingConfig::getConfig().getValidatingParser().parse(is);
75             auto_ptr<EncryptedData> encData2(
76                 dynamic_cast<EncryptedData*>(XMLObjectBuilder::buildOneFromElement(doc2->getDocumentElement(),true))
77                 );
78
79             Decrypter decrypter(m_resolver);
80             DOMDocumentFragment* frag = decrypter.decryptData(*encData2.get());
81             XMLHelper::serialize(static_cast<DOMElement*>(frag->getFirstChild()), buf);
82             //TS_TRACE(buf.c_str());
83             TS_ASSERT(doc->getDocumentElement()->isEqualNode(frag->getFirstChild()));
84             frag->release();
85             doc->release();
86         }
87         catch (XMLToolingException& e) {
88             TS_TRACE(e.what());
89             doc->release();
90             throw;
91         }
92     }
93
94 };