async function pem2JWK()

in modules/integration-browser/src/decrypt_materials_manager_web_crypto.ts [138:179]


async function pem2JWK(keyInfo: RsaKeyInfo, { material, type }: RSAKey) {
  const OAEP_SHA1_MFG1 = 'RSA-OAEP'
  const OAEP_SHA256_MFG1 = 'RSA-OAEP-256'
  const OAEP_SHA384_MFG1 = 'RSA-OAEP-384'
  const OAEP_SHA512_MFG1 = 'RSA-OAEP-512'
  /* Browsers do not support PKCS1.
   * Leaving this here to make sure this is clear.
   * const RSASSA_PKCS1_V1_5_SHA1 = 'RSASSA-PKCS1-v1_5'
   */

  // @ts-ignore
  const jwk = keyto.from(material, 'pem').toJwk(type)

  const paddingAlgorithm = keyInfo['padding-algorithm']
  const paddingHash = keyInfo['padding-hash']
  if (paddingAlgorithm === 'oaep-mgf1') {
    jwk.alg =
      paddingHash === 'sha1'
        ? OAEP_SHA1_MFG1
        : paddingHash === 'sha256'
        ? OAEP_SHA256_MFG1
        : paddingHash === 'sha384'
        ? OAEP_SHA384_MFG1
        : paddingHash === 'sha512'
        ? OAEP_SHA512_MFG1
        : false
  } else if (paddingAlgorithm === 'pkcs1') {
    throw new Error('Unsupported right now')
  }

  if (type === 'public') {
    const publicKey = await RawRsaKeyringWebCrypto.importPublicKey(jwk)
    return { publicKey }
  }

  if (type === 'private') {
    const privateKey = await RawRsaKeyringWebCrypto.importPrivateKey(jwk)
    return { privateKey }
  }

  throw new Error('Unknown type')
}