Ejemplo n.º 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);
        }
Ejemplo n.º 2
0
        public void AddUserID(ulong lKeyID, string strName, string strEmail, string strPassphrase)
        {
            TransportableSecretKey tskKey = skrKeyRing.Find(lKeyID);
            TransportablePublicKey tpkKey = pkrKeyRing.Find(lKeyID, false);

            CertifiedUserID cuiUID = new CertifiedUserID();
            UserIDPacket    uipUID = new UserIDPacket();

            uipUID.UserID = strName.Trim() + " <" + strEmail.Trim() + ">";
            cuiUID.UserID = uipUID;

            SecretKeyPacket skpSignatureKey = tskKey.FindKey(AsymActions.Sign);
            SignaturePacket spSelfSig       = new SignaturePacket();

            spSelfSig.Version       = SignaturePacketVersionNumbers.v4;
            spSelfSig.HashAlgorithm = HashAlgorithms.SHA1;
            spSelfSig.KeyID         = skpSignatureKey.PublicKey.KeyID;
            spSelfSig.TimeCreated   = DateTime.Now;
            cuiUID.Certificates     = new System.Collections.ArrayList();
            cuiUID.Sign(spSelfSig, skpSignatureKey, strPassphrase, tpkKey.PrimaryKey);

            tpkKey.Certifications.Add(cuiUID);
            tskKey.UserIDs.Add(uipUID);
        }
Ejemplo n.º 3
0
        private SymAlgorithms GetSymAlgorithmPreferences(TransportablePublicKey[] tpkKeys)
        {
            bool bCAST5  = true;
            bool bAES256 = true;
            bool bAES192 = true;
            bool bAES128 = true;

            for (int i = 0; i < tpkKeys.Length; i++)
            {
                TransportablePublicKey tpkKey = tpkKeys[i];
                ulong       lKeyID            = tpkKey.PrimaryKey.KeyID;
                IEnumerator ieCerts           = tpkKey.Certifications.GetEnumerator();
                while (ieCerts.MoveNext())
                {
                    if (!(ieCerts.Current is CertifiedUserID))
                    {
                        continue;
                    }

                    CertifiedUserID cuiID  = (CertifiedUserID)ieCerts.Current;
                    IEnumerator     ieSigs = cuiID.Certificates.GetEnumerator();
                    while (ieSigs.MoveNext())
                    {
                        if (!(ieSigs.Current is SignaturePacket))
                        {
                            continue;
                        }

                        SignaturePacket spSig = (SignaturePacket)ieSigs.Current;
                        if ((spSig.Version == SignaturePacketVersionNumbers.v4) && (spSig.KeyID == lKeyID))
                        {
                            try {
                                bool            bTmpCAST5  = false;
                                bool            bTmpAES256 = false;
                                bool            bTmpAES192 = false;
                                bool            bTmpAES128 = false;
                                SymAlgorithms[] saThisKey  = spSig.FindPreferedSymAlgorithms();
                                for (int j = 0; j < saThisKey.Length; j++)
                                {
                                    if (saThisKey[j] == SymAlgorithms.AES128)
                                    {
                                        bTmpAES128 = true;
                                    }
                                    else if (saThisKey[j] == SymAlgorithms.AES192)
                                    {
                                        bTmpAES192 = true;
                                    }
                                    else if (saThisKey[j] == SymAlgorithms.AES256)
                                    {
                                        bTmpAES256 = true;
                                    }
                                    else if (saThisKey[j] == SymAlgorithms.CAST5)
                                    {
                                        bTmpCAST5 = true;
                                    }
                                }

                                if (!bTmpCAST5)
                                {
                                    bCAST5 = false;
                                }

                                if (!bTmpAES256)
                                {
                                    bAES256 = false;
                                }

                                if (!bTmpAES192)
                                {
                                    bAES192 = false;
                                }

                                if (!bTmpAES128)
                                {
                                    bAES128 = false;
                                }
                            } catch (InvalidOperationException) {}
                        }
                    }
                }
            }
            if (bAES256)
            {
                return(SymAlgorithms.AES256);
            }

            if (bAES192)
            {
                return(SymAlgorithms.AES192);
            }

            if (bAES128)
            {
                return(SymAlgorithms.AES128);
            }

            if (bCAST5)
            {
                return(SymAlgorithms.CAST5);
            }

            return(SymAlgorithms.Triple_DES);
        }
Ejemplo n.º 4
0
        public void GenerateKey(string strName, string strEmail, string strKeyType, int iKeySize, long lExpiration, string strPassphrase)
        {
            if (strKeyType == "ElGamal/DSA")
            {
                System.Security.Cryptography.RandomNumberGenerator rngRand = System.Security.Cryptography.RandomNumberGenerator.Create();

                // let's first create the encryption key
                BigInteger[][] biEncryptionKey = GenerateEncryptionKey(iKeySize);

                // now the signature key
                BigInteger[][] biSignatureKey = GenerateSignatureKey();

                PublicKeyPacket pkpSignatureKey = new PublicKeyPacket(false);
                pkpSignatureKey.Algorithm   = AsymAlgorithms.DSA;
                pkpSignatureKey.KeyMaterial = biSignatureKey[0];
                pkpSignatureKey.TimeCreated = DateTime.Now;
                pkpSignatureKey.Version     = PublicKeyPacketVersionNumbers.v4;

                SecretKeyPacket skpSignatureKey = new SecretKeyPacket(false);
                skpSignatureKey.SymmetricalAlgorithm = SymAlgorithms.AES256;
                skpSignatureKey.PublicKey            = pkpSignatureKey;
                skpSignatureKey.InitialVector        = new byte[CipherHelper.CipherBlockSize(SymAlgorithms.AES256)];
                rngRand.GetBytes(skpSignatureKey.InitialVector);
                skpSignatureKey.EncryptKeyMaterial(biSignatureKey[1], strPassphrase);
                skpSignatureKey.PublicKey = pkpSignatureKey;

                PublicKeyPacket pkpEncryptionKey = new PublicKeyPacket(true);
                pkpEncryptionKey.Algorithm   = AsymAlgorithms.ElGamal_Encrypt_Only;
                pkpEncryptionKey.KeyMaterial = biEncryptionKey[0];
                pkpEncryptionKey.TimeCreated = DateTime.Now;
                pkpEncryptionKey.Version     = PublicKeyPacketVersionNumbers.v4;

                SecretKeyPacket skpEncryptionKey = new SecretKeyPacket(true);
                skpEncryptionKey.SymmetricalAlgorithm = SymAlgorithms.AES256;
                skpEncryptionKey.PublicKey            = pkpEncryptionKey;
                skpEncryptionKey.InitialVector        = new byte[CipherHelper.CipherBlockSize(SymAlgorithms.AES256)];
                rngRand.GetBytes(skpEncryptionKey.InitialVector);
                skpEncryptionKey.EncryptKeyMaterial(biEncryptionKey[1], strPassphrase);
                skpEncryptionKey.PublicKey = pkpEncryptionKey;

                CertifiedUserID cuiUID = new CertifiedUserID();
                UserIDPacket    uipUID = new UserIDPacket();
                uipUID.UserID = strName.Trim() + " <" + strEmail.Trim() + ">";
                cuiUID.UserID = uipUID;
                SignaturePacket spSelfSig = new SignaturePacket();
                spSelfSig.Version       = SignaturePacketVersionNumbers.v4;
                spSelfSig.HashAlgorithm = HashAlgorithms.SHA1;
                spSelfSig.KeyID         = pkpSignatureKey.KeyID;
                spSelfSig.TimeCreated   = DateTime.Now;
                SignatureSubPacket sspPrimaryUserID = new SignatureSubPacket();
                sspPrimaryUserID.Type          = SignatureSubPacketTypes.PrimaryUserID;
                sspPrimaryUserID.PrimaryUserID = true;
                spSelfSig.AddSubPacket(sspPrimaryUserID, true);
                SignatureSubPacket sspPreferedSymAlgos = new SignatureSubPacket();
                sspPreferedSymAlgos.Type             = SignatureSubPacketTypes.PreferedSymmetricAlgorithms;
                sspPreferedSymAlgos.PreferedSymAlgos = new SymAlgorithms[] { SymAlgorithms.AES256, SymAlgorithms.AES192, SymAlgorithms.AES256, SymAlgorithms.CAST5, SymAlgorithms.Triple_DES };
                spSelfSig.AddSubPacket(sspPreferedSymAlgos, true);
                SignatureSubPacket sspPreferedHashAlgos = new SignatureSubPacket();
                sspPreferedHashAlgos.Type = SignatureSubPacketTypes.PreferedHashAlgorithms;
                sspPreferedHashAlgos.PreferedHashAlgos = new HashAlgorithms[] { HashAlgorithms.SHA1 };
                spSelfSig.AddSubPacket(sspPreferedHashAlgos, true);
                if (lExpiration != 0)
                {
                    SignatureSubPacket sspExpiration = new SignatureSubPacket();
                    sspExpiration.Type = SignatureSubPacketTypes.SignatureExpirationTime;
                    sspExpiration.SignatureExpirationTime = new DateTime(lExpiration);
                    spSelfSig.AddSubPacket(sspExpiration, true);
                }
                cuiUID.Certificates = new System.Collections.ArrayList();
                cuiUID.Sign(spSelfSig, skpSignatureKey, strPassphrase, pkpSignatureKey);

                CertifiedPublicSubkey cpsEncryptionKey = new CertifiedPublicSubkey();
                cpsEncryptionKey.Subkey = pkpEncryptionKey;
                cpsEncryptionKey.SignKeyBindingSignature(pkpSignatureKey, skpSignatureKey, strPassphrase, new DateTime(lExpiration), true);

                TransportablePublicKey tpkPublicKey = new TransportablePublicKey();
                tpkPublicKey.PrimaryKey = pkpSignatureKey;
                tpkPublicKey.SubKeys.Add(cpsEncryptionKey);
                tpkPublicKey.Certifications.Add(cuiUID);

                TransportableSecretKey tskSecretKey = new TransportableSecretKey();
                tskSecretKey.PrimaryKey = skpSignatureKey;
                tskSecretKey.SubKeys.Add(skpEncryptionKey);
                tskSecretKey.UserIDs.Add(uipUID);

                this.pkrKeyRing.AddPublicKey(tpkPublicKey);
                this.skrKeyRing.AddSecretKey(tskSecretKey);
                pkrKeyRing.Save();
                skrKeyRing.Save();

                // it's an RSA key
            }
            else if (strKeyType == "RSA")
            {
            }
        }
Ejemplo n.º 5
0
        public void SignKey(ulong lSignedKeyID, ulong lSigningKeyID, string strUserID, int nIntroducerDepth, bool bIsExportable, int nType, string strPassphrase)
        {
            TransportableSecretKey tskKey          = skrKeyRing.Find(lSigningKeyID);
            SecretKeyPacket        skpSignatureKey = tskKey.FindKey(AsymActions.Sign);

            TransportablePublicKey tpkKey = pkrKeyRing.Find(lSignedKeyID, false);

            SignaturePacket spCertificate = new SignaturePacket();

            spCertificate.SignatureType = (SignatureTypes)nType;
            spCertificate.Version       = SignaturePacketVersionNumbers.v4;
            spCertificate.HashAlgorithm = HashAlgorithms.SHA1;
            spCertificate.KeyID         = skpSignatureKey.PublicKey.KeyID;
            spCertificate.TimeCreated   = DateTime.Now;

            CertifiedUserID cuiID     = null;
            IEnumerator     ieUserIDs = tpkKey.Certifications.GetEnumerator();

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

                CertifiedUserID cuiThisID = (CertifiedUserID)ieUserIDs.Current;
                if (cuiThisID.ToString() == strUserID)
                {
                    cuiID = cuiThisID;
                }
            }
            if (cuiID == null)
            {
                throw new Exception("UserID could not be found!");
            }

            if (bIsExportable == false)
            {
                SignatureSubPacket sspNotExportable = new SignatureSubPacket();
                sspNotExportable.Type = SignatureSubPacketTypes.ExportableSignature;
                sspNotExportable.ExportableSignature = false;
                spCertificate.AddSubPacket(sspNotExportable, true);
            }

            if (nIntroducerDepth > 0)
            {
                SignatureSubPacket sspTrust = new SignatureSubPacket();
                sspTrust.Type        = SignatureSubPacketTypes.TrustSignature;
                sspTrust.TrustLevel  = (byte)nIntroducerDepth;
                sspTrust.TrustAmount = 120;
                spCertificate.AddSubPacket(sspTrust, true);
            }

            cuiID.Sign(spCertificate, skpSignatureKey, strPassphrase, tpkKey.PrimaryKey);
            tpkKey.Certifications.Remove(cuiID);
            tpkKey.Certifications.Add(cuiID);

            pkrKeyRing.Delete(lSignedKeyID);
            pkrKeyRing.AddPublicKey(tpkKey);
            pkrKeyRing.Save();
        }
Ejemplo n.º 6
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);
        }