Version 5.4.3.2, tag libreoffice-5.4.3.2
[LibreOffice.git] / xmlsecurity / source / dialogs / resourcemanager.cxx
blob2f2e6596f720c3ca34c89037a545c088a9e58920
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
21 #include "resourcemanager.hxx"
23 #include <vcl/svapp.hxx>
24 #include <vcl/fixed.hxx>
25 #include <svl/solar.hrc>
26 #include <unotools/syslocale.hxx>
27 #include <rtl/ustring.h>
28 #include <rtl/ustrbuf.h>
29 #include <vector>
31 using namespace std;
33 namespace XmlSec
35 static ResMgr* pResMgr = nullptr;
36 static SvtSysLocale* pSysLocale = nullptr;
38 ResMgr* GetResMgr()
40 if (!pResMgr)
41 pResMgr = ResMgr::CreateResMgr("xmlsec");
42 return pResMgr;
45 const LocaleDataWrapper& GetLocaleData()
47 if (!pSysLocale)
48 pSysLocale = new SvtSysLocale;
49 return pSysLocale->GetLocaleData();
52 DateTime GetDateTime( const css::util::DateTime& _rDT )
54 return DateTime(_rDT);
57 OUString GetDateTimeString( const css::util::DateTime& _rDT )
59 // String with date and time information (#i20172#)
60 DateTime aDT( GetDateTime( _rDT ) );
61 const LocaleDataWrapper& rLoDa = GetLocaleData();
63 return rLoDa.getDate( aDT ) + " " + rLoDa.getTime( aDT );
66 OUString GetDateString( const css::util::DateTime& _rDT )
68 return GetLocaleData().getDate( GetDateTime( _rDT ) );
71 OUString GetCertificateKind( const css::security::CertificateKind &rKind )
73 switch (rKind)
75 case css::security::CertificateKind_X509:
76 return OUString("X.509");
77 case css::security::CertificateKind_OPENPGP:
78 return OUString("OpenPGP");
79 default:
80 return OUString();
85 Creates two strings based on the distinguished name which are displayed in the
86 certificate details view. The first string contains only the values of the attribute
87 and values pairs, which are separated by commas. All escape characters ('"') are
88 removed.
89 The second string is for the details view at the bottom. It shows the attribute/value
90 pairs on different lines. All escape characters ('"') are removed.
92 pair< OUString, OUString> GetDNForCertDetailsView( const OUString & rRawString)
94 vector< pair< OUString, OUString > > vecAttrValueOfDN = parseDN(rRawString);
95 OUStringBuffer s1, s2;
96 typedef vector< pair < OUString, OUString > >::const_iterator CIT;
97 for (CIT i = vecAttrValueOfDN.begin(); i < vecAttrValueOfDN.end(); ++i)
99 if (i != vecAttrValueOfDN.begin())
101 s1.append(',');
102 s2.append('\n');
104 s1.append(i->second);
105 s2.append(i->first + " = " + i->second);
107 return make_pair(s1.makeStringAndClear(), s2.makeStringAndClear());
111 Whenever the attribute value contains special characters, such as '"' or ',' (without '')
112 then the value will be enclosed in double quotes by the respective Windows or NSS function
113 which we use to retrieve, for example, the subject name. If double quotes appear in the value then
114 they are escaped with a double quote. This function removes the escape characters.
116 #ifdef _WIN32
117 vector< pair< OUString, OUString> > parseDN(const OUString& rRawString)
119 vector< pair<OUString, OUString> > retVal;
120 bool bInEscape = false;
121 bool bInValue = false;
122 bool bInType = true;
123 sal_Int32 nTypeNameStart = 0;
124 OUString sType;
125 OUStringBuffer sbufValue;
126 sal_Int32 length = rRawString.getLength();
128 for (sal_Int32 i = 0; i < length; i++)
130 sal_Unicode c = rRawString[i];
132 if (c == '=')
134 if (! bInValue)
136 sType = rRawString.copy(nTypeNameStart, i - nTypeNameStart);
137 sType = sType.trim();
138 bInType = false;
140 else
142 sbufValue.append(c);
145 else if (c == '"')
147 if (!bInEscape)
149 //If this is the quote is the first of the couple which enclose the
150 //whole value, because the value contains special characters
151 //then we just drop it. That is, this character must be followed by
152 //a character which is not '"'.
153 if ( i + 1 < length && rRawString[i+1] == '"')
154 bInEscape = true;
155 else
156 bInValue = !bInValue; //value is enclosed in " "
158 else
160 //This quote is escaped by a preceding quote and therefore is
161 //part of the value
162 sbufValue.append(c);
163 bInEscape = false;
166 else if (c == ',' || c == '+')
168 //The comma separate the attribute value pairs.
169 //If the comma is not part of a value (the value would then be enclosed in '"'),
170 //then we have reached the end of the value
171 if (!bInValue)
173 OSL_ASSERT(!sType.isEmpty());
174 retVal.push_back(make_pair(sType, sbufValue.makeStringAndClear()));
175 sType.clear();
176 //The next char is the start of the new type
177 nTypeNameStart = i + 1;
178 bInType = true;
180 else
182 //The whole string is enclosed because it contains special characters.
183 //The enclosing '"' are not part of certificate but will be added by
184 //the function (Windows or NSS) which retrieves DN
185 sbufValue.append(c);
188 else
190 if (!bInType)
191 sbufValue.append(c);
194 if (sbufValue.getLength())
196 OSL_ASSERT(!sType.isEmpty());
197 retVal.push_back(make_pair(sType, sbufValue.makeStringAndClear()));
199 return retVal;
201 #else
202 vector< pair< OUString, OUString> > parseDN(const OUString& rRawString)
204 vector< pair<OUString, OUString> > retVal;
205 //bInEscape == true means that the preceding character is an escape character
206 bool bInEscape = false;
207 bool bInValue = false;
208 bool bInType = true;
209 sal_Int32 nTypeNameStart = 0;
210 OUString sType;
211 OUStringBuffer sbufValue;
212 sal_Int32 length = rRawString.getLength();
214 for (sal_Int32 i = 0; i < length; i++)
216 sal_Unicode c = rRawString[i];
218 if (c == '=')
220 if (! bInValue)
222 sType = rRawString.copy(nTypeNameStart, i - nTypeNameStart);
223 sType = sType.trim();
224 bInType = false;
226 else
228 sbufValue.append(c);
231 else if (c == '\\')
233 if (!bInEscape)
235 bInEscape = true;
237 else
238 { // bInEscape is true
239 sbufValue.append(c);
240 bInEscape = false;
243 else if (c == '"')
245 //an unescaped '"' is either at the beginning or end of the value
246 if (!bInEscape)
248 if ( !bInValue)
249 bInValue = true;
250 else if (bInValue)
251 bInValue = false;
253 else
255 //This quote is escaped by a preceding quote and therefore is
256 //part of the value
257 sbufValue.append(c);
258 bInEscape = false;
261 else if (c == ',' || c == '+')
263 //The comma separate the attribute value pairs.
264 //If the comma is not part of a value (the value would then be enclosed in '"'),
265 //then we have reached the end of the value
266 if (!bInValue)
268 OSL_ASSERT(!sType.isEmpty());
269 retVal.push_back(make_pair(sType, sbufValue.makeStringAndClear()));
270 sType.clear();
271 //The next char is the start of the new type
272 nTypeNameStart = i + 1;
273 bInType = true;
275 else
277 //The whole string is enclosed because it contains special characters.
278 //The enclosing '"' are not part of certificate but will be added by
279 //the function (Windows or NSS) which retrieves DN
280 sbufValue.append(c);
283 else
285 if (!bInType)
287 sbufValue.append(c);
288 bInEscape = false;
292 if (!sbufValue.isEmpty())
294 OSL_ASSERT(!sType.isEmpty());
295 retVal.push_back(make_pair(sType, sbufValue.makeStringAndClear()));
297 return retVal;
300 #endif
302 OUString GetContentPart( const OUString& _rRawString )
304 char const * aIDs[] = { "CN", "OU", "O", "E", nullptr };
305 bool shouldBeParsed = false;
306 int i = 0;
307 while ( aIDs[i] )
309 if (_rRawString.startsWith(OUString::createFromAscii(aIDs[i++])))
311 shouldBeParsed = true;
312 break;
316 if (!shouldBeParsed)
317 return _rRawString;
319 OUString retVal;
320 i = 0;
321 vector< pair< OUString, OUString > > vecAttrValueOfDN = parseDN(_rRawString);
322 while ( aIDs[i] )
324 OUString sPartId = OUString::createFromAscii( aIDs[i++] );
325 typedef vector< pair < OUString, OUString > >::const_iterator CIT;
326 for (CIT idn = vecAttrValueOfDN.begin(); idn != vecAttrValueOfDN.end(); ++idn)
328 if (idn->first.equals(sPartId))
330 retVal = idn->second;
331 break;
334 if (!retVal.isEmpty())
335 break;
337 return retVal;
340 OUString GetHexString( const css::uno::Sequence< sal_Int8 >& _rSeq, const char* _pSep, sal_uInt16 _nLineBreak )
342 const sal_Int8* pSerNumSeq = _rSeq.getConstArray();
343 int nCnt = _rSeq.getLength();
344 OUStringBuffer aStr;
345 const char pHexDigs[ 17 ] = "0123456789ABCDEF";
346 char pBuffer[ 3 ] = " ";
347 sal_uInt8 nNum;
348 sal_uInt16 nBreakStart = _nLineBreak? _nLineBreak : 1;
349 sal_uInt16 nBreak = nBreakStart;
350 for( int i = 0 ; i < nCnt ; ++i )
352 nNum = sal_uInt8( pSerNumSeq[ i ] );
354 // exchange the buffer[0] and buffer[1], which make it consistent with Mozilla and Windows
355 pBuffer[ 1 ] = pHexDigs[ nNum & 0x0F ];
356 nNum >>= 4;
357 pBuffer[ 0 ] = pHexDigs[ nNum ];
358 aStr.appendAscii( pBuffer );
360 --nBreak;
361 if( nBreak )
362 aStr.appendAscii( _pSep );
363 else
365 nBreak = nBreakStart;
366 aStr.append( '\n' );
370 return aStr.makeStringAndClear();
374 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */