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 <svl/solar.hrc>
26 #include <unotools/syslocale.hxx>
27 #include <rtl/ustring.h>
28 #include <rtl/ustrbuf.h>
35 static ResMgr
* pResMgr
= nullptr;
36 static SvtSysLocale
* pSysLocale
= nullptr;
41 pResMgr
= ResMgr::CreateResMgr("xmlsec");
45 const LocaleDataWrapper
& GetLocaleData()
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
)
75 case css::security::CertificateKind_X509
:
76 return OUString("X.509");
77 case css::security::CertificateKind_OPENPGP
:
78 return OUString("OpenPGP");
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
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())
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.
117 vector
< pair
< OUString
, OUString
> > parseDN(const OUString
& rRawString
)
119 vector
< pair
<OUString
, OUString
> > retVal
;
120 bool bInEscape
= false;
121 bool bInValue
= false;
123 sal_Int32 nTypeNameStart
= 0;
125 OUStringBuffer sbufValue
;
126 sal_Int32 length
= rRawString
.getLength();
128 for (sal_Int32 i
= 0; i
< length
; i
++)
130 sal_Unicode c
= rRawString
[i
];
136 sType
= rRawString
.copy(nTypeNameStart
, i
- nTypeNameStart
);
137 sType
= sType
.trim();
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] == '"')
156 bInValue
= !bInValue
; //value is enclosed in " "
160 //This quote is escaped by a preceding quote and therefore is
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
173 OSL_ASSERT(!sType
.isEmpty());
174 retVal
.push_back(make_pair(sType
, sbufValue
.makeStringAndClear()));
176 //The next char is the start of the new type
177 nTypeNameStart
= i
+ 1;
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
194 if (sbufValue
.getLength())
196 OSL_ASSERT(!sType
.isEmpty());
197 retVal
.push_back(make_pair(sType
, sbufValue
.makeStringAndClear()));
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;
209 sal_Int32 nTypeNameStart
= 0;
211 OUStringBuffer sbufValue
;
212 sal_Int32 length
= rRawString
.getLength();
214 for (sal_Int32 i
= 0; i
< length
; i
++)
216 sal_Unicode c
= rRawString
[i
];
222 sType
= rRawString
.copy(nTypeNameStart
, i
- nTypeNameStart
);
223 sType
= sType
.trim();
238 { // bInEscape is true
245 //an unescaped '"' is either at the beginning or end of the value
255 //This quote is escaped by a preceding quote and therefore is
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
268 OSL_ASSERT(!sType
.isEmpty());
269 retVal
.push_back(make_pair(sType
, sbufValue
.makeStringAndClear()));
271 //The next char is the start of the new type
272 nTypeNameStart
= i
+ 1;
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
292 if (!sbufValue
.isEmpty())
294 OSL_ASSERT(!sType
.isEmpty());
295 retVal
.push_back(make_pair(sType
, sbufValue
.makeStringAndClear()));
302 OUString
GetContentPart( const OUString
& _rRawString
)
304 char const * aIDs
[] = { "CN", "OU", "O", "E", nullptr };
305 bool shouldBeParsed
= false;
309 if (_rRawString
.startsWith(OUString::createFromAscii(aIDs
[i
++])))
311 shouldBeParsed
= true;
321 vector
< pair
< OUString
, OUString
> > vecAttrValueOfDN
= parseDN(_rRawString
);
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
;
334 if (!retVal
.isEmpty())
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();
345 const char pHexDigs
[ 17 ] = "0123456789ABCDEF";
346 char pBuffer
[ 3 ] = " ";
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 ];
357 pBuffer
[ 0 ] = pHexDigs
[ nNum
];
358 aStr
.appendAscii( pBuffer
);
362 aStr
.appendAscii( _pSep
);
365 nBreak
= nBreakStart
;
370 return aStr
.makeStringAndClear();
374 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */