keystore_get.cpp 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. /*
  2. * Copyright (C) 2012 The Android Open Source Project
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include <android/security/keystore/IKeystoreService.h>
  17. #include <binder/IServiceManager.h>
  18. #include <keystore/keystore_get.h>
  19. #include <vector>
  20. using namespace android;
  21. using namespace keystore;
  22. ssize_t keystore_get(const char* key, size_t keyLength, uint8_t** value) {
  23. sp<IServiceManager> sm = initdefaultServiceManager();
  24. sp<IBinder> binder = sm->getService(String16("android.security.keystore"));
  25. sp<android::security::keystore::IKeystoreService> service =
  26. interface_cast<android::security::keystore::IKeystoreService>(binder);
  27. if (service == nullptr) {
  28. return -1;
  29. }
  30. ::std::vector<uint8_t> result;
  31. auto ret = service->get(String16(key, keyLength), -1, &result);
  32. if (!ret.isOk()) return -1;
  33. if (value) {
  34. *value = reinterpret_cast<uint8_t*>(malloc(result.size()));
  35. if (!*value) return -1;
  36. memcpy(*value, &result[0], result.size());
  37. }
  38. return result.size();
  39. }