示例#1
0
        /// <summary>
        /// Verifies a key user id certificetion revocation status
        /// </summary>
        /// <param name="keyID">the key to verify</param>
        /// <param name="userID">the user id to verify</param>
        /// <param name="certifierKeyID">the key that issued the certification</param>
        /// <returns>the revocation status of the user id</returns>
        public bool isRevoked(ulong keyID, string userID, ulong certifierKeyID)
        {
            TransportablePublicKey tpkKey = this.Find(keyID, true);

            if (tpkKey == null)
            {
                return(false);
            }
            bool            found        = false;
            CertifiedUserID toBeVerified = null;

            foreach (CertifiedUserID cui in tpkKey.Certifications)
            {
                if (cui.UserID.UserID == userID)
                {
                    found        = true;
                    toBeVerified = cui;
                    break;
                }
            }
            if (!found)
            {
                throw new Exception("UserId not found among Key certificates");
            }
            toBeVerified.Validate(tpkKey.PrimaryKey, this);
            foreach (SignaturePacket sign in toBeVerified.Certificates)
            {
                if (sign.SignatureType == SignatureTypes.CertificationRevocationSignature && sign.KeyID == certifierKeyID && sign.SignatureStatus == SignatureStatusTypes.Valid && sign.isRevocable())
                {
                    return(true);
                }
            }
            return(false);
        }
示例#2
0
        public string GetPublicKeyProperties(ulong lKeyID)
        {
            TransportablePublicKey tpkKey = pkrKeyRing.Find(lKeyID, false);

            XmlDocument xmlDoc = new XmlDocument();

            XmlElement xmlPublicKey = xmlDoc.CreateElement("PublicKey");

            xmlPublicKey.SetAttribute("keyid", "0x" + tpkKey.PrimaryKey.KeyID.ToString("x"));
            xmlPublicKey.SetAttribute("fingerprint", tpkKey.PrimaryKey.Fingerprint.ToString(16));
            xmlPublicKey.SetAttribute("created", tpkKey.PrimaryKey.TimeCreated.Ticks.ToString());
            try {
                xmlPublicKey.SetAttribute("expiration", tpkKey.KeyExpirationTime.Ticks.ToString());
            } catch (System.Exception) {
                xmlPublicKey.SetAttribute("expiration", "never");
            }
            xmlPublicKey.SetAttribute("size", tpkKey.PrimaryKey.KeyMaterial[0].bitCount().ToString());
            xmlPublicKey.SetAttribute("algorithm", tpkKey.PrimaryKey.Algorithm.ToString());

            XmlElement xmlUserIDs = xmlDoc.CreateElement("UserIDs");

            XmlElement xmlUserID;

            IEnumerator ieUserIDs = tpkKey.Certifications.GetEnumerator();

            while (ieUserIDs.MoveNext())
            {
                if (!(ieUserIDs.Current is CertifiedUserID))
                {
                    continue;
                }

                CertifiedUserID cuiUID = (CertifiedUserID)ieUserIDs.Current;
                cuiUID.Validate(tpkKey.PrimaryKey, pkrKeyRing);

                xmlUserID = xmlDoc.CreateElement("UserID");
                xmlUserID.SetAttribute("name", cuiUID.UserID.UserID);
                string strPrimary = "false";
                if (tpkKey.PrimaryUserID == cuiUID.UserID.UserID)
                {
                    strPrimary = "true";
                }

                xmlUserID.SetAttribute("primary", strPrimary);

                DateTime    dtTimeCreated = DateTime.Now;
                XmlElement  xmlSignature;
                IEnumerator ieSignatures = cuiUID.Certificates.GetEnumerator();
                while (ieSignatures.MoveNext())
                {
                    if (!(ieSignatures.Current is SignaturePacket))
                    {
                        continue;
                    }

                    SignaturePacket spSignature = (SignaturePacket)ieSignatures.Current;
                    xmlSignature = xmlDoc.CreateElement("Signature");
                    xmlSignature.SetAttribute("keyid", "0x" + spSignature.KeyID.ToString("x"));
                    xmlSignature.SetAttribute("created", spSignature.TimeCreated.Ticks.ToString());
                    string strExpiration = "";
                    try {
                        strExpiration = spSignature.FindExpirationTime().Ticks.ToString();
                    } catch (InvalidOperationException) {
                        strExpiration = "never";
                    }
                    xmlSignature.SetAttribute("expiration", strExpiration);
                    xmlSignature.SetAttribute("signaturestatus", spSignature.SignatureStatus.ToString());

                    string strCreator = "";
                    try {
                        TransportablePublicKey tpkSignatureKey = pkrKeyRing.Find(spSignature.KeyID, false);
                        strCreator = tpkSignatureKey.PrimaryUserID;
                    } catch (Exception) {
                        strCreator = "0x" + spSignature.KeyID.ToString("x");
                    }
                    xmlSignature.SetAttribute("creator", strCreator);
                    xmlSignature.SetAttribute("algorithm", spSignature.SignatureAlgorithm.ToString());
                    if (spSignature.KeyID == tpkKey.PrimaryKey.KeyID)
                    {
                        dtTimeCreated = spSignature.TimeCreated;
                    }

                    xmlUserID.AppendChild(xmlSignature);
                }
                xmlUserID.SetAttribute("created", dtTimeCreated.Ticks.ToString());

                xmlUserIDs.AppendChild(xmlUserID);
            }
            xmlPublicKey.AppendChild(xmlUserIDs);

            XmlElement xmlSubkeys = xmlDoc.CreateElement("Subkeys");

            XmlElement  xmlSubkey;
            IEnumerator ieSubkeys = tpkKey.SubKeys.GetEnumerator();

            while (ieSubkeys.MoveNext())
            {
                if (!(ieSubkeys.Current is CertifiedPublicSubkey))
                {
                    continue;
                }

                CertifiedPublicSubkey cpsSubkey = (CertifiedPublicSubkey)ieSubkeys.Current;

                xmlSubkey = xmlDoc.CreateElement("Subkey");
                xmlSubkey.SetAttribute("keyid", "0x" + cpsSubkey.Subkey.KeyID.ToString("x"));
                xmlSubkey.SetAttribute("fingerprint", cpsSubkey.Subkey.Fingerprint.ToString(16));
                xmlSubkey.SetAttribute("created", cpsSubkey.Subkey.TimeCreated.Ticks.ToString());

                string strExpiration = "";
                try {
                    strExpiration = cpsSubkey.KeyBindingSignature.FindExpirationTime().Ticks.ToString();
                } catch (InvalidOperationException) {
                    strExpiration = "never";
                }
                xmlSubkey.SetAttribute("expiration", strExpiration);
                xmlSubkey.SetAttribute("size", cpsSubkey.Subkey.KeyMaterial[0].bitCount().ToString());
                xmlSubkey.SetAttribute("algorithm", cpsSubkey.Subkey.Algorithm.ToString());

                xmlSubkeys.AppendChild(xmlSubkey);
            }

            xmlPublicKey.AppendChild(xmlSubkeys);
            xmlDoc.AppendChild(xmlPublicKey);
            return(xmlDoc.OuterXml);
        }