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
/* 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 usages(&self) -> &[KeyUsage] {
        &self.usages
    }

    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
    }

    /// <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()
        }
    }
}