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 .
20 #include <comphelper/xmlsechelper.hxx>
22 #include <rtl/ustrbuf.hxx>
23 #include <osl/diagnose.h>
24 #include <o3tl/string_view.hxx>
29 namespace comphelper::xmlsec
31 OUString
GetCertificateKind( const css::security::CertificateKind
&rKind
)
35 case css::security::CertificateKind_X509
:
37 case css::security::CertificateKind_OPENPGP
:
45 Creates two strings based on the distinguished name which are displayed in the
46 certificate details view. The first string contains only the values of the attribute
47 and values pairs, which are separated by commas. All escape characters ('"') are
49 The second string is for the details view at the bottom. It shows the attribute/value
50 pairs on different lines. All escape characters ('"') are removed.
52 std::pair
< OUString
, OUString
> GetDNForCertDetailsView( std::u16string_view rRawString
)
54 std::vector
< std::pair
< OUString
, OUString
> > vecAttrValueOfDN
= parseDN(rRawString
);
55 OUStringBuffer s1
, s2
;
56 for (auto i
= vecAttrValueOfDN
.cbegin(); i
< vecAttrValueOfDN
.cend(); ++i
)
58 if (i
!= vecAttrValueOfDN
.cbegin())
64 s2
.append(i
->first
+ " = " + i
->second
);
66 return std::make_pair(s1
.makeStringAndClear(), s2
.makeStringAndClear());
70 Whenever the attribute value contains special characters, such as '"' or ',' (without '')
71 then the value will be enclosed in double quotes by the respective Windows or NSS function
72 which we use to retrieve, for example, the subject name. If double quotes appear in the value then
73 they are escaped with a double quote. This function removes the escape characters.
76 std::vector
< std::pair
< OUString
, OUString
> > parseDN(std::u16string_view rRawString
)
78 std::vector
< std::pair
<OUString
, OUString
> > retVal
;
79 bool bInEscape
= false;
80 bool bInValue
= false;
82 sal_Int32 nTypeNameStart
= 0;
83 std::u16string_view sType
;
84 OUStringBuffer sbufValue
;
85 size_t length
= rRawString
.size();
87 for (size_t i
= 0; i
< length
; i
++)
89 sal_Unicode c
= rRawString
[i
];
95 sType
= rRawString
.substr(nTypeNameStart
, i
- nTypeNameStart
);
96 sType
= o3tl::trim(sType
);
108 //If this is the quote is the first of the couple which enclose the
109 //whole value, because the value contains special characters
110 //then we just drop it. That is, this character must be followed by
111 //a character which is not '"'.
112 if ( i
+ 1 < length
&& rRawString
[i
+1] == '"')
115 bInValue
= !bInValue
; //value is enclosed in " "
119 //This quote is escaped by a preceding quote and therefore is
125 else if (c
== ',' || c
== '+')
127 //The comma separate the attribute value pairs.
128 //If the comma is not part of a value (the value would then be enclosed in '"'),
129 //then we have reached the end of the value
132 OSL_ASSERT(!sType
.empty());
133 retVal
.push_back(std::make_pair(OUString(sType
), sbufValue
.makeStringAndClear()));
135 //The next char is the start of the new type
136 nTypeNameStart
= i
+ 1;
141 //The whole string is enclosed because it contains special characters.
142 //The enclosing '"' are not part of certificate but will be added by
143 //the function (Windows or NSS) which retrieves DN
153 if (sbufValue
.getLength())
155 OSL_ASSERT(!sType
.empty());
156 retVal
.push_back(std::make_pair(OUString(sType
), sbufValue
.makeStringAndClear()));
161 std::vector
< std::pair
< OUString
, OUString
> > parseDN(std::u16string_view rRawString
)
163 std::vector
< std::pair
<OUString
, OUString
> > retVal
;
164 //bInEscape == true means that the preceding character is an escape character
165 bool bInEscape
= false;
166 bool bInValue
= false;
168 sal_Int32 nTypeNameStart
= 0;
169 std::u16string_view sType
;
170 OUStringBuffer sbufValue
;
171 size_t length
= rRawString
.size();
173 for (size_t i
= 0; i
< length
; i
++)
175 sal_Unicode c
= rRawString
[i
];
181 sType
= rRawString
.substr(nTypeNameStart
, i
- nTypeNameStart
);
182 sType
= o3tl::trim(sType
);
197 { // bInEscape is true
204 //an unescaped '"' is either at the beginning or end of the value
214 //This quote is escaped by a preceding quote and therefore is
220 else if (c
== ',' || c
== '+')
222 //The comma separate the attribute value pairs.
223 //If the comma is not part of a value (the value would then be enclosed in '"'),
224 //then we have reached the end of the value
227 OSL_ASSERT(!sType
.empty());
228 retVal
.emplace_back(sType
, sbufValue
.makeStringAndClear());
230 //The next char is the start of the new type
231 nTypeNameStart
= i
+ 1;
236 //The whole string is enclosed because it contains special characters.
237 //The enclosing '"' are not part of certificate but will be added by
238 //the function (Windows or NSS) which retrieves DN
251 if (!sbufValue
.isEmpty())
253 OSL_ASSERT(!sType
.empty());
254 retVal
.emplace_back(sType
, sbufValue
.makeStringAndClear());
261 OUString
GetContentPart( const OUString
& _rRawString
, const css::security::CertificateKind
&rKind
)
263 char const * aIDs
[] = { "CN", "OU", "O", "E", nullptr };
265 // tdf#131733 Don't process OpenPGP certs, only X509
266 if (rKind
== css::security::CertificateKind_OPENPGP
)
271 std::vector
< std::pair
< OUString
, OUString
> > vecAttrValueOfDN
= parseDN(_rRawString
);
274 OUString sPartId
= OUString::createFromAscii( aIDs
[i
++] );
275 auto idn
= std::find_if(vecAttrValueOfDN
.cbegin(), vecAttrValueOfDN
.cend(),
276 [&sPartId
](const std::pair
< OUString
, OUString
>& dn
) { return dn
.first
== sPartId
; });
277 if (idn
!= vecAttrValueOfDN
.cend())
278 retVal
= idn
->second
;
279 if (!retVal
.isEmpty())
282 return retVal
.isEmpty() ? _rRawString
: retVal
;
285 OUString
GetHexString( const css::uno::Sequence
< sal_Int8
>& _rSeq
, const char* _pSep
, sal_uInt16 _nLineBreak
)
287 const sal_Int8
* pSerNumSeq
= _rSeq
.getConstArray();
288 int nCnt
= _rSeq
.getLength();
290 const char pHexDigs
[ 17 ] = "0123456789ABCDEF";
291 char pBuffer
[ 3 ] = " ";
293 sal_uInt16 nBreakStart
= _nLineBreak
? _nLineBreak
: 1;
294 sal_uInt16 nBreak
= nBreakStart
;
295 for( int i
= 0 ; i
< nCnt
; ++i
)
297 nNum
= sal_uInt8( pSerNumSeq
[ i
] );
299 // exchange the buffer[0] and buffer[1], which make it consistent with Mozilla and Windows
300 pBuffer
[ 1 ] = pHexDigs
[ nNum
& 0x0F ];
302 pBuffer
[ 0 ] = pHexDigs
[ nNum
];
303 aStr
.appendAscii( pBuffer
);
307 aStr
.appendAscii( _pSep
);
310 nBreak
= nBreakStart
;
315 return aStr
.makeStringAndClear();
319 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */