1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/* 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::{Heap, JSObject, Value};
use js::rust::HandleObject;

use crate::dom::bindings::codegen::Bindings::CryptoKeyBinding::{
    CryptoKeyMethods, KeyType, KeyUsage,
};
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)]
#[derive(MallocSizeOf)]
pub enum Handle {
    Aes128(Vec<u8>),
    Aes192(Vec<u8>),
    Aes256(Vec<u8>),
    Pbkdf2(Vec<u8>),
    Hkdf(Vec<u8>),
    Hmac(Vec<u8>),
}

/// <https://w3c.github.io/webcrypto/#cryptokey-interface>
#[dom_struct]
pub struct CryptoKey {
    reflector_: Reflector,

    /// <https://w3c.github.io/webcrypto/#dom-cryptokey-type>
    key_type: KeyType,

    /// <https://w3c.github.io/webcrypto/#dom-cryptokey-extractable>
    extractable: Cell<bool>,

    /// The name of the algorithm used
    ///
    /// This is always the same as the `name` of the
    /// [`[[algorithm]]`](https://w3c.github.io/webcrypto/#dom-cryptokey-algorithm)
    /// internal slot, but we store it here again for convenience
    algorithm: DOMString,

    /// <https://w3c.github.io/webcrypto/#dom-cryptokey-algorithm>
    #[ignore_malloc_size_of = "Defined in mozjs"]
    algorithm_object: Heap<*mut JSObject>,

    /// <https://w3c.github.io/webcrypto/#dom-cryptokey-usages>
    usages: Vec<KeyUsage>,
    #[no_trace]
    handle: Handle,
}

impl CryptoKey {
    fn new_inherited(
        key_type: KeyType,
        extractable: bool,
        usages: Vec<KeyUsage>,
        algorithm: DOMString,
        handle: Handle,
    ) -> CryptoKey {
        CryptoKey {
            reflector_: Reflector::new(),
            key_type,
            extractable: Cell::new(extractable),
            algorithm,
            algorithm_object: Heap::default(),
            usages,
            handle,
        }
    }

    pub fn new(
        global: &GlobalScope,
        key_type: KeyType,
        extractable: bool,
        algorithm: DOMString,
        algorithm_object: HandleObject,
        usages: Vec<KeyUsage>,
        handle: Handle,
    ) -> DomRoot<CryptoKey> {
        let object = reflect_dom_object(
            Box::new(CryptoKey::new_inherited(
                key_type,
                extractable,
                usages,
                algorithm,
                handle,
            )),
            global,
        );

        object.algorithm_object.set(algorithm_object.get());

        object
    }

    pub fn algorithm(&self) -> String {
        self.algorithm.to_string()
    }

    pub fn usages(&self) -> &[KeyUsage] {
        &self.usages
    }

    pub fn handle(&self) -> &Handle {
        &self.handle
    }
}

impl CryptoKeyMethods<crate::DomTypeHolder> for CryptoKey {
    /// <https://w3c.github.io/webcrypto/#dom-cryptokey-type>
    fn Type(&self) -> KeyType {
        self.key_type
    }

    /// <https://w3c.github.io/webcrypto/#dom-cryptokey-extractable>
    fn Extractable(&self) -> bool {
        self.extractable.get()
    }

    /// <https://w3c.github.io/webcrypto/#dom-cryptokey-algorithm>
    fn Algorithm(&self, _cx: JSContext) -> NonNull<JSObject> {
        NonNull::new(self.algorithm_object.get()).unwrap()
    }

    #[allow(unsafe_code)]
    /// <https://w3c.github.io/webcrypto/#dom-cryptokey-usages>
    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()
        }
    }
}

impl Handle {
    pub fn as_bytes(&self) -> &[u8] {
        match self {
            Self::Aes128(bytes) => bytes,
            Self::Aes192(bytes) => bytes,
            Self::Aes256(bytes) => bytes,
            Self::Pbkdf2(bytes) => bytes,
            Self::Hkdf(bytes) => bytes,
            Self::Hmac(bytes) => bytes,
        }
    }
}