Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Attempt to invoke virtual method 'org.bouncycastle.math.ec.ECCurve org.bouncycastle.asn1.x9.X9ECParameters.getCurve()' on a null object reference #425

Closed
ghost opened this issue Nov 4, 2018 · 1 comment

Comments

@ghost
Copy link

ghost commented Nov 4, 2018

Not sure if I'm doing something wrong, or if it's a bug.

I have an Ed25519 public key, that I have loaded from a byte array. Using the below encrypt(...) method to encrypt a byte array, but this exception gets thrown: Attempt to invoke virtual method 'org.bouncycastle.math.ec.ECCurve org.bouncycastle.asn1.x9.X9ECParameters.getCurve()' on a null object reference. Works fine using RSA keys.

Using 1.60.

final class PublicKey {

    public  PublicKey(PGPPublicKey publicKey) {

        this.publicKey = publicKey;
    }

    PGPPublicKey publicKey;

    static PublicKey fromBytes(byte[] input) throws PGPException, IOException {

        InputStream inputStream = new ByteArrayInputStream(input);
        JcaPGPPublicKeyRingCollection keyRings;

        try {

            keyRings = new JcaPGPPublicKeyRingCollection(inputStream);
        }

        finally {

            inputStream.close();
        }

        Iterator<PGPPublicKeyRing> keyRingsIterator = keyRings.getKeyRings();

        while (keyRingsIterator.hasNext())
        {
            PGPPublicKeyRing keyRing = keyRingsIterator.next();
            Iterator<PGPPublicKey> publicKeysIterator = keyRing.getPublicKeys();

            while (publicKeysIterator.hasNext())
            {
                PGPPublicKey publicKey = publicKeysIterator.next();

                if (publicKey.isEncryptionKey())
                    return new PublicKey(publicKey);
            }
        }

        throw new IllegalArgumentException();
    }

    public byte[] encrypt( final byte[] message)
            throws IOException, PGPException
    {
        final ByteArrayInputStream in = new ByteArrayInputStream( message );
        final ByteArrayOutputStream bOut = new ByteArrayOutputStream();
        final PGPLiteralDataGenerator literal = new PGPLiteralDataGenerator();
        final OutputStream pOut = literal.open(bOut, PGPLiteralData.BINARY, "filename", in.available(), new Date() );
        Streams.pipeAll( in, pOut );
        final byte[] bytes = bOut.toByteArray();
        final PGPEncryptedDataGenerator generator = new PGPEncryptedDataGenerator(
                new JcePGPDataEncryptorBuilder( SymmetricKeyAlgorithmTags.AES_256 ).setWithIntegrityPacket( true )
                        .setSecureRandom(
                                new SecureRandom() )

                        .setProvider(Utils.bouncyCastleProvider ) );
        generator.addMethod( new JcePublicKeyKeyEncryptionMethodGenerator( this.publicKey ).setProvider( Utils.bouncyCastleProvider ) );
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        OutputStream cOut = generator.open( out, bytes.length );
        cOut.write( bytes );
        cOut.close();
        out.close();
        return out.toByteArray();
    }
}

Stack trace:

org.bouncycastle.jcajce.provider.asymmetric.util.EC5Util.getCurve(Unknown Source:56), org.bouncycastle.jcajce.provider.asymmetric.ec.AlgorithmParametersSpi.engineInit(Unknown Source:12), org.bouncycastle.jcajce.provider.asymmetric.ec.AlgorithmParametersSpi.engineInit(Unknown Source:2), java.security.AlgorithmParameters.init(AlgorithmParameters.java:382), org.bouncycastle.openpgp.operator.jcajce.JcePublicKeyKeyEncryptionMethodGenerator.encryptSessionInfo(Unknown Source:48), org.bouncycastle.openpgp.operator.PublicKeyKeyEncryptionMethodGenerator.generate(Unknown Source:16), org.bouncycastle.openpgp.PGPEncryptedDataGenerator.open(Unknown Source:120), org.bouncycastle.openpgp.PGPEncryptedDataGenerator.open(Unknown Source:1)

Kind regards, Emil

@bcgit
Copy link
Collaborator

bcgit commented Jan 28, 2019

This was a bit complicated, GPG has started supporting Curve25519 but the private value is reversed (relative to the Java view of the world). I've adjusted the PGP API to take this into account so it should now be possible to process messages encrypted using Curve25519. It's in the current beta at https://www.bouncycastle.org/betas 161b25 or later.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

0 participants