Update all remaining Files app's private APIs to work on entries.
[chromium-blink-merge.git] / chrome / renderer / resources / extensions / platform_keys_custom_bindings.js
blob924e60eab81cda87f44cb3e51b549b7999832ed7
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 // Custom binding for the platformKeys API.
7 var binding = require('binding').Binding.create('platformKeys');
8 var SubtleCrypto = require('platformKeys.SubtleCrypto').SubtleCrypto;
9 var getPublicKey = require('platformKeys.getPublicKey').getPublicKey;
10 var internalAPI = require('platformKeys.internalAPI');
12 var keyModule = require('platformKeys.Key');
13 var Key = keyModule.Key;
14 var KeyType = keyModule.KeyType;
15 var KeyUsage = keyModule.KeyUsage;
17 function createPublicKey(publicKeySpki, algorithm) {
18   return new Key(KeyType.public, publicKeySpki, algorithm, [KeyUsage.verify],
19                  true /* extractable */);
22 function createPrivateKey(publicKeySpki, algorithm) {
23   return new Key(KeyType.private, publicKeySpki, algorithm, [KeyUsage.sign],
24                  false /* not extractable */);
27 binding.registerCustomHook(function(api) {
28   var apiFunctions = api.apiFunctions;
29   var subtleCrypto = new SubtleCrypto('' /* tokenId */);
31   apiFunctions.setHandleRequest(
32       'selectClientCertificates', function(details, callback) {
33         internalAPI.selectClientCertificates(details, function(matches) {
34           if (chrome.runtime.lastError) {
35             callback([]);
36             return;
37           }
38           callback($Array.map(matches, function(match) {
39             // internalAPI.selectClientCertificates returns publicExponent as
40             // ArrayBuffer, but it should be a Uint8Array.
41             if (match.keyAlgorithm.publicExponent) {
42               match.keyAlgorithm.publicExponent =
43                   new Uint8Array(match.keyAlgorithm.publicExponent);
44             }
45             return match;
46           }));
47         });
48       });
50   apiFunctions.setHandleRequest(
51       'subtleCrypto', function() { return subtleCrypto });
53   apiFunctions.setHandleRequest(
54       'getKeyPair', function(cert, params, callback) {
55         getPublicKey(cert, params, function(publicKey, algorithm) {
56           if (chrome.runtime.lastError) {
57             callback();
58             return;
59           }
60           callback(createPublicKey(publicKey, algorithm),
61                    createPrivateKey(publicKey, algorithm));
62         });
63       });
64 });
66 exports.binding = binding.generate();