1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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>
36 static ResMgr
* pResMgr
= 0;
37 static SvtSysLocale
* pSysLocale
= 0;
42 pResMgr
= ResMgr::CreateResMgr("xmlsec");
46 const LocaleDataWrapper
& GetLocaleData()
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
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())
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.
106 vector
< pair
< OUString
, OUString
> > parseDN(const OUString
& rRawString
)
108 vector
< pair
<OUString
, OUString
> > retVal
;
109 bool bInEscape
= false;
110 bool bInValue
= false;
112 sal_Int32 nTypeNameStart
= 0;
114 OUStringBuffer sbufValue
;
115 sal_Int32 length
= rRawString
.getLength();
117 for (sal_Int32 i
= 0; i
< length
; i
++)
119 sal_Unicode c
= rRawString
[i
];
125 sType
= rRawString
.copy(nTypeNameStart
, i
- nTypeNameStart
);
126 sType
= sType
.trim();
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] == '"')
145 bInValue
= !bInValue
; //value is enclosed in " "
149 //This quote is escaped by a preceding quote and therefore is
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
162 OSL_ASSERT(!sType
.isEmpty());
163 retVal
.push_back(make_pair(sType
, sbufValue
.makeStringAndClear()));
165 //The next char is the start of the new type
166 nTypeNameStart
= i
+ 1;
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
183 if (sbufValue
.getLength())
185 OSL_ASSERT(!sType
.isEmpty());
186 retVal
.push_back(make_pair(sType
, sbufValue
.makeStringAndClear()));
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;
198 sal_Int32 nTypeNameStart
= 0;
200 OUStringBuffer sbufValue
;
201 sal_Int32 length
= rRawString
.getLength();
203 for (sal_Int32 i
= 0; i
< length
; i
++)
205 sal_Unicode c
= rRawString
[i
];
211 sType
= rRawString
.copy(nTypeNameStart
, i
- nTypeNameStart
);
212 sType
= sType
.trim();
227 { // bInEscape is true
234 //an unescaped '"' is either at the beginning or end of the value
244 //This quote is escaped by a preceding quote and therefore is
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
257 OSL_ASSERT(!sType
.isEmpty());
258 retVal
.push_back(make_pair(sType
, sbufValue
.makeStringAndClear()));
260 //The next char is the start of the new type
261 nTypeNameStart
= i
+ 1;
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
281 if (!sbufValue
.isEmpty())
283 OSL_ASSERT(!sType
.isEmpty());
284 retVal
.push_back(make_pair(sType
, sbufValue
.makeStringAndClear()));
291 OUString
GetContentPart( const OUString
& _rRawString
)
293 char const * aIDs
[] = { "CN", "OU", "O", "E", NULL
};
296 vector
< pair
< OUString
, OUString
> > vecAttrValueOfDN
= parseDN(_rRawString
);
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
;
309 if (!retVal
.isEmpty())
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();
320 const char pHexDigs
[ 17 ] = "0123456789ABCDEF";
321 char pBuffer
[ 3 ] = " ";
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 ];
332 pBuffer
[ 0 ] = pHexDigs
[ nNum
];
333 aStr
.appendAscii( pBuffer
);
337 aStr
.appendAscii( _pSep
);
340 nBreak
= nBreakStart
;
345 return aStr
.makeStringAndClear();
349 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */