Add hashing options to key extraction support.
[shibboleth/cpp-xmltooling.git] / xmltoolingtest / SecurityHelperTest.h
1 /*
2  *  Copyright 2001-2009 Internet2
3  *
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
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 #include "XMLObjectBaseTestCase.h"
18
19 #include <xmltooling/security/SecurityHelper.h>
20
21 class SecurityHelperTest : public CxxTest::TestSuite {
22     vector<XSECCryptoX509*> certs;
23
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);
28     }
29 public:
30     void setUp() {
31     }
32
33     void tearDown() {
34         for_each(certs.begin(), certs.end(), xmltooling::cleanup<XSECCryptoX509>());
35         certs.clear();
36     }
37
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"));
45
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()));
48
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()));
52     }
53
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"));
64
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()));
67     }
68
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");
76
77         TSM_ASSERT_EQUALS("Wrong certificate count", certs.size(), 3);
78
79         auto_ptr<XSECCryptoKey> key1(certs[0]->clonePublicKey());
80         auto_ptr<XSECCryptoKey> key2(certs[1]->clonePublicKey());
81         auto_ptr<XSECCryptoKey> key3(certs[2]->clonePublicKey());
82
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()));
85
86         TSM_ASSERT_EQUALS(
87             "Certificate and its key produced different DER encodings",
88             SecurityHelper::getDEREncoding(*certs[2]), SecurityHelper::getDEREncoding(*key1.get())
89             );
90
91         TSM_ASSERT_EQUALS(
92             "Certificate and its key produced different hashed encodings",
93             SecurityHelper::getDEREncoding(*certs[2], true), SecurityHelper::getDEREncoding(*key1.get(), true)
94             );
95
96         for_each(certs.begin(), certs.end(), xmltooling::cleanup<XSECCryptoX509>());
97         certs.clear();
98     }
99
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");
110
111         TSM_ASSERT_EQUALS("Wrong certificate count", certs.size(), 3);
112
113         auto_ptr<XSECCryptoKey> key1(certs[0]->clonePublicKey());
114         auto_ptr<XSECCryptoKey> key2(certs[0]->clonePublicKey());
115         auto_ptr<XSECCryptoKey> key3(certs[0]->clonePublicKey());
116
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()));
119
120         for_each(certs.begin(), certs.end(), xmltooling::cleanup<XSECCryptoX509>());
121         certs.clear();
122     }
123 };