blob: fe38504279575b89a9e75e9c30fb3015b9208c1c [file] [log] [blame]
Max Bires148c08e2020-10-13 13:41:41 -07001// Copyright 2020, The Android Open Source Project
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15//! This is the implementation for the remote provisioning AIDL interface between
16//! the network providers for remote provisioning and the system. This interface
17//! allows the caller to prompt the Remote Provisioning HAL to generate keys and
18//! CBOR blobs that can be ferried to a provisioning server that will return
19//! certificate chains signed by some root authority and stored in a keystore SQLite
20//! DB.
21
22use android_hardware_security_keymint::aidl::android::hardware::security::keymint::SecurityLevel::SecurityLevel;
23
24use android_security_remoteprovisioning::aidl::android::security::remoteprovisioning::{
25 AttestationPoolStatus::AttestationPoolStatus, IRemoteProvisioning::BnRemoteProvisioning,
26 IRemoteProvisioning::IRemoteProvisioning,
27};
Stephen Crane221bbb52020-12-16 15:52:10 -080028use android_security_remoteprovisioning::binder::Strong;
Max Bires148c08e2020-10-13 13:41:41 -070029use anyhow::Result;
30
31use crate::error::map_or_log_err;
32use crate::globals::{get_keymint_device, DB};
33
34/// Implementation of the IRemoteProvisioning service.
35pub struct RemoteProvisioningService {
36 // TODO(b/179222809): Add the remote provisioner hal aidl interface when available
37}
38
39impl RemoteProvisioningService {
40 /// Creates a new instance of the remote provisioning service
Stephen Crane221bbb52020-12-16 15:52:10 -080041 pub fn new_native_binder() -> Result<Strong<dyn IRemoteProvisioning>> {
Max Bires148c08e2020-10-13 13:41:41 -070042 let result = BnRemoteProvisioning::new_binder(Self {});
43 Ok(result)
44 }
45
46 /// Populates the AttestationPoolStatus parcelable with information about how many
47 /// certs will be expiring by the date provided in `expired_by` along with how many
48 /// keys have not yet been assigned.
49 pub fn get_pool_status(
50 &self,
51 expired_by: i64,
52 sec_level: SecurityLevel,
53 ) -> Result<AttestationPoolStatus> {
54 let (_, _, uuid) = get_keymint_device(&sec_level)?;
55 DB.with::<_, Result<AttestationPoolStatus>>(|db| {
56 let mut db = db.borrow_mut();
57 Ok(db.get_attestation_pool_status(expired_by, &uuid)?)
58 })
59 }
60
61 /// Generates a CBOR blob which will be assembled by the calling code into a larger
62 /// CBOR blob intended for delivery to a provisioning serever. This blob will contain
63 /// `num_csr` certificate signing requests for attestation keys generated in the TEE,
64 /// along with a server provided `eek` and `challenge`. The endpoint encryption key will
65 /// be used to encrypt the sensitive contents being transmitted to the server, and the
66 /// challenge will ensure freshness. A `test_mode` flag will instruct the remote provisioning
67 /// HAL if it is okay to accept EEKs that aren't signed by something that chains back to the
68 /// baked in root of trust in the underlying IRemotelyProvisionedComponent instance.
69 pub fn generate_csr(
70 &self,
71 _test_mode: bool,
72 _num_csr: i32,
73 _eek: &[u8],
74 _challenge: &[u8],
75 _sec_level: SecurityLevel,
76 ) -> Result<Vec<u8>> {
77 // TODO(b/179222809): implement with actual remote provisioner AIDL when available. For now
78 // it isnice to have some junk values
79 Ok(vec![0, 1, 3, 3])
80 }
81
82 /// Provisions a certificate chain for a key whose CSR was included in generate_csr. The
83 /// `public_key` is used to index into the SQL database in order to insert the `certs` blob
84 /// which represents a PEM encoded X.509 certificate chain. The `expiration_date` is provided
85 /// as a convenience from the caller to avoid having to parse the certificates semantically
86 /// here.
87 pub fn provision_cert_chain(
88 &self,
89 public_key: &[u8],
90 certs: &[u8],
91 expiration_date: i64,
92 sec_level: SecurityLevel,
93 ) -> Result<()> {
94 DB.with::<_, Result<()>>(|db| {
95 let mut db = db.borrow_mut();
96 let (_, _, uuid) = get_keymint_device(&sec_level)?;
97 Ok(db.store_signed_attestation_certificate_chain(
98 public_key,
99 certs, /* DER encoded certificate chain */
100 expiration_date,
101 &uuid,
102 )?)
103 })
104 }
105
106 /// Submits a request to the Remote Provisioner HAL to generate a signing key pair.
107 /// `is_test_mode` indicates whether or not the returned public key should be marked as being
108 /// for testing in order to differentiate them from private keys. If the call is successful,
109 /// the key pair is then added to the database.
110 pub fn generate_key_pair(&self, _is_test_mode: bool, _sec_level: SecurityLevel) -> Result<()> {
111 Ok(())
112 }
113}
114
115impl binder::Interface for RemoteProvisioningService {}
116
117// Implementation of IRemoteProvisioning. See AIDL spec at
118// :aidl/android/security/remoteprovisioning/IRemoteProvisioning.aidl
119impl IRemoteProvisioning for RemoteProvisioningService {
120 fn getPoolStatus(
121 &self,
122 expired_by: i64,
123 sec_level: SecurityLevel,
124 ) -> binder::public_api::Result<AttestationPoolStatus> {
125 map_or_log_err(self.get_pool_status(expired_by, sec_level), Ok)
126 }
127
128 fn generateCsr(
129 &self,
130 test_mode: bool,
131 num_csr: i32,
132 eek: &[u8],
133 challenge: &[u8],
134 sec_level: SecurityLevel,
135 ) -> binder::public_api::Result<Vec<u8>> {
136 map_or_log_err(self.generate_csr(test_mode, num_csr, eek, challenge, sec_level), Ok)
137 }
138
139 fn provisionCertChain(
140 &self,
141 public_key: &[u8],
142 certs: &[u8],
143 expiration_date: i64,
144 sec_level: SecurityLevel,
145 ) -> binder::public_api::Result<()> {
146 map_or_log_err(self.provision_cert_chain(public_key, certs, expiration_date, sec_level), Ok)
147 }
148
149 fn generateKeyPair(
150 &self,
151 is_test_mode: bool,
152 sec_level: SecurityLevel,
153 ) -> binder::public_api::Result<()> {
154 map_or_log_err(self.generate_key_pair(is_test_mode, sec_level), Ok)
155 }
156}