Migrate off keystore

Implement our own keypair generation and signing (using BoringSSL) and
our own private key blob protection (using Ring). This includes
replacing the old compos_key_service with the new signing_key.

Use DICE as the source of the VM secret used to protect the private
key instead of assuming keystore has one.

Changed compsvc to return the RSAPublicKey directly. Previously we
returned the self-signed cert from Keystore, and composd then
extracted the public key. As a result composd no longer needs any
native helper code to call BoringSSL; however now compsvc does.

Removed similarly redundant key-extraction code from compos_key_cmd.

Create SystemRandom when we need it rather than having it as a field;
it's stateless anyway.

Bug: 214233409
Test: atest ComposKeyTestCase compsvc_device_tests
Change-Id: I8b14fe2acdf43f49d45e2d32d4b6f482bd420eee
diff --git a/compos/composd/native/Android.bp b/compos/composd/native/Android.bp
index 135f4d4..ccd8651 100644
--- a/compos/composd/native/Android.bp
+++ b/compos/composd/native/Android.bp
@@ -8,40 +8,10 @@
     srcs: ["lib.rs"],
     rustlibs: [
         "libanyhow",
-        "libcxx",
         "liblibc",
     ],
-    static_libs: [
-        "libcomposd_native_cpp",
-    ],
     shared_libs: [
         "libartpalette-system",
-        "libcrypto",
     ],
     apex_available: ["com.android.compos"],
 }
-
-cc_library_static {
-    name: "libcomposd_native_cpp",
-    srcs: ["composd_native.cpp"],
-    shared_libs: ["libcrypto"],
-    generated_headers: ["composd_native_header"],
-    generated_sources: ["composd_native_code"],
-    apex_available: ["com.android.compos"],
-}
-
-genrule {
-    name: "composd_native_code",
-    tools: ["cxxbridge"],
-    cmd: "$(location cxxbridge) $(in) >> $(out)",
-    srcs: ["lib.rs"],
-    out: ["composd_native_cxx_generated.cc"],
-}
-
-genrule {
-    name: "composd_native_header",
-    tools: ["cxxbridge"],
-    cmd: "$(location cxxbridge) $(in) --header >> $(out)",
-    srcs: ["lib.rs"],
-    out: ["lib.rs.h"],
-}
diff --git a/compos/composd/native/composd_native.cpp b/compos/composd/native/composd_native.cpp
deleted file mode 100644
index ebed816..0000000
--- a/compos/composd/native/composd_native.cpp
+++ /dev/null
@@ -1,66 +0,0 @@
-/*
- * Copyright (C) 2021 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *      http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-#include "composd_native.h"
-
-#include <openssl/evp.h>
-#include <openssl/mem.h>
-#include <openssl/sha.h>
-#include <openssl/x509.h>
-
-#include <algorithm>
-#include <iterator>
-
-using rust::Slice;
-using rust::String;
-
-namespace {
-KeyResult make_error(const char* message) {
-    return KeyResult{{}, message};
-}
-} // namespace
-
-KeyResult extract_rsa_public_key(rust::Slice<const uint8_t> der_certificate) {
-    auto data = der_certificate.data();
-    bssl::UniquePtr<X509> x509(d2i_X509(nullptr, &data, der_certificate.size()));
-    if (!x509) {
-        return make_error("Failed to parse certificate");
-    }
-    if (data != der_certificate.data() + der_certificate.size()) {
-        return make_error("Certificate has unexpected trailing data");
-    }
-
-    bssl::UniquePtr<EVP_PKEY> pkey(X509_get_pubkey(x509.get()));
-    if (EVP_PKEY_base_id(pkey.get()) != EVP_PKEY_RSA) {
-        return make_error("Subject key is not RSA");
-    }
-    RSA* rsa = EVP_PKEY_get0_RSA(pkey.get());
-    if (!rsa) {
-        return make_error("Failed to extract RSA key");
-    }
-
-    uint8_t* out = nullptr;
-    int size = i2d_RSAPublicKey(rsa, &out);
-    if (size < 0 || !out) {
-        return make_error("Failed to convert to RSAPublicKey");
-    }
-    bssl::UniquePtr<uint8_t> buffer(out);
-
-    KeyResult result;
-    result.key.reserve(size);
-    std::copy(out, out + size, std::back_inserter(result.key));
-    return result;
-}
diff --git a/compos/composd/native/composd_native.h b/compos/composd/native/composd_native.h
deleted file mode 100644
index 112ef73..0000000
--- a/compos/composd/native/composd_native.h
+++ /dev/null
@@ -1,21 +0,0 @@
-/*
- * Copyright (C) 2021 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *      http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-#pragma once
-
-#include "lib.rs.h"
-
-KeyResult extract_rsa_public_key(rust::Slice<const uint8_t> der_certificate);
diff --git a/compos/composd/native/lib.rs b/compos/composd/native/lib.rs
index cbec7fd..042eb2a 100644
--- a/compos/composd/native/lib.rs
+++ b/compos/composd/native/lib.rs
@@ -15,28 +15,6 @@
 //! Native helpers for composd.
 
 pub use art::*;
-pub use crypto::*;
-
-#[cxx::bridge]
-mod crypto {
-    /// Contains either a key or a reason why the key could not be extracted.
-    struct KeyResult {
-        /// The extracted key. If empty, the attempt to extract the key failed.
-        key: Vec<u8>,
-        /// A description of what went wrong if the attempt failed.
-        error: String,
-    }
-
-    unsafe extern "C++" {
-        include!("composd_native.h");
-
-        // SAFETY: The C++ implementation manages its own memory, and does not retain or abuse
-        // the der_certificate reference. cxx handles the mapping of the return value.
-
-        /// Parse the supplied DER X.509 certificate and extract the subject's RsaPublicKey.
-        fn extract_rsa_public_key(der_certificate: &[u8]) -> KeyResult;
-    }
-}
 
 mod art {
     use anyhow::{anyhow, Result};
diff --git a/compos/composd/src/instance_starter.rs b/compos/composd/src/instance_starter.rs
index 4fed98a..a886584 100644
--- a/compos/composd/src/instance_starter.rs
+++ b/compos/composd/src/instance_starter.rs
@@ -138,13 +138,7 @@
 
         let key_data = service.generateSigningKey().context("Generating signing key")?;
         fs::write(&self.key_blob, &key_data.keyBlob).context("Writing key blob")?;
-
-        let key_result = composd_native::extract_rsa_public_key(&key_data.certificate);
-        let rsa_public_key = key_result.key;
-        if rsa_public_key.is_empty() {
-            bail!("Failed to extract public key from certificate: {}", key_result.error);
-        }
-        fs::write(&self.public_key, &rsa_public_key).context("Writing public key")?;
+        fs::write(&self.public_key, &key_data.publicKey).context("Writing public key")?;
 
         // Unlike when starting an existing instance, we don't need to verify the key, since we
         // just generated it and have it in memory.