1 /*
2  * Copyright (C) 2017 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;
18 
19 import android.annotation.NonNull;
20 import android.annotation.Nullable;
21 
22 import java.security.KeyPair;
23 import java.security.cert.Certificate;
24 import java.util.ArrayList;
25 import java.util.Arrays;
26 import java.util.Collections;
27 import java.util.List;
28 
29 /**
30  * The {@code AttestedKeyPair} class contains a {@code KeyPair} instance of
31  * keys generated by Keystore and owned by KeyChain, as well as an attestation
32  * record for the key.
33  *
34  * <p>Such keys can be obtained by calling
35  * {@link android.app.admin.DevicePolicyManager#generateKeyPair}.
36  */
37 
38 public final class AttestedKeyPair {
39     private final KeyPair mKeyPair;
40     private final List<Certificate> mAttestationRecord;
41 
42     /**
43      * Public constructor for creating a new instance (useful for testing).
44      *
45      * @param keyPair the key pair associated with the attestation record.
46      * @param attestationRecord attestation record for the provided key pair.
47      */
AttestedKeyPair( @ullable KeyPair keyPair, @NonNull List<Certificate> attestationRecord)48     public AttestedKeyPair(
49             @Nullable KeyPair keyPair, @NonNull List<Certificate> attestationRecord) {
50         mKeyPair = keyPair;
51         mAttestationRecord = attestationRecord;
52     }
53 
54     /**
55      * @hide used by platform.
56      */
AttestedKeyPair(@ullable KeyPair keyPair, @Nullable Certificate[] attestationRecord)57     public AttestedKeyPair(@Nullable KeyPair keyPair, @Nullable Certificate[] attestationRecord) {
58         mKeyPair = keyPair;
59         if (attestationRecord == null) {
60             mAttestationRecord = new ArrayList();
61         } else {
62             mAttestationRecord = Arrays.asList(attestationRecord);
63         }
64     }
65 
66     /**
67      * Returns the generated key pair associated with the attestation record
68      * in this instance.
69      */
getKeyPair()70     public @Nullable KeyPair getKeyPair() {
71         return mKeyPair;
72     }
73 
74     /**
75      * Returns the attestation record for the key pair in this instance.
76      *
77      * The attestation record is a chain of certificates. The leaf certificate links to the public
78      * key of this key pair and other properties of the key or the device. If the key is in secure
79      * hardware, and if the secure hardware supports attestation, the leaf certificate will be
80      * signed by a chain of certificates rooted at a trustworthy CA key. Otherwise the chain will be
81      * rooted at an untrusted certificate.
82      *
83      * The attestation record could be for properties of the key, or include device identifiers.
84      *
85      * See {@link android.security.keystore.KeyGenParameterSpec.Builder#setAttestationChallenge}
86      * and  <a href="https://developer.android.com/training/articles/security-key-attestation.html">
87      * Key Attestation</a> for the format of the attestation record inside the certificate.
88      */
getAttestationRecord()89     public @NonNull List<Certificate> getAttestationRecord() {
90         return Collections.unmodifiableList(mAttestationRecord);
91     }
92 }
93