Esempio n. 1
0
 // get a pre-builded chain
 public X509Chain(X509CertificateCollection chain) : this()
 {
     _chain = new X509CertificateCollection();
     _chain.AddRange(chain);
 }
Esempio n. 2
0
        public bool Build(X509Certificate leaf)
        {
            _status = X509ChainStatusFlags.NoError;
            if (_chain == null)
            {
                // chain not supplied - we must build it ourselve
                _chain = new X509CertificateCollection();
                X509Certificate x   = leaf;
                X509Certificate tmp = x;
                while ((x != null) && (!x.IsSelfSigned))
                {
                    tmp = x;                     // last valid
                    _chain.Add(x);
                    x = FindCertificateParent(x);
                }
                // find a trusted root
                _root = FindCertificateRoot(tmp);
            }
            else
            {
                // chain supplied - still have to check signatures!
                int last = _chain.Count;
                if (last > 0)
                {
                    if (IsParent(leaf, _chain [0]))
                    {
                        int i = 1;
                        for (; i < last; i++)
                        {
                            if (!IsParent(_chain [i - 1], _chain [i]))
                            {
                                break;
                            }
                        }
                        if (i == last)
                        {
                            _root = FindCertificateRoot(_chain [last - 1]);
                        }
                    }
                }
                else
                {
                    // is the leaf a root ? (trusted or untrusted)
                    _root = FindCertificateRoot(leaf);
                }
            }

            // validate the chain
            if ((_chain != null) && (_status == X509ChainStatusFlags.NoError))
            {
                foreach (X509Certificate x in _chain)
                {
                    // validate dates for each certificate in the chain
                    // note: we DO NOT check for nested date/time
                    if (!IsValid(x))
                    {
                        return(false);
                    }
                }
                // check leaf
                if (!IsValid(leaf))
                {
                    // switch status code if the failure is expiration
                    if (_status == X509ChainStatusFlags.NotTimeNested)
                    {
                        _status = X509ChainStatusFlags.NotTimeValid;
                    }
                    return(false);
                }
                // check root
                if ((_root != null) && !IsValid(_root))
                {
                    return(false);
                }
            }
            return(_status == X509ChainStatusFlags.NoError);
        }
Esempio n. 3
0
        // constructors

        public X509Chain()
        {
            certs = new X509CertificateCollection();
        }
Esempio n. 4
0
 public void LoadCertificates(X509CertificateCollection collection)
 {
     certs.AddRange(collection);
 }