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.keystore;
18 
19 import java.security.Key;
20 
21 /**
22  * {@link Key} backed by Android Keystore.
23  *
24  * @hide
25  */
26 public class AndroidKeyStoreKey implements Key {
27     private final String mAlias;
28     private final int mUid;
29     private final String mAlgorithm;
30 
AndroidKeyStoreKey(String alias, int uid, String algorithm)31     public AndroidKeyStoreKey(String alias, int uid, String algorithm) {
32         mAlias = alias;
33         mUid = uid;
34         mAlgorithm = algorithm;
35     }
36 
getAlias()37     String getAlias() {
38         return mAlias;
39     }
40 
getUid()41     int getUid() {
42         return mUid;
43     }
44 
45     @Override
getAlgorithm()46     public String getAlgorithm() {
47         return mAlgorithm;
48     }
49 
50     @Override
getFormat()51     public String getFormat() {
52         // This key does not export its key material
53         return null;
54     }
55 
56     @Override
getEncoded()57     public byte[] getEncoded() {
58         // This key does not export its key material
59         return null;
60     }
61 
62     @Override
hashCode()63     public int hashCode() {
64         final int prime = 31;
65         int result = 1;
66         result = prime * result + ((mAlgorithm == null) ? 0 : mAlgorithm.hashCode());
67         result = prime * result + ((mAlias == null) ? 0 : mAlias.hashCode());
68         result = prime * result + mUid;
69         return result;
70     }
71 
72     @Override
equals(Object obj)73     public boolean equals(Object obj) {
74         if (this == obj) {
75             return true;
76         }
77         if (obj == null) {
78             return false;
79         }
80         if (getClass() != obj.getClass()) {
81             return false;
82         }
83         AndroidKeyStoreKey other = (AndroidKeyStoreKey) obj;
84         if (mAlgorithm == null) {
85             if (other.mAlgorithm != null) {
86                 return false;
87             }
88         } else if (!mAlgorithm.equals(other.mAlgorithm)) {
89             return false;
90         }
91         if (mAlias == null) {
92             if (other.mAlias != null) {
93                 return false;
94             }
95         } else if (!mAlias.equals(other.mAlias)) {
96             return false;
97         }
98         if (mUid != other.mUid) {
99             return false;
100         }
101         return true;
102     }
103 }
104