php - 使用 ECDSA P-256 SHA-256 使用 PHP 创建 JWT

标签 php cryptography jwt digital-signature xbox-one

我想知道是否可以使用 P256 曲线和 PHP 正确创建签名。 PHP 中的 OpenSSL 支持创建 key 并按顺序获取正确的内容。

根据本文档 - http://self-issued.info/docs/draft-jones-json-web-token-01.html#DefiningECDSA - 第 8.3 节规定:

A JWT is signed with an ECDSA P-256 SHA-256 signature as follows:

  1. Generate a digital signature of the UTF-8 representation of the JWT Signing Input using ECDSA P-256 SHA-256 with the desired private key. The output will be the EC point (R, S), where R and S are unsigned integers.
  2. Turn R and S into byte arrays in big endian order. Each array will be 32 bytes long.
  3. Concatenate the two byte arrays in the order R and then S.
  4. Base64url encode the 64 byte array as defined in this specification.

这里的问题在于获取 R 和 S 字节数组。

这是我正在尝试做的事情的示例。

//Create Array Configuration
$config = array(
    "curve_name" => "prime256v1",
    "private_key_type" => OPENSSL_KEYTYPE_EC,
);
$ourkey = openssl_pkey_new($config);

/* We would get the key details to help extract out other information such as x/y coord 
of curve and private key but it's not necessary to show for this part*/

// Extract the private key from $res to $privKey
openssl_pkey_export($ourkey, $privKey);

$data = "Example data we will be using to sign";
$data = hash("sha256", $data);

$signature = "";
openssl_sign($data, $signature, $privKey); // Should I include the digest algo in this call as well?

这里的问题是这个签名不是我可以用来连接在一起以形成我需要的真正签名的 R 和 S...我认为。

最终,有什么方法可以从 php 中的 openssl 函数获取 R 和 S 值吗?

非常感谢任何帮助!

最佳答案

如果您需要 JWT 之类的东西,但不是特别需要 JWT,请考虑 PASETO .

假设您选择 v2,PASETO 不使用 ECDSA,它使用比 Ed25519 更安全的 EdDSA。此外,该标准不遗余力地成为 boring这样实现显然是安全的。

(如果您选择 v1,PASETO 使用 RSASSA-PSS。)

如果您确实特别需要 JWT,那么 lcobucci/jwt 就是您的最佳选择。 ,依次使用 PHPECC用于 ECDSA。

曾几何时,PHPECC 充满了侧 channel 漏洞,这使其成为在生产系统中使用的糟糕选择。如今,它的安全性与 ECDSA 的 PHP 实现一样安全。我编写了一个名为 easy-ecc 的可用性包装器如果有人只想在最安全的配置中使用 PHPECC。

据我所知,Luís Cobucci 的 JWT 库是唯一经过安全公司正式审核的 JWT PHP 实现。报告公开并生效here .

关于php - 使用 ECDSA P-256 SHA-256 使用 PHP 创建 JWT,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52214433/

相关文章:

javascript - 没有图像验证的谷歌验证码

PHP 和 HTML : Encrypt form

c++ - 私钥解密代码使用公共(public)接口(interface)说明符到 BSAFE 库 v6.0?

go - Amazon Cognito 的 JWK 不包含 X5c 字段

php - 一个查询中的多个 SQL 插入

javascript - 在没有 LMS 的情况下使用 SCORM 运行时 API?

java - 两个设备之间的安全 RSA 交换

c# - 在 .NET Core 中使用 JWT token 或 API key 的授权机制

authentication - JWT 的正确工作流程是什么?

php - 需要关系数的意见