Simplify a bit
[LibreOffice.git] / comphelper / source / misc / xmlsechelper.cxx
blobe69f734b2b8b7996e4e9c8830ae0df8f5d730181
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 .
20 #include <comphelper/xmlsechelper.hxx>
22 #include <rtl/ustrbuf.hxx>
23 #include <osl/diagnose.h>
24 #include <o3tl/string_view.hxx>
26 #include <utility>
27 #include <vector>
29 namespace comphelper::xmlsec
31 OUString GetCertificateKind( const css::security::CertificateKind &rKind )
33 switch (rKind)
35 case css::security::CertificateKind_X509:
36 return "X.509";
37 case css::security::CertificateKind_OPENPGP:
38 return "OpenPGP";
39 default:
40 return OUString();
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
48 removed.
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())
60 s1.append(',');
61 s2.append('\n');
63 s1.append(i->second);
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.
75 #ifdef _WIN32
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;
81 bool bInType = true;
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];
91 if (c == '=')
93 if (! bInValue)
95 sType = rRawString.substr(nTypeNameStart, i - nTypeNameStart);
96 sType = o3tl::trim(sType);
97 bInType = false;
99 else
101 sbufValue.append(c);
104 else if (c == '"')
106 if (!bInEscape)
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] == '"')
113 bInEscape = true;
114 else
115 bInValue = !bInValue; //value is enclosed in " "
117 else
119 //This quote is escaped by a preceding quote and therefore is
120 //part of the value
121 sbufValue.append(c);
122 bInEscape = false;
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
130 if (!bInValue)
132 OSL_ASSERT(!sType.empty());
133 retVal.emplace_back(OUString(sType), sbufValue.makeStringAndClear());
134 sType = {};
135 //The next char is the start of the new type
136 nTypeNameStart = i + 1;
137 bInType = true;
139 else
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
144 sbufValue.append(c);
147 else
149 if (!bInType)
150 sbufValue.append(c);
153 if (sbufValue.getLength())
155 OSL_ASSERT(!sType.empty());
156 retVal.emplace_back(OUString(sType), sbufValue.makeStringAndClear());
158 return retVal;
160 #else
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;
167 bool bInType = true;
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];
177 if (c == '=')
179 if (! bInValue)
181 sType = rRawString.substr(nTypeNameStart, i - nTypeNameStart);
182 sType = o3tl::trim(sType);
183 bInType = false;
185 else
187 sbufValue.append(c);
190 else if (c == '\\')
192 if (!bInEscape)
194 bInEscape = true;
196 else
197 { // bInEscape is true
198 sbufValue.append(c);
199 bInEscape = false;
202 else if (c == '"')
204 //an unescaped '"' is either at the beginning or end of the value
205 if (!bInEscape)
207 if ( !bInValue)
208 bInValue = true;
209 else if (bInValue)
210 bInValue = false;
212 else
214 //This quote is escaped by a preceding quote and therefore is
215 //part of the value
216 sbufValue.append(c);
217 bInEscape = false;
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
225 if (!bInValue)
227 OSL_ASSERT(!sType.empty());
228 retVal.emplace_back(sType, sbufValue.makeStringAndClear());
229 sType = {};
230 //The next char is the start of the new type
231 nTypeNameStart = i + 1;
232 bInType = true;
234 else
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
239 sbufValue.append(c);
242 else
244 if (!bInType)
246 sbufValue.append(c);
247 bInEscape = false;
251 if (!sbufValue.isEmpty())
253 OSL_ASSERT(!sType.empty());
254 retVal.emplace_back(sType, sbufValue.makeStringAndClear());
256 return retVal;
259 #endif
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 )
267 return _rRawString;
269 OUString retVal;
270 int i = 0;
271 std::vector< std::pair< OUString, OUString > > vecAttrValueOfDN = parseDN(_rRawString);
272 while ( aIDs[i] )
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())
280 break;
282 return retVal.isEmpty() ? _rRawString : retVal;
285 OUString GetHexString( const css::uno::Sequence< sal_Int8 >& _rSeq, const char* _pSep, sal_uInt16 _nLineBreak )
287 OUStringBuffer aStr;
288 const char pHexDigs[ 17 ] = "0123456789ABCDEF";
289 char pBuffer[ 3 ] = " ";
290 sal_uInt16 nBreakStart = _nLineBreak? _nLineBreak : 1;
291 sal_uInt16 nBreak = nBreakStart;
292 for (sal_Int8 n : _rSeq)
294 sal_uInt8 nNum = static_cast<sal_uInt8>(n);
295 // exchange the buffer[0] and buffer[1], which make it consistent with Mozilla and Windows
296 pBuffer[ 1 ] = pHexDigs[ nNum & 0x0F ];
297 nNum >>= 4;
298 pBuffer[ 0 ] = pHexDigs[ nNum ];
299 aStr.appendAscii( pBuffer );
301 --nBreak;
302 if( nBreak )
303 aStr.appendAscii( _pSep );
304 else
306 nBreak = nBreakStart;
307 aStr.append( '\n' );
311 return aStr.makeStringAndClear();
315 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */