1 /*
2  * Copyright (C) 2015 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package android.security.keystore2;
18 
19 import android.annotation.NonNull;
20 import android.security.KeyStoreSecurityLevel;
21 import android.security.keystore.KeyProperties;
22 import android.system.keystore2.KeyDescriptor;
23 import android.system.keystore2.KeyMetadata;
24 
25 import java.math.BigInteger;
26 import java.security.interfaces.RSAPublicKey;
27 
28 /**
29  * {@link RSAPublicKey} backed by Android Keystore.
30  *
31  * @hide
32  */
33 public class AndroidKeyStoreRSAPublicKey extends AndroidKeyStorePublicKey implements RSAPublicKey {
34     private final BigInteger mModulus;
35     private final BigInteger mPublicExponent;
36 
AndroidKeyStoreRSAPublicKey(@onNull KeyDescriptor descriptor, @NonNull KeyMetadata metadata, @NonNull byte[] x509EncodedForm, @NonNull KeyStoreSecurityLevel securityLevel, @NonNull BigInteger modulus, @NonNull BigInteger publicExponent)37     public AndroidKeyStoreRSAPublicKey(@NonNull KeyDescriptor descriptor,
38             @NonNull KeyMetadata metadata,
39             @NonNull byte[] x509EncodedForm,
40             @NonNull KeyStoreSecurityLevel securityLevel, @NonNull BigInteger modulus,
41             @NonNull BigInteger publicExponent) {
42         super(descriptor, metadata, x509EncodedForm, KeyProperties.KEY_ALGORITHM_RSA,
43                 securityLevel);
44         mModulus = modulus;
45         mPublicExponent = publicExponent;
46     }
47 
AndroidKeyStoreRSAPublicKey(@onNull KeyDescriptor descriptor, @NonNull KeyMetadata metadata, @NonNull KeyStoreSecurityLevel securityLevel, @NonNull RSAPublicKey info)48     public AndroidKeyStoreRSAPublicKey(@NonNull KeyDescriptor descriptor,
49             @NonNull KeyMetadata metadata,
50             @NonNull KeyStoreSecurityLevel securityLevel, @NonNull RSAPublicKey info) {
51         this(descriptor, metadata, info.getEncoded(), securityLevel, info.getModulus(),
52                 info.getPublicExponent());
53         if (!"X.509".equalsIgnoreCase(info.getFormat())) {
54             throw new IllegalArgumentException(
55                     "Unsupported key export format: " + info.getFormat());
56         }
57     }
58 
59     @Override
getPrivateKey()60     public AndroidKeyStorePrivateKey getPrivateKey() {
61         return new AndroidKeyStoreRSAPrivateKey(getUserKeyDescriptor(), getKeyIdDescriptor().nspace,
62                 getAuthorizations(), getSecurityLevel(), mModulus);
63     }
64 
65     @Override
getModulus()66     public BigInteger getModulus() {
67         return mModulus;
68     }
69 
70     @Override
getPublicExponent()71     public BigInteger getPublicExponent() {
72         return mPublicExponent;
73     }
74 }
75