mirror of
https://github.com/servo/servo.git
synced 2025-08-06 14:10:11 +01:00
crypto: Begin SubtleCrypto implementation (#33628)
* Update IDLs and Bindings conf Signed-off-by: Daniel Adams <msub2official@gmail.com> * Add AES crate Signed-off-by: Daniel Adams <msub2official@gmail.com> * Implement DOM interfaces Signed-off-by: Daniel Adams <msub2official@gmail.com> * IDL tidy Signed-off-by: Daniel Adams <msub2official@gmail.com> * Remove deriveKey from inRealms for now until implemented Signed-off-by: Daniel Adams <msub2official@gmail.com> * Fix CryptoKey rustdoc comments Signed-off-by: Daniel Adams <msub2official@gmail.com> * Move string constants to top of file Signed-off-by: Daniel Adams <msub2official@gmail.com> * Use properly rooted CryptoKey Signed-off-by: Daniel Adams <msub2official@gmail.com> * Code clarity Signed-off-by: Daniel Adams <msub2official@gmail.com> * Rework NormalizedAlgorithm to not hold a DOMString Signed-off-by: Daniel Adams <msub2official@gmail.com> * Add Rustdoc for CryptoKey interface Signed-off-by: Daniel Adams <msub2official@gmail.com> * Move ignore mallocsizeof to rand crate, remove from crypto Signed-off-by: Daniel Adams <msub2official@gmail.com> * Update cargo lock Signed-off-by: Daniel Adams <msub2official@gmail.com> * Fix key handling, implement exportKey with JWK TODO Signed-off-by: Daniel Adams <msub2official@gmail.com> * Add missing spec link Signed-off-by: Daniel Adams <msub2official@gmail.com> * Use create_buffer_source, remove aes dep from libservo Signed-off-by: Daniel Adams <msub2official@gmail.com> * Fix crash when running in worker Signed-off-by: Daniel Adams <msub2official@gmail.com> * Update expectations Signed-off-by: Daniel Adams <msub2official@gmail.com> * fmt Signed-off-by: Daniel Adams <msub2official@gmail.com> * Move CryptoKey and SubtleCrypto behind pref for now Signed-off-by: Daniel Adams <msub2official@gmail.com> * Update expectations Signed-off-by: Daniel Adams <msub2official@gmail.com> * Readd timeout expectation Signed-off-by: Daniel Adams <msub2official@gmail.com> --------- Signed-off-by: Daniel Adams <msub2official@gmail.com>
This commit is contained in:
parent
66bc430b24
commit
fc0d4d8157
82 changed files with 39536 additions and 557 deletions
140
components/script/dom/cryptokey.rs
Normal file
140
components/script/dom/cryptokey.rs
Normal file
|
@ -0,0 +1,140 @@
|
|||
/* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
|
||||
|
||||
use std::cell::Cell;
|
||||
use std::ptr::NonNull;
|
||||
|
||||
use dom_struct::dom_struct;
|
||||
use js::jsapi::{JSObject, Value};
|
||||
|
||||
use crate::dom::bindings::cell::DomRefCell;
|
||||
use crate::dom::bindings::codegen::Bindings::CryptoKeyBinding::{
|
||||
CryptoKeyMethods, KeyType, KeyUsage,
|
||||
};
|
||||
use crate::dom::bindings::codegen::Bindings::SubtleCryptoBinding::{AesKeyAlgorithm, KeyAlgorithm};
|
||||
use crate::dom::bindings::reflector::{reflect_dom_object, Reflector};
|
||||
use crate::dom::bindings::root::DomRoot;
|
||||
use crate::dom::bindings::str::DOMString;
|
||||
use crate::dom::globalscope::GlobalScope;
|
||||
use crate::js::conversions::ToJSValConvertible;
|
||||
use crate::script_runtime::JSContext;
|
||||
|
||||
/// The underlying cryptographic data this key represents
|
||||
#[allow(dead_code)]
|
||||
pub enum Handle {
|
||||
Aes128(Vec<u8>),
|
||||
Aes192(Vec<u8>),
|
||||
Aes256(Vec<u8>),
|
||||
}
|
||||
|
||||
/// <https://w3c.github.io/webcrypto/#cryptokey-interface>
|
||||
#[dom_struct]
|
||||
pub struct CryptoKey {
|
||||
reflector_: Reflector,
|
||||
key_type: KeyType,
|
||||
extractable: Cell<bool>,
|
||||
// This would normally be KeyAlgorithm but we cannot Send DOMString, which
|
||||
// is a member of Algorithm
|
||||
algorithm: DomRefCell<String>,
|
||||
usages: Vec<KeyUsage>,
|
||||
#[ignore_malloc_size_of = "Defined in external cryptography crates"]
|
||||
#[no_trace]
|
||||
handle: Handle,
|
||||
}
|
||||
|
||||
impl CryptoKey {
|
||||
fn new_inherited(
|
||||
key_type: KeyType,
|
||||
extractable: bool,
|
||||
algorithm: KeyAlgorithm,
|
||||
usages: Vec<KeyUsage>,
|
||||
handle: Handle,
|
||||
) -> CryptoKey {
|
||||
CryptoKey {
|
||||
reflector_: Reflector::new(),
|
||||
key_type,
|
||||
extractable: Cell::new(extractable),
|
||||
algorithm: DomRefCell::new(algorithm.name.to_string()),
|
||||
usages,
|
||||
handle,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn new(
|
||||
global: &GlobalScope,
|
||||
key_type: KeyType,
|
||||
extractable: bool,
|
||||
algorithm: KeyAlgorithm,
|
||||
usages: Vec<KeyUsage>,
|
||||
handle: Handle,
|
||||
) -> DomRoot<CryptoKey> {
|
||||
reflect_dom_object(
|
||||
Box::new(CryptoKey::new_inherited(
|
||||
key_type,
|
||||
extractable,
|
||||
algorithm,
|
||||
usages,
|
||||
handle,
|
||||
)),
|
||||
global,
|
||||
)
|
||||
}
|
||||
|
||||
pub fn algorithm(&self) -> String {
|
||||
self.algorithm.borrow().to_string()
|
||||
}
|
||||
|
||||
pub fn handle(&self) -> &Handle {
|
||||
&self.handle
|
||||
}
|
||||
}
|
||||
|
||||
impl CryptoKeyMethods for CryptoKey {
|
||||
/// <https://w3c.github.io/webcrypto/#cryptokey-interface-members>
|
||||
fn Type(&self) -> KeyType {
|
||||
self.key_type.clone()
|
||||
}
|
||||
|
||||
/// <https://w3c.github.io/webcrypto/#cryptokey-interface-members>
|
||||
fn Extractable(&self) -> bool {
|
||||
self.extractable.get()
|
||||
}
|
||||
|
||||
#[allow(unsafe_code)]
|
||||
/// <https://w3c.github.io/webcrypto/#cryptokey-interface-members>
|
||||
fn Algorithm(&self, cx: JSContext) -> NonNull<JSObject> {
|
||||
let parent = KeyAlgorithm {
|
||||
name: DOMString::from_string(self.algorithm()),
|
||||
};
|
||||
let algorithm = match self.handle() {
|
||||
Handle::Aes128(_) => AesKeyAlgorithm {
|
||||
parent,
|
||||
length: 128,
|
||||
},
|
||||
Handle::Aes192(_) => AesKeyAlgorithm {
|
||||
parent,
|
||||
length: 192,
|
||||
},
|
||||
Handle::Aes256(_) => AesKeyAlgorithm {
|
||||
parent,
|
||||
length: 256,
|
||||
},
|
||||
};
|
||||
unsafe {
|
||||
rooted!(in(*cx) let mut alg: Value);
|
||||
algorithm.to_jsval(*cx, alg.handle_mut());
|
||||
NonNull::new(alg.to_object()).unwrap()
|
||||
}
|
||||
}
|
||||
|
||||
#[allow(unsafe_code)]
|
||||
/// <https://w3c.github.io/webcrypto/#cryptokey-interface-members>
|
||||
fn Usages(&self, cx: JSContext) -> NonNull<JSObject> {
|
||||
unsafe {
|
||||
rooted!(in(*cx) let mut usages: Value);
|
||||
self.usages.to_jsval(*cx, usages.handle_mut());
|
||||
NonNull::new(usages.to_object()).unwrap()
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue