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