Search code examples
javascriptencryptionpkiasn.1ber

cms Enveloped Data binary encoding problem in pkijs


I'm trying trying to implement exchange of CMS encrypted content using the pkijs library. Basically I've just copied the example from here: https://pkijs.org/examples/CMSEnvelopedExample.html

here the excerpt for encryption

cmsEnveloped.addRecipientByCertificate(certSimpl, {  oaepHashAlgorithm: oaepHashAlg });

return cmsEnveloped.encrypt(encAlg, valueBuffer).then(() => {
  var cmsContentSimpl = new ContentInfo();
  cmsContentSimpl.contentType = "1.2.840.113549.1.7.3";
  cmsContentSimpl.content = cmsEnveloped.toSchema();
  cmsEnvelopedBuffer = cmsContentSimpl.toSchema().toBER(false);

The problematic part is the binary encoding of the asn.1 schema with .toBER(false). If the valueBuffer is not extremely small it is very slow ~13sek for 1 Mb. My question are: why is this so slow? and could I just encode it differently e.g. toJSON()and then stringToArrayBuffer? Are there any security concerns?


Solution

  • There was some unoptimized code in the implementation of toBER in asn1js. You should upgrade the dependency to [email protected] if you encounter the same problem. This is the issue on github: https://github.com/PeculiarVentures/PKI.js/issues/347