android_keyring/
keystore.rs

1use crate::methods::{
2    ClassDecl, Constructible, FromValue, JResult, Method, NoParam, SignatureComp, StaticMethod,
3    ToValue,
4};
5use jni::{
6    JNIEnv,
7    objects::{GlobalRef, JObject, JValueGen},
8};
9use std::marker::PhantomData;
10
11pub struct KeyStore {
12    self_: GlobalRef,
13}
14impl FromValue for KeyStore {
15    fn signature() -> SignatureComp {
16        Self::class().into()
17    }
18
19    fn from_object(self_: GlobalRef, _env: &mut JNIEnv) -> JResult<Self> {
20        Ok(Self { self_ })
21    }
22}
23impl KeyStore {
24    fn class() -> ClassDecl {
25        ClassDecl("Ljava/security/KeyStore;")
26    }
27
28    pub fn get_instance(env: &mut JNIEnv<'_>, store: &str) -> JResult<KeyStore> {
29        struct ThisMethod<'a>(PhantomData<&'a ()>);
30        impl<'a> StaticMethod for ThisMethod<'a> {
31            type Param = &'a str;
32            type Return = KeyStore;
33
34            const NAME: &'static str = "getInstance";
35        }
36
37        ThisMethod::call(Self::class(), env, store)
38    }
39
40    pub fn load(&self, env: &mut JNIEnv<'_>) -> JResult<()> {
41        struct LoadStoreParameter;
42        impl ToValue for LoadStoreParameter {
43            fn signature() -> SignatureComp {
44                ClassDecl("Ljava/security/KeyStore$LoadStoreParameter;").into()
45            }
46
47            fn to_value<'a>(
48                &self,
49                _env: &mut JNIEnv<'a>,
50            ) -> JResult<jni::objects::JValueGen<JObject<'a>>> {
51                Ok(JObject::null().into())
52            }
53        }
54
55        struct ThisMethod;
56        impl Method for ThisMethod {
57            type Param = LoadStoreParameter;
58            type Return = ();
59
60            const NAME: &str = "load";
61        }
62
63        ThisMethod::call(&self.self_, env, LoadStoreParameter)
64    }
65
66    pub fn contains_alias(&self, env: &mut JNIEnv<'_>, alias: &str) -> JResult<bool> {
67        struct ThisMethod<'a>(PhantomData<&'a ()>);
68        impl<'a> Method for ThisMethod<'a> {
69            type Param = &'a str;
70            type Return = bool;
71
72            const NAME: &'static str = "containsAlias";
73        }
74
75        ThisMethod::call(&self.self_, env, alias)
76    }
77
78    pub fn get_key(&self, env: &mut JNIEnv<'_>, alias: &str) -> JResult<Option<Key>> {
79        struct ThisMethod<'a>(PhantomData<&'a ()>);
80        impl<'a> Method for ThisMethod<'a> {
81            type Param = (&'a str, Option<Vec<u16>>);
82            type Return = Option<Key>;
83
84            const NAME: &'static str = "getKey";
85        }
86
87        ThisMethod::call(&self.self_, env, (alias, None))
88    }
89}
90
91#[derive(Debug)]
92pub struct Key {
93    self_: GlobalRef,
94}
95impl FromValue for Key {
96    fn signature() -> SignatureComp {
97        Self::class().into()
98    }
99
100    fn from_object(self_: GlobalRef, _env: &mut JNIEnv) -> JResult<Self> {
101        Ok(Self { self_ })
102    }
103}
104impl ToValue for Key {
105    fn signature() -> SignatureComp {
106        Self::class().into()
107    }
108
109    fn to_value<'a>(&self, env: &mut JNIEnv<'a>) -> JResult<JValueGen<JObject<'a>>> {
110        Ok(env.new_local_ref(&self.self_)?.into())
111    }
112}
113impl Key {
114    fn class() -> ClassDecl {
115        ClassDecl("Ljava/security/Key;")
116    }
117}
118
119pub struct SecretKey {
120    self_: GlobalRef,
121}
122impl FromValue for SecretKey {
123    fn signature() -> SignatureComp {
124        Self::class().into()
125    }
126
127    fn from_object(self_: GlobalRef, _env: &mut JNIEnv) -> JResult<Self> {
128        Ok(Self { self_ })
129    }
130}
131impl SecretKey {
132    fn class() -> ClassDecl {
133        ClassDecl("Ljavax/crypto/SecretKey;")
134    }
135}
136impl From<SecretKey> for Key {
137    fn from(value: SecretKey) -> Self {
138        Key { self_: value.self_ }
139    }
140}
141
142pub struct KeyGenerator {
143    self_: GlobalRef,
144}
145impl FromValue for KeyGenerator {
146    fn signature() -> SignatureComp {
147        Self::class().into()
148    }
149
150    fn from_object(self_: GlobalRef, _env: &mut JNIEnv) -> JResult<Self> {
151        Ok(Self { self_ })
152    }
153}
154impl KeyGenerator {
155    fn class() -> ClassDecl {
156        ClassDecl("Ljavax/crypto/KeyGenerator;")
157    }
158
159    pub fn get_instance(env: &mut JNIEnv, algorithm: &str, provider: &str) -> JResult<Self> {
160        struct ThisMethod<'a>(PhantomData<&'a ()>);
161        impl<'a> StaticMethod for ThisMethod<'a> {
162            type Param = (&'a str, &'a str);
163            type Return = KeyGenerator;
164
165            const NAME: &'static str = "getInstance";
166        }
167
168        ThisMethod::call(Self::class(), env, (algorithm, provider))
169    }
170
171    pub fn init(&self, env: &mut JNIEnv, spec: AlgorithmParameterSpec) -> JResult<()> {
172        struct ThisMethod;
173        impl Method for ThisMethod {
174            type Param = AlgorithmParameterSpec;
175            type Return = ();
176
177            const NAME: &str = "init";
178        }
179
180        ThisMethod::call(&self.self_, env, spec)
181    }
182
183    pub fn generate_key(&self, env: &mut JNIEnv) -> JResult<SecretKey> {
184        struct ThisMethod;
185        impl Method for ThisMethod {
186            type Param = NoParam;
187            type Return = SecretKey;
188
189            const NAME: &str = "generateKey";
190        }
191
192        ThisMethod::call(&self.self_, env, NoParam)
193    }
194}
195
196pub struct KeyGenParameterSpecBuilder {
197    self_: GlobalRef,
198}
199impl FromValue for KeyGenParameterSpecBuilder {
200    fn signature() -> SignatureComp {
201        Self::class().into()
202    }
203
204    fn from_object(self_: GlobalRef, _env: &mut JNIEnv) -> JResult<Self> {
205        Ok(Self { self_ })
206    }
207}
208impl KeyGenParameterSpecBuilder {
209    fn class() -> ClassDecl {
210        ClassDecl("Landroid/security/keystore/KeyGenParameterSpec$Builder;")
211    }
212}
213impl KeyGenParameterSpecBuilder {
214    pub fn new(env: &mut JNIEnv, alias: &str, purpose: i32) -> JResult<Self> {
215        struct ThisMethod<'a>(PhantomData<&'a ()>);
216        impl<'a> Constructible for ThisMethod<'a> {
217            type Param = (&'a str, i32);
218            type Return = KeyGenParameterSpecBuilder;
219        }
220
221        ThisMethod::call_new(Self::class(), env, (alias, purpose))
222    }
223
224    pub fn set_block_modes(
225        &self,
226        env: &mut JNIEnv,
227        modes: &[&str],
228    ) -> JResult<KeyGenParameterSpecBuilder> {
229        struct ThisMethod<'a>(PhantomData<&'a ()>);
230        impl<'a> Method for ThisMethod<'a> {
231            type Param = &'a [&'a str];
232            type Return = KeyGenParameterSpecBuilder;
233
234            const NAME: &'static str = "setBlockModes";
235        }
236
237        ThisMethod::call(&self.self_, env, modes)
238    }
239
240    pub fn set_encryption_paddings(
241        &self,
242        env: &mut JNIEnv,
243        modes: &[&str],
244    ) -> JResult<KeyGenParameterSpecBuilder> {
245        struct ThisMethod<'a>(PhantomData<&'a ()>);
246        impl<'a> Method for ThisMethod<'a> {
247            type Param = &'a [&'a str];
248            type Return = KeyGenParameterSpecBuilder;
249
250            const NAME: &'static str = "setEncryptionPaddings";
251        }
252
253        ThisMethod::call(&self.self_, env, modes)
254    }
255
256    pub fn set_user_authentication_required(
257        &self,
258        env: &mut JNIEnv,
259        required: bool,
260    ) -> JResult<KeyGenParameterSpecBuilder> {
261        struct ThisMethod;
262        impl Method for ThisMethod {
263            type Param = bool;
264            type Return = KeyGenParameterSpecBuilder;
265
266            const NAME: &str = "setUserAuthenticationRequired";
267        }
268
269        ThisMethod::call(&self.self_, env, required)
270    }
271
272    pub fn build(&self, env: &mut JNIEnv) -> JResult<KeyGenParameterSpec> {
273        struct ThisMethod;
274        impl Method for ThisMethod {
275            type Param = NoParam;
276            type Return = KeyGenParameterSpec;
277
278            const NAME: &str = "build";
279        }
280
281        ThisMethod::call(&self.self_, env, NoParam)
282    }
283}
284
285pub struct KeyGenParameterSpec {
286    self_: GlobalRef,
287}
288impl FromValue for KeyGenParameterSpec {
289    fn signature() -> SignatureComp {
290        Self::class().into()
291    }
292
293    fn from_object(self_: GlobalRef, _env: &mut JNIEnv) -> JResult<Self> {
294        Ok(Self { self_ })
295    }
296}
297impl KeyGenParameterSpec {
298    fn class() -> ClassDecl {
299        ClassDecl("Landroid/security/keystore/KeyGenParameterSpec;")
300    }
301}
302
303pub struct AlgorithmParameterSpec {
304    self_: GlobalRef,
305}
306impl From<KeyGenParameterSpec> for AlgorithmParameterSpec {
307    fn from(value: KeyGenParameterSpec) -> Self {
308        Self { self_: value.self_ }
309    }
310}
311impl ToValue for AlgorithmParameterSpec {
312    fn signature() -> SignatureComp {
313        Self::class().into()
314    }
315
316    fn to_value<'a>(&self, env: &mut JNIEnv<'a>) -> JResult<jni::objects::JValueGen<JObject<'a>>> {
317        Ok(env.new_local_ref(&self.self_)?.into())
318    }
319}
320impl AlgorithmParameterSpec {
321    fn class() -> ClassDecl {
322        ClassDecl("Ljava/security/spec/AlgorithmParameterSpec;")
323    }
324}