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
23 #include <openssl/pem.h>
\r
24 #include <xercesc/util/XMLUniDefs.hpp>
\r
25 #include <xsec/dsig/DSIGReference.hpp>
\r
26 #include <xsec/enc/XSECKeyInfoResolverDefault.hpp>
\r
27 #include <xsec/enc/OpenSSL/OpenSSLCryptoX509.hpp>
\r
28 #include <xsec/enc/OpenSSL/OpenSSLCryptoKeyRSA.hpp>
\r
29 #include <xsec/enc/XSECCryptoException.hpp>
\r
30 #include <xsec/framework/XSECException.hpp>
\r
32 using namespace xmlencryption;
\r
34 class _addcert : public std::binary_function<X509Data*,XSECCryptoX509*,void> {
\r
36 void operator()(X509Data* bag, XSECCryptoX509* cert) const {
\r
37 safeBuffer& buf=cert->getDEREncodingSB();
\r
38 X509Certificate* x=X509CertificateBuilder::buildX509Certificate();
\r
39 x->setValue(buf.sbStrToXMLCh());
\r
40 bag->getX509Certificates().push_back(x);
\r
44 class EncryptionTest : public CxxTest::TestSuite {
\r
45 XSECCryptoKey* m_key;
\r
46 vector<XSECCryptoX509*> m_certs;
\r
49 string keypath=data_path + "key.pem";
\r
50 BIO* in=BIO_new(BIO_s_file_internal());
\r
51 if (in && BIO_read_filename(in,keypath.c_str())>0) {
\r
52 EVP_PKEY* pkey=PEM_read_bio_PrivateKey(in, NULL, NULL, NULL);
\r
54 m_key=new OpenSSLCryptoKeyRSA(pkey);
\r
55 EVP_PKEY_free(pkey);
\r
58 if (in) BIO_free(in);
\r
59 TS_ASSERT(m_key!=NULL);
\r
61 string certpath=data_path + "cert.pem";
\r
62 in=BIO_new(BIO_s_file_internal());
\r
63 if (in && BIO_read_filename(in,certpath.c_str())>0) {
\r
65 while (x=PEM_read_bio_X509(in,NULL,NULL,NULL)) {
\r
66 m_certs.push_back(new OpenSSLCryptoX509(x));
\r
70 if (in) BIO_free(in);
\r
71 TS_ASSERT(m_certs.size()>0);
\r
77 for_each(m_certs.begin(),m_certs.end(),xmltooling::cleanup<XSECCryptoX509>());
\r
81 TS_TRACE("testBasic");
\r
83 string path=data_path + "ComplexXMLObject.xml";
\r
84 ifstream fs(path.c_str());
\r
85 DOMDocument* doc=XMLToolingConfig::getConfig().getParser().parse(fs);
\r
86 TS_ASSERT(doc!=NULL);
\r
89 Encrypter encrypter;
\r
90 Encrypter::EncryptionParams ep;
\r
91 Encrypter::KeyEncryptionParams kep(DSIGConstants::s_unicodeStrURIRSA_1_5,m_key->clone());
\r
92 auto_ptr<EncryptedData> encData(encrypter.encryptElement(doc->getDocumentElement(),ep,&kep));
\r
95 XMLHelper::serialize(encData->marshall(), buf);
\r
96 istringstream is(buf);
\r
97 DOMDocument* doc2=XMLToolingConfig::getConfig().getValidatingParser().parse(is);
\r
98 auto_ptr<EncryptedData> encData2(
\r
99 dynamic_cast<EncryptedData*>(XMLObjectBuilder::buildOneFromElement(doc2->getDocumentElement(),true))
\r
102 Decrypter decrypter(new KeyResolver(m_key->clone()));
\r
103 DOMDocumentFragment* frag = decrypter.decryptData(encData2.get());
\r
104 XMLHelper::serialize(static_cast<DOMElement*>(frag->getFirstChild()), buf);
\r
105 TS_TRACE(buf.c_str());
\r
106 TS_ASSERT(doc->getDocumentElement()->isEqualNode(frag->getFirstChild()));
\r
110 catch (XMLToolingException& e) {
\r
111 TS_TRACE(e.what());
\r