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.ArrayUtils;
22 import android.system.keystore2.KeyDescriptor;
23 import android.system.keystore2.KeyMetadata;
24 
25 import java.security.PublicKey;
26 import java.util.Arrays;
27 
28 /**
29  * {@link PublicKey} backed by Android Keystore.
30  *
31  * @hide
32  */
33 public abstract class AndroidKeyStorePublicKey extends AndroidKeyStoreKey implements PublicKey {
34     private final byte[] mCertificate;
35     private final byte[] mCertificateChain;
36     private final byte[] mEncoded;
37 
AndroidKeyStorePublicKey(@onNull KeyDescriptor descriptor, @NonNull KeyMetadata metadata, @NonNull byte[] x509EncodedForm, @NonNull String algorithm, @NonNull KeyStoreSecurityLevel securityLevel)38     public AndroidKeyStorePublicKey(@NonNull KeyDescriptor descriptor,
39             @NonNull KeyMetadata metadata, @NonNull byte[] x509EncodedForm,
40             @NonNull String algorithm, @NonNull KeyStoreSecurityLevel securityLevel) {
41         super(descriptor, metadata.key.nspace, metadata.authorizations, algorithm, securityLevel);
42         mCertificate = metadata.certificate;
43         mCertificateChain = metadata.certificateChain;
44         mEncoded = x509EncodedForm;
45     }
46 
getPrivateKey()47     abstract AndroidKeyStorePrivateKey getPrivateKey();
48 
49     @Override
getFormat()50     public String getFormat() {
51         return "X.509";
52     }
53 
54     @Override
getEncoded()55     public byte[] getEncoded() {
56         return ArrayUtils.cloneIfNotEmpty(mEncoded);
57     }
58 
59     @Override
hashCode()60     public int hashCode() {
61         final int prime = 31;
62         int result = 1;
63 
64         result = prime * result + super.hashCode();
65         result = prime * result + Arrays.hashCode(mCertificate);
66         result = prime * result + Arrays.hashCode(mCertificateChain);
67 
68         return result;
69     }
70 
71     @Override
equals(Object obj)72     public boolean equals(Object obj) {
73         if (this == obj) {
74             return true;
75         }
76         if (!super.equals(obj)) {
77             return false;
78         }
79 
80         /*
81          * getClass().equals(ojb.getClass()) is implied by the call to super.equals() above. This
82          * means we can cast obj to AndroidKeyStorePublicKey here.
83          */
84         final AndroidKeyStorePublicKey other = (AndroidKeyStorePublicKey) obj;
85 
86         return Arrays.equals(mCertificate, other.mCertificate) && Arrays.equals(mCertificateChain,
87                 other.mCertificateChain);
88     }
89 }
90