xref: /trunk/main/xmlsecurity/qa/certext/SanCertExt.cxx (revision bcc22a4c08e1268a4f06e54fb146f88ef49f2cdc)
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 "gtest/gtest.h"
41 #include "sal/types.h"
42 #include "comphelper/sequence.hxx"
43 #include <rtl/ustring.hxx>
44 
45 #include <neon/ne_ssl.h>
46 
47 using namespace com::sun::star;
48 
49 #define OID_SUBJECT_ALTERNATIVE_NAME "2.5.29.17"
50 #define SEINITIALIZER_COMPONENT "com.sun.star.xml.crypto.SEInitializer"
51 
52 
53 namespace {
54 
55     class Test: public ::testing::Test {
56 
57     protected:
58         static uno::Sequence< security::CertAltNameEntry > altNames;
59         static bool runOnce;
60 
61         uno::Reference< xml::crypto::XSecurityEnvironment > initUno();
62         void init();
63         rtl::OString getB64CertFromFile(const char filename[]);
64         test::OfficeConnection connection_;
65 
66     public:
67 
68         Test();
69 
70         ~Test();
71 
72         virtual void SetUp();
73 
74         virtual void TearDown();
75     };
76 
77     uno::Sequence< security::CertAltNameEntry > Test::altNames;
78     bool Test::runOnce = false;
79 
80 
81     Test::Test()
82     {
83         if (runOnce)
84             return;
85         runOnce = true;
86         connection_.setUp();
87         init();
88     }
89 
90     Test::~Test()
91     {
92         if (runOnce)
93         {
94             connection_.tearDown();
95             runOnce = false;
96         }
97     }
98 
99 
100     uno::Reference< xml::crypto::XSecurityEnvironment > Test::initUno()
101     {
102         uno::Reference< uno::XComponentContext > context(connection_.getComponentContext(), uno::UNO_QUERY_THROW);
103         uno::Reference< lang::XMultiServiceFactory > factory(context->getServiceManager(), uno::UNO_QUERY_THROW);
104         uno::Reference< xml::crypto::XSEInitializer > xSEInitializer(factory->createInstance(
105             rtl::OUString::createFromAscii( SEINITIALIZER_COMPONENT )), uno::UNO_QUERY_THROW);
106         uno::Reference< xml::crypto::XXMLSecurityContext > xSecurityContext(
107             xSEInitializer->createSecurityContext(rtl::OUString()));
108         return xSecurityContext->getSecurityEnvironment();
109     }
110 
111 
112     void Test::init()
113     {
114         uno::Reference< xml::crypto::XSecurityEnvironment > xSecurityEnv = initUno();
115         rtl::OString b64Cert(getB64CertFromFile("User_35_Root_11.crt"));
116         uno::Reference< security::XCertificate > xCert = xSecurityEnv->createCertificateFromAscii(
117             rtl::OStringToOUString( b64Cert, RTL_TEXTENCODING_ASCII_US ) );
118         uno::Sequence< uno::Reference< security::XCertificateExtension > > extensions = xCert->getExtensions();
119         for (sal_Int32 i = 0 ; i < extensions.getLength(); i++)
120         {
121             uno::Reference< security::XCertificateExtension >element = extensions[i];
122             rtl::OString aId ( (const sal_Char *)element->getExtensionId().getArray(), element->getExtensionId().getLength());
123             if (aId.equals(OID_SUBJECT_ALTERNATIVE_NAME))
124             {
125                 uno::Reference< security::XSanExtension > sanExtension ( element, uno::UNO_QUERY );
126                 altNames = sanExtension->getAlternativeNames();
127                 break;
128             }
129         }
130     }
131 
132     rtl::OString Test::getB64CertFromFile(const char filename[])
133     {
134         ne_ssl_certificate* cert = ne_ssl_cert_read(filename);
135         char* certExportB64 = ne_ssl_cert_export(cert);
136         rtl::OString certB64( certExportB64 );
137         return certB64;
138     }
139 
140 
141     void Test::SetUp() {
142     }
143 
144     void Test::TearDown() {
145     }
146 
147     TEST_F(Test, test_Others) {
148         ASSERT_NO_THROW( ASSERT_TRUE( altNames.getLength() > 0 ) );
149         for(int n = 1; n < altNames.getLength(); n++)
150         {
151             if (altNames[n].Type ==  security::ExtAltNameType_OTHER_NAME)
152             {
153                 ::com::sun::star::beans::NamedValue otherNameProp;
154                 if (altNames[n].Value >>= otherNameProp)
155                 {
156                     ASSERT_EQ( rtl::OUString::createFromAscii("1.2.3.4"), otherNameProp.Name);
157                     uno::Sequence< sal_Int8 > ipAddress;
158                     otherNameProp.Value >>= ipAddress;
159                     ASSERT_NO_THROW( ASSERT_TRUE( ipAddress.getLength() > 0 ) );
160                 }
161             }
162         }
163     }
164 
165     TEST_F(Test, test_RFC822) {
166         ASSERT_NO_THROW( ASSERT_TRUE( altNames.getLength() > 0 ) );
167         for(int n = 1; n < altNames.getLength(); n++)
168         {
169             if (altNames[n].Type ==  security::ExtAltNameType_RFC822_NAME)
170             {
171                 rtl::OUString value;
172                 altNames[n].Value >>= value;
173                 ASSERT_EQ( rtl::OUString::createFromAscii("my@other.address"), value );
174             }
175         }
176     }
177 
178     TEST_F(Test, test_DNS) {
179         ASSERT_NO_THROW( ASSERT_TRUE( altNames.getLength() > 0 ) );
180         for(int n = 1; n < altNames.getLength(); n++)
181         {
182             if (altNames[n].Type ==  security::ExtAltNameType_DNS_NAME)
183             {
184                 rtl::OUString value;
185                 altNames[n].Value >>= value;
186                 ASSERT_EQ( rtl::OUString::createFromAscii("alt.openoffice.org"), value);
187             }
188         }
189     }
190 
191     TEST_F(Test, test_Direcory) {
192         // Not implemented
193     }
194 
195     TEST_F(Test, test_URI) {
196         ASSERT_NO_THROW( ASSERT_TRUE( altNames.getLength() > 0 ) );
197         for(int n = 1; n < altNames.getLength(); n++)
198         {
199             if (altNames[n].Type ==  security::ExtAltNameType_URL)
200             {
201                 rtl::OUString value;
202                 altNames[n].Value >>= value;
203                 ASSERT_EQ( rtl::OUString::createFromAscii("http://my.url.here/"), value);
204             }
205         }
206     }
207 
208     TEST_F(Test, test_IP) {
209         ASSERT_NO_THROW( ASSERT_TRUE( altNames.getLength() > 0 ) );
210         for(int n = 1; n < altNames.getLength(); n++)
211         {
212             if (altNames[n].Type ==  security::ExtAltNameType_IP_ADDRESS)
213             {
214                 uno::Sequence< sal_Int8 > ipAddress;
215                 altNames[n].Value >>= ipAddress;
216                 ASSERT_NO_THROW( ASSERT_TRUE( ipAddress.getLength() > 0 ) );
217             }
218         }
219 
220     }
221 
222     TEST_F(Test, test_RID) {
223         ASSERT_NO_THROW( ASSERT_TRUE( altNames.getLength() > 0 ) );
224         for(int n = 1; n < altNames.getLength(); n++)
225         {
226             if (altNames[n].Type ==  security::ExtAltNameType_REGISTERED_ID)
227             {
228                 rtl::OUString value;
229                 altNames[n].Value >>= value;
230                 ASSERT_TRUE( rtl::OUString::createFromAscii("1.2.3.4").equals(value));
231             }
232         }
233     }
234 
235     TEST_F(Test, test_EDI) {
236         // Not implemented
237     }
238 
239     TEST_F(Test, test_X400) {
240         // Not implemented
241     }
242 }
243