1*c82f2877SAndrew Rist /**************************************************************
2*c82f2877SAndrew Rist  *
3*c82f2877SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*c82f2877SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*c82f2877SAndrew Rist  * distributed with this work for additional information
6*c82f2877SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*c82f2877SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*c82f2877SAndrew Rist  * "License"); you may not use this file except in compliance
9*c82f2877SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*c82f2877SAndrew Rist  *
11*c82f2877SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*c82f2877SAndrew Rist  *
13*c82f2877SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*c82f2877SAndrew Rist  * software distributed under the License is distributed on an
15*c82f2877SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*c82f2877SAndrew Rist  * KIND, either express or implied.  See the License for the
17*c82f2877SAndrew Rist  * specific language governing permissions and limitations
18*c82f2877SAndrew Rist  * under the License.
19*c82f2877SAndrew Rist  *
20*c82f2877SAndrew Rist  *************************************************************/
21*c82f2877SAndrew Rist 
22*c82f2877SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_xmlsecurity.hxx"
26cdf0e10cSrcweir #include <sal/config.h>
27cdf0e10cSrcweir #include <rtl/uuid.h>
28cdf0e10cSrcweir #include <rtl/ustring.hxx>
29cdf0e10cSrcweir #include <com/sun/star/security/ExtAltNameType.hpp>
30cdf0e10cSrcweir #include <com/sun/star/security/CertAltNameEntry.hpp>
31cdf0e10cSrcweir #include <com/sun/star/beans/PropertyValue.hpp>
32cdf0e10cSrcweir #include <com/sun/star/uno/Reference.hxx>
33cdf0e10cSrcweir #include <comphelper/sequence.hxx>
34cdf0e10cSrcweir #include <seccomon.h>
35cdf0e10cSrcweir #include <cert.h>
36cdf0e10cSrcweir #include <certt.h>
37cdf0e10cSrcweir #include <secitem.h>
38cdf0e10cSrcweir #include <secport.h>
39cdf0e10cSrcweir 
40cdf0e10cSrcweir 
41cdf0e10cSrcweir #ifndef _SANEXTENSION_NSSIMPL_HXX_
42cdf0e10cSrcweir #include "sanextension_nssimpl.hxx"
43cdf0e10cSrcweir #endif
44cdf0e10cSrcweir 
45cdf0e10cSrcweir using namespace ::com::sun::star;
46cdf0e10cSrcweir using namespace ::com::sun::star::uno ;
47cdf0e10cSrcweir using namespace ::com::sun::star::security ;
48cdf0e10cSrcweir using ::rtl::OUString ;
49cdf0e10cSrcweir 
50cdf0e10cSrcweir using ::com::sun::star::security::XCertificateExtension ;
51cdf0e10cSrcweir 
52cdf0e10cSrcweir 
SanExtensionImpl()53cdf0e10cSrcweir SanExtensionImpl :: SanExtensionImpl() :
54cdf0e10cSrcweir m_critical( sal_False )
55cdf0e10cSrcweir {
56cdf0e10cSrcweir }
57cdf0e10cSrcweir 
~SanExtensionImpl()58cdf0e10cSrcweir SanExtensionImpl :: ~SanExtensionImpl() {
59cdf0e10cSrcweir }
60cdf0e10cSrcweir 
61cdf0e10cSrcweir 
62cdf0e10cSrcweir //Methods from XCertificateExtension
isCritical()63cdf0e10cSrcweir sal_Bool SAL_CALL SanExtensionImpl :: isCritical() throw( ::com::sun::star::uno::RuntimeException ) {
64cdf0e10cSrcweir     return m_critical ;
65cdf0e10cSrcweir }
66cdf0e10cSrcweir 
getExtensionId()67cdf0e10cSrcweir ::com::sun::star::uno::Sequence< sal_Int8 > SAL_CALL SanExtensionImpl :: getExtensionId() throw( ::com::sun::star::uno::RuntimeException ) {
68cdf0e10cSrcweir     return m_xExtnId ;
69cdf0e10cSrcweir }
70cdf0e10cSrcweir 
getExtensionValue()71cdf0e10cSrcweir ::com::sun::star::uno::Sequence< sal_Int8 > SAL_CALL SanExtensionImpl :: getExtensionValue() throw( ::com::sun::star::uno::RuntimeException ) {
72cdf0e10cSrcweir     return m_xExtnValue ;
73cdf0e10cSrcweir }
74cdf0e10cSrcweir 
75cdf0e10cSrcweir namespace {
76cdf0e10cSrcweir     // Helper functions from nss/lib/certdb/genname.c
GetNamesLength(CERTGeneralName * names)77cdf0e10cSrcweir     static int GetNamesLength(CERTGeneralName *names)
78cdf0e10cSrcweir     {
79cdf0e10cSrcweir         int              length = 0;
80cdf0e10cSrcweir         CERTGeneralName  *first;
81cdf0e10cSrcweir 
82cdf0e10cSrcweir         first = names;
83cdf0e10cSrcweir         if (names != NULL) {
84cdf0e10cSrcweir             do {
85cdf0e10cSrcweir                 length++;
86cdf0e10cSrcweir                 names = CERT_GetNextGeneralName(names);
87cdf0e10cSrcweir             } while (names != first);
88cdf0e10cSrcweir         }
89cdf0e10cSrcweir         return length;
90cdf0e10cSrcweir     }
91cdf0e10cSrcweir 
92cdf0e10cSrcweir }
93cdf0e10cSrcweir 
94cdf0e10cSrcweir //Methods from XSanExtension
getAlternativeNames()95cdf0e10cSrcweir ::com::sun::star::uno::Sequence< com::sun::star::security::CertAltNameEntry > SAL_CALL SanExtensionImpl :: getAlternativeNames() throw( ::com::sun::star::uno::RuntimeException ){
96cdf0e10cSrcweir 
97cdf0e10cSrcweir     if (!m_Entries.hasElements())
98cdf0e10cSrcweir     {
99cdf0e10cSrcweir         SECItem item;
100cdf0e10cSrcweir 
101cdf0e10cSrcweir         item.type = siDERCertBuffer;
102cdf0e10cSrcweir         item.data = (unsigned char*) m_xExtnValue.getArray();
103cdf0e10cSrcweir         item.len = m_xExtnValue.getLength();
104cdf0e10cSrcweir 
105cdf0e10cSrcweir         PRArenaPool *arena;
106cdf0e10cSrcweir         CERTGeneralName *nameList;
107cdf0e10cSrcweir         arena = PORT_NewArena(DER_DEFAULT_CHUNKSIZE);
108cdf0e10cSrcweir 
109cdf0e10cSrcweir         if (!arena)
110cdf0e10cSrcweir             return m_Entries;
111cdf0e10cSrcweir 
112cdf0e10cSrcweir         nameList = CERT_DecodeAltNameExtension(arena, &item);
113cdf0e10cSrcweir 
114cdf0e10cSrcweir         CERTGeneralName* current = nameList;
115cdf0e10cSrcweir 
116cdf0e10cSrcweir         int size = GetNamesLength(nameList);
117cdf0e10cSrcweir         CertAltNameEntry* arrCertAltNameEntry = new CertAltNameEntry[size];
118cdf0e10cSrcweir         for(int i = 0; i < size ; i++){
119cdf0e10cSrcweir             switch (current->type) {
120cdf0e10cSrcweir                 case certOtherName: {
121cdf0e10cSrcweir                     arrCertAltNameEntry[i].Type = ExtAltNameType_OTHER_NAME;
122cdf0e10cSrcweir                     ::com::sun::star::beans::PropertyValue otherNameProp;
123cdf0e10cSrcweir                     otherNameProp.Name = ::rtl::OUString::createFromAscii(CERT_GetOidString(&current->name.OthName.oid));
124cdf0e10cSrcweir 
125cdf0e10cSrcweir                     Sequence< sal_Int8 > otherName( current->name.OthName.name.len ) ;
126cdf0e10cSrcweir                     for( unsigned int r = 0; r < current->name.OthName.name.len ; r ++ )
127cdf0e10cSrcweir                         otherName[r] = *( current->name.OthName.name.data + r ) ;
128cdf0e10cSrcweir 
129cdf0e10cSrcweir                     otherNameProp.Value <<= otherName;
130cdf0e10cSrcweir 
131cdf0e10cSrcweir                     arrCertAltNameEntry[i].Value <<= otherNameProp;
132cdf0e10cSrcweir                     break;
133cdf0e10cSrcweir                                     }
134cdf0e10cSrcweir                 case certRFC822Name:
135cdf0e10cSrcweir                     arrCertAltNameEntry[i].Type = ExtAltNameType_RFC822_NAME;
136cdf0e10cSrcweir                     arrCertAltNameEntry[i].Value <<= ::rtl::OUString((const sal_Char*)current->name.other.data, current->name.other.len, RTL_TEXTENCODING_ASCII_US);
137cdf0e10cSrcweir                     break;
138cdf0e10cSrcweir                 case certDNSName:
139cdf0e10cSrcweir                     arrCertAltNameEntry[i].Type = ExtAltNameType_DNS_NAME;
140cdf0e10cSrcweir                     arrCertAltNameEntry[i].Value <<= ::rtl::OUString((const sal_Char*)current->name.other.data, current->name.other.len, RTL_TEXTENCODING_ASCII_US);
141cdf0e10cSrcweir                     break;
142cdf0e10cSrcweir                 case certX400Address: {
143cdf0e10cSrcweir                     // unsupported
144cdf0e10cSrcweir                     arrCertAltNameEntry[i].Type = ExtAltNameType_X400_ADDRESS;
145cdf0e10cSrcweir                     break;
146cdf0e10cSrcweir                                       }
147cdf0e10cSrcweir                 case certDirectoryName: {
148cdf0e10cSrcweir 					// unsupported
149cdf0e10cSrcweir 					arrCertAltNameEntry[i].Type = ExtAltNameType_DIRECTORY_NAME;
150cdf0e10cSrcweir                     break;
151cdf0e10cSrcweir                                         }
152cdf0e10cSrcweir                 case certEDIPartyName:  {
153cdf0e10cSrcweir                     // unsupported
154cdf0e10cSrcweir                     arrCertAltNameEntry[i].Type = ExtAltNameType_EDI_PARTY_NAME;
155cdf0e10cSrcweir                     break;
156cdf0e10cSrcweir                                         }
157cdf0e10cSrcweir                 case certURI:
158cdf0e10cSrcweir                     arrCertAltNameEntry[i].Type = ExtAltNameType_URL;
159cdf0e10cSrcweir                     arrCertAltNameEntry[i].Value <<= ::rtl::OUString((const sal_Char*)current->name.other.data, current->name.other.len, RTL_TEXTENCODING_ASCII_US);
160cdf0e10cSrcweir                     break;
161cdf0e10cSrcweir                 case certIPAddress: {
162cdf0e10cSrcweir                     arrCertAltNameEntry[i].Type = ExtAltNameType_IP_ADDRESS;
163cdf0e10cSrcweir 
164cdf0e10cSrcweir                     Sequence< sal_Int8 > ipAddress( current->name.other.len ) ;
165cdf0e10cSrcweir                     for( unsigned int r = 0; r < current->name.other.len ; r ++ )
166cdf0e10cSrcweir                         ipAddress[r] = *( current->name.other.data + r ) ;
167cdf0e10cSrcweir 
168cdf0e10cSrcweir                     arrCertAltNameEntry[i].Value <<= ipAddress;
169cdf0e10cSrcweir                     break;
170cdf0e10cSrcweir                                     }
171cdf0e10cSrcweir                 case certRegisterID:
172cdf0e10cSrcweir                     arrCertAltNameEntry[i].Type = ExtAltNameType_REGISTERED_ID;
173cdf0e10cSrcweir 
174cdf0e10cSrcweir 
175cdf0e10cSrcweir 					rtl::OString nssOid = ::rtl::OString(CERT_GetOidString(&current->name.other));
176cdf0e10cSrcweir 					rtl::OString unoOid = removeOIDFromString(nssOid);
177cdf0e10cSrcweir                     arrCertAltNameEntry[i].Value <<= rtl::OStringToOUString( unoOid, RTL_TEXTENCODING_ASCII_US );
178cdf0e10cSrcweir                     break;
179cdf0e10cSrcweir             }
180cdf0e10cSrcweir             current = CERT_GetNextGeneralName(current);
181cdf0e10cSrcweir         }
182cdf0e10cSrcweir 
183cdf0e10cSrcweir         m_Entries = ::comphelper::arrayToSequence< com::sun::star::security::CertAltNameEntry >(arrCertAltNameEntry, size);
184cdf0e10cSrcweir 
185cdf0e10cSrcweir         delete [] arrCertAltNameEntry;
186cdf0e10cSrcweir 
187cdf0e10cSrcweir         PORT_FreeArena(arena, PR_FALSE);
188cdf0e10cSrcweir 
189cdf0e10cSrcweir 
190cdf0e10cSrcweir     }
191cdf0e10cSrcweir 
192cdf0e10cSrcweir     return m_Entries;
193cdf0e10cSrcweir }
194cdf0e10cSrcweir 
removeOIDFromString(const::rtl::OString & oidString)195cdf0e10cSrcweir ::rtl::OString SanExtensionImpl :: removeOIDFromString( const ::rtl::OString &oidString)
196cdf0e10cSrcweir 	{
197cdf0e10cSrcweir 		::rtl::OString objID;
198cdf0e10cSrcweir 		::rtl::OString oid("OID.");
199cdf0e10cSrcweir 		if (oidString.match(oid))
200cdf0e10cSrcweir 			objID = oidString.copy(oid.getLength());
201cdf0e10cSrcweir 		else
202cdf0e10cSrcweir 			objID = oidString;
203cdf0e10cSrcweir 		return objID;
204cdf0e10cSrcweir 
205cdf0e10cSrcweir 	}
206cdf0e10cSrcweir //Helper method
setCertExtn(::com::sun::star::uno::Sequence<sal_Int8> extnId,::com::sun::star::uno::Sequence<sal_Int8> extnValue,sal_Bool critical)207cdf0e10cSrcweir void SanExtensionImpl :: setCertExtn( ::com::sun::star::uno::Sequence< sal_Int8 > extnId, ::com::sun::star::uno::Sequence< sal_Int8 > extnValue, sal_Bool critical ) {
208cdf0e10cSrcweir     m_critical = critical ;
209cdf0e10cSrcweir     m_xExtnId = extnId ;
210cdf0e10cSrcweir     m_xExtnValue = extnValue ;
211cdf0e10cSrcweir }
212cdf0e10cSrcweir 
setCertExtn(unsigned char * value,unsigned int vlen,unsigned char * id,unsigned int idlen,sal_Bool critical)213cdf0e10cSrcweir void SanExtensionImpl :: setCertExtn( unsigned char* value, unsigned int vlen, unsigned char* id, unsigned int idlen, sal_Bool critical ) {
214cdf0e10cSrcweir     unsigned int i ;
215cdf0e10cSrcweir     if( value != NULL && vlen != 0 ) {
216cdf0e10cSrcweir         Sequence< sal_Int8 > extnv( vlen ) ;
217cdf0e10cSrcweir         for( i = 0; i < vlen ; i ++ )
218cdf0e10cSrcweir             extnv[i] = *( value + i ) ;
219cdf0e10cSrcweir 
220cdf0e10cSrcweir         m_xExtnValue = extnv ;
221cdf0e10cSrcweir     } else {
222cdf0e10cSrcweir         m_xExtnValue = Sequence<sal_Int8>();
223cdf0e10cSrcweir     }
224cdf0e10cSrcweir 
225cdf0e10cSrcweir     if( id != NULL && idlen != 0 ) {
226cdf0e10cSrcweir         Sequence< sal_Int8 > extnId( idlen ) ;
227cdf0e10cSrcweir         for( i = 0; i < idlen ; i ++ )
228cdf0e10cSrcweir             extnId[i] = *( id + i ) ;
229cdf0e10cSrcweir 
230cdf0e10cSrcweir         m_xExtnId = extnId ;
231cdf0e10cSrcweir     } else {
232cdf0e10cSrcweir         m_xExtnId =  Sequence<sal_Int8>();
233cdf0e10cSrcweir     }
234cdf0e10cSrcweir 
235cdf0e10cSrcweir     m_critical = critical ;
236cdf0e10cSrcweir }
237cdf0e10cSrcweir 
extractCertExt()238cdf0e10cSrcweir void SanExtensionImpl :: extractCertExt () {
239cdf0e10cSrcweir }
240cdf0e10cSrcweir 
241