如何通过 shell 使用 openssl 库从 JWKS 中提取公钥

发布时间:2021-02-26 18:14

我从端点获取 JWKS,它看起来像这样:

{
  "keys": [
    {
      "kty": "RSA",
      "use": "sig",
      "x5t": "M2maFm3VYlMBOn3GetVWGXkrKrk",
      "kid": "SIGNING_KEY",
      "x5c": "MIIC………(base64 encoded cert)………..tow==",
      "alg": "RS256"
    }
  ]
}

我正在尝试使用 shell 和 .pub

将此 x5c 值转换为公钥 (openssl) 文件

我尝试复制粘贴上述 json 中的 x5c 值并通过以下方式添加到 .pem 文件中:

vi certificate.pem
fold -w 64 certificate.pem

然后在 certificate.pem 文件中添加以下内容

-----BEGIN CERTIFICATE-----
<value>
-----END CERTIFICATE-----

在此之后,我尝试运行以下命令来获取公钥:

openssl x509 -pubkey -inform pem -in certificate.pem -noout > key.pub

但是得到了类似这样的编码错误:

unable to load certificate
140735207381436:error:0D0680A8:asn1 encoding routines:ASN1_CHECK_TLEN:wrong tag:tasn_dec.c:1319:
140735207381436:error:0D07803A:asn1 encoding routines:ASN1_ITEM_EX_D2I:nested asn1 error:tasn_dec.c:381:Type=X509_CINF
140735207381436:error:0D08303A:asn1 encoding routines:ASN1_TEMPLATE_NOEXP_D2I:nested asn1 error:tasn_dec.c:751:Field=cert_info, Type=X509
140735207381436:error:0906700D:PEM routines:PEM_ASN1_read_bio:ASN1 lib:pem_oth.c:83:

我搞砸了什么?谢谢

回答1