如何在 Delphi 中将 JSON Web Key 转换为 PEM 格式?

问题描述

Earlier I found out 在使用 JWT Library 中的签名之前,我需要将 JSON Web Key (JWK) 转换为 PEM 格式。

JWK 格式的原始私钥:

{
  "kty": "EC","d": "Rwyv99W3GnfjYbI0X-b5Umhvh88oRCKQkPxiwCPVGgg","crv": "P-256","x": "sDbcYT8HzBk1tUl849ZHrhpIn8ZV7HfD1DwYdsP1ip0","y": "EWodfKWQ6oE0ppyi7tRO_61BgAQsZyDjDGj9kLZiUts"
}

需要获取 PEM 格式,如下所示:

-----BEGIN EC PRIVATE KEY-----
MHcCAQEEIEcMr/fVtxp342GyNF/m+VJob4fPKEQikJD8YsAj1RoIoAoGCCqGSM49
AwEHoUQDQgAEsDbcYT8HzBk1tUl849ZHrhpIn8ZV7HfD1DwYdsP1ip0Rah18pZDq
gTSmnKLu1E7/rUGABCxnIOMMaP2QtmJS2w==
-----END EC PRIVATE KEY-----

一个 online converter 可以满足我的需求。是否可以在 Delphi 中进行相同的转换?

解决方法

您将拥有低级 OpenSSL 所需的一切。

它的 API 有点神秘,但您可以使用 EC_POINT*() 函数来实现它。

检查我们做了什么 mormot.crypt.openssl 使用低级 ECC 私钥并将其与 OpenSSL 集成:

  • ecdsa_sign_osl 获取原始私钥并将其转换为 OpenSSL PEC_KEY;
  • OpenSslSaveKeys 将此密钥保存为 PEM。

您只需要导出 "d": "Rwyv99W3GnfjYbI0X-b5Umhvh88oRCKQkPxiwCPVGgg" 参数。它似乎与 TEccPrivateKey 中用作输入参数的 ecdsa_sign_osl() 布局相同。

您可能还会在 mormot.crypt.ecc256r1.pas 中找到一些纯 pascal 代码计算 ECC prime256v1。

,

找到了解决方案。阅读详情here

简化示例:

uses
  JSON,EncdDecd;

function Base64urlToBase64(Base64urlStr: String): String;
begin
  Result := StringReplace(Base64urlStr,'_','/',[rfReplaceAll]);
  Result := StringReplace(Result,'-','+',[rfReplaceAll]);
end;

function JwkToPem(JWK: TJSONObject): String;
var
  BinKey: TBytes;
begin
  BinKey :=

  [$30] + // ASN.1
  [$77] + // Length of all following bytes (119 bytes)

  [$02] + // Type (integer)
  [$01] + // Length of integer (1 byte)
  [$01] + // Value of integer (1)

  [$04] + // Type (octet string)
  [$20] + // Length of string (32 bytes)
  DecodeBase64(Base64urlToBase64(JWK.Get('d').JsonValue.Value)) +  // Private Key

  [$A0] + // Tag 0
  [$0A] + // Length of tag (10 bytes)
  [$06] + // Type (Object ID)
  [$08] + // Length of the Object ID (8 bytes)
  [$2A,$86,$48,$CE,$3D,$03,$01,$07] + // - The object ID of the curve prime256v1

  [$A1] + // Tag 1
  [$44] + // Length of tag (68 bytes)
  [$03] + // Type – Bit string
  [$42] + // Length of the bit string (66 bytes)
  [$00] + // ???
  [$04] + // Uncompressed Public Key
  DecodeBase64(Base64urlToBase64(JWK.Get('x').JsonValue.Value))+ // Public Key X coord
  DecodeBase64(Base64urlToBase64(JWK.Get('y').JsonValue.Value)); // Public Key Y coord

  Result :=
  '-----BEGIN EC PRIVATE KEY-----'+#13#10+
  EncodeBase64(Pointer(BinKey),Length(BinKey))+#13#10+
  '-----END EC PRIVATE KEY-----';
end;