1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2000, 2010 Oracle and/or its affiliates.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * This file is part of OpenOffice.org.
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.
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).
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.
26 ************************************************************************/
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_xmlsecurity.hxx"
31 #include "resourcemanager.hxx"
33 #include <vcl/svapp.hxx>
34 #include <vcl/fixed.hxx>
35 #include <svtools/stdctrl.hxx>
36 #include <svl/solar.hrc>
37 #include <unotools/syslocale.hxx>
38 #include <rtl/ustring.h>
39 #include <rtl/ustrbuf.h>
42 using ::rtl::OUString
;
47 static ResMgr
* pResMgr
= 0;
48 static SvtSysLocale
* pSysLocale
= 0;
50 ResMgr
* GetResMgr( void )
54 ByteString
aName( "xmlsec" );
55 // pResMgr = ResMgr::CreateResMgr( aName.GetBuffer(), Application::GetSettings().GetUILanguage() );
56 // LanguageType aLang( LANGUAGE_ENGLISH_US );
57 // pResMgr = ResMgr::CreateResMgr( aName.GetBuffer(), aLang );
58 // MT: Change to Locale
59 pResMgr
= ResMgr::CreateResMgr( aName
.GetBuffer() );
65 const LocaleDataWrapper
& GetLocaleData( void )
68 pSysLocale
= new SvtSysLocale
;
69 return pSysLocale
->GetLocaleData();
72 DateTime
GetDateTime( const ::com::sun::star::util::DateTime
& _rDT
)
75 Date( _rDT
.Day
, _rDT
.Month
, _rDT
.Year
),
76 Time( _rDT
.Hours
, _rDT
.Minutes
, _rDT
.Seconds
, _rDT
.HundredthSeconds
) );
79 String
GetDateTimeString( const ::com::sun::star::util::DateTime
& _rDT
)
81 // --> PB 2004-10-12 #i20172# String with date and time information
82 DateTime
aDT( GetDateTime( _rDT
) );
83 const LocaleDataWrapper
& rLoDa
= GetLocaleData();
84 String
sRet( rLoDa
.getDate( aDT
) );
86 sRet
+= rLoDa
.getTime( aDT
);
90 String
GetDateTimeString( const rtl::OUString
& _rDate
, const rtl::OUString
& _rTime
)
92 String
sDay( _rDate
, 6, 2 );
93 String
sMonth( _rDate
, 4, 2 );
94 String
sYear( _rDate
, 0, 4 );
96 String
sHour( _rTime
, 0, 2 );
97 String
sMin( _rTime
, 4, 2 );
98 String
sSec( _rTime
, 6, 2 );
101 Date
aDate( (USHORT
)sDay
.ToInt32(), (USHORT
) sMonth
.ToInt32(), (USHORT
)sYear
.ToInt32() );
102 Time
aTime( sHour
.ToInt32(), sMin
.ToInt32(), sSec
.ToInt32(), 0 );
103 const LocaleDataWrapper
& rLoDa
= GetLocaleData();
104 String
aStr( rLoDa
.getDate( aDate
) );
105 aStr
.AppendAscii( " " );
106 aStr
+= rLoDa
.getTime( aTime
);
110 String
GetDateString( const ::com::sun::star::util::DateTime
& _rDT
)
112 return GetLocaleData().getDate( GetDateTime( _rDT
) );
116 Creates two strings based on the distinguished name which are displayed in the
117 certificate details view. The first string contains only the values of the attribute
118 and valudes pairs, which are separated by commas. All escape characters ('"') are
120 The second string is for the details view at the bottom. It shows the attribute/value
121 pairs on different lines. All escape characters ('"') are removed.
123 pair
< OUString
, OUString
> GetDNForCertDetailsView( const OUString
& rRawString
)
125 vector
< pair
< OUString
, OUString
> > vecAttrValueOfDN
= parseDN(rRawString
);
126 ::rtl::OUStringBuffer s1
, s2
;
127 OUString
sEqual(RTL_CONSTASCII_USTRINGPARAM(" = "));
128 typedef vector
< pair
< OUString
, OUString
> >::const_iterator CIT
;
129 for (CIT i
= vecAttrValueOfDN
.begin(); i
< vecAttrValueOfDN
.end(); i
++)
131 if (i
!= vecAttrValueOfDN
.begin())
133 s1
.append(static_cast<sal_Unicode
>(','));
134 s2
.append(static_cast<sal_Unicode
>('\n'));
136 s1
.append(i
->second
);
139 s2
.append(i
->second
);
141 return make_pair(s1
.makeStringAndClear(), s2
.makeStringAndClear());
145 Whenever the attribute value contains special characters, such as '"' or ',' (without '')
146 then the value will be enclosed in double quotes by the respective Windows or NSS function
147 which we use to retrieve, for example, the subject name. If double quotes appear in the value then
148 they are escaped with a double quote. This function removes the escape characters.
151 vector
< pair
< OUString
, OUString
> > parseDN(const OUString
& rRawString
)
153 vector
< pair
<OUString
, OUString
> > retVal
;
154 bool bInEscape
= false;
155 bool bInValue
= false;
157 sal_Int32 nTypeNameStart
= 0;
159 ::rtl::OUStringBuffer sbufValue
;
160 sal_Int32 length
= rRawString
.getLength();
162 for (sal_Int32 i
= 0; i
< length
; i
++)
164 sal_Unicode c
= rRawString
[i
];
170 sType
= rRawString
.copy(nTypeNameStart
, i
- nTypeNameStart
);
171 sType
= sType
.trim();
183 //If this is the quote is the first of the couple which enclose the
184 //whole value, because the value contains special characters
185 //then we just drop it. That is, this character must be followed by
186 //a character which is not '"'.
187 if ( i
+ 1 < length
&& rRawString
[i
+1] == '"')
190 bInValue
= !bInValue
; //value is enclosed in " "
194 //This quote is escaped by a preceding quote and therefore is
200 else if (c
== ',' || c
== '+')
202 //The comma separate the attribute value pairs.
203 //If the comma is not part of a value (the value would then be enclosed in '"'),
204 //then we have reached the end of the value
207 OSL_ASSERT(sType
.getLength());
208 retVal
.push_back(make_pair(sType
, sbufValue
.makeStringAndClear()));
210 //The next char is the start of the new type
211 nTypeNameStart
= i
+ 1;
216 //The whole string is enclosed because it contains special characters.
217 //The enclosing '"' are not part of certificate but will be added by
218 //the function (Windows or NSS) which retrieves DN
228 if (sbufValue
.getLength())
230 OSL_ASSERT(sType
.getLength());
231 retVal
.push_back(make_pair(sType
, sbufValue
.makeStringAndClear()));
236 vector
< pair
< OUString
, OUString
> > parseDN(const OUString
& rRawString
)
238 vector
< pair
<OUString
, OUString
> > retVal
;
239 //bInEscape == true means that the preceding character is an escape character
240 bool bInEscape
= false;
241 bool bInValue
= false;
243 sal_Int32 nTypeNameStart
= 0;
245 ::rtl::OUStringBuffer sbufValue
;
246 sal_Int32 length
= rRawString
.getLength();
248 for (sal_Int32 i
= 0; i
< length
; i
++)
250 sal_Unicode c
= rRawString
[i
];
256 sType
= rRawString
.copy(nTypeNameStart
, i
- nTypeNameStart
);
257 sType
= sType
.trim();
272 { // bInEscape is true
279 //an unescaped '"' is either at the beginning or end of the value
289 //This quote is escaped by a preceding quote and therefore is
295 else if (c
== ',' || c
== '+')
297 //The comma separate the attribute value pairs.
298 //If the comma is not part of a value (the value would then be enclosed in '"'),
299 //then we have reached the end of the value
302 OSL_ASSERT(sType
.getLength());
303 retVal
.push_back(make_pair(sType
, sbufValue
.makeStringAndClear()));
305 //The next char is the start of the new type
306 nTypeNameStart
= i
+ 1;
311 //The whole string is enclosed because it contains special characters.
312 //The enclosing '"' are not part of certificate but will be added by
313 //the function (Windows or NSS) which retrieves DN
326 if (sbufValue
.getLength())
328 OSL_ASSERT(sType
.getLength());
329 retVal
.push_back(make_pair(sType
, sbufValue
.makeStringAndClear()));
336 String
GetContentPart( const String
& _rRawString
)
338 char const * aIDs
[] = { "CN", "OU", "O", "E", NULL
};
341 vector
< pair
< OUString
, OUString
> > vecAttrValueOfDN
= parseDN(_rRawString
);
344 OUString sPartId
= OUString::createFromAscii( aIDs
[i
++] );
345 typedef vector
< pair
< OUString
, OUString
> >::const_iterator CIT
;
346 for (CIT idn
= vecAttrValueOfDN
.begin(); idn
!= vecAttrValueOfDN
.end(); idn
++)
348 if (idn
->first
.equals(sPartId
))
350 retVal
= idn
->second
;
354 if (retVal
.getLength())
360 String
GetHexString( const ::com::sun::star::uno::Sequence
< sal_Int8
>& _rSeq
, const char* _pSep
, UINT16 _nLineBreak
)
362 const sal_Int8
* pSerNumSeq
= _rSeq
.getConstArray();
363 int nCnt
= _rSeq
.getLength();
365 const char pHexDigs
[ 17 ] = "0123456789ABCDEF";
366 char pBuffer
[ 3 ] = " ";
368 UINT16 nBreakStart
= _nLineBreak
? _nLineBreak
: 1;
369 UINT16 nBreak
= nBreakStart
;
370 for( int i
= 0 ; i
< nCnt
; ++i
)
372 nNum
= UINT8( pSerNumSeq
[ i
] );
374 //MM : exchange the buffer[0] and buffer[1], which make it consistent with Mozilla and Windows
375 pBuffer
[ 1 ] = pHexDigs
[ nNum
& 0x0F ];
377 pBuffer
[ 0 ] = pHexDigs
[ nNum
];
378 aStr
.AppendAscii( pBuffer
);
382 aStr
.AppendAscii( _pSep
);
385 nBreak
= nBreakStart
;
386 aStr
.AppendAscii( "\n" );
393 long ShrinkToFitWidth( Control
& _rCtrl
, long _nOffs
)
395 long nWidth
= _rCtrl
.GetTextWidth( _rCtrl
.GetText() );
396 Size
aSize( _rCtrl
.GetSizePixel() );
398 aSize
.Width() = nWidth
;
399 _rCtrl
.SetSizePixel( aSize
);
403 void AlignAfterImage( const FixedImage
& _rImage
, Control
& _rCtrl
, long _nXOffset
)
405 Point
aPos( _rImage
.GetPosPixel() );
406 Size
aSize( _rImage
.GetSizePixel() );
412 n
+= aSize
.Height() / 2; // y-position is in the middle of the image
413 n
-= _rCtrl
.GetSizePixel().Height() / 2; // center Control
415 _rCtrl
.SetPosPixel( aPos
);
418 void AlignAfterImage( const FixedImage
& _rImage
, FixedInfo
& _rFI
, long _nXOffset
)
420 AlignAfterImage( _rImage
, static_cast< Control
& >( _rFI
), _nXOffset
);
421 ShrinkToFitWidth( _rFI
);
424 void AlignAndFitImageAndControl( FixedImage
& _rImage
, FixedInfo
& _rFI
, long _nXOffset
)
426 _rImage
.SetSizePixel( _rImage
.GetImage().GetSizePixel() );
427 AlignAfterImage( _rImage
, _rFI
, _nXOffset
);