-
Notifications
You must be signed in to change notification settings - Fork 5.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(op_crates/crypto): Introduce Web Crypto API #8984
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,8 @@ | ||
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license. | ||
"use strict"; | ||
|
||
// Implements https://www.w3.org/TR/WebCryptoAPI | ||
|
||
((window) => { | ||
const core = window.Deno.core; | ||
|
||
|
@@ -41,11 +43,145 @@ | |
return arrayBufferView; | ||
} | ||
|
||
// Algorithm normalization, which involves storing the expected data for | ||
// each pair of algorithm and crypto operation, is done on the JS side because | ||
// it is convenient. | ||
// Shall it stay that way, or are we better to move it on the Rust side? | ||
|
||
// We need this method after initialization, anytime we need to normalize | ||
// a provided algorithm. We store it here to prevent prototype pollution. | ||
const toUpperCase = String.prototype.toUpperCase; | ||
|
||
class RegisteredAlgorithmsContainer { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This class is never used There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It is meant to be used in further PRs. |
||
#nameIndex; | ||
#definitions; | ||
|
||
constructor(definitions) { | ||
this.#nameIndex = Object.create(null); | ||
this.#definitions = Object.create(null); | ||
for (const [name, definition] of Object.entries(definitions)) { | ||
this.#nameIndex[name.toUpperCase()] = name; | ||
this.#definitions[name] = definition; | ||
} | ||
} | ||
|
||
/** | ||
* A definition is an object whose keys are the keys that the input must | ||
* have and whose values are validation functions for the associate value. | ||
* The validation function will either return the value if it valid, or | ||
* throw an error that must be forwarded. | ||
*/ | ||
getDefinition(name) { | ||
return this.#definitions[name]; | ||
} | ||
|
||
normalizeName(providedName) { | ||
const upperCaseName = toUpperCase.call(providedName); | ||
return this.#nameIndex[upperCaseName]; | ||
} | ||
} | ||
|
||
const supportedAlgorithms = {}; | ||
|
||
function normalizeAlgorithm(algorithm, registeredAlgorithms) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Doing all that work in JS because:
|
||
let alg; | ||
if (typeof algorithm === "string") { | ||
alg = { name: algorithm }; | ||
} else if (typeof algorithm === "object" && algorithm !== null) { | ||
if (typeof algorithm.name !== "string") { | ||
throw new TypeError("Algorithm name is missing or not a string"); | ||
} | ||
alg = { ...algorithm }; | ||
} else { | ||
throw new TypeError("Argument 1 must be an object or a string"); | ||
} | ||
const algorithmName = registeredAlgorithms.normalizeName(alg.name); | ||
if (algorithmName === undefined) { | ||
throw new DOMException( | ||
"Unrecognized algorithm name", | ||
"NotSupportedError", | ||
); | ||
} | ||
const definition = registeredAlgorithms.getDefinition(algorithmName); | ||
for (const [propertyName, validate] of Object.entries(definition)) { | ||
alg[propertyName] = validate(algorithm[propertyName]); | ||
} | ||
alg.name = algorithmName; | ||
return alg; | ||
} | ||
|
||
const subtle = { | ||
async decrypt(algorithm, key, data) { | ||
await Promise.resolve(); | ||
throw new Error("Not implemented"); | ||
}, | ||
async deriveBits(algorithm, baseKey, length) { | ||
await Promise.resolve(); | ||
throw new Error("Not implemented"); | ||
}, | ||
async deriveKey( | ||
algorithm, | ||
baseKey, | ||
derivedKeyType, | ||
extractable, | ||
keyUsages, | ||
) { | ||
await Promise.resolve(); | ||
throw new Error("Not implemented"); | ||
}, | ||
async digest(algorithm, data) { | ||
await Promise.resolve(); | ||
throw new Error("Not implemented"); | ||
}, | ||
async encrypt(algorithm, key, data) { | ||
await Promise.resolve(); | ||
throw new Error("Not implemented"); | ||
}, | ||
async exportKey(format, key) { | ||
await Promise.resolve(); | ||
throw new Error("Not implemented"); | ||
}, | ||
async generateKey(algorithm, extractable, keyUsages) { | ||
await Promise.resolve(); | ||
throw new Error("Not implemented"); | ||
}, | ||
async importKey(format, keyData, algorithm, extractable, keyUsages) { | ||
await Promise.resolve(); | ||
throw new Error("Not implemented"); | ||
}, | ||
async sign(algorithm, key, data) { | ||
await Promise.resolve(); | ||
throw new Error("Not implemented"); | ||
}, | ||
async unwrapKey( | ||
format, | ||
wrappedKey, | ||
unwrappingKey, | ||
unwrapAlgorithm, | ||
unwrappedKeyAlgorithm, | ||
extractable, | ||
keyUsages, | ||
) { | ||
await Promise.resolve(); | ||
throw new Error("Not implemented"); | ||
}, | ||
async verify(algorithm, key, signature, data) { | ||
await Promise.resolve(); | ||
throw new Error("Not implemented"); | ||
}, | ||
async wrapKey(format, key, wrappingKey, wrapAlgorithm) { | ||
await Promise.resolve(); | ||
throw new Error("Not implemented"); | ||
}, | ||
}; | ||
|
||
window.crypto = { | ||
getRandomValues, | ||
subtle, | ||
}; | ||
window.__bootstrap = window.__bootstrap || {}; | ||
window.__bootstrap.crypto = { | ||
getRandomValues, | ||
subtle, | ||
}; | ||
})(this); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What do you think?