Debian Codegraph @

xmlsecurity/source/xmlsec/mscrypt/x509certificate_mscryptimpl.cxx

raw
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
4 *
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/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
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 .
18 */
19
20 #include <string.h>
21
22 #include <sal/config.h>
23 #include <sal/log.hxx>
24 #include <comphelper/servicehelper.hxx>
25 #include <comphelper/windowserrorstring.hxx>
26 #include <cppuhelper/supportsservice.hxx>
27 #include "x509certificate_mscryptimpl.hxx"
28 #include <certificateextension_xmlsecimpl.hxx>
29 #include <biginteger.hxx>
30 #include "sanextension_mscryptimpl.hxx"
31
32 #include <rtl/locale.h>
33 #include <rtl/ref.hxx>
34 #include <rtl/ustrbuf.hxx>
35 #include <osl/nlsupport.h>
36 #include <osl/process.h>
37 #include <o3tl/char16_t2wchar_t.hxx>
38 #include <o3tl/string_view.hxx>
39
40 #include <memory>
41 #include <string_view>
42 #include <utility>
43 #include <vector>
44 #include <tools/time.hxx>
45 #include <svl/sigstruct.hxx>
46
47 using namespace com::sun::star;
48 using namespace ::com::sun::star::uno ;
49 using namespace ::com::sun::star::security ;
50
51 using ::com::sun::star::security::XCertificate ;
52 using ::com::sun::star::util::DateTime ;
53
54 /*Returns the index within rRawString where sTypeName starts and where it ends.
55 The starting index is pair.first. The ending index in pair.second points
56 one char after the last character of the type.
57 sTypeName can be
58 "S" or "CN" (without ""). Do not use spaces at the beginning of the type name.
59 If the type name is not found then pair.first and pair.second are -1.
60 */
61 static std::pair< sal_Int32, sal_Int32 >
62 findTypeInDN(const OUString& rRawString, std::u16string_view sTypeName)
63 {
64 std::pair< sal_Int32, sal_Int32 > retVal;
65 bool bInEscape = false;
66 bool bInValue = false;
67 bool bFound = false;
68 sal_Int32 nTypeNameStart = 0;
69 sal_Int32 length = rRawString.getLength();
70
71 for (sal_Int32 i = 0; i < length; i++)
72 {
73 sal_Unicode c = rRawString[i];
74
75 if (c == '=')
76 {
77 if (! bInValue)
78 {
79 std::u16string_view sType = rRawString.subView(nTypeNameStart, i - nTypeNameStart);
80 sType = o3tl::trim(sType);
81 if (o3tl::equalsIgnoreAsciiCase(sType, sTypeName))
82 {
83 bFound = true;
84 break;
85 }
86 }
87 }
88 else if (c == '"')
89 {
90 if (!bInEscape)
91 {
92 //If this is the quote is the first of the couple which enclose the
93 //whole value, because the value contains special characters
94 //then we just drop it. That is, this character must be followed by
95 //a character which is not '"'.
96 if ( i + 1 < length && rRawString[i+1] == '"')
97 bInEscape = true;
98 else
99 bInValue = !bInValue; //value is enclosed in " "
100 }
101 else
102 {
103 //This quote is escaped by a preceding quote and therefore is
104 //part of the value
105 bInEscape = false;
106 }
107 }
108 else if (c == ',' || c == '+')
109 {
110 //The comma separate the attribute value pairs.
111 //If the comma is not part of a value (the value would then be enclosed in '"'),
112 //then we have reached the end of the value
113 if (!bInValue)
114 {
115 //The next char is the start of the new type
116 nTypeNameStart = i + 1;
117 }
118 }
119 }
120
121 //Found the Type Name, but there can still be spaces after the last comma
122 //and the beginning of the type.
123 if (bFound)
124 {
125 while (true)
126 {
127 sal_Unicode c = rRawString[nTypeNameStart];
128 if (c != ' ' && c != '\t')
129 //found
130 break;
131 nTypeNameStart ++;
132 }
133 // search end (one after last letter)
134 sal_Int32 nTypeNameEnd = nTypeNameStart;
135 nTypeNameEnd++;
136 while (true)
137 {
138 sal_Unicode c = rRawString[nTypeNameEnd];
139 if (c == ' ' || c == '\t' || c == '=')
140 break;
141 nTypeNameEnd++;
142 }
143 retVal = std::make_pair(nTypeNameStart, nTypeNameEnd);
144 }
145 else
146 {
147 retVal = std::make_pair(-1, -1);
148 }
149 return retVal;
150 }
151
152
153 /*
154 MS Crypto uses the 'S' tag (equal to the 'ST' tag in NSS), but the NSS can't recognise
155 it, so the 'S' tag should be changed to 'ST' tag. However I am not sure if this is necessary
156 anymore, because we provide always the signers certificate when signing. So libmlsec can find
157 the private key based on the provided certificate (X509Certificate element) and does not need
158 the issuer name (X509IssuerName element). The issuer name in the xml signature has also no
159 effect for the signature nor the certificate validation.
160 In many RFCs, for example 4519, on speaks of 'ST'. However, the certificate does not contain
161 strings for type names. Instead it uses OIDs.
162 */
163
164 static OUString replaceTagSWithTagST(OUString const & oldDN)
165 {
166 std::pair<sal_Int32, sal_Int32 > pairIndex = findTypeInDN(oldDN, u"S");
167
168 if (pairIndex.first != -1)
169 {
170 return OUString::Concat(oldDN.subView(0, pairIndex.first))+"ST"
171 +oldDN.subView(pairIndex.second);
172 }
173 return oldDN;
174 }
175 /* end */
176
177 X509Certificate_MSCryptImpl::X509Certificate_MSCryptImpl() :
178 m_pCertContext( nullptr )
179 {
180 }
181
182 X509Certificate_MSCryptImpl::~X509Certificate_MSCryptImpl() {
183 if( m_pCertContext != nullptr ) {
184 CertFreeCertificateContext( m_pCertContext ) ;
185 }
186 }
187
188 //Methods from XCertificate
189 sal_Int16 SAL_CALL X509Certificate_MSCryptImpl::getVersion() {
190 if( m_pCertContext != nullptr && m_pCertContext->pCertInfo != nullptr ) {
191 return static_cast<char>(m_pCertContext->pCertInfo->dwVersion) ;
192 } else {
193 return -1 ;
194 }
195 }
196
197 css::uno::Sequence< sal_Int8 > SAL_CALL X509Certificate_MSCryptImpl::getSerialNumber() {
198 if( m_pCertContext != nullptr && m_pCertContext->pCertInfo != nullptr ) {
199 Sequence< sal_Int8 > serial( m_pCertContext->pCertInfo->SerialNumber.cbData ) ;
200 auto serialRange = asNonConstRange(serial);
201 for( unsigned int i = 0 ; i < m_pCertContext->pCertInfo->SerialNumber.cbData ; i ++ )
202 serialRange[i] = *( m_pCertContext->pCertInfo->SerialNumber.pbData + m_pCertContext->pCertInfo->SerialNumber.cbData - i - 1 ) ;
203
204 return serial ;
205 } else {
206 return Sequence< sal_Int8 >();
207 }
208 }
209
210 OUString SAL_CALL X509Certificate_MSCryptImpl::getIssuerName() {
211 if( m_pCertContext != nullptr && m_pCertContext->pCertInfo != nullptr ) {
212 DWORD cchIssuer = CertNameToStrW(
213 X509_ASN_ENCODING | PKCS_7_ASN_ENCODING ,
214 &( m_pCertContext->pCertInfo->Issuer ),
215 CERT_X500_NAME_STR | CERT_NAME_STR_REVERSE_FLAG ,
216 nullptr, 0
217 ) ;
218
219 // Here the cbIssuer count the last 0x00 , take care.
220 if( cchIssuer != 0 ) {
221 auto issuer = std::make_unique<wchar_t[]>(cchIssuer);
222
223 cchIssuer = CertNameToStrW(
224 X509_ASN_ENCODING | PKCS_7_ASN_ENCODING ,
225 &( m_pCertContext->pCertInfo->Issuer ),
226 CERT_X500_NAME_STR | CERT_NAME_STR_REVERSE_FLAG ,
227 issuer.get(), cchIssuer
228 ) ;
229
230 if( cchIssuer <= 0 ) {
231 throw RuntimeException() ;
232 }
233
234 if(issuer.get()[cchIssuer -1] == 0) cchIssuer--; //delimit the last 0x00;
235 OUString xIssuer(o3tl::toU(issuer.get()), cchIssuer) ;
236
237 return replaceTagSWithTagST(xIssuer);
238 } else {
239 return OUString() ;
240 }
241 } else {
242 return OUString() ;
243 }
244 }
245
246 OUString SAL_CALL X509Certificate_MSCryptImpl::getSubjectName()
247 {
248 if( m_pCertContext != nullptr && m_pCertContext->pCertInfo != nullptr )
249 {
250 DWORD cchSubject = CertNameToStrW(
251 X509_ASN_ENCODING | PKCS_7_ASN_ENCODING ,
252 &( m_pCertContext->pCertInfo->Subject ),
253 CERT_X500_NAME_STR | CERT_NAME_STR_REVERSE_FLAG ,
254 nullptr, 0
255 ) ;
256
257 if( cchSubject != 0 )
258 {
259 auto subject = std::make_unique<wchar_t[]>(cchSubject);
260
261 cchSubject = CertNameToStrW(
262 X509_ASN_ENCODING | PKCS_7_ASN_ENCODING ,
263 &( m_pCertContext->pCertInfo->Subject ),
264 CERT_X500_NAME_STR | CERT_NAME_STR_REVERSE_FLAG ,
265 subject.get(), cchSubject
266 ) ;
267
268 if( cchSubject <= 0 ) {
269 throw RuntimeException() ;
270 }
271
272 OUString xSubject(o3tl::toU(subject.get()));
273
274 return replaceTagSWithTagST(xSubject);
275 } else
276 {
277 return OUString() ;
278 }
279 }
280 else
281 {
282 return OUString() ;
283 }
284 }
285
286 css::util::DateTime SAL_CALL X509Certificate_MSCryptImpl::getNotValidBefore() {
287 if( m_pCertContext != nullptr && m_pCertContext->pCertInfo != nullptr ) {
288 SYSTEMTIME explTime ;
289 DateTime dateTime ;
290 FILETIME localFileTime;
291
292 if (FileTimeToLocalFileTime(&( m_pCertContext->pCertInfo->NotBefore ), &localFileTime))
293 {
294 if( FileTimeToSystemTime( &localFileTime, &explTime ) ) {
295 //Convert the time to readable local time
296 dateTime.NanoSeconds = explTime.wMilliseconds * ::tools::Time::nanoPerMilli ;
297 dateTime.Seconds = explTime.wSecond ;
298 dateTime.Minutes = explTime.wMinute ;
299 dateTime.Hours = explTime.wHour ;
300 dateTime.Day = explTime.wDay ;
301 dateTime.Month = explTime.wMonth ;
302 dateTime.Year = explTime.wYear ;
303 }
304 }
305
306 return dateTime ;
307 } else {
308 return DateTime() ;
309 }
310 }
311
312 css::util::DateTime SAL_CALL X509Certificate_MSCryptImpl::getNotValidAfter() {
313 if( m_pCertContext != nullptr && m_pCertContext->pCertInfo != nullptr ) {
314 SYSTEMTIME explTime ;
315 DateTime dateTime ;
316 FILETIME localFileTime;
317
318 if (FileTimeToLocalFileTime(&( m_pCertContext->pCertInfo->NotAfter ), &localFileTime))
319 {
320 if( FileTimeToSystemTime( &localFileTime, &explTime ) ) {
321 //Convert the time to readable local time
322 dateTime.NanoSeconds = explTime.wMilliseconds * ::tools::Time::nanoPerMilli ;
323 dateTime.Seconds = explTime.wSecond ;
324 dateTime.Minutes = explTime.wMinute ;
325 dateTime.Hours = explTime.wHour ;
326 dateTime.Day = explTime.wDay ;
327 dateTime.Month = explTime.wMonth ;
328 dateTime.Year = explTime.wYear ;
329 }
330 }
331
332 return dateTime ;
333 } else {
334 return DateTime() ;
335 }
336 }
337
338 css::uno::Sequence< sal_Int8 > SAL_CALL X509Certificate_MSCryptImpl::getIssuerUniqueID() {
339 if( m_pCertContext != nullptr && m_pCertContext->pCertInfo != nullptr ) {
340 Sequence< sal_Int8 > issuerUid( m_pCertContext->pCertInfo->IssuerUniqueId.cbData ) ;
341 auto issuerUidRange = asNonConstRange(issuerUid);
342 for( unsigned int i = 0 ; i < m_pCertContext->pCertInfo->IssuerUniqueId.cbData; i ++ )
343 issuerUidRange[i] = *( m_pCertContext->pCertInfo->IssuerUniqueId.pbData + i ) ;
344
345 return issuerUid ;
346 } else {
347 return Sequence< sal_Int8 >();
348 }
349 }
350
351 css::uno::Sequence< sal_Int8 > SAL_CALL X509Certificate_MSCryptImpl::getSubjectUniqueID() {
352 if( m_pCertContext != nullptr && m_pCertContext->pCertInfo != nullptr ) {
353 Sequence< sal_Int8 > subjectUid( m_pCertContext->pCertInfo->SubjectUniqueId.cbData ) ;
354 auto subjectUidRange = asNonConstRange(subjectUid);
355 for( unsigned int i = 0 ; i < m_pCertContext->pCertInfo->SubjectUniqueId.cbData; i ++ )
356 subjectUidRange[i] = *( m_pCertContext->pCertInfo->SubjectUniqueId.pbData + i ) ;
357
358 return subjectUid ;
359 } else {
360 return Sequence< sal_Int8 >();
361 }
362 }
363
364 css::uno::Sequence< css::uno::Reference< css::security::XCertificateExtension > > SAL_CALL X509Certificate_MSCryptImpl::getExtensions() {
365 if( m_pCertContext != nullptr && m_pCertContext->pCertInfo != nullptr && m_pCertContext->pCertInfo->cExtension != 0 ) {
366 rtl::Reference<CertificateExtension_XmlSecImpl> xExtn ;
367 Sequence< Reference< XCertificateExtension > > xExtns( m_pCertContext->pCertInfo->cExtension ) ;
368 auto pExtns = xExtns.getArray();
369
370 for( unsigned int i = 0; i < m_pCertContext->pCertInfo->cExtension; i++ ) {
371 CERT_EXTENSION* pExtn = &(m_pCertContext->pCertInfo->rgExtension[i]) ;
372
373
374 OUString objId = OUString::createFromAscii( pExtn->pszObjId );
375
376 if ( objId == "2.5.29.17" )
377 xExtn = reinterpret_cast<CertificateExtension_XmlSecImpl*>(new SanExtensionImpl());
378 else
379 xExtn = new CertificateExtension_XmlSecImpl;
380
381 xExtn->setCertExtn( pExtn->Value.pbData, pExtn->Value.cbData, reinterpret_cast<unsigned char*>(pExtn->pszObjId), strlen( pExtn->pszObjId ), pExtn->fCritical ) ;
382
383 pExtns[i] = xExtn ;
384 }
385
386 return xExtns ;
387 } else {
388 return Sequence< Reference< XCertificateExtension > >();
389 }
390 }
391
392 css::uno::Reference< css::security::XCertificateExtension > SAL_CALL X509Certificate_MSCryptImpl::findCertificateExtension( const css::uno::Sequence< sal_Int8 >& /*oid*/ ) {
393 if( m_pCertContext != nullptr && m_pCertContext->pCertInfo != nullptr && m_pCertContext->pCertInfo->cExtension != 0 ) {
394 rtl::Reference<CertificateExtension_XmlSecImpl> xExtn ;
395
396 for( unsigned int i = 0; i < m_pCertContext->pCertInfo->cExtension; i++ ) {
397 CERT_EXTENSION* pExtn = &( m_pCertContext->pCertInfo->rgExtension[i] ) ;
398
399 //TODO: Compare the oid
400 if( false ) {
401 xExtn = new CertificateExtension_XmlSecImpl;
402 xExtn->setCertExtn( pExtn->Value.pbData, pExtn->Value.cbData, reinterpret_cast<unsigned char*>(pExtn->pszObjId), strlen( pExtn->pszObjId ), pExtn->fCritical ) ;
403 }
404 }
405
406 return xExtn ;
407 } else {
408 return nullptr ;
409 }
410 }
411
412
413 css::uno::Sequence< sal_Int8 > SAL_CALL X509Certificate_MSCryptImpl::getEncoded() {
414 if( m_pCertContext != nullptr && m_pCertContext->cbCertEncoded > 0 ) {
415 Sequence< sal_Int8 > rawCert( m_pCertContext->cbCertEncoded ) ;
416 auto prawCert = rawCert.getArray();
417
418 for( unsigned int i = 0 ; i < m_pCertContext->cbCertEncoded ; i ++ )
419 prawCert[i] = *( m_pCertContext->pbCertEncoded + i ) ;
420
421 return rawCert ;
422 } else {
423 return Sequence< sal_Int8 >();
424 }
425 }
426
427 //Helper methods
428 void X509Certificate_MSCryptImpl::setMswcryCert( const CERT_CONTEXT* cert ) {
429 if( m_pCertContext != nullptr ) {
430 CertFreeCertificateContext( m_pCertContext ) ;
431 m_pCertContext = nullptr ;
432 }
433
434 if( cert != nullptr ) {
435 m_pCertContext = CertDuplicateCertificateContext( cert ) ;
436 }
437 }
438
439 const CERT_CONTEXT* X509Certificate_MSCryptImpl::getMswcryCert() const {
440 if( m_pCertContext != nullptr ) {
441 return m_pCertContext ;
442 } else {
443 return nullptr ;
444 }
445 }
446
447 void X509Certificate_MSCryptImpl::setRawCert( Sequence< sal_Int8 > const & rawCert ) {
448 if( m_pCertContext != nullptr ) {
449 CertFreeCertificateContext( m_pCertContext ) ;
450 m_pCertContext = nullptr ;
451 }
452
453 if( rawCert.getLength() != 0 ) {
454 m_pCertContext = CertCreateCertificateContext( X509_ASN_ENCODING, reinterpret_cast<const sal_uInt8*>(&rawCert[0]), rawCert.getLength() ) ;
455 }
456 }
457
458 static OUString findOIDDescription(std::string_view oid)
459 {
460 static constexpr std::pair<std::string_view, OUString> OIDs[] = {
461 { "1.2.840.113549", u"RSA Data Security Inc."_ustr },
462 { "1.2.840.113549.1", u"PKCS"_ustr },
463 { "1.2.840.113549.2", u"RSA digest algorithm"_ustr },
464 { "1.2.840.113549.3", u"RSA cipher algorithm"_ustr },
465 { "1.2.840.113549.1.1", u"PKCS #1"_ustr },
466 { "1.2.840.113549.1.2", u"Unknown"_ustr },
467 { "1.2.840.113549.1.3", u"Unknown"_ustr },
468 { "1.2.840.113549.1.4", u"Unknown"_ustr },
469 { "1.2.840.113549.1.5", u"PKCS #5"_ustr },
470 { "1.2.840.113549.1.6", u"Unknown"_ustr },
471 { "1.2.840.113549.1.7", u"PKCS #7"_ustr },
472 { "1.2.840.113549.1.8", u"Unknown"_ustr },
473 { "1.2.840.113549.1.9", u"PKCS #9"_ustr },
474 { "1.2.840.113549.1.10", u"Unknown"_ustr },
475 { "1.2.840.113549.1.12", u"PKCS #12"_ustr },
476 { "1.2.840.113549.1.1.2", u"PKCS #1 MD2 With RSA Encryption"_ustr },
477 { "1.2.840.113549.1.1.3", u"PKCS #1 MD4 With RSA Encryption"_ustr },
478 { "1.2.840.113549.1.1.4", u"PKCS #1 MD5 With RSA Encryption"_ustr },
479 { "1.2.840.113549.1.1.1", u"PKCS #1 RSA Encryption"_ustr },
480 { "1.2.840.113549.1.1.2", u"PKCS #1 MD2 With RSA Encryption"_ustr },
481 { "1.2.840.113549.1.1.3", u"PKCS #1 MD4 With RSA Encryption"_ustr },
482 { "1.2.840.113549.1.1.4", u"PKCS #1 MD5 With RSA Encryption"_ustr },
483 { "1.2.840.113549.1.1.5", u"PKCS #1 SHA-1 With RSA Encryption"_ustr },
484 { "1.2.840.113549.1.1.5", u"PKCS #1 SHA-1 With RSA Encryption"_ustr },
485 { "1.2.840.113549.1.3.1", u"Unknown"_ustr },
486 { "1.2.840.113549.1.7.1", u"PKCS #7 Data"_ustr },
487 { "1.2.840.113549.1.7.2", u"PKCS #7 Signed Data"_ustr },
488 { "1.2.840.113549.1.7.3", u"PKCS #7 Enveloped Data"_ustr },
489 { "1.2.840.113549.1.7.4", u"PKCS #7 Signed and Enveloped Data"_ustr },
490 { "1.2.840.113549.1.7.5", u"PKCS #7 Digested Data"_ustr },
491 { "1.2.840.113549.1.7.6", u"PKCS #7 Encrypted Data"_ustr },
492 { "1.2.840.113549.1.9.1", u"PKCS #9 Email Address"_ustr },
493 { "1.2.840.113549.1.9.2", u"PKCS #9 Unstructured Name"_ustr },
494 { "1.2.840.113549.1.9.3", u"PKCS #9 Content Type"_ustr },
495 { "1.2.840.113549.1.9.4", u"PKCS #9 Message Digest"_ustr },
496 { "1.2.840.113549.1.9.5", u"PKCS #9 Signing Time"_ustr },
497 { "1.2.840.113549.1.9.6", u"PKCS #9 Counter Signature"_ustr },
498 { "1.2.840.113549.1.9.7", u"PKCS #9 Challenge Password"_ustr },
499 { "1.2.840.113549.1.9.8", u"PKCS #9 Unstructured Address"_ustr },
500 { "1.2.840.113549.1.9.9", u"PKCS #9 Extended Certificate Attributes"_ustr },
501 { "1.2.840.113549.1.9.15", u"PKCS #9 S/MIME Capabilities"_ustr },
502 { "1.2.840.113549.1.9.15.1", u"Unknown"_ustr },
503 { "1.2.840.113549.3.2", u"RC2-CBC"_ustr },
504 { "1.2.840.113549.3.4", u"RC4"_ustr },
505 { "1.2.840.113549.3.7", u"DES-EDE3-CBC"_ustr },
506 { "1.2.840.113549.3.9", u"RC5-CBCPad"_ustr },
507 { "1.2.840.10046", u"ANSI X9.42"_ustr },
508 { "1.2.840.10046.2.1", u"Diffie-Hellman Public Key Algorithm"_ustr },
509 { "1.2.840.10040", u"ANSI X9.57"_ustr },
510 { "1.2.840.10040.4.1", u"ANSI X9.57 DSA Signature"_ustr },
511 { "1.2.840.10040.4.3", u"ANSI X9.57 Algorithm DSA Signature with SHA-1 Digest"_ustr },
512 { "2.5", u"Directory"_ustr },
513 { "2.5.8", u"X.500-defined algorithms"_ustr },
514 { "2.5.8.1", u"X.500-defined encryption algorithms"_ustr },
515 { "2.5.8.2", u"Unknown"_ustr },
516 { "2.5.8.3", u"Unknown"_ustr },
517 { "2.5.8.1.1", u"RSA Encryption Algorithm"_ustr },
518 { "1.3.14", u"Open Systems Implementors Workshop"_ustr },
519 { "1.3.14.3.2", u"OIW SECSIG Algorithm"_ustr },
520 { "1.3.14.3.2.2", u"Unknown"_ustr },
521 { "1.3.14.3.2.3", u"Unknown"_ustr },
522 { "1.3.14.3.2.4", u"Unknown"_ustr },
523 { "1.3.14.3.2.6", u"DES-ECB"_ustr },
524 { "1.3.14.3.2.7", u"DES-CBC"_ustr },
525 { "1.3.14.3.2.8", u"DES-OFB"_ustr },
526 { "1.3.14.3.2.9", u"DES-CFB"_ustr },
527 { "1.3.14.3.2.10", u"DES-MAC"_ustr },
528 { "1.3.14.3.2.11", u"Unknown"_ustr },
529 { "1.3.14.3.2.12", u"Unknown"_ustr },
530 { "1.3.14.3.2.13", u"Unknown"_ustr },
531 { "1.3.14.3.2.14", u"Unknown"_ustr },
532 { "1.3.14.3.2.15", u"ISO SHA with RSA Signature"_ustr },
533 { "1.3.14.3.2.16", u"Unknown"_ustr },
534 { "1.3.14.3.2.17", u"DES-EDE"_ustr },
535 { "1.3.14.3.2.18", u"Unknown"_ustr },
536 { "1.3.14.3.2.19", u"Unknown"_ustr },
537 { "1.3.14.3.2.20", u"Unknown"_ustr },
538 { "1.3.14.3.2.21", u"Unknown"_ustr },
539 { "1.3.14.3.2.22", u"Unknown"_ustr },
540 { "1.3.14.3.2.23", u"Unknown"_ustr },
541 { "1.3.14.3.2.24", u"Unknown"_ustr },
542 { "1.3.14.3.2.25", u"Unknown"_ustr },
543 { "1.3.14.3.2.26", u"SHA-1"_ustr },
544 { "1.3.14.3.2.27", u"Forgezza DSA Signature with SHA-1 Digest"_ustr },
545 { "1.3.14.3.2.28", u"Unknown"_ustr },
546 { "1.3.14.3.2.29", u"Unknown"_ustr },
547 { "1.3.14.7.2", u"Unknown"_ustr },
548 { "1.3.14.7.2.1", u"Unknown"_ustr },
549 { "1.3.14.7.2.2", u"Unknown"_ustr },
550 { "1.3.14.7.2.3", u"Unknown"_ustr },
551 { "1.3.14.7.2.2.1", u"Unknown"_ustr },
552 { "1.3.14.7.2.3.1", u"Unknown"_ustr },
553 { "2.16.840.1.101.2.1", u"US DOD Infosec"_ustr },
554 { "2.16.840.1.101.2.1.1.1", u"Unknown"_ustr },
555 { "2.16.840.1.101.2.1.1.2", u"MISSI DSS Algorithm (Old)"_ustr },
556 { "2.16.840.1.101.2.1.1.3", u"Unknown"_ustr },
557 { "2.16.840.1.101.2.1.1.4", u"Skipjack CBC64"_ustr },
558 { "2.16.840.1.101.2.1.1.5", u"Unknown"_ustr },
559 { "2.16.840.1.101.2.1.1.6", u"Unknown"_ustr },
560 { "2.16.840.1.101.2.1.1.7", u"Unknown"_ustr },
561 { "2.16.840.1.101.2.1.1.8", u"Unknown"_ustr },
562 { "2.16.840.1.101.2.1.1.9", u"Unknown"_ustr },
563 { "2.16.840.1.101.2.1.1.10", u"MISSI KEA Algorithm"_ustr },
564 { "2.16.840.1.101.2.1.1.11", u"Unknown"_ustr },
565 { "2.16.840.1.101.2.1.1.12", u"MISSI KEA and DSS Algorithm (Old)"_ustr },
566 { "2.16.840.1.101.2.1.1.13", u"Unknown"_ustr },
567 { "2.16.840.1.101.2.1.1.14", u"Unknown"_ustr },
568 { "2.16.840.1.101.2.1.1.15", u"Unknown"_ustr },
569 { "2.16.840.1.101.2.1.1.16", u"Unknown"_ustr },
570 { "2.16.840.1.101.2.1.1.17", u"Unknown"_ustr },
571 { "2.16.840.1.101.2.1.1.18", u"Unknown"_ustr },
572 { "2.16.840.1.101.2.1.1.19", u"MISSI DSS Algorithm"_ustr },
573 { "2.16.840.1.101.2.1.1.20", u"MISSI KEA and DSS Algorithm"_ustr },
574 { "2.16.840.1.101.2.1.1.21", u"Unknown"_ustr },
575 { "1.2.643.2.2.35.0", u"GOST_R_34.10-2001_Test"_ustr },
576 { "1.2.643.2.2.35.1", u"GOST_R_34.10-2001_Sign_DH_PRO"_ustr },
577 { "1.2.643.2.2.35.2", u"GOST_R_34.10-2001_Sign_DH_CARD"_ustr },
578 { "1.2.643.2.2.35.3", u"GOST_R_34.10-2001_Sign_DH"_ustr },
579 { "1.2.643.2.2.36.0", u"GOST_R_34.10-2001_Sign_DH_PRO"_ustr },
580 };
581
582 for (const auto& [item_oid, desc] : OIDs)
583 if (oid == item_oid)
584 return desc;
585
586 return OUString() ;
587 }
588
589 static css::uno::Sequence< sal_Int8 > getThumbprint(const CERT_CONTEXT* pCertContext, DWORD dwPropId)
590 {
591 if( pCertContext != nullptr )
592 {
593 DWORD cbData = dwPropId == CERT_SHA256_HASH_PROP_ID ? 32 : 20;
594 unsigned char fingerprint[32];
595 if (CertGetCertificateContextProperty(pCertContext, dwPropId, fingerprint, &cbData))
596 {
597 Sequence< sal_Int8 > thumbprint( cbData ) ;
598 auto pthumbprint = thumbprint.getArray();
599 for( unsigned int i = 0 ; i < cbData ; i ++ )
600 {
601 pthumbprint[i] = fingerprint[i];
602 }
603
604 return thumbprint;
605 }
606 else
607 {
608 DWORD e = GetLastError();
609 cbData = e;
610 }
611 }
612
613 return Sequence< sal_Int8 >();
614 }
615
616 OUString SAL_CALL X509Certificate_MSCryptImpl::getSubjectPublicKeyAlgorithm()
617 {
618 if( m_pCertContext != nullptr && m_pCertContext->pCertInfo != nullptr )
619 {
620 CRYPT_ALGORITHM_IDENTIFIER algorithm = m_pCertContext->pCertInfo->SubjectPublicKeyInfo.Algorithm;
621 return findOIDDescription( algorithm.pszObjId ) ;
622 }
623 else
624 {
625 return OUString() ;
626 }
627 }
628
629 css::uno::Sequence< sal_Int8 > SAL_CALL X509Certificate_MSCryptImpl::getSubjectPublicKeyValue()
630 {
631 if( m_pCertContext != nullptr && m_pCertContext->pCertInfo != nullptr )
632 {
633 CRYPT_BIT_BLOB publicKey = m_pCertContext->pCertInfo->SubjectPublicKeyInfo.PublicKey;
634
635 Sequence< sal_Int8 > key( publicKey.cbData ) ;
636 auto keyRange = asNonConstRange(key);
637 for( unsigned int i = 0 ; i < publicKey.cbData ; i++ )
638 {
639 keyRange[i] = *(publicKey.pbData + i) ;
640 }
641
642 return key;
643 }
644 else
645 {
646 return Sequence< sal_Int8 >();
647 }
648 }
649
650 OUString SAL_CALL X509Certificate_MSCryptImpl::getSignatureAlgorithm()
651 {
652 if( m_pCertContext != nullptr && m_pCertContext->pCertInfo != nullptr )
653 {
654 CRYPT_ALGORITHM_IDENTIFIER algorithm = m_pCertContext->pCertInfo->SignatureAlgorithm;
655 return findOIDDescription( algorithm.pszObjId ) ;
656 }
657 else
658 {
659 return OUString() ;
660 }
661 }
662
663 uno::Sequence<sal_Int8> X509Certificate_MSCryptImpl::getSHA256Thumbprint()
664 {
665 return getThumbprint(m_pCertContext, CERT_SHA256_HASH_PROP_ID);
666 }
667
668 svl::crypto::SignatureMethodAlgorithm X509Certificate_MSCryptImpl::getSignatureMethodAlgorithm()
669 {
670 svl::crypto::SignatureMethodAlgorithm nRet = svl::crypto::SignatureMethodAlgorithm::RSA;
671
672 if (!m_pCertContext || !m_pCertContext->pCertInfo)
673 return nRet;
674
675 CRYPT_ALGORITHM_IDENTIFIER algorithm = m_pCertContext->pCertInfo->SubjectPublicKeyInfo.Algorithm;
676 OString aObjId(algorithm.pszObjId);
677 if (aObjId == szOID_ECC_PUBLIC_KEY)
678 nRet = svl::crypto::SignatureMethodAlgorithm::ECDSA;
679
680 return nRet;
681 }
682
683 css::uno::Sequence< sal_Int8 > SAL_CALL X509Certificate_MSCryptImpl::getSHA1Thumbprint()
684 {
685 return getThumbprint(m_pCertContext, CERT_SHA1_HASH_PROP_ID);
686 }
687
688 css::uno::Sequence< sal_Int8 > SAL_CALL X509Certificate_MSCryptImpl::getMD5Thumbprint()
689 {
690 return getThumbprint(m_pCertContext, CERT_MD5_HASH_PROP_ID);
691 }
692
693 CertificateKind SAL_CALL X509Certificate_MSCryptImpl::getCertificateKind()
694 {
695 return CertificateKind_X509;
696 }
697
698 sal_Int32 SAL_CALL X509Certificate_MSCryptImpl::getCertificateUsage( )
699 {
700 sal_Int32 usage =
701 CERT_DATA_ENCIPHERMENT_KEY_USAGE |
702 CERT_DIGITAL_SIGNATURE_KEY_USAGE |
703 CERT_KEY_AGREEMENT_KEY_USAGE |
704 CERT_KEY_CERT_SIGN_KEY_USAGE |
705 CERT_KEY_ENCIPHERMENT_KEY_USAGE |
706 CERT_NON_REPUDIATION_KEY_USAGE |
707 CERT_OFFLINE_CRL_SIGN_KEY_USAGE;
708
709 if( m_pCertContext != nullptr && m_pCertContext->pCertInfo != nullptr && m_pCertContext->pCertInfo->cExtension != 0 )
710 {
711 CERT_EXTENSION* pExtn = CertFindExtension(
712 szOID_KEY_USAGE,
713 m_pCertContext->pCertInfo->cExtension,
714 m_pCertContext->pCertInfo->rgExtension);
715
716 if (pExtn != nullptr)
717 {
718 DWORD length = 0;
719 bool rc = CryptDecodeObject(
720 X509_ASN_ENCODING,
721 X509_KEY_USAGE,
722 pExtn->Value.pbData,
723 pExtn->Value.cbData,
724 0,
725 nullptr,
726 &length);
727
728 if (!rc)
729 SAL_WARN("xmlsecurity.xmlsec", "CryptDecodeObject failed: " << comphelper::WindowsErrorString(GetLastError()));
730 else
731 {
732 std::vector<char>buffer(length);
733
734 rc = CryptDecodeObject(
735 X509_ASN_ENCODING,
736 X509_KEY_USAGE,
737 pExtn->Value.pbData,
738 pExtn->Value.cbData,
739 0,
740 buffer.data(),
741 &length);
742
743 CRYPT_BIT_BLOB *blob = reinterpret_cast<CRYPT_BIT_BLOB*>(buffer.data());
744 if (!rc)
745 SAL_WARN("xmlsecurity.xmlsec", "CryptDecodeObject failed: " << comphelper::WindowsErrorString(GetLastError()));
746 else if (blob->cbData == 1)
747 usage = blob->pbData[0];
748 else
749 SAL_WARN("xmlsecurity.xmlsec", "CryptDecodeObject(X509_KEY_USAGE) returned unexpected amount of data: " << blob->cbData);
750 }
751 }
752 }
753
754 return usage;
755 }
756
757 /* XServiceInfo */
758 OUString SAL_CALL X509Certificate_MSCryptImpl::getImplementationName()
759 {
760 return "com.sun.star.xml.security.gpg.XCertificate_MsCryptImpl";
761 }
762
763 /* XServiceInfo */
764 sal_Bool SAL_CALL X509Certificate_MSCryptImpl::supportsService(const OUString& serviceName)
765 {
766 return cppu::supportsService(this, serviceName);
767 }
768
769 /* XServiceInfo */
770 Sequence<OUString> SAL_CALL X509Certificate_MSCryptImpl::getSupportedServiceNames()
771 {
772 return { OUString() };
773 }
774
775 namespace xmlsecurity {
776
777 // based on some guesswork and:
778 // https://datatracker.ietf.org/doc/html/rfc1485
779 // https://docs.microsoft.com/en-us/windows/win32/api/wincrypt/nf-wincrypt-certnametostra#CERT_X500_NAME_STR
780 // the main problem appears to be that in values NSS uses \ escapes but CryptoAPI requires " quotes around value
781 static OUString CompatDNNSS(OUString const& rDN)
782 {
783 OUStringBuffer buf(rDN.getLength());
784 enum { DEFAULT, INVALUE, INQUOTE } state(DEFAULT);
785 for (sal_Int32 i = 0; i < rDN.getLength(); ++i)
786 {
787 if (state == DEFAULT)
788 {
789 buf.append(rDN[i]);
790 if (rDN[i] == '=')
791 {
792 if (rDN.getLength() == i+1)
793 {
794 break; // invalid?
795 }
796 else
797 {
798 buf.append('"');
799 state = INVALUE;
800 }
801 }
802 }
803 else if (state == INVALUE)
804 {
805 if (rDN[i] == '+' || rDN[i] == ',' || rDN[i] == ';')
806 {
807 buf.append("\"" + OUStringChar(rDN[i]));
808 state = DEFAULT;
809 }
810 else if (rDN[i] == '\\')
811 {
812 if (rDN.getLength() == i+1)
813 {
814 break; // invalid?
815 }
816 if (rDN[i+1] == '"')
817 {
818 buf.append('"');
819 }
820 buf.append(rDN[i+1]);
821 ++i;
822 }
823 else
824 {
825 buf.append(rDN[i]);
826 }
827 if (i+1 == rDN.getLength())
828 {
829 buf.append('"');
830 state = DEFAULT;
831 }
832 }
833 }
834 return buf.makeStringAndClear();
835 }
836
837 static bool EncodeDistinguishedName(std::u16string_view const rName, CERT_NAME_BLOB & rBlob)
838 {
839 LPCWSTR pszError;
840 if (!CertStrToNameW(X509_ASN_ENCODING,
841 reinterpret_cast<LPCWSTR>(rName.data()), CERT_X500_NAME_STR,
842 nullptr, nullptr, &rBlob.cbData, &pszError))
843 {
844 SAL_INFO("xmlsecurity.xmlsec", "CertStrToNameW failed: " << comphelper::WindowsErrorString(GetLastError()) << "; " << OUString(o3tl::toU(pszError)));
845 return false;
846 }
847 rBlob.pbData = new BYTE[rBlob.cbData];
848 if (!CertStrToNameW(X509_ASN_ENCODING,
849 reinterpret_cast<LPCWSTR>(rName.data()), CERT_X500_NAME_STR,
850 nullptr, rBlob.pbData, &rBlob.cbData, &pszError))
851 {
852 SAL_INFO("xmlsecurity.xmlsec", "CertStrToNameW failed: " << comphelper::WindowsErrorString(GetLastError()) << "; " << OUString(o3tl::toU(pszError)));
853 return false;
854 }
855 return true;
856 }
857
858 bool EqualDistinguishedNames(
859 std::u16string_view const rName1, std::u16string_view const rName2,
860 EqualMode const eMode)
861 {
862 if (eMode == COMPAT_BOTH && !rName1.empty() && rName1 == rName2)
863 { // handle case where both need to be converted
864 return true;
865 }
866 CERT_NAME_BLOB blob1;
867 if (!EncodeDistinguishedName(rName1, blob1))
868 {
869 return false;
870 }
871 CERT_NAME_BLOB blob2;
872 bool ret(false);
873 if (EncodeDistinguishedName(rName2, blob2))
874 {
875 ret = CertCompareCertificateName(X509_ASN_ENCODING,
876 &blob1, &blob2) == TRUE;
877 delete[] blob2.pbData;
878 }
879 if (!ret && eMode == COMPAT_2ND)
880 {
881 CERT_NAME_BLOB blob2compat;
882 if (!EncodeDistinguishedName(CompatDNNSS(OUString(rName2)), blob2compat))
883 {
884 delete[] blob1.pbData;
885 return false;
886 }
887 ret = CertCompareCertificateName(X509_ASN_ENCODING,
888 &blob1, &blob2compat) == TRUE;
889 delete[] blob2compat.pbData;
890 }
891 delete[] blob1.pbData;
892 return ret;
893 }
894
895 } // namespace xmlsecurity
896
897 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */