2 * Copyright 2001-2005 Internet2
\r
4 * Licensed under the Apache License, Version 2.0 (the "License");
\r
5 * you may not use this file except in compliance with the License.
\r
6 * You may obtain a copy of the License at
\r
8 * http://www.apache.org/licenses/LICENSE-2.0
\r
10 * Unless required by applicable law or agreed to in writing, software
\r
11 * distributed under the License is distributed on an "AS IS" BASIS,
\r
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
13 * See the License for the specific language governing permissions and
\r
14 * limitations under the License.
\r
17 #include "XMLObjectBaseTestCase.h"
\r
19 #include <xmltooling/encryption/Decrypter.h>
\r
20 #include <xmltooling/encryption/Encrypter.h>
\r
21 #include <xmltooling/signature/CredentialResolver.h>
\r
24 #include <xercesc/util/XMLUniDefs.hpp>
\r
25 #include <xsec/dsig/DSIGReference.hpp>
\r
27 using namespace xmlencryption;
\r
29 class _addcert : public std::binary_function<X509Data*,XSECCryptoX509*,void> {
\r
31 void operator()(X509Data* bag, XSECCryptoX509* cert) const {
\r
32 safeBuffer& buf=cert->getDEREncodingSB();
\r
33 X509Certificate* x=X509CertificateBuilder::buildX509Certificate();
\r
34 x->setValue(buf.sbStrToXMLCh());
\r
35 bag->getX509Certificates().push_back(x);
\r
39 class EncryptionTest : public CxxTest::TestSuite {
\r
40 CredentialResolver* m_resolver;
\r
44 string config = data_path + "FilesystemCredentialResolver.xml";
\r
45 ifstream in(config.c_str());
\r
46 DOMDocument* doc=XMLToolingConfig::getConfig().getParser().parse(in);
\r
47 XercesJanitor<DOMDocument> janitor(doc);
\r
48 m_resolver = XMLToolingConfig::getConfig().CredentialResolverManager.newPlugin(
\r
49 FILESYSTEM_CREDENTIAL_RESOLVER,doc->getDocumentElement()
\r
57 void testEncryption() {
\r
58 string path=data_path + "ComplexXMLObject.xml";
\r
59 ifstream fs(path.c_str());
\r
60 DOMDocument* doc=XMLToolingConfig::getConfig().getParser().parse(fs);
\r
61 TS_ASSERT(doc!=NULL);
\r
64 Locker locker(m_resolver);
\r
65 Encrypter encrypter;
\r
66 Encrypter::EncryptionParams ep;
\r
67 Encrypter::KeyEncryptionParams kep(DSIGConstants::s_unicodeStrURIRSA_1_5,m_resolver->getKey());
\r
68 auto_ptr<EncryptedData> encData(encrypter.encryptElement(doc->getDocumentElement(),ep,&kep));
\r
71 XMLHelper::serialize(encData->marshall(), buf);
\r
72 istringstream is(buf);
\r
73 DOMDocument* doc2=XMLToolingConfig::getConfig().getValidatingParser().parse(is);
\r
74 auto_ptr<EncryptedData> encData2(
\r
75 dynamic_cast<EncryptedData*>(XMLObjectBuilder::buildOneFromElement(doc2->getDocumentElement(),true))
\r
78 Decrypter decrypter(new KeyResolver(m_resolver->getKey()));
\r
79 DOMDocumentFragment* frag = decrypter.decryptData(encData2.get());
\r
80 XMLHelper::serialize(static_cast<DOMElement*>(frag->getFirstChild()), buf);
\r
81 //TS_TRACE(buf.c_str());
\r
82 TS_ASSERT(doc->getDocumentElement()->isEqualNode(frag->getFirstChild()));
\r
86 catch (XMLToolingException& e) {
\r