2 * Copyright 2001-2009 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>());
38 void testKeysFromFiles() {
39 string pathname = data_path + "key.pem";
40 auto_ptr<XSECCryptoKey> key1(SecurityHelper::loadKeyFromFile(pathname.c_str()));
41 pathname = data_path + "key.der";
42 auto_ptr<XSECCryptoKey> key2(SecurityHelper::loadKeyFromFile(pathname.c_str()));
43 pathname = data_path + "test.pfx";
44 auto_ptr<XSECCryptoKey> key3(SecurityHelper::loadKeyFromFile(pathname.c_str(), NULL, "password"));
46 TSM_ASSERT("PEM/DER keys did not match", SecurityHelper::matches(*key1.get(), *key2.get()));
47 TSM_ASSERT("DER/PKCS12 keys did not match", SecurityHelper::matches(*key2.get(), *key3.get()));
49 pathname = data_path + "key2.pem";
50 auto_ptr<XSECCryptoKey> key4(SecurityHelper::loadKeyFromFile(pathname.c_str()));
51 TSM_ASSERT("Different keys matched", !SecurityHelper::matches(*key3.get(), *key4.get()));
54 void testKeysFromURLs() {
55 string pathname = data_path + "key.pem.bak";
56 auto_ptr<SOAPTransport> t1(getTransport("https://spaces.internet2.edu/download/attachments/5305/key.pem"));
57 auto_ptr<XSECCryptoKey> key1(SecurityHelper::loadKeyFromURL(*t1.get(), pathname.c_str()));
58 pathname = data_path + "key.der.bak";
59 auto_ptr<SOAPTransport> t2(getTransport("https://spaces.internet2.edu/download/attachments/5305/key.der"));
60 auto_ptr<XSECCryptoKey> key2(SecurityHelper::loadKeyFromURL(*t2.get(), pathname.c_str()));
61 pathname = data_path + "test.pfx.bak";
62 auto_ptr<SOAPTransport> t3(getTransport("https://spaces.internet2.edu/download/attachments/5305/test.pfx"));
63 auto_ptr<XSECCryptoKey> key3(SecurityHelper::loadKeyFromURL(*t3.get(), pathname.c_str(), NULL, "password"));
65 TSM_ASSERT("PEM/DER keys did not match", SecurityHelper::matches(*key1.get(), *key2.get()));
66 TSM_ASSERT("DER/PKCS12 keys did not match", SecurityHelper::matches(*key2.get(), *key3.get()));
69 void testCertificatesFromFiles() {
70 string pathname = data_path + "cert.pem";
71 SecurityHelper::loadCertificatesFromFile(certs, pathname.c_str());
72 pathname = data_path + "cert.der";
73 SecurityHelper::loadCertificatesFromFile(certs, pathname.c_str());
74 pathname = data_path + "test.pfx";
75 SecurityHelper::loadCertificatesFromFile(certs, pathname.c_str(), NULL, "password");
77 TSM_ASSERT_EQUALS("Wrong certificate count", certs.size(), 3);
79 auto_ptr<XSECCryptoKey> key1(certs[0]->clonePublicKey());
80 auto_ptr<XSECCryptoKey> key2(certs[1]->clonePublicKey());
81 auto_ptr<XSECCryptoKey> key3(certs[2]->clonePublicKey());
83 TSM_ASSERT("PEM/DER keys did not match", SecurityHelper::matches(*key1.get(), *key2.get()));
84 TSM_ASSERT("DER/PKCS12 keys did not match", SecurityHelper::matches(*key2.get(), *key3.get()));
87 "Certificate and its key produced different DER encodings",
88 SecurityHelper::getDEREncoding(*certs[2]), SecurityHelper::getDEREncoding(*key1.get())
92 "Certificate and its key produced different hashed encodings",
93 SecurityHelper::getDEREncoding(*certs[2], true), SecurityHelper::getDEREncoding(*key1.get(), true)
96 for_each(certs.begin(), certs.end(), xmltooling::cleanup<XSECCryptoX509>());
100 void testCertificatesFromURLs() {
101 string pathname = data_path + "cert.pem.bak";
102 auto_ptr<SOAPTransport> t1(getTransport("https://spaces.internet2.edu/download/attachments/5305/cert.pem"));
103 SecurityHelper::loadCertificatesFromURL(certs, *t1.get(), pathname.c_str());
104 pathname = data_path + "cert.der.bak";
105 auto_ptr<SOAPTransport> t2(getTransport("https://spaces.internet2.edu/download/attachments/5305/cert.der"));
106 SecurityHelper::loadCertificatesFromURL(certs, *t2.get(), pathname.c_str());
107 pathname = data_path + "test.pfx.bak";
108 auto_ptr<SOAPTransport> t3(getTransport("https://spaces.internet2.edu/download/attachments/5305/test.pfx"));
109 SecurityHelper::loadCertificatesFromURL(certs, *t3.get(), pathname.c_str(), NULL, "password");
111 TSM_ASSERT_EQUALS("Wrong certificate count", certs.size(), 3);
113 auto_ptr<XSECCryptoKey> key1(certs[0]->clonePublicKey());
114 auto_ptr<XSECCryptoKey> key2(certs[0]->clonePublicKey());
115 auto_ptr<XSECCryptoKey> key3(certs[0]->clonePublicKey());
117 TSM_ASSERT("PEM/DER keys did not match", SecurityHelper::matches(*key1.get(), *key2.get()));
118 TSM_ASSERT("DER/PKCS12 keys did not match", SecurityHelper::matches(*key2.get(), *key3.get()));
120 for_each(certs.begin(), certs.end(), xmltooling::cleanup<XSECCryptoX509>());