From 46d6fd0ffa7e35cd07d99f9c54e8054eff315c7e Mon Sep 17 00:00:00 2001 From: Joel Galenson Date: Thu, 19 Nov 2020 17:58:33 -0800 Subject: [PATCH] Make Keystore2's crypto operations their own crate This cleanly separates out the underlying C++ code and allows clients to depend only on the safe wrapper. Test: keystore2_crypto_test Test: keystore2_crypto_test_rust Change-Id: I730ebe22ac66287a5650a36b7aeb61c69172e0f8 --- keystore2/Android.bp | 42 ---------- keystore2/src/crypto/Android.bp | 84 +++++++++++++++++++ .../src/{ => crypto}/certificate_utils.cpp | 0 keystore2/src/{ => crypto}/crypto.cpp | 0 keystore2/src/{ => crypto}/crypto.hpp | 0 .../crypto}/include/certificate_utils.h | 0 keystore2/src/{crypto.rs => crypto/lib.rs} | 3 + .../tests/certificate_utils_test.cpp | 0 .../src/{ => crypto}/tests/gtest_main.cpp | 0 keystore2/src/{ => crypto}/tests/test_keys.h | 0 keystore2/src/lib.rs | 1 - 11 files changed, 87 insertions(+), 43 deletions(-) create mode 100644 keystore2/src/crypto/Android.bp rename keystore2/src/{ => crypto}/certificate_utils.cpp (100%) rename keystore2/src/{ => crypto}/crypto.cpp (100%) rename keystore2/src/{ => crypto}/crypto.hpp (100%) rename keystore2/{ => src/crypto}/include/certificate_utils.h (100%) rename keystore2/src/{crypto.rs => crypto/lib.rs} (95%) rename keystore2/src/{ => crypto}/tests/certificate_utils_test.cpp (100%) rename keystore2/src/{ => crypto}/tests/gtest_main.cpp (100%) rename keystore2/src/{ => crypto}/tests/test_keys.h (100%) diff --git a/keystore2/Android.bp b/keystore2/Android.bp index 40d91c21..9552df5c 100644 --- a/keystore2/Android.bp +++ b/keystore2/Android.bp @@ -44,7 +44,6 @@ rust_test { "libandroid_logger", "libanyhow", "libbinder_rs", - "libkeystore2_crypto_bindgen", "libkeystore2_selinux", "liblazy_static", "liblibsqlite3_sys", @@ -52,28 +51,6 @@ rust_test { "librusqlite", "libthiserror", ], - shared_libs: ["libkeystore2_crypto"], -} - -cc_library { - name: "libkeystore2_crypto", - srcs: [ - "src/crypto.cpp", - "src/certificate_utils.cpp", - ], - export_include_dirs: ["include",], - shared_libs: [ - "libcrypto", - "liblog", - ], -} - -rust_bindgen { - name: "libkeystore2_crypto_bindgen", - wrapper_src: "src/crypto.hpp", - crate_name: "keystore2_crypto_bindgen", - source_stem: "bindings", - host_supported: true, } rust_binary { @@ -87,22 +64,3 @@ rust_binary { ], init_rc: ["keystore2.rc"], } - -cc_test { - cflags: [ - "-Wall", - "-Werror", - "-Wextra", - ], - srcs: [ - "src/tests/certificate_utils_test.cpp", - "src/tests/gtest_main.cpp", - ], - static_libs: [ - "libkeystore2_crypto", - ], - shared_libs: [ - "libcrypto", - ], - name: "keystore2_crypto_test", -} diff --git a/keystore2/src/crypto/Android.bp b/keystore2/src/crypto/Android.bp new file mode 100644 index 00000000..061cf9ac --- /dev/null +++ b/keystore2/src/crypto/Android.bp @@ -0,0 +1,84 @@ +// Copyright 2020, 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. + +rust_library { + name: "libkeystore2_crypto_rust", + crate_name: "keystore2_crypto", + srcs: ["lib.rs"], + rustlibs: [ + "libkeystore2_crypto_bindgen", + ], + static_libs: ["libkeystore2_crypto"], + shared_libs: ["libcrypto"], +} + +cc_library { + name: "libkeystore2_crypto", + srcs: [ + "crypto.cpp", + "certificate_utils.cpp", + ], + export_include_dirs: ["include",], + shared_libs: [ + "libcrypto", + "liblog", + ], +} + +rust_bindgen { + name: "libkeystore2_crypto_bindgen", + wrapper_src: "crypto.hpp", + crate_name: "keystore2_crypto_bindgen", + source_stem: "bindings", + host_supported: true, +} + +rust_test { + name: "keystore2_crypto_test_rust", + crate_name: "keystore2_crypto_test_rust", + srcs: ["lib.rs"], + test_suites: ["general-tests"], + auto_gen_config: true, + rustlibs: [ + "libkeystore2_crypto_bindgen", + "libkeystore2_crypto_rust", + ], + static_libs: [ + "libkeystore2_crypto", + ], + shared_libs: [ + "libc++", + "libcrypto", + "liblog", + ], +} + +cc_test { + cflags: [ + "-Wall", + "-Werror", + "-Wextra", + ], + srcs: [ + "tests/certificate_utils_test.cpp", + "tests/gtest_main.cpp", + ], + static_libs: [ + "libkeystore2_crypto", + ], + shared_libs: [ + "libcrypto", + ], + name: "keystore2_crypto_test", +} diff --git a/keystore2/src/certificate_utils.cpp b/keystore2/src/crypto/certificate_utils.cpp similarity index 100% rename from keystore2/src/certificate_utils.cpp rename to keystore2/src/crypto/certificate_utils.cpp diff --git a/keystore2/src/crypto.cpp b/keystore2/src/crypto/crypto.cpp similarity index 100% rename from keystore2/src/crypto.cpp rename to keystore2/src/crypto/crypto.cpp diff --git a/keystore2/src/crypto.hpp b/keystore2/src/crypto/crypto.hpp similarity index 100% rename from keystore2/src/crypto.hpp rename to keystore2/src/crypto/crypto.hpp diff --git a/keystore2/include/certificate_utils.h b/keystore2/src/crypto/include/certificate_utils.h similarity index 100% rename from keystore2/include/certificate_utils.h rename to keystore2/src/crypto/include/certificate_utils.h diff --git a/keystore2/src/crypto.rs b/keystore2/src/crypto/lib.rs similarity index 95% rename from keystore2/src/crypto.rs rename to keystore2/src/crypto/lib.rs index b25b6482..6ec5edb6 100644 --- a/keystore2/src/crypto.rs +++ b/keystore2/src/crypto/lib.rs @@ -12,6 +12,9 @@ // See the License for the specific language governing permissions and // limitations under the License. +// TODO: Once this is complete, remove this and document everything public. +#![allow(missing_docs)] + #[cfg(test)] mod tests { diff --git a/keystore2/src/tests/certificate_utils_test.cpp b/keystore2/src/crypto/tests/certificate_utils_test.cpp similarity index 100% rename from keystore2/src/tests/certificate_utils_test.cpp rename to keystore2/src/crypto/tests/certificate_utils_test.cpp diff --git a/keystore2/src/tests/gtest_main.cpp b/keystore2/src/crypto/tests/gtest_main.cpp similarity index 100% rename from keystore2/src/tests/gtest_main.cpp rename to keystore2/src/crypto/tests/gtest_main.cpp diff --git a/keystore2/src/tests/test_keys.h b/keystore2/src/crypto/tests/test_keys.h similarity index 100% rename from keystore2/src/tests/test_keys.h rename to keystore2/src/crypto/tests/test_keys.h diff --git a/keystore2/src/lib.rs b/keystore2/src/lib.rs index 3e13c5f5..067399e0 100644 --- a/keystore2/src/lib.rs +++ b/keystore2/src/lib.rs @@ -14,7 +14,6 @@ //! This crate implements the Android Keystore 2.0 service. -mod crypto; pub mod database; pub mod error; pub mod globals;