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 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_xmlsecurity.hxx"
30 #include <sal/config.h>
31 #include <rtl/uuid.h>
32 #include <rtl/ustring.hxx>
33 #include <com/sun/star/security/ExtAltNameType.hpp>
34 #include <com/sun/star/security/CertAltNameEntry.hpp>
35 #include <com/sun/star/beans/PropertyValue.hpp>
36 #include <com/sun/star/uno/Reference.hxx>
37 #include <comphelper/sequence.hxx>
38 #include <seccomon.h>
39 #include <cert.h>
40 #include <certt.h>
41 #include <secitem.h>
42 #include <secport.h>
43 
44 
45 #ifndef _SANEXTENSION_NSSIMPL_HXX_
46 #include "sanextension_nssimpl.hxx"
47 #endif
48 
49 using namespace ::com::sun::star;
50 using namespace ::com::sun::star::uno ;
51 using namespace ::com::sun::star::security ;
52 using ::rtl::OUString ;
53 
54 using ::com::sun::star::security::XCertificateExtension ;
55 
56 
57 SanExtensionImpl :: SanExtensionImpl() :
58 m_critical( sal_False )
59 {
60 }
61 
62 SanExtensionImpl :: ~SanExtensionImpl() {
63 }
64 
65 
66 //Methods from XCertificateExtension
67 sal_Bool SAL_CALL SanExtensionImpl :: isCritical() throw( ::com::sun::star::uno::RuntimeException ) {
68     return m_critical ;
69 }
70 
71 ::com::sun::star::uno::Sequence< sal_Int8 > SAL_CALL SanExtensionImpl :: getExtensionId() throw( ::com::sun::star::uno::RuntimeException ) {
72     return m_xExtnId ;
73 }
74 
75 ::com::sun::star::uno::Sequence< sal_Int8 > SAL_CALL SanExtensionImpl :: getExtensionValue() throw( ::com::sun::star::uno::RuntimeException ) {
76     return m_xExtnValue ;
77 }
78 
79 namespace {
80     // Helper functions from nss/lib/certdb/genname.c
81     static int GetNamesLength(CERTGeneralName *names)
82     {
83         int              length = 0;
84         CERTGeneralName  *first;
85 
86         first = names;
87         if (names != NULL) {
88             do {
89                 length++;
90                 names = CERT_GetNextGeneralName(names);
91             } while (names != first);
92         }
93         return length;
94     }
95 
96 }
97 
98 //Methods from XSanExtension
99 ::com::sun::star::uno::Sequence< com::sun::star::security::CertAltNameEntry > SAL_CALL SanExtensionImpl :: getAlternativeNames() throw( ::com::sun::star::uno::RuntimeException ){
100 
101     if (!m_Entries.hasElements())
102     {
103         SECItem item;
104 
105         item.type = siDERCertBuffer;
106         item.data = (unsigned char*) m_xExtnValue.getArray();
107         item.len = m_xExtnValue.getLength();
108 
109         PRArenaPool *arena;
110         CERTGeneralName *nameList;
111         arena = PORT_NewArena(DER_DEFAULT_CHUNKSIZE);
112 
113         if (!arena)
114             return m_Entries;
115 
116         nameList = CERT_DecodeAltNameExtension(arena, &item);
117 
118         CERTGeneralName* current = nameList;
119 
120         int size = GetNamesLength(nameList);
121         CertAltNameEntry* arrCertAltNameEntry = new CertAltNameEntry[size];
122         for(int i = 0; i < size ; i++){
123             switch (current->type) {
124                 case certOtherName: {
125                     arrCertAltNameEntry[i].Type = ExtAltNameType_OTHER_NAME;
126                     ::com::sun::star::beans::PropertyValue otherNameProp;
127                     otherNameProp.Name = ::rtl::OUString::createFromAscii(CERT_GetOidString(&current->name.OthName.oid));
128 
129                     Sequence< sal_Int8 > otherName( current->name.OthName.name.len ) ;
130                     for( unsigned int r = 0; r < current->name.OthName.name.len ; r ++ )
131                         otherName[r] = *( current->name.OthName.name.data + r ) ;
132 
133                     otherNameProp.Value <<= otherName;
134 
135                     arrCertAltNameEntry[i].Value <<= otherNameProp;
136                     break;
137                                     }
138                 case certRFC822Name:
139                     arrCertAltNameEntry[i].Type = ExtAltNameType_RFC822_NAME;
140                     arrCertAltNameEntry[i].Value <<= ::rtl::OUString((const sal_Char*)current->name.other.data, current->name.other.len, RTL_TEXTENCODING_ASCII_US);
141                     break;
142                 case certDNSName:
143                     arrCertAltNameEntry[i].Type = ExtAltNameType_DNS_NAME;
144                     arrCertAltNameEntry[i].Value <<= ::rtl::OUString((const sal_Char*)current->name.other.data, current->name.other.len, RTL_TEXTENCODING_ASCII_US);
145                     break;
146                 case certX400Address: {
147                     // unsupported
148                     arrCertAltNameEntry[i].Type = ExtAltNameType_X400_ADDRESS;
149                     break;
150                                       }
151                 case certDirectoryName: {
152 					// unsupported
153 					arrCertAltNameEntry[i].Type = ExtAltNameType_DIRECTORY_NAME;
154                     break;
155                                         }
156                 case certEDIPartyName:  {
157                     // unsupported
158                     arrCertAltNameEntry[i].Type = ExtAltNameType_EDI_PARTY_NAME;
159                     break;
160                                         }
161                 case certURI:
162                     arrCertAltNameEntry[i].Type = ExtAltNameType_URL;
163                     arrCertAltNameEntry[i].Value <<= ::rtl::OUString((const sal_Char*)current->name.other.data, current->name.other.len, RTL_TEXTENCODING_ASCII_US);
164                     break;
165                 case certIPAddress: {
166                     arrCertAltNameEntry[i].Type = ExtAltNameType_IP_ADDRESS;
167 
168                     Sequence< sal_Int8 > ipAddress( current->name.other.len ) ;
169                     for( unsigned int r = 0; r < current->name.other.len ; r ++ )
170                         ipAddress[r] = *( current->name.other.data + r ) ;
171 
172                     arrCertAltNameEntry[i].Value <<= ipAddress;
173                     break;
174                                     }
175                 case certRegisterID:
176                     arrCertAltNameEntry[i].Type = ExtAltNameType_REGISTERED_ID;
177 
178 
179 					rtl::OString nssOid = ::rtl::OString(CERT_GetOidString(&current->name.other));
180 					rtl::OString unoOid = removeOIDFromString(nssOid);
181                     arrCertAltNameEntry[i].Value <<= rtl::OStringToOUString( unoOid, RTL_TEXTENCODING_ASCII_US );
182                     break;
183             }
184             current = CERT_GetNextGeneralName(current);
185         }
186 
187         m_Entries = ::comphelper::arrayToSequence< com::sun::star::security::CertAltNameEntry >(arrCertAltNameEntry, size);
188 
189         delete [] arrCertAltNameEntry;
190 
191         PORT_FreeArena(arena, PR_FALSE);
192 
193 
194     }
195 
196     return m_Entries;
197 }
198 
199 ::rtl::OString SanExtensionImpl :: removeOIDFromString( const ::rtl::OString &oidString)
200 	{
201 		::rtl::OString objID;
202 		::rtl::OString oid("OID.");
203 		if (oidString.match(oid))
204 			objID = oidString.copy(oid.getLength());
205 		else
206 			objID = oidString;
207 		return objID;
208 
209 	}
210 //Helper method
211 void SanExtensionImpl :: setCertExtn( ::com::sun::star::uno::Sequence< sal_Int8 > extnId, ::com::sun::star::uno::Sequence< sal_Int8 > extnValue, sal_Bool critical ) {
212     m_critical = critical ;
213     m_xExtnId = extnId ;
214     m_xExtnValue = extnValue ;
215 }
216 
217 void SanExtensionImpl :: setCertExtn( unsigned char* value, unsigned int vlen, unsigned char* id, unsigned int idlen, sal_Bool critical ) {
218     unsigned int i ;
219     if( value != NULL && vlen != 0 ) {
220         Sequence< sal_Int8 > extnv( vlen ) ;
221         for( i = 0; i < vlen ; i ++ )
222             extnv[i] = *( value + i ) ;
223 
224         m_xExtnValue = extnv ;
225     } else {
226         m_xExtnValue = Sequence<sal_Int8>();
227     }
228 
229     if( id != NULL && idlen != 0 ) {
230         Sequence< sal_Int8 > extnId( idlen ) ;
231         for( i = 0; i < idlen ; i ++ )
232             extnId[i] = *( id + i ) ;
233 
234         m_xExtnId = extnId ;
235     } else {
236         m_xExtnId =  Sequence<sal_Int8>();
237     }
238 
239     m_critical = critical ;
240 }
241 
242 void SanExtensionImpl :: extractCertExt () {
243 }
244 
245