Alan Stokes | 337874a | 2021-06-16 16:49:32 +0100 | [diff] [blame] | 1 | // Copyright 2021, 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 | //! Provides a binder service for key generation & verification for CompOs. We assume we have |
| 16 | //! access to Keystore in the VM, but not persistent storage; instead the host stores the key |
| 17 | //! on our behalf via this service. |
| 18 | |
| 19 | use android_hardware_security_keymint::aidl::android::hardware::security::keymint::{ |
| 20 | Algorithm::Algorithm, Digest::Digest, KeyParameter::KeyParameter, |
| 21 | KeyParameterValue::KeyParameterValue, KeyPurpose::KeyPurpose, PaddingMode::PaddingMode, |
| 22 | SecurityLevel::SecurityLevel, Tag::Tag, |
| 23 | }; |
| 24 | use android_system_keystore2::aidl::android::system::keystore2::{ |
| 25 | Domain::Domain, IKeystoreSecurityLevel::IKeystoreSecurityLevel, |
| 26 | IKeystoreService::IKeystoreService, KeyDescriptor::KeyDescriptor, |
| 27 | }; |
| 28 | use anyhow::{anyhow, Context, Result}; |
| 29 | use compos_aidl_interface::aidl::com::android::compos::{ |
Alan Stokes | 8ccebf1 | 2021-07-14 12:04:31 +0100 | [diff] [blame^] | 30 | CompOsKeyData::CompOsKeyData, ICompOsKeyService::ICompOsKeyService, |
Alan Stokes | 337874a | 2021-06-16 16:49:32 +0100 | [diff] [blame] | 31 | }; |
| 32 | use compos_aidl_interface::binder::{ |
Alan Stokes | 8ccebf1 | 2021-07-14 12:04:31 +0100 | [diff] [blame^] | 33 | self, get_interface, ExceptionCode, Interface, Status, Strong, |
Alan Stokes | 337874a | 2021-06-16 16:49:32 +0100 | [diff] [blame] | 34 | }; |
Alan Stokes | 8ccebf1 | 2021-07-14 12:04:31 +0100 | [diff] [blame^] | 35 | use log::warn; |
Alan Stokes | 337874a | 2021-06-16 16:49:32 +0100 | [diff] [blame] | 36 | use ring::rand::{SecureRandom, SystemRandom}; |
| 37 | use ring::signature; |
| 38 | use scopeguard::ScopeGuard; |
| 39 | use std::ffi::CString; |
Alan Stokes | 337874a | 2021-06-16 16:49:32 +0100 | [diff] [blame] | 40 | |
Alan Stokes | 337874a | 2021-06-16 16:49:32 +0100 | [diff] [blame] | 41 | const KEYSTORE_SERVICE_NAME: &str = "android.system.keystore2.IKeystoreService/default"; |
| 42 | const COMPOS_NAMESPACE: i64 = 101; |
| 43 | const PURPOSE_SIGN: KeyParameter = |
| 44 | KeyParameter { tag: Tag::PURPOSE, value: KeyParameterValue::KeyPurpose(KeyPurpose::SIGN) }; |
| 45 | const ALGORITHM: KeyParameter = |
| 46 | KeyParameter { tag: Tag::ALGORITHM, value: KeyParameterValue::Algorithm(Algorithm::RSA) }; |
| 47 | const PADDING: KeyParameter = KeyParameter { |
| 48 | tag: Tag::PADDING, |
| 49 | value: KeyParameterValue::PaddingMode(PaddingMode::RSA_PKCS1_1_5_SIGN), |
| 50 | }; |
| 51 | const DIGEST: KeyParameter = |
| 52 | KeyParameter { tag: Tag::DIGEST, value: KeyParameterValue::Digest(Digest::SHA_2_256) }; |
| 53 | const KEY_SIZE: KeyParameter = |
| 54 | KeyParameter { tag: Tag::KEY_SIZE, value: KeyParameterValue::Integer(2048) }; |
| 55 | const EXPONENT: KeyParameter = |
| 56 | KeyParameter { tag: Tag::RSA_PUBLIC_EXPONENT, value: KeyParameterValue::LongInteger(65537) }; |
| 57 | const NO_AUTH_REQUIRED: KeyParameter = |
| 58 | KeyParameter { tag: Tag::NO_AUTH_REQUIRED, value: KeyParameterValue::BoolValue(true) }; |
| 59 | |
| 60 | const KEY_DESCRIPTOR: KeyDescriptor = |
| 61 | KeyDescriptor { domain: Domain::BLOB, nspace: COMPOS_NAMESPACE, alias: None, blob: None }; |
| 62 | |
Alan Stokes | 8ccebf1 | 2021-07-14 12:04:31 +0100 | [diff] [blame^] | 63 | pub struct CompOsKeyService { |
Alan Stokes | 337874a | 2021-06-16 16:49:32 +0100 | [diff] [blame] | 64 | random: SystemRandom, |
Alan Stokes | 337874a | 2021-06-16 16:49:32 +0100 | [diff] [blame] | 65 | security_level: Strong<dyn IKeystoreSecurityLevel>, |
| 66 | } |
| 67 | |
| 68 | impl Interface for CompOsKeyService {} |
| 69 | |
| 70 | impl ICompOsKeyService for CompOsKeyService { |
| 71 | fn generateSigningKey(&self) -> binder::Result<CompOsKeyData> { |
| 72 | self.do_generate() |
| 73 | .map_err(|e| new_binder_exception(ExceptionCode::ILLEGAL_STATE, e.to_string())) |
| 74 | } |
| 75 | |
| 76 | fn verifySigningKey(&self, key_blob: &[u8], public_key: &[u8]) -> binder::Result<bool> { |
| 77 | Ok(if let Err(e) = self.do_verify(key_blob, public_key) { |
| 78 | warn!("Signing key verification failed: {}", e.to_string()); |
| 79 | false |
| 80 | } else { |
| 81 | true |
| 82 | }) |
| 83 | } |
Alan Stokes | c3bab54 | 2021-07-06 15:48:33 +0100 | [diff] [blame] | 84 | |
| 85 | fn sign(&self, key_blob: &[u8], data: &[u8]) -> binder::Result<Vec<u8>> { |
| 86 | self.do_sign(key_blob, data) |
| 87 | .map_err(|e| new_binder_exception(ExceptionCode::ILLEGAL_STATE, e.to_string())) |
| 88 | } |
Alan Stokes | 337874a | 2021-06-16 16:49:32 +0100 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | /// Constructs a new Binder error `Status` with the given `ExceptionCode` and message. |
| 92 | fn new_binder_exception<T: AsRef<str>>(exception: ExceptionCode, message: T) -> Status { |
| 93 | Status::new_exception(exception, CString::new(message.as_ref()).ok().as_deref()) |
| 94 | } |
| 95 | |
| 96 | impl CompOsKeyService { |
Alan Stokes | 8ccebf1 | 2021-07-14 12:04:31 +0100 | [diff] [blame^] | 97 | pub fn new() -> Result<Self> { |
| 98 | let keystore_service = get_interface::<dyn IKeystoreService>(KEYSTORE_SERVICE_NAME) |
| 99 | .context("No Keystore service")?; |
| 100 | |
| 101 | Ok(Self { |
Alan Stokes | 337874a | 2021-06-16 16:49:32 +0100 | [diff] [blame] | 102 | random: SystemRandom::new(), |
Alan Stokes | 4a489d4 | 2021-06-28 10:12:05 +0100 | [diff] [blame] | 103 | security_level: keystore_service |
| 104 | .getSecurityLevel(SecurityLevel::TRUSTED_ENVIRONMENT) |
Alan Stokes | 8ccebf1 | 2021-07-14 12:04:31 +0100 | [diff] [blame^] | 105 | .context("Getting SecurityLevel failed")?, |
| 106 | }) |
Alan Stokes | 337874a | 2021-06-16 16:49:32 +0100 | [diff] [blame] | 107 | } |
| 108 | |
Alan Stokes | 337874a | 2021-06-16 16:49:32 +0100 | [diff] [blame] | 109 | fn do_generate(&self) -> Result<CompOsKeyData> { |
| 110 | let key_parameters = |
| 111 | [PURPOSE_SIGN, ALGORITHM, PADDING, DIGEST, KEY_SIZE, EXPONENT, NO_AUTH_REQUIRED]; |
| 112 | let attestation_key = None; |
| 113 | let flags = 0; |
| 114 | let entropy = []; |
| 115 | |
| 116 | let key_metadata = self |
Alan Stokes | 4a489d4 | 2021-06-28 10:12:05 +0100 | [diff] [blame] | 117 | .security_level |
Alan Stokes | 337874a | 2021-06-16 16:49:32 +0100 | [diff] [blame] | 118 | .generateKey(&KEY_DESCRIPTOR, attestation_key, &key_parameters, flags, &entropy) |
| 119 | .context("Generating key failed")?; |
| 120 | |
| 121 | if let (Some(certificate), Some(blob)) = (key_metadata.certificate, key_metadata.key.blob) { |
| 122 | Ok(CompOsKeyData { certificate, keyBlob: blob }) |
| 123 | } else { |
| 124 | Err(anyhow!("Missing cert or blob")) |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | fn do_verify(&self, key_blob: &[u8], public_key: &[u8]) -> Result<()> { |
| 129 | let mut data = [0u8; 32]; |
| 130 | self.random.fill(&mut data).context("No random data")?; |
| 131 | |
Alan Stokes | c3bab54 | 2021-07-06 15:48:33 +0100 | [diff] [blame] | 132 | let signature = self.do_sign(key_blob, &data)?; |
Alan Stokes | 337874a | 2021-06-16 16:49:32 +0100 | [diff] [blame] | 133 | |
| 134 | let public_key = |
| 135 | signature::UnparsedPublicKey::new(&signature::RSA_PKCS1_2048_8192_SHA256, public_key); |
| 136 | public_key.verify(&data, &signature).context("Signature verification failed")?; |
| 137 | |
| 138 | Ok(()) |
| 139 | } |
| 140 | |
Alan Stokes | c3bab54 | 2021-07-06 15:48:33 +0100 | [diff] [blame] | 141 | fn do_sign(&self, key_blob: &[u8], data: &[u8]) -> Result<Vec<u8>> { |
Alan Stokes | 337874a | 2021-06-16 16:49:32 +0100 | [diff] [blame] | 142 | let key_descriptor = KeyDescriptor { blob: Some(key_blob.to_vec()), ..KEY_DESCRIPTOR }; |
| 143 | let operation_parameters = [PURPOSE_SIGN, ALGORITHM, PADDING, DIGEST]; |
| 144 | let forced = false; |
| 145 | |
| 146 | let response = self |
Alan Stokes | 4a489d4 | 2021-06-28 10:12:05 +0100 | [diff] [blame] | 147 | .security_level |
Alan Stokes | 337874a | 2021-06-16 16:49:32 +0100 | [diff] [blame] | 148 | .createOperation(&key_descriptor, &operation_parameters, forced) |
| 149 | .context("Creating key failed")?; |
| 150 | let operation = scopeguard::guard( |
| 151 | response.iOperation.ok_or_else(|| anyhow!("No operation created"))?, |
| 152 | |op| op.abort().unwrap_or_default(), |
| 153 | ); |
| 154 | |
| 155 | if response.operationChallenge.is_some() { |
| 156 | return Err(anyhow!("Key requires user authorization")); |
| 157 | } |
| 158 | |
| 159 | let signature = operation.finish(Some(&data), None).context("Signing failed")?; |
| 160 | // Operation has finished, we're no longer responsible for aborting it |
| 161 | ScopeGuard::into_inner(operation); |
| 162 | |
| 163 | signature.ok_or_else(|| anyhow!("No signature returned")) |
| 164 | } |
| 165 | } |