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