mirror of
https://github.com/servo/servo.git
synced 2025-08-03 04:30:10 +01:00
Return cached object from CryptoKey.algorithm
getter (#34092)
* Return cached object from CryptoKey.algorithm getter Signed-off-by: Simon Wülker <simon.wuelker@arcor.de> * Test that CryptoKey.algorithm returns a cached object Signed-off-by: Simon Wülker <simon.wuelker@arcor.de> * Move duplicated code into a helper function Signed-off-by: Simon Wülker <simon.wuelker@arcor.de> --------- Signed-off-by: Simon Wülker <simon.wuelker@arcor.de>
This commit is contained in:
parent
f6aaf82b9b
commit
73d2f3c453
4 changed files with 142 additions and 49 deletions
|
@ -12,7 +12,7 @@ use aes::{Aes128, Aes192, Aes256};
|
|||
use base64::prelude::*;
|
||||
use dom_struct::dom_struct;
|
||||
use js::conversions::ConversionResult;
|
||||
use js::jsapi::JSObject;
|
||||
use js::jsapi::{JSObject, JS_NewObject};
|
||||
use js::jsval::ObjectValue;
|
||||
use js::rust::MutableHandleObject;
|
||||
use js::typedarray::ArrayBufferU8;
|
||||
|
@ -25,8 +25,8 @@ use crate::dom::bindings::codegen::Bindings::CryptoKeyBinding::{
|
|||
CryptoKeyMethods, KeyType, KeyUsage,
|
||||
};
|
||||
use crate::dom::bindings::codegen::Bindings::SubtleCryptoBinding::{
|
||||
AesCbcParams, AesCtrParams, AesKeyGenParams, Algorithm, AlgorithmIdentifier, JsonWebKey,
|
||||
KeyAlgorithm, KeyFormat, SubtleCryptoMethods,
|
||||
AesCbcParams, AesCtrParams, AesKeyAlgorithm, AesKeyGenParams, Algorithm, AlgorithmIdentifier,
|
||||
JsonWebKey, KeyAlgorithm, KeyFormat, SubtleCryptoMethods,
|
||||
};
|
||||
use crate::dom::bindings::codegen::UnionTypes::{
|
||||
ArrayBufferViewOrArrayBuffer, ArrayBufferViewOrArrayBufferOrJsonWebKey,
|
||||
|
@ -796,6 +796,7 @@ impl SubtleCrypto {
|
|||
|
||||
/// <https://w3c.github.io/webcrypto/#aes-cbc-operations>
|
||||
/// <https://w3c.github.io/webcrypto/#aes-ctr-operations>
|
||||
#[allow(unsafe_code)]
|
||||
fn generate_key_aes(
|
||||
&self,
|
||||
usages: Vec<KeyUsage>,
|
||||
|
@ -827,25 +828,40 @@ impl SubtleCrypto {
|
|||
_ => return Err(Error::NotSupported),
|
||||
};
|
||||
|
||||
Ok(CryptoKey::new(
|
||||
let cx = GlobalScope::get_cx();
|
||||
rooted!(in(*cx) let mut algorithm_object = unsafe {JS_NewObject(*cx, ptr::null()) });
|
||||
assert!(!algorithm_object.is_null());
|
||||
|
||||
AesKeyAlgorithm::from_name_and_size(
|
||||
name.clone(),
|
||||
key_gen_params.length,
|
||||
algorithm_object.handle_mut(),
|
||||
cx,
|
||||
);
|
||||
|
||||
let crypto_key = CryptoKey::new(
|
||||
&self.global(),
|
||||
KeyType::Secret,
|
||||
extractable,
|
||||
KeyAlgorithm { name },
|
||||
name,
|
||||
algorithm_object.handle(),
|
||||
usages,
|
||||
handle,
|
||||
))
|
||||
);
|
||||
|
||||
Ok(crypto_key)
|
||||
}
|
||||
|
||||
/// <https://w3c.github.io/webcrypto/#aes-cbc-operations>
|
||||
/// <https://w3c.github.io/webcrypto/#aes-ctr-operations>
|
||||
#[allow(unsafe_code)]
|
||||
fn import_key_aes(
|
||||
&self,
|
||||
format: KeyFormat,
|
||||
data: &[u8],
|
||||
extractable: bool,
|
||||
usages: Vec<KeyUsage>,
|
||||
alg: &str,
|
||||
alg_name: &str,
|
||||
) -> Result<DomRoot<CryptoKey>, Error> {
|
||||
if usages.iter().any(|usage| {
|
||||
!matches!(
|
||||
|
@ -865,15 +881,30 @@ impl SubtleCrypto {
|
|||
256 => Handle::Aes256(data.to_vec()),
|
||||
_ => return Err(Error::Data),
|
||||
};
|
||||
let name = DOMString::from(alg);
|
||||
Ok(CryptoKey::new(
|
||||
|
||||
let name = DOMString::from(alg_name.to_string());
|
||||
|
||||
let cx = GlobalScope::get_cx();
|
||||
rooted!(in(*cx) let mut algorithm_object = unsafe {JS_NewObject(*cx, ptr::null()) });
|
||||
assert!(!algorithm_object.is_null());
|
||||
|
||||
AesKeyAlgorithm::from_name_and_size(
|
||||
name.clone(),
|
||||
(data.len() * 8) as u16,
|
||||
algorithm_object.handle_mut(),
|
||||
cx,
|
||||
);
|
||||
let crypto_key = CryptoKey::new(
|
||||
&self.global(),
|
||||
KeyType::Secret,
|
||||
extractable,
|
||||
KeyAlgorithm { name },
|
||||
name,
|
||||
algorithm_object.handle(),
|
||||
usages,
|
||||
handle,
|
||||
))
|
||||
);
|
||||
|
||||
Ok(crypto_key)
|
||||
}
|
||||
|
||||
/// <https://w3c.github.io/webcrypto/#aes-cbc-operations>
|
||||
|
@ -939,3 +970,19 @@ fn data_to_jwk_params(alg: &str, size: &str, key: &[u8]) -> (DOMString, DOMStrin
|
|||
data.retain(|c| c != '=');
|
||||
(jwk_alg, DOMString::from(data))
|
||||
}
|
||||
|
||||
impl AesKeyAlgorithm {
|
||||
/// Fill the object referenced by `out` with an [AesKeyAlgorithm]
|
||||
/// of the specified name and size.
|
||||
#[allow(unsafe_code)]
|
||||
fn from_name_and_size(name: DOMString, size: u16, out: MutableHandleObject, cx: JSContext) {
|
||||
let key_algorithm = Self {
|
||||
parent: KeyAlgorithm { name: name.clone() },
|
||||
length: size,
|
||||
};
|
||||
|
||||
unsafe {
|
||||
key_algorithm.to_jsobject(*cx, out);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue