Skip to content

Commit 04bccd0

Browse files
authored
Merge pull request #55 from denislevin/denisl/cs/DontInstallRootCertificate
cs: Don't Install Root Certificate (CWE-327)
2 parents 3182274 + be3d293 commit 04bccd0

File tree

7 files changed

+119
-0
lines changed

7 files changed

+119
-0
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
* @name Do not add certificates to the system root store.
3+
* @description Application- or user-specific certificates placed in the system root store could
4+
* weaken security for other processing running on the same system.
5+
* @kind problem
6+
* @id cs/adding-cert-to-root-store
7+
* @problem.severity error
8+
* @tags security
9+
* external/cwe/cwe-327
10+
*/
11+
import csharp
12+
import semmle.code.csharp.dataflow.DataFlow::DataFlow
13+
14+
class AddCertToRootStoreConfig extends DataFlow::Configuration {
15+
AddCertToRootStoreConfig() { this = "Adding Certificate To Root Store" }
16+
17+
override predicate isSource(DataFlow::Node source) {
18+
exists(ObjectCreation oc | oc = source.asExpr() |
19+
oc.getType().(RefType).hasQualifiedName("System.Security.Cryptography.X509Certificates.X509Store") and
20+
oc.getArgument(0).(Access).getTarget().hasName("Root")
21+
)
22+
}
23+
24+
override predicate isSink(DataFlow::Node sink) {
25+
exists(MethodCall mc |
26+
(mc.getTarget().hasQualifiedName("System.Security.Cryptography.X509Certificates.X509Store", "Add") or
27+
mc.getTarget().hasQualifiedName("System.Security.Cryptography.X509Certificates.X509Store", "AddRange")) and
28+
sink.asExpr() = mc.getQualifier()
29+
)
30+
}
31+
}
32+
33+
from Expr oc, Expr mc, AddCertToRootStoreConfig config
34+
where config.hasFlow(DataFlow::exprNode(oc), DataFlow::exprNode(mc))
35+
select mc, "Certificate added to the root certificate store."
36+
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
| Test.cs:20:13:20:17 | access to local variable store | Certificate added to the root certificate store. |
2+
| Test.cs:30:13:30:17 | access to local variable store | Certificate added to the root certificate store. |
3+
| Test.cs:75:13:75:17 | access to local variable store | Certificate added to the root certificate store. |
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Security Features/CWE-327/DontInstallRootCert.ql
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
// semmle-extractor-options: /r:System.Security.Cryptography.X509Certificates.dll
2+
3+
using System;
4+
using System.Collections.Generic;
5+
using System.Linq;
6+
using System.Security.Cryptography.X509Certificates;
7+
using System.Text;
8+
using System.Threading.Tasks;
9+
10+
namespace RootCert
11+
{
12+
public class Class1
13+
{
14+
public void InstallRootCert()
15+
{
16+
string file = "mytest.pfx"; // Contains name of certificate file
17+
X509Store store = new X509Store(StoreName.Root);
18+
store.Open(OpenFlags.ReadWrite);
19+
// BAD: adding a certificate to the Root store
20+
store.Add(new X509Certificate2(X509Certificate2.CreateFromCertFile(file)));
21+
store.Close();
22+
}
23+
24+
public void InstallRootCert2()
25+
{
26+
string file = "mytest.pfx"; // Contains name of certificate file
27+
X509Store store = new X509Store(StoreName.Root, StoreLocation.CurrentUser);
28+
store.Open(OpenFlags.ReadWrite);
29+
// BAD: adding a certificate to the Root store
30+
store.Add(new X509Certificate2(X509Certificate2.CreateFromCertFile(file)));
31+
store.Close();
32+
}
33+
34+
public void InstallUserCert()
35+
{
36+
string file = "mytest.pfx"; // Contains name of certificate file
37+
X509Store store = new X509Store(StoreName.My);
38+
store.Open(OpenFlags.ReadWrite);
39+
// GOOD: adding a certificate to My store
40+
store.Add(new X509Certificate2(X509Certificate2.CreateFromCertFile(file)));
41+
store.Close();
42+
}
43+
44+
public void RemoveUserCert()
45+
{
46+
string file = "mytest.pfx"; // Contains name of certificate file
47+
X509Store store = new X509Store(StoreName.My);
48+
store.Open(OpenFlags.ReadWrite);
49+
// GOOD: removing a certificate from My store
50+
store.Remove(new X509Certificate2(X509Certificate2.CreateFromCertFile(file)));
51+
store.Close();
52+
}
53+
54+
public void RemoveRootCert()
55+
{
56+
string file = "mytest.pfx"; // Contains name of certificate file
57+
X509Store store = new X509Store(StoreName.Root);
58+
store.Open(OpenFlags.ReadWrite);
59+
// GOOD: removing a certificate from Root store
60+
store.Remove(new X509Certificate2(X509Certificate2.CreateFromCertFile(file)));
61+
store.Close();
62+
}
63+
64+
public void InstallRootCertRange()
65+
{
66+
string file1 = "mytest1.pfx"; // Contains name of certificate file
67+
string file2 = "mytest2.pfx"; // Contains name of certificate file
68+
var certCollection = new X509Certificate2[] {
69+
new X509Certificate2(X509Certificate2.CreateFromCertFile(file1)),
70+
new X509Certificate2(X509Certificate2.CreateFromCertFile(file2)),
71+
};
72+
X509Store store = new X509Store(StoreName.Root, StoreLocation.CurrentUser);
73+
store.Open(OpenFlags.ReadWrite);
74+
// BAD: adding multiple certificates to the Root store
75+
store.AddRange(new X509Certificate2Collection(certCollection));
76+
store.Close();
77+
}
78+
}
79+
}

csharp/ql/test/query-tests/Security Features/CWE-327/InsufficientKeySize.cs renamed to csharp/ql/test/query-tests/Security Features/CWE-327/InsufficientKeySize/InsufficientKeySize.cs

File renamed without changes.

csharp/ql/test/query-tests/Security Features/CWE-327/InsufficientKeySize.expected renamed to csharp/ql/test/query-tests/Security Features/CWE-327/InsufficientKeySize/InsufficientKeySize.expected

File renamed without changes.

csharp/ql/test/query-tests/Security Features/CWE-327/InsufficientKeySize.qlref renamed to csharp/ql/test/query-tests/Security Features/CWE-327/InsufficientKeySize/InsufficientKeySize.qlref

File renamed without changes.

0 commit comments

Comments
 (0)