2012-05-09 81 views
7

是否有办法立即使CRL(证书撤销列表)缓存失效,导致客户端再次下载CRL?使CRL缓存失效

我想在C#中实现它,而不是诉诸命令行'certutil.exe'。

更妙的是要能设置失效时间(如UtcNow +12小时)

+0

如果您的问题仅限于cmd窗口,则可以在没有可见的命令行窗口的情况下运行进程(certutil)。 – Ondra

回答

0

我知道你不想使用certutil.exe但这种方式,你可以在应用程序运行不cmd窗口显示如果那是你不想要的。

public bool ClearCRLCache() 
{ 
    var pw = new ProcessWrapper(); 
    var result = pw.Start("certutil.exe", "-urlcache * delete"); 
    // -2147024637 is the exitcode when the urlcache is empty 
    return (result == 0 || result == -2147024637); 
} 

类ProcessWrapper:

public class ProcessWrapper 
{ 
    /// <summary> 
    /// Output from stderr 
    /// </summary> 
    public string StdErr { get; private set; } 

    /// <summary> 
    /// Output from stdout 
    /// </summary> 
    public string StdOut { get; private set; } 

    /// <summary> 
    /// Starts a process 
    /// </summary> 
    /// <param name="command">Executable filename</param> 
    /// <returns>Process exitcode</returns> 
    public int Start(string command) 
    { 
     return Start(command, ""); 
    } 

    /// <summary> 
    /// Starts a process with commandline arguments 
    /// </summary> 
    /// <param name="command">Executable filename</param> 
    /// <param name="arguments">Commanline arguments for the process</param> 
    /// <returns>Process exitcode</returns> 
    public int Start(string command, string arguments) 
    { 
     return Start(command, arguments, ""); 
    } 

    /// <summary> 
    /// Starts a process with commandline arguments and working directory 
    /// </summary> 
    /// <param name="command">Executable filename</param> 
    /// <param name="arguments">Commanline arguments for the process</param> 
    /// <param name="workingDirectory">Working directory for the process</param> 
    /// <returns>Process exitcode</returns> 
    public int Start(string command, string arguments, string workingDirectory) 
    { 
     StdErr = ""; 
     StdOut = ""; 
     var proc = new Process(); 
     proc.StartInfo.FileName = command; 
     proc.StartInfo.Arguments = arguments; 
     proc.StartInfo.WorkingDirectory = workingDirectory; 
     proc.StartInfo.UseShellExecute = false; 
     proc.StartInfo.RedirectStandardOutput = true; 
     proc.StartInfo.RedirectStandardError = true; 
     proc.EnableRaisingEvents = true; 
     proc.StartInfo.CreateNoWindow = true; 

     // Write messages from stderr to StdErr property 
     proc.ErrorDataReceived += (sender, e) => 
     { 
      StdErr += e.Data + Environment.NewLine; 
     }; 

     // Write messages from stdout to StdOut property 
     proc.OutputDataReceived += (sender, e) => 
     { 
      StdOut += e.Data + Environment.NewLine; 
     }; 

     proc.Start(); 

     proc.BeginErrorReadLine(); 
     proc.BeginOutputReadLine(); 

     proc.WaitForExit(); 
     return proc.ExitCode; 
    } 
} 
1

我已经实现了这样的解决方案,它的客户机上更新CRL缓存每隔X小时,具体的调度设置。 您可以阅读有关CRL的位置: http://social.technet.microsoft.com/wiki/contents/articles/4954.certificate-status-and-revocation-checking.aspx

CRL缓存存储在客户机上的特殊文件夹和包括存储在元数据和内容文件夹两个文件。这些文件夹放在“C:\ Documents and Settings {用户名} \ Application Data \ Microsoft \ CryptnetUrlCache”中,并且每台计算机的缓存位置为“%WINDIR%\ System32 \ config \ SystemProfile \ Application Data \ Microsoft \ CryptnetUrlCache” 。 Cahce文件被命名为CRL url的MD5哈希总和。 “元数据”文件夹中的文件包含一些常量数据,上次更新日期,CRL URL,CRL文件大小等。 “Content”文件夹中的文件是CRL文件本身,并且与“元数据”中的文件具有相同的名称。 我解析元文件,检查它是否无效,并通过CRL url加载新的CRL文件,将其放到“Content”文件夹并重建元数据文件。 我使用BouncyCastle库来达到这些目的。作为调度库我使用Quartz.Net。