WebCrypto 支持 RSA,但强制您为可以执行的不同操作选择填充方案。 RSA 可以执行签名/验证和加密/解密(+ key 包装),但这对于 webcrypto API 来说不太可能。
当我使用 RSA-OAEP 生成 RSA key 时,相同的 key 可用于 RSA-PSS,但是没有明确的方法来移动使用 webcrypto API 的 key 。
我最初的想法是采取以下步骤来转换 key :
- 使用
exportKey
和jwk
导出 key - 将
alg
从(例如)RSA-OAEP-512
“修复”为PS512
“修复”
key_ops
a.
加密
->验证
b.
解密
->签名
使用正确的算法导入“固定” key
当 key 不可导出时,该系统就会崩溃,我想这样做是为了私钥的额外安全性(恶意脚本、self-xss 等)。
在算法类型之间“转换”但保留 key 但无法导出的好方法是什么?
最佳答案
webcrypto API 不允许使用 key 进行签名和加密。可能是因为不推荐这种做法。请参阅此答案 https://crypto.stackexchange.com/a/12138/42888
it is safe, cryptographically speaking, to use the same RSA key pair for signature and encryption, provided that the key pair is used safely for signature and used safely for encryption.
However this is a bad idea for a different reason: key management. Signature keys and encryption keys have different requirements in terms of backups, access control, repudiation, etc. The fallback for a signature key in case of a catastrophic event is to destroy it to avoid future forgeries, so a signature key does not need to be backed up extensively. Conversely, the fallback for an encryption key is to keep it around to decrypt existing documents, so it needs to be backed up reliably.
要回答您的问题,您可以:
- 生成可提取的 RSA key ,
- 导出到pcks8或jwk
- 将其导入两次,作为不可提取的:key1->
RSA-OAEP
、key2->RSA-PSS
- 销毁原始且可提取的 key
然后,生成的 key 是相等且不可提取的,并且原始 key 仅在浏览器内存中进行管理。
由于您将在浏览器中获得两个 key ,因此如果公钥的接收者不同,请考虑生成不同的 key 用于签名和加密
关于security - Web加密 : Safe conversion from RSA-OAEP to RSA-PSS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47747630/