从 WebCrypto 生成的 CryptoKey(私有)中提取私钥缓冲区

发布于 2025-01-16 19:25:46 字数 833 浏览 2 评论 0原文

我可能已经知道答案(即不可能),但希望我忽略了一些东西,因为否则 WebCrypto 实现确实是令人难以置信的垃圾。

基本上问题是这样的:
我需要使用 WebCrypto 导入/导出 ECDH (P-256) 私钥 - 我并不是指愚蠢的高级 PKCS8 (PEM) 格式,它还编码一堆其他数据(例如版本、公钥)等),我的意思是纯密钥,作为 32 字节缓冲区。使用公钥确实很容易做到这一点,我只需将格式设置为“原始”并获取(或导入)64 字节缓冲区公钥。然而,这不适用于私钥(不支持“原始”格式),相反,我必须将密钥导出/导入为 PKCS8,然后根据其 ASN.1 架构解析密钥(实际上是两次 - 私钥)密钥缓冲区以 rfc5915 结构编码,该结构又在 pkcs8 结构内编码)。

这是一个极其痛苦的过程,涉及 ASN.1 实现库(或者上帝禁止内部实现)和适当的 ASN.1 结构(模板)的定义。我所需要的只是一种简单地从生成的 CryptoKey 中提取 32 字节私钥缓冲区的方法,反之亦然(将 32 字节私钥缓冲区导入到 CryptoKey 中)。

我希望有一种简单的方法可以仅使用我忽略的 WebCrypto 来做到这一点(因为我看不到为什么 CryptoKey 支持公钥的原始格式而不是私有密钥的单一合理的安全原因 - 毕竟,您可以导出它我能想到的唯一原因是,WebCrypto 是由一个虐待狂设计的,他的目的是折磨开发人员)。
如果没有,那么如果您能向我指出一些为 JS 或更好的 TS 编写的优秀、可用的 ASN.1 库,我将非常感激。到目前为止,我尝试了至少 10 个不同的 ASN.1 库,只有一个可以工作(至少有一些文档),只能在 Node 中工作,而不能在浏览器中工作,这有点毫无意义。

如果您之前必须处理这样的问题,感谢您提供任何指示和/或建议......

I probably already know the answer (ie. not possible), but hopefully I overlooked something, because otherwise the WebCrypto implementation is truly unbelievably crap..

Basically the problem is this:
I need to import/export ECDH (P-256) private keys using WebCrypto - and I don't mean the stupid high-level PKCS8 (PEM) format, that encodes a bunch of other data as well (such as version, public key, etc.), I mean a pure key, as 32-byte buffer. This is really easy to do with the public key, I only set the format to 'raw' and get (or import) 64-byte buffer public key. This however does not work for private key (which doesn't support the 'raw' format), instead I have to export/import the key as PKCS8, then parse the key according to its ASN.1 schema (twice actually - the private key buffer encoded in rfc5915 structure, which is in turn encoded within the pkcs8 structure).

This is an extremely painful process that involves ASN.1 implementation libraries (or god forbid an in-house impl.) and the definitions of appropriate ASN.1 structures (templates). All I need is a way to simply extract a 32-byte private key buffer from generated CryptoKey and vice versa (import 32-byte private key buffer into a CryptoKey).

I'm hoping there's a simple way to do so using only WebCrypto that I overlooked (since I can't see a single rational, security reason why CryptoKey supports the raw format for public keys and not private - after all, you can export it as pkcs8. The only reason I could think of is, that WebCrypto was designed by a sadist with a thing for torturing developers).
If not, then I'd really appreciate if you'd point me to some good, working ASN.1 library written for JS, or even better, TS. So far I tried at least 10 different ASN.1 libraries and only one that worked (with at least some documentation), worked only in Node and not browser, which is kinda pointless.

Thank you for any pointers and/or advice if you had to deal with issue like this before...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文