how to import ECDH public key (Cannot create a key using the specified key usages)

Viewed 1074

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)
  })

0 Answers
Related