• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2014 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/km_openssl/asymmetric_key.h>
18 
19 #include <openssl/asn1.h>
20 #include <openssl/stack.h>
21 #include <openssl/x509.h>
22 #include <openssl/x509v3.h>
23 
24 #include <keymaster/android_keymaster_utils.h>
25 #include <keymaster/keymaster_context.h>
26 #include <keymaster/km_openssl/openssl_err.h>
27 #include <keymaster/km_openssl/openssl_utils.h>
28 
29 namespace keymaster {
30 
formatted_key_material(keymaster_key_format_t format,UniquePtr<uint8_t[]> * material,size_t * size) const31 keymaster_error_t AsymmetricKey::formatted_key_material(keymaster_key_format_t format,
32                                                         UniquePtr<uint8_t[]>* material,
33                                                         size_t* size) const {
34     if (format != KM_KEY_FORMAT_X509) return KM_ERROR_UNSUPPORTED_KEY_FORMAT;
35 
36     if (material == nullptr || size == nullptr) return KM_ERROR_OUTPUT_PARAMETER_NULL;
37 
38     EVP_PKEY_Ptr pkey(EVP_PKEY_new());
39     if (!InternalToEvp(pkey.get())) return TranslateLastOpenSslError();
40 
41     int key_data_length = i2d_PUBKEY(pkey.get(), nullptr);
42     if (key_data_length <= 0) return TranslateLastOpenSslError();
43 
44     material->reset(new (std::nothrow) uint8_t[key_data_length]);
45     if (material->get() == nullptr) return KM_ERROR_MEMORY_ALLOCATION_FAILED;
46 
47     uint8_t* tmp = material->get();
48     if (i2d_PUBKEY(pkey.get(), &tmp) != key_data_length) {
49         material->reset();
50         return TranslateLastOpenSslError();
51     }
52 
53     *size = key_data_length;
54     return KM_ERROR_OK;
55 }
56 
57 }  // namespace keymaster
58