/*
         * verify a clear text signed file
         */
        public static PgpSignatureInfo VerifyFile(
            Stream inputStream,
            Stream pubkeyRing)
        {
            var aIn = new ArmoredInputStream(inputStream);

            Stream outStr = new MemoryStream();
            //
            // write out signed section using the local line separator.
            // note: trailing white space needs to be removed from the end of
            // each line RFC 4880 Section 7.1
            //
            var lineOut   = new MemoryStream();
            var lookAhead = ReadInputLine(lineOut, aIn);
            var newline   = Encoding.ASCII.GetBytes(Environment.NewLine);

            if (lookAhead != -1 && aIn.IsClearText())
            {
                var line = lineOut.ToArray();
                outStr.Write(line, 0, GetLengthWithoutSeparatorOrTrailingWhitespace(line));
                outStr.Write(newline, 0, newline.Length);

                while (lookAhead != -1 && aIn.IsClearText())
                {
                    lookAhead = ReadInputLine(lineOut, lookAhead, aIn);

                    line = lineOut.ToArray();
                    outStr.Write(line, 0, GetLengthWithoutSeparatorOrTrailingWhitespace(line));
                    outStr.Write(newline, 0, newline.Length);
                }
            }

            //outStr.Close();

            var pgpRings = new PgpPublicKeyRingBundle(pubkeyRing);

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

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

            //
            // read the input, making sure we ignore the last newline.
            //
            var sigIn = outStr;

            // Set position of stream to start.
            sigIn.Position = 0;
            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);
            }

            var siginfo = new PgpSignatureInfo();

            if (sig.Verify())
            {
                siginfo.KeyId         = String.Format("{0:X}", sig.KeyId);
                siginfo.Valid         = true;
                siginfo.Version       = sig.Version;
                siginfo.Created       = sig.CreationTime;
                siginfo.HashAlgorithm = sig.HashAlgorithm;
                siginfo.Signature     = sig;
                return(siginfo);
            }
            siginfo.KeyId         = String.Format("{0:X}", sig.KeyId);
            siginfo.Valid         = false;
            siginfo.Version       = sig.Version;
            siginfo.Created       = sig.CreationTime;
            siginfo.HashAlgorithm = sig.HashAlgorithm;
            siginfo.Signature     = sig;

            return(siginfo);
        }
예제 #2
0
        /**
         * verify the signature in in against the file fileName.
         */
        public static PgpSignatureInfo VerifySignature(
            string fileName,
            Stream signature,
            Stream keyIn)
        {
            signature = PgpUtilities.GetDecoderStream(signature);

            var pgpFact = new PgpObjectFactory(signature);
            PgpSignatureList p3;
            var o    = pgpFact.NextPgpObject();
            var data = o as PgpCompressedData;

            if (data != null)
            {
                var c1 = data;
                pgpFact = new PgpObjectFactory(c1.GetDataStream());

                p3 = (PgpSignatureList)pgpFact.NextPgpObject();
            }
            else
            {
                p3 = (PgpSignatureList)o;
            }

            var pgpPubRingCollection = new PgpPublicKeyRingBundle(
                PgpUtilities.GetDecoderStream(keyIn));
            Stream dIn = File.OpenRead(fileName);
            var    sig = p3[0];
            var    key = pgpPubRingCollection.GetPublicKey(sig.KeyId);

            sig.InitVerify(key);

            int ch;

            while ((ch = dIn.ReadByte()) >= 0)
            {
                sig.Update((byte)ch);
            }

            dIn.Close();

            var siginfo = new PgpSignatureInfo();

            if (sig.Verify())
            {
                siginfo.KeyId         = String.Format("{0:X}", sig.KeyId);
                siginfo.Valid         = true;
                siginfo.Version       = sig.Version;
                siginfo.Created       = sig.CreationTime;
                siginfo.HashAlgorithm = sig.HashAlgorithm;
                siginfo.Signature     = sig;
                return(siginfo);
            }
            siginfo.KeyId         = String.Format("{0:X}", sig.KeyId);
            siginfo.Valid         = false;
            siginfo.Version       = sig.Version;
            siginfo.Created       = sig.CreationTime;
            siginfo.HashAlgorithm = sig.HashAlgorithm;
            siginfo.Signature     = sig;

            return(siginfo);
        }