diff --git a/csharp/ql/src/Security Features/CWE-327/DontInstallRootCert.qhelp b/csharp/ql/src/Security Features/CWE-327/DontInstallRootCert.qhelp new file mode 100644 index 000000000000..f7f93ceef299 --- /dev/null +++ b/csharp/ql/src/Security Features/CWE-327/DontInstallRootCert.qhelp @@ -0,0 +1,53 @@ + + + + +

+ Adding certificates to the system root certificate store can weaken security for all applications + running on the same machine. Any certificate in the root store is trusted as a certificate + authority, which means a malicious or compromised certificate could be used to intercept encrypted + communications or impersonate trusted services for all users on the system. +

+ +
+ + +

+ Instead of adding certificates to the system root store, use an application-specific certificate + store such as StoreName.My or StoreName.CertificateAuthority. If root + trust is genuinely required, ensure the operation is restricted to controlled environments and + is removed after use. +

+ +
+ + +

+ The following example adds a certificate directly to the root store, which weakens security for + all applications on the system. +

+ + + +

+ The following example uses a user-specific store instead, limiting the scope of the trusted + certificate. +

+ + + +
+ +
  • + Microsoft: X509Store Class. +
  • +
  • + Microsoft: StoreName Enum. +
  • +
  • + CWE: CWE-327: Use of a Broken or Risky Cryptographic Algorithm. +
  • +
    +
    diff --git a/csharp/ql/src/Security Features/CWE-327/DontInstallRootCertBad.cs b/csharp/ql/src/Security Features/CWE-327/DontInstallRootCertBad.cs new file mode 100644 index 000000000000..1fad1313b618 --- /dev/null +++ b/csharp/ql/src/Security Features/CWE-327/DontInstallRootCertBad.cs @@ -0,0 +1,14 @@ +using System.Security.Cryptography.X509Certificates; + +public class RootCertExample +{ + public void AddCertificateToRootStore(X509Certificate2 cert) + { + // BAD: Adding a certificate to the system root store weakens security + // for all applications on the machine. + var store = new X509Store(StoreName.Root, StoreLocation.LocalMachine); + store.Open(OpenFlags.ReadWrite); + store.Add(cert); + store.Close(); + } +} diff --git a/csharp/ql/src/Security Features/CWE-327/DontInstallRootCertGood.cs b/csharp/ql/src/Security Features/CWE-327/DontInstallRootCertGood.cs new file mode 100644 index 000000000000..55a7fc660ee9 --- /dev/null +++ b/csharp/ql/src/Security Features/CWE-327/DontInstallRootCertGood.cs @@ -0,0 +1,13 @@ +using System.Security.Cryptography.X509Certificates; + +public class RootCertExample +{ + public void AddCertificateToUserStore(X509Certificate2 cert) + { + // GOOD: Using a user-specific store limits the scope of the trusted certificate. + var store = new X509Store(StoreName.My, StoreLocation.CurrentUser); + store.Open(OpenFlags.ReadWrite); + store.Add(cert); + store.Close(); + } +}