public X509Certificate2 GetRootCertificate() { if (this.certRoot != null) { return(this.certRoot); } X509Certificate2 result = DefaultCertificateProvider.LoadCertificateFromWindowsStore(CONFIG.sMakeCertRootCN); this.certRoot = result; return(result); }
internal X509Certificate2 LoadOrCreateCertificate(string sHostname, out bool bAttemptedCreation) { bAttemptedCreation = false; X509Certificate2 x509Certificate = DefaultCertificateProvider.LoadCertificateFromWindowsStore(sHostname); if (x509Certificate != null) { return(x509Certificate); } bAttemptedCreation = true; x509Certificate = this.CreateCert(sHostname, false); if (x509Certificate == null) { FiddlerApplication.Log.LogFormat("!Fiddler.CertMaker> Tried to create cert for {0}, but can't find it from thread {1}!", new object[] { sHostname, Thread.CurrentThread.ManagedThreadId }); } return(x509Certificate); }
private X509Certificate2 CreateCert(string sHostname, bool isRoot) { if (sHostname.IndexOfAny(new char[] { '"', '\r', '\n', '\0' }) != -1) { return(null); } if (!isRoot && this.GetRootCertificate() == null) { try { this.GetWriterLock(); if (this.GetRootCertificate() == null && !this.CreateRootCertificate()) { FiddlerApplication.DoNotifyUser("Creation of the root certificate was not successful.", "Certificate Error"); X509Certificate2 result = null; return(result); } } finally { this.FreeWriterLock(); } } if (!File.Exists(this._sMakeCertLocation)) { FiddlerApplication.DoNotifyUser("Cannot locate:\n\t\"" + this._sMakeCertLocation + "\"\n\nPlease move makecert.exe to the Fiddler installation directory.", "MakeCert.exe not found"); throw new FileNotFoundException("Cannot locate: " + this._sMakeCertLocation + ". Please move makecert.exe to the Fiddler installation directory."); } X509Certificate2 x509Certificate = null; string stringPref = FiddlerApplication.Prefs.GetStringPref("fiddler.certmaker.DateFormatString", "MM/dd/yyyy"); int num = -FiddlerApplication.Prefs.GetInt32Pref("fiddler.certmaker.GraceDays", 366); DateTime dateTime = DateTime.Now.AddDays((double)num); string text; if (isRoot) { text = string.Format(CONFIG.sMakeCertParamsRoot, new object[] { sHostname, CONFIG.sMakeCertSubjectO, CONFIG.sMakeCertRootCN, dateTime.ToString(stringPref, CultureInfo.InvariantCulture), FiddlerApplication.Prefs.GetStringPref("fiddler.certmaker.Root.extraparams", string.Empty) }); } else { text = string.Format(CONFIG.sMakeCertParamsEE, new object[] { sHostname, CONFIG.sMakeCertSubjectO, CONFIG.sMakeCertRootCN, dateTime.ToString(stringPref, CultureInfo.InvariantCulture), FiddlerApplication.Prefs.GetStringPref("fiddler.certmaker.EE.extraparams", string.Empty) }); } int num2; string executableOutput; try { this.GetWriterLock(); X509Certificate2 x509Certificate2; if (!this.certServerCache.TryGetValue(sHostname, out x509Certificate2)) { x509Certificate2 = DefaultCertificateProvider.LoadCertificateFromWindowsStore(sHostname); } if (x509Certificate2 != null) { if (CONFIG.bDebugCertificateGeneration) { FiddlerApplication.Log.LogFormat("/Fiddler.CertMaker>{1} A racing thread already successfully CreatedCert({0})", new object[] { sHostname, Thread.CurrentThread.ManagedThreadId }); } X509Certificate2 result = x509Certificate2; return(result); } if (CONFIG.bDebugCertificateGeneration) { FiddlerApplication.Log.LogFormat("/Fiddler.CertMaker> Invoking makecert.exe with arguments: {0}", new object[] { text }); } executableOutput = Utilities.GetExecutableOutput(this._sMakeCertLocation, text, out num2); if (CONFIG.bDebugCertificateGeneration) { FiddlerApplication.Log.LogFormat("/Fiddler.CertMaker>{3}-CreateCert({0}) => ({1}){2}", new object[] { sHostname, num2, (num2 == 0) ? "." : ("\r\n" + executableOutput), Thread.CurrentThread.ManagedThreadId }); } if (num2 == 0) { int num3 = 6; do { x509Certificate = DefaultCertificateProvider.LoadCertificateFromWindowsStore(sHostname); Thread.Sleep(50 * (6 - num3)); if (CONFIG.bDebugCertificateGeneration && x509Certificate == null) { FiddlerApplication.Log.LogFormat("!WARNING: Couldn't find certificate for {0} on try #{1}", new object[] { sHostname, 6 - num3 }); } num3--; if (x509Certificate != null) { break; } }while (num3 >= 0); } if (x509Certificate != null) { if (isRoot) { this.certRoot = x509Certificate; } else { this.certServerCache[sHostname] = x509Certificate; } } } finally { this.FreeWriterLock(); } if (x509Certificate == null) { string text2 = string.Format("Creation of the interception certificate failed.\n\nmakecert.exe returned {0}.\n\n{1}", num2, executableOutput); FiddlerApplication.Log.LogFormat("Fiddler.CertMaker> [{0} {1}] Returned Error: {2} ", new object[] { this._sMakeCertLocation, text, text2 }); if (CONFIG.bDebugCertificateGeneration) { FiddlerApplication.DoNotifyUser(text2, "Unable to Generate Certificate"); } } return(x509Certificate); }