2 * Copyright 2001-2007 Internet2
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include "XMLObjectBaseTestCase.h"
19 #include <xmltooling/security/SecurityHelper.h>
21 class SecurityHelperTest : public CxxTest::TestSuite {
22 vector<XSECCryptoX509*> certs;
24 SOAPTransport* getTransport(const char* url) {
25 SOAPTransport::Address addr("SecurityHelperTest", "spaces.internet2.edu", url);
26 string scheme(addr.m_endpoint, strchr(addr.m_endpoint,':') - addr.m_endpoint);
27 return XMLToolingConfig::getConfig().SOAPTransportManager.newPlugin(scheme.c_str(), addr);
34 for_each(certs.begin(), certs.end(), xmltooling::cleanup<XSECCryptoX509>());
37 void testKeysFromFiles() {
38 string pathname = data_path + "key.pem";
39 auto_ptr<XSECCryptoKey> key1(SecurityHelper::loadKeyFromFile(pathname.c_str()));
40 pathname = data_path + "key.der";
41 auto_ptr<XSECCryptoKey> key2(SecurityHelper::loadKeyFromFile(pathname.c_str()));
42 pathname = data_path + "test.pfx";
43 auto_ptr<XSECCryptoKey> key3(SecurityHelper::loadKeyFromFile(pathname.c_str(), NULL, "password"));
45 TSM_ASSERT("PEM/DER keys did not match", SecurityHelper::matches(key1.get(), key2.get()));
46 TSM_ASSERT("DER/PKCS12 keys did not match", SecurityHelper::matches(key2.get(), key3.get()));
48 pathname = data_path + "key2.pem";
49 auto_ptr<XSECCryptoKey> key4(SecurityHelper::loadKeyFromFile(pathname.c_str()));
50 TSM_ASSERT("Different keys matched", !SecurityHelper::matches(key3.get(), key4.get()));
53 void testKeysFromURLs() {
54 string pathname = data_path + "key.pem.bak";
55 auto_ptr<SOAPTransport> t1(getTransport("https://spaces.internet2.edu/download/attachments/5305/key.pem"));
56 auto_ptr<XSECCryptoKey> key1(SecurityHelper::loadKeyFromURL(*t1.get(), pathname.c_str()));
57 pathname = data_path + "key.der.bak";
58 auto_ptr<SOAPTransport> t2(getTransport("https://spaces.internet2.edu/download/attachments/5305/key.der"));
59 auto_ptr<XSECCryptoKey> key2(SecurityHelper::loadKeyFromURL(*t2.get(), pathname.c_str()));
60 pathname = data_path + "test.pfx.bak";
61 auto_ptr<SOAPTransport> t3(getTransport("https://spaces.internet2.edu/download/attachments/5305/test.pfx"));
62 auto_ptr<XSECCryptoKey> key3(SecurityHelper::loadKeyFromURL(*t3.get(), pathname.c_str(), NULL, "password"));
64 TSM_ASSERT("PEM/DER keys did not match", SecurityHelper::matches(key1.get(), key2.get()));
65 TSM_ASSERT("DER/PKCS12 keys did not match", SecurityHelper::matches(key2.get(), key3.get()));
68 void testCertificatesFromFiles() {
69 string pathname = data_path + "cert.pem";
70 SecurityHelper::loadCertificatesFromFile(certs, pathname.c_str());
71 pathname = data_path + "cert.der";
72 SecurityHelper::loadCertificatesFromFile(certs, pathname.c_str());
73 pathname = data_path + "test.pfx";
74 SecurityHelper::loadCertificatesFromFile(certs, pathname.c_str(), NULL, "password");
76 TSM_ASSERT_EQUALS("Wrong certificate count", certs.size(), 3);
78 auto_ptr<XSECCryptoKey> key1(certs[0]->clonePublicKey());
79 auto_ptr<XSECCryptoKey> key2(certs[0]->clonePublicKey());
80 auto_ptr<XSECCryptoKey> key3(certs[0]->clonePublicKey());
82 TSM_ASSERT("PEM/DER keys did not match", SecurityHelper::matches(key1.get(), key2.get()));
83 TSM_ASSERT("DER/PKCS12 keys did not match", SecurityHelper::matches(key2.get(), key3.get()));
85 for_each(certs.begin(), certs.end(), xmltooling::cleanup<XSECCryptoX509>());
89 void testCertificatesFromURLs() {
90 string pathname = data_path + "cert.pem.bak";
91 auto_ptr<SOAPTransport> t1(getTransport("https://spaces.internet2.edu/download/attachments/5305/cert.pem"));
92 SecurityHelper::loadCertificatesFromURL(certs, *t1.get(), pathname.c_str());
93 pathname = data_path + "cert.der.bak";
94 auto_ptr<SOAPTransport> t2(getTransport("https://spaces.internet2.edu/download/attachments/5305/cert.der"));
95 SecurityHelper::loadCertificatesFromURL(certs, *t2.get(), pathname.c_str());
96 pathname = data_path + "test.pfx.bak";
97 auto_ptr<SOAPTransport> t3(getTransport("https://spaces.internet2.edu/download/attachments/5305/test.pfx"));
98 SecurityHelper::loadCertificatesFromURL(certs, *t3.get(), pathname.c_str(), NULL, "password");
100 TSM_ASSERT_EQUALS("Wrong certificate count", certs.size(), 3);
102 auto_ptr<XSECCryptoKey> key1(certs[0]->clonePublicKey());
103 auto_ptr<XSECCryptoKey> key2(certs[0]->clonePublicKey());
104 auto_ptr<XSECCryptoKey> key3(certs[0]->clonePublicKey());
106 TSM_ASSERT("PEM/DER keys did not match", SecurityHelper::matches(key1.get(), key2.get()));
107 TSM_ASSERT("DER/PKCS12 keys did not match", SecurityHelper::matches(key2.get(), key3.get()));
109 for_each(certs.begin(), certs.end(), xmltooling::cleanup<XSECCryptoX509>());