|
| 1 | +using System; |
| 2 | +using System.Collections; |
| 3 | +using System.IO; |
| 4 | +using System.Text; |
| 5 | + |
| 6 | +using NUnit.Framework; |
| 7 | +using Org.BouncyCastle.Asn1.Gnu; |
| 8 | +using Org.BouncyCastle.Crypto; |
| 9 | +using Org.BouncyCastle.Crypto.Generators; |
| 10 | +using Org.BouncyCastle.Crypto.Parameters; |
| 11 | +using Org.BouncyCastle.Security; |
| 12 | +using Org.BouncyCastle.Utilities; |
| 13 | +using Org.BouncyCastle.Utilities.Encoders; |
| 14 | +using Org.BouncyCastle.Utilities.Test; |
| 15 | + |
| 16 | +namespace Org.BouncyCastle.Bcpg.OpenPgp.Tests |
| 17 | +{ |
| 18 | + [TestFixture] |
| 19 | + public class PgpEdDsaTest2 |
| 20 | + : SimpleTest |
| 21 | + { |
| 22 | + private static readonly string edDSASampleKey = |
| 23 | + "-----BEGIN PGP PUBLIC KEY BLOCK-----\n" + |
| 24 | + "Comment: Alice's OpenPGP certificate\n" + |
| 25 | + "Comment: https://www.ietf.org/id/draft-bre-openpgp-samples-01.html\n" + |
| 26 | + "\n" + |
| 27 | + "mDMEXEcE6RYJKwYBBAHaRw8BAQdArjWwk3FAqyiFbFBKT4TzXcVBqPTB3gmzlC/U\n" + |
| 28 | + "b7O1u120JkFsaWNlIExvdmVsYWNlIDxhbGljZUBvcGVucGdwLmV4YW1wbGU+iJAE\n" + |
| 29 | + "ExYIADgCGwMFCwkIBwIGFQoJCAsCBBYCAwECHgECF4AWIQTrhbtfozp14V6UTmPy\n" + |
| 30 | + "MVUMT0fjjgUCXaWfOgAKCRDyMVUMT0fjjukrAPoDnHBSogOmsHOsd9qGsiZpgRnO\n" + |
| 31 | + "dypvbm+QtXZqth9rvwD9HcDC0tC+PHAsO7OTh1S1TC9RiJsvawAfCPaQZoed8gK4\n" + |
| 32 | + "OARcRwTpEgorBgEEAZdVAQUBAQdAQv8GIa2rSTzgqbXCpDDYMiKRVitCsy203x3s\n" + |
| 33 | + "E9+eviIDAQgHiHgEGBYIACAWIQTrhbtfozp14V6UTmPyMVUMT0fjjgUCXEcE6QIb\n" + |
| 34 | + "DAAKCRDyMVUMT0fjjlnQAQDFHUs6TIcxrNTtEZFjUFm1M0PJ1Dng/cDW4xN80fsn\n" + |
| 35 | + "0QEA22Kr7VkCjeAEC08VSTeV+QFsmz55/lntWkwYWhmvOgE=\n" + |
| 36 | + "=iIGO\n" + |
| 37 | + "-----END PGP PUBLIC KEY BLOCK-----\n"; |
| 38 | + |
| 39 | + private static readonly string edDSASecretKey = |
| 40 | + "-----BEGIN PGP PRIVATE KEY BLOCK-----\n" + |
| 41 | + "Comment: Alice's OpenPGP Transferable Secret Key\n" + |
| 42 | + "Comment: https://www.ietf.org/id/draft-bre-openpgp-samples-01.html\n" + |
| 43 | + "\n" + |
| 44 | + "lFgEXEcE6RYJKwYBBAHaRw8BAQdArjWwk3FAqyiFbFBKT4TzXcVBqPTB3gmzlC/U\n" + |
| 45 | + "b7O1u10AAP9XBeW6lzGOLx7zHH9AsUDUTb2pggYGMzd0P3ulJ2AfvQ4RtCZBbGlj\n" + |
| 46 | + "ZSBMb3ZlbGFjZSA8YWxpY2VAb3BlbnBncC5leGFtcGxlPoiQBBMWCAA4AhsDBQsJ\n" + |
| 47 | + "CAcCBhUKCQgLAgQWAgMBAh4BAheAFiEE64W7X6M6deFelE5j8jFVDE9H444FAl2l\n" + |
| 48 | + "nzoACgkQ8jFVDE9H447pKwD6A5xwUqIDprBzrHfahrImaYEZzncqb25vkLV2arYf\n" + |
| 49 | + "a78A/R3AwtLQvjxwLDuzk4dUtUwvUYibL2sAHwj2kGaHnfICnF0EXEcE6RIKKwYB\n" + |
| 50 | + "BAGXVQEFAQEHQEL/BiGtq0k84Km1wqQw2DIikVYrQrMttN8d7BPfnr4iAwEIBwAA\n" + |
| 51 | + "/3/xFPG6U17rhTuq+07gmEvaFYKfxRB6sgAYiW6TMTpQEK6IeAQYFggAIBYhBOuF\n" + |
| 52 | + "u1+jOnXhXpROY/IxVQxPR+OOBQJcRwTpAhsMAAoJEPIxVQxPR+OOWdABAMUdSzpM\n" + |
| 53 | + "hzGs1O0RkWNQWbUzQ8nUOeD9wNbjE3zR+yfRAQDbYqvtWQKN4AQLTxVJN5X5AWyb\n" + |
| 54 | + "Pnn+We1aTBhaGa86AQ==\n" + |
| 55 | + "=n8OM\n" + |
| 56 | + "-----END PGP PRIVATE KEY BLOCK-----\n"; |
| 57 | + |
| 58 | + private static readonly string revBlock = |
| 59 | + "-----BEGIN PGP PUBLIC KEY BLOCK-----\n" + |
| 60 | + "Comment: Alice's revocation certificate\n" + |
| 61 | + "Comment: https://www.ietf.org/id/draft-bre-openpgp-samples-01.html\n" + |
| 62 | + "\n" + |
| 63 | + "iHgEIBYIACAWIQTrhbtfozp14V6UTmPyMVUMT0fjjgUCXaWkOwIdAAAKCRDyMVUM\n" + |
| 64 | + "T0fjjoBlAQDA9ukZFKRFGCooVcVoDVmxTaHLUXlIg9TPh2f7zzI9KgD/SLNXUOaH\n" + |
| 65 | + "O6TozOS7C9lwIHwwdHdAxgf5BzuhLT9iuAM=\n" + |
| 66 | + "=Tm8h\n" + |
| 67 | + "-----END PGP PUBLIC KEY BLOCK-----\n"; |
| 68 | + |
| 69 | + |
| 70 | + private void EncryptDecryptBcTest(PgpPublicKey pubKey, PgpPrivateKey secKey) |
| 71 | + { |
| 72 | + byte[] text = { (byte)'h', (byte)'e', (byte)'l', (byte)'l', (byte)'o', (byte)' ', (byte)'w', (byte)'o', (byte)'r', (byte)'l', (byte)'d', (byte)'!', (byte)'\n' }; |
| 73 | + |
| 74 | + PgpLiteralDataGenerator lData = new PgpLiteralDataGenerator(); |
| 75 | + MemoryStream ldOut = new MemoryStream(); |
| 76 | + Stream pOut = lData.Open(ldOut, PgpLiteralDataGenerator.Utf8, PgpLiteralData.Console, text.Length, DateTime.Now); |
| 77 | + |
| 78 | + pOut.Write(text, 0, text.Length); |
| 79 | + |
| 80 | + pOut.Close(); |
| 81 | + |
| 82 | + byte[] data = ldOut.ToArray(); |
| 83 | + |
| 84 | + MemoryStream cbOut = new MemoryStream(); |
| 85 | + |
| 86 | + PgpEncryptedDataGenerator cPk = new PgpEncryptedDataGenerator(SymmetricKeyAlgorithmTag.Cast5); |
| 87 | + |
| 88 | + cPk.AddMethod(pubKey); |
| 89 | + |
| 90 | + Stream cOut = cPk.Open(/* new UncloseableOutputStream(*/ cbOut /* ) */, data.Length); |
| 91 | + |
| 92 | + cOut.Write(data, 0, data.Length); |
| 93 | + |
| 94 | + cOut.Close(); |
| 95 | + |
| 96 | + PgpObjectFactory pgpF = new PgpObjectFactory(cbOut.ToArray()); |
| 97 | + |
| 98 | + PgpEncryptedDataList encList = (PgpEncryptedDataList)pgpF.NextObject(); |
| 99 | + |
| 100 | + PgpPublicKeyEncryptedData encP = (PgpPublicKeyEncryptedData)encList.Get(0); |
| 101 | + |
| 102 | + Stream clear = encP.GetDataStream(secKey); |
| 103 | + |
| 104 | + pgpF = new PgpObjectFactory(clear); |
| 105 | + |
| 106 | + PgpLiteralData ld = (PgpLiteralData)pgpF.NextObject(); |
| 107 | + |
| 108 | + clear = ld.GetInputStream(); |
| 109 | + MemoryStream bOut = new MemoryStream(); |
| 110 | + |
| 111 | + int ch; |
| 112 | + while ((ch = clear.ReadByte()) >= 0) |
| 113 | + { |
| 114 | + bOut.WriteByte((byte)ch); |
| 115 | + } |
| 116 | + |
| 117 | + byte[] tout = bOut.ToArray(); |
| 118 | + |
| 119 | + if (!AreEqual(tout, text)) |
| 120 | + { |
| 121 | + Fail("wrong plain text in generated packet"); |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + private void KeyringBcTest() |
| 126 | + { |
| 127 | + String identity = "[email protected]"; |
| 128 | + char[] passPhrase = "Hello, world!".ToCharArray(); |
| 129 | + |
| 130 | + |
| 131 | + Ed25519KeyPairGenerator edKp = new Ed25519KeyPairGenerator(); |
| 132 | + edKp.Init(new Ed25519KeyGenerationParameters(new SecureRandom())); |
| 133 | + |
| 134 | + |
| 135 | + PgpKeyPair dsaKeyPair = new PgpKeyPair(PublicKeyAlgorithmTag.EdDsa, edKp.GenerateKeyPair(), DateTime.Now); |
| 136 | + |
| 137 | + X25519KeyPairGenerator dhKp = new X25519KeyPairGenerator(); |
| 138 | + dhKp.Init(new X25519KeyGenerationParameters(new SecureRandom())); |
| 139 | + |
| 140 | + PgpKeyPair dhKeyPair = new PgpKeyPair(PublicKeyAlgorithmTag.ECDH, dhKp.GenerateKeyPair(), DateTime.Now); |
| 141 | + |
| 142 | + EncryptDecryptBcTest(dhKeyPair.PublicKey, dhKeyPair.PrivateKey); |
| 143 | + |
| 144 | + PgpKeyRingGenerator keyRingGen = new PgpKeyRingGenerator( |
| 145 | + PgpSignature.PositiveCertification, dsaKeyPair, |
| 146 | + identity, SymmetricKeyAlgorithmTag.Aes256, HashAlgorithmTag.Sha1, true, passPhrase, true, null, null, new SecureRandom() |
| 147 | + ); |
| 148 | + |
| 149 | + keyRingGen.AddSubKey(dhKeyPair); |
| 150 | + |
| 151 | + MemoryStream secretOut = new MemoryStream(); |
| 152 | + |
| 153 | + PgpSecretKeyRing secRing = keyRingGen.GenerateSecretKeyRing(); |
| 154 | + |
| 155 | + PgpPublicKeyRing pubRing = keyRingGen.GeneratePublicKeyRing(); |
| 156 | + |
| 157 | + secRing.Encode(secretOut); |
| 158 | + |
| 159 | + secretOut.Close(); |
| 160 | + secRing = new PgpSecretKeyRing(secretOut.ToArray()); |
| 161 | + |
| 162 | + var pit = secRing.GetSecretKeys().GetEnumerator(); |
| 163 | + pit.MoveNext(); |
| 164 | + pit.MoveNext(); |
| 165 | + PgpPublicKey sKey = ((PgpSecretKey)pit.Current).PublicKey; |
| 166 | + |
| 167 | + PgpPublicKey vKey = secRing.GetPublicKey(); |
| 168 | + |
| 169 | + int count = 0; |
| 170 | + foreach (PgpSignature sig in sKey.GetSignatures()) |
| 171 | + { |
| 172 | + if (sig.KeyId == vKey.KeyId && sig.SignatureType == PgpSignature.SubkeyBinding) |
| 173 | + { |
| 174 | + count++; |
| 175 | + sig.InitVerify(vKey); |
| 176 | + if (!sig.VerifyCertification(vKey, sKey)) |
| 177 | + { |
| 178 | + Fail("failed to verify sub-key signature."); |
| 179 | + } |
| 180 | + } |
| 181 | + } |
| 182 | + IsTrue(count == 1); |
| 183 | + |
| 184 | + secRing = new PgpSecretKeyRing(secretOut.ToArray()); |
| 185 | + |
| 186 | + PgpPublicKey pubKey = sKey; |
| 187 | + PgpPrivateKey privKey = secRing.GetSecretKey(pubKey.KeyId).ExtractPrivateKey(passPhrase); |
| 188 | + |
| 189 | + if (privKey == null) |
| 190 | + { |
| 191 | + Fail("Could not find private key"); |
| 192 | + } |
| 193 | + |
| 194 | + EncryptDecryptBcTest(pubKey, privKey); |
| 195 | + } |
| 196 | + |
| 197 | + public override void PerformTest() |
| 198 | + { |
| 199 | + ArmoredInputStream aIn = new ArmoredInputStream(new MemoryStream(Encoding.ASCII.GetBytes(edDSASampleKey))); |
| 200 | + |
| 201 | + PgpPublicKeyRing pubKeyRing = new PgpPublicKeyRing(aIn); |
| 202 | + |
| 203 | + IsTrue(AreEqual(Hex.Decode("EB85 BB5F A33A 75E1 5E94 4E63 F231 550C 4F47 E38E"), pubKeyRing.GetPublicKey().GetFingerprint())); |
| 204 | + |
| 205 | + aIn = new ArmoredInputStream(new MemoryStream(Encoding.ASCII.GetBytes(edDSASecretKey))); |
| 206 | + |
| 207 | + PgpSecretKeyRing secRing = new PgpSecretKeyRing(aIn); |
| 208 | + |
| 209 | + IsTrue(secRing.GetSecretKey().IsSigningKey); |
| 210 | + |
| 211 | + PgpSignatureGenerator pgpGen = new PgpSignatureGenerator(PublicKeyAlgorithmTag.EdDsa, HashAlgorithmTag.Sha256); |
| 212 | + |
| 213 | + pgpGen.InitSign(PgpSignature.SubkeyBinding, secRing.GetSecretKey().ExtractPrivateKey(null)); |
| 214 | + |
| 215 | + PgpSignature sig = pgpGen.GenerateCertification(pubKeyRing.GetPublicKey(), pubKeyRing.GetPublicKey(5145070902336167606L)); |
| 216 | + |
| 217 | + sig.InitVerify(pubKeyRing.GetPublicKey()); |
| 218 | + |
| 219 | + IsTrue(sig.VerifyCertification(pubKeyRing.GetPublicKey(), pubKeyRing.GetPublicKey(5145070902336167606L))); |
| 220 | + |
| 221 | + EncryptDecryptBcTest(pubKeyRing.GetPublicKey(5145070902336167606L), |
| 222 | + secRing.GetSecretKey(5145070902336167606L).ExtractPrivateKey(null)); |
| 223 | + |
| 224 | + aIn = new ArmoredInputStream(new MemoryStream(Encoding.ASCII.GetBytes(revBlock))); |
| 225 | + |
| 226 | + PgpSignatureList sigs = (PgpSignatureList)new PgpObjectFactory(aIn).NextObject(); |
| 227 | + |
| 228 | + sig = sigs.Get(0); |
| 229 | + |
| 230 | + sig.InitVerify(pubKeyRing.GetPublicKey()); |
| 231 | + |
| 232 | + IsTrue(sig.VerifyCertification(pubKeyRing.GetPublicKey())); |
| 233 | + |
| 234 | + KeyringBcTest(); |
| 235 | + SksKeyTest(); |
| 236 | + AliceBcKeyTest(); |
| 237 | + } |
| 238 | + |
| 239 | + private void AliceBcKeyTest() |
| 240 | + { |
| 241 | + byte[] text = { (byte)'h', (byte)'e', (byte)'l', (byte)'l', (byte)'o', (byte)' ', (byte)'w', (byte)'o', (byte)'r', (byte)'l', (byte)'d', (byte)'!', (byte)'\n' }; |
| 242 | + ArmoredInputStream aIn = new ArmoredInputStream(new MemoryStream(Encoding.ASCII.GetBytes(edDSASampleKey))); |
| 243 | + |
| 244 | + PgpPublicKeyRing rng = new PgpPublicKeyRing(aIn); |
| 245 | + |
| 246 | + aIn = new ArmoredInputStream(new MemoryStream(Encoding.ASCII.GetBytes(edDSASecretKey))); |
| 247 | + |
| 248 | + PgpSecretKeyRing secRing = new PgpSecretKeyRing(aIn); |
| 249 | + |
| 250 | + PgpPublicKey pubKey = rng.GetPublicKey(5145070902336167606L); |
| 251 | + PgpPrivateKey privKey = secRing.GetSecretKey(5145070902336167606L).ExtractPrivateKey(null); |
| 252 | + |
| 253 | + PgpLiteralDataGenerator lData = new PgpLiteralDataGenerator(); |
| 254 | + MemoryStream ldOut = new MemoryStream(); |
| 255 | + Stream pOut = lData.Open(ldOut, PgpLiteralDataGenerator.Utf8, PgpLiteralData.Console, text.Length, DateTime.Now); |
| 256 | + |
| 257 | + pOut.Write(text, 0, text.Length); |
| 258 | + |
| 259 | + pOut.Close(); |
| 260 | + |
| 261 | + byte[] data = ldOut.ToArray(); |
| 262 | + |
| 263 | + MemoryStream cbOut = new MemoryStream(); |
| 264 | + |
| 265 | + PgpEncryptedDataGenerator cPk = new PgpEncryptedDataGenerator(SymmetricKeyAlgorithmTag.Aes128, true); |
| 266 | + |
| 267 | + cPk.AddMethod(pubKey); |
| 268 | + |
| 269 | + Stream cOut = cPk.Open(/* new UncloseableOutputStream( */ cbOut /* ) */, data.Length); |
| 270 | + |
| 271 | + cOut.Write(data, 0, data.Length); |
| 272 | + |
| 273 | + cOut.Close(); |
| 274 | + |
| 275 | + PgpObjectFactory pgpF = new PgpObjectFactory(cbOut.ToArray()); |
| 276 | + |
| 277 | + PgpEncryptedDataList encList = (PgpEncryptedDataList)pgpF.NextObject(); |
| 278 | + |
| 279 | + PgpPublicKeyEncryptedData encP = (PgpPublicKeyEncryptedData)encList.Get(0); |
| 280 | + |
| 281 | + Stream clear = encP.GetDataStream(privKey); |
| 282 | + |
| 283 | + pgpF = new PgpObjectFactory(clear); |
| 284 | + |
| 285 | + PgpLiteralData ld = (PgpLiteralData)pgpF.NextObject(); |
| 286 | + |
| 287 | + clear = ld.GetInputStream(); |
| 288 | + MemoryStream bOut = new MemoryStream(); |
| 289 | + |
| 290 | + int ch; |
| 291 | + while ((ch = clear.ReadByte()) >= 0) |
| 292 | + { |
| 293 | + bOut.WriteByte((byte)ch); |
| 294 | + } |
| 295 | + |
| 296 | + byte[] tout = bOut.ToArray(); |
| 297 | + |
| 298 | + if (!AreEqual(tout, text)) |
| 299 | + { |
| 300 | + Fail("wrong plain text in generated packet"); |
| 301 | + } |
| 302 | + } |
| 303 | + |
| 304 | + private void SksKeyTest() |
| 305 | + { |
| 306 | + byte[] data = Strings.ToByteArray("testing, 1, 2, 3, testing..."); |
| 307 | + |
| 308 | + ArmoredInputStream aIn = new ArmoredInputStream(new MemoryStream(Encoding.ASCII.GetBytes(@"-----BEGIN PGP PUBLIC KEY BLOCK----- |
| 309 | +Version: SKS 1.1.6 |
| 310 | +
|
| 311 | +mDMEXl1WjhYJKwYBBAHaRw8BAQdAoPlx4e6UlAd0tDq8SPjwNHqUciv+FybLYrPocBJ6Ze20 |
| 312 | +HlJvYiBEZW5uaXMgPHJvYmRAdGVsZWNvbTI2LmNoPoiQBBMWCAA4FiEEtDGzEElVJ9+SNbQu |
| 313 | +ZgxU5RXBR+oFAl5dVo4CGwMFCwkIBwIGFQoJCAsCBBYCAwECHgECF4AACgkQZgxU5RXBR+oS |
| 314 | +ywD/RowXCrbr8dj9uVpuVKe2FFN+SdlWk/xae0LlniAeJ6QBAK+SnvX2bVStf1XIUxupqheZ |
| 315 | +zj+W8kojFNXPK9UBECwIuDgEXl1WjhIKKwYBBAGXVQEFAQEHQBC8TTeQKgW1ml2S/uMrYETD |
| 316 | +w56ilf/FTTTdViCJjiVGAwEIB4h4BBgWCAAgFiEEtDGzEElVJ9+SNbQuZgxU5RXBR+oFAl5d |
| 317 | +Vo4CGwwACgkQZgxU5RXBR+pY+QD/ap3BMh/ottU4nzEg7Vo2lF/IxsBTLKkKXaXxN4a19O0B |
| 318 | +ALUL3OLNjjcGZzKaNkkg0MGjwg/S+1xod7+75Jk3CmMD |
| 319 | +=GN3J |
| 320 | +-----END PGP PUBLIC KEY BLOCK----- |
| 321 | +"))); |
| 322 | + |
| 323 | + // make sure we can parse it without falling over. |
| 324 | + PgpPublicKeyRing rng = new PgpPublicKeyRing(aIn); |
| 325 | + |
| 326 | + PgpEncryptedDataGenerator encDataGen = new |
| 327 | + PgpEncryptedDataGenerator(SymmetricKeyAlgorithmTag.Aes128, true); |
| 328 | + |
| 329 | + encDataGen.AddMethod(rng.GetPublicKey(6752245936421807937L)); |
| 330 | + |
| 331 | + MemoryStream cbOut = new MemoryStream(); |
| 332 | + |
| 333 | + Stream cOut = encDataGen.Open(/* new UncloseableOutputStream( */ cbOut /* ) */, data.Length); |
| 334 | + |
| 335 | + cOut.Write(data, 0, data.Length); |
| 336 | + |
| 337 | + cOut.Close(); |
| 338 | + } |
| 339 | + |
| 340 | + public override string Name |
| 341 | + { |
| 342 | + get { return "PgpEdDsaTest2"; } |
| 343 | + } |
| 344 | + |
| 345 | + public static void Main( |
| 346 | + string[] args) |
| 347 | + { |
| 348 | + RunTest(new PgpECDsaTest()); |
| 349 | + } |
| 350 | + |
| 351 | + [Test] |
| 352 | + public void TestFunction() |
| 353 | + { |
| 354 | + string resultText = Perform().ToString(); |
| 355 | + |
| 356 | + Assert.AreEqual(Name + ": Okay", resultText); |
| 357 | + } |
| 358 | + } |
| 359 | +} |
0 commit comments