1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 var internalAPI = require('platformKeys.internalAPI');
7 var normalizeAlgorithm =
8 requireNative('platform_keys_natives').NormalizeAlgorithm;
10 // Returns the normalized parameters of |importParams|.
11 // Any unknown parameters will be ignored.
12 function normalizeImportParams(importParams) {
13 if (!importParams.name ||
14 Object.prototype.toString.call(importParams.name) != '[object String]') {
15 throw new Error('Algorithm: name: Missing or not a String');
18 var filteredParams = { name: importParams.name };
20 var hashIsNone = false;
21 if (importParams.hash) {
22 if (importParams.hash.name.toLowerCase() === 'none') {
24 // Temporarily replace |hash| by a valid WebCrypto Hash for normalization.
25 // This will be reverted to 'none' after normalization.
26 filteredParams.hash = { name: 'SHA-1' };
28 filteredParams.hash = { name: importParams.hash.name }
32 // Apply WebCrypto's algorithm normalization.
33 var resultParams = normalizeAlgorithm(filteredParams, 'ImportKey');
35 throw new Error('A required parameter was missing or out-of-range');
38 resultParams.hash = { name: 'none' };
43 function combineAlgorithms(algorithm, importParams) {
44 // internalAPI.getPublicKey returns publicExponent as ArrayBuffer, but it
45 // should be a Uint8Array.
46 if (algorithm.publicExponent) {
47 algorithm.publicExponent = new Uint8Array(algorithm.publicExponent);
50 algorithm.hash = importParams.hash;
54 function getPublicKey(cert, importParams, callback) {
55 importParams = normalizeImportParams(importParams);
56 internalAPI.getPublicKey(
57 cert, importParams.name, function(publicKey, algorithm) {
58 if (chrome.runtime.lastError) {
62 var combinedAlgorithm = combineAlgorithms(algorithm, importParams);
63 callback(publicKey, combinedAlgorithm);
67 exports.getPublicKey = getPublicKey;