blob: 6e232e1c69740be22d39a03868aa8ebc331219ab [file] [log] [blame]
Shawn Willden815e8962020-12-11 13:05:27 +00001/*
2 * Copyright 2020, 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#pragma once
18
Shawn Willden815e8962020-12-11 13:05:27 +000019#include <aidl/android/hardware/security/keymint/Certificate.h>
20#include <aidl/android/hardware/security/keymint/HardwareAuthToken.h>
21#include <aidl/android/hardware/security/keymint/HardwareAuthenticatorType.h>
22#include <aidl/android/hardware/security/keymint/KeyFormat.h>
23#include <aidl/android/hardware/security/keymint/KeyParameter.h>
24#include <aidl/android/hardware/security/keymint/KeyPurpose.h>
25#include <aidl/android/hardware/security/keymint/SecurityLevel.h>
26#include <aidl/android/hardware/security/keymint/Tag.h>
27
28#include <keymaster/keymaster_enforcement.h>
29
Shawn Willden96d4e8c2020-12-07 17:03:54 -070030namespace aidl::android::hardware::security::keymint::km_utils {
Shawn Willden815e8962020-12-11 13:05:27 +000031
32using ::ndk::ScopedAStatus;
33using std::vector;
34
35inline keymaster_tag_t legacy_enum_conversion(const Tag value) {
Shawn Willden96d4e8c2020-12-07 17:03:54 -070036 return static_cast<keymaster_tag_t>(value);
Shawn Willden815e8962020-12-11 13:05:27 +000037}
38
39inline Tag legacy_enum_conversion(const keymaster_tag_t value) {
Shawn Willden96d4e8c2020-12-07 17:03:54 -070040 return static_cast<Tag>(value);
Shawn Willden815e8962020-12-11 13:05:27 +000041}
42
43inline keymaster_purpose_t legacy_enum_conversion(const KeyPurpose value) {
44 return static_cast<keymaster_purpose_t>(value);
45}
46
47inline keymaster_key_format_t legacy_enum_conversion(const KeyFormat value) {
48 return static_cast<keymaster_key_format_t>(value);
49}
50
51inline SecurityLevel legacy_enum_conversion(const keymaster_security_level_t value) {
52 return static_cast<SecurityLevel>(value);
53}
54
55inline hw_authenticator_type_t legacy_enum_conversion(const HardwareAuthenticatorType value) {
56 return static_cast<hw_authenticator_type_t>(value);
57}
58
59inline ScopedAStatus kmError2ScopedAStatus(const keymaster_error_t value) {
60 return (value == KM_ERROR_OK
61 ? ScopedAStatus::ok()
62 : ScopedAStatus(AStatus_fromServiceSpecificError(static_cast<int32_t>(value))));
63}
64
65inline keymaster_tag_type_t typeFromTag(const keymaster_tag_t tag) {
66 return keymaster_tag_get_type(tag);
67}
68
Shawn Willden763166c2021-01-10 19:45:01 -070069KeyParameter kmParam2Aidl(const keymaster_key_param_t& param);
Shawn Willden96d4e8c2020-12-07 17:03:54 -070070vector<KeyParameter> kmParamSet2Aidl(const keymaster_key_param_set_t& set);
Shawn Willden815e8962020-12-11 13:05:27 +000071keymaster_key_param_set_t aidlKeyParams2Km(const vector<KeyParameter>& keyParams);
72
73class KmParamSet : public keymaster_key_param_set_t {
74 public:
75 explicit KmParamSet(const vector<KeyParameter>& keyParams)
76 : keymaster_key_param_set_t(aidlKeyParams2Km(keyParams)) {}
77
78 KmParamSet(KmParamSet&& other) : keymaster_key_param_set_t{other.params, other.length} {
79 other.length = 0;
80 other.params = nullptr;
81 }
82
83 KmParamSet(const KmParamSet&) = delete;
Shawn Willden96d4e8c2020-12-07 17:03:54 -070084 ~KmParamSet() { keymaster_free_param_set(this); }
Shawn Willden815e8962020-12-11 13:05:27 +000085};
86
87inline vector<uint8_t> kmBlob2vector(const keymaster_key_blob_t& blob) {
88 vector<uint8_t> result(blob.key_material, blob.key_material + blob.key_material_size);
89 return result;
90}
91
92inline vector<uint8_t> kmBlob2vector(const keymaster_blob_t& blob) {
93 vector<uint8_t> result(blob.data, blob.data + blob.data_length);
94 return result;
95}
96
97inline vector<uint8_t> kmBuffer2vector(const ::keymaster::Buffer& buf) {
98 vector<uint8_t> result(buf.peek_read(), buf.peek_read() + buf.available_read());
99 return result;
100}
101
102inline vector<Certificate> kmCertChain2Aidl(const keymaster_cert_chain_t& cert_chain) {
103 vector<Certificate> result;
104 if (!cert_chain.entry_count || !cert_chain.entries) return result;
105
106 result.resize(cert_chain.entry_count);
107 for (size_t i = 0; i < cert_chain.entry_count; ++i) {
108 result[i].encodedCertificate = kmBlob2vector(cert_chain.entries[i]);
109 }
110
111 return result;
112}
113
114template <typename T, typename OutIter>
115inline OutIter copy_bytes_to_iterator(const T& value, OutIter dest) {
116 const uint8_t* value_ptr = reinterpret_cast<const uint8_t*>(&value);
117 return std::copy(value_ptr, value_ptr + sizeof(value), dest);
118}
119
David Drysdale81815912021-04-19 19:11:41 +0100120vector<uint8_t> authToken2AidlVec(const std::optional<HardwareAuthToken>& token);
Shawn Willden815e8962020-12-11 13:05:27 +0000121
Shawn Willden815e8962020-12-11 13:05:27 +0000122inline void addClientAndAppData(const vector<uint8_t>& clientId, const vector<uint8_t>& appData,
123 ::keymaster::AuthorizationSet* params) {
124 params->Clear();
125 if (clientId.size()) {
126 params->push_back(::keymaster::TAG_APPLICATION_ID, clientId.data(), clientId.size());
127 }
128 if (appData.size()) {
129 params->push_back(::keymaster::TAG_APPLICATION_DATA, appData.data(), appData.size());
130 }
131}
132
Shawn Willden96d4e8c2020-12-07 17:03:54 -0700133} // namespace aidl::android::hardware::security::keymint::km_utils