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