Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(646)

Side by Side Diff: chrome/renderer/resources/extensions/enterprise_platform_keys/subtle_crypto.js

Issue 214863002: Extension API enterprise.platformKeys. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Removed 'location: policy' comment. Created 6 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright 2014 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.
4
5 var utils = require('utils');
6 var internalAPI = require('enterprise.platformKeys.internalAPI');
7 var intersect = require('enterprise.platformKeys.utils').intersect;
8 var KeyPair = require('enterprise.platformKeys.KeyPair').KeyPair;
9 var keyModule = require('enterprise.platformKeys.Key');
10 var Key = keyModule.Key;
11 var getSpki = keyModule.getSpki;
12 var KeyUsage = keyModule.KeyUsage;
13
14 // The following errors are specified in WebCrypto.
15 // TODO(pneubeck): These should be DOMExceptions.
16 function CreateNotSupportedError() {
17 return new Error('The algorithm is not supported');
18 }
19
20 function CreateInvalidAccessError() {
21 return new Error('The requested operation is not valid for the provided key');
22 }
23
24 function CreateDataError() {
25 return new Error('Data provided to an operation does not meet requirements');
26 }
27
28 function CreateSyntaxError() {
29 return new Error('A required parameter was missing our out-of-range');
30 }
31
32 function CreateOperationError() {
33 return new Error('The operation failed for an operation-specific reason');
34 }
35
36 /**
37 * Implementation of WebCrypto.SubtleCrypto used in enterprise.platformKeys.
38 * @param {string} tokenId The id of the backing Token.
39 * @constructor
40 */
41 var SubtleCryptoImpl = function(tokenId) {
42 this.tokenId = tokenId;
43 };
44
45 SubtleCryptoImpl.prototype.generateKey =
46 function(algorithm, extractable, keyUsages) {
47 var subtleCrypto = this;
48 return new Promise(function(resolve, reject) {
49 // TODO(pneubeck): Apply the algorithm normalization of the WebCrypto
50 // implementation.
51
52 if (extractable) {
53 // Note: This deviates from WebCrypto.SubtleCrypto.
54 throw CreateNotSupportedError();
55 }
56 if (intersect(keyUsages, [KeyUsage.sign, KeyUsage.verify]).length !=
57 keyUsages.length) {
58 throw CreateDataError();
59 }
60 if (!algorithm.name) {
61 // TODO(pneubeck): It's not clear from the WebCrypto spec which error to
62 // throw here.
63 throw CreateSyntaxError();
64 }
65
66 if (algorithm.name != 'RSASSA-PKCS1-v1_5') {
67 // Note: This deviates from WebCrypto.SubtleCrypto.
68 throw CreateNotSupportedError();
69 }
70 if (!algorithm.modulusLength || !algorithm.publicExponent)
71 throw CreateSyntaxError();
72
73 internalAPI.generateKey(
74 subtleCrypto.tokenId, algorithm.modulusLength, function(spki) {
75 if (chrome.runtime.lastError) {
76 reject(CreateOperationError());
77 return;
78 }
79 resolve(new KeyPair(spki, algorithm, keyUsages));
80 });
81 });
82 };
83
84 SubtleCryptoImpl.prototype.sign = function(algorithm, key, dataView) {
85 var subtleCrypto = this;
86 return new Promise(function(resolve, reject) {
87 if (key.type != 'private' || key.usages.indexOf(KeyUsage.sign) == -1)
88 throw CreateInvalidAccessError();
89
90 // Create an ArrayBuffer that equals the dataView. Note that dataView.buffer
91 // might contain more data than dataView.
92 var data = dataView.buffer.slice(dataView.byteOffset,
93 dataView.byteOffset + dataView.byteLength);
94 internalAPI.sign(
95 subtleCrypto.tokenId, getSpki(key), data, function(signature) {
96 if (chrome.runtime.lastError) {
97 reject(CreateOperationError());
98 return;
99 }
100 resolve(signature);
101 });
102 });
103 };
104
105 SubtleCryptoImpl.prototype.exportKey = function(format, key) {
106 return new Promise(function(resolve, reject) {
107 if (format == 'pkcs8') {
108 // Either key.type is not 'private' or the key is not extractable. In both
109 // cases the error is the same.
110 throw CreateInvalidAccessError();
111 } else if (format == 'spki') {
112 if (key.type != 'public')
113 throw CreateInvalidAccessError();
114 resolve(getSpki(key));
115 } else {
116 // TODO(pneubeck): It should be possible to export to format 'jwk'.
117 throw CreateNotSupportedError();
118 }
119 });
120 };
121
122 exports.SubtleCrypto =
123 utils.expose('SubtleCrypto',
124 SubtleCryptoImpl,
125 {functions:['generateKey', 'sign', 'exportKey']});
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698