Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OpenPGP encryption with BouncyCastle

I have been trying to put together an in-memory public-key encryption infrastructure using OpenPGP via Bouncy Castle. One of our vendors uses OpenPGP public key encryption to encrypt all their feeds, and requires us to do the same, so I'm stuck with the technology and the implementation. So now I'm coding an OpenPGP encryption/ decryption toolkit for automating these feeds.

The examples at bouncycastle.org inexplicably default to writing encrypted data to and collecting keys from a file system; this is not what I want to do, so I've been trying to get everything stream-based.

I have gotten to the point where I can actually get my code to compile and run, but my encrypted payload is empty. I think I'm missing something silly, but after several days of trying this and that, I have lost the ability to objectively examine this.

My utility class contains these methods:

    public static PgpPublicKey ImportPublicKey(
        this Stream publicIn)
    {
        var pubRings =
            new PgpPublicKeyRingBundle(PgpUtilities.GetDecoderStream(publicIn)).GetKeyRings().OfType<PgpPublicKeyRing>();
        var pubKeys = pubRings.SelectMany(x => x.GetPublicKeys().OfType<PgpPublicKey>());
        var pubKey = pubKeys.FirstOrDefault();
        return pubKey;
    }

    public static Stream Streamify(this string theString, Encoding encoding = null)
    {
        encoding = encoding ?? Encoding.UTF8;
        var stream = new MemoryStream(encoding.GetBytes(theString));
        return stream;
    }

    public static string Stringify(this Stream theStream,
                                   Encoding encoding = null)
    {
        encoding = encoding ?? Encoding.UTF8;
        using (var reader = new StreamReader(theStream, encoding))
        {
            return reader.ReadToEnd();
        }
    }

    public static byte[] ReadFully(this Stream stream)
    {
        if (!stream.CanRead) throw new ArgumentException("This is not a readable stream.");
        var buffer = new byte[32768];
        using (var ms = new MemoryStream())
        {
            while (true)
            {
                var read = stream.Read(buffer, 0, buffer.Length);
                if (read <= 0)
                    return ms.ToArray();
                ms.Write(buffer, 0, read);
            }
        }
    }

    public static void PgpEncrypt(
        this Stream toEncrypt,
        Stream outStream,
        PgpPublicKey encryptionKey,
        bool armor = true,
        bool verify = true,
        CompressionAlgorithmTag compressionAlgorithm = CompressionAlgorithmTag.Zip)
    {
        if (armor) outStream = new ArmoredOutputStream(outStream);
        var compressor = new PgpCompressedDataGenerator(CompressionAlgorithmTag.Zip);
        outStream = compressor.Open(outStream);
        var data = toEncrypt.ReadFully();
        var encryptor = new PgpEncryptedDataGenerator(SymmetricKeyAlgorithmTag.Cast5, verify, new SecureRandom());
        encryptor.AddMethod(encryptionKey);
        outStream = encryptor.Open(outStream, data.Length);
        outStream.Write(data, 0, data.Length);
    }

My test method looks like this:

    private static void EncryptMessage()
    {
        var pubKey = @"<public key text>";

        var clearText = "This is an encrypted message.  There are many like it but this one is cryptic.";
        using (var stream = pubKey.Streamify())
        {
            var key = stream.ImportPublicKey();
            using (var clearStream = clearText.Streamify())
            using (var cryptoStream = new MemoryStream())
            {
                clearStream.PgpEncrypt(cryptoStream,key);
                cryptoStream.Position = 0;
                Console.WriteLine(cryptoStream.Stringify());
                Console.WriteLine("Press any key to continue.");
            }
        }
        Console.ReadKey();
    }

The result I get looks like this:

-----BEGIN PGP MESSAGE-----
Version: BCPG C# v1.7.4114.6378


Press any key to continue.

Can someone tell me what I am doing wrong?

like image 235
Jeremy Holovacs Avatar asked Sep 17 '13 18:09

Jeremy Holovacs


People also ask

What encryption does OpenPGP use?

It uses a combination of encryption methodologies such as hashing, data compression, symmetric private-key cryptography, and asymmetric public-key cryptography to keep data secure. PGP encryption can be used to secure text files, emails, data files, directories, and disk partitions.

Is OpenPGP compatible with PGP?

Yes, both GnuPG and the commercial PGP.com are implementations of the same OpenPGP standard; in fact, GnuPG was specifically designed to be PGP-compatible. The only differences you might encounter are: Different supported algorithms. For example, GnuPG supports the Camellia encryption algorithm, while PGP.com does not.

How does OpenPGP encryption work?

The public key is tied to a particular person's identity, and anyone can use it to send them a message. The sender sends their encrypted PGP session key to the recipient, and they are able to decrypt it using their private key. Using this session key, the recipient is now able to decrypt the actual message.

How do I open encrypted messages in OpenPGP?

Paste the cipher-text above into your terminal, enter the password if prompted then, press enter and then Ctrl+D to tell the system that it is at the end of the input. GnuPG will then present to you the decrypted plain-text for this cipher-text.


1 Answers

OK, I managed to get this working. There were several problems with this implementation. One problem was that certain things had to be done in order. Here is what seems to need to happen:

  • The raw data needs to be put into a PgpLiteralData object
  • The literal data needs to be encrypted.
  • The encrypted data needs to be compressed.
  • The compressed data (optionally) needs to be armored.
  • The underlying streams need to be closed in order of usage.

There should be a more elegant way to do this, but the streams used by the BouncyCastle library are all frustratingly one-way, and at several points, I needed to convert the stream to a byte array to get another part to work. I include the code I used and independently verified; if someone has a verifyably better way of doing this, I would be quite interested.

public static class OpenPgpUtility
{
    public static void ExportKeyPair(
        Stream secretOut,
        Stream publicOut,
        AsymmetricKeyParameter publicKey,
        AsymmetricKeyParameter privateKey,
        string identity,
        char[] passPhrase,
        bool armor)
    {
        if (armor)
        {
            secretOut = new ArmoredOutputStream(secretOut);
        }

        var secretKey = new PgpSecretKey(
            PgpSignature.DefaultCertification,
            PublicKeyAlgorithmTag.RsaGeneral,
            publicKey,
            privateKey,
            DateTime.UtcNow,
            identity,
            SymmetricKeyAlgorithmTag.Cast5,
            passPhrase,
            null,
            null,
            new SecureRandom()
            );

        secretKey.Encode(secretOut);

        if (armor)
        {
            secretOut.Close();
            publicOut = new ArmoredOutputStream(publicOut);
        }

        var key = secretKey.PublicKey;

        key.Encode(publicOut);

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

    public static PgpPublicKey ImportPublicKey(
        this Stream publicIn)
    {
        var pubRings =
            new PgpPublicKeyRingBundle(PgpUtilities.GetDecoderStream(publicIn)).GetKeyRings().OfType<PgpPublicKeyRing>();
        var pubKeys = pubRings.SelectMany(x => x.GetPublicKeys().OfType<PgpPublicKey>());
        var pubKey = pubKeys.FirstOrDefault();
        return pubKey;
    }

    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 static Stream Streamify(this string theString, Encoding encoding = null)
    {
        encoding = encoding ?? Encoding.UTF8;
        var stream = new MemoryStream(encoding.GetBytes(theString));
        return stream;
    }

    public static string Stringify(this Stream theStream,
                                   Encoding encoding = null)
    {
        encoding = encoding ?? Encoding.UTF8;
        using (var reader = new StreamReader(theStream, encoding))
        {
            return reader.ReadToEnd();
        }
    }

    public static byte[] ReadFully(this Stream stream, int position = 0)
    {
        if (!stream.CanRead) throw new ArgumentException("This is not a readable stream.");
        if (stream.CanSeek) stream.Position = 0;
        var buffer = new byte[32768];
        using (var ms = new MemoryStream())
        {
            while (true)
            {
                var read = stream.Read(buffer, 0, buffer.Length);
                if (read <= 0)
                    return ms.ToArray();
                ms.Write(buffer, 0, read);
            }
        }
    }

    public static void PgpEncrypt(
        this Stream toEncrypt,
        Stream outStream,
        PgpPublicKey encryptionKey,
        bool armor = true,
        bool verify = false,
        CompressionAlgorithmTag compressionAlgorithm = CompressionAlgorithmTag.Zip)
    {
        var encryptor = new PgpEncryptedDataGenerator(SymmetricKeyAlgorithmTag.Cast5, verify, new SecureRandom());
        var literalizer = new PgpLiteralDataGenerator();
        var compressor = new PgpCompressedDataGenerator(compressionAlgorithm);
        encryptor.AddMethod(encryptionKey);

        //it would be nice if these streams were read/write, and supported seeking.  Since they are not,
        //we need to shunt the data to a read/write stream so that we can control the flow of data as
        //we go.
        using (var stream = new MemoryStream()) // this is the read/write stream
        using (var armoredStream = armor ? new ArmoredOutputStream(stream) : stream as Stream)
        using (var compressedStream = compressor.Open(armoredStream))
        {
            //data is encrypted first, then compressed, but because of the one-way nature of these streams,
            //other "interim" streams are required.  The raw data is encapsulated in a "Literal" PGP object.
            var rawData = toEncrypt.ReadFully();
            var buffer = new byte[1024];
            using (var literalOut = new MemoryStream())
            using (var literalStream = literalizer.Open(literalOut, 'b', "STREAM", DateTime.UtcNow, buffer))
            {
                literalStream.Write(rawData, 0, rawData.Length);
                literalStream.Close();
                var literalData = literalOut.ReadFully();

                //The literal data object is then encrypted, which flows into the compressing stream and
                //(optionally) into the ASCII armoring stream.
                using (var encryptedStream = encryptor.Open(compressedStream, literalData.Length))
                {
                    encryptedStream.Write(literalData, 0, literalData.Length);
                    encryptedStream.Close();
                    compressedStream.Close();
                    armoredStream.Close();

                    //the stream processes are now complete, and our read/write stream is now populated with 
                    //encrypted data.  Convert the stream to a byte array and write to the out stream.
                    stream.Position = 0;
                    var data = stream.ReadFully();
                    outStream.Write(data, 0, data.Length);
                }
            }
        }
    }
}

My test method looked like this:

    private static void EncryptMessage()
    {
        var pubKey = @"<public key text here>";

        var clearText = @"<message text here>";
        using (var stream = pubKey.Streamify())
        {
            var key = stream.ImportPublicKey();
            using (var clearStream = clearText.Streamify())
            using (var cryptoStream = new MemoryStream())
            {
                clearStream.PgpEncrypt(cryptoStream, key);
                cryptoStream.Position = 0;
                var cryptoString = cryptoStream.Stringify();
                Console.WriteLine(cryptoString);
                Console.WriteLine("Press any key to continue.");
            }
        }
        Console.ReadKey();
    }

Since someone asked, my decryption algorithm looked like this:

public static Stream PgpDecrypt(
    this Stream encryptedData,
    string armoredPrivateKey,
    string privateKeyPassword,
    Encoding armorEncoding = null)
{
    armorEncoding = armorEncoding ?? Encoding.UTF8;
    var stream = PgpUtilities.GetDecoderStream(encryptedData);
    var layeredStreams = new List<Stream> { stream }; //this is to clean up/ dispose of any layered streams.
    var dataObjectFactory = new PgpObjectFactory(stream);
    var dataObject = dataObjectFactory.NextPgpObject();
    Dictionary<long, PgpSecretKey> secretKeys;

    using (var privateKeyStream = armoredPrivateKey.Streamify(armorEncoding))
    {
        var secRings =
            new PgpSecretKeyRingBundle(PgpUtilities.GetDecoderStream(privateKeyStream)).GetKeyRings()
                                                                                       .OfType<PgpSecretKeyRing>();
        var pgpSecretKeyRings = secRings as PgpSecretKeyRing[] ?? secRings.ToArray();
        if (!pgpSecretKeyRings.Any()) throw new ArgumentException("No secret keys found.");
        secretKeys = pgpSecretKeyRings.SelectMany(x => x.GetSecretKeys().OfType<PgpSecretKey>())
                                      .ToDictionary(key => key.KeyId, value => value);
    }

    while (!(dataObject is PgpLiteralData) && dataObject != null)
    {
        try
        {
            var compressedData = dataObject as PgpCompressedData;
            var listedData = dataObject as PgpEncryptedDataList;

            //strip away the compression stream
            if (compressedData != null)
            {
                stream = compressedData.GetDataStream();
                layeredStreams.Add(stream);
                dataObjectFactory = new PgpObjectFactory(stream);
            }

            //strip the PgpEncryptedDataList
            if (listedData != null)
            {
                var encryptedDataList = listedData.GetEncryptedDataObjects()
                                                  .OfType<PgpPublicKeyEncryptedData>().First();
                var decryptionKey = secretKeys[encryptedDataList.KeyId]
                    .ExtractPrivateKey(privateKeyPassword.ToCharArray());
                stream = encryptedDataList.GetDataStream(decryptionKey);
                layeredStreams.Add(stream);
                dataObjectFactory = new PgpObjectFactory(stream);
            }

            dataObject = dataObjectFactory.NextPgpObject();
        }
        catch (Exception ex)
        {
            //Log exception here.
            throw new PgpException("Failed to strip encapsulating streams.", ex);
        }
    }

    foreach (var layeredStream in layeredStreams)
    {
        layeredStream.Close();
        layeredStream.Dispose();
    }

    if (dataObject == null) return null;

    var literalData = (PgpLiteralData)dataObject;
    var ms = new MemoryStream();
    using (var clearData = literalData.GetInputStream())
    {
        Streams.PipeAll(clearData, ms);
    }
    ms.Position = 0;
    return ms;
}
like image 143
Jeremy Holovacs Avatar answered Sep 28 '22 09:09

Jeremy Holovacs