New KeyResolver/Validator/Encrypter/Decrypter classes.
[shibboleth/cpp-xmltooling.git] / xmltoolingtest / EncryptionTest.h
1 /*\r
2  *  Copyright 2001-2005 Internet2\r
3  * \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
7  *\r
8  *     http://www.apache.org/licenses/LICENSE-2.0\r
9  *\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
15  */\r
16 \r
17 #include "XMLObjectBaseTestCase.h"\r
18 \r
19 #include <xmltooling/encryption/Decrypter.h>\r
20 #include <xmltooling/encryption/Encrypter.h>\r
21 \r
22 #include <fstream>\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
31 \r
32 using namespace xmlencryption;\r
33 \r
34 class _addcert : public std::binary_function<X509Data*,XSECCryptoX509*,void> {\r
35 public:\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
41     }\r
42 };\r
43 \r
44 class EncryptionTest : public CxxTest::TestSuite {\r
45     XSECCryptoKey* m_key;\r
46     vector<XSECCryptoX509*> m_certs;\r
47 public:\r
48     void setUp() {\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
53             if (pkey) {\r
54                 m_key=new OpenSSLCryptoKeyRSA(pkey);\r
55                 EVP_PKEY_free(pkey);\r
56             }\r
57         }\r
58         if (in) BIO_free(in);\r
59         TS_ASSERT(m_key!=NULL);\r
60 \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
64             X509* x=NULL;\r
65             while (x=PEM_read_bio_X509(in,NULL,NULL,NULL)) {\r
66                 m_certs.push_back(new OpenSSLCryptoX509(x));\r
67                 X509_free(x);\r
68             }\r
69         }\r
70         if (in) BIO_free(in);\r
71         TS_ASSERT(m_certs.size()>0);\r
72         \r
73     }\r
74 \r
75     void tearDown() {\r
76         delete m_key;\r
77         for_each(m_certs.begin(),m_certs.end(),xmltooling::cleanup<XSECCryptoX509>());\r
78     }\r
79 \r
80     void testBasic() {\r
81         TS_TRACE("testBasic");\r
82 \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
87 \r
88         try {\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
93 \r
94             string buf;\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
100                 );\r
101 \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
107             frag->release();\r
108             doc->release();\r
109         }\r
110         catch (XMLToolingException& e) {\r
111             TS_TRACE(e.what());\r
112             doc->release();\r
113             throw;\r
114         }\r
115     }\r
116 \r
117 };\r