blob: 19fbdac16576ce26c998f4eeda2e273af41dc628 [file] [log] [blame]
Eran Messeri852c8f12017-11-15 05:55:52 +00001/*
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
17package android.security;
18
Eran Messeri390539d2019-08-05 17:38:55 +010019import android.annotation.NonNull;
20import android.annotation.Nullable;
21
Eran Messeri852c8f12017-11-15 05:55:52 +000022import java.security.KeyPair;
23import java.security.cert.Certificate;
24import java.util.ArrayList;
25import java.util.Arrays;
Eran Messerie7a65b62019-08-23 13:37:43 +010026import java.util.Collections;
Eran Messeri852c8f12017-11-15 05:55:52 +000027import 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
38public final class AttestedKeyPair {
39 private final KeyPair mKeyPair;
Eran Messerie7a65b62019-08-23 13:37:43 +010040 private final List<Certificate> mAttestationRecord;
Eran Messeri852c8f12017-11-15 05:55:52 +000041
42 /**
Eran Messeri390539d2019-08-05 17:38:55 +010043 * 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.
Eran Messeri852c8f12017-11-15 05:55:52 +000047 */
Eran Messerie7a65b62019-08-23 13:37:43 +010048 public AttestedKeyPair(
49 @Nullable KeyPair keyPair, @NonNull List<Certificate> attestationRecord) {
Eran Messeri852c8f12017-11-15 05:55:52 +000050 mKeyPair = keyPair;
51 mAttestationRecord = attestationRecord;
52 }
53
54 /**
Eran Messerie7a65b62019-08-23 13:37:43 +010055 * @hide used by platform.
56 */
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 /**
Eran Messeri852c8f12017-11-15 05:55:52 +000067 * Returns the generated key pair associated with the attestation record
68 * in this instance.
69 */
Eran Messeri390539d2019-08-05 17:38:55 +010070 public @Nullable KeyPair getKeyPair() {
Eran Messeri852c8f12017-11-15 05:55:52 +000071 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 */
Eran Messeri390539d2019-08-05 17:38:55 +010089 public @NonNull List<Certificate> getAttestationRecord() {
Eran Messerie7a65b62019-08-23 13:37:43 +010090 return Collections.unmodifiableList(mAttestationRecord);
Eran Messeri852c8f12017-11-15 05:55:52 +000091 }
92}