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 :
21 : #include "resourcemanager.hxx"
22 :
23 : #include <vcl/svapp.hxx>
24 : #include <vcl/fixed.hxx>
25 : #include <svtools/stdctrl.hxx>
26 : #include <svl/solar.hrc>
27 : #include <unotools/syslocale.hxx>
28 : #include <rtl/ustring.h>
29 : #include <rtl/ustrbuf.h>
30 : #include <vector>
31 :
32 : using namespace std;
33 :
34 : namespace XmlSec
35 : {
36 : static ResMgr* pResMgr = 0;
37 : static SvtSysLocale* pSysLocale = 0;
38 :
39 0 : ResMgr* GetResMgr( void )
40 : {
41 0 : if (!pResMgr)
42 0 : pResMgr = ResMgr::CreateResMgr("xmlsec");
43 0 : return pResMgr;
44 : }
45 :
46 0 : const LocaleDataWrapper& GetLocaleData( void )
47 : {
48 0 : if (!pSysLocale)
49 0 : pSysLocale = new SvtSysLocale;
50 0 : return pSysLocale->GetLocaleData();
51 : }
52 :
53 0 : DateTime GetDateTime( const ::com::sun::star::util::DateTime& _rDT )
54 : {
55 : return DateTime(
56 : Date( _rDT.Day, _rDT.Month, _rDT.Year ),
57 0 : Time( _rDT.Hours, _rDT.Minutes, _rDT.Seconds, _rDT.HundredthSeconds ) );
58 : }
59 :
60 0 : OUString GetDateTimeString( const ::com::sun::star::util::DateTime& _rDT )
61 : {
62 : // String with date and time information (#i20172#)
63 0 : DateTime aDT( GetDateTime( _rDT ) );
64 0 : const LocaleDataWrapper& rLoDa = GetLocaleData();
65 0 : OUStringBuffer sRet( rLoDa.getDate( aDT ) );
66 0 : sRet.append( ' ' );
67 0 : sRet.append( rLoDa.getTime( aDT ) );
68 0 : return sRet.makeStringAndClear();
69 : }
70 :
71 0 : OUString GetDateString( const ::com::sun::star::util::DateTime& _rDT )
72 : {
73 0 : return GetLocaleData().getDate( GetDateTime( _rDT ) );
74 : }
75 :
76 : /*
77 : Creates two strings based on the distinguished name which are displayed in the
78 : certificate details view. The first string contains only the values of the attribute
79 : and valudes pairs, which are separated by commas. All escape characters ('"') are
80 : removed.
81 : The second string is for the details view at the bottom. It shows the attribute/value
82 : pairs on different lines. All escape characters ('"') are removed.
83 : */
84 0 : pair< OUString, OUString> GetDNForCertDetailsView( const OUString & rRawString)
85 : {
86 0 : vector< pair< OUString, OUString > > vecAttrValueOfDN = parseDN(rRawString);
87 0 : ::rtl::OUStringBuffer s1, s2;
88 0 : OUString sEqual(RTL_CONSTASCII_USTRINGPARAM(" = "));
89 : typedef vector< pair < OUString, OUString > >::const_iterator CIT;
90 0 : for (CIT i = vecAttrValueOfDN.begin(); i < vecAttrValueOfDN.end(); ++i)
91 : {
92 0 : if (i != vecAttrValueOfDN.begin())
93 : {
94 0 : s1.append(static_cast<sal_Unicode>(','));
95 0 : s2.append(static_cast<sal_Unicode>('\n'));
96 : }
97 0 : s1.append(i->second);
98 0 : s2.append(i->first);
99 0 : s2.append(sEqual);
100 0 : s2.append(i->second);
101 : }
102 0 : return make_pair(s1.makeStringAndClear(), s2.makeStringAndClear());
103 : }
104 :
105 : /*
106 : Whenever the attribute value contains special characters, such as '"' or ',' (without '')
107 : then the value will be enclosed in double quotes by the respective Windows or NSS function
108 : which we use to retrieve, for example, the subject name. If double quotes appear in the value then
109 : they are escaped with a double quote. This function removes the escape characters.
110 : */
111 : #ifdef WNT
112 : vector< pair< OUString, OUString> > parseDN(const OUString& rRawString)
113 : {
114 : vector< pair<OUString, OUString> > retVal;
115 : bool bInEscape = false;
116 : bool bInValue = false;
117 : bool bInType = true;
118 : sal_Int32 nTypeNameStart = 0;
119 : OUString sType;
120 : ::rtl::OUStringBuffer sbufValue;
121 : sal_Int32 length = rRawString.getLength();
122 :
123 : for (sal_Int32 i = 0; i < length; i++)
124 : {
125 : sal_Unicode c = rRawString[i];
126 :
127 : if (c == '=')
128 : {
129 : if (! bInValue)
130 : {
131 : sType = rRawString.copy(nTypeNameStart, i - nTypeNameStart);
132 : sType = sType.trim();
133 : bInType = false;
134 : }
135 : else
136 : {
137 : sbufValue.append(c);
138 : }
139 : }
140 : else if (c == '"')
141 : {
142 : if (!bInEscape)
143 : {
144 : //If this is the quote is the first of the couple which enclose the
145 : //whole value, because the value contains special characters
146 : //then we just drop it. That is, this character must be followed by
147 : //a character which is not '"'.
148 : if ( i + 1 < length && rRawString[i+1] == '"')
149 : bInEscape = true;
150 : else
151 : bInValue = !bInValue; //value is enclosed in " "
152 : }
153 : else
154 : {
155 : //This quote is escaped by a preceding quote and therefore is
156 : //part of the value
157 : sbufValue.append(c);
158 : bInEscape = false;
159 : }
160 : }
161 : else if (c == ',' || c == '+')
162 : {
163 : //The comma separate the attribute value pairs.
164 : //If the comma is not part of a value (the value would then be enclosed in '"'),
165 : //then we have reached the end of the value
166 : if (!bInValue)
167 : {
168 : OSL_ASSERT(!sType.isEmpty());
169 : retVal.push_back(make_pair(sType, sbufValue.makeStringAndClear()));
170 : sType = OUString();
171 : //The next char is the start of the new type
172 : nTypeNameStart = i + 1;
173 : bInType = true;
174 : }
175 : else
176 : {
177 : //The whole string is enclosed because it contains special characters.
178 : //The enclosing '"' are not part of certificate but will be added by
179 : //the function (Windows or NSS) which retrieves DN
180 : sbufValue.append(c);
181 : }
182 : }
183 : else
184 : {
185 : if (!bInType)
186 : sbufValue.append(c);
187 : }
188 : }
189 : if (sbufValue.getLength())
190 : {
191 : OSL_ASSERT(!sType.isEmpty());
192 : retVal.push_back(make_pair(sType, sbufValue.makeStringAndClear()));
193 : }
194 : return retVal;
195 : }
196 : #else
197 0 : vector< pair< OUString, OUString> > parseDN(const OUString& rRawString)
198 : {
199 0 : vector< pair<OUString, OUString> > retVal;
200 : //bInEscape == true means that the preceding character is an escape character
201 0 : bool bInEscape = false;
202 0 : bool bInValue = false;
203 0 : bool bInType = true;
204 0 : sal_Int32 nTypeNameStart = 0;
205 0 : OUString sType;
206 0 : ::rtl::OUStringBuffer sbufValue;
207 0 : sal_Int32 length = rRawString.getLength();
208 :
209 0 : for (sal_Int32 i = 0; i < length; i++)
210 : {
211 0 : sal_Unicode c = rRawString[i];
212 :
213 0 : if (c == '=')
214 : {
215 0 : if (! bInValue)
216 : {
217 0 : sType = rRawString.copy(nTypeNameStart, i - nTypeNameStart);
218 0 : sType = sType.trim();
219 0 : bInType = false;
220 : }
221 : else
222 : {
223 0 : sbufValue.append(c);
224 : }
225 : }
226 0 : else if (c == '\\')
227 : {
228 0 : if (!bInEscape)
229 : {
230 0 : bInEscape = true;
231 : }
232 : else
233 : { // bInEscape is true
234 0 : sbufValue.append(c);
235 0 : bInEscape = false;
236 : }
237 : }
238 0 : else if (c == '"')
239 : {
240 : //an unescaped '"' is either at the beginning or end of the value
241 0 : if (!bInEscape)
242 : {
243 0 : if ( !bInValue)
244 0 : bInValue = true;
245 0 : else if (bInValue)
246 0 : bInValue = false;
247 : }
248 : else
249 : {
250 : //This quote is escaped by a preceding quote and therefore is
251 : //part of the value
252 0 : sbufValue.append(c);
253 0 : bInEscape = false;
254 : }
255 : }
256 0 : else if (c == ',' || c == '+')
257 : {
258 : //The comma separate the attribute value pairs.
259 : //If the comma is not part of a value (the value would then be enclosed in '"'),
260 : //then we have reached the end of the value
261 0 : if (!bInValue)
262 : {
263 : OSL_ASSERT(!sType.isEmpty());
264 0 : retVal.push_back(make_pair(sType, sbufValue.makeStringAndClear()));
265 0 : sType = OUString();
266 : //The next char is the start of the new type
267 0 : nTypeNameStart = i + 1;
268 0 : bInType = true;
269 : }
270 : else
271 : {
272 : //The whole string is enclosed because it contains special characters.
273 : //The enclosing '"' are not part of certificate but will be added by
274 : //the function (Windows or NSS) which retrieves DN
275 0 : sbufValue.append(c);
276 : }
277 : }
278 : else
279 : {
280 0 : if (!bInType)
281 : {
282 0 : sbufValue.append(c);
283 0 : bInEscape = false;
284 : }
285 : }
286 : }
287 0 : if (sbufValue.getLength())
288 : {
289 : OSL_ASSERT(!sType.isEmpty());
290 0 : retVal.push_back(make_pair(sType, sbufValue.makeStringAndClear()));
291 : }
292 0 : return retVal;
293 : }
294 :
295 : #endif
296 :
297 0 : OUString GetContentPart( const OUString& _rRawString )
298 : {
299 0 : char const * aIDs[] = { "CN", "OU", "O", "E", NULL };
300 0 : OUString retVal;
301 0 : int i = 0;
302 0 : vector< pair< OUString, OUString > > vecAttrValueOfDN = parseDN(_rRawString);
303 0 : while ( aIDs[i] )
304 : {
305 0 : OUString sPartId = OUString::createFromAscii( aIDs[i++] );
306 : typedef vector< pair < OUString, OUString > >::const_iterator CIT;
307 0 : for (CIT idn = vecAttrValueOfDN.begin(); idn != vecAttrValueOfDN.end(); ++idn)
308 : {
309 0 : if (idn->first.equals(sPartId))
310 : {
311 0 : retVal = idn->second;
312 0 : break;
313 : }
314 : }
315 0 : if (!retVal.isEmpty())
316 : break;
317 0 : }
318 0 : return retVal;
319 : }
320 :
321 0 : OUString GetHexString( const ::com::sun::star::uno::Sequence< sal_Int8 >& _rSeq, const char* _pSep, sal_uInt16 _nLineBreak )
322 : {
323 0 : const sal_Int8* pSerNumSeq = _rSeq.getConstArray();
324 0 : int nCnt = _rSeq.getLength();
325 0 : OUStringBuffer aStr;
326 0 : const char pHexDigs[ 17 ] = "0123456789ABCDEF";
327 0 : char pBuffer[ 3 ] = " ";
328 : sal_uInt8 nNum;
329 0 : sal_uInt16 nBreakStart = _nLineBreak? _nLineBreak : 1;
330 0 : sal_uInt16 nBreak = nBreakStart;
331 0 : for( int i = 0 ; i < nCnt ; ++i )
332 : {
333 0 : nNum = sal_uInt8( pSerNumSeq[ i ] );
334 :
335 : // exchange the buffer[0] and buffer[1], which make it consistent with Mozilla and Windows
336 0 : pBuffer[ 1 ] = pHexDigs[ nNum & 0x0F ];
337 0 : nNum >>= 4;
338 0 : pBuffer[ 0 ] = pHexDigs[ nNum ];
339 0 : aStr.appendAscii( pBuffer );
340 :
341 0 : --nBreak;
342 0 : if( nBreak )
343 0 : aStr.appendAscii( _pSep );
344 : else
345 : {
346 0 : nBreak = nBreakStart;
347 0 : aStr.append( '\n' );
348 : }
349 : }
350 :
351 0 : return aStr.makeStringAndClear();
352 : }
353 :
354 0 : long ShrinkToFitWidth( Control& _rCtrl, long _nOffs )
355 : {
356 0 : long nWidth = _rCtrl.GetTextWidth( _rCtrl.GetText() );
357 0 : Size aSize( _rCtrl.GetSizePixel() );
358 0 : nWidth += _nOffs;
359 0 : aSize.Width() = nWidth;
360 0 : _rCtrl.SetSizePixel( aSize );
361 0 : return nWidth;
362 : }
363 :
364 0 : void AlignAfterImage( const FixedImage& _rImage, Control& _rCtrl, long _nXOffset )
365 : {
366 0 : Point aPos( _rImage.GetPosPixel() );
367 0 : Size aSize( _rImage.GetSizePixel() );
368 0 : long n = aPos.X();
369 0 : n += aSize.Width();
370 0 : n += _nXOffset;
371 0 : aPos.X() = n;
372 0 : n = aPos.Y();
373 0 : n += aSize.Height() / 2; // y-position is in the middle of the image
374 0 : n -= _rCtrl.GetSizePixel().Height() / 2; // center Control
375 0 : aPos.Y() = n;
376 0 : _rCtrl.SetPosPixel( aPos );
377 0 : }
378 :
379 0 : void AlignAfterImage( const FixedImage& _rImage, FixedInfo& _rFI, long _nXOffset )
380 : {
381 0 : AlignAfterImage( _rImage, static_cast< Control& >( _rFI ), _nXOffset );
382 0 : ShrinkToFitWidth( _rFI );
383 0 : }
384 :
385 0 : void AlignAndFitImageAndControl( FixedImage& _rImage, FixedInfo& _rFI, long _nXOffset )
386 : {
387 0 : _rImage.SetSizePixel( _rImage.GetImage().GetSizePixel() );
388 0 : AlignAfterImage( _rImage, _rFI, _nXOffset );
389 0 : }
390 : }
391 :
392 :
393 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|