Skip to content

Failing to decrypt messages in binary format when using expectSigned: true #1803

@dnechay

Description

@dnechay
  • OpenPGP.js version: 5.11.2
  • Affected platform (Browser or Node.js version): Probably both, found in Node.js

Tried to decrypt a message (no matter what is the original content - binary or text) using expectSigned: true and desired return format: 'binary' always throws on .decrypt operation

/path_to_project/node_modules/openpgp/node_modules/@openpgp/web-stream-tools/lib/util.js:53
      throw new Error('concatUint8Array: Data must be in the form of a Uint8Array');
                ^
Error: Error decrypting message: concatUint8Array: Data must be in the form of a Uint8Array
    at concatUint8Array (/path_to_project/node_modules/openpgp/node_modules/@openpgp/web-stream-tools/lib/util.js:53:17)
    at join (/path_to_project/node_modules/openpgp/node_modules/@openpgp/web-stream-tools/lib/streams.js:93:10)
    at v.data [as readToEnd] (/path_to_project/node_modules/openpgp/node_modules/@openpgp/web-stream-tools/lib/writer.js:35:18)
    at async Object.exports.decrypt (/path_to_project/node_modules/openpgp/src/openpgp.js:366:12)
    at async libraryExample (/path_to_project/example/enrypt.ts:107:31)
    at async /path_to_project/example/enrypt.ts:139:3

Sample code (taken from example)

import * as openpgp from 'openpgp';

const ExampleKeys = {
  private: `-----BEGIN PGP PRIVATE KEY BLOCK-----

xVgEZyOGRhYJKwYBBAHaRw8BAQdA6soD3CBjRnPfsYIxOGObykL8X8y+dZlf
IzAI7mk40E4AAQCPRLKy/1n/QxTRk/Ql+Dt2VYADqDmczBxhWELCbz9Wvw/J
zRxleGFtcGxlIDxzaW1wbGVAZXhhbXBsZS5jb20+wowEEBYKAD4FgmcjhkYE
CwkHCAmQZ4KBmVeekE0DFQgKBBYAAgECGQECmwMCHgEWIQQkzuPtNIUkzz4M
CFhngoGZV56QTQAA89oBAKm5Tai1Ynx4BCU6PSLp0QMEE2ImV/LzwHkLMz52
mzeJAP9NyyNyIMNH1SpSezy309UhEdJVapGoXQwO1eQR4B+LCMddBGcjhkYS
CisGAQQBl1UBBQEBB0BykPL7zxjKQpZMYuEnIDBz7vshngm4zJMNOsE6pDSH
NgMBCAcAAP9b+n3/5QKVd0UP/ow3uyH0X44gc2U4fKV8IhZBJLiSEA9ZwngE
GBYKACoFgmcjhkYJkGeCgZlXnpBNApsMFiEEJM7j7TSFJM8+DAhYZ4KBmVee
kE0AAE0WAP9FE0I9dHToxLAkMKiM9tTzL43GVl6K8Lvn9nrLJcfLgQEAtFXL
39GhAUKNbHMpdeEmxukrdF+rXzUfvOsYGPLIgwI=
=GfVY
-----END PGP PRIVATE KEY BLOCK-----`,
  public: `-----BEGIN PGP PUBLIC KEY BLOCK-----

xjMEZyOGRhYJKwYBBAHaRw8BAQdA6soD3CBjRnPfsYIxOGObykL8X8y+dZlf
IzAI7mk40E7NHGV4YW1wbGUgPHNpbXBsZUBleGFtcGxlLmNvbT7CjAQQFgoA
PgWCZyOGRgQLCQcICZBngoGZV56QTQMVCAoEFgACAQIZAQKbAwIeARYhBCTO
4+00hSTPPgwIWGeCgZlXnpBNAADz2gEAqblNqLVifHgEJTo9IunRAwQTYiZX
8vPAeQszPnabN4kA/03LI3Igw0fVKlJ7PLfT1SER0lVqkahdDA7V5BHgH4sI
zjgEZyOGRhIKKwYBBAGXVQEFAQEHQHKQ8vvPGMpClkxi4ScgMHPu+yGeCbjM
kw06wTqkNIc2AwEIB8J4BBgWCgAqBYJnI4ZGCZBngoGZV56QTQKbDBYhBCTO
4+00hSTPPgwIWGeCgZlXnpBNAABNFgD/RRNCPXR06MSwJDCojPbU8y+NxlZe
ivC75/Z6yyXHy4EBALRVy9/RoQFCjWxzKXXhJsbpK3Rfq181H7zrGBjyyIMC
=wH1v
-----END PGP PUBLIC KEY BLOCK-----`,
};

const libraryExample = async () => {
  // put keys in backtick (``) to avoid errors caused by spaces or tabs
  const publicKeyArmored = ExampleKeys.public;
  const privateKeyArmored = ExampleKeys.private; // encrypted private key

  const publicKey = await openpgp.readKey({ armoredKey: publicKeyArmored });

  const privateKey = await openpgp.readPrivateKey({
    armoredKey: privateKeyArmored,
  });

  const encrypted = await openpgp.encrypt({
    message: await openpgp.createMessage({
      text: 'Hello, World!',
    }), // input as Message object
    encryptionKeys: publicKey,
    signingKeys: privateKey, // optional
    format: 'armored',
  });
  console.log(encrypted); // '-----BEGIN PGP MESSAGE ... END PGP MESSAGE-----'

  const message = await openpgp.readMessage({
    armoredMessage: encrypted, // parse armored message
  });
  const { data: decrypted } = await openpgp.decrypt({
    message,
    verificationKeys: publicKey, // optional
    decryptionKeys: privateKey,
    expectSigned: true,
    format: 'binary',
  });
  console.log(decrypted); // 'Hello, World!'
};

(async () => {
  await libraryExample();
})();

Works fine if you use format: 'utf8' or disable expectSigned option.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions