Bump version to 5.0-14
[LibreOffice.git] / xmlsecurity / source / dialogs / resourcemanager.cxx
blob83420b9d9a16bc53d2573eadba9dd49cc1913486
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 <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>
32 using namespace std;
34 namespace XmlSec
36 static ResMgr* pResMgr = 0;
37 static SvtSysLocale* pSysLocale = 0;
39 ResMgr* GetResMgr()
41 if (!pResMgr)
42 pResMgr = ResMgr::CreateResMgr("xmlsec");
43 return pResMgr;
46 const LocaleDataWrapper& GetLocaleData()
48 if (!pSysLocale)
49 pSysLocale = new SvtSysLocale;
50 return pSysLocale->GetLocaleData();
53 DateTime GetDateTime( const ::com::sun::star::util::DateTime& _rDT )
55 return DateTime(_rDT);
58 OUString GetDateTimeString( const ::com::sun::star::util::DateTime& _rDT )
60 // String with date and time information (#i20172#)
61 DateTime aDT( GetDateTime( _rDT ) );
62 const LocaleDataWrapper& rLoDa = GetLocaleData();
64 return rLoDa.getDate( aDT ) + " " + rLoDa.getTime( aDT );
67 OUString GetDateString( const ::com::sun::star::util::DateTime& _rDT )
69 return GetLocaleData().getDate( GetDateTime( _rDT ) );
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.
80 pair< OUString, OUString> GetDNForCertDetailsView( const OUString & rRawString)
82 vector< pair< OUString, OUString > > vecAttrValueOfDN = parseDN(rRawString);
83 OUStringBuffer s1, s2;
84 OUString sEqual(" = ");
85 typedef vector< pair < OUString, OUString > >::const_iterator CIT;
86 for (CIT i = vecAttrValueOfDN.begin(); i < vecAttrValueOfDN.end(); ++i)
88 if (i != vecAttrValueOfDN.begin())
90 s1.append(',');
91 s2.append('\n');
93 s1.append(i->second);
94 s2.append(i->first + sEqual + i->second);
96 return make_pair(s1.makeStringAndClear(), s2.makeStringAndClear());
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.
105 #ifdef WNT
106 vector< pair< OUString, OUString> > parseDN(const OUString& rRawString)
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();
117 for (sal_Int32 i = 0; i < length; i++)
119 sal_Unicode c = rRawString[i];
121 if (c == '=')
123 if (! bInValue)
125 sType = rRawString.copy(nTypeNameStart, i - nTypeNameStart);
126 sType = sType.trim();
127 bInType = false;
129 else
131 sbufValue.append(c);
134 else if (c == '"')
136 if (!bInEscape)
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 " "
147 else
149 //This quote is escaped by a preceding quote and therefore is
150 //part of the value
151 sbufValue.append(c);
152 bInEscape = false;
155 else if (c == ',' || c == '+')
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)
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;
169 else
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);
177 else
179 if (!bInType)
180 sbufValue.append(c);
183 if (sbufValue.getLength())
185 OSL_ASSERT(!sType.isEmpty());
186 retVal.push_back(make_pair(sType, sbufValue.makeStringAndClear()));
188 return retVal;
190 #else
191 vector< pair< OUString, OUString> > parseDN(const OUString& rRawString)
193 vector< pair<OUString, OUString> > retVal;
194 //bInEscape == true means that the preceding character is an escape character
195 bool bInEscape = false;
196 bool bInValue = false;
197 bool bInType = true;
198 sal_Int32 nTypeNameStart = 0;
199 OUString sType;
200 OUStringBuffer sbufValue;
201 sal_Int32 length = rRawString.getLength();
203 for (sal_Int32 i = 0; i < length; i++)
205 sal_Unicode c = rRawString[i];
207 if (c == '=')
209 if (! bInValue)
211 sType = rRawString.copy(nTypeNameStart, i - nTypeNameStart);
212 sType = sType.trim();
213 bInType = false;
215 else
217 sbufValue.append(c);
220 else if (c == '\\')
222 if (!bInEscape)
224 bInEscape = true;
226 else
227 { // bInEscape is true
228 sbufValue.append(c);
229 bInEscape = false;
232 else if (c == '"')
234 //an unescaped '"' is either at the beginning or end of the value
235 if (!bInEscape)
237 if ( !bInValue)
238 bInValue = true;
239 else if (bInValue)
240 bInValue = false;
242 else
244 //This quote is escaped by a preceding quote and therefore is
245 //part of the value
246 sbufValue.append(c);
247 bInEscape = false;
250 else if (c == ',' || c == '+')
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 if (!bInValue)
257 OSL_ASSERT(!sType.isEmpty());
258 retVal.push_back(make_pair(sType, sbufValue.makeStringAndClear()));
259 sType.clear();
260 //The next char is the start of the new type
261 nTypeNameStart = i + 1;
262 bInType = true;
264 else
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 sbufValue.append(c);
272 else
274 if (!bInType)
276 sbufValue.append(c);
277 bInEscape = false;
281 if (!sbufValue.isEmpty())
283 OSL_ASSERT(!sType.isEmpty());
284 retVal.push_back(make_pair(sType, sbufValue.makeStringAndClear()));
286 return retVal;
289 #endif
291 OUString GetContentPart( const OUString& _rRawString )
293 char const * aIDs[] = { "CN", "OU", "O", "E", NULL };
294 OUString retVal;
295 int i = 0;
296 vector< pair< OUString, OUString > > vecAttrValueOfDN = parseDN(_rRawString);
297 while ( aIDs[i] )
299 OUString sPartId = OUString::createFromAscii( aIDs[i++] );
300 typedef vector< pair < OUString, OUString > >::const_iterator CIT;
301 for (CIT idn = vecAttrValueOfDN.begin(); idn != vecAttrValueOfDN.end(); ++idn)
303 if (idn->first.equals(sPartId))
305 retVal = idn->second;
306 break;
309 if (!retVal.isEmpty())
310 break;
312 return retVal;
315 OUString GetHexString( const ::com::sun::star::uno::Sequence< sal_Int8 >& _rSeq, const char* _pSep, sal_uInt16 _nLineBreak )
317 const sal_Int8* pSerNumSeq = _rSeq.getConstArray();
318 int nCnt = _rSeq.getLength();
319 OUStringBuffer aStr;
320 const char pHexDigs[ 17 ] = "0123456789ABCDEF";
321 char pBuffer[ 3 ] = " ";
322 sal_uInt8 nNum;
323 sal_uInt16 nBreakStart = _nLineBreak? _nLineBreak : 1;
324 sal_uInt16 nBreak = nBreakStart;
325 for( int i = 0 ; i < nCnt ; ++i )
327 nNum = sal_uInt8( pSerNumSeq[ i ] );
329 // exchange the buffer[0] and buffer[1], which make it consistent with Mozilla and Windows
330 pBuffer[ 1 ] = pHexDigs[ nNum & 0x0F ];
331 nNum >>= 4;
332 pBuffer[ 0 ] = pHexDigs[ nNum ];
333 aStr.appendAscii( pBuffer );
335 --nBreak;
336 if( nBreak )
337 aStr.appendAscii( _pSep );
338 else
340 nBreak = nBreakStart;
341 aStr.append( '\n' );
345 return aStr.makeStringAndClear();
349 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */