简体   繁体   English

以ASN.1编码的解码时间戳(javascript)

[英]Decoding timestamp encoded in ASN.1 (javascript)

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! 我正在尝试解码从HTTP发布请求中收到的时间戳,但这是一个非常复杂的任务,我什至对ASN.1 / RFC 3161都没有任何适当的了解,因此,如果有人愿意帮助我出来,我真的会很高兴!

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 ). 我正在尝试使用asn1 npm软件包( https://www.npmjs.com/package/asn1 )。

This is the response I get when I run the code: 这是我运行代码时得到的响应: 在此处输入图片说明

And if I change my await response.arrayBuffer() to await response.text() I get this: 如果我将我的await response.arrayBuffer()更改为await response.text()我会得到:

在此处输入图片说明

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! 我真的不知道如何解决这个问题,我尝试了很多不同的方法,但是似乎没有任何效果,如果有人能指出我正确的方向,那将是很棒的!

It looks like the ASN.1 schema is available from the RFC too (not surprising I guess). 看起来ASN.1模式也可以从RFC获得(我想不足为奇)。 It's given in Appendix C (see here ). 它在附录C中给出(请参阅此处 )。

Using that and an ASN.1 compiler you can produce source code that can decode the response. 使用该代码和ASN.1编译器,您可以生成可以解码响应的源代码。 There's some free ASN1 compilers targeting C/C++, I don't know about JavaScript ones. 有一些针对C / C ++的免费ASN1编译器,我不了解JavaScript。 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. 如果您感觉很勇敢,可以尝试使用代码,它将为您提供C / C ++源代码,然后您可以将其编译为Web程序集,然后可以从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). 从github页面来看,来自npm的程序包似乎有一些问题(ASN.1相当复杂)。 It maybe just a bit broken. 可能有点破。

To get the hang of it, you might like to try the ASN.1 Playground . 要掌握它,您可能想尝试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. 给它一个架构,进行编译,上传您作为响应获得的一些数据,对其进行解码,看看它是否大致符合您的期望。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM