1 /* 2 * Copyright (C) 2016 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 17 #ifndef KEYSTORE_KEYSTORE_UTILS_H_ 18 #define KEYSTORE_KEYSTORE_UTILS_H_ 19 20 #include <cstdint> 21 #include <vector> 22 23 #include <openssl/evp.h> 24 #include <openssl/pem.h> 25 26 #include <memory> 27 28 #include <keystore/keymaster_types.h> 29 30 size_t readFully(int fd, uint8_t* data, size_t size); 31 size_t writeFully(int fd, uint8_t* data, size_t size); 32 33 void add_legacy_key_authorizations(int keyType, keystore::AuthorizationSet* params); 34 35 /** 36 * Returns the app ID (in the Android multi-user sense) for the current 37 * UNIX UID. 38 */ 39 uid_t get_app_id(uid_t uid); 40 41 /** 42 * Returns the user ID (in the Android multi-user sense) for the current 43 * UNIX UID. 44 */ 45 uid_t get_user_id(uid_t uid); 46 47 struct EVP_PKEY_Delete { operatorEVP_PKEY_Delete48 void operator()(EVP_PKEY* p) const { EVP_PKEY_free(p); } 49 }; 50 typedef std::unique_ptr<EVP_PKEY, EVP_PKEY_Delete> Unique_EVP_PKEY; 51 52 struct PKCS8_PRIV_KEY_INFO_Delete { operatorPKCS8_PRIV_KEY_INFO_Delete53 void operator()(PKCS8_PRIV_KEY_INFO* p) const { PKCS8_PRIV_KEY_INFO_free(p); } 54 }; 55 typedef std::unique_ptr<PKCS8_PRIV_KEY_INFO, PKCS8_PRIV_KEY_INFO_Delete> Unique_PKCS8_PRIV_KEY_INFO; 56 57 class Blob; 58 59 // Tags for audit logging. Be careful and don't log sensitive data. 60 // Should be in sync with frameworks/base/core/java/android/app/admin/SecurityLogTags.logtags 61 constexpr int SEC_TAG_KEY_DESTROYED = 210026; 62 constexpr int SEC_TAG_KEY_INTEGRITY_VIOLATION = 210032; 63 constexpr int SEC_TAG_AUTH_KEY_GENERATED = 210024; 64 constexpr int SEC_TAG_KEY_IMPORTED = 210025; 65 66 void log_key_integrity_violation(const char* name, uid_t uid); 67 68 namespace keystore { 69 70 hidl_vec<uint8_t> blob2hidlVec(const Blob& blob); 71 72 SecurityLevel flagsToSecurityLevel(int32_t flags); 73 uint32_t securityLevelToFlags(SecurityLevel secLevel); 74 75 } // namespace keystore 76 77 #endif // KEYSTORE_KEYSTORE_UTILS_H_ 78