Exemplo n.º 1
0
        /// <summary>Fetches the CRL bytes from an URL.</summary>
        /// <remarks>
        /// Fetches the CRL bytes from an URL.
        /// If no url is passed as parameter, the url will be obtained from the certificate.
        /// If you want to load a CRL from a local file, subclass this method and pass an
        /// URL with the path to the local file to this method. An other option is to use
        /// the CrlClientOffline class.
        /// </remarks>
        /// <seealso cref="ICrlClient.GetEncoded(Org.BouncyCastle.X509.X509Certificate, System.String)"/>
        public virtual ICollection <byte[]> GetEncoded(X509Certificate checkCert, String url)
        {
            if (checkCert == null)
            {
                return(null);
            }
            IList <Uri> urllist = new List <Uri>(urls);

            if (urllist.Count == 0)
            {
                LOGGER.Info("Looking for CRL for certificate " + checkCert.SubjectDN);
                try {
                    if (url == null)
                    {
                        url = CertificateUtil.GetCRLURL(checkCert);
                    }
                    if (url == null)
                    {
                        throw new ArgumentException("Passed url can not be null.");
                    }
                    urllist.Add(new Uri(url));
                    LOGGER.Info("Found CRL url: " + url);
                }
                catch (Exception e) {
                    LOGGER.Info("Skipped CRL url: " + e.Message);
                }
            }
            IList <byte[]> ar = new List <byte[]>();

            foreach (Uri urlt in urllist)
            {
                try {
                    LOGGER.Info("Checking CRL: " + urlt);
                    Stream       inp  = SignUtils.GetHttpResponse(urlt);
                    byte[]       buf  = new byte[1024];
                    MemoryStream bout = new MemoryStream();
                    while (true)
                    {
                        int n = inp.JRead(buf, 0, buf.Length);
                        if (n <= 0)
                        {
                            break;
                        }
                        bout.Write(buf, 0, n);
                    }
                    inp.Dispose();
                    ar.Add(bout.ToArray());
                    LOGGER.Info("Added CRL found at: " + urlt);
                }
                catch (Exception e) {
                    LOGGER.Info("Skipped CRL: " + e.Message + " for " + urlt);
                }
            }
            return(ar);
        }