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>
25 #include <com/sun/star/xml/crypto/XXMLSecurityContext.hpp>
30 namespace comphelper::xmlsec
32 OUString
GetCertificateKind( const css::security::CertificateKind
&rKind
)
36 case css::security::CertificateKind_X509
:
38 case css::security::CertificateKind_OPENPGP
:
39 return u
"OpenPGP"_ustr
;
46 Creates two strings based on the distinguished name which are displayed in the
47 certificate details view. The first string contains only the values of the attribute
48 and values pairs, which are separated by commas. All escape characters ('"') are
50 The second string is for the details view at the bottom. It shows the attribute/value
51 pairs on different lines. All escape characters ('"') are removed.
53 std::pair
< OUString
, OUString
> GetDNForCertDetailsView( std::u16string_view rRawString
)
55 std::vector
< std::pair
< OUString
, OUString
> > vecAttrValueOfDN
= parseDN(rRawString
);
56 OUStringBuffer s1
, s2
;
57 for (auto i
= vecAttrValueOfDN
.cbegin(); i
< vecAttrValueOfDN
.cend(); ++i
)
59 if (i
!= vecAttrValueOfDN
.cbegin())
65 s2
.append(i
->first
+ " = " + i
->second
);
67 return std::make_pair(s1
.makeStringAndClear(), s2
.makeStringAndClear());
71 Whenever the attribute value contains special characters, such as '"' or ',' (without '')
72 then the value will be enclosed in double quotes by the respective Windows or NSS function
73 which we use to retrieve, for example, the subject name. If double quotes appear in the value then
74 they are escaped with a double quote. This function removes the escape characters.
77 std::vector
< std::pair
< OUString
, OUString
> > parseDN(std::u16string_view rRawString
)
79 std::vector
< std::pair
<OUString
, OUString
> > retVal
;
80 bool bInEscape
= false;
81 bool bInValue
= false;
83 sal_Int32 nTypeNameStart
= 0;
84 std::u16string_view sType
;
85 OUStringBuffer sbufValue
;
86 size_t length
= rRawString
.size();
88 for (size_t i
= 0; i
< length
; i
++)
90 sal_Unicode c
= rRawString
[i
];
96 sType
= rRawString
.substr(nTypeNameStart
, i
- nTypeNameStart
);
97 sType
= o3tl::trim(sType
);
109 //If this is the quote is the first of the couple which enclose the
110 //whole value, because the value contains special characters
111 //then we just drop it. That is, this character must be followed by
112 //a character which is not '"'.
113 if ( i
+ 1 < length
&& rRawString
[i
+1] == '"')
116 bInValue
= !bInValue
; //value is enclosed in " "
120 //This quote is escaped by a preceding quote and therefore is
126 else if (c
== ',' || c
== '+')
128 //The comma separate the attribute value pairs.
129 //If the comma is not part of a value (the value would then be enclosed in '"'),
130 //then we have reached the end of the value
133 OSL_ASSERT(!sType
.empty());
134 retVal
.emplace_back(OUString(sType
), sbufValue
.makeStringAndClear());
136 //The next char is the start of the new type
137 nTypeNameStart
= i
+ 1;
142 //The whole string is enclosed because it contains special characters.
143 //The enclosing '"' are not part of certificate but will be added by
144 //the function (Windows or NSS) which retrieves DN
154 if (sbufValue
.getLength())
156 OSL_ASSERT(!sType
.empty());
157 retVal
.emplace_back(OUString(sType
), sbufValue
.makeStringAndClear());
162 std::vector
< std::pair
< OUString
, OUString
> > parseDN(std::u16string_view rRawString
)
164 std::vector
< std::pair
<OUString
, OUString
> > retVal
;
165 //bInEscape == true means that the preceding character is an escape character
166 bool bInEscape
= false;
167 bool bInValue
= false;
169 sal_Int32 nTypeNameStart
= 0;
170 std::u16string_view sType
;
171 OUStringBuffer sbufValue
;
172 size_t length
= rRawString
.size();
174 for (size_t i
= 0; i
< length
; i
++)
176 sal_Unicode c
= rRawString
[i
];
182 sType
= rRawString
.substr(nTypeNameStart
, i
- nTypeNameStart
);
183 sType
= o3tl::trim(sType
);
198 { // bInEscape is true
205 //an unescaped '"' is either at the beginning or end of the value
215 //This quote is escaped by a preceding quote and therefore is
221 else if (c
== ',' || c
== '+')
223 //The comma separate the attribute value pairs.
224 //If the comma is not part of a value (the value would then be enclosed in '"'),
225 //then we have reached the end of the value
228 OSL_ASSERT(!sType
.empty());
229 retVal
.emplace_back(sType
, sbufValue
.makeStringAndClear());
231 //The next char is the start of the new type
232 nTypeNameStart
= i
+ 1;
237 //The whole string is enclosed because it contains special characters.
238 //The enclosing '"' are not part of certificate but will be added by
239 //the function (Windows or NSS) which retrieves DN
252 if (!sbufValue
.isEmpty())
254 OSL_ASSERT(!sType
.empty());
255 retVal
.emplace_back(sType
, sbufValue
.makeStringAndClear());
262 OUString
GetContentPart( const OUString
& _rRawString
, const css::security::CertificateKind
&rKind
)
264 static constexpr OUString aIDs
[] { u
"CN"_ustr
, u
"OU"_ustr
, u
"O"_ustr
, u
"E"_ustr
};
266 // tdf#131733 Don't process OpenPGP certs, only X509
267 if (rKind
== css::security::CertificateKind_OPENPGP
)
271 std::vector
< std::pair
< OUString
, OUString
> > vecAttrValueOfDN
= parseDN(_rRawString
);
272 for ( const auto & sPartId
: aIDs
)
274 auto idn
= std::find_if(vecAttrValueOfDN
.cbegin(), vecAttrValueOfDN
.cend(),
275 [&sPartId
](const std::pair
< OUString
, OUString
>& dn
) { return dn
.first
== sPartId
; });
276 if (idn
!= vecAttrValueOfDN
.cend())
277 retVal
= idn
->second
;
278 if (!retVal
.isEmpty())
281 return retVal
.isEmpty() ? _rRawString
: retVal
;
284 OUString
GetHexString( const css::uno::Sequence
< sal_Int8
>& _rSeq
, const char* _pSep
, sal_uInt16 _nLineBreak
)
287 const char pHexDigs
[ 17 ] = "0123456789ABCDEF";
288 char pBuffer
[ 3 ] = " ";
289 sal_uInt16 nBreakStart
= _nLineBreak
? _nLineBreak
: 1;
290 sal_uInt16 nBreak
= nBreakStart
;
291 for (sal_Int8 n
: _rSeq
)
293 sal_uInt8 nNum
= static_cast<sal_uInt8
>(n
);
294 // exchange the buffer[0] and buffer[1], which make it consistent with Mozilla and Windows
295 pBuffer
[ 1 ] = pHexDigs
[ nNum
& 0x0F ];
297 pBuffer
[ 0 ] = pHexDigs
[ nNum
];
298 aStr
.appendAscii( pBuffer
);
302 aStr
.appendAscii( _pSep
);
305 nBreak
= nBreakStart
;
310 return aStr
.makeStringAndClear();
313 css::uno::Reference
<css::security::XCertificate
> FindCertInContext(
314 const css::uno::Reference
<css::xml::crypto::XXMLSecurityContext
>& xSecurityContext
,
315 const OUString
& rSHA1Thumbprint
)
317 if (!xSecurityContext
.is())
320 css::uno::Reference
<css::xml::crypto::XSecurityEnvironment
> xSE
321 = xSecurityContext
->getSecurityEnvironment();
322 css::uno::Sequence
<css::uno::Reference
<css::security::XCertificate
>> xCertificates
323 = xSE
->getPersonalCertificates();
325 auto aCertsIter
= asNonConstRange(xCertificates
);
328 = std::find_if(aCertsIter
.begin(), aCertsIter
.end(),
329 [&rSHA1Thumbprint
](auto& xCert
)
331 return rSHA1Thumbprint
332 == GetHexString(xCert
->getSHA1Thumbprint(), "");
335 if (pxCert
== aCertsIter
.end())
342 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */