1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 #include "precompiled_xmlsecurity.hxx"
25 #include "sal/config.h"
26 #include "test/officeconnection.hxx"
27 
28 #include <com/sun/star/security/XSanExtension.hpp>
29 #include <com/sun/star/security/ExtAltNameType.hpp>
30 #include <com/sun/star/xml/crypto/XSecurityEnvironment.hpp>
31 #include <com/sun/star/xml/crypto/XSEInitializer.hpp>
32 #include <com/sun/star/xml/crypto/XXMLSecurityContext.hpp>
33 #include <com/sun/star/security/XCertificate.hpp>
34 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
35 #include <com/sun/star/beans/NamedValue.hpp>
36 #include "com/sun/star/uno/XComponentContext.hpp"
37 #include "com/sun/star/uno/Reference.hxx"
38 
39 #include "cppuhelper/bootstrap.hxx"
40 #include "cppunit/TestAssert.h"
41 #include "cppunit/TestFixture.h"
42 #include "cppunit/extensions/HelperMacros.h"
43 #include "cppunit/plugin/TestPlugIn.h"
44 #include "sal/types.h"
45 #include "comphelper/sequence.hxx"
46 #include <rtl/ustring.hxx>
47 
48 #include <neon/ne_ssl.h>
49 
50 using namespace com::sun::star;
51 
52 #define OID_SUBJECT_ALTERNATIVE_NAME "2.5.29.17"
53 #define SEINITIALIZER_COMPONENT "com.sun.star.xml.crypto.SEInitializer"
54 
55 
56 namespace {
57 
58 	class Test: public CppUnit::TestFixture {
59 
60 	private:
61 		static uno::Sequence< security::CertAltNameEntry > altNames;
62 		static bool runOnce;
63 
64 		uno::Reference< xml::crypto::XSecurityEnvironment > initUno();
65 		void init();
66 		rtl::OString getB64CertFromFile(const char filename[]);
67 		test::OfficeConnection connection_;
68 
69 	public:
70 
71 		Test();
72 
73 		~Test();
74 
75 		virtual void setUp();
76 
77 		virtual void tearDown();
78 
79 		void test_Others();
80 
81 		void test_RFC822();
82 
83 		void test_DNS();
84 
85 		void test_Direcory();
86 
87 		void test_URI();
88 
89 		void test_IP();
90 
91 		void test_RID();
92 
93 		void test_EDI();
94 
95 		void test_X400();
96 
97 		CPPUNIT_TEST_SUITE(Test);
98 		CPPUNIT_TEST(test_Others);
99 		CPPUNIT_TEST(test_RFC822);
100 		CPPUNIT_TEST(test_DNS);
101 		CPPUNIT_TEST(test_Direcory);
102 		CPPUNIT_TEST(test_URI);
103 		CPPUNIT_TEST(test_IP);
104 		CPPUNIT_TEST(test_RID);
105 		CPPUNIT_TEST(test_EDI);
106 		CPPUNIT_TEST(test_X400);
107 		CPPUNIT_TEST_SUITE_END();
108 	};
109 
110 	uno::Sequence< security::CertAltNameEntry > Test::altNames;
111 	bool Test::runOnce = false;
112 
113 	CPPUNIT_TEST_SUITE_REGISTRATION(Test);
114 
115 	Test::Test()
116 	{
117 		if (runOnce)
118 			return;
119 		runOnce = true;
120 		connection_.setUp();
121 		init();
122 	}
123 
124 	Test::~Test()
125 	{
126 		if (runOnce)
127 		{
128 			connection_.tearDown();
129 			runOnce = false;
130 		}
131 	}
132 
133 
134 	uno::Reference< xml::crypto::XSecurityEnvironment > Test::initUno()
135 	{
136 		uno::Reference< uno::XComponentContext > context(connection_.getComponentContext(), uno::UNO_QUERY_THROW);
137 		uno::Reference< lang::XMultiServiceFactory > factory(context->getServiceManager(), uno::UNO_QUERY_THROW);
138 		uno::Reference< xml::crypto::XSEInitializer > xSEInitializer(factory->createInstance(
139 			rtl::OUString::createFromAscii( SEINITIALIZER_COMPONENT )),	uno::UNO_QUERY_THROW);
140 		uno::Reference< xml::crypto::XXMLSecurityContext > xSecurityContext(
141 			xSEInitializer->createSecurityContext(rtl::OUString()));
142 		return xSecurityContext->getSecurityEnvironment();
143 	}
144 
145 
146 	void Test::init()
147 	{
148 		uno::Reference< xml::crypto::XSecurityEnvironment > xSecurityEnv = initUno();
149 		rtl::OString b64Cert(getB64CertFromFile("User_35_Root_11.crt"));
150 		uno::Reference< security::XCertificate > xCert = xSecurityEnv->createCertificateFromAscii(
151 			rtl::OStringToOUString( b64Cert, RTL_TEXTENCODING_ASCII_US ) );
152 		uno::Sequence< uno::Reference< security::XCertificateExtension > > extensions = xCert->getExtensions();
153 		for (sal_Int32 i = 0 ; i < extensions.getLength(); i++)
154 		{
155 			uno::Reference< security::XCertificateExtension >element = extensions[i];
156 			rtl::OString aId ( (const sal_Char *)element->getExtensionId().getArray(), element->getExtensionId().getLength());
157 			if (aId.equals(OID_SUBJECT_ALTERNATIVE_NAME))
158 			{
159 				uno::Reference< security::XSanExtension > sanExtension ( element, uno::UNO_QUERY );
160 				altNames = sanExtension->getAlternativeNames();
161 				break;
162 			}
163 		}
164 	}
165 
166 	rtl::OString Test::getB64CertFromFile(const char filename[])
167 	{
168 		ne_ssl_certificate* cert = ne_ssl_cert_read(filename);
169 		char* certExportB64 = ne_ssl_cert_export(cert);
170 		rtl::OString certB64( certExportB64 );
171 		return certB64;
172 	}
173 
174 
175 	void Test::setUp() {
176 	}
177 
178 	void Test::tearDown() {
179 	}
180 
181 	void Test::test_Others() {
182 		CPPUNIT_ASSERT_ASSERTION_PASS( CPPUNIT_ASSERT( altNames.getLength() > 0 ) );
183 		for(int n = 1; n < altNames.getLength(); n++)
184 		{
185 			if (altNames[n].Type ==  security::ExtAltNameType_OTHER_NAME)
186 			{
187 				::com::sun::star::beans::NamedValue otherNameProp;
188 				if (altNames[n].Value >>= otherNameProp)
189 				{
190 					CPPUNIT_ASSERT_EQUAL( rtl::OUString::createFromAscii("1.2.3.4"), otherNameProp.Name);
191 					uno::Sequence< sal_Int8 > ipAddress;
192 					otherNameProp.Value >>= ipAddress;
193 					CPPUNIT_ASSERT_ASSERTION_PASS( CPPUNIT_ASSERT( ipAddress.getLength() > 0 ) );
194 				}
195 			}
196 		}
197 	}
198 
199 	void Test::test_RFC822() {
200 		CPPUNIT_ASSERT_ASSERTION_PASS( CPPUNIT_ASSERT( altNames.getLength() > 0 ) );
201 		for(int n = 1; n < altNames.getLength(); n++)
202 		{
203 			if (altNames[n].Type ==  security::ExtAltNameType_RFC822_NAME)
204 			{
205 				rtl::OUString value;
206 				altNames[n].Value >>= value;
207 				CPPUNIT_ASSERT_EQUAL( rtl::OUString::createFromAscii("my@other.address"), value);
208 			}
209 		}
210 	}
211 
212 	void Test::test_DNS() {
213 		CPPUNIT_ASSERT_ASSERTION_PASS( CPPUNIT_ASSERT( altNames.getLength() > 0 ) );
214 		for(int n = 1; n < altNames.getLength(); n++)
215 		{
216 			if (altNames[n].Type ==  security::ExtAltNameType_DNS_NAME)
217 			{
218 				rtl::OUString value;
219 				altNames[n].Value >>= value;
220 				CPPUNIT_ASSERT_EQUAL( rtl::OUString::createFromAscii("alt.openoffice.org"), value);
221 			}
222 		}
223 	}
224 
225 	void Test::test_Direcory() {
226 		// Not implemented
227 	}
228 
229 	void Test::test_URI() {
230 		CPPUNIT_ASSERT_ASSERTION_PASS( CPPUNIT_ASSERT( altNames.getLength() > 0 ) );
231 		for(int n = 1; n < altNames.getLength(); n++)
232 		{
233 			if (altNames[n].Type ==  security::ExtAltNameType_URL)
234 			{
235 				rtl::OUString value;
236 				altNames[n].Value >>= value;
237 				CPPUNIT_ASSERT_EQUAL( rtl::OUString::createFromAscii("http://my.url.here/"), value);
238 			}
239 		}
240 	}
241 
242 	void Test::test_IP() {
243 		CPPUNIT_ASSERT_ASSERTION_PASS( CPPUNIT_ASSERT( altNames.getLength() > 0 ) );
244 		for(int n = 1; n < altNames.getLength(); n++)
245 		{
246 			if (altNames[n].Type ==  security::ExtAltNameType_IP_ADDRESS)
247 			{
248 				uno::Sequence< sal_Int8 > ipAddress;
249 				altNames[n].Value >>= ipAddress;
250 				CPPUNIT_ASSERT_ASSERTION_PASS( CPPUNIT_ASSERT( ipAddress.getLength() > 0 ) );
251 			}
252 		}
253 
254 	}
255 
256 	void Test::test_RID() {
257 		CPPUNIT_ASSERT_ASSERTION_PASS( CPPUNIT_ASSERT( altNames.getLength() > 0 ) );
258 		for(int n = 1; n < altNames.getLength(); n++)
259 		{
260 			if (altNames[n].Type ==  security::ExtAltNameType_REGISTERED_ID)
261 			{
262 				rtl::OUString value;
263 				altNames[n].Value >>= value;
264 				CPPUNIT_ASSERT( rtl::OUString::createFromAscii("1.2.3.4").equals(value));
265 			}
266 		}
267 	}
268 
269 	void Test::test_EDI() {
270 		// Not implemented
271 	}
272 
273 	void Test::test_X400() {
274 		// Not implemented
275 	}
276 }
277 CPPUNIT_PLUGIN_IMPLEMENT();
278