/// <summary> /// Finds a Key given a keyid. Performs a remote LDAP search if specified. /// </summary> /// <param name="lKeyID">Key to find</param> /// <param name="remote">LDAP search</param> /// <returns>a key</returns> public TransportablePublicKey Find(ulong lKeyID, bool remote) { IEnumerator ieKeys = alPublicKeys.GetEnumerator(); while (ieKeys.MoveNext()) { TransportablePublicKey tpkKey = (TransportablePublicKey)ieKeys.Current; if (tpkKey.PrimaryKey.KeyID == lKeyID) { return(tpkKey); } IEnumerator ieSubkeys = tpkKey.SubKeys.GetEnumerator(); while (ieSubkeys.MoveNext()) { CertifiedPublicSubkey cpsSubkey = (CertifiedPublicSubkey)ieSubkeys.Current; if (cpsSubkey.Subkey.KeyID == lKeyID) { return(tpkKey); } } } if (remote) { ldapKeyFinder.KeyFinder kf = new ldapKeyFinder.KeyFinder(); string key = kf.MyLDAPSearch(SharpPrivacyLibrary.LdapKeyServer, SharpPrivacyLibrary.LdapPort, "pgpkey", "(pgpsignerid=" + lKeyID.ToString("X") + ")"); if (key != null) { ArmorTypes atType = new ArmorTypes(); string strKey = Armor.RemoveArmor(key, ref atType, ref key); if (strKey.Length > 0) { TransportablePublicKey tpkKey = new TransportablePublicKey(strKey); AddPublicKey(tpkKey); return(tpkKey); } } } return(null); }
public TransportablePublicKey Find(ulong lKeyID) { IEnumerator ieKeys = alPublicKeys.GetEnumerator(); while (ieKeys.MoveNext()) { TransportablePublicKey tpkKey = (TransportablePublicKey)ieKeys.Current; if (tpkKey.PrimaryKey.KeyID == lKeyID) { return(tpkKey); } IEnumerator ieSubkeys = tpkKey.SubKeys.GetEnumerator(); while (ieSubkeys.MoveNext()) { CertifiedPublicSubkey cpsSubkey = (CertifiedPublicSubkey)ieSubkeys.Current; if (cpsSubkey.Subkey.KeyID == lKeyID) { return(tpkKey); } } } return(null); }
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") { } }
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); }