在NodeJS中使用ASN.1公钥

huangapple go评论80阅读模式
英文:

Using ASN.1 public key in NodeJS

问题

I am trying to communicate with a web service. In order to do that, I need to encrypt a message using the public key that I received from the web service. The doc says the following about the public key format:

Format: X.509 encoded key in ASN.1 (sic!)

(ANS.1 is ASN.1 I guess).
The public key is:

-----BEGIN CERTIFICATE-----MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDE+ApyETIF1cXzKnU144P6lg/FcilmuQS2wBvaWp6t9OovthGmrsszd7eo4rL6Nitj1YOKETTtnwm4T+1EEyBrgwcfXAlm3FasTC/HIzhRRa+F8Yuz+UZkGvgP8Qa6B0vRob2BjhWx1PfwuWHQxGvAjiqUJ/dEMjocFuCrY5NZqwIDAQAB-----END CERTIFICATE-----

I tried to use this is a NodeJS code with the following:

const key = crypto.createPublicKey({
    key: Buffer.from(publicKey),
    format: 'der',
    type: 'pkcs1'
});

But I received the following error:

node:internal/crypto/keys:607
    handle.init(kKeyTypePublic, data, format, type, passphrase);
           ^
    
Error: error:0D0680A8:asn1 encoding routines:asn1_check_tlen:wrong tag
    at Object.createPublicKey (node:internal/crypto/keys:607:12)
    at Object.<anonymous> (/XXXXXXXX/wsClient.js:16:20)
    ...

I can't even convert this public key using openssl into any usable format. The only way to see inside it for me was to use the following online tool:
https://lapo.it/asn1js/

Here I can at least see that the public key is valid, but I don't know how to use it in NodeJS. Converting it is also an acceptable solution for me.

英文:

I am trying to communicate with a web service. In order to do that, I need to encrypt a message using the public key that I received from the web service. The doc says the following about the public key format:

Format: X.509 encoded key in ANS.1 (sic!)

(ANS.1 is ASN.1 I guess).
The public key is:

-----BEGIN CERTIFICATE-----MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDE+ApyETIF1cXzKnU144P6lg/FcilmuQS2wBvaWp6t9OovthGmrsszd7eo4rL6Nitj1YOKETTtnwm4T+1EEyBrgwcfXAlm3FasTC/HIzhRRa+F8Yuz+UZkGvgP8Qa6B0vRob2BjhWx1PfwuWHQxGvAjiqUJ/dEMjocFuCrY5NZqwIDAQAB-----END CERTIFICATE-----

I tried to use this is a NodeJS code with the following:

const key = crypto.createPublicKey({
    key: Buffer.from(publicKey),
    format: &#39;der&#39;,
    type: &#39;pkcs1&#39;
});

But I received the following error:

node:internal/crypto/keys:607
    handle.init(kKeyTypePublic, data, format, type, passphrase);
           ^

Error: error:0D0680A8:asn1 encoding routines:asn1_check_tlen:wrong tag
    at Object.createPublicKey (node:internal/crypto/keys:607:12)
    at Object.&lt;anonymous&gt; (/XXXXXXXX/wsClient.js:16:20)
    at Module._compile (node:internal/modules/cjs/loader:1149:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1203:10)
    at Module.load (node:internal/modules/cjs/loader:1027:32)
    at Module._load (node:internal/modules/cjs/loader:868:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)
    at node:internal/main/run_main_module:23:47 {
  opensslErrorStack: [
    &#39;error:0D09B00D:asn1 encoding routines:d2i_PublicKey:ASN1 lib&#39;,
    &#39;error:0D07803A:asn1 encoding routines:asn1_item_embed_d2i:nested asn1 error&#39;
  ],
  library: &#39;asn1 encoding routines&#39;,
  function: &#39;asn1_check_tlen&#39;,
  reason: &#39;wrong tag&#39;,
  code: &#39;ERR_OSSL_ASN1_WRONG_TAG&#39;
}

I can't even convert this public key using openssl into any usable format. The only way to see inside it for me was to use the following online tool:
https://lapo.it/asn1js/

Here I can at least see that the public key is valid, but I don't know how to use it in NodeJS. Converting it is also an accaptable solution for me.

答案1

得分: 1

在经历了一些痛苦的几个小时后,事实证明必须进行两项操作:

  1. 将“BEGIN CERTIFICATE”替换为“BEGIN PUBLIC KEY”,同样的操作也需要对结束部分进行替换。
  2. 它们还需要位于单独的一行。

之后,NodeJS Crypto能够解析这个密钥。
有趣的是,phpseclib能够解析原始格式的密钥,然后将其输出为正确的格式,这就是我发现的解决方法。

英文:

After some painful hours it turns out that two things had to be done:

  1. replace "BEGIN CERTIFICATE" with "BEGIN PUBLIC KEY" and the same for the end
  2. They also needed to be in a separate line

After that NodeJS Crypto is able to parse the key.
Interestingly phpseclib was able to parse the key in the original format and then output it in the correct one, that's how I realized the solution.

huangapple
  • 本文由 发表于 2023年1月5日 19:56:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/75018022.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定