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/NamedValue.hpp>
36 #include <com/sun/star/uno/Reference.hxx>
37 #include <comphelper/sequence.hxx>
38 
39 
40 #ifndef _SANEXTENSION_MSCRYPTIMPL_HXX_
41 #include "sanextension_mscryptimpl.hxx"
42 #endif
43 
44 using namespace ::com::sun::star;
45 using namespace ::com::sun::star::uno ;
46 using namespace ::com::sun::star::security ;
47 using ::rtl::OUString ;
48 
49 using ::com::sun::star::security::XCertificateExtension ;
50 
51 
52 SanExtensionImpl :: SanExtensionImpl() :
53 	m_critical( sal_False )
54 {
55 }
56 
57 SanExtensionImpl :: ~SanExtensionImpl() {
58 }
59 
60 
61 //Methods from XCertificateExtension
62 sal_Bool SAL_CALL SanExtensionImpl :: isCritical() throw( ::com::sun::star::uno::RuntimeException ) {
63 	return m_critical ;
64 }
65 
66 ::com::sun::star::uno::Sequence< sal_Int8 > SAL_CALL SanExtensionImpl :: getExtensionId() throw( ::com::sun::star::uno::RuntimeException ) {
67 	return m_xExtnId ;
68 }
69 
70 ::com::sun::star::uno::Sequence< sal_Int8 > SAL_CALL SanExtensionImpl :: getExtensionValue() throw( ::com::sun::star::uno::RuntimeException ) {
71 	return m_xExtnValue ;
72 }
73 
74 //Methods from XSanExtension
75 ::com::sun::star::uno::Sequence< com::sun::star::security::CertAltNameEntry > SAL_CALL SanExtensionImpl :: getAlternativeNames() throw( ::com::sun::star::uno::RuntimeException ){
76 
77     if (!m_Entries.hasElements())
78     {
79         CERT_ALT_NAME_INFO *subjectName;
80         DWORD size;
81         CryptDecodeObjectEx(X509_ASN_ENCODING, X509_ALTERNATE_NAME, (unsigned char*) m_xExtnValue.getArray(), m_xExtnValue.getLength(), CRYPT_DECODE_ALLOC_FLAG | CRYPT_DECODE_NOCOPY_FLAG, NULL,&subjectName, &size);
82 
83         CertAltNameEntry* arrCertAltNameEntry = new CertAltNameEntry[subjectName->cAltEntry];
84 
85         for (unsigned int i = 0; i < (unsigned int)subjectName->cAltEntry; i++){
86           PCERT_ALT_NAME_ENTRY pEntry = &subjectName->rgAltEntry[i];
87 
88           switch(pEntry->dwAltNameChoice) {
89             case CERT_ALT_NAME_OTHER_NAME :
90                 {
91                     arrCertAltNameEntry[i].Type = ExtAltNameType_OTHER_NAME;
92                     PCERT_OTHER_NAME pOtherName = pEntry->pOtherName;
93 
94                     ::com::sun::star::beans::NamedValue otherNameProp;
95                     otherNameProp.Name = ::rtl::OUString::createFromAscii(pOtherName->pszObjId);
96 
97                     Sequence< sal_Int8 > otherName( pOtherName->Value.cbData ) ;
98 		            for( unsigned int n = 0; n < (unsigned int) pOtherName->Value.cbData ; n ++ )
99 			            otherName[n] = *( pOtherName->Value.pbData + n ) ;
100 
101                     otherNameProp.Value <<= otherName;
102 
103                     arrCertAltNameEntry[i].Value <<= otherNameProp;
104                     break;
105                 }
106             case CERT_ALT_NAME_RFC822_NAME :
107                 arrCertAltNameEntry[i].Type = ExtAltNameType_RFC822_NAME;
108                 arrCertAltNameEntry[i].Value <<= ::rtl::OUString((const sal_Unicode*)pEntry->pwszRfc822Name);
109                 break;
110             case CERT_ALT_NAME_DNS_NAME :
111                 arrCertAltNameEntry[i].Type = ExtAltNameType_DNS_NAME;
112                 arrCertAltNameEntry[i].Value <<= ::rtl::OUString((const sal_Unicode*)pEntry->pwszDNSName);
113                 break;
114             case CERT_ALT_NAME_DIRECTORY_NAME :
115                 {
116                     arrCertAltNameEntry[i].Type = ExtAltNameType_DIRECTORY_NAME;
117                     break;
118                 }
119             case CERT_ALT_NAME_URL :
120                 arrCertAltNameEntry[i].Type = ExtAltNameType_URL;
121                 arrCertAltNameEntry[i].Value <<= ::rtl::OUString((const sal_Unicode*)pEntry->pwszURL);
122                 break;
123             case CERT_ALT_NAME_IP_ADDRESS :
124                 {
125                     arrCertAltNameEntry[i].Type = ExtAltNameType_IP_ADDRESS;
126 
127                     Sequence< sal_Int8 > ipAddress( pEntry->IPAddress.cbData ) ;
128 		            for( unsigned int n = 0; n < pEntry->IPAddress.cbData ; n ++ )
129 			            ipAddress[n] = *( pEntry->IPAddress.pbData + n ) ;
130 
131                     arrCertAltNameEntry[i].Value <<= ipAddress;
132                     break;
133                 }
134             case CERT_ALT_NAME_REGISTERED_ID :
135                 arrCertAltNameEntry[i].Type = ExtAltNameType_REGISTERED_ID;
136                 arrCertAltNameEntry[i].Value <<= ::rtl::OUString::createFromAscii(pEntry->pszRegisteredID);
137                 break;
138           }
139         }
140         m_Entries = ::comphelper::arrayToSequence< com::sun::star::security::CertAltNameEntry >(arrCertAltNameEntry, subjectName->cAltEntry);
141 
142         delete [] arrCertAltNameEntry;
143     }
144 
145     return m_Entries;
146 }
147 
148 //Helper method
149 void SanExtensionImpl :: setCertExtn( ::com::sun::star::uno::Sequence< sal_Int8 > extnId, ::com::sun::star::uno::Sequence< sal_Int8 > extnValue, sal_Bool critical ) {
150 	m_critical = critical ;
151 	m_xExtnId = extnId ;
152 	m_xExtnValue = extnValue ;
153 }
154 
155 void SanExtensionImpl :: setCertExtn( unsigned char* value, unsigned int vlen, unsigned char* id, unsigned int idlen, sal_Bool critical ) {
156 	unsigned int i ;
157 	if( value != NULL && vlen != 0 ) {
158 		Sequence< sal_Int8 > extnv( vlen ) ;
159 		for( i = 0; i < vlen ; i ++ )
160 			extnv[i] = *( value + i ) ;
161 
162 		m_xExtnValue = extnv ;
163 	} else {
164 		m_xExtnValue = Sequence<sal_Int8>();
165 	}
166 
167 	if( id != NULL && idlen != 0 ) {
168 		Sequence< sal_Int8 > extnId( idlen ) ;
169 		for( i = 0; i < idlen ; i ++ )
170 			extnId[i] = *( id + i ) ;
171 
172 		m_xExtnId = extnId ;
173 	} else {
174 		m_xExtnId =  Sequence<sal_Int8>();
175 	}
176 
177 	m_critical = critical ;
178 }
179 
180 void SanExtensionImpl :: extractCertExt () {
181 }
182 
183