Add native code for cert munging.

Compsvc returns a self-signed cert (from Keystore), but we only want
the public key. Extracting this in Rust is non-trivial, so instead we
use existing support in BoringSSL in native code. (The details are
copied from compos_key_cmd.cpp, which in turn were copied from the
now-deleted FakeCompOS in odsign.)

We could alternatively do this in compsvc itself, but I was slightly
more reluctant to introduce native code there.

Bug: 186126194
Test: Run composd_cmd twice, check it accepts the key pair it generated.
Change-Id: I3faab9a7ada149d7f2776c2fb4d2656837c95e6f
diff --git a/compos/composd/src/instance_starter.rs b/compos/composd/src/instance_starter.rs
index 5352250..ec95ff8 100644
--- a/compos/composd/src/instance_starter.rs
+++ b/compos/composd/src/instance_starter.rs
@@ -74,7 +74,7 @@
         let compos_instance = self.start_existing_instance();
         match compos_instance {
             Ok(_) => return compos_instance,
-            Err(e) => warn!("Failed to start {}: {}", self.instance_name, e),
+            Err(e) => warn!("Failed to start: {}", e),
         }
 
         self.start_new_instance(service)
@@ -84,6 +84,8 @@
         // No point even trying if the files we need aren't there.
         self.check_files_exist()?;
 
+        info!("Starting {} CompOs instance", self.instance_name);
+
         let key_blob = fs::read(&self.key_blob).context("Reading private key blob")?;
         let public_key = fs::read(&self.public_key).context("Reading public key")?;
 
@@ -119,8 +121,13 @@
 
         let key_data = service.generateSigningKey().context("Generating signing key")?;
         fs::write(&self.key_blob, &key_data.keyBlob).context("Writing key blob")?;
-        // TODO: Extract public key from cert
-        fs::write(&self.public_key, &key_data.certificate).context("Writing public key")?;
+
+        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")?;
 
         // We don't need to verify the key, since we just generated it and have it in memory.