File tree Expand file tree Collapse file tree
csharp/ql/src/Security Features/CWE-327 Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ <!DOCTYPE qhelp PUBLIC
2+ "-//Semmle//qhelp//EN"
3+ "qhelp.dtd">
4+ <qhelp >
5+ <overview >
6+
7+ <p >
8+ Adding certificates to the system root certificate store can weaken security for all applications
9+ running on the same machine. Any certificate in the root store is trusted as a certificate
10+ authority, which means a malicious or compromised certificate could be used to intercept encrypted
11+ communications or impersonate trusted services for all users on the system.
12+ </p >
13+
14+ </overview >
15+ <recommendation >
16+
17+ <p >
18+ Instead of adding certificates to the system root store, use an application-specific certificate
19+ store such as <code >StoreName.My</code > or <code >StoreName.CertificateAuthority</code >. If root
20+ trust is genuinely required, ensure the operation is restricted to controlled environments and
21+ is removed after use.
22+ </p >
23+
24+ </recommendation >
25+ <example >
26+
27+ <p >
28+ The following example adds a certificate directly to the root store, which weakens security for
29+ all applications on the system.
30+ </p >
31+
32+ <sample src =" DontInstallRootCertBad.cs" />
33+
34+ <p >
35+ The following example uses a user-specific store instead, limiting the scope of the trusted
36+ certificate.
37+ </p >
38+
39+ <sample src =" DontInstallRootCertGood.cs" />
40+
41+ </example >
42+ <references >
43+ <li >
44+ Microsoft: <a href =" https://learn.microsoft.com/en-us/dotnet/api/system.security.cryptography.x509certificates.x509store" >X509Store Class</a >.
45+ </li >
46+ <li >
47+ Microsoft: <a href =" https://learn.microsoft.com/en-us/dotnet/api/system.security.cryptography.x509certificates.storename" >StoreName Enum</a >.
48+ </li >
49+ <li >
50+ CWE: <a href =" https://cwe.mitre.org/data/definitions/327.html" >CWE-327: Use of a Broken or Risky Cryptographic Algorithm</a >.
51+ </li >
52+ </references >
53+ </qhelp >
Original file line number Diff line number Diff line change 1+ using System . Security . Cryptography . X509Certificates ;
2+
3+ public class RootCertExample
4+ {
5+ public void AddCertificateToRootStore ( X509Certificate2 cert )
6+ {
7+ // BAD: Adding a certificate to the system root store weakens security
8+ // for all applications on the machine.
9+ var store = new X509Store ( StoreName . Root , StoreLocation . LocalMachine ) ;
10+ store . Open ( OpenFlags . ReadWrite ) ;
11+ store . Add ( cert ) ;
12+ store . Close ( ) ;
13+ }
14+ }
Original file line number Diff line number Diff line change 1+ using System . Security . Cryptography . X509Certificates ;
2+
3+ public class RootCertExample
4+ {
5+ public void AddCertificateToUserStore ( X509Certificate2 cert )
6+ {
7+ // GOOD: Using a user-specific store limits the scope of the trusted certificate.
8+ var store = new X509Store ( StoreName . My , StoreLocation . CurrentUser ) ;
9+ store . Open ( OpenFlags . ReadWrite ) ;
10+ store . Add ( cert ) ;
11+ store . Close ( ) ;
12+ }
13+ }
You can’t perform that action at this time.
0 commit comments