private static string CreateSignature(
            string message,
            Stream keyIn,
            Stream outputStream,
            char[]  pass,
            bool armor)
        {
            if (armor)
            {
                outputStream = new ArmoredOutputStream(outputStream);
            }

            PgpSecretKey          pgpSec     = PgpExampleUtilities.ReadSecretKey(keyIn);
            PgpPrivateKey         pgpPrivKey = pgpSec.ExtractPrivateKey(pass);
            PgpSignatureGenerator sGen       = new PgpSignatureGenerator(
                pgpSec.PublicKey.Algorithm, HashAlgorithmTag.Sha1);

            sGen.InitSign(PgpSignature.BinaryDocument, pgpPrivKey);

            BcpgOutputStream bOut = new BcpgOutputStream(outputStream);

            sGen.Update(System.Text.Encoding.UTF8.GetBytes(message));
            PgpSignature sig = sGen.Generate();

            sig.Encode(bOut);

            if (armor)
            {
                outputStream.Close();
            }

            MemoryStream ms = new MemoryStream();

            sig.Encode(ms);
            byte[] bytes = ms.ToArray();
            return(Convert.ToBase64String(bytes));
        }
Ejemplo n.º 2
0
        private void doTestTextSig(
            PublicKeyAlgorithmTag encAlgorithm,
            HashAlgorithmTag hashAlgorithm,
            PgpPublicKey pubKey,
            PgpPrivateKey privKey,
            byte[]                                  data,
            byte[]                                  canonicalData)
        {
            PgpSignatureGenerator sGen   = new PgpSignatureGenerator(encAlgorithm, HashAlgorithmTag.Sha1);
            MemoryStream          bOut   = new MemoryStream();
            MemoryStream          testIn = new MemoryStream(data, false);
            DateTime creationTime        = DateTime.UtcNow;

            sGen.InitSign(PgpSignature.CanonicalTextDocument, privKey);
            sGen.GenerateOnePassVersion(false).Encode(bOut);

            PgpLiteralDataGenerator lGen = new PgpLiteralDataGenerator();
            Stream lOut = lGen.Open(
                new UncloseableStream(bOut),
                PgpLiteralData.Text,
                "_CONSOLE",
                data.Length * 2,
                creationTime);

            int ch;

            while ((ch = testIn.ReadByte()) >= 0)
            {
                lOut.WriteByte((byte)ch);
                sGen.Update((byte)ch);
            }

            lOut.Write(data, 0, data.Length);
            sGen.Update(data);

            lGen.Close();

            PgpSignature sig = sGen.Generate();

            if (sig.CreationTime == DateTimeUtilities.UnixMsToDateTime(0))
            {
                Fail("creation time not set in v4 signature");
            }

            sig.Encode(bOut);

            verifySignature(bOut.ToArray(), hashAlgorithm, pubKey, canonicalData);
        }
Ejemplo n.º 3
0
        public static void AddSignature(PgpPublicKey key, KeyStoreDB keyDb, string keyExportName, PgpSecretKey secKey, char[] passPhrase,
                                        SignatureOperation op, DateTime expiryDate, PgpSignature certLevel = null, string userId = "")
        {
            string fileData = string.Empty;
            bool   useTemp  = true;

            if (key == null)
            {
                throw new ArgumentNullException("key");
            }
            if (keyDb == null)
            {
                throw new ArgumentNullException("keyDb");
            }
            if (!Enum.IsDefined(typeof(SignatureOperation), op))
            {
                throw new ArgumentOutOfRangeException(string.Format("op: {0}", op));
            }

            AlgorithmAgreement algorithms = new AlgorithmAgreement(new List <PgpPublicKey>()
            {
                key
            });
            PgpPrivateKey         privateKey = secKey.ExtractPrivateKey(passPhrase);
            PgpSignatureGenerator sigGen     = new PgpSignatureGenerator(key.Algorithm, algorithms.AgreedHashAlgorithm);

            switch (op)
            {
            case SignatureOperation.AddUserId:
                break;

            case SignatureOperation.RevokeKey:
                MemoryStream mStream = new MemoryStream();
                using (ArmoredOutputStream outArmour = new ArmoredOutputStream(mStream)) {
                    outArmour.SetHeader("Version", "Lynx Privacy");
                    sigGen.InitSign(PgpSignature.KeyRevocation, privateKey);
                    PgpSignature sig = sigGen.GenerateCertification(secKey.PublicKey);
                    PgpPublicKey.AddCertification(secKey.PublicKey, sig);
                    sig.InitVerify(secKey.PublicKey);
                    if (!sig.VerifyCertification(secKey.PublicKey))
                    {
                        throw new PgpException("revocation verification failed.");
                    }
                    sig.Encode(outArmour);
                    outArmour.Close();
                }
                mStream.Position = 0;
                StreamReader srdr   = new StreamReader(mStream);
                string       armour = srdr.ReadToEnd();
                string       outstr = armour.Replace("BEGIN PGP SIGNATURE", "BEGIN PGP PUBLIC KEY BLOCK")
                                      .Replace("END PGP SIGNATURE", "END PGP PUBLIC KEY BLOCK");
                mStream.Close();
                if (string.IsNullOrEmpty(keyExportName))
                {
                    useTemp = true;
                    string tempPath = Path.GetTempPath();
                    keyExportName = Path.Combine(tempPath, Guid.NewGuid().ToString() + ".tmppgp");
                }
                File.WriteAllText(keyExportName, outstr);
                keyExportName = "";
                //Debug.Assert(secKey.PublicKey.IsRevoked() == true);
                break;

            case SignatureOperation.SetKeyExpiry:
                break;

            case SignatureOperation.CertifyKey:
                break;

            default:
                break;
            }


            if (secKey.PublicKey != null)
            {
                if (string.IsNullOrEmpty(keyExportName))
                {
                    useTemp = true;
                    string tempPath = Path.GetTempPath();
                    keyExportName = Path.Combine(tempPath, Guid.NewGuid().ToString() + ".tmppgp");
                }
                ExportKey expKey = new ExportKey(keyDb);
                expKey.UpdateDbSecretKey(secKey, keyExportName);
                if (useTemp)
                {
                    //File.Delete(keyExportName);
                }
            }
        }