2

I am trying to decode the timestamp which I am receiving from my HTTP post request, but this is a really complex task, I do not even have any proper insight into ASN.1/RFC 3161 so if anyone out there is willing to help me out, I would really be stoked!

Code:

import { Action } from 'kawax-js';
import base64 from 'base-64';
var Ber = require('asn1').Ber;

class Timestamp extends Action {

 static type = 'TIMESTAMP';

 call = async (data) => {
  const authEncoded = base64.encode(username+":"+password);
  const formBody = Object.keys(data).map(key => 
  encodeURIComponent(key) + '=' + 
  encodeURIComponent(data[key])).join('&');
  const response = await 
  fetch("https://sign.test.cryptolog.com/tsa/post/", {
   method: 'POST',
   headers: {
    'Accept': 'application/x-www-form-urlencoded',
    'Content-Type': 'application/x-www-form-urlencoded',
    'Authorization': 'Basic ' + authEncoded,
    'Access-Control-Allow-Origin': '*'
   },
   body: formBody
  });
  const bodyData = await response.arrayBuffer();
  var reader = new Ber.Reader(Buffer.from(bodyData));
  if (reader.peek() === Ber.Boolean)
  console.log("",reader.readBoolean());
  return bodyData;
 }
}

export default Timestamp;

I am trying to use the asn1 npm package(https://www.npmjs.com/package/asn1).

This is the response I get when I run the code: enter image description here

And if I change my await response.arrayBuffer() to await response.text() I get this:

enter image description here

I don't really know how to approach this, I tried a lot of different stuff but nothing seems to work, if anyone could point me into right direction that would be great!

  • From your post it seems that the variant with `response.arrayBuffer()` is correctly decoding a part of the ASN.1 data (unfortunately this is not very clear since the code and the logs do not match in any way). In order to be able to parse the rest of the ASN.1 you will need to know the schema that was used when the data was encoded. An ASN.1 schema will give you a description for the structure of the encoded data, so that you know what fields are there and which ones are you interested in. – Andrei Bozantan Sep 28 '18 at 07:42
  • @AndreiBozantan, by the looks of it the encoding that has been used is BER, which is cannonical. That means that the data is self-describing, the schema is not needed. Something like this https://www.obj-sys.com/products/asn1ve/index.php ASN1 viewer should be able to read the data and tell you what it looks like, it'll even give you a schema file built from decoding the data! – bazza Oct 26 '18 at 19:58

1 Answers1

0

It looks like the ASN.1 schema is available from the RFC too (not surprising I guess). It's given in Appendix C (see here).

Using that and an ASN.1 compiler you can produce source code that can decode the response. There's some free ASN1 compilers targeting C/C++, I don't know about JavaScript ones. If you were feeling brave, you could try this one, which will give you C/C++ source code, which you might then might compile to a web assembly that you could then call from JavaScript. That feels, well, dirty, but it might just work.

That package from npm looks like it has some issues judging from its github page (ASN.1 is pretty complicated). It maybe just a bit broken.

To get the hang of it, you might like to try the ASN.1 Playground. Give it the schema, compile it, upload some of the data you're getting as a response, decode it and see if it's roughly what you're expecting.

bazza
  • 7,580
  • 15
  • 22