Port to binderized keymaster HAL
This patch ports keystore to the HIDL based binderized keymaster HAL.
Keystore has no more dependencies on legacy keymaster headers, and
therefore data structures, constant declarations, or enums. All
keymaster related data structures and enums used by keystore are the
once defined by the HIDL based keymaster HAL definition. In the process
of porting, keystore underwent some changes:
* Keystore got a new implementation of AuthorizationSet that is fully
based on the new HIDL data structures. Key parameters are now either
organised as AuthorizationSets or hidl_vec<KeyParameter>. (Formerly,
this was a mixture of keymaster's AuthorizationSet,
std::vec<keymaster_key_param_t>, and keymaster_key_param_set_t.) The
former is used for memory management and provides algorithms for
assembling, joining, and subtracting sets of parameters. The latter
is used as wire format for the HAL IPC; it can wrap the memory owned
by an AuthorizationSet for this purpose. The AuthorizationSet is
accompanied by a new implementation of type safe functions for
creating and accessing tagged key parameters,
Authorizations (keystore/keymaster_tags.h).
* A new type (KSSReturnCode) was introduced that wraps keystore service
response codes. Keystore has two sets of error codes. ErrorCode
errors are less than 0 and use 0 as success value. ResponseCode
errors are greater than zero and use 1 as success value. This patch
changes ResponseCode to be an enum class so that is no longer
assignable to int without a cast. The new return type can only be
initialized by ResponseCode or ErrorCode and when accessed as int32_t,
which happens on serialization when the response is send to a client,
the success values are coalesced onto 1 as expected by the
clients. KSSreturnCode is also comparable to ResponseCode and
ErrorCode, and the predicate isOk() returns true if it was initialized
with either ErrorCode::OK (0) or ReponseCode::NO_ERROR (1).
* A bug was fixed, that caused the keystore verify function to return
success, regardless of the input, internal errors, or lack of
permissions.
* The marshalling code in IKeystoreService.cpp was rewritten. For data
structures that are known to keymaster, the client facing side of
keystore uses HIDL based data structures as (target) source
for (un)marshaling to avoid further conversion. hidl_vecs are used to
wrap parcel memory without copying and taking ownership where
possible.
* Explicit use of malloc is reduced (malloc was required by the C nature
of the old HAL). The new implementations avoid explicit use of
malloc/new and waive the use of pointers for return values. Instead,
functions return by value objects that take ownership of secondary
memory allocations where required.
Test: runtest --path=cts/tests/tests/keystore/src/android/keystore/cts
Bug: 32020919
Change-Id: I59d3a0f4a6bdf6bb3bbf791ad8827c463effa286
diff --git a/keystore-engine/android_engine.cpp b/keystore-engine/android_engine.cpp
index de67df2..6295873 100644
--- a/keystore-engine/android_engine.cpp
+++ b/keystore-engine/android_engine.cpp
@@ -20,6 +20,8 @@
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
+#define LOG_TAG "keystore-engine"
+
#include <UniquePtr.h>
#include <sys/socket.h>
@@ -39,10 +41,12 @@
#include <binder/IServiceManager.h>
#include <keystore/keystore.h>
#include <keystore/IKeystoreService.h>
+#include <keystore/keystore_hidl_support.h>
using namespace android;
namespace {
+using namespace keystore;
extern const RSA_METHOD keystore_rsa_method;
extern const ECDSA_METHOD keystore_ecdsa_method;
@@ -153,41 +157,35 @@
ALOGE("could not contact keystore");
return 0;
}
+ auto inBlob = blob2hidlVec(in ,len);
+ hidl_vec<uint8_t> reply;
- uint8_t* reply = NULL;
- size_t reply_len;
- int32_t ret = service->sign(String16(key_id), in, len, &reply, &reply_len);
- if (ret < 0) {
- ALOGW("There was an error during rsa_decrypt: could not connect");
+ auto ret = service->sign(String16(key_id), inBlob, &reply);
+ if (!ret.isOk()) {
+ ALOGW("Error during sign from keystore: %d", int32_t(ret));
return 0;
- } else if (ret != 0) {
- ALOGW("Error during sign from keystore: %d", ret);
- return 0;
- } else if (reply_len == 0) {
+ } else if (reply.size() == 0) {
ALOGW("No valid signature returned");
- free(reply);
return 0;
}
- if (reply_len > len) {
+ if (reply.size() > len) {
/* The result of the RSA operation can never be larger than the size of
* the modulus so we assume that the result has extra zeros on the
* left. This provides attackers with an oracle, but there's nothing
* that we can do about it here. */
- memcpy(out, reply + reply_len - len, len);
- } else if (reply_len < len) {
+ memcpy(out, &reply[reply.size() - len], len);
+ } else if (reply.size() < len) {
/* If the Keystore implementation returns a short value we assume that
* it's because it removed leading zeros from the left side. This is
* bad because it provides attackers with an oracle but we cannot do
* anything about a broken Keystore implementation here. */
memset(out, 0, len);
- memcpy(out + len - reply_len, reply, reply_len);
+ memcpy(out + len - reply.size(), &reply[0], reply.size());
} else {
- memcpy(out, reply, len);
+ memcpy(out, &reply[0], len);
}
- free(reply);
-
ALOGV("rsa=%p keystore_rsa_priv_dec successful", rsa);
return 1;
}
@@ -253,28 +251,24 @@
size_t ecdsa_size = ECDSA_size(ec_key);
- uint8_t* reply = NULL;
- size_t reply_len;
- int32_t ret = service->sign(String16(reinterpret_cast<const char*>(key_id)),
- digest, digest_len, &reply, &reply_len);
- if (ret < 0) {
- ALOGW("There was an error during ecdsa_sign: could not connect");
+ auto hidlDigest = blob2hidlVec(digest, digest_len);
+ hidl_vec<uint8_t> reply;
+ auto ret = service->sign(String16(reinterpret_cast<const char*>(key_id)),
+ hidlDigest, &reply);
+ if (!ret.isOk()) {
+ ALOGW("Error during sign from keystore: %d", int32_t(ret));
return 0;
- } else if (ret != 0) {
- ALOGW("Error during sign from keystore: %d", ret);
- return 0;
- } else if (reply_len == 0) {
+ } else if (reply.size() == 0) {
ALOGW("No valid signature returned");
- free(reply);
return 0;
- } else if (reply_len > ecdsa_size) {
+ } else if (reply.size() > ecdsa_size) {
ALOGW("Signature is too large");
- free(reply);
return 0;
}
- memcpy(sig, reply, reply_len);
- *sig_len = reply_len;
+ // Reviewer: should't sig_len be checked here? Or is it just assumed that it is at least ecdsa_size?
+ memcpy(sig, &reply[0], reply.size());
+ *sig_len = reply.size();
ALOGV("ecdsa_sign(%p, %u, %p) => success", digest, (unsigned)digest_len,
ec_key);
@@ -410,20 +404,15 @@
return 0;
}
- uint8_t *pubkey = NULL;
- size_t pubkey_len;
- int32_t ret = service->get_pubkey(String16(key_id), &pubkey, &pubkey_len);
- if (ret < 0) {
- ALOGW("could not contact keystore");
- return NULL;
- } else if (ret != 0) {
- ALOGW("keystore reports error: %d", ret);
+ hidl_vec<uint8_t> pubkey;
+ auto ret = service->get_pubkey(String16(key_id), &pubkey);
+ if (!ret.isOk()) {
+ ALOGW("keystore reports error: %d", int32_t(ret));
return NULL;
}
- const uint8_t *inp = pubkey;
- Unique_EVP_PKEY pkey(d2i_PUBKEY(NULL, &inp, pubkey_len));
- free(pubkey);
+ const uint8_t *inp = &pubkey[0];
+ Unique_EVP_PKEY pkey(d2i_PUBKEY(NULL, &inp, pubkey.size()));
if (pkey.get() == NULL) {
ALOGW("Cannot convert pubkey");
return NULL;