Revised decryption APIs to clarify CredentialResolver/KeyResolver difference.
[shibboleth/cpp-xmltooling.git] / xmltoolingtest / EncryptionTest.h
1 /*
2  *  Copyright 2001-2007 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/security/CredentialResolver.h>
22
23 #include <fstream>
24 #include <xercesc/util/XMLUniDefs.hpp>
25 #include <xsec/dsig/DSIGReference.hpp>
26
27 using namespace xmlencryption;
28
29 class _addcert : public std::binary_function<X509Data*,XSECCryptoX509*,void> {
30 public:
31     void operator()(X509Data* bag, XSECCryptoX509* cert) const {
32         safeBuffer& buf=cert->getDEREncodingSB();
33         X509Certificate* x=X509CertificateBuilder::buildX509Certificate();
34         x->setValue(buf.sbStrToXMLCh());
35         bag->getX509Certificates().push_back(x);
36     }
37 };
38
39 class EncryptionTest : public CxxTest::TestSuite {
40     CredentialResolver* m_resolver;
41 public:
42     void setUp() {
43         m_resolver=NULL;
44         string config = data_path + "FilesystemCredentialResolver.xml";
45         ifstream in(config.c_str());
46         DOMDocument* doc=XMLToolingConfig::getConfig().getParser().parse(in);
47         XercesJanitor<DOMDocument> janitor(doc);
48         m_resolver = XMLToolingConfig::getConfig().CredentialResolverManager.newPlugin(
49             FILESYSTEM_CREDENTIAL_RESOLVER,doc->getDocumentElement()
50             );
51     }
52
53     void tearDown() {
54         delete m_resolver;
55     }
56
57     void testEncryption() {
58         string path=data_path + "ComplexXMLObject.xml";
59         ifstream fs(path.c_str());
60         DOMDocument* doc=XMLToolingConfig::getConfig().getParser().parse(fs);
61         TS_ASSERT(doc!=NULL);
62
63         try {
64             Locker locker(m_resolver);
65             Encrypter encrypter;
66             Encrypter::EncryptionParams ep;
67             Encrypter::KeyEncryptionParams kep(DSIGConstants::s_unicodeStrURIRSA_1_5,m_resolver->getKey());
68             auto_ptr<EncryptedData> encData(encrypter.encryptElement(doc->getDocumentElement(),ep,&kep));
69
70             string buf;
71             XMLHelper::serialize(encData->marshall(), buf);
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))
76                 );
77
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()));
83             frag->release();
84             doc->release();
85         }
86         catch (XMLToolingException& e) {
87             TS_TRACE(e.what());
88             doc->release();
89             throw;
90         }
91     }
92
93 };