Decoding timestamp encoded in ASN.1 (javascript)

1.8k views Asked by At

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!

1

There are 1 answers

0
bazza On

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.