• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.system.keystore2.Authorization;
22 import android.system.keystore2.Domain;
23 import android.system.keystore2.KeyDescriptor;
24 
25 import java.security.Key;
26 
27 /**
28  * {@link Key} backed by Android Keystore.
29  *
30  * @hide
31  */
32 public class AndroidKeyStoreKey implements Key {
33     // This is the original KeyDescriptor by which the key was loaded from
34     // with alias and domain.
35     private final KeyDescriptor mDescriptor;
36     // The key id can be used make certain manipulations to the keystore database
37     // assuring that the manipulation is made to the exact key that was loaded
38     // from the database. Alias based manipulations can not assure this, because
39     // aliases can be rebound to other keys at any time.
40     private final long mKeyId;
41     private final Authorization[] mAuthorizations;
42     // TODO extract algorithm string from metadata.
43     private final String mAlgorithm;
44 
45     // This is the security level interface, that this key is associated with.
46     // We do not include this member in comparisons.
47     private final KeyStoreSecurityLevel mSecurityLevel;
48 
AndroidKeyStoreKey(@onNull KeyDescriptor descriptor, long keyId, @NonNull Authorization[] authorizations, @NonNull String algorithm, @NonNull KeyStoreSecurityLevel securityLevel)49     AndroidKeyStoreKey(@NonNull KeyDescriptor descriptor,
50             long keyId,
51             @NonNull Authorization[] authorizations,
52             @NonNull String algorithm,
53             @NonNull KeyStoreSecurityLevel securityLevel) {
54         mDescriptor = descriptor;
55         mKeyId = keyId;
56         mAuthorizations = authorizations;
57         mAlgorithm = algorithm;
58         mSecurityLevel = securityLevel;
59     }
60 
getUserKeyDescriptor()61     KeyDescriptor getUserKeyDescriptor() {
62         return mDescriptor;
63     }
64 
getKeyIdDescriptor()65     KeyDescriptor getKeyIdDescriptor() {
66         KeyDescriptor descriptor = new KeyDescriptor();
67         descriptor.nspace = mKeyId;
68         descriptor.domain = Domain.KEY_ID;
69         descriptor.alias = null;
70         descriptor.blob = null;
71         return descriptor;
72     }
73 
getAuthorizations()74     Authorization[] getAuthorizations() {
75         return mAuthorizations;
76     }
77 
getSecurityLevel()78     KeyStoreSecurityLevel getSecurityLevel() {
79         return mSecurityLevel;
80     }
81 
82 
83     @Override
getAlgorithm()84     public String getAlgorithm() {
85         return mAlgorithm;
86     }
87 
88     @Override
getFormat()89     public String getFormat() {
90         // This key does not export its key material
91         return null;
92     }
93 
94     @Override
getEncoded()95     public byte[] getEncoded() {
96         // This key does not export its key material
97         return null;
98     }
99 
100     @Override
hashCode()101     public int hashCode() {
102         final int prime = 31;
103         int result = 1;
104 
105         result = prime * result + ((mDescriptor == null) ? 0 : mDescriptor.hashCode());
106         result = prime * result + (int) (mKeyId >>> 32);
107         result = prime * result + (int) (mKeyId & 0xffffffff);
108         result = prime * result + ((mAuthorizations == null) ? 0 : mAuthorizations.hashCode());
109         result = prime * result + ((mAlgorithm == null) ? 0 : mAlgorithm.hashCode());
110         return result;
111     }
112 
113     @Override
equals(Object obj)114     public boolean equals(Object obj) {
115         if (this == obj) {
116             return true;
117         }
118         if (obj == null) {
119             return false;
120         }
121         if (getClass() != obj.getClass()) {
122             return false;
123         }
124         AndroidKeyStoreKey other = (AndroidKeyStoreKey) obj;
125         if (mKeyId != other.mKeyId) {
126             return false;
127         }
128 
129         return true;
130     }
131 }
132