|
| 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 | +} |
0 commit comments