Welcome to MLink Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
393 views
in Technique[技术] by (71.8m points)

How do I export a SubtleCrypto ECDSA private key with PKCS#8 in JavaScript?

I'm trying to export an ECDSA (this probably applies to ECDH as well) private key I've generated with SubtleCrypto in JavaScript. According to MDN (yes, this is for importKey, but this is where you are linked from the page for exportKey), I can use PKCS#8 for this purpose, and so I put together the following code snippet that seeks to implement a such export:

crypto.subtle.generateKey(
    {
        "name": "ECDSA",
        "namedCurve": "P-256"
    },
    true,
    ["sign", "verify"]
).then(keys => {
    crypto.subtle.exportKey("pkcs8", keys.privateKey)
        .then(exported_key => {
            console.log(exported_key)
        })
}

This works in Chromium and GNOME Web (WebKit). In Firefox, I get the following error logged to the console:

Uncaught (in promise) DOMException: Operation is not supported

This seems to come from the latter expression, i.e. the call to exportKey. Firefox support is important to me and what I'm working on, so I'd like to figure this out, and I imagine it is rather that I'm doing something wrong than a bug in Firefox, so I'm thinking this is better to figure out than just to say Firefox isn't supported regardless.

One might think this is due to Firefox not supporting exporting EC keys with PKSC#8, but looking at the compatibility table on MDN this seems to not be the case since it is marked as being supported from version 34 and onwards and there are no implementation notes. I digress.

What might I be doing wrong here, and how can I make it work?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to MLink Developer Q&A Community for programmer and developer-Open, Learning and Share
...