xref: /trunk/main/xmlsecurity/qa/certext/SanCertExt.cxx (revision 1ecadb572e7010ff3b3382ad9bf179dbc6efadbb)
1 /*************************************************************************
2 *
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2000, 2010 Oracle and/or its affiliates.
6 *
7 * OpenOffice.org - a multi-platform office productivity suite
8 *
9 * This file is part of OpenOffice.org.
10 *
11 * OpenOffice.org is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License version 3
13 * only, as published by the Free Software Foundation.
14 *
15 * OpenOffice.org is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 * GNU Lesser General Public License version 3 for more details
19 * (a copy is included in the LICENSE file that accompanied this code).
20 *
21 * You should have received a copy of the GNU Lesser General Public License
22 * version 3 along with OpenOffice.org.  If not, see
23 * <http://www.openoffice.org/license.html>
24 * for a copy of the LGPLv3 License.
25 *
26 ************************************************************************/
27 
28 #include "precompiled_xmlsecurity.hxx"
29 #include "sal/config.h"
30 #include "test/officeconnection.hxx"
31 
32 #include <com/sun/star/security/XSanExtension.hpp>
33 #include <com/sun/star/security/ExtAltNameType.hpp>
34 #include <com/sun/star/xml/crypto/XSecurityEnvironment.hpp>
35 #include <com/sun/star/xml/crypto/XSEInitializer.hpp>
36 #include <com/sun/star/xml/crypto/XXMLSecurityContext.hpp>
37 #include <com/sun/star/security/XCertificate.hpp>
38 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
39 #include <com/sun/star/beans/NamedValue.hpp>
40 #include "com/sun/star/uno/XComponentContext.hpp"
41 #include "com/sun/star/uno/Reference.hxx"
42 
43 #include "cppuhelper/bootstrap.hxx"
44 #include "cppunit/TestAssert.h"
45 #include "cppunit/TestFixture.h"
46 #include "cppunit/extensions/HelperMacros.h"
47 #include "cppunit/plugin/TestPlugIn.h"
48 #include "sal/types.h"
49 #include "comphelper/sequence.hxx"
50 #include <rtl/ustring.hxx>
51 
52 #include <neon/ne_ssl.h>
53 
54 using namespace com::sun::star;
55 
56 #define OID_SUBJECT_ALTERNATIVE_NAME "2.5.29.17"
57 #define SEINITIALIZER_COMPONENT "com.sun.star.xml.crypto.SEInitializer"
58 
59 
60 namespace {
61 
62     class Test: public CppUnit::TestFixture {
63 
64     private:
65         static uno::Sequence< security::CertAltNameEntry > altNames;
66         static bool runOnce;
67 
68         uno::Reference< xml::crypto::XSecurityEnvironment > initUno();
69         void init();
70         rtl::OString getB64CertFromFile(const char filename[]);
71         test::OfficeConnection connection_;
72 
73     public:
74 
75         Test();
76 
77         ~Test();
78 
79         virtual void setUp();
80 
81         virtual void tearDown();
82 
83         void test_Others();
84 
85         void test_RFC822();
86 
87         void test_DNS();
88 
89         void test_Direcory();
90 
91         void test_URI();
92 
93         void test_IP();
94 
95         void test_RID();
96 
97         void test_EDI();
98 
99         void test_X400();
100 
101         CPPUNIT_TEST_SUITE(Test);
102         CPPUNIT_TEST(test_Others);
103         CPPUNIT_TEST(test_RFC822);
104         CPPUNIT_TEST(test_DNS);
105         CPPUNIT_TEST(test_Direcory);
106         CPPUNIT_TEST(test_URI);
107         CPPUNIT_TEST(test_IP);
108         CPPUNIT_TEST(test_RID);
109         CPPUNIT_TEST(test_EDI);
110         CPPUNIT_TEST(test_X400);
111         CPPUNIT_TEST_SUITE_END();
112     };
113 
114     uno::Sequence< security::CertAltNameEntry > Test::altNames;
115     bool Test::runOnce = false;
116 
117     CPPUNIT_TEST_SUITE_REGISTRATION(Test);
118 
119     Test::Test()
120     {
121         if (runOnce)
122             return;
123         runOnce = true;
124         connection_.setUp();
125         init();
126     }
127 
128     Test::~Test()
129     {
130         if (runOnce)
131         {
132             connection_.tearDown();
133             runOnce = false;
134         }
135     }
136 
137 
138     uno::Reference< xml::crypto::XSecurityEnvironment > Test::initUno()
139     {
140         uno::Reference< uno::XComponentContext > context(connection_.getComponentContext(), uno::UNO_QUERY_THROW);
141         uno::Reference< lang::XMultiServiceFactory > factory(context->getServiceManager(), uno::UNO_QUERY_THROW);
142         uno::Reference< xml::crypto::XSEInitializer > xSEInitializer(factory->createInstance(
143             rtl::OUString::createFromAscii( SEINITIALIZER_COMPONENT )), uno::UNO_QUERY_THROW);
144         uno::Reference< xml::crypto::XXMLSecurityContext > xSecurityContext(
145             xSEInitializer->createSecurityContext(rtl::OUString()));
146         return xSecurityContext->getSecurityEnvironment();
147     }
148 
149 
150     void Test::init()
151     {
152         uno::Reference< xml::crypto::XSecurityEnvironment > xSecurityEnv = initUno();
153         rtl::OString b64Cert(getB64CertFromFile("User_35_Root_11.crt"));
154         uno::Reference< security::XCertificate > xCert = xSecurityEnv->createCertificateFromAscii(
155             rtl::OStringToOUString( b64Cert, RTL_TEXTENCODING_ASCII_US ) );
156         uno::Sequence< uno::Reference< security::XCertificateExtension > > extensions = xCert->getExtensions();
157         for (sal_Int32 i = 0 ; i < extensions.getLength(); i++)
158         {
159             uno::Reference< security::XCertificateExtension >element = extensions[i];
160             rtl::OString aId ( (const sal_Char *)element->getExtensionId().getArray(), element->getExtensionId().getLength());
161             if (aId.equals(OID_SUBJECT_ALTERNATIVE_NAME))
162             {
163                 uno::Reference< security::XSanExtension > sanExtension ( element, uno::UNO_QUERY );
164                 altNames = sanExtension->getAlternativeNames();
165                 break;
166             }
167         }
168     }
169 
170     rtl::OString Test::getB64CertFromFile(const char filename[])
171     {
172         ne_ssl_certificate* cert = ne_ssl_cert_read(filename);
173         char* certExportB64 = ne_ssl_cert_export(cert);
174         rtl::OString certB64( certExportB64 );
175         return certB64;
176     }
177 
178 
179     void Test::setUp() {
180     }
181 
182     void Test::tearDown() {
183     }
184 
185     void Test::test_Others() {
186         CPPUNIT_ASSERT_ASSERTION_PASS( CPPUNIT_ASSERT( altNames.getLength() > 0 ) );
187         for(int n = 1; n < altNames.getLength(); n++)
188         {
189             if (altNames[n].Type ==  security::ExtAltNameType_OTHER_NAME)
190             {
191                 ::com::sun::star::beans::NamedValue otherNameProp;
192                 if (altNames[n].Value >>= otherNameProp)
193                 {
194                     CPPUNIT_ASSERT_EQUAL( rtl::OUString::createFromAscii("1.2.3.4"), otherNameProp.Name);
195                     uno::Sequence< sal_Int8 > ipAddress;
196                     otherNameProp.Value >>= ipAddress;
197                     CPPUNIT_ASSERT_ASSERTION_PASS( CPPUNIT_ASSERT( ipAddress.getLength() > 0 ) );
198                 }
199             }
200         }
201     }
202 
203     void Test::test_RFC822() {
204         CPPUNIT_ASSERT_ASSERTION_PASS( CPPUNIT_ASSERT( altNames.getLength() > 0 ) );
205         for(int n = 1; n < altNames.getLength(); n++)
206         {
207             if (altNames[n].Type ==  security::ExtAltNameType_RFC822_NAME)
208             {
209                 rtl::OUString value;
210                 altNames[n].Value >>= value;
211                 CPPUNIT_ASSERT_EQUAL( rtl::OUString::createFromAscii("my@other.address"), value);
212             }
213         }
214     }
215 
216     void Test::test_DNS() {
217         CPPUNIT_ASSERT_ASSERTION_PASS( CPPUNIT_ASSERT( altNames.getLength() > 0 ) );
218         for(int n = 1; n < altNames.getLength(); n++)
219         {
220             if (altNames[n].Type ==  security::ExtAltNameType_DNS_NAME)
221             {
222                 rtl::OUString value;
223                 altNames[n].Value >>= value;
224                 CPPUNIT_ASSERT_EQUAL( rtl::OUString::createFromAscii("alt.openoffice.org"), value);
225             }
226         }
227     }
228 
229     void Test::test_Direcory() {
230         // Not implemented
231     }
232 
233     void Test::test_URI() {
234         CPPUNIT_ASSERT_ASSERTION_PASS( CPPUNIT_ASSERT( altNames.getLength() > 0 ) );
235         for(int n = 1; n < altNames.getLength(); n++)
236         {
237             if (altNames[n].Type ==  security::ExtAltNameType_URL)
238             {
239                 rtl::OUString value;
240                 altNames[n].Value >>= value;
241                 CPPUNIT_ASSERT_EQUAL( rtl::OUString::createFromAscii("http://my.url.here/"), value);
242             }
243         }
244     }
245 
246     void Test::test_IP() {
247         CPPUNIT_ASSERT_ASSERTION_PASS( CPPUNIT_ASSERT( altNames.getLength() > 0 ) );
248         for(int n = 1; n < altNames.getLength(); n++)
249         {
250             if (altNames[n].Type ==  security::ExtAltNameType_IP_ADDRESS)
251             {
252                 uno::Sequence< sal_Int8 > ipAddress;
253                 altNames[n].Value >>= ipAddress;
254                 CPPUNIT_ASSERT_ASSERTION_PASS( CPPUNIT_ASSERT( ipAddress.getLength() > 0 ) );
255             }
256         }
257 
258     }
259 
260     void Test::test_RID() {
261         CPPUNIT_ASSERT_ASSERTION_PASS( CPPUNIT_ASSERT( altNames.getLength() > 0 ) );
262         for(int n = 1; n < altNames.getLength(); n++)
263         {
264             if (altNames[n].Type ==  security::ExtAltNameType_REGISTERED_ID)
265             {
266                 rtl::OUString value;
267                 altNames[n].Value >>= value;
268                 CPPUNIT_ASSERT( rtl::OUString::createFromAscii("1.2.3.4").equals(value));
269             }
270         }
271     }
272 
273     void Test::test_EDI() {
274         // Not implemented
275     }
276 
277     void Test::test_X400() {
278         // Not implemented
279     }
280 }
281 CPPUNIT_PLUGIN_IMPLEMENT();
282