Branch data Line data Source code
1 : : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : : /*
3 : : * This file is part of the LibreOffice project.
4 : : *
5 : : * This Source Code Form is subject to the terms of the Mozilla Public
6 : : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : : *
9 : : * This file incorporates work covered by the following license notice:
10 : : *
11 : : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : : * contributor license agreements. See the NOTICE file distributed
13 : : * with this work for additional information regarding copyright
14 : : * ownership. The ASF licenses this file to you under the Apache
15 : : * License, Version 2.0 (the "License"); you may not use this file
16 : : * except in compliance with the License. You may obtain a copy of
17 : : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : : */
19 : :
20 : : #include "nssrenam.h" // rename problematic symbols
21 : : #include "nspr.h"
22 : : #include "nss.h"
23 : : #include "secder.h"
24 : :
25 : : #include "hasht.h"
26 : : #include "secoid.h"
27 : : #include "pk11func.h"
28 : :
29 : : #include <sal/config.h>
30 : : #include <comphelper/servicehelper.hxx>
31 : : #include "x509certificate_nssimpl.hxx"
32 : :
33 : : #include "certificateextension_xmlsecimpl.hxx"
34 : :
35 : : #include "sanextension_nssimpl.hxx"
36 : :
37 : : using namespace ::com::sun::star::uno ;
38 : : using namespace ::com::sun::star::security ;
39 : : using ::rtl::OUString ;
40 : :
41 : : using ::com::sun::star::security::XCertificate ;
42 : : using ::com::sun::star::util::DateTime ;
43 : :
44 : 0 : X509Certificate_NssImpl :: X509Certificate_NssImpl() :
45 : 0 : m_pCert( NULL )
46 : : {
47 : 0 : }
48 : :
49 : 0 : X509Certificate_NssImpl :: ~X509Certificate_NssImpl() {
50 [ # # ]: 0 : if( m_pCert != NULL ) {
51 [ # # ]: 0 : CERT_DestroyCertificate( m_pCert ) ;
52 : : }
53 [ # # ]: 0 : }
54 : :
55 : : //Methods from XCertificate
56 : 0 : sal_Int16 SAL_CALL X509Certificate_NssImpl :: getVersion() throw ( ::com::sun::star::uno::RuntimeException) {
57 [ # # ]: 0 : if( m_pCert != NULL ) {
58 [ # # ]: 0 : if( m_pCert->version.len > 0 ) {
59 : 0 : return ( char )*( m_pCert->version.data ) ;
60 : : } else
61 : 0 : return 0 ;
62 : : } else {
63 : 0 : return -1 ;
64 : : }
65 : : }
66 : :
67 : 0 : ::com::sun::star::uno::Sequence< sal_Int8 > SAL_CALL X509Certificate_NssImpl :: getSerialNumber() throw ( ::com::sun::star::uno::RuntimeException) {
68 [ # # ][ # # ]: 0 : if( m_pCert != NULL && m_pCert->serialNumber.len > 0 ) {
69 [ # # ]: 0 : Sequence< sal_Int8 > serial( m_pCert->serialNumber.len ) ;
70 [ # # ]: 0 : for( unsigned int i = 0 ; i < m_pCert->serialNumber.len ; i ++ )
71 [ # # ]: 0 : serial[i] = *( m_pCert->serialNumber.data + i ) ;
72 : :
73 [ # # ][ # # ]: 0 : return serial ;
74 : : } else {
75 : 0 : return ::com::sun::star::uno::Sequence< sal_Int8 >();
76 : : }
77 : : }
78 : :
79 : 0 : ::rtl::OUString SAL_CALL X509Certificate_NssImpl :: getIssuerName() throw ( ::com::sun::star::uno::RuntimeException) {
80 [ # # ]: 0 : if( m_pCert != NULL ) {
81 : 0 : return OUString(m_pCert->issuerName , PL_strlen(m_pCert->issuerName) , RTL_TEXTENCODING_UTF8) ;
82 : : } else {
83 : 0 : return OUString() ;
84 : : }
85 : : }
86 : :
87 : 0 : ::rtl::OUString SAL_CALL X509Certificate_NssImpl :: getSubjectName() throw ( ::com::sun::star::uno::RuntimeException) {
88 [ # # ]: 0 : if( m_pCert != NULL ) {
89 : 0 : return OUString(m_pCert->subjectName , PL_strlen(m_pCert->subjectName) , RTL_TEXTENCODING_UTF8);
90 : : } else {
91 : 0 : return OUString() ;
92 : : }
93 : : }
94 : :
95 : 0 : ::com::sun::star::util::DateTime SAL_CALL X509Certificate_NssImpl :: getNotValidBefore() throw ( ::com::sun::star::uno::RuntimeException) {
96 [ # # ]: 0 : if( m_pCert != NULL ) {
97 : : SECStatus rv ;
98 : : PRTime notBefore ;
99 : : PRExplodedTime explTime ;
100 : 0 : DateTime dateTime ;
101 : :
102 [ # # ]: 0 : rv = DER_DecodeTimeChoice( ¬Before, &m_pCert->validity.notBefore ) ;
103 [ # # ]: 0 : if( rv ) {
104 : 0 : return DateTime() ;
105 : : }
106 : :
107 : : //Convert the time to readable local time
108 [ # # ]: 0 : PR_ExplodeTime( notBefore, PR_LocalTimeParameters, &explTime ) ;
109 : :
110 : 0 : dateTime.HundredthSeconds = static_cast< sal_Int16 >( explTime.tm_usec / 1000 );
111 : 0 : dateTime.Seconds = static_cast< sal_Int16 >( explTime.tm_sec );
112 : 0 : dateTime.Minutes = static_cast< sal_Int16 >( explTime.tm_min );
113 : 0 : dateTime.Hours = static_cast< sal_Int16 >( explTime.tm_hour );
114 : 0 : dateTime.Day = static_cast< sal_Int16 >( explTime.tm_mday );
115 : 0 : dateTime.Month = static_cast< sal_Int16 >( explTime.tm_month+1 );
116 : 0 : dateTime.Year = static_cast< sal_Int16 >( explTime.tm_year );
117 : :
118 : 0 : return dateTime ;
119 : : } else {
120 : 0 : return DateTime() ;
121 : : }
122 : : }
123 : :
124 : 0 : ::com::sun::star::util::DateTime SAL_CALL X509Certificate_NssImpl :: getNotValidAfter() throw ( ::com::sun::star::uno::RuntimeException) {
125 [ # # ]: 0 : if( m_pCert != NULL ) {
126 : : SECStatus rv ;
127 : : PRTime notAfter ;
128 : : PRExplodedTime explTime ;
129 : 0 : DateTime dateTime ;
130 : :
131 [ # # ]: 0 : rv = DER_DecodeTimeChoice( ¬After, &m_pCert->validity.notAfter ) ;
132 [ # # ]: 0 : if( rv ) {
133 : 0 : return DateTime() ;
134 : : }
135 : :
136 : : //Convert the time to readable local time
137 [ # # ]: 0 : PR_ExplodeTime( notAfter, PR_LocalTimeParameters, &explTime ) ;
138 : :
139 : 0 : dateTime.HundredthSeconds = static_cast< sal_Int16 >( explTime.tm_usec / 1000 );
140 : 0 : dateTime.Seconds = static_cast< sal_Int16 >( explTime.tm_sec );
141 : 0 : dateTime.Minutes = static_cast< sal_Int16 >( explTime.tm_min );
142 : 0 : dateTime.Hours = static_cast< sal_Int16 >( explTime.tm_hour );
143 : 0 : dateTime.Day = static_cast< sal_Int16 >( explTime.tm_mday );
144 : 0 : dateTime.Month = static_cast< sal_Int16 >( explTime.tm_month+1 );
145 : 0 : dateTime.Year = static_cast< sal_Int16 >( explTime.tm_year );
146 : :
147 : 0 : return dateTime ;
148 : : } else {
149 : 0 : return DateTime() ;
150 : : }
151 : : }
152 : :
153 : 0 : ::com::sun::star::uno::Sequence< sal_Int8 > SAL_CALL X509Certificate_NssImpl :: getIssuerUniqueID() throw ( ::com::sun::star::uno::RuntimeException) {
154 [ # # ][ # # ]: 0 : if( m_pCert != NULL && m_pCert->issuerID.len > 0 ) {
155 [ # # ]: 0 : Sequence< sal_Int8 > issuerUid( m_pCert->issuerID.len ) ;
156 [ # # ]: 0 : for( unsigned int i = 0 ; i < m_pCert->issuerID.len ; i ++ )
157 [ # # ]: 0 : issuerUid[i] = *( m_pCert->issuerID.data + i ) ;
158 : :
159 [ # # ][ # # ]: 0 : return issuerUid ;
160 : : } else {
161 : 0 : return ::com::sun::star::uno::Sequence< sal_Int8 >();
162 : : }
163 : : }
164 : :
165 : 0 : ::com::sun::star::uno::Sequence< sal_Int8 > SAL_CALL X509Certificate_NssImpl :: getSubjectUniqueID() throw ( ::com::sun::star::uno::RuntimeException) {
166 [ # # ][ # # ]: 0 : if( m_pCert != NULL && m_pCert->subjectID.len > 0 ) {
167 [ # # ]: 0 : Sequence< sal_Int8 > subjectUid( m_pCert->subjectID.len ) ;
168 [ # # ]: 0 : for( unsigned int i = 0 ; i < m_pCert->subjectID.len ; i ++ )
169 [ # # ]: 0 : subjectUid[i] = *( m_pCert->subjectID.data + i ) ;
170 : :
171 [ # # ][ # # ]: 0 : return subjectUid ;
172 : : } else {
173 : 0 : return ::com::sun::star::uno::Sequence< sal_Int8 >();
174 : : }
175 : : }
176 : :
177 : 0 : ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Reference< ::com::sun::star::security::XCertificateExtension > > SAL_CALL X509Certificate_NssImpl :: getExtensions() throw ( ::com::sun::star::uno::RuntimeException) {
178 [ # # ][ # # ]: 0 : if( m_pCert != NULL && m_pCert->extensions != NULL ) {
179 : : CERTCertExtension** extns ;
180 : : CertificateExtension_XmlSecImpl* pExtn ;
181 : : sal_Bool crit ;
182 : : int len ;
183 : :
184 [ # # ]: 0 : for( len = 0, extns = m_pCert->extensions; *extns != NULL; len ++, extns ++ ) ;
185 [ # # ]: 0 : Sequence< Reference< XCertificateExtension > > xExtns( len ) ;
186 : :
187 [ # # ]: 0 : for( extns = m_pCert->extensions, len = 0; *extns != NULL; extns ++, len ++ ) {
188 : 0 : const SECItem id = (*extns)->id;
189 [ # # ]: 0 : ::rtl::OString oidString(CERT_GetOidString(&id));
190 : :
191 : : // remove "OID." prefix if existing
192 : 0 : ::rtl::OString objID;
193 : 0 : ::rtl::OString oid("OID.");
194 [ # # ]: 0 : if (oidString.match(oid))
195 : 0 : objID = oidString.copy(oid.getLength());
196 : : else
197 : 0 : objID = oidString;
198 : :
199 [ # # ]: 0 : if ( objID.equals("2.5.29.17") )
200 [ # # ]: 0 : pExtn = (CertificateExtension_XmlSecImpl*) new SanExtensionImpl() ;
201 : : else
202 [ # # ]: 0 : pExtn = new CertificateExtension_XmlSecImpl() ;
203 : :
204 [ # # ]: 0 : if( (*extns)->critical.data == NULL )
205 : 0 : crit = sal_False ;
206 : : else
207 [ # # ]: 0 : crit = ( (*extns)->critical.data[0] == 0xFF ) ? sal_True : sal_False ;
208 [ # # ]: 0 : pExtn->setCertExtn( (*extns)->value.data, (*extns)->value.len, (unsigned char*)objID.getStr(), objID.getLength(), crit ) ;
209 : :
210 [ # # ][ # # ]: 0 : xExtns[len] = pExtn ;
[ # # ]
211 : 0 : }
212 : :
213 [ # # ][ # # ]: 0 : return xExtns ;
214 : : } else {
215 : 0 : return ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Reference< ::com::sun::star::security::XCertificateExtension > > ();
216 : : }
217 : : }
218 : :
219 : 0 : ::com::sun::star::uno::Reference< ::com::sun::star::security::XCertificateExtension > SAL_CALL X509Certificate_NssImpl :: findCertificateExtension( const ::com::sun::star::uno::Sequence< sal_Int8 >& oid ) throw (::com::sun::star::uno::RuntimeException) {
220 [ # # ][ # # ]: 0 : if( m_pCert != NULL && m_pCert->extensions != NULL ) {
221 : : CertificateExtension_XmlSecImpl* pExtn ;
222 : : CERTCertExtension** extns ;
223 : : SECItem idItem ;
224 : : sal_Bool crit ;
225 : :
226 : 0 : idItem.data = ( unsigned char* )&oid[0] ;
227 : 0 : idItem.len = oid.getLength() ;
228 : :
229 : 0 : pExtn = NULL ;
230 [ # # ]: 0 : for( extns = m_pCert->extensions; *extns != NULL; extns ++ ) {
231 [ # # ][ # # ]: 0 : if( SECITEM_CompareItem( &idItem, &(*extns)->id ) == SECEqual ) {
232 : 0 : const SECItem id = (*extns)->id;
233 [ # # ]: 0 : ::rtl::OString objId(CERT_GetOidString(&id));
234 [ # # ]: 0 : if ( objId.equals("OID.2.5.29.17") )
235 [ # # ]: 0 : pExtn = (CertificateExtension_XmlSecImpl*) new SanExtensionImpl() ;
236 : : else
237 [ # # ]: 0 : pExtn = new CertificateExtension_XmlSecImpl() ;
238 [ # # ]: 0 : if( (*extns)->critical.data == NULL )
239 : 0 : crit = sal_False ;
240 : : else
241 [ # # ]: 0 : crit = ( (*extns)->critical.data[0] == 0xFF ) ? sal_True : sal_False ;
242 [ # # ]: 0 : pExtn->setCertExtn( (*extns)->value.data, (*extns)->value.len, (*extns)->id.data, (*extns)->id.len, crit ) ;
243 : : }
244 : : }
245 : :
246 [ # # ][ # # ]: 0 : return pExtn ;
247 : : } else {
248 : 0 : return NULL ;
249 : : }
250 : : }
251 : :
252 : :
253 : 0 : ::com::sun::star::uno::Sequence< sal_Int8 > SAL_CALL X509Certificate_NssImpl :: getEncoded() throw ( ::com::sun::star::uno::RuntimeException) {
254 [ # # ][ # # ]: 0 : if( m_pCert != NULL && m_pCert->derCert.len > 0 ) {
255 [ # # ]: 0 : Sequence< sal_Int8 > rawCert( m_pCert->derCert.len ) ;
256 : :
257 [ # # ]: 0 : for( unsigned int i = 0 ; i < m_pCert->derCert.len ; i ++ )
258 [ # # ]: 0 : rawCert[i] = *( m_pCert->derCert.data + i ) ;
259 : :
260 [ # # ][ # # ]: 0 : return rawCert ;
261 : : } else {
262 : 0 : return ::com::sun::star::uno::Sequence< sal_Int8 >();
263 : : }
264 : : }
265 : :
266 : : //Helper methods
267 : 0 : void X509Certificate_NssImpl :: setCert( CERTCertificate* cert ) {
268 [ # # ]: 0 : if( m_pCert != NULL ) {
269 : 0 : CERT_DestroyCertificate( m_pCert ) ;
270 : 0 : m_pCert = NULL ;
271 : : }
272 : :
273 [ # # ]: 0 : if( cert != NULL ) {
274 : 0 : m_pCert = CERT_DupCertificate( cert ) ;
275 : : }
276 : 0 : }
277 : :
278 : 0 : const CERTCertificate* X509Certificate_NssImpl :: getNssCert() const {
279 [ # # ]: 0 : if( m_pCert != NULL ) {
280 : 0 : return m_pCert ;
281 : : } else {
282 : 0 : return NULL ;
283 : : }
284 : : }
285 : :
286 : 0 : void X509Certificate_NssImpl :: setRawCert( Sequence< sal_Int8 > rawCert ) throw ( ::com::sun::star::uno::RuntimeException) {
287 : : CERTCertificate* cert ;
288 : : SECItem certItem ;
289 : :
290 [ # # ]: 0 : certItem.data = ( unsigned char* )&rawCert[0] ;
291 : 0 : certItem.len = rawCert.getLength() ;
292 : :
293 [ # # ]: 0 : cert = CERT_DecodeDERCertificate( &certItem, PR_TRUE, NULL ) ;
294 [ # # ]: 0 : if( cert == NULL )
295 [ # # ]: 0 : throw RuntimeException() ;
296 : :
297 [ # # ]: 0 : if( m_pCert != NULL ) {
298 [ # # ]: 0 : CERT_DestroyCertificate( m_pCert ) ;
299 : 0 : m_pCert = NULL ;
300 : : }
301 : :
302 : 0 : m_pCert = cert ;
303 : 0 : }
304 : :
305 : : /* XUnoTunnel */
306 : 0 : sal_Int64 SAL_CALL X509Certificate_NssImpl :: getSomething( const Sequence< sal_Int8 >& aIdentifier ) throw( RuntimeException ) {
307 [ # # ][ # # ]: 0 : if( aIdentifier.getLength() == 16 && 0 == memcmp( getUnoTunnelId().getConstArray(), aIdentifier.getConstArray(), 16 ) ) {
[ # # ]
308 : 0 : return sal::static_int_cast<sal_Int64>(reinterpret_cast<sal_uIntPtr>(this));
309 : : }
310 : 0 : return 0 ;
311 : : }
312 : :
313 : : /* XUnoTunnel extension */
314 : :
315 : : namespace
316 : : {
317 : : class theX509Certificate_NssImplUnoTunnelId : public rtl::Static< UnoTunnelIdInit, theX509Certificate_NssImplUnoTunnelId > {};
318 : : }
319 : :
320 : 0 : const Sequence< sal_Int8>& X509Certificate_NssImpl :: getUnoTunnelId() {
321 : 0 : return theX509Certificate_NssImplUnoTunnelId::get().getSeq();
322 : : }
323 : :
324 : 0 : ::rtl::OUString getAlgorithmDescription(SECAlgorithmID *aid)
325 : : {
326 : : SECOidTag tag;
327 : 0 : tag = SECOID_GetAlgorithmTag(aid);
328 : :
329 : 0 : const char *pDesc = SECOID_FindOIDTagDescription(tag);
330 : :
331 : 0 : return rtl::OUString::createFromAscii( pDesc ) ;
332 : : }
333 : :
334 : 0 : ::com::sun::star::uno::Sequence< sal_Int8 > getThumbprint(CERTCertificate *pCert, SECOidTag id)
335 : : {
336 [ # # ]: 0 : if( pCert != NULL )
337 : : {
338 : : unsigned char fingerprint[20];
339 [ # # ]: 0 : int length = ((id == SEC_OID_MD5)?MD5_LENGTH:SHA1_LENGTH);
340 : :
341 : 0 : memset(fingerprint, 0, sizeof fingerprint);
342 [ # # ]: 0 : PK11_HashBuf(id, fingerprint, pCert->derCert.data, pCert->derCert.len);
343 : :
344 [ # # ]: 0 : Sequence< sal_Int8 > thumbprint( length ) ;
345 [ # # ]: 0 : for( int i = 0 ; i < length ; i ++ )
346 [ # # ]: 0 : thumbprint[i] = fingerprint[i];
347 : :
348 [ # # ][ # # ]: 0 : return thumbprint;
349 : : }
350 : : else
351 : : {
352 : 0 : return ::com::sun::star::uno::Sequence< sal_Int8 >();
353 : : }
354 : : }
355 : :
356 : 0 : ::rtl::OUString SAL_CALL X509Certificate_NssImpl::getSubjectPublicKeyAlgorithm()
357 : : throw ( ::com::sun::star::uno::RuntimeException)
358 : : {
359 [ # # ]: 0 : if( m_pCert != NULL )
360 : : {
361 : 0 : return getAlgorithmDescription(&(m_pCert->subjectPublicKeyInfo.algorithm));
362 : : }
363 : : else
364 : : {
365 : 0 : return OUString() ;
366 : : }
367 : : }
368 : :
369 : 0 : ::com::sun::star::uno::Sequence< sal_Int8 > SAL_CALL X509Certificate_NssImpl::getSubjectPublicKeyValue()
370 : : throw ( ::com::sun::star::uno::RuntimeException)
371 : : {
372 [ # # ]: 0 : if( m_pCert != NULL )
373 : : {
374 : 0 : SECItem spk = m_pCert->subjectPublicKeyInfo.subjectPublicKey;
375 : 0 : DER_ConvertBitString(&spk);
376 : :
377 [ # # ]: 0 : if ( spk.len>0)
378 : : {
379 [ # # ]: 0 : Sequence< sal_Int8 > key( spk.len ) ;
380 [ # # ]: 0 : for( unsigned int i = 0 ; i < spk.len ; i ++ )
381 : : {
382 [ # # ]: 0 : key[i] = *( spk.data + i ) ;
383 : : }
384 : :
385 [ # # ][ # # ]: 0 : return key ;
386 : : }
387 : : }
388 : :
389 : 0 : return ::com::sun::star::uno::Sequence< sal_Int8 >();
390 : : }
391 : :
392 : 0 : ::rtl::OUString SAL_CALL X509Certificate_NssImpl::getSignatureAlgorithm()
393 : : throw ( ::com::sun::star::uno::RuntimeException)
394 : : {
395 [ # # ]: 0 : if( m_pCert != NULL )
396 : : {
397 : 0 : return getAlgorithmDescription(&(m_pCert->signature));
398 : : }
399 : : else
400 : : {
401 : 0 : return OUString() ;
402 : : }
403 : : }
404 : :
405 : 0 : ::com::sun::star::uno::Sequence< sal_Int8 > SAL_CALL X509Certificate_NssImpl::getSHA1Thumbprint()
406 : : throw ( ::com::sun::star::uno::RuntimeException)
407 : : {
408 : 0 : return getThumbprint(m_pCert, SEC_OID_SHA1);
409 : : }
410 : :
411 : 0 : ::com::sun::star::uno::Sequence< sal_Int8 > SAL_CALL X509Certificate_NssImpl::getMD5Thumbprint()
412 : : throw ( ::com::sun::star::uno::RuntimeException)
413 : : {
414 : 0 : return getThumbprint(m_pCert, SEC_OID_MD5);
415 : : }
416 : :
417 : 0 : sal_Int32 SAL_CALL X509Certificate_NssImpl::getCertificateUsage( )
418 : : throw ( ::com::sun::star::uno::RuntimeException)
419 : : {
420 : : SECStatus rv;
421 : : SECItem tmpitem;
422 : : sal_Int32 usage;
423 : :
424 [ # # ]: 0 : rv = CERT_FindKeyUsageExtension(m_pCert, &tmpitem);
425 [ # # ]: 0 : if ( rv == SECSuccess )
426 : : {
427 : 0 : usage = tmpitem.data[0];
428 [ # # ]: 0 : PORT_Free(tmpitem.data);
429 : 0 : tmpitem.data = NULL;
430 : : }
431 : : else
432 : : {
433 : 0 : usage = KU_ALL;
434 : : }
435 : :
436 : : /*
437 : : * to make the nss implementation compatible with MSCrypto,
438 : : * the following usage is ignored
439 : : *
440 : : *
441 : : if ( CERT_GovtApprovedBitSet(m_pCert) )
442 : : {
443 : : usage |= KU_NS_GOVT_APPROVED;
444 : : }
445 : : */
446 : :
447 : 0 : return usage;
448 : : }
449 : :
450 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|