private void repeatHeaderTest()
        {
            MemoryStream        bOut = new MemoryStream();
            ArmoredOutputStream aOut = new ArmoredOutputStream(bOut);

            aOut.SetHeader("Comment", "Line 1");
            aOut.AddHeader("Comment", "Line 2");

            aOut.Write(sample, 0, sample.Length);

            aOut.Close();

            MemoryStream       bIn = new MemoryStream(bOut.ToArray(), false);
            ArmoredInputStream aIn = new ArmoredInputStream(bIn, true);

            string[] hdrs  = aIn.GetArmorHeaders();
            int      count = 0;

            for (int i = 0; i != hdrs.Length; i++)
            {
                if (hdrs[i].IndexOf("Comment: ") == 0)
                {
                    count++;
                }
            }

            IsEquals(2, count);
        }
        private void messageTest(
            string message,
            string type)
        {
            ArmoredInputStream aIn = new ArmoredInputStream(
                new MemoryStream(Encoding.ASCII.GetBytes(message)));

            string[] headers = aIn.GetArmorHeaders();

            if (headers == null || headers.Length != 1)
            {
                Fail("wrong number of headers found");
            }

            if (!"Hash: SHA256".Equals(headers[0]))
            {
                Fail("header value wrong: " + headers[0]);
            }

            //
            // read the input, making sure we ingore the last newline.
            //
            MemoryStream bOut = new MemoryStream();
            int          ch;

            while ((ch = aIn.ReadByte()) >= 0 && aIn.IsClearText())
            {
                bOut.WriteByte((byte)ch);
            }

            PgpPublicKeyRingBundle pgpRings = new PgpPublicKeyRingBundle(publicKey);

            PgpObjectFactory pgpFact = new PgpObjectFactory(aIn);
            PgpSignatureList p3      = (PgpSignatureList)pgpFact.NextPgpObject();
            PgpSignature     sig     = p3[0];

            sig.InitVerify(pgpRings.GetPublicKey(sig.KeyId));

            MemoryStream lineOut   = new MemoryStream();
            Stream       sigIn     = new MemoryStream(bOut.ToArray(), false);
            int          lookAhead = ReadInputLine(lineOut, sigIn);

            ProcessLine(sig, lineOut.ToArray());

            if (lookAhead != -1)
            {
                do
                {
                    lookAhead = ReadInputLine(lineOut, lookAhead, sigIn);

                    sig.Update((byte)'\r');
                    sig.Update((byte)'\n');

                    ProcessLine(sig, lineOut.ToArray());
                }while (lookAhead != -1);
            }

            if (!sig.Verify())
            {
                Fail("signature failed to verify m_in " + type);
            }
        }
		private void messageTest(
			string message,
			string type)
		{
			ArmoredInputStream aIn = new ArmoredInputStream(
				new MemoryStream(Encoding.ASCII.GetBytes(message)));

			string[] headers = aIn.GetArmorHeaders();

			if (headers == null || headers.Length != 1)
			{
				Fail("wrong number of headers found");
			}

			if (!"Hash: SHA256".Equals(headers[0]))
			{
				Fail("header value wrong: " + headers[0]);
			}

			//
			// read the input, making sure we ingore the last newline.
			//
			MemoryStream bOut = new MemoryStream();
			int ch;

			while ((ch = aIn.ReadByte()) >= 0 && aIn.IsClearText())
			{
				bOut.WriteByte((byte)ch);
			}

			PgpPublicKeyRingBundle pgpRings = new PgpPublicKeyRingBundle(publicKey);

			PgpObjectFactory	pgpFact = new PgpObjectFactory(aIn);
			PgpSignatureList	p3 = (PgpSignatureList)pgpFact.NextPgpObject();
			PgpSignature		sig = p3[0];

			sig.InitVerify(pgpRings.GetPublicKey(sig.KeyId));

			MemoryStream lineOut = new MemoryStream();
			Stream sigIn = new MemoryStream(bOut.ToArray(), false);
			int lookAhead = ReadInputLine(lineOut, sigIn);

			ProcessLine(sig, lineOut.ToArray());

			if (lookAhead != -1)
			{
				do
				{
					lookAhead = ReadInputLine(lineOut, lookAhead, sigIn);

					sig.Update((byte) '\r');
					sig.Update((byte) '\n');

					ProcessLine(sig, lineOut.ToArray());
				}
				while (lookAhead != -1);
			}

			if (!sig.Verify())
			{
				Fail("signature failed to verify m_in " + type);
			}
		}
예제 #4
0
        /// <summary>
        /// Verify signature for cleartext (e.g. emails)
        /// </summary>
        /// <param name="data">Data to verify</param>
        /// <returns>Return true if signature validates, else false.</returns>
        public bool VerifyClear(byte[] data)
        {
            Context = new CryptoContext(Context);

            var crlf     = new byte[] { (byte)'\r', (byte)'\n' };
            var encoding = ASCIIEncoding.UTF8;

            using (var dataIn = new MemoryStream(data))
                using (var armoredIn = new ArmoredInputStream(dataIn))
                {
                    if (!armoredIn.IsClearText())
                    {
                        throw new CryptoException("Error, message is not armored clear-text.");
                    }

                    var headers = armoredIn.GetArmorHeaders();
                    if (headers != null)
                    {
                        foreach (var header in headers)
                        {
                            if (Regex.IsMatch(header, @"Charset: ([^\s]*)"))
                            {
                                var encodingType = Regex.Match(header, @"Charset: ([^\s]*)").Groups[1].Value;
                                encoding = Encoding.GetEncoding(encodingType);
                            }
                        }
                    }

                    using (var clearOut = new MemoryStream())
                    {
                        using (var clearIn = new MemoryStream())
                        {
                            int ch = 0;
                            while ((ch = armoredIn.ReadByte()) >= 0 && armoredIn.IsClearText())
                            {
                                clearIn.WriteByte((byte)ch);
                            }

                            clearIn.Position = 0;

                            using (var stringIn = new StringReader(encoding.GetString(clearIn.ToArray())))
                            {
                                do
                                {
                                    var line = stringIn.ReadLine();
                                    if (line == null)
                                    {
                                        break;
                                    }

                                    line = line
                                           .TrimEnd(null)
                                           .TrimEnd(new char[] { ' ', '\t', '\n', '\r' })
                                           .TrimEnd(null)
                                           + "\r\n";

                                    var buff = encoding.GetBytes(line);
                                    clearOut.Write(buff, 0, buff.Length);
                                }while (true);
                            }
                        }

                        clearOut.Position = 0;

                        var factory       = new PgpObjectFactory(armoredIn);
                        var signatureList = (PgpSignatureList)factory.NextPgpObject();
                        var signature     = signatureList[0];

                        Context.IsEncrypted = false;
                        Context.IsSigned    = true;
                        Context.SignedBy    = GetPublicKey(signature.KeyId);

                        if (Context.SignedBy == null)
                        {
                            throw new PublicKeyNotFoundException("Public key not found for key id \"" + signature.KeyId + "\".");
                        }

                        signature.InitVerify(GetPublicKey(signature.KeyId));
                        signature.Update(clearOut.ToArray(), 0, (int)(clearOut.Length - 2));
                        Context.SignatureValidated = signature.Verify();

                        return(Context.SignatureValidated);
                    }
                }
        }