private PgpSecretKey GetLastSecretKey(PgpSecretKeyRingBundle secretKeyRingBundle) { return((from PgpSecretKeyRing kRing in secretKeyRingBundle.GetKeyRings() select kRing.GetSecretKeys().Cast <PgpSecretKey>() .LastOrDefault(k => k.IsSigningKey)) .LastOrDefault(key => key != null)); }
/// <summary> /// Return the first key we can use to encrypt. /// Note: A file can contain multiple keys (stored in "key rings") /// </summary> private PgpSecretKey getFirstSecretKey(PgpSecretKeyRingBundle secretKeyRingBundle) { foreach (PgpSecretKeyRing kRing in secretKeyRingBundle.GetKeyRings()) { // Note: You may need to use something other than the first key // in your key ring. Keep that in mind. // ex: .Where(k => !k.IsSigningKey) /* * PgpSecretKey key = kRing.GetSecretKeys() * .Cast<PgpSecretKey>() * .Where(k => k.IsSigningKey) * .FirstOrDefault(); * * if (key != null) * { * return key; * } */ IEnumerator ikeys = kRing.GetSecretKeys().GetEnumerator(); if (ikeys.MoveNext()) { PgpSecretKey key = (PgpSecretKey)ikeys.Current; if (key.IsSigningKey) { return(key); } } } return(null); }
private PgpSecretKey ReadSecretKey( Stream inputStream) { PgpSecretKeyRingBundle pgpSec = new PgpSecretKeyRingBundle(inputStream); // // we just loop through the collection till we find a key suitable for encryption, in the real // world you would probably want to be a bit smarter about this. // // // iterate through the key rings. // foreach (PgpSecretKeyRing kRing in pgpSec.GetKeyRings()) { foreach (PgpSecretKey k in kRing.GetSecretKeys()) { if (k.IsSigningKey) { return(k); } } } throw new ArgumentException("Can't find signing key in key ring."); }
private static PgpSecretKey ReadSigningSecretKey(System.IO.Stream keyInStream) { PgpSecretKeyRingBundle pgpSec = CreatePgpSecretKeyRingBundle(keyInStream); PgpSecretKey key = null; System.Collections.IEnumerator rIt = pgpSec.GetKeyRings().GetEnumerator(); while (key == null && rIt.MoveNext()) { PgpSecretKeyRing kRing = (PgpSecretKeyRing)rIt.Current; System.Collections.IEnumerator kIt = kRing.GetSecretKeys().GetEnumerator(); while (key == null && kIt.MoveNext()) { PgpSecretKey k = (PgpSecretKey)kIt.Current; if (k.IsSigningKey) { key = k; } } } if (key == null) { throw new System.Exception("Wrong private key - Can't find signing key in key ring."); } else { return(key); } }
private PgpSecretKey readSecretKey(Stream privateKeyStream) { PgpSecretKeyRingBundle pgpSec; try { pgpSec = new PgpSecretKeyRingBundle(PgpUtilities.GetDecoderStream(privateKeyStream)); } catch (Exception e) { throw new Exception("Invalid private key stream, reason: " + e.Message); } var keyRings = pgpSec.GetKeyRings(); foreach (PgpSecretKeyRing keyRing in keyRings) { foreach (PgpSecretKey key in keyRing.GetSecretKeys()) { if (key.UserIds.Cast <String>().Where(id => id == _userId).Count() > 0) { try { key.ExtractPrivateKey(_passPhrase.ToCharArray()); return(key); } catch { continue; } } } } throw new Exception("Could not find a valid signing key"); }
public static void Decrypt(Stream inputStream, Stream privateKeyStream, string passPhrase, string outputFilePath) { var objectFactory = new PgpObjectFactory(PgpUtilities.GetDecoderStream(inputStream)); var secretKeyRing = new PgpSecretKeyRingBundle(PgpUtilities.GetDecoderStream(privateKeyStream)); PgpEncryptedDataList encryptedDataList = (objectFactory.NextPgpObject() as PgpEncryptedDataList) ?? (PgpEncryptedDataList)objectFactory.NextPgpObject(); PgpObjectFactory plainFact = RetrievePgpObjectFactory(encryptedDataList, secretKeyRing, passPhrase); PgpObject message = plainFact.NextPgpObject(); if (message is PgpOnePassSignatureList) { throw new PgpException("Encrypted message contains a signed message - not literal data."); } if (message is PgpCompressedData) { DecryptCompressedData(message, outputFilePath); } else if (message is PgpLiteralData) { PipeStreamToFile(outputFilePath, (PgpLiteralData)message); } else { throw new PgpException("Message is not a simple encrypted file - type unknown."); } }
/// <summary> /// Reads secret key from given privateKey /// </summary> /// <param name="privateKeyFile">Path to private key file</param> /// <returns>PgpSecretKey of the given privateKey</returns> internal static PgpSecretKey ReadSecretKey(string privateKeyFile) { PgpSecretKey secretKey = null; using (Stream secretKeyStream = File.OpenRead(privateKeyFile)) { var secretKeyRingBundle = new PgpSecretKeyRingBundle(PgpUtilities.GetDecoderStream(secretKeyStream)); foreach (PgpSecretKeyRing keyRing in secretKeyRingBundle.GetKeyRings()) { foreach (PgpSecretKey key in keyRing.GetSecretKeys()) { if (key.IsSigningKey) { secretKey = key; } } } if (secretKey == null) { throw new Exception("Wrong private key - Can't find signing key in key ring."); } } return(secretKey); }
/// <summary> /// Decrypts a PGP encrypted file. /// </summary> /// <param name="inputFilePath">The encrypted input file path.</param> /// <param name="privateKeyRingBundle">The private key ring bundle.</param> /// <param name="passPhrase">The pass phrase.</param> /// <param name="outputDirectory">The output directory. Defaults to the directory of the input file.</param> /// <param name="outputFileName">The output file name. Defaults to the file name embedded in the PGP file.</param> /// <returns>The path to the decrypted file.</returns> public static string DecryptFile( string inputFilePath, PgpSecretKeyRingBundle privateKeyRingBundle, string passPhrase, string outputDirectory = null, string outputFileName = null) { if (outputDirectory == null) { outputDirectory = Path.GetDirectoryName(inputFilePath); } string outputPath; using (Stream inputStream = File.OpenRead(inputFilePath)) using (PgpFileDecryptingStream pgpStream = new PgpFileDecryptingStream(inputStream, privateKeyRingBundle, passPhrase)) { outputPath = Path.Combine(outputDirectory, outputFileName ?? pgpStream.WrappedFileName); using (Stream outputStream = File.Create(outputPath)) { pgpStream.CopyTo(outputStream); } FileInfo fileInfo = new FileInfo(outputPath); fileInfo.LastWriteTime = pgpStream.WrappedFileModifiedTime; fileInfo.CreationTime = pgpStream.WrappedFileModifiedTime; } return(outputPath); }
public String DecryptKeycode(String privateKeyStr, String encryptedKeycode) { byte[] rawMessage = System.Text.Encoding.ASCII.GetBytes(encryptedKeycode); Stream inputStream = new MemoryStream(rawMessage); inputStream = PgpUtilities.GetDecoderStream(inputStream); PgpObjectFactory pgpF = new PgpObjectFactory(inputStream); PgpEncryptedDataList enc = null; PgpObject o = pgpF.NextPgpObject(); // // the first object might be a PGP marker packet. // if (o is PgpEncryptedDataList) { enc = (PgpEncryptedDataList)o; } else { enc = (PgpEncryptedDataList)pgpF.NextPgpObject(); } byte[] decodedPrivateKey = System.Text.Encoding.ASCII.GetBytes(privateKeyStr); PgpPrivateKey key = null; Stream decodedStream = PgpUtilities.GetDecoderStream(new MemoryStream(decodedPrivateKey)); PgpSecretKeyRingBundle privRings = new PgpSecretKeyRingBundle(decodedStream); PgpPublicKeyEncryptedData dataObject = null; foreach (PgpPublicKeyEncryptedData encryptedData in enc.GetEncryptedDataObjects()) { key = FindKeyById(privRings, encryptedData.KeyId); dataObject = encryptedData; } if (key == null) { throw new SendSafely.Exceptions.InvalidKeyException("Can't find encryption key in key ring."); } Stream dataStream = dataObject.GetDataStream(key); PgpObjectFactory pgpFact = new PgpObjectFactory(dataStream); PgpLiteralData ld = (PgpLiteralData)pgpFact.NextPgpObject(); Stream unc = ld.GetInputStream(); String keycode; using (StreamReader reader = new StreamReader(unc, Encoding.UTF8)) { keycode = reader.ReadToEnd(); } return(keycode); }
private PgpSecretKey GetsecretKeyByUserId(PgpSecretKeyRingBundle secretKeyRingBundle, String secretKeyUserId) { foreach (PgpSecretKeyRing kRing in secretKeyRingBundle.GetKeyRings()) { PgpSecretKey key = kRing.GetSecretKeys() .Cast <PgpSecretKey>() .Where(k => k.IsSigningKey) .FirstOrDefault(); if (key != null) { foreach (String userId in key.UserIds) { if (userId.Contains(secretKeyUserId)) { return(key); } } } } return(null); }
private void DoTestKey(BigInteger keyId, string passphrase, bool utf8) { PgpSecretKeyRingBundle secretKeyRing = LoadSecretKeyCollection("secring.gpg"); PgpSecretKeyRing secretKey = secretKeyRing.GetSecretKeyRing(keyId.LongValue); Assert.NotNull(secretKey, "Could not locate secret keyring with Id=" + keyId.ToString(16)); PgpSecretKey key = secretKey.GetSecretKey(); Assert.NotNull(key, "Could not locate secret key!"); try { char[] pass = passphrase.ToCharArray(); PgpPrivateKey privateKey = utf8 ? key.ExtractPrivateKeyUtf8(pass) : key.ExtractPrivateKey(pass); Assert.IsTrue(privateKey.KeyId == keyId.LongValue); } catch (PgpException e) { throw new PgpException("Password incorrect!", e); } // all fine! }
public static PgpSecretKey ImportSecretKey(this Stream secretIn) { var secRings = new PgpSecretKeyRingBundle(PgpUtilities.GetDecoderStream(secretIn)).GetKeyRings().OfType <PgpSecretKeyRing>(); var secKeys = secRings.SelectMany(x => x.GetSecretKeys().OfType <PgpSecretKey>()); var secKey = secKeys.FirstOrDefault(); return(secKey); }
public string DecryptKeycode(string privateKeyStr, string encryptedKeycode) { var rawMessage = Encoding.ASCII.GetBytes(encryptedKeycode); Stream inputStream = new MemoryStream(rawMessage); inputStream = PgpUtilities.GetDecoderStream(inputStream); var pgpF = new PgpObjectFactory(inputStream); PgpEncryptedDataList enc; var o = pgpF.NextPgpObject(); // // the first object might be a PGP marker packet. // if (o is PgpEncryptedDataList list) { enc = list; } else { enc = (PgpEncryptedDataList)pgpF.NextPgpObject(); } var decodedPrivateKey = Encoding.ASCII.GetBytes(privateKeyStr); PgpPrivateKey key = null; var decodedStream = PgpUtilities.GetDecoderStream(new MemoryStream(decodedPrivateKey)); var privRings = new PgpSecretKeyRingBundle(decodedStream); PgpPublicKeyEncryptedData dataObject = null; foreach (PgpPublicKeyEncryptedData encryptedData in enc.GetEncryptedDataObjects()) { key = FindKeyById(privRings, encryptedData.KeyId); dataObject = encryptedData; } if (key == null) { throw new InvalidKeyException("Can't find encryption key in key ring."); } var dataStream = dataObject.GetDataStream(key); var pgpFact = new PgpObjectFactory(dataStream); var ld = (PgpLiteralData)pgpFact.NextPgpObject(); var unc = ld.GetInputStream(); using var reader = new StreamReader(unc, Encoding.UTF8); var keycode = reader.ReadToEnd(); return(keycode); }
/// <summary> /// Delete a secret pgp keyring. /// </summary> /// <remarks> /// Deletes a secret pgp keyring. /// </remarks> /// <param name="keyring">The pgp keyring.</param> /// <exception cref="System.ArgumentNullException"> /// <paramref name="keyring"/> is <c>null</c>. /// </exception> public virtual void Delete(PgpSecretKeyRing keyring) { if (keyring == null) { throw new ArgumentNullException(nameof(keyring)); } SecretKeyRingBundle = PgpSecretKeyRingBundle.RemoveSecretKeyRing(SecretKeyRingBundle, keyring); SaveSecretKeyRingBundle(); }
private static PgpPrivateKey FindSecretKey(PgpSecretKeyRingBundle pgpSec, long keyId, char[] passPhrase) { PgpSecretKey pgpSecKey = pgpSec.GetSecretKey(keyId); if (pgpSecKey == null) { return(null); } return(pgpSecKey.ExtractPrivateKey(passPhrase)); }
private PgpSecretKey GetLastSecretKey(PgpSecretKeyRingBundle secret_key_ring_bundle) { IEnumerable pgpKeyRings = secret_key_ring_bundle.GetKeyRings(); return((from PgpSecretKeyRing kring in secret_key_ring_bundle.GetKeyRings() select kring.GetSecretKeys().Cast <PgpSecretKey>(). LastOrDefault(k => k.IsSigningKey)).LastOrDefault(key => key != null)); }
/// <summary> /// Search a secret key ring collection for a secret key corresponding to keyID if it exists. /// </summary> /// <param name="secretKeyRingBundle"></param> /// <param name="keyID"></param> /// <param name="passPhrase"></param> /// <returns></returns> /// <exception cref="PgpException"></exception> internal static PgpPrivateKey FindSecretKey(PgpSecretKeyRingBundle secretKeyRingBundle, long keyId, char[] passPhrase) { PgpSecretKey _pgpSecretKey = secretKeyRingBundle.GetSecretKey(keyId); if (_pgpSecretKey == null) { return(null); } return(_pgpSecretKey.ExtractPrivateKey(passPhrase)); }
private static PgpPrivateKey ExtractSecretKey(PgpSecretKeyRingBundle pgpSec, long keyId, char[] pass) { var pgpSecKey = pgpSec.GetSecretKey(keyId); if (pgpSecKey == null) { return(null); } return(pgpSecKey.ExtractPrivateKey(pass)); }
public static PgpPrivateKey FindSecretKeybyKeyId(PgpSecretKeyRingBundle pgpSec, string keyID, char[] pass) { var pgpSecKey = pgpSec.GetSecretKey(System.Convert.ToInt64(keyID, 16)); if (pgpSecKey == null) { return(null); } return(pgpSecKey.ExtractPrivateKey(pass)); }
/** * Search a secret key ring collection for a secret key corresponding to keyID if it * exists. * * @param pgpSec a secret key ring collection. * @param keyID keyID we want. * @param pass passphrase to decrypt secret key with. * @return * @throws PGPException * @throws NoSuchProviderException */ internal static PgpPrivateKey FindSecretKey(PgpSecretKeyRingBundle pgpSec, long keyID, char[] pass) { PgpSecretKey pgpSecKey = pgpSec.GetSecretKey(keyID); if (pgpSecKey == null) { return null; } return pgpSecKey.ExtractPrivateKey(pass); }
/** * Search a secret key ring collection for a secret key corresponding to keyID if it * exists. * * @param pgpSec a secret key ring collection. * @param keyID keyID we want. * @param pass passphrase to decrypt secret key with. * @return * @throws PGPException * @throws NoSuchProviderException */ public static PgpPrivateKey FindSecretKeybyKeyId(PgpSecretKeyRingBundle pgpSec, long keyID, char[] pass) { var pgpSecKey = pgpSec.GetSecretKey(keyID); if (pgpSecKey == null) { return(null); } return(pgpSecKey.ExtractPrivateKey(pass)); }
/** * Search a secret key ring collection for a secret key corresponding to keyID if it * exists. * * @param pgpSec a secret key ring collection. * @param keyID keyID we want. * @param pass passphrase to decrypt secret key with. * @return * @throws PGPException * @throws NoSuchProviderException */ internal static PgpPrivateKey FindSecretKey(PgpSecretKeyRingBundle pgpSec, long keyID, char[] pass) { PgpSecretKey pgpSecKey = pgpSec.GetSecretKey(keyID); if (pgpSecKey == null) { return(null); } return(pgpSecKey.ExtractPrivateKey(pass)); }
private PgpPrivateKey FindKeyById(PgpSecretKeyRingBundle privRings, long keyId) { PgpSecretKey pgpSecKey = privRings.GetSecretKey(keyId); if (pgpSecKey == null) { return(null); } return(pgpSecKey.ExtractPrivateKey(null)); }
public PgpSecretKey LoadSecretKey(Stream key) { var armoredStream = PgpUtilities.GetDecoderStream(key); var pgpSecretKeyBundle = new PgpSecretKeyRingBundle(armoredStream); foreach (PgpSecretKeyRing keyRing in pgpSecretKeyBundle.GetKeyRings()) { return(keyRing.GetSecretKey()); } throw new ArgumentException("Can't find secret key in key ring."); }
/// <summary> /// Return the first key we can use to encrypt. /// Note: A file can contain multiple keys (stored in "key rings") /// </summary> private PgpSecretKey GetFirstSecretKey(PgpSecretKeyRingBundle secretKeyRingBundle) { foreach (PgpSecretKeyRing kRing in secretKeyRingBundle.GetKeyRings()) { PgpSecretKey key = kRing.GetSecretKeys().Cast <PgpSecretKey>().Where(k => k.IsSigningKey).FirstOrDefault(); if (key != null) { return(key); } } return(null); }
public PgpSecretKey GetSecretKeyForSigning(string email) { using (var inputStream = File.OpenRead(Context.PrivateKeyRingFile)) { using (var decoderStream = PgpUtilities.GetDecoderStream(inputStream)) { var pgpPub = new PgpSecretKeyRingBundle(decoderStream); var emailSearch = "<" + email.ToLower().Trim() + ">"; foreach (PgpSecretKeyRing kRing in pgpPub.GetKeyRings()) { foreach (PgpSecretKey k in kRing.GetSecretKeys()) { if (!IsSigningKey(k.PublicKey)) { continue; } if (!k.IsMasterKey) { foreach (PgpSignature sig in k.PublicKey.GetSignaturesOfType(24)) { var pubKey = this.GetPublicKey(sig.KeyId); if (!pubKey.IsMasterKey) { continue; } foreach (string id in pubKey.GetUserIds()) { if (id.ToLower().IndexOf(emailSearch) > -1) { return(k); } } } } foreach (string id in k.PublicKey.GetUserIds()) { if (id.ToLower().IndexOf(emailSearch) > -1) { return(k); } } } } return(null); } } }
private PgpSecretKey readSecretKey(PgpSecretKeyRingBundle bundle) { var keyRings = bundle.GetKeyRings(); foreach (var keyRing in keyRings) { if (keyRing is PgpSecretKeyRing) { return(((PgpSecretKeyRing)keyRing).GetSecretKey()); } } throw new ArgumentException("Secret Key for message not found"); }
private PgpSecretKey ReadSecretKey(Stream privateKeyStream) { using (Stream inputStream = PgpUtilities.GetDecoderStream(privateKeyStream)) { SecretKeys = new PgpSecretKeyRingBundle(inputStream); PgpSecretKey foundKey = GetFirstSecretKey(SecretKeys); if (foundKey != null) { return(foundKey); } } throw new ArgumentException("Can't find signing key in key ring."); }
public void FindSecretKeyTest() { PgpSecretKeyRingBundle pgpSec = null; // TODO: Initialize to an appropriate value long keyId = 0; // TODO: Initialize to an appropriate value char[] pass = null; // TODO: Initialize to an appropriate value PgpPrivateKey expected = null; // TODO: Initialize to an appropriate value PgpPrivateKey actual; actual = Publisher_Accessor.FindSecretKey(pgpSec, keyId, pass); Assert.AreEqual(expected, actual); Assert.Inconclusive("Verify the correctness of this test method."); }
private static PgpPrivateKey FindSecretKey(Stream keyIn, long keyId, char[] pass) { PgpSecretKeyRingBundle pgpSec = new PgpSecretKeyRingBundle( PgpUtilities.GetDecoderStream(keyIn)); PgpSecretKey pgpSecKey = pgpSec.GetSecretKey(keyId); if (pgpSecKey == null) { return(null); } return(pgpSecKey.ExtractPrivateKey(pass)); }
private PgpSecretKey ReadSecretKey(string privateKeyPath) { using (Stream keyIn = File.OpenRead(privateKeyPath)) using (Stream inputStream = PgpUtilities.GetDecoderStream(keyIn)) { PgpSecretKeyRingBundle secretKeyRingBundle = new PgpSecretKeyRingBundle(inputStream); PgpSecretKey foundKey = GetFirstSecretKey(secretKeyRingBundle); if (foundKey != null) { return(foundKey); } } throw new ArgumentException("Can't find signing key in key ring."); }
public void SecretKeyRingWithPersonalCertificateTest() { checkSecretKeyRingWithPersonalCertificate(secWithPersonalCertificate); PgpSecretKeyRingBundle secRing = new PgpSecretKeyRingBundle(secWithPersonalCertificate); checkSecretKeyRingWithPersonalCertificate(secRing.GetEncoded()); }
public void RewrapTestV3() { // Read the secret key rings PgpSecretKeyRingBundle privRings = new PgpSecretKeyRingBundle( new MemoryStream(privv3, false)); char[] newPass = "******".ToCharArray(); foreach (PgpSecretKeyRing pgpPrivEnum in privRings.GetKeyRings()) { PgpSecretKeyRing pgpPriv = pgpPrivEnum; foreach (PgpSecretKey pgpKeyEnum in pgpPriv.GetSecretKeys()) { long oldKeyID = pgpKeyEnum.KeyId; // re-encrypt the key with an empty password pgpPriv = PgpSecretKeyRing.RemoveSecretKey(pgpPriv, pgpKeyEnum); PgpSecretKey pgpKey = PgpSecretKey.CopyWithNewPassword( pgpKeyEnum, v3KeyPass, null, SymmetricKeyAlgorithmTag.Null, Random); pgpPriv = PgpSecretKeyRing.InsertSecretKey(pgpPriv, pgpKey); // this should succeed PgpPrivateKey privTmp = pgpKey.ExtractPrivateKey(null); if (pgpKey.KeyId != oldKeyID) { Fail("key ID mismatch"); } } foreach (PgpSecretKey pgpKeyEnum in pgpPriv.GetSecretKeys()) { long oldKeyID = pgpKeyEnum.KeyId; // re-encrypt the key with an empty password pgpPriv = PgpSecretKeyRing.RemoveSecretKey(pgpPriv, pgpKeyEnum); PgpSecretKey pgpKey = PgpSecretKey.CopyWithNewPassword( pgpKeyEnum, null, newPass, SymmetricKeyAlgorithmTag.Cast5, Random); pgpPriv = PgpSecretKeyRing.InsertSecretKey(pgpPriv, pgpKey); // this should succeed PgpPrivateKey privTmp = pgpKey.ExtractPrivateKey(newPass); if (pgpKey.KeyId != oldKeyID) { Fail("key ID mismatch"); } } } }
public void PerformTest1() { PgpPublicKeyRingBundle pubRings = new PgpPublicKeyRingBundle(pub1); int count = 0; foreach (PgpPublicKeyRing pgpPub1 in pubRings.GetKeyRings()) { count++; int keyCount = 0; byte[] bytes = pgpPub1.GetEncoded(); PgpPublicKeyRing pgpPub2 = new PgpPublicKeyRing(bytes); foreach (PgpPublicKey pubKey in pgpPub2.GetPublicKeys()) { keyCount++; foreach (PgpSignature sig in pubKey.GetSignatures()) { if (sig == null) Fail("null signature found"); } } if (keyCount != 2) { Fail("wrong number of public keys"); } } if (count != 1) { Fail("wrong number of public keyrings"); } // // exact match // count = 0; foreach (PgpPublicKeyRing pgpPub3 in pubRings.GetKeyRings("test (Test key) <*****@*****.**>")) { if (pgpPub3 == null) Fail("null keyring found"); count++; } if (count != 1) { Fail("wrong number of public keyrings on exact match"); } // // partial match 1 expected // count = 0; foreach (PgpPublicKeyRing pgpPub4 in pubRings.GetKeyRings("test", true)) { if (pgpPub4 == null) Fail("null keyring found"); count++; } if (count != 1) { Fail("wrong number of public keyrings on partial match 1"); } // // partial match 0 expected // count = 0; foreach (PgpPublicKeyRing pgpPub5 in pubRings.GetKeyRings("XXX", true)) { if (pgpPub5 == null) Fail("null keyring found"); count++; } if (count != 0) { Fail("wrong number of public keyrings on partial match 0"); } // // case-insensitive partial match // count = 0; foreach (PgpPublicKeyRing pgpPub6 in pubRings.GetKeyRings("*****@*****.**", true, true)) { if (pgpPub6 == null) Fail("null keyring found"); count++; } if (count != 1) { Fail("wrong number of public keyrings on case-insensitive partial match"); } PgpSecretKeyRingBundle secretRings = new PgpSecretKeyRingBundle(sec1); count = 0; foreach (PgpSecretKeyRing pgpSec1 in secretRings.GetKeyRings()) { count++; int keyCount = 0; byte[] bytes = pgpSec1.GetEncoded(); PgpSecretKeyRing pgpSec2 = new PgpSecretKeyRing(bytes); foreach (PgpSecretKey k in pgpSec2.GetSecretKeys()) { keyCount++; PgpPublicKey pk = k.PublicKey; pk.GetSignatures(); byte[] pkBytes = pk.GetEncoded(); PgpPublicKeyRing pkR = new PgpPublicKeyRing(pkBytes); } if (keyCount != 2) { Fail("wrong number of secret keys"); } } if (count != 1) { Fail("wrong number of secret keyrings"); } // // exact match // count = 0; foreach (PgpSecretKeyRing o1 in secretRings.GetKeyRings("test (Test key) <*****@*****.**>")) { if (o1 == null) Fail("null keyring found"); count++; } if (count != 1) { Fail("wrong number of secret keyrings on exact match"); } // // partial match 1 expected // count = 0; foreach (PgpSecretKeyRing o2 in secretRings.GetKeyRings("test", true)) { if (o2 == null) Fail("null keyring found"); count++; } if (count != 1) { Fail("wrong number of secret keyrings on partial match 1"); } // // exact match 0 expected // count = 0; foreach (PgpSecretKeyRing o3 in secretRings.GetKeyRings("test", false)) { if (o3 == null) Fail("null keyring found"); count++; } if (count != 0) { Fail("wrong number of secret keyrings on partial match 0"); } // // case-insensitive partial match // count = 0; foreach (PgpSecretKeyRing o4 in secretRings.GetKeyRings("*****@*****.**", true, true)) { if (o4 == null) Fail("null keyring found"); count++; } if (count != 1) { Fail("wrong number of secret keyrings on case-insensitive partial match"); } }
public void PerformTest2() { PgpPublicKeyRingBundle pubRings = new PgpPublicKeyRingBundle(pub2); int count = 0; byte[] encRing = pubRings.GetEncoded(); pubRings = new PgpPublicKeyRingBundle(encRing); foreach (PgpPublicKeyRing pgpPub1 in pubRings.GetKeyRings()) { count++; int keyCount = 0; byte[] bytes = pgpPub1.GetEncoded(); PgpPublicKeyRing pgpPub2 = new PgpPublicKeyRing(bytes); foreach (PgpPublicKey pk in pgpPub2.GetPublicKeys()) { byte[] pkBytes = pk.GetEncoded(); PgpPublicKeyRing pkR = new PgpPublicKeyRing(pkBytes); keyCount++; } if (keyCount != 2) { Fail("wrong number of public keys"); } } if (count != 2) { Fail("wrong number of public keyrings"); } PgpSecretKeyRingBundle secretRings2 = new PgpSecretKeyRingBundle(sec2); count = 0; encRing = secretRings2.GetEncoded(); PgpSecretKeyRingBundle secretRings = new PgpSecretKeyRingBundle(encRing); foreach (PgpSecretKeyRing pgpSec1 in secretRings2.GetKeyRings()) { count++; int keyCount = 0; byte[] bytes = pgpSec1.GetEncoded(); PgpSecretKeyRing pgpSec2 = new PgpSecretKeyRing(bytes); foreach (PgpSecretKey k in pgpSec2.GetSecretKeys()) { keyCount++; PgpPublicKey pk = k.PublicKey; if (pk.KeyId == -1413891222336124627L) { int sCount = 0; foreach (PgpSignature pgpSignature in pk.GetSignaturesOfType(PgpSignature.SubkeyBinding)) { int type = pgpSignature.SignatureType; if (type != PgpSignature.SubkeyBinding) { Fail("failed to return correct signature type"); } sCount++; } if (sCount != 1) { Fail("failed to find binding signature"); } } pk.GetSignatures(); if (k.KeyId == -4049084404703773049L || k.KeyId == -1413891222336124627L) { k.ExtractPrivateKey(sec2pass1); } else if (k.KeyId == -6498553574938125416L || k.KeyId == 59034765524361024L) { k.ExtractPrivateKey(sec2pass2); } } if (keyCount != 2) { Fail("wrong number of secret keys"); } } if (count != 2) { Fail("wrong number of secret keyrings"); } }
public void PerformTest3() { PgpPublicKeyRingBundle pubRings = new PgpPublicKeyRingBundle(pub3); int count = 0; byte[] encRing = pubRings.GetEncoded(); pubRings = new PgpPublicKeyRingBundle(encRing); foreach (PgpPublicKeyRing pgpPub1 in pubRings.GetKeyRings()) { count++; int keyCount = 0; byte[] bytes = pgpPub1.GetEncoded(); PgpPublicKeyRing pgpPub2 = new PgpPublicKeyRing(bytes); foreach (PgpPublicKey pubK in pgpPub2.GetPublicKeys()) { keyCount++; pubK.GetSignatures(); } if (keyCount != 2) { Fail("wrong number of public keys"); } } if (count != 1) { Fail("wrong number of public keyrings"); } PgpSecretKeyRingBundle secretRings2 = new PgpSecretKeyRingBundle(sec3); count = 0; encRing = secretRings2.GetEncoded(); PgpSecretKeyRingBundle secretRings = new PgpSecretKeyRingBundle(encRing); foreach (PgpSecretKeyRing pgpSec1 in secretRings2.GetKeyRings()) { count++; int keyCount = 0; byte[] bytes = pgpSec1.GetEncoded(); PgpSecretKeyRing pgpSec2 = new PgpSecretKeyRing(bytes); foreach (PgpSecretKey k in pgpSec2.GetSecretKeys()) { keyCount++; k.ExtractPrivateKey(sec3pass1); } if (keyCount != 2) { Fail("wrong number of secret keys"); } } if (count != 1) { Fail("wrong number of secret keyrings"); } }
public void RewrapTest() { SecureRandom rand = new SecureRandom(); // Read the secret key rings PgpSecretKeyRingBundle privRings = new PgpSecretKeyRingBundle( new MemoryStream(rewrapKey, false)); foreach (PgpSecretKeyRing pgpPrivEnum in privRings.GetKeyRings()) { foreach (PgpSecretKey pgpKeyEnum in pgpPrivEnum.GetSecretKeys()) { // re-encrypt the key with an empty password PgpSecretKeyRing pgpPriv = PgpSecretKeyRing.RemoveSecretKey(pgpPrivEnum, pgpKeyEnum); PgpSecretKey pgpKey = PgpSecretKey.CopyWithNewPassword( pgpKeyEnum, rewrapPass, null, SymmetricKeyAlgorithmTag.Null, rand); pgpPriv = PgpSecretKeyRing.InsertSecretKey(pgpPriv, pgpKey); // this should succeed PgpPrivateKey privTmp = pgpKey.ExtractPrivateKey(null); } } }
public void PerformTest9() { PgpSecretKeyRingBundle secretRings1 = new PgpSecretKeyRingBundle(sec9); int count = 0; byte[] encRing = secretRings1.GetEncoded(); PgpSecretKeyRingBundle secretRings2 = new PgpSecretKeyRingBundle(encRing); foreach (PgpSecretKeyRing pgpSec1 in secretRings1.GetKeyRings()) { count++; int keyCount = 0; byte[] bytes = pgpSec1.GetEncoded(); PgpSecretKeyRing pgpSec2 = new PgpSecretKeyRing(bytes); foreach (PgpSecretKey k in pgpSec2.GetSecretKeys()) { keyCount++; PgpPrivateKey pKey = k.ExtractPrivateKey(sec9pass); if (keyCount == 1 && pKey != null) { Fail("primary secret key found, null expected"); } } if (keyCount != 3) { Fail("wrong number of secret keys"); } } if (count != 1) { Fail("wrong number of secret keyrings"); } }
public void PerformTest8() { PgpPublicKeyRingBundle pubRings = new PgpPublicKeyRingBundle(pub8); int count = 0; byte[] encRing = pubRings.GetEncoded(); pubRings = new PgpPublicKeyRingBundle(encRing); foreach (PgpPublicKeyRing pgpPub1 in pubRings.GetKeyRings()) { count++; int keyCount = 0; byte[] bytes = pgpPub1.GetEncoded(); PgpPublicKeyRing pgpPub2 = new PgpPublicKeyRing(bytes); foreach (PgpPublicKey o in pgpPub2.GetPublicKeys()) { if (o == null) Fail("null key found"); keyCount++; } if (keyCount != 2) { Fail("wrong number of public keys"); } } if (count != 2) { Fail("wrong number of public keyrings"); } PgpSecretKeyRingBundle secretRings1 = new PgpSecretKeyRingBundle(sec8); count = 0; encRing = secretRings1.GetEncoded(); PgpSecretKeyRingBundle secretRings2 = new PgpSecretKeyRingBundle(encRing); foreach (PgpSecretKeyRing pgpSec1 in secretRings1.GetKeyRings()) { count++; int keyCount = 0; byte[] bytes = pgpSec1.GetEncoded(); PgpSecretKeyRing pgpSec2 = new PgpSecretKeyRing(bytes); foreach (PgpSecretKey k in pgpSec2.GetSecretKeys()) { keyCount++; k.ExtractPrivateKey(sec8pass); } if (keyCount != 2) { Fail("wrong number of secret keys"); } } if (count != 1) { Fail("wrong number of secret keyrings"); } }
public void PerformTest4() { PgpSecretKeyRingBundle secretRings1 = new PgpSecretKeyRingBundle(sec4); int count = 0; byte[] encRing = secretRings1.GetEncoded(); PgpSecretKeyRingBundle secretRings2 = new PgpSecretKeyRingBundle(encRing); foreach (PgpSecretKeyRing pgpSec1 in secretRings1.GetKeyRings()) { count++; int keyCount = 0; byte[] bytes = pgpSec1.GetEncoded(); PgpSecretKeyRing pgpSec2 = new PgpSecretKeyRing(bytes); foreach (PgpSecretKey k in pgpSec2.GetSecretKeys()) { keyCount++; k.ExtractPrivateKey(sec3pass1); } if (keyCount != 2) { Fail("wrong number of secret keys"); } } if (count != 1) { Fail("wrong number of secret keyrings"); } }
public void EccKeyTest() { var secretRings = new PgpSecretKeyRingBundle(_eccSec1); var count = 0; foreach (PgpSecretKeyRing secretKeyRing in secretRings.GetKeyRings()) { count++; CheckEccKey(secretKeyRing); } if (count != 1) { Fail("wrong number of secret keyrings"); } }
private void checkSecretKeyRingWithPersonalCertificate( byte[] keyRing) { PgpSecretKeyRingBundle secCol = new PgpSecretKeyRingBundle(keyRing); int count = 0; foreach (PgpSecretKeyRing ring in secCol.GetKeyRings()) { IEnumerator e = ring.GetExtraPublicKeys().GetEnumerator(); while (e.MoveNext()) { ++count; } } if (count != 1) { Fail("personal certificate data subkey not found - count = " + count); } }
private PgpSecretKey ReadSecretKey( Stream inputStream) { PgpSecretKeyRingBundle pgpSec = new PgpSecretKeyRingBundle(inputStream); // // we just loop through the collection till we find a key suitable for encryption, in the real // world you would probably want to be a bit smarter about this. // // // iterate through the key rings. // foreach (PgpSecretKeyRing kRing in pgpSec.GetKeyRings()) { foreach (PgpSecretKey k in kRing.GetSecretKeys()) { if (k.IsSigningKey) { return k; } } } throw new ArgumentException("Can't find signing key in key ring."); }
/** * A simple routine that opens a key ring file and loads the first available key * suitable for signature generation. * * @param input stream to read the secret key ring collection from. * @return a secret key. * @throws IOException on a problem with using the input stream. * @throws PGPException if there is an issue parsing the input stream. */ internal static PgpSecretKey ReadSecretKey(Stream input) { PgpSecretKeyRingBundle pgpSec = new PgpSecretKeyRingBundle( PgpUtilities.GetDecoderStream(input)); // // we just loop through the collection till we find a key suitable for encryption, in the real // world you would probably want to be a bit smarter about this. // foreach (PgpSecretKeyRing keyRing in pgpSec.GetKeyRings()) { foreach (PgpSecretKey key in keyRing.GetSecretKeys()) { if (key.IsSigningKey) { return key; } } } throw new ArgumentException("Can't find signing key in key ring."); }