コード例 #1
0
 /// <summary>
 /// Attempts to move a self-signed certificate to the root store.
 /// </summary>
 /// <returns>true if succeeded, else false</returns>
 internal bool CreateTrustedRootCertificate()
 {
     rootCertificate = GetRootCertificate();
     if (rootCertificate != null)
     {
         return(true);
     }
     try
     {
         rootCertificate = CreateCertificate(RootCertificateName, true);
     }
     catch (Exception e)
     {
         ProxyServer.ExceptionFunc(e);
     }
     if (rootCertificate != null)
     {
         try
         {
             var fileName = Path.Combine(System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location), "rootCert.pfx");
             File.WriteAllBytes(fileName, rootCertificate.Export(X509ContentType.Pkcs12));
         }
         catch (Exception e)
         {
             ProxyServer.ExceptionFunc(e);
         }
     }
     return(rootCertificate != null);
 }
コード例 #2
0
        /// <summary>
        /// Create an SSL certificate
        /// </summary>
        /// <param name="store"></param>
        /// <param name="certificateName"></param>
        /// <param name="isRootCertificate"></param>
        /// <returns></returns>
        public virtual X509Certificate2 CreateCertificate(string certificateName, bool isRootCertificate)
        {
            try
            {
                if (certificateCache.ContainsKey(certificateName))
                {
                    var cached = certificateCache[certificateName];
                    cached.LastAccess = DateTime.Now;
                    return(cached.Certificate);
                }
            }
            catch
            {
            }
            X509Certificate2 certificate = null;

            lock (string.Intern(certificateName))
            {
                if (certificateCache.ContainsKey(certificateName) == false)
                {
                    try
                    {
                        certificate = certEngine.CreateCert(certificateName, isRootCertificate, rootCertificate);
                    }
                    catch (Exception e)
                    {
                        ProxyServer.ExceptionFunc(e);
                    }
                    if (certificate != null && !certificateCache.ContainsKey(certificateName))
                    {
                        certificateCache.Add(certificateName, new CachedCertificate()
                        {
                            Certificate = certificate
                        });
                    }
                }
                else
                {
                    if (certificateCache.ContainsKey(certificateName))
                    {
                        var cached = certificateCache[certificateName];
                        cached.LastAccess = DateTime.Now;
                        return(cached.Certificate);
                    }
                }
            }



            return(certificate);
        }
コード例 #3
0
        X509Certificate2 GetRootCertificate()
        {
            var fileName = Path.Combine(System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location), "rootCert.pfx");

            if (File.Exists(fileName))
            {
                try
                {
                    return(new X509Certificate2(fileName, string.Empty, X509KeyStorageFlags.Exportable));
                }
                catch (Exception e)
                {
                    ProxyServer.ExceptionFunc(e);
                    return(null);
                }
            }
            return(null);
        }