6

I am trying to run code with webcrypto but cannot seem to import ECDH public key. What am I missing ?

I get this error: cannot create a key using the specified key usages.

Browser: Google Chrome Version 71.0.3578.98 (Official Build) (64-bit)

(Works fine on Firefox).

window.crypto.subtle
  .generateKey(
    {
      name: 'ECDH',
      namedCurve: 'P-256',
    },
    true,
    ['deriveKey', 'deriveBits']
  )
  .then(function(key) {
    return window.crypto.subtle
      .exportKey('raw', key.publicKey)
      .then(function(ecdhPub) {
        return window.crypto.subtle
          .importKey(
            'raw',
            ecdhPub,
            {
              name: 'ECDH',
              namedCurve: 'P-256',
            },
            false,
            ['deriveKey', 'deriveBits']
          )
          .then(function(ecdhPubKey) {
            console.log('DONE !!', ecdhPubKey)
          })
          .catch(function(err) {
            console.log('COULD NOT IMPORT...')
            console.error(err)
          })
      })
      .catch(function(err) {
        console.log('COULD NOT EXPORT...')
        console.error(err)
      })
  })
  .catch(function(err) {
    console.log('COULD NOT GENERATE KEYS...')
    console.error(err)
  })
Lily B
  • 4,973
  • 4
  • 34
  • 49
  • 7
    The problem is with public key import: you have to set usages to `[]` in Google Chrome. – Lily B Jan 14 '19 at 14:07
  • Do you have any idea why this is the case? Wish I had the last hour of my life back... Thinking this through, you are only recovering the publicKey of the sharedKey. Since this key cannot deriveKey / deriveBits (the only uses of the privateKey), then I guess it would make sense to force the usages to []. Although, having it "sometimes" work when exporting and encoding keys is a terrible experience. – mattdlockyer Jun 09 '19 at 04:25

0 Answers0