1 /*
2  * Copyright 2017 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 #include <keymaster/logger.h>
18 #include <keymaster/wrapped_key.h>
19 
20 #include <openssl/asn1t.h>
21 
22 #include <keymaster/android_keymaster_utils.h>
23 #include <keymaster/km_openssl/openssl_err.h>
24 #include <keymaster/km_openssl/openssl_utils.h>
25 
26 namespace keymaster {
27 
28 IMPLEMENT_ASN1_FUNCTIONS(KM_WRAPPED_KEY_DESCRIPTION);
29 IMPLEMENT_ASN1_FUNCTIONS(KM_WRAPPED_KEY);
30 
31 struct KM_WRAPPED_KEY_Delete {
operator ()keymaster::KM_WRAPPED_KEY_Delete32     void operator()(KM_WRAPPED_KEY* p) { KM_WRAPPED_KEY_free(p); }
33 };
34 
35 struct KM_WRAPPED_KEY_DESCRIPTION_Delete {
operator ()keymaster::KM_WRAPPED_KEY_DESCRIPTION_Delete36     void operator()(KM_WRAPPED_KEY_DESCRIPTION* p) { KM_WRAPPED_KEY_DESCRIPTION_free(p); }
37 };
38 
39 // DER encode a wrapped key for secure import
build_wrapped_key(const KeymasterKeyBlob & transit_key,const KeymasterBlob & iv,keymaster_key_format_t key_format,const KeymasterKeyBlob & secure_key,const KeymasterBlob & tag,const AuthorizationSet & auth_set,KeymasterKeyBlob * der_wrapped_key)40 keymaster_error_t build_wrapped_key(const KeymasterKeyBlob& transit_key, const KeymasterBlob& iv,
41                                     keymaster_key_format_t key_format,
42                                     const KeymasterKeyBlob& secure_key, const KeymasterBlob& tag,
43                                     const AuthorizationSet& auth_set,
44                                     KeymasterKeyBlob* der_wrapped_key) {
45     UniquePtr<KM_WRAPPED_KEY, KM_WRAPPED_KEY_Delete> wrapped_key(KM_WRAPPED_KEY_new());
46     if (!wrapped_key.get()) return KM_ERROR_MEMORY_ALLOCATION_FAILED;
47 
48     if (!ASN1_OCTET_STRING_set(wrapped_key->transit_key, transit_key.key_material,
49                                transit_key.key_material_size) ||
50         !ASN1_OCTET_STRING_set(wrapped_key->iv, iv.data, iv.data_length) ||
51         !ASN1_OCTET_STRING_set(wrapped_key->secure_key, secure_key.key_material,
52                                secure_key.key_material_size) ||
53         !ASN1_OCTET_STRING_set(wrapped_key->tag, tag.data, tag.data_length) ||
54         !ASN1_INTEGER_set(wrapped_key->wrapped_key_description->key_format, key_format)) {
55         return TranslateLastOpenSslError();
56     }
57 
58     auto err = build_auth_list(auth_set, wrapped_key->wrapped_key_description->auth_list);
59     if (err != KM_ERROR_OK) {
60         return err;
61     }
62 
63     int len = i2d_KM_WRAPPED_KEY(wrapped_key.get(), nullptr);
64     if (len < 0) {
65         return TranslateLastOpenSslError();
66     }
67     if (!der_wrapped_key->Reset(len)) {
68         return KM_ERROR_MEMORY_ALLOCATION_FAILED;
69     }
70 
71     uint8_t* p = der_wrapped_key->writable_data();
72     len = i2d_KM_WRAPPED_KEY(wrapped_key.get(), &p);
73     if (len < 0) {
74         return TranslateLastOpenSslError();
75     }
76 
77     return KM_ERROR_OK;
78 }
79 
80 // Parse the DER-encoded wrapped key format
parse_wrapped_key(const KeymasterKeyBlob & wrapped_key,KeymasterBlob * iv,KeymasterKeyBlob * transit_key,KeymasterKeyBlob * secure_key,KeymasterBlob * tag,AuthorizationSet * auth_list,keymaster_key_format_t * key_format,KeymasterBlob * wrapped_key_description)81 keymaster_error_t parse_wrapped_key(const KeymasterKeyBlob& wrapped_key, KeymasterBlob* iv,
82                                     KeymasterKeyBlob* transit_key, KeymasterKeyBlob* secure_key,
83                                     KeymasterBlob* tag, AuthorizationSet* auth_list,
84                                     keymaster_key_format_t* key_format,
85                                     KeymasterBlob* wrapped_key_description) {
86     if (!iv || !transit_key || !secure_key || !tag || !auth_list || !key_format ||
87         !wrapped_key_description) {
88         return KM_ERROR_UNEXPECTED_NULL_POINTER;
89     }
90 
91     const uint8_t* tmp = wrapped_key.key_material;
92     UniquePtr<KM_WRAPPED_KEY, KM_WRAPPED_KEY_Delete> record(
93         d2i_KM_WRAPPED_KEY(nullptr, &tmp, wrapped_key.key_material_size));
94     if (!record.get()) return TranslateLastOpenSslError();
95 
96     *iv = KeymasterBlob(record->iv->data, record->iv->length);
97     if (record->iv->data && !iv->data) {
98         return KM_ERROR_MEMORY_ALLOCATION_FAILED;
99     }
100 
101     *transit_key = KeymasterKeyBlob(record->transit_key->data, record->transit_key->length);
102     if (record->transit_key->data && !transit_key->key_material) {
103         return KM_ERROR_MEMORY_ALLOCATION_FAILED;
104     }
105 
106     *secure_key = KeymasterKeyBlob(record->secure_key->data, record->secure_key->length);
107     if (record->secure_key->data && !secure_key->key_material) {
108         return KM_ERROR_MEMORY_ALLOCATION_FAILED;
109     }
110 
111     *tag = KeymasterBlob(record->tag->data, record->tag->length);
112     if (record->tag->data && !tag->data) {
113         return KM_ERROR_MEMORY_ALLOCATION_FAILED;
114     }
115 
116     // Re-serialize the wrapped key description
117     int len = i2d_KM_WRAPPED_KEY_DESCRIPTION(record->wrapped_key_description, nullptr);
118     if (len < 0) {
119         return TranslateLastOpenSslError();
120     }
121     if (!wrapped_key_description->Reset(len)) {
122         return KM_ERROR_MEMORY_ALLOCATION_FAILED;
123     }
124     uint8_t* p = wrapped_key_description->writable_data();
125     if (i2d_KM_WRAPPED_KEY_DESCRIPTION(record->wrapped_key_description, &p) < 0) {
126         return TranslateLastOpenSslError();
127     }
128 
129     *key_format = static_cast<keymaster_key_format_t>(
130         ASN1_INTEGER_get(record->wrapped_key_description->key_format));
131     return extract_auth_list(record->wrapped_key_description->auth_list, auth_list);
132 }
133 
134 }  // namespace keymaster
135