/// <summary>
        /// Returns the public key of this server from the certificate file.
        /// </summary>
        /// <returns></returns>
        public static NodePublicKey GetServerPublicKey()
        {
            FieldPublicKey key = new FieldPublicKey(
                GetServerPublicKeyString());

            return(NodePublicKey.BuildWith(key));
        }
        /// <summary>
        /// Checks to see if the given public key is on the Key Ring.
        /// Note that if the Key Ring is empty, then this function will
        /// add this key to the Key Ring and return true!  This is the
        /// intended behavior (to bond with the first remote host that
        /// connects with it).  If you don't want that behavior, then
        /// provide your own key file with a "safe" public key.
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public static bool PublicKeyRingHasKey(NodePublicKey key)
        {
            if (key == null)
            {
                throw new ArgumentNullException("key");
            }

            lock (Instance.m_keyRing_Lock)
            {
                KeyRingInit(); //make sure we've loaded the keyring
                if (Instance.m_keyRing.Keys.Count == 0)
                {
                    //No keys, so we bond with the first one,
                    //like a newly hatched chick
                    Instance.m_keyRing = Instance.m_keyRing.Keys.Append(key);
                    KeyRingSave();
                    return(true);
                }
                else
                {
                    //check against each key individually
                    bool found = false;
                    foreach (NodePublicKey pubKey in Instance.m_keyRing.Keys)
                    {
                        if (pubKey.Key == key.Key)
                        {
                            found = true;
                            break;
                        }
                    }
                    return(found);
                }
            }
        }
        /// <summary>
        /// Removes an existing public key from the KeyRing (if it's there)
        /// and then saves the new ring to the PublicKeyRing file.
        /// </summary>
        /// <param name="key">New public key to remove</param>
        public static void PublicKeyRingRemove(NodePublicKey key)
        {
            if (key == null)
            {
                throw new ArgumentNullException("key");
            }

            lock (Instance.m_keyRing_Lock)
            {
                KeyRingInit(); //make sure we've loaded the keyring
                NodePublicKey findKey = null;
                foreach (NodePublicKey item in Instance.m_keyRing.Keys)
                {
                    if (item.Key == key.Key)
                    {
                        findKey = item;
                        break;
                    }
                }
                if (findKey != null)
                {
                    Instance.m_keyRing = Instance.m_keyRing.Keys.Remove(findKey);
                    KeyRingSave();
                }
            }
        }
        /// <summary>
        /// Adds a new public key to the KeyRing (if it's not already on there)
        /// and then saves the new ring to the PublicKeyRing file.
        /// </summary>
        /// <param name="key">New public key to add</param>
        public static void PublicKeyRingAdd(NodePublicKey key)
        {
            if (key == null)
            {
                throw new ArgumentNullException("key");
            }

            lock (Instance.m_keyRing_Lock)
            {
                KeyRingInit(); //make sure we've loaded the keyring
                //only add if it doesn't already exist
                if (!PublicKeyRingHasKey(key))
                {
                    Instance.m_keyRing = Instance.m_keyRing.Keys.Append(key);
                    KeyRingSave();
                }
            }
        }
 static bool CertificateValidationCallback(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
 {
     if (sslPolicyErrors != SslPolicyErrors.None)
     {
         if (sslPolicyErrors == SslPolicyErrors.RemoteCertificateChainErrors)
         {
             //Make sure the only error is an untrusted root
             //(because we're assuming it's a self-signed certificate and
             // we're going to check it against an internal list of public keys)
             bool failed = false;
             foreach (X509ChainStatus status in chain.ChainStatus)
             {
                 if (status.Status != X509ChainStatusFlags.UntrustedRoot)
                 {
                     failed = true;
                     break;
                 }
             }
             //Pull the public key out of the certificate
             NodePublicKey key = NodePublicKey.BuildWith(new FieldPublicKey(certificate.GetPublicKeyString()));
             if (!failed && PublicKeyRingHasKey(key))
             {
                 return(true);
             }
             else
             {
                 Console.WriteLine("SSL Certificate Validation Error!");
                 Console.WriteLine(sslPolicyErrors.ToString());
                 return(false);
             }
         }
         else
         {
             Console.WriteLine("SSL Certificate Validation Error!");
             Console.WriteLine(sslPolicyErrors.ToString());
             return(false);
         }
     }
     else
     {
         return(true);
     }
 }
Exemplo n.º 6
0
        public static NodePublicKey BuildWith(FieldPublicKey Key)
        {
            //build fields
            Dictionary <FieldIdentifier, FieldBase> mutableFields =
                new Dictionary <FieldIdentifier, FieldBase>();

            mutableFields.Add(new FieldIdentifier(m_KeyName), Key);

            //build children
            KeyedNodeCollection <NodeBase> mutableChildren =
                new KeyedNodeCollection <NodeBase>();

            //build node
            NodePublicKey Builder = new NodePublicKey(
                new ReadOnlyDictionary <FieldIdentifier, FieldBase>(mutableFields),
                new ReadOnlyCollection <NodeBase>(mutableChildren));

            return(Builder);
        }
Exemplo n.º 7
0
        /// <summary>
        /// Checks to see if the given public key is on the Key Ring.
        /// Note that if the Key Ring is empty, then this function will
        /// add this key to the Key Ring and return true!  This is the 
        /// intended behavior (to bond with the first remote host that
        /// connects with it).  If you don't want that behavior, then 
        /// provide your own key file with a "safe" public key.
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public static bool PublicKeyRingHasKey(NodePublicKey key)
        {
            if (key == null)
            {
                throw new ArgumentNullException("key");
            }

            lock (Instance.m_keyRing_Lock)
            {
                KeyRingInit(); //make sure we've loaded the keyring
                if (Instance.m_keyRing.Keys.Count == 0)
                {
                    //No keys, so we bond with the first one, 
                    //like a newly hatched chick
                    Instance.m_keyRing = Instance.m_keyRing.Keys.Append(key);
                    KeyRingSave();
                    return true;
                }
                else
                {
                    //check against each key individually
                    bool found = false;
                    foreach (NodePublicKey pubKey in Instance.m_keyRing.Keys)
                    {
                        if (pubKey.Key == key.Key)
                        {
                            found = true;
                            break;
                        }
                    }
                    return found;
                }
            }
        }
Exemplo n.º 8
0
        /// <summary>
        /// Removes an existing public key from the KeyRing (if it's there)
        /// and then saves the new ring to the PublicKeyRing file.
        /// </summary>
        /// <param name="key">New public key to remove</param>
        public static void PublicKeyRingRemove(NodePublicKey key)
        {
            if (key == null)
            {
                throw new ArgumentNullException("key");
            }

            lock (Instance.m_keyRing_Lock)
            {
                KeyRingInit(); //make sure we've loaded the keyring
                NodePublicKey findKey = null;
                foreach (NodePublicKey item in Instance.m_keyRing.Keys)
                {
                    if (item.Key == key.Key)
                    {
                        findKey = item;
                        break;
                    }
                }
                if (findKey != null)
                {
                    Instance.m_keyRing = Instance.m_keyRing.Keys.Remove(findKey);
                    KeyRingSave();
                }
            }
        }
Exemplo n.º 9
0
        /// <summary>
        /// Adds a new public key to the KeyRing (if it's not already on there)
        /// and then saves the new ring to the PublicKeyRing file.
        /// </summary>
        /// <param name="key">New public key to add</param>
        public static void PublicKeyRingAdd(NodePublicKey key)
        {
            if (key == null)
            {
                throw new ArgumentNullException("key");
            }

            lock (Instance.m_keyRing_Lock)
            {
                KeyRingInit(); //make sure we've loaded the keyring
                //only add if it doesn't already exist
                if (!PublicKeyRingHasKey(key))
                {
                    Instance.m_keyRing = Instance.m_keyRing.Keys.Append(key);
                    KeyRingSave();
                }
            }
        }
Exemplo n.º 10
0
        public static NodePublicKey BuildWith(FieldPublicKey Key)
        {
            //build fields
            Dictionary<FieldIdentifier, FieldBase> mutableFields =
                new Dictionary<FieldIdentifier, FieldBase>();
            mutableFields.Add(new FieldIdentifier(m_KeyName), Key);

            //build children
            KeyedNodeCollection<NodeBase> mutableChildren =
                new KeyedNodeCollection<NodeBase>();

            //build node
            NodePublicKey Builder = new NodePublicKey(
                new ReadOnlyDictionary<FieldIdentifier, FieldBase>(mutableFields),
                new ReadOnlyCollection<NodeBase>(mutableChildren));

            return Builder;
        }