How to load a PKCS#12 Digital Certificate with Javascript WebCrypto API(如何使用 Javascript WebCrypto API 加载 PKCS#12 数字证书)
问题描述
I'm trying to sign data using the WebCrypto API, but instead of creating a private/public key and exporting it to pkcs#1 or 8, I would really like to use a user's PKCS#12 to sign data. I've read the W3C spec, but cannot make much of it and can't find any good material on how to do this. Right now I want to leave ActiveX and Java Applets aside. Is there a way to tweak the following:
Any pointers?
UPDATE Here's the code I've been working
Web cryptography api does not support PKCS # 12. You can use a third party library to decode the p12 as forge https://github.com/digitalbazaar/forge#pkcs12 and load privateKey in webcrypto
Reading the PKCS#12 certificate
PKCS#12 is stored in DER, so first read it from a File or use a pre-stored base64
Decode PKCS#12 with forge and extract private key
Then decode DER format to ASN1, and let forge reads the content
Then get the private key from pkcs12
of the desired certificate (see forge doc) and convert to PKCS # 8 to be imported with webcrypto
Convert to PKCS#8
Import key in Webcrypto
And finally import the key in webcrypto
Digital signature
With the imported cryptoKey returned from the above method you can sign with webcrypto.
I include coding from base64 because data conversions are not trivial
In pkc12 you also have the certification chain if you need to build advanced formats like AdES
这篇关于如何使用 Javascript WebCrypto API 加载 PKCS#12 数字证书的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!