How to create a FiddlerCore Custom Root Certificate

105 Views Asked by At

I am new to FiddlerCore and after trial and error I was able to get most of my code working as intended. The main issue I am now having is creating and trusting a custom root certificate. If I read the documentation correctly, in order for us to not have to install a temporary certificate each time the program is ran, we need to create, read, and trust our own custom certificate. I have placed the FiddlerCore document scripts in my program but it is not recognizing the read/write rootcertificateandandprivatekeyto pkcs12file, attached below is my code:

        private void createcert()
        {
            BCCertMaker.BCCertMaker certProvider = new BCCertMaker.BCCertMaker();
            CertMaker.oCertProvider = certProvider;

            if (!CertMaker.rootCertExists())
            {
                string rootCertificatePath = @"Path\To\Your\Root\Certificate\RootCertificate.p12";
                string rootCertificatePassword = "S0m3T0pS3cr3tP4ssw0rd";
                if (!File.Exists(rootCertificatePath))
                {
                    certProvider.CreateRootCertificate();
                    certProvider.WriteRootCertificateAndPrivateKeyToPkcs12File(rootCertificatePath, rootCertificatePassword);
                }

                /*          if (!CertMaker.rootCertExists())
                          {
                              CertMaker.createRootCert();
                              CertMaker.trustRootCert();
                          }*/
            }
        }
        private void readcert()
        {
            BCCertMaker.BCCertMaker certProvider = new BCCertMaker.BCCertMaker();
            CertMaker.oCertProvider = certProvider;
            string rootCertificatePath = @"Path\To\Your\Root\Certificate\RootCertificate.p12";
            string rootCertificatePassword = "S0m3T0pS3cr3tP4ssw0rd";
            if (File.Exists(rootCertificatePath))
            {
                certProvider.ReadRootCertificateAndPrivateKeyFromPkcs12File(rootCertificatePath, rootCertificatePassword);
            }
        }

        private void installcert()
        {
            if (!CertMaker.rootCertIsTrusted())
            {
                CertMaker.trustRootCert();
            }
        }

The Error code is this:

Severity Code Description Project File Line Suppression State Error CS1061 'BCCertMaker' does not contain a definition for 'WriteRootCertificateAndPrivateKeyToPkcs12File' and no accessible extension method 'WriteRootCertificateAndPrivateKeyToPkcs12File' accepting a first argument of type 'BCCertMaker' could be found (are you missing a using directive or an assembly reference?) FiddlerCore tuto 1 C:\Users\SHADOWGHOSTS\Desktop\FiddlerCore-Tuto-main\FiddlerCore tuto 1\Form1.cs 147 Active

and the documentation I was following is here: https://docs.telerik.com/fiddlercore/basic-usage/use-custom-root-certificate

I added this code and it is giving me an error as shown above.

1

There are 1 best solutions below

0
Valerii Bas On

I'm experiencing a similar issue. I suspect the problem may be related to using an outdated version of the FiddlerCore library. To address this, you can implement the ICertificateProvider interface and create a custom GetRootCertificate method. This should help resolve the issue.

public class CustomProvider : ICertificateProvider
{
    private ICertificateProvider bcProvider;

    public CustomProvider()
    {
        bcProvider = new BCCertMaker.BCCertMaker();
    }
    public bool ClearCertificateCache()
    {
        return bcProvider.ClearCertificateCache();
    }

    public bool CreateRootCertificate()
    {
        return bcProvider.CreateRootCertificate();
    }

    public X509Certificate2 GetCertificateForHost(string sHostname)
    {
        //You might want to override certificate creation here
        return bcProvider.GetCertificateForHost(sHostname);
    }

    public X509Certificate2 GetRootCertificate()
    {
        //Here you have to implement getting your own Certificate
        throw new NotImplementedException();
    }

    public bool rootCertIsTrusted(out bool bUserTrusted, out bool bMachineTrusted)
    {
        return bcProvider.rootCertIsTrusted(out bUserTrusted, out bMachineTrusted);
    }

    public bool TrustRootCertificate()
    {
        return bcProvider.TrustRootCertificate();
    }
}

Then you can utilize your provider.

var customProvider = new CustomProvider();
CertMaker.oCertProvider = customProvider;